OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library base64; |
| 6 |
| 7 import 'dart:typed_data'; |
| 8 |
| 9 const _decodeTable = |
| 10 const [null, null, null, null, null, null, null, null, |
| 11 null, null, null, null, null, null, null, null, |
| 12 null, null, null, null, null, null, null, null, |
| 13 null, null, null, null, null, null, null, null, |
| 14 null, null, null, null, null, null, null, null, |
| 15 null, null, null, 62, null, null, null, 63, |
| 16 52, 53, 54, 55, 56, 57, 58, 59, |
| 17 60, 61, null, null, null, 0, null, null, |
| 18 null, 0, 1, 2, 3, 4, 5, 6, |
| 19 7, 8, 9, 10, 11, 12, 13, 14, |
| 20 15, 16, 17, 18, 19, 20, 21, 22, |
| 21 23, 24, 25, null, null, null, null, null, |
| 22 null, 26, 27, 28, 29, 30, 31, 32, |
| 23 33, 34, 35, 36, 37, 38, 39, 40, |
| 24 41, 42, 43, 44, 45, 46, 47, 48, |
| 25 49, 50, 51]; |
| 26 |
| 27 Uint8List decodeBase64(String s) { |
| 28 if (s.length % 4 != 0) throw "Malformed Base64: $s"; |
| 29 |
| 30 var odd_bits = 0; |
| 31 if (s[s.length - 1] == '=') { |
| 32 if (s[s.length - 2] == '=') { |
| 33 odd_bits = 2; |
| 34 } else { |
| 35 odd_bits = 1; |
| 36 } |
| 37 } |
| 38 |
| 39 var decodedByteLength = s.length ~/ 4 * 3 - odd_bits; |
| 40 var result = new Uint8List(decodedByteLength); |
| 41 var limit = s.length; |
| 42 if (odd_bits != 0) { |
| 43 limit = limit - 4; |
| 44 } |
| 45 |
| 46 var i = 0, j = 0; |
| 47 while (i < limit) { |
| 48 var triple = _decodeTable[s.codeUnitAt(i++)]; |
| 49 triple = (triple << 6) | _decodeTable[s.codeUnitAt(i++)]; |
| 50 triple = (triple << 6) | _decodeTable[s.codeUnitAt(i++)]; |
| 51 triple = (triple << 6) | _decodeTable[s.codeUnitAt(i++)]; |
| 52 result[j++] = triple >> 16; |
| 53 result[j++] = (triple >> 8) & 255; |
| 54 result[j++] = triple & 255; |
| 55 } |
| 56 |
| 57 if (odd_bits != 0) { |
| 58 var triple = _decodeTable[s.codeUnitAt(i++)]; |
| 59 triple = (triple << 6) | _decodeTable[s.codeUnitAt(i++)]; |
| 60 triple = (triple << 6) | _decodeTable[s.codeUnitAt(i++)]; |
| 61 triple = (triple << 6) | _decodeTable[s.codeUnitAt(i++)]; |
| 62 result[j++] = triple >> 16; |
| 63 if (odd_bits == 1) { |
| 64 result[j++] = (triple >> 8) & 255; |
| 65 } |
| 66 } |
| 67 assert(j == decodedByteLength); |
| 68 return result; |
| 69 } |
OLD | NEW |