Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in base64 encoding escape newlines only when between quotes #491

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/com/fasterxml/jackson/core/Base64Variant.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,13 @@ public String encode(byte[] input, boolean addQuotes)
b24 = (b24 << 8) | (((int) input[inputPtr++]) & 0xFF);
encodeBase64Chunk(sb, b24);
if (--chunksBeforeLF <= 0) {
// note: must quote in JSON value, so not really useful...
sb.append('\\');
sb.append('n');
if (addQuotes) {
// note: must quote in JSON value, so not really useful...
sb.append('\\');
sb.append('n');
} else {
sb.append('\n');
}
chunksBeforeLF = getMaxLineLength() >> 2;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/fasterxml/jackson/core/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ protected String getAndVerifyText(JsonParser p) throws IOException
*/

protected static String quote(String str) {
return '"'+str+'"';
return '"'+str.replace("\n","\\n")+'"';
}

protected static String aposToQuotes(String json) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.fasterxml.jackson.core.base64;

import java.util.Random;

import org.junit.Assert;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.Base64Variant;
import com.fasterxml.jackson.core.Base64Variants;

public class Base64CodecTest
extends com.fasterxml.jackson.core.BaseTest
{
public void testVariantAccess()
{
for (Base64Variant var : new Base64Variant[] {
extends com.fasterxml.jackson.core.BaseTest {
public void testVariantAccess() {
for (Base64Variant var : new Base64Variant[]{
Base64Variants.MIME,
Base64Variants.MIME_NO_LINEFEEDS,
Base64Variants.MODIFIED_FOR_URL,
Expand All @@ -26,8 +27,7 @@ public void testVariantAccess()
}
}

public void testProps()
{
public void testProps() {
Base64Variant std = Base64Variants.MIME;
// let's verify basic props of std cocec
assertEquals("MIME", std.getName());
Expand All @@ -40,8 +40,7 @@ public void testProps()
assertEquals(76, std.getMaxLineLength());
}

public void testCharEncoding() throws Exception
{
public void testCharEncoding() throws Exception {
Base64Variant std = Base64Variants.MIME;
assertEquals(Base64Variant.BASE64_VALUE_INVALID, std.decodeBase64Char('?'));
assertEquals(Base64Variant.BASE64_VALUE_INVALID, std.decodeBase64Char((int) '?'));
Expand All @@ -50,15 +49,15 @@ public void testCharEncoding() throws Exception

assertEquals(Base64Variant.BASE64_VALUE_INVALID, std.decodeBase64Byte((byte) '?'));
assertEquals(Base64Variant.BASE64_VALUE_INVALID, std.decodeBase64Byte((byte) 0xA0));

assertEquals(0, std.decodeBase64Char('A'));
assertEquals(1, std.decodeBase64Char((int) 'B'));
assertEquals(2, std.decodeBase64Char((byte)'C'));
assertEquals(2, std.decodeBase64Char((byte) 'C'));

assertEquals(0, std.decodeBase64Byte((byte) 'A'));
assertEquals(1, std.decodeBase64Byte((byte) 'B'));
assertEquals(2, std.decodeBase64Byte((byte)'C'));
assertEquals(2, std.decodeBase64Byte((byte) 'C'));

assertEquals('/', std.encodeBase64BitsAsChar(63));
assertEquals((byte) 'b', std.encodeBase64BitsAsByte(27));

Expand All @@ -74,21 +73,44 @@ public void testCharEncoding() throws Exception
Assert.assertArrayEquals(exp, act);
}

public void testConvenienceMethods() throws Exception
{
public void testConvenienceMethods() throws Exception {
Base64Variant std = Base64Variants.MIME;

byte[] input = new byte[] { 1, 2, 34, 127, -1 };
byte[] input = new byte[]{1, 2, 34, 127, -1};
String encoded = std.encode(input, false);
byte[] decoded = std.decode(encoded);
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);
}


public void testEncodeDecodeBigArray() throws Exception {
Base64Variant std = Base64Variants.MIME;

byte[] input = new byte[500];
new Random().nextBytes(input);

String encoded = std.encode(input, false);
byte[] decoded = std.decode(encoded);
byte[] decoded = std.decode(encoded);
Assert.assertArrayEquals(input, decoded);

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

// [core#414]: check white-space allow too
decoded = std.decode("\n"+encoded);
decoded = std.decode("\n" + encoded);
Assert.assertArrayEquals(input, decoded);
decoded = std.decode(" "+encoded);
decoded = std.decode(" " + encoded);
Assert.assertArrayEquals(input, decoded);
decoded = std.decode(encoded + " ");
Assert.assertArrayEquals(input, decoded);
Expand All @@ -97,8 +119,7 @@ public void testConvenienceMethods() throws Exception
}

@SuppressWarnings("unused")
public void testErrors() throws Exception
{
public void testErrors() throws Exception {
try {
Base64Variant b = new Base64Variant("foobar", "xyz", false, '!', 24);
fail("Should not pass");
Expand Down