Skip to content

Commit

Permalink
Fix #414
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 15, 2017
1 parent 96633b7 commit d0a34b2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.fasterxml.jackson</groupId>
<!-- For 2.9.2 and beyond, new parent pom; extends jackson-bom -->
<artifactId>jackson-base</artifactId>
<version>2.9.3</version>
<version>2.9.4-SNAPSHOT</version>
</parent>

<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
5 changes: 5 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ JSON library.
=== Releases ===
------------------------------------------------------------------------

2.9.4 (not yet released)

#414: Base64 MIME variant does not ignore white space chars as per RFC2045
(reporte by tmoschou@github)

2.9.3 (09-Dec-2017)

#419: `ArrayIndexOutOfBoundsException` from `UTF32Reader.read()` on invalid input
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/fasterxml/jackson/core/Base64Variant.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.fasterxml.jackson.core.util.ByteArrayBuilder;

/**
* Abstract base class used to define specific details of which
* Class used to define specific details of which
* variant of Base64 encoding/decoding is to be used. Although there is
* somewhat standard basic version (so-called "MIME Base64"), other variants
* exists, see <a href="http://en.wikipedia.org/wiki/Base64">Base64 Wikipedia entry</a> for details.
Expand Down Expand Up @@ -457,13 +457,17 @@ public void decode(String str, ByteArrayBuilder builder) throws IllegalArgumentE
{
int ptr = 0;
int len = str.length();

while (ptr < len) {

main_loop:
while (true) {
// first, we'll skip preceding white space, if any
char ch;
do {
if (ptr >= len) {
break main_loop;
}
ch = str.charAt(ptr++);
} while ((ptr < len) && (ch <= INT_SPACE));
} while (ch <= INT_SPACE);
int bits = decodeBase64Char(ch);
if (bits < 0) {
_reportInvalidBase64(ch, 0, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public void testStreaming() throws IOException
public void testSimple() throws IOException
{
for (int mode : ALL_MODES) {
_testSimple(mode);
// [core#414]: Allow leading/trailign white-space, ensure it is accepted
_testSimple(mode, false, false);
_testSimple(mode, true, false);
_testSimple(mode, false, true);
_testSimple(mode, true, true);
}
}

Expand Down Expand Up @@ -233,23 +237,27 @@ private void _testStreaming(int mode) throws IOException
}
}

private void _testSimple(int mode)
throws IOException
private void _testSimple(int mode, boolean leadingWS, boolean trailingWS) throws IOException
{
/* The usual sample input string, from Thomas Hobbes's "Leviathan"
* (via Wikipedia)
*/
// The usual sample input string, from Thomas Hobbes's "Leviathan"
// (via Wikipedia)
final String RESULT = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
final byte[] RESULT_BYTES = RESULT.getBytes("US-ASCII");

// And here's what should produce it...
final String INPUT_STR =
String INPUT_STR =
"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz"
+"IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg"
+"dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu"
+"dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo"
+"ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="
;
if (leadingWS) {
INPUT_STR = " "+INPUT_STR;
}
if (leadingWS) {
INPUT_STR = INPUT_STR+" ";
}

final String DOC = "\""+INPUT_STR+"\"";
JsonParser p = createParser(mode, DOC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ public void testConvenienceMethods() throws Exception
Assert.assertArrayEquals(input, decoded);

assertEquals(quote(encoded), std.encode(input, true));

// [core#414]: check white-space allow too
decoded = std.decode("\n"+encoded);
Assert.assertArrayEquals(input, decoded);
decoded = std.decode(" "+encoded);
Assert.assertArrayEquals(input, decoded);
decoded = std.decode(encoded + " ");
Assert.assertArrayEquals(input, decoded);
decoded = std.decode(encoded + "\n");
Assert.assertArrayEquals(input, decoded);
}

@SuppressWarnings("unused")
Expand Down

0 comments on commit d0a34b2

Please sign in to comment.