| 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 import 'dart:convert'; | |
| 6 | |
| 7 import 'package:charcode/ascii.dart'; | |
| 8 | |
| 9 import 'encoder_sink.dart'; | |
| 10 | |
| 11 /// A String representing a mapping from numbers between 0 and 63, inclusive, to | |
| 12 /// their corresponding encoded character. | |
| 13 /// | |
| 14 /// This is the table for URL-safe encodings. | |
| 15 const _encodeTableUrlSafe = | |
| 16 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; | |
| 17 | |
| 18 /// A String representing a mapping from numbers between 0 and 63, inclusive, to | |
| 19 /// their corresponding encoded character. | |
| 20 /// | |
| 21 /// This is the table for URL-unsafe encodings. | |
| 22 const _encodeTable = | |
| 23 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
| 24 | |
| 25 /// The line length for Base64 strings with line separators. | |
| 26 const _lineLength = 76; | |
| 27 | |
| 28 /// This is deprecated. | |
| 29 /// | |
| 30 /// Use the `Base64Encoder` class in `dart:convert` instead. | |
| 31 @Deprecated("Will be removed in crypto 1.0.0.") | |
| 32 class Base64Encoder extends Converter<List<int>, String> { | |
| 33 /// Whether this encoder generates URL-safe strings. | |
| 34 final bool _urlSafe; | |
| 35 | |
| 36 /// Whether this encoder adds line breaks to the output. | |
| 37 final bool _addLineSeparator; | |
| 38 | |
| 39 /// The sequence of bytes to use as a padding character. | |
| 40 final List<int> _pad; | |
| 41 | |
| 42 /// Creates a new [Base64Encoder]. | |
| 43 /// | |
| 44 /// The default [BASE64.encoder] will be good enough for most cases. A new | |
| 45 /// codec only needs to be instantiated when you want to do multiple | |
| 46 /// conversions with the same configuration. | |
| 47 /// | |
| 48 /// If [urlSafe] is `true`, a URL-safe alphabet will be used. Specifically, | |
| 49 /// the characters `-` and `_` will be used instead of `+` and `/`. | |
| 50 /// | |
| 51 /// If [addLineSeparator] is `true`, `\r\n` line separators will be added | |
| 52 /// every 76 characters. | |
| 53 /// | |
| 54 /// If [encodePaddingCharacter] is `true`, the padding character `=` will be | |
| 55 /// written as `%3D`. | |
| 56 const Base64Encoder( | |
| 57 {bool urlSafe: false, | |
| 58 bool addLineSeparator: false, | |
| 59 bool encodePaddingCharacter: false}) | |
| 60 : _urlSafe = urlSafe, | |
| 61 _addLineSeparator = addLineSeparator, | |
| 62 _pad = encodePaddingCharacter | |
| 63 ? const [$percent, $3, $D] | |
| 64 : const [$equal]; | |
| 65 | |
| 66 /// Converts [bytes] to Base64. | |
| 67 /// | |
| 68 /// If [start] and [end] are provided, only the sublist `bytes.sublist(start, | |
| 69 /// end)` is converted. | |
| 70 String convert(List<int> bytes, [int start = 0, int end]) { | |
| 71 RangeError.checkValidRange(start, end, bytes.length); | |
| 72 if (end == null) end = bytes.length; | |
| 73 | |
| 74 var length = end - start; | |
| 75 if (length == 0) return ""; | |
| 76 | |
| 77 var lookup = _urlSafe ? _encodeTableUrlSafe : _encodeTable; | |
| 78 | |
| 79 // The total length of the 24-bit chunks. | |
| 80 var remainderLength = length.remainder(3); | |
| 81 var chunkLength = length - remainderLength; | |
| 82 | |
| 83 // The size of the base output. | |
| 84 var baseOutputLength = (length ~/ 3) * 4; | |
| 85 var remainderOutputLength = remainderLength > 0 ? 3 + _pad.length : 0; | |
| 86 | |
| 87 var outputLength = baseOutputLength + remainderOutputLength; | |
| 88 if (_addLineSeparator) { | |
| 89 // Add extra expected length to account for line separators. | |
| 90 outputLength += ((outputLength - 1) ~/ _lineLength) * 2; | |
| 91 } | |
| 92 var out = new List<int>(outputLength); | |
| 93 | |
| 94 // Encode 24 bit chunks. | |
| 95 var input = start; | |
| 96 var output = 0; | |
| 97 var chunks = 0; | |
| 98 while (input < chunkLength) { | |
| 99 // Get a 24-bit chunk from the next three input bytes. Mask each byte to | |
| 100 // make sure we don't do something bad if the user passes in non-byte | |
| 101 // ints. | |
| 102 var chunk = (bytes[input++] << 16) & 0x00FF0000; | |
| 103 chunk |= (bytes[input++] << 8) & 0x0000FF00; | |
| 104 chunk |= bytes[input++] & 0x000000FF; | |
| 105 | |
| 106 // Split the 24-bit chunk into four 6-bit sections to encode as | |
| 107 // characters. | |
| 108 out[output++] = lookup.codeUnitAt(chunk >> 18); | |
| 109 out[output++] = lookup.codeUnitAt((chunk >> 12) & 0x3F); | |
| 110 out[output++] = lookup.codeUnitAt((chunk >> 6) & 0x3F); | |
| 111 out[output++] = lookup.codeUnitAt(chunk & 0x3F); | |
| 112 | |
| 113 // Add an optional line separator for every 76 characters we emit; that | |
| 114 // is, every 19 chunks. | |
| 115 chunks++; | |
| 116 if (_addLineSeparator && chunks == 19 && output < outputLength - 2) { | |
| 117 out[output++] = $cr; | |
| 118 out[output++] = $lf; | |
| 119 chunks = 0; | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 // If the input length isn't a multiple of 3, encode the remaining bytes and | |
| 124 // add padding. | |
| 125 if (remainderLength == 1) { | |
| 126 var byte = bytes[input]; | |
| 127 out[output++] = lookup.codeUnitAt(byte >> 2); | |
| 128 out[output++] = lookup.codeUnitAt((byte << 4) & 0x3F); | |
| 129 out.setRange(output, output + _pad.length, _pad); | |
| 130 out.setRange(output + _pad.length, output + 2 * _pad.length, _pad); | |
| 131 } else if (remainderLength == 2) { | |
| 132 var byte1 = bytes[input++]; | |
| 133 var byte2 = bytes[input]; | |
| 134 out[output++] = lookup.codeUnitAt(byte1 >> 2); | |
| 135 out[output++] = lookup.codeUnitAt(((byte1 << 4) | (byte2 >> 4)) & 0x3F); | |
| 136 out[output++] = lookup.codeUnitAt((byte2 << 2) & 0x3F); | |
| 137 out.setRange(output, output + _pad.length, _pad); | |
| 138 } | |
| 139 | |
| 140 return new String.fromCharCodes(out); | |
| 141 } | |
| 142 | |
| 143 Base64EncoderSink startChunkedConversion(Sink<String> sink) { | |
| 144 StringConversionSink stringSink = sink is StringConversionSink | |
| 145 ? sink | |
| 146 : new StringConversionSink.from(sink); | |
| 147 | |
| 148 return new Base64EncoderSink(stringSink, _urlSafe, _addLineSeparator); | |
| 149 } | |
| 150 } | |
| OLD | NEW |