perf: decode Base64 with bounded lookup-table groups

This commit is contained in:
qaiu committed 2026-11-20 19:37:04 +08:00
1 parent 8c07c6e657
commit 5681d3b805
3 files changed
+103 -30

No files matched your search

@@ -1,49 +1,85 @@
package cn.qaiu.util;
/** Strict Base64 decoding without the HotSpot Base64 intrinsic (CVE-2026-114938). */
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
/**
* Strict Base64 decoding without the HotSpot Base64 intrinsic (CVE-2026-114938).
* Like the JDK's String decoder, this reads ISO-8859-1 bytes.
* Unmappable characters become '?', which is rejected by both alphabets.
*/
public final class Base64Decoder {
private static final String BASIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static final String URL_SAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
private static final byte[] BASIC = alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
private static final byte[] URL_SAFE = alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
private Base64Decoder() {
}
public static byte[] decode(String input) {
return decode(input, BASIC);
return decode(input.getBytes(StandardCharsets.ISO_8859_1), BASIC);
}
public static byte[] decodeUrl(String input) {
return decode(input, URL_SAFE);
return decode(input.getBytes(StandardCharsets.ISO_8859_1), URL_SAFE);
}
private static byte[] decode(String input, String alphabet) {
int length = input.length();
int firstPadding = input.indexOf('=');
int dataLength = firstPadding < 0 ? length : firstPadding;
private static byte[] decode(byte[] input, byte[] alphabet) {
int length = input.length;
int dataLength = length;
if (dataLength > 0 && input[dataLength - 1] == '=') {
dataLength--;
if (dataLength > 0 && input[dataLength - 1] == '=') {
dataLength--;
}
}
int remainder = dataLength % 4;
if (remainder == 1 || (firstPadding >= 0
&& (remainder == 0 || length % 4 != 0
|| length - dataLength != 4 - remainder
|| input.charAt(length - 1) != '='))) {
int padding = length - dataLength;
if (remainder == 1 || (padding != 0 && remainder + padding != 4)) {
throw new IllegalArgumentException("Invalid Base64 padding or length");
}
byte[] decoded = new byte[dataLength / 4 * 3 + remainder * 6 / 8];
int buffer = 0;
int bits = 0;
int groupEnd = dataLength - remainder;
byte[] decoded = new byte[(groupEnd / 4) * 3 + (remainder == 0 ? 0 : remainder - 1)];
int outputIndex = 0;
for (int i = 0; i < dataLength; i++) {
int value = alphabet.indexOf(input.charAt(i));
if (value < 0) {
// Each complete group consumes exactly four characters and writes exactly three bytes.
for (int i = 0; i < groupEnd; i += 4) {
int a = alphabet[input[i] & 0xff];
int b = alphabet[input[i + 1] & 0xff];
int c = alphabet[input[i + 2] & 0xff];
int d = alphabet[input[i + 3] & 0xff];
if ((a | b | c | d) < 0) {
throw new IllegalArgumentException("Invalid Base64 character");
}
buffer = (buffer << 6) | value;
bits += 6;
if (bits >= 8) {
bits -= 8;
decoded[outputIndex++] = (byte) (buffer >>> bits);
int bits = (a << 18) | (b << 12) | (c << 6) | d;
decoded[outputIndex++] = (byte) (bits >>> 16);
decoded[outputIndex++] = (byte) (bits >>> 8);
decoded[outputIndex++] = (byte) bits;
}
// The two possible tails consume two or three characters and write one or two bytes.
if (remainder != 0) {
int a = alphabet[input[groupEnd] & 0xff];
int b = alphabet[input[groupEnd + 1] & 0xff];
int c = remainder == 3 ? alphabet[input[groupEnd + 2] & 0xff] : 0;
if ((a | b | c) < 0) {
throw new IllegalArgumentException("Invalid Base64 character");
}
decoded[outputIndex] = (byte) ((a << 2) | (b >>> 4));
if (remainder == 3) {
decoded[outputIndex + 1] = (byte) ((b << 4) | (c >>> 2));
}
}
return decoded;
}
private static byte[] alphabet(String characters) {
// Index with an unsigned byte. Every non-alphabet value, including padding, maps to -1.
byte[] table = new byte[256];
Arrays.fill(table, (byte) -1);
for (int i = 0; i < characters.length(); i++) {
table[characters.charAt(i)] = (byte) i;
}
return table;
}
}