| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
|
| 21 |
import java.io.UnsupportedEncodingException; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class Base64 { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public static final int DEFAULT = 0; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public static final int NO_PADDING = 1; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static final int NO_WRAP = 2; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static final int CRLF = 4; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public static final int URL_SAFE = 8; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static final int NO_CLOSE = 16; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static abstract class Coder { |
| 74 |
public byte[] output; |
| 75 |
public int op; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public abstract boolean process(byte[] input, int offset, int len, boolean finish); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public abstract int maxOutputSize(int len); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public static byte[] decode(String str, int flags) { |
| 120 |
return decode(str.getBytes(), flags); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public static byte[] decode(byte[] input, int flags) { |
| 138 |
return decode(input, 0, input.length, flags); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public static byte[] decode(byte[] input, int offset, int len, int flags) { |
| 158 |
|
| 159 |
|
| 160 |
Decoder decoder = new Decoder(flags, new byte[len*3/4]); |
| 161 |
|
| 162 |
if (!decoder.process(input, offset, len, true)) { |
| 163 |
throw new IllegalArgumentException("bad base-64"); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
if (decoder.op == decoder.output.length) { |
| 168 |
return decoder.output; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
byte[] temp = new byte[decoder.op]; |
| 174 |
System.arraycopy(decoder.output, 0, temp, 0, decoder.op); |
| 175 |
return temp; |
| 176 |
} |
| 177 |
|
| 178 |
static class Decoder extends Coder { |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
private static final int DECODE[] = { |
| 184 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 185 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 186 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, |
| 187 |
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, |
| 188 |
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 189 |
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
| 190 |
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 191 |
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, |
| 192 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 193 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 194 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 195 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 196 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 197 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 198 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 199 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 200 |
}; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
private static final int DECODE_WEBSAFE[] = { |
| 207 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 208 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 209 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, |
| 210 |
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, |
| 211 |
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 212 |
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, |
| 213 |
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 214 |
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, |
| 215 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 216 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 217 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 218 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 219 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 220 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 221 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 222 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 223 |
}; |
| 224 |
|
| 225 |
|
| 226 |
private static final int SKIP = -1; |
| 227 |
private static final int EQUALS = -2; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
private int state; |
| 239 |
private int value; |
| 240 |
|
| 241 |
final private int[] alphabet; |
| 242 |
|
| 243 |
public Decoder(int flags, byte[] output) { |
| 244 |
this.output = output; |
| 245 |
|
| 246 |
alphabet = ((flags & URL_SAFE) == 0) ? DECODE : DECODE_WEBSAFE; |
| 247 |
state = 0; |
| 248 |
value = 0; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public int maxOutputSize(int len) { |
| 256 |
return len * 3/4 + 10; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public boolean process(byte[] input, int offset, int len, boolean finish) { |
| 266 |
if (this.state == 6) return false; |
| 267 |
|
| 268 |
int p = offset; |
| 269 |
len += offset; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
int state = this.state; |
| 277 |
int value = this.value; |
| 278 |
int op = 0; |
| 279 |
final byte[] output = this.output; |
| 280 |
final int[] alphabet = this.alphabet; |
| 281 |
|
| 282 |
while (p < len) { |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
if (state == 0) { |
| 298 |
while (p+4 <= len && |
| 299 |
(value = ((alphabet[input[p] & 0xff] << 18) | |
| 300 |
(alphabet[input[p+1] & 0xff] << 12) | |
| 301 |
(alphabet[input[p+2] & 0xff] << 6) | |
| 302 |
(alphabet[input[p+3] & 0xff]))) >= 0) { |
| 303 |
output[op+2] = (byte) value; |
| 304 |
output[op+1] = (byte) (value >> 8); |
| 305 |
output[op] = (byte) (value >> 16); |
| 306 |
op += 3; |
| 307 |
p += 4; |
| 308 |
} |
| 309 |
if (p >= len) break; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
int d = alphabet[input[p++] & 0xff]; |
| 318 |
|
| 319 |
switch (state) { |
| 320 |
case 0: |
| 321 |
if (d >= 0) { |
| 322 |
value = d; |
| 323 |
++state; |
| 324 |
} else if (d != SKIP) { |
| 325 |
this.state = 6; |
| 326 |
return false; |
| 327 |
} |
| 328 |
break; |
| 329 |
|
| 330 |
case 1: |
| 331 |
if (d >= 0) { |
| 332 |
value = (value << 6) | d; |
| 333 |
++state; |
| 334 |
} else if (d != SKIP) { |
| 335 |
this.state = 6; |
| 336 |
return false; |
| 337 |
} |
| 338 |
break; |
| 339 |
|
| 340 |
case 2: |
| 341 |
if (d >= 0) { |
| 342 |
value = (value << 6) | d; |
| 343 |
++state; |
| 344 |
} else if (d == EQUALS) { |
| 345 |
|
| 346 |
|
| 347 |
output[op++] = (byte) (value >> 4); |
| 348 |
state = 4; |
| 349 |
} else if (d != SKIP) { |
| 350 |
this.state = 6; |
| 351 |
return false; |
| 352 |
} |
| 353 |
break; |
| 354 |
|
| 355 |
case 3: |
| 356 |
if (d >= 0) { |
| 357 |
|
| 358 |
value = (value << 6) | d; |
| 359 |
output[op+2] = (byte) value; |
| 360 |
output[op+1] = (byte) (value >> 8); |
| 361 |
output[op] = (byte) (value >> 16); |
| 362 |
op += 3; |
| 363 |
state = 0; |
| 364 |
} else if (d == EQUALS) { |
| 365 |
|
| 366 |
|
| 367 |
output[op+1] = (byte) (value >> 2); |
| 368 |
output[op] = (byte) (value >> 10); |
| 369 |
op += 2; |
| 370 |
state = 5; |
| 371 |
} else if (d != SKIP) { |
| 372 |
this.state = 6; |
| 373 |
return false; |
| 374 |
} |
| 375 |
break; |
| 376 |
|
| 377 |
case 4: |
| 378 |
if (d == EQUALS) { |
| 379 |
++state; |
| 380 |
} else if (d != SKIP) { |
| 381 |
this.state = 6; |
| 382 |
return false; |
| 383 |
} |
| 384 |
break; |
| 385 |
|
| 386 |
case 5: |
| 387 |
if (d != SKIP) { |
| 388 |
this.state = 6; |
| 389 |
return false; |
| 390 |
} |
| 391 |
break; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
if (!finish) { |
| 396 |
|
| 397 |
|
| 398 |
this.state = state; |
| 399 |
this.value = value; |
| 400 |
this.op = op; |
| 401 |
return true; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
switch (state) { |
| 408 |
case 0: |
| 409 |
|
| 410 |
break; |
| 411 |
case 1: |
| 412 |
|
| 413 |
|
| 414 |
this.state = 6; |
| 415 |
return false; |
| 416 |
case 2: |
| 417 |
|
| 418 |
|
| 419 |
output[op++] = (byte) (value >> 4); |
| 420 |
break; |
| 421 |
case 3: |
| 422 |
|
| 423 |
|
| 424 |
output[op++] = (byte) (value >> 10); |
| 425 |
output[op++] = (byte) (value >> 2); |
| 426 |
break; |
| 427 |
case 4: |
| 428 |
|
| 429 |
this.state = 6; |
| 430 |
return false; |
| 431 |
case 5: |
| 432 |
|
| 433 |
|
| 434 |
break; |
| 435 |
} |
| 436 |
|
| 437 |
this.state = state; |
| 438 |
this.op = op; |
| 439 |
return true; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public static String encodeToString(byte[] input, int flags) { |
| 457 |
try { |
| 458 |
return new String(encode(input, flags), "US-ASCII"); |
| 459 |
} catch (UnsupportedEncodingException e) { |
| 460 |
|
| 461 |
throw new AssertionError(e); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
public static String encodeToString(byte[] input, int offset, int len, int flags) { |
| 478 |
try { |
| 479 |
return new String(encode(input, offset, len, flags), "US-ASCII"); |
| 480 |
} catch (UnsupportedEncodingException e) { |
| 481 |
|
| 482 |
throw new AssertionError(e); |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
public static byte[] encode(byte[] input, int flags) { |
| 496 |
return encode(input, 0, input.length, flags); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public static byte[] encode(byte[] input, int offset, int len, int flags) { |
| 512 |
Encoder encoder = new Encoder(flags, null); |
| 513 |
|
| 514 |
|
| 515 |
int output_len = len / 3 * 4; |
| 516 |
|
| 517 |
|
| 518 |
if (encoder.do_padding) { |
| 519 |
if (len % 3 > 0) { |
| 520 |
output_len += 4; |
| 521 |
} |
| 522 |
} else { |
| 523 |
switch (len % 3) { |
| 524 |
case 0: break; |
| 525 |
case 1: output_len += 2; break; |
| 526 |
case 2: output_len += 3; break; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
if (encoder.do_newline && len > 0) { |
| 532 |
output_len += (((len-1) / (3 * Encoder.LINE_GROUPS)) + 1) * |
| 533 |
(encoder.do_cr ? 2 : 1); |
| 534 |
} |
| 535 |
|
| 536 |
encoder.output = new byte[output_len]; |
| 537 |
encoder.process(input, offset, len, true); |
| 538 |
|
| 539 |
assert encoder.op == output_len; |
| 540 |
|
| 541 |
return encoder.output; |
| 542 |
} |
| 543 |
|
| 544 |
static class Encoder extends Coder { |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
public static final int LINE_GROUPS = 19; |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
private static final byte ENCODE[] = { |
| 557 |
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 558 |
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 559 |
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 560 |
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', |
| 561 |
}; |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
private static final byte ENCODE_WEBSAFE[] = { |
| 568 |
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 569 |
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 570 |
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 571 |
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_', |
| 572 |
}; |
| 573 |
|
| 574 |
final private byte[] tail; |
| 575 |
int tailLen; |
| 576 |
private int count; |
| 577 |
|
| 578 |
final public boolean do_padding; |
| 579 |
final public boolean do_newline; |
| 580 |
final public boolean do_cr; |
| 581 |
final private byte[] alphabet; |
| 582 |
|
| 583 |
public Encoder(int flags, byte[] output) { |
| 584 |
this.output = output; |
| 585 |
|
| 586 |
do_padding = (flags & NO_PADDING) == 0; |
| 587 |
do_newline = (flags & NO_WRAP) == 0; |
| 588 |
do_cr = (flags & CRLF) != 0; |
| 589 |
alphabet = ((flags & URL_SAFE) == 0) ? ENCODE : ENCODE_WEBSAFE; |
| 590 |
|
| 591 |
tail = new byte[2]; |
| 592 |
tailLen = 0; |
| 593 |
|
| 594 |
count = do_newline ? LINE_GROUPS : -1; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
public int maxOutputSize(int len) { |
| 602 |
return len * 8/5 + 10; |
| 603 |
} |
| 604 |
|
| 605 |
public boolean process(byte[] input, int offset, int len, boolean finish) { |
| 606 |
|
| 607 |
final byte[] alphabet = this.alphabet; |
| 608 |
final byte[] output = this.output; |
| 609 |
int op = 0; |
| 610 |
int count = this.count; |
| 611 |
|
| 612 |
int p = offset; |
| 613 |
len += offset; |
| 614 |
int v = -1; |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
switch (tailLen) { |
| 621 |
case 0: |
| 622 |
|
| 623 |
break; |
| 624 |
|
| 625 |
case 1: |
| 626 |
if (p+2 <= len) { |
| 627 |
|
| 628 |
|
| 629 |
v = ((tail[0] & 0xff) << 16) | |
| 630 |
((input[p++] & 0xff) << 8) | |
| 631 |
(input[p++] & 0xff); |
| 632 |
tailLen = 0; |
| 633 |
}; |
| 634 |
break; |
| 635 |
|
| 636 |
case 2: |
| 637 |
if (p+1 <= len) { |
| 638 |
|
| 639 |
v = ((tail[0] & 0xff) << 16) | |
| 640 |
((tail[1] & 0xff) << 8) | |
| 641 |
(input[p++] & 0xff); |
| 642 |
tailLen = 0; |
| 643 |
} |
| 644 |
break; |
| 645 |
} |
| 646 |
|
| 647 |
if (v != -1) { |
| 648 |
output[op++] = alphabet[(v >> 18) & 0x3f]; |
| 649 |
output[op++] = alphabet[(v >> 12) & 0x3f]; |
| 650 |
output[op++] = alphabet[(v >> 6) & 0x3f]; |
| 651 |
output[op++] = alphabet[v & 0x3f]; |
| 652 |
if (--count == 0) { |
| 653 |
if (do_cr) output[op++] = '\r'; |
| 654 |
output[op++] = '\n'; |
| 655 |
count = LINE_GROUPS; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
while (p+3 <= len) { |
| 665 |
v = ((input[p] & 0xff) << 16) | |
| 666 |
((input[p+1] & 0xff) << 8) | |
| 667 |
(input[p+2] & 0xff); |
| 668 |
output[op] = alphabet[(v >> 18) & 0x3f]; |
| 669 |
output[op+1] = alphabet[(v >> 12) & 0x3f]; |
| 670 |
output[op+2] = alphabet[(v >> 6) & 0x3f]; |
| 671 |
output[op+3] = alphabet[v & 0x3f]; |
| 672 |
p += 3; |
| 673 |
op += 4; |
| 674 |
if (--count == 0) { |
| 675 |
if (do_cr) output[op++] = '\r'; |
| 676 |
output[op++] = '\n'; |
| 677 |
count = LINE_GROUPS; |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
if (finish) { |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
if (p-tailLen == len-1) { |
| 688 |
int t = 0; |
| 689 |
v = ((tailLen > 0 ? tail[t++] : input[p++]) & 0xff) << 4; |
| 690 |
tailLen -= t; |
| 691 |
output[op++] = alphabet[(v >> 6) & 0x3f]; |
| 692 |
output[op++] = alphabet[v & 0x3f]; |
| 693 |
if (do_padding) { |
| 694 |
output[op++] = '='; |
| 695 |
output[op++] = '='; |
| 696 |
} |
| 697 |
if (do_newline) { |
| 698 |
if (do_cr) output[op++] = '\r'; |
| 699 |
output[op++] = '\n'; |
| 700 |
} |
| 701 |
} else if (p-tailLen == len-2) { |
| 702 |
int t = 0; |
| 703 |
v = (((tailLen > 1 ? tail[t++] : input[p++]) & 0xff) << 10) | |
| 704 |
(((tailLen > 0 ? tail[t++] : input[p++]) & 0xff) << 2); |
| 705 |
tailLen -= t; |
| 706 |
output[op++] = alphabet[(v >> 12) & 0x3f]; |
| 707 |
output[op++] = alphabet[(v >> 6) & 0x3f]; |
| 708 |
output[op++] = alphabet[v & 0x3f]; |
| 709 |
if (do_padding) { |
| 710 |
output[op++] = '='; |
| 711 |
} |
| 712 |
if (do_newline) { |
| 713 |
if (do_cr) output[op++] = '\r'; |
| 714 |
output[op++] = '\n'; |
| 715 |
} |
| 716 |
} else if (do_newline && op > 0 && count != LINE_GROUPS) { |
| 717 |
if (do_cr) output[op++] = '\r'; |
| 718 |
output[op++] = '\n'; |
| 719 |
} |
| 720 |
|
| 721 |
assert tailLen == 0; |
| 722 |
assert p == len; |
| 723 |
} else { |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
if (p == len-1) { |
| 728 |
tail[tailLen++] = input[p]; |
| 729 |
} else if (p == len-2) { |
| 730 |
tail[tailLen++] = input[p]; |
| 731 |
tail[tailLen++] = input[p+1]; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
this.op = op; |
| 736 |
this.count = count; |
| 737 |
|
| 738 |
return true; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
@UnsupportedAppUsage |
| 743 |
private Base64() { } |
| 744 |
} |