| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.dev.util; |
| 7 |
|
| 8 |
import java.util.Random; |
| 9 |
import java.util.concurrent.TimeUnit; |
| 10 |
|
| 11 |
/** |
| 12 |
* This class converts binary to base64 and vice versa. |
| 13 |
*/ |
| 14 |
public class Base64 { |
| 15 |
|
| 16 |
private static final byte[] CODE = new byte[64]; |
| 17 |
private static final byte[] REV = new byte[256]; |
| 18 |
|
| 19 |
private Base64() { |
| 20 |
// utility class |
| 21 |
} |
| 22 |
|
| 23 |
static { |
| 24 |
for (int i = 'A'; i <= 'Z'; i++) { |
| 25 |
CODE[i - 'A'] = (byte) i; |
| 26 |
CODE[i - 'A' + 26] = (byte) (i + 'a' - 'A'); |
| 27 |
} |
| 28 |
for (int i = 0; i < 10; i++) { |
| 29 |
CODE[i + 2 * 26] = (byte) ('0' + i); |
| 30 |
} |
| 31 |
CODE[62] = (byte) '+'; |
| 32 |
CODE[63] = (byte) '/'; |
| 33 |
for (int i = 0; i < 255; i++) { |
| 34 |
REV[i] = -1; |
| 35 |
} |
| 36 |
for (int i = 0; i < 64; i++) { |
| 37 |
REV[CODE[i]] = (byte) i; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
private static void check(String a, String b) { |
| 42 |
if (!a.equals(b)) { |
| 43 |
throw new RuntimeException("mismatch: " + a + " <> " + b); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Run the tests. |
| 49 |
* |
| 50 |
* @param args the command line parameters |
| 51 |
*/ |
| 52 |
public static void main(String... args) { |
| 53 |
check(new String(encode(new byte[] {})), ""); |
| 54 |
check(new String(encode("A".getBytes())), "QQ=="); |
| 55 |
check(new String(encode("AB".getBytes())), "QUI="); |
| 56 |
check(new String(encode("ABC".getBytes())), "QUJD"); |
| 57 |
check(new String(encode("ABCD".getBytes())), "QUJDRA=="); |
| 58 |
check(new String(decode(new byte[] {})), ""); |
| 59 |
check(new String(decode("QQ==".getBytes())), "A"); |
| 60 |
check(new String(decode("QUI=".getBytes())), "AB"); |
| 61 |
check(new String(decode("QUJD".getBytes())), "ABC"); |
| 62 |
check(new String(decode("QUJDRA==".getBytes())), "ABCD"); |
| 63 |
int len = 10000; |
| 64 |
test(false, len); |
| 65 |
test(true, len); |
| 66 |
test(false, len); |
| 67 |
test(true, len); |
| 68 |
} |
| 69 |
|
| 70 |
private static void test(boolean fast, int len) { |
| 71 |
Random random = new Random(10); |
| 72 |
long time = System.nanoTime(); |
| 73 |
byte[] bin = new byte[len]; |
| 74 |
random.nextBytes(bin); |
| 75 |
for (int i = 0; i < len; i++) { |
| 76 |
byte[] dec; |
| 77 |
if (fast) { |
| 78 |
byte[] enc = encodeFast(bin); |
| 79 |
dec = decodeFast(enc); |
| 80 |
} else { |
| 81 |
byte[] enc = encode(bin); |
| 82 |
dec = decode(enc); |
| 83 |
} |
| 84 |
test(bin, dec); |
| 85 |
} |
| 86 |
time = System.nanoTime() - time; |
| 87 |
System.out.println("fast=" + fast + " time=" + TimeUnit.NANOSECONDS.toMillis(time)); |
| 88 |
} |
| 89 |
|
| 90 |
private static void test(byte[] in, byte[] out) { |
| 91 |
if (in.length != out.length) { |
| 92 |
throw new RuntimeException("Length error"); |
| 93 |
} |
| 94 |
for (int i = 0; i < in.length; i++) { |
| 95 |
if (in[i] != out[i]) { |
| 96 |
throw new RuntimeException("Error at " + i); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
private static byte[] encode(byte[] bin) { |
| 102 |
byte[] code = CODE; |
| 103 |
int size = bin.length; |
| 104 |
int len = ((size + 2) / 3) * 4; |
| 105 |
byte[] enc = new byte[len]; |
| 106 |
int fast = size / 3 * 3, i = 0, j = 0; |
| 107 |
for (; i < fast; i += 3, j += 4) { |
| 108 |
int a = ((bin[i] & 255) << 16) + ((bin[i + 1] & 255) << 8) + (bin[i + 2] & 255); |
| 109 |
enc[j] = code[a >> 18]; |
| 110 |
enc[j + 1] = code[(a >> 12) & 63]; |
| 111 |
enc[j + 2] = code[(a >> 6) & 63]; |
| 112 |
enc[j + 3] = code[a & 63]; |
| 113 |
} |
| 114 |
if (i < size) { |
| 115 |
int a = (bin[i++] & 255) << 16; |
| 116 |
enc[j] = code[a >> 18]; |
| 117 |
if (i < size) { |
| 118 |
a += (bin[i] & 255) << 8; |
| 119 |
enc[j + 2] = code[(a >> 6) & 63]; |
| 120 |
} else { |
| 121 |
enc[j + 2] = (byte) '='; |
| 122 |
} |
| 123 |
enc[j + 1] = code[(a >> 12) & 63]; |
| 124 |
enc[j + 3] = (byte) '='; |
| 125 |
} |
| 126 |
return enc; |
| 127 |
} |
| 128 |
|
| 129 |
private static byte[] encodeFast(byte[] bin) { |
| 130 |
byte[] code = CODE; |
| 131 |
int size = bin.length; |
| 132 |
int len = ((size * 4) + 2) / 3; |
| 133 |
byte[] enc = new byte[len]; |
| 134 |
int fast = size / 3 * 3, i = 0, j = 0; |
| 135 |
for (; i < fast; i += 3, j += 4) { |
| 136 |
int a = ((bin[i] & 255) << 16) + ((bin[i + 1] & 255) << 8) + (bin[i + 2] & 255); |
| 137 |
enc[j] = code[a >> 18]; |
| 138 |
enc[j + 1] = code[(a >> 12) & 63]; |
| 139 |
enc[j + 2] = code[(a >> 6) & 63]; |
| 140 |
enc[j + 3] = code[a & 63]; |
| 141 |
} |
| 142 |
if (i < size) { |
| 143 |
int a = (bin[i++] & 255) << 16; |
| 144 |
enc[j] = code[a >> 18]; |
| 145 |
if (i < size) { |
| 146 |
a += (bin[i] & 255) << 8; |
| 147 |
enc[j + 2] = code[(a >> 6) & 63]; |
| 148 |
} |
| 149 |
enc[j + 1] = code[(a >> 12) & 63]; |
| 150 |
} |
| 151 |
return enc; |
| 152 |
} |
| 153 |
|
| 154 |
private static byte[] trim(byte[] enc) { |
| 155 |
byte[] rev = REV; |
| 156 |
int j = 0, size = enc.length; |
| 157 |
if (size > 1 && enc[size - 2] == '=') { |
| 158 |
size--; |
| 159 |
} |
| 160 |
if (size > 0 && enc[size - 1] == '=') { |
| 161 |
size--; |
| 162 |
} |
| 163 |
for (int i = 0; i < size; i++) { |
| 164 |
if (rev[enc[i] & 255] < 0) { |
| 165 |
j++; |
| 166 |
} |
| 167 |
} |
| 168 |
if (j == 0) { |
| 169 |
return enc; |
| 170 |
} |
| 171 |
byte[] buff = new byte[size - j]; |
| 172 |
for (int i = 0, k = 0; i < size; i++) { |
| 173 |
int x = enc[i] & 255; |
| 174 |
if (rev[x] >= 0) { |
| 175 |
buff[k++] = (byte) x; |
| 176 |
} |
| 177 |
} |
| 178 |
return buff; |
| 179 |
} |
| 180 |
|
| 181 |
private static byte[] decode(byte[] enc) { |
| 182 |
enc = trim(enc); |
| 183 |
byte[] rev = REV; |
| 184 |
int len = enc.length, size = (len * 3) / 4; |
| 185 |
if (len > 0 && enc[len - 1] == '=') { |
| 186 |
size--; |
| 187 |
if (len > 1 && enc[len - 2] == '=') { |
| 188 |
size--; |
| 189 |
} |
| 190 |
} |
| 191 |
byte[] bin = new byte[size]; |
| 192 |
int fast = size / 3 * 3, i = 0, j = 0; |
| 193 |
for (; i < fast; i += 3, j += 4) { |
| 194 |
int a = (rev[enc[j] & 255] << 18) + (rev[enc[j + 1] & 255] << 12) + |
| 195 |
(rev[enc[j + 2] & 255] << 6) + rev[enc[j + 3] & 255]; |
| 196 |
bin[i] = (byte) (a >> 16); |
| 197 |
bin[i + 1] = (byte) (a >> 8); |
| 198 |
bin[i + 2] = (byte) a; |
| 199 |
} |
| 200 |
if (i < size) { |
| 201 |
int a = (rev[enc[j] & 255] << 10) + (rev[enc[j + 1] & 255] << 4); |
| 202 |
bin[i++] = (byte) (a >> 8); |
| 203 |
if (i < size) { |
| 204 |
a += rev[enc[j + 2] & 255] >> 2; |
| 205 |
bin[i] = (byte) a; |
| 206 |
} |
| 207 |
} |
| 208 |
return bin; |
| 209 |
} |
| 210 |
|
| 211 |
private static byte[] decodeFast(byte[] enc) { |
| 212 |
byte[] rev = REV; |
| 213 |
int len = enc.length, size = (len * 3) / 4; |
| 214 |
byte[] bin = new byte[size]; |
| 215 |
int fast = size / 3 * 3, i = 0, j = 0; |
| 216 |
for (; i < fast; i += 3, j += 4) { |
| 217 |
int a = (rev[enc[j] & 255] << 18) + (rev[enc[j + 1] & 255] << 12) + |
| 218 |
(rev[enc[j + 2] & 255] << 6) + rev[enc[j + 3] & 255]; |
| 219 |
bin[i] = (byte) (a >> 16); |
| 220 |
bin[i + 1] = (byte) (a >> 8); |
| 221 |
bin[i + 2] = (byte) a; |
| 222 |
} |
| 223 |
if (i < size) { |
| 224 |
int a = (rev[enc[j] & 255] << 10) + (rev[enc[j + 1] & 255] << 4); |
| 225 |
bin[i++] = (byte) (a >> 8); |
| 226 |
if (i < size) { |
| 227 |
a += rev[enc[j + 2] & 255] >> 2; |
| 228 |
bin[i] = (byte) a; |
| 229 |
} |
| 230 |
} |
| 231 |
return bin; |
| 232 |
} |
| 233 |
|
| 234 |
} |