Skip to content

Commit 8e601f5

Browse files
committed
[CODEC-234] Base32.decode should support lowercase letters.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1809441 13f79535-47bb-0310-9956-ffa450edef68
1 parent c18b192 commit 8e601f5

3 files changed

Lines changed: 67 additions & 2 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ The <action> type attribute can be add,update,fix,remove.
4545
<release version="1.11" date="2017-MM-DD" description="Feature and fix release.">
4646
<!-- The first attribute below should be the issue id; makes it easier to navigate in the IDE outline -->
4747

48+
<action issue="CODEC-234" dev="ggregory" type="update" due-to="Christopher Schultz, Sebb">Base32.decode should support lowercase letters</action>
4849
<action issue="CODEC-233" dev="sebb" type="update" due-to="Yossi Tamari">Soundex should support more algorithm variants</action>
4950
<action issue="CODEC-145" dev="sebb" type="fix" due-to="Jesse Glick">Base64.encodeBase64String could better use newStringUsAscii (ditto encodeBase64URLSafeString)</action>
5051
<action issue="CODEC-144" dev="sebb" type="fix">BaseNCodec: encodeToString and encodeAsString methods are identical</action>

src/main/java/org/apache/commons/codec/binary/Base32.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public class Base32 extends BaseNCodec {
7272
-1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, // 30-3f 2-7
7373
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 40-4f A-O
7474
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 50-5a P-Z
75+
-1, -1, -1, -1, -1, // 5b - 5f
76+
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 60 - 6f a-o
77+
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 70 - 7a p-z/**/
7578
};
7679

7780
/**
@@ -96,7 +99,10 @@ public class Base32 extends BaseNCodec {
9699
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20-2f
97100
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 30-3f 2-7
98101
-1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 40-4f A-O
99-
25, 26, 27, 28, 29, 30, 31, // 50-57 P-V
102+
25, 26, 27, 28, 29, 30, 31, // 50-56 P-V
103+
-1, -1, -1, -1, -1, -1, -1, -1, -1, // 57-5f Z-_
104+
-1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 60-6f `-o
105+
25, 26, 27, 28, 29, 30, 31 // 70-76 p-v
100106
};
101107

102108
/**

src/test/java/org/apache/commons/codec/binary/Base32Test.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
package org.apache.commons.codec.binary;
2020

2121
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertArrayEquals;
2223
import static org.junit.Assert.assertNotNull;
2324
import static org.junit.Assert.fail;
2425

2526
import java.nio.charset.Charset;
2627
import java.util.Arrays;
2728

2829
import org.apache.commons.codec.Charsets;
30+
import org.apache.commons.codec.DecoderException;
2931
import org.apache.commons.lang3.ArrayUtils;
3032
import org.junit.Test;
3133

@@ -44,6 +46,26 @@ public class Base32Test {
4446
{"foobar" ,"MZXW6YTBOI======"},
4547
};
4648

49+
private static final Object[][] BASE32_BINARY_TEST_CASES;
50+
51+
// { null, "O0o0O0o0" }
52+
// BASE32_BINARY_TEST_CASES[2][0] = new Hex().decode("739ce739ce");
53+
54+
static {
55+
Hex hex = new Hex();
56+
try {
57+
BASE32_BINARY_TEST_CASES = new Object[][] {
58+
new Object[] { hex.decode("623a01735836e9a126e12fbf95e013ee6892997c"),
59+
"MI5AC42YG3U2CJXBF67ZLYAT5ZUJFGL4" },
60+
new Object[] { hex.decode("623a01735836e9a126e12fbf95e013ee6892997c"),
61+
"mi5ac42yg3u2cjxbf67zlyat5zujfgl4" },
62+
new Object[] { hex.decode("739ce42108"),
63+
"OOOOIIII" }
64+
};
65+
} catch (DecoderException de) {
66+
throw new Error(":(", de);
67+
}
68+
}
4769
private static final String [][] BASE32HEX_TEST_CASES = { // RFC 4648
4870
{"" ,""},
4971
{"f" ,"CO======"},
@@ -54,7 +76,6 @@ public class Base32Test {
5476
{"foobar" ,"CPNMUOJ1E8======"},
5577
};
5678

57-
5879
private static final String [][] BASE32_TEST_CASES_CHUNKED = { //Chunked
5980
{"" ,""},
6081
{"f" ,"MY======\r\n"},
@@ -116,6 +137,22 @@ public void testBase32HexSamples() throws Exception {
116137
}
117138
}
118139

140+
@Test
141+
public void testBase32HexSamplesReverse() throws Exception {
142+
final Base32 codec = new Base32(true);
143+
for (final String[] element : BASE32HEX_TEST_CASES) {
144+
assertEquals(element[0], new String(codec.decode(element[1]), CHARSET_UTF8));
145+
}
146+
}
147+
148+
@Test
149+
public void testBase32HexSamplesReverseLowercase() throws Exception {
150+
final Base32 codec = new Base32(true);
151+
for (final String[] element : BASE32HEX_TEST_CASES) {
152+
assertEquals(element[0], new String(codec.decode(element[1].toLowerCase()), CHARSET_UTF8));
153+
}
154+
}
155+
119156
@Test
120157
public void testBase32Samples() throws Exception {
121158
final Base32 codec = new Base32();
@@ -124,6 +161,27 @@ public void testBase32Samples() throws Exception {
124161
}
125162
}
126163

164+
@Test
165+
public void testBase32BinarySamples() throws Exception {
166+
final Base32 codec = new Base32();
167+
for (final Object[] element : BASE32_BINARY_TEST_CASES) {
168+
String expected;
169+
if(element.length > 2)
170+
expected = (String)element[2];
171+
else
172+
expected = (String)element[1];
173+
assertEquals(expected.toUpperCase(), codec.encodeAsString((byte[])element[0]));
174+
}
175+
}
176+
177+
@Test
178+
public void testBase32BinarySamplesReverse() throws Exception {
179+
final Base32 codec = new Base32();
180+
for (final Object[] element : BASE32_BINARY_TEST_CASES) {
181+
assertArrayEquals((byte[])element[0], codec.decode((String)element[1]));
182+
}
183+
}
184+
127185
@Test
128186
public void testBase32SamplesNonDefaultPadding() throws Exception {
129187
final Base32 codec = new Base32((byte)0x25); // '%' <=> 0x25

0 commit comments

Comments
 (0)