OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of crypto; | 5 part of crypto; |
6 | 6 |
7 const Base64Codec BASE64 = const Base64Codec(); | 7 const Base64Codec BASE64 = const Base64Codec(); |
8 | 8 |
9 const List<int> _decodeTable = | 9 const List<int> _decodeTable = |
10 const [ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -1, -2, -2, | 10 const [ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -1, -2, -2, |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 class Base64Decoder extends Converter<String, List<int>> { | 271 class Base64Decoder extends Converter<String, List<int>> { |
272 | 272 |
273 /** | 273 /** |
274 * Instantiates a new [Base64Decoder] | 274 * Instantiates a new [Base64Decoder] |
275 */ | 275 */ |
276 const Base64Decoder(); | 276 const Base64Decoder(); |
277 | 277 |
278 List<int> convert(String input) { | 278 List<int> convert(String input) { |
279 int length = input.length; | 279 int length = input.length; |
280 if (length == 0) { | 280 if (length == 0) { |
281 return new List<int>(0); | 281 return new Uint8List<int>(0); |
282 } | 282 } |
283 | 283 |
284 int normalLength = 0; | 284 int normalLength = 0; |
285 int i = 0; | 285 int i = 0; |
286 // Count '\r', '\n' and illegal characters, check if | 286 // Count '\r', '\n' and illegal characters, check if |
287 // '/', '+' / '-', '_' are used consistently, for illegal characters, | 287 // '/', '+' / '-', '_' are used consistently, for illegal characters, |
288 // throw an exception. | 288 // throw an exception. |
289 | 289 |
290 while (i < length) { | 290 while (i < length) { |
291 int codeUnit = input.codeUnitAt(i); | 291 int codeUnit = input.codeUnitAt(i); |
(...skipping 30 matching lines...) Expand all Loading... |
322 padLength++; | 322 padLength++; |
323 i -= 2; | 323 i -= 2; |
324 } else if (_decodeTable[currentCodeUnit] > 0) { | 324 } else if (_decodeTable[currentCodeUnit] > 0) { |
325 break; | 325 break; |
326 } else if (currentCodeUnit == _PAD_BYTES[0]) { | 326 } else if (currentCodeUnit == _PAD_BYTES[0]) { |
327 padLength++; | 327 padLength++; |
328 } | 328 } |
329 i--; | 329 i--; |
330 } | 330 } |
331 int outputLength = ((normalLength * 6) >> 3) - padLength; | 331 int outputLength = ((normalLength * 6) >> 3) - padLength; |
332 List<int> out = new List<int>(outputLength); | 332 List<int> out = new Uint8List<int>(outputLength); |
333 | 333 |
334 for (int i = 0, o = 0; o < outputLength; ) { | 334 for (int i = 0, o = 0; o < outputLength; ) { |
335 // Accumulate 4 valid 6 bit Base 64 characters into an int. | 335 // Accumulate 4 valid 6 bit Base 64 characters into an int. |
336 int x = 0; | 336 int x = 0; |
337 for (int j = 4; j > 0; ) { | 337 for (int j = 4; j > 0; ) { |
338 int c = _decodeTable[input.codeUnitAt(i++)]; | 338 int c = _decodeTable[input.codeUnitAt(i++)]; |
339 if (c >= 0) { | 339 if (c >= 0) { |
340 x = ((x << 6) & 0x00FFFFFF) | c; | 340 x = ((x << 6) & 0x00FFFFFF) | c; |
341 j--; | 341 j--; |
342 } | 342 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 | 390 |
391 void close() { | 391 void close() { |
392 if (_unconverted.isNotEmpty) { | 392 if (_unconverted.isNotEmpty) { |
393 _outSink.add(_decoder.convert(_unconverted)); | 393 _outSink.add(_decoder.convert(_unconverted)); |
394 } | 394 } |
395 _outSink.close(); | 395 _outSink.close(); |
396 } | 396 } |
397 } | 397 } |
398 | 398 |
399 | 399 |
OLD | NEW |