| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package com.adobe.internal.xmp.impl; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class Base64 |
| 23 |
{ |
| 24 |
|
| 25 |
private static final byte INVALID = -1; |
| 26 |
|
| 27 |
private static final byte WHITESPACE = -2; |
| 28 |
|
| 29 |
private static final byte EQUAL = -3; |
| 30 |
|
| 31 |
|
| 32 |
private static byte[] base64 = { |
| 33 |
(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', |
| 34 |
(byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', |
| 35 |
(byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', |
| 36 |
(byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', |
| 37 |
(byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', |
| 38 |
(byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', |
| 39 |
(byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', |
| 40 |
(byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', |
| 41 |
(byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', |
| 42 |
(byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', |
| 43 |
(byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', |
| 44 |
(byte) 's', (byte) 't', (byte) 'u', (byte) 'v', |
| 45 |
(byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z', |
| 46 |
(byte) '0', (byte) '1', (byte) '2', (byte) '3', |
| 47 |
(byte) '4', (byte) '5', (byte) '6', (byte) '7', |
| 48 |
(byte) '8', (byte) '9', (byte) '+', (byte) '/' |
| 49 |
}; |
| 50 |
|
| 51 |
private static byte[] ascii = new byte[255]; |
| 52 |
|
| 53 |
static { |
| 54 |
|
| 55 |
for (int idx = 0; idx < 255; idx++) |
| 56 |
{ |
| 57 |
ascii[idx] = INVALID; |
| 58 |
} |
| 59 |
|
| 60 |
for (int idx = 0; idx < base64.length; idx++) |
| 61 |
{ |
| 62 |
ascii[base64[idx]] = (byte) idx; |
| 63 |
} |
| 64 |
|
| 65 |
ascii[0x09] = WHITESPACE; |
| 66 |
ascii[0x0A] = WHITESPACE; |
| 67 |
ascii[0x0D] = WHITESPACE; |
| 68 |
ascii[0x20] = WHITESPACE; |
| 69 |
|
| 70 |
|
| 71 |
ascii[0x3d] = EQUAL; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public static final byte[] encode(byte[] src) |
| 82 |
{ |
| 83 |
return encode(src, 0); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static final byte[] encode(byte[] src, int lineFeed) |
| 96 |
{ |
| 97 |
|
| 98 |
lineFeed = lineFeed / 4 * 4; |
| 99 |
if (lineFeed < 0) |
| 100 |
{ |
| 101 |
lineFeed = 0; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
int codeLength = ((src.length + 2) / 3) * 4; |
| 106 |
if (lineFeed > 0) |
| 107 |
{ |
| 108 |
codeLength += (codeLength - 1) / lineFeed; |
| 109 |
} |
| 110 |
|
| 111 |
byte[] dst = new byte[codeLength]; |
| 112 |
int bits24; |
| 113 |
int bits6; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
int didx = 0; |
| 118 |
int sidx = 0; |
| 119 |
int lf = 0; |
| 120 |
while (sidx + 3 <= src.length) |
| 121 |
{ |
| 122 |
bits24 = (src[sidx++] & 0xFF) << 16; |
| 123 |
bits24 |= (src[sidx++] & 0xFF) << 8; |
| 124 |
bits24 |= (src[sidx++] & 0xFF) << 0; |
| 125 |
bits6 = (bits24 & 0x00FC0000) >> 18; |
| 126 |
dst[didx++] = base64[bits6]; |
| 127 |
bits6 = (bits24 & 0x0003F000) >> 12; |
| 128 |
dst[didx++] = base64[bits6]; |
| 129 |
bits6 = (bits24 & 0x00000FC0) >> 6; |
| 130 |
dst[didx++] = base64[bits6]; |
| 131 |
bits6 = (bits24 & 0x0000003F); |
| 132 |
dst[didx++] = base64[bits6]; |
| 133 |
|
| 134 |
lf += 4; |
| 135 |
if (didx < codeLength && lineFeed > 0 && lf % lineFeed == 0) |
| 136 |
{ |
| 137 |
dst[didx++] = 0x0A; |
| 138 |
} |
| 139 |
} |
| 140 |
if (src.length - sidx == 2) |
| 141 |
{ |
| 142 |
bits24 = (src[sidx ] & 0xFF) << 16; |
| 143 |
bits24 |= (src[sidx + 1] & 0xFF) << 8; |
| 144 |
bits6 = (bits24 & 0x00FC0000) >> 18; |
| 145 |
dst[didx++] = base64[bits6]; |
| 146 |
bits6 = (bits24 & 0x0003F000) >> 12; |
| 147 |
dst[didx++] = base64[bits6]; |
| 148 |
bits6 = (bits24 & 0x00000FC0) >> 6; |
| 149 |
dst[didx++] = base64[bits6]; |
| 150 |
dst[didx++] = (byte) '='; |
| 151 |
} |
| 152 |
else if (src.length - sidx == 1) |
| 153 |
{ |
| 154 |
bits24 = (src[sidx] & 0xFF) << 16; |
| 155 |
bits6 = (bits24 & 0x00FC0000) >> 18; |
| 156 |
dst[didx++] = base64[bits6]; |
| 157 |
bits6 = (bits24 & 0x0003F000) >> 12; |
| 158 |
dst[didx++] = base64[bits6]; |
| 159 |
dst[didx++] = (byte) '='; |
| 160 |
dst[didx++] = (byte) '='; |
| 161 |
} |
| 162 |
return dst; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public static final String encode(String src) |
| 172 |
{ |
| 173 |
return new String(encode(src.getBytes())); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public static final byte[] decode(byte[] src) throws IllegalArgumentException |
| 187 |
{ |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
int sidx; |
| 192 |
int srcLen = 0; |
| 193 |
for (sidx = 0; sidx < src.length; sidx++) |
| 194 |
{ |
| 195 |
byte val = ascii[src[sidx]]; |
| 196 |
if (val >= 0) |
| 197 |
{ |
| 198 |
src[srcLen++] = val; |
| 199 |
} |
| 200 |
else if (val == INVALID) |
| 201 |
{ |
| 202 |
throw new IllegalArgumentException("Invalid base 64 string"); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
while (srcLen > 0 && src[srcLen - 1] == EQUAL) |
| 210 |
{ |
| 211 |
srcLen--; |
| 212 |
} |
| 213 |
byte[] dst = new byte[srcLen * 3 / 4]; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
int didx; |
| 219 |
for (sidx = 0, didx = 0; didx < dst.length - 2; sidx += 4, didx += 3) |
| 220 |
{ |
| 221 |
dst[didx ] = (byte) (((src[sidx ] << 2) & 0xFF) |
| 222 |
| ((src[sidx + 1] >>> 4) & 0x03)); |
| 223 |
dst[didx + 1] = (byte) (((src[sidx + 1] << 4) & 0xFF) |
| 224 |
| ((src[sidx + 2] >>> 2) & 0x0F)); |
| 225 |
dst[didx + 2] = (byte) (((src[sidx + 2] << 6) & 0xFF) |
| 226 |
| ((src[sidx + 3]) & 0x3F)); |
| 227 |
} |
| 228 |
if (didx < dst.length) |
| 229 |
{ |
| 230 |
dst[didx] = (byte) (((src[sidx ] << 2) & 0xFF) |
| 231 |
| ((src[sidx + 1] >>> 4) & 0x03)); |
| 232 |
} |
| 233 |
if (++didx < dst.length) |
| 234 |
{ |
| 235 |
dst[didx] = (byte) (((src[sidx + 1] << 4) & 0xFF) |
| 236 |
| ((src[sidx + 2] >>> 2) & 0x0F)); |
| 237 |
} |
| 238 |
return dst; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public static final String decode(String src) |
| 249 |
{ |
| 250 |
return new String(decode(src.getBytes())); |
| 251 |
} |
| 252 |
} |