Skip to content

Commit

Permalink
[NETBEANS-5427] Accept non-printable characters in Base64Encoder.deco…
Browse files Browse the repository at this point in the history
…de().

And use j.u.Base64.getMimeDecoder() where it's been replaced.
  • Loading branch information
piercemar committed Apr 16, 2021
1 parent 7b89e9f commit 9203b07
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void testDecodePassword() throws Exception {
}

public void testDecodeBase64() {
final byte[] data = "P4sswørd".getBytes(StandardCharsets.UTF_8);
final byte[] data = "P4ssw\u00f8rd".getBytes(StandardCharsets.UTF_8);
final String encoded = "UDRzc3fDuHJk";
final byte[] result = DatabaseConnectionConvertor.decodeBase64(encoded);
assertEquals(data.length, result.length);
Expand Down
2 changes: 1 addition & 1 deletion ide/diff/src/org/netbeans/modules/diff/builtin/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
class Base64 {

private static final java.util.Base64.Decoder DECODER = java.util.Base64.getDecoder();
private static final java.util.Base64.Decoder DECODER = java.util.Base64.getMimeDecoder();

private Base64() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,6 @@ private char scramble(char ch) {
}

private byte[] decode(String str) {
return Base64.getDecoder().decode(str);
return Base64.getMimeDecoder().decode(str);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,6 @@ private char scramble(char ch) {
}

private byte[] decode(String str) {
return Base64.getDecoder().decode(str);
return Base64.getMimeDecoder().decode(str);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.openide.nodes.Node;
import org.openide.awt.StatusDisplayer;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.List;
import org.netbeans.modules.subversion.client.SvnClientExceptionHandler;
Expand Down Expand Up @@ -356,23 +357,10 @@ private String exportBinaryFile(File file) throws IOException {
}

static String encodeToWrappedBase64(byte[] data) {
return wrapText(Base64.getEncoder().encodeToString(data), 60, System.getProperty("line.separator"));
return Base64.getMimeEncoder(
60,
System.getProperty("line.separator").getBytes(StandardCharsets.ISO_8859_1)
).encodeToString(data);
}

static String wrapText(String text, int length, String separator) {
if (length > 0) {
StringBuilder sb = new StringBuilder(text.length() + (((text.length() - 1) / length) * separator.length()));
int idx = 0;
while (idx < text.length()) {
if (idx > 0) {
sb.append(separator);
}
sb.append(text.substring(idx, Math.min(idx + length, text.length())));
idx += length;
}
return sb.toString();
} else {
return text;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,4 @@ public void testEncodeToWrappedBase64() {
assertEquals(expected, ExportDiffAction.encodeToWrappedBase64(data));
}

/**
* Test of wrapText method, of class ExportDiffAction.
*/
@Test
public void testWrapText() {
final String data = "Wrap this line in chunks of 5!";
final String wrapped = "Wrap \nthis \nline \nin ch\nunks \nof 5!";
assertEquals(wrapped, ExportDiffAction.wrapText(data, 5, "\n"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.netbeans.modules.proxy;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
Expand All @@ -29,41 +30,44 @@
@Deprecated
public class Base64Encoder {

private static final Base64.Encoder MIME_ENCODER = Base64.getMimeEncoder(
60,
System.getProperty("line.separator").getBytes(StandardCharsets.ISO_8859_1)
);

private Base64Encoder() {
}

/**
*
* @deprecated use {@link java.util.Base64#getEncoder()}.encode(data)
* instead.
*/
@Deprecated
public static String encode(byte[] data) {
return encode(data, false);
}

/**
* @deprecated use
* {@link java.util.Base64#getMimeEncoder(int, byte[])}.encode(s) instead.
*/
public static String encode(byte[] data, boolean useNewlines) {
final String encoded = Base64.getEncoder().encodeToString(data);
if (useNewlines) {
return wrapText(encoded, 60, System.getProperty("line.separator"));
} else {
return encoded;
}
}

static String wrapText(String text, int length, String separator) {
if (length > 0) {
StringBuilder sb = new StringBuilder(text.length() + (((text.length() - 1) / length) * separator.length()));
int idx = 0;
while (idx < text.length()) {
if (idx > 0) {
sb.append(separator);
}
sb.append(text.substring(idx, Math.min(idx + length, text.length())));
idx += length;
}
return sb.toString();
return MIME_ENCODER.encodeToString(data);
} else {
return text;
return Base64.getEncoder().encodeToString(data);
}
}

/**
*
* @deprecated use {@link java.util.Base64#getMimeDecoder()}.decode(s)
* instead.
*/
@Deprecated
public static byte[] decode(String s) {
return Base64.getDecoder().decode(s);
return Base64.getMimeDecoder().decode(s);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,30 @@ public void testEncode_byteArr_boolean_wrap() {
}

/**
* Test of wrapText method, of class Base64Encoder.
* Test of decode method, of class Base64Encoder.
*/
@Test
public void testWrapText() {
final String data = "Wrap this line in chunks of 5!";
final String wrapped = "Wrap \nthis \nline \nin ch\nunks \nof 5!";
assertEquals(wrapped, Base64Encoder.wrapText(data, 5, "\n"));
public void testDecode() {
final String encoded = "VGhpcyBzdHJpbmcgaGFzIGJlZW4gZGVjb2RlZCBmcm9tIEJhc2U2NA==";
final byte[] expected = ("This string has been decoded from Base64")
.getBytes(StandardCharsets.UTF_8);

assertArrayEquals(expected, Base64Encoder.decode(encoded));
}

/**
/**
* Test of decode method, of class Base64Encoder.
*/
@Test
public void testDecode() {
final String encoded = "VGhpcyBzdHJpbmcgaGFzIGJlZW4gZGVjb2RlZCBmcm9tIEJhc2U2NA==";
public void testDecode_nonPrintable() {
final String encoded = "\tVGhpcyBzdHJpbmcg\n"
+ " aGFzIGJlZW4gZGVj\r"
+ "b2RlZCBmcm9tIEJh"
+ "c2U2NA== ";
final byte[] expected = ("This string has been decoded from Base64")
.getBytes(StandardCharsets.UTF_8);

assertArrayEquals(expected, Base64Encoder.decode(encoded));
}

}

0 comments on commit 9203b07

Please sign in to comment.