Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: lib/src/base64.dart

Issue 1350913002: Run the formatter over crypto. (Closed) Base URL: git@github.com:dart-lang/crypto.git@master
Patch Set: Fix a missing value. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/crypto.dart ('k') | lib/src/crypto_utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 = const [
10 const [ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -1, -2, -2, 10 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -1, -2, -2,
11 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 11 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
12 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, 62, -2, 63, 12 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, 62, -2, 63,
13 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, 0, -2, -2, 13 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, 0, -2, -2,
14 -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14 -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, 63, 15 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, 63,
16 -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 16 -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
17 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, 17 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
18 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 18 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
19 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 19 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
20 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 20 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
21 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 21 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
22 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 22 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
23 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 23 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
24 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 24 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
25 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2 ]; 25 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
26 ];
26 27
27 const String _encodeTableUrlSafe = 28 const String _encodeTableUrlSafe =
28 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; 29 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
29 30
30 const String _encodeTable = 31 const String _encodeTable =
31 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 32 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
32 33
33 const int _LINE_LENGTH = 76; 34 const int _LINE_LENGTH = 76;
34 const int _CR = 13; // '\r' 35 const int _CR = 13; // '\r'
35 const int _LF = 10; // '\n' 36 const int _LF = 10; // '\n'
36 const List<int> _PAD_BYTES = const [61]; // '=' 37 const List<int> _PAD_BYTES = const [61]; // '='
37 const List<int> _ENCODED_PAD_BYTES = const [37, 51, 68]; // '%3D' 38 const List<int> _ENCODED_PAD_BYTES = const [37, 51, 68]; // '%3D'
38 const String _PAD = "="; 39 const String _PAD = "=";
39 const String _ENCODED_PAD = "%3D"; 40 const String _ENCODED_PAD = "%3D";
40 41
41 class Base64Codec extends Codec<List<int>, String> { 42 class Base64Codec extends Codec<List<int>, String> {
42
43 final bool _urlSafe; 43 final bool _urlSafe;
44 final bool _addLineSeparator; 44 final bool _addLineSeparator;
45 final bool _encodePaddingCharacter; 45 final bool _encodePaddingCharacter;
46 46
47 /** 47 /**
48 * Instantiates a new [Base64Codec]. 48 * Instantiates a new [Base64Codec].
49 * 49 *
50 * The optional [urlSafe] argument specifies if [encoder] and [encode] 50 * The optional [urlSafe] argument specifies if [encoder] and [encode]
51 * should generate a string, that is safe to use in an URL. 51 * should generate a string, that is safe to use in an URL.
52 * 52 *
53 * If [urlSafe] is `true` (and not overriden at the method invocation) 53 * If [urlSafe] is `true` (and not overriden at the method invocation)
54 * the [encoder] and [encode] use '-' instead of '+' and '_' instead of '/'. 54 * the [encoder] and [encode] use '-' instead of '+' and '_' instead of '/'.
55 * 55 *
56 * The default value of [urlSafe] is `false`. 56 * The default value of [urlSafe] is `false`.
57 * 57 *
58 * The optional [addLineSeparator] argument specifies if the [encoder] and 58 * The optional [addLineSeparator] argument specifies if the [encoder] and
59 * [encode] should add line separators. 59 * [encode] should add line separators.
60 * 60 *
61 * If `addLineSeparator` is `true` [encode] adds an 61 * If `addLineSeparator` is `true` [encode] adds an
62 * optional line separator (CR + LF) for each 76 char output. 62 * optional line separator (CR + LF) for each 76 char output.
63 * 63 *
64 * The default value of [addLineSeparator] if `false`. 64 * The default value of [addLineSeparator] if `false`.
65 * 65 *
66 * If [encodePaddingCharacter] is `true` `encode` converts `=` to `%3D`. 66 * If [encodePaddingCharacter] is `true` `encode` converts `=` to `%3D`.
67 * The default value of [encodePaddingCharacter] is `false`. 67 * The default value of [encodePaddingCharacter] is `false`.
68 */ 68 */
69 const Base64Codec({bool urlSafe: false, 69 const Base64Codec(
70 bool addLineSeparator: false, 70 {bool urlSafe: false,
71 bool encodePaddingCharacter: false}) 71 bool addLineSeparator: false,
72 bool encodePaddingCharacter: false})
72 : _urlSafe = urlSafe, 73 : _urlSafe = urlSafe,
73 _addLineSeparator = addLineSeparator, 74 _addLineSeparator = addLineSeparator,
74 _encodePaddingCharacter = encodePaddingCharacter; 75 _encodePaddingCharacter = encodePaddingCharacter;
75 76
76 String get name => "base64"; 77 String get name => "base64";
77 78
78 String encode(List<int> bytes, 79 String encode(List<int> bytes,
79 {bool urlSafe, 80 {bool urlSafe, bool addLineSeparator, bool encodePaddingCharacter}) {
80 bool addLineSeparator,
81 bool encodePaddingCharacter}) {
82 if (urlSafe == null) urlSafe = _urlSafe; 81 if (urlSafe == null) urlSafe = _urlSafe;
83 if (addLineSeparator == null) addLineSeparator = _addLineSeparator; 82 if (addLineSeparator == null) addLineSeparator = _addLineSeparator;
84 if (encodePaddingCharacter == null) { 83 if (encodePaddingCharacter == null) {
85 encodePaddingCharacter = _encodePaddingCharacter; 84 encodePaddingCharacter = _encodePaddingCharacter;
86 } 85 }
87 return new Base64Encoder( 86 return new Base64Encoder(
88 urlSafe: urlSafe, 87 urlSafe: urlSafe,
89 addLineSeparator: addLineSeparator, 88 addLineSeparator: addLineSeparator,
90 encodePaddingCharacter: encodePaddingCharacter) 89 encodePaddingCharacter: encodePaddingCharacter).convert(bytes);
91 .convert(bytes);
92
93
94 } 90 }
95 91
96 Base64Encoder get encoder => 92 Base64Encoder get encoder => new Base64Encoder(
97 new Base64Encoder(urlSafe: _urlSafe, 93 urlSafe: _urlSafe,
98 addLineSeparator: _addLineSeparator, 94 addLineSeparator: _addLineSeparator,
99 encodePaddingCharacter: _encodePaddingCharacter); 95 encodePaddingCharacter: _encodePaddingCharacter);
100 96
101 Base64Decoder get decoder => new Base64Decoder(); 97 Base64Decoder get decoder => new Base64Decoder();
102
103 } 98 }
104 99
105 /** 100 /**
106 * This class encodes byte strings (lists of unsigned 101 * This class encodes byte strings (lists of unsigned
107 * 8-bit integers) to strings according to Base64. 102 * 8-bit integers) to strings according to Base64.
108 */ 103 */
109 class Base64Encoder extends Converter<List<int>, String> { 104 class Base64Encoder extends Converter<List<int>, String> {
110 final bool _urlSafe; 105 final bool _urlSafe;
111 final bool _addLineSeparator; 106 final bool _addLineSeparator;
112 final bool _encodePaddingCharacter; 107 final bool _encodePaddingCharacter;
(...skipping 14 matching lines...) Expand all
127 * should add line separators. 122 * should add line separators.
128 * 123 *
129 * If it is `true` [convert] adds an optional line separator(CR + LF) 124 * If it is `true` [convert] adds an optional line separator(CR + LF)
130 * for each 76 char output. 125 * for each 76 char output.
131 * 126 *
132 * The default value of [addLineSeparator] if `false`. 127 * The default value of [addLineSeparator] if `false`.
133 * 128 *
134 * If [encodePaddingCharacter] is `true` `encode` converts `=` to `%3D`. 129 * If [encodePaddingCharacter] is `true` `encode` converts `=` to `%3D`.
135 * The default value of [encodePaddingCharacter] is `false`. 130 * The default value of [encodePaddingCharacter] is `false`.
136 */ 131 */
137 const Base64Encoder({bool urlSafe: false, 132 const Base64Encoder(
138 bool addLineSeparator: false, 133 {bool urlSafe: false,
139 bool encodePaddingCharacter: false}) 134 bool addLineSeparator: false,
135 bool encodePaddingCharacter: false})
140 : _urlSafe = urlSafe, 136 : _urlSafe = urlSafe,
141 _addLineSeparator = addLineSeparator, 137 _addLineSeparator = addLineSeparator,
142 _encodePaddingCharacter = encodePaddingCharacter, 138 _encodePaddingCharacter = encodePaddingCharacter,
143 _pad = encodePaddingCharacter == true ? _ENCODED_PAD_BYTES : _PAD_BYTES; 139 _pad = encodePaddingCharacter == true ? _ENCODED_PAD_BYTES : _PAD_BYTES;
144 140
145 /** 141 /**
146 * Converts [bytes] to its Base64 representation as a string. 142 * Converts [bytes] to its Base64 representation as a string.
147 * 143 *
148 * if [start] and [end] are provided, only the sublist 144 * if [start] and [end] are provided, only the sublist
149 * `bytes.sublist(start, end)` is converted. 145 * `bytes.sublist(start, end)` is converted.
150 */ 146 */
151 String convert(List<int> bytes, [int start = 0, int end]) { 147 String convert(List<int> bytes, [int start = 0, int end]) {
152 int bytes_length = bytes.length; 148 int bytes_length = bytes.length;
153 RangeError.checkValidRange(start, end, bytes_length); 149 RangeError.checkValidRange(start, end, bytes_length);
154 if (end == null) end = bytes_length; 150 if (end == null) end = bytes_length;
155 int length = end - start; 151 int length = end - start;
156 if (length == 0) { 152 if (length == 0) {
157 return ""; 153 return "";
158 } 154 }
159 final String lookup = _urlSafe ? _encodeTableUrlSafe : _encodeTable; 155 final String lookup = _urlSafe ? _encodeTableUrlSafe : _encodeTable;
160 // Size of 24 bit chunks. 156 // Size of 24 bit chunks.
161 final int remainderLength = length.remainder(3); 157 final int remainderLength = length.remainder(3);
162 final int chunkLength = length - remainderLength; 158 final int chunkLength = length - remainderLength;
163 // Size of base output. 159 // Size of base output.
164 int baseOutputLength = ((length ~/ 3) * 4); 160 int baseOutputLength = ((length ~/ 3) * 4);
165 int remainderOutputLength; 161 int remainderOutputLength;
166 if(_encodePaddingCharacter) { 162 if (_encodePaddingCharacter) {
167 remainderOutputLength = ((remainderLength > 0) ? 6 : 0); 163 remainderOutputLength = ((remainderLength > 0) ? 6 : 0);
168 } else { 164 } else {
169 remainderOutputLength = ((remainderLength > 0) ? 4 : 0); 165 remainderOutputLength = ((remainderLength > 0) ? 4 : 0);
170 } 166 }
171 167
172 int outputLength = baseOutputLength + remainderOutputLength; 168 int outputLength = baseOutputLength + remainderOutputLength;
173 // Add extra for line separators. 169 // Add extra for line separators.
174 if (_addLineSeparator) { 170 if (_addLineSeparator) {
175 outputLength += ((outputLength - 1) ~/ _LINE_LENGTH) << 1; 171 outputLength += ((outputLength - 1) ~/ _LINE_LENGTH) << 1;
176 } 172 }
177 List<int> out = new List<int>(outputLength); 173 List<int> out = new List<int>(outputLength);
178 174
179 // Encode 24 bit chunks. 175 // Encode 24 bit chunks.
180 int j = 0, i = start, c = 0; 176 int j = 0, i = start, c = 0;
181 while (i < chunkLength) { 177 while (i < chunkLength) {
182 int x = ((bytes[i++] << 16) & 0x00FFFFFF) | 178 int x = ((bytes[i++] << 16) & 0x00FFFFFF) |
183 ((bytes[i++] << 8) & 0x00FFFFFF) | 179 ((bytes[i++] << 8) & 0x00FFFFFF) |
184 bytes[i++]; 180 bytes[i++];
185 out[j++] = lookup.codeUnitAt(x >> 18); 181 out[j++] = lookup.codeUnitAt(x >> 18);
186 out[j++] = lookup.codeUnitAt((x >> 12) & 0x3F); 182 out[j++] = lookup.codeUnitAt((x >> 12) & 0x3F);
187 out[j++] = lookup.codeUnitAt((x >> 6) & 0x3F); 183 out[j++] = lookup.codeUnitAt((x >> 6) & 0x3F);
188 out[j++] = lookup.codeUnitAt(x & 0x3F); 184 out[j++] = lookup.codeUnitAt(x & 0x3F);
189 // Add optional line separator for each 76 char output. 185 // Add optional line separator for each 76 char output.
190 if (_addLineSeparator && ++c == 19 && j < outputLength - 2) { 186 if (_addLineSeparator && ++c == 19 && j < outputLength - 2) {
191 out[j++] = _CR; 187 out[j++] = _CR;
192 out[j++] = _LF; 188 out[j++] = _LF;
193 c = 0; 189 c = 0;
194 } 190 }
195 } 191 }
196 192
197 // If input length if not a multiple of 3, encode remaining bytes and 193 // If input length if not a multiple of 3, encode remaining bytes and
(...skipping 21 matching lines...) Expand all
219 if (sink is StringConversionSink) { 215 if (sink is StringConversionSink) {
220 stringSink = sink; 216 stringSink = sink;
221 } else { 217 } else {
222 stringSink = new StringConversionSink.from(sink); 218 stringSink = new StringConversionSink.from(sink);
223 } 219 }
224 return new _Base64EncoderSink(stringSink, _urlSafe, _addLineSeparator); 220 return new _Base64EncoderSink(stringSink, _urlSafe, _addLineSeparator);
225 } 221 }
226 } 222 }
227 223
228 class _Base64EncoderSink extends ChunkedConversionSink<List<int>> { 224 class _Base64EncoderSink extends ChunkedConversionSink<List<int>> {
229
230 final Base64Encoder _encoder; 225 final Base64Encoder _encoder;
231 final ChunkedConversionSink<String> _outSink; 226 final ChunkedConversionSink<String> _outSink;
232 final List<int> _buffer = new List<int>(); 227 final List<int> _buffer = new List<int>();
233 int _bufferCount = 0; 228 int _bufferCount = 0;
234 229
235 _Base64EncoderSink(this._outSink, urlSafe, addLineSeparator) 230 _Base64EncoderSink(this._outSink, urlSafe, addLineSeparator)
236 : _encoder = new Base64Encoder(urlSafe: urlSafe, 231 : _encoder = new Base64Encoder(
237 addLineSeparator: addLineSeparator); 232 urlSafe: urlSafe, addLineSeparator: addLineSeparator);
238
239 233
240 void add(List<int> chunk) { 234 void add(List<int> chunk) {
241 var nextBufferCount = (chunk.length + _bufferCount) % 3; 235 var nextBufferCount = (chunk.length + _bufferCount) % 3;
242 236
243 int decodableLength = _bufferCount + chunk.length - nextBufferCount; 237 int decodableLength = _bufferCount + chunk.length - nextBufferCount;
244 238
245 if (_bufferCount + chunk.length > _buffer.length) { 239 if (_bufferCount + chunk.length > _buffer.length) {
246 _buffer.replaceRange(_bufferCount, 240 _buffer.replaceRange(_bufferCount, _buffer.length,
247 _buffer.length, 241 chunk.sublist(0, _buffer.length - _bufferCount));
248 chunk.sublist(0, _buffer.length - _bufferCount));
249 _buffer.addAll(chunk.sublist(_buffer.length - _bufferCount)); 242 _buffer.addAll(chunk.sublist(_buffer.length - _bufferCount));
250 } else { 243 } else {
251 _buffer.replaceRange(_bufferCount, _bufferCount + chunk.length, chunk); 244 _buffer.replaceRange(_bufferCount, _bufferCount + chunk.length, chunk);
252 } 245 }
253 246
254 _outSink.add(_encoder.convert(_buffer, 0, decodableLength)); 247 _outSink.add(_encoder.convert(_buffer, 0, decodableLength));
255 _buffer.removeRange(0, decodableLength); 248 _buffer.removeRange(0, decodableLength);
256 _bufferCount = nextBufferCount; 249 _bufferCount = nextBufferCount;
257 } 250 }
258 251
259 void close() { 252 void close() {
260 if (_bufferCount > 0) { 253 if (_bufferCount > 0) {
261 _outSink.add(_encoder.convert(_buffer.sublist(0, _bufferCount))); 254 _outSink.add(_encoder.convert(_buffer.sublist(0, _bufferCount)));
262 } 255 }
263 _outSink.close(); 256 _outSink.close();
264 } 257 }
265 } 258 }
266 259
267 /** 260 /**
268 * This class decodes strings to lists of bytes(lists of 261 * This class decodes strings to lists of bytes(lists of
269 * unsigned 8-bit integers) according to Base64. 262 * unsigned 8-bit integers) according to Base64.
270 */ 263 */
271 class Base64Decoder extends Converter<String, List<int>> { 264 class Base64Decoder extends Converter<String, List<int>> {
272
273 /** 265 /**
274 * Instantiates a new [Base64Decoder] 266 * Instantiates a new [Base64Decoder]
275 */ 267 */
276 const Base64Decoder(); 268 const Base64Decoder();
277 269
278 List<int> convert(String input) { 270 List<int> convert(String input) {
279 int length = input.length; 271 int length = input.length;
280 if (length == 0) { 272 if (length == 0) {
281 return new Uint8List(0); 273 return new Uint8List(0);
282 } 274 }
(...skipping 16 matching lines...) Expand all
299 i += 2; 291 i += 2;
300 } else { 292 } else {
301 throw new FormatException('Invalid character', input, i); 293 throw new FormatException('Invalid character', input, i);
302 } 294 }
303 } 295 }
304 if (c >= 0) normalLength++; 296 if (c >= 0) normalLength++;
305 i++; 297 i++;
306 } 298 }
307 299
308 if (normalLength % 4 != 0) { 300 if (normalLength % 4 != 0) {
309 throw new FormatException('''Size of Base 64 characters in Input 301 throw new FormatException(
310 must be a multiple of 4''', input, normalLength); 302 '''Size of Base 64 characters in Input
303 must be a multiple of 4''',
304 input,
305 normalLength);
311 } 306 }
312 307
313 // Count pad characters. 308 // Count pad characters.
314 int padLength = 0; 309 int padLength = 0;
315 i = length - 1; 310 i = length - 1;
316 while(i >= 0) { 311 while (i >= 0) {
317 int currentCodeUnit = input.codeUnitAt(i); 312 int currentCodeUnit = input.codeUnitAt(i);
318 if (currentCodeUnit == _ENCODED_PAD_BYTES[2] && 313 if (currentCodeUnit == _ENCODED_PAD_BYTES[2] &&
319 i >= 2 && 314 i >= 2 &&
320 input.codeUnitAt(i - 1) == _ENCODED_PAD_BYTES[1] && 315 input.codeUnitAt(i - 1) == _ENCODED_PAD_BYTES[1] &&
321 input.codeUnitAt(i - 2) == _ENCODED_PAD_BYTES[0]) { 316 input.codeUnitAt(i - 2) == _ENCODED_PAD_BYTES[0]) {
322 padLength++; 317 padLength++;
323 i -= 2; 318 i -= 2;
324 } else if (_decodeTable[currentCodeUnit] > 0) { 319 } else if (_decodeTable[currentCodeUnit] > 0) {
325 break; 320 break;
326 } else if (currentCodeUnit == _PAD_BYTES[0]) { 321 } else if (currentCodeUnit == _PAD_BYTES[0]) {
327 padLength++; 322 padLength++;
328 } 323 }
329 i--; 324 i--;
330 } 325 }
331 int outputLength = ((normalLength * 6) >> 3) - padLength; 326 int outputLength = ((normalLength * 6) >> 3) - padLength;
332 List<int> out = new Uint8List(outputLength); 327 List<int> out = new Uint8List(outputLength);
333 328
334 for (int i = 0, o = 0; o < outputLength; ) { 329 for (int i = 0, o = 0; o < outputLength;) {
335 // Accumulate 4 valid 6 bit Base 64 characters into an int. 330 // Accumulate 4 valid 6 bit Base 64 characters into an int.
336 int x = 0; 331 int x = 0;
337 for (int j = 4; j > 0; ) { 332 for (int j = 4; j > 0;) {
338 int c = _decodeTable[input.codeUnitAt(i++)]; 333 int c = _decodeTable[input.codeUnitAt(i++)];
339 if (c >= 0) { 334 if (c >= 0) {
340 x = ((x << 6) & 0x00FFFFFF) | c; 335 x = ((x << 6) & 0x00FFFFFF) | c;
341 j--; 336 j--;
342 } 337 }
343 } 338 }
344 out[o++] = x >> 16; 339 out[o++] = x >> 16;
345 if (o < outputLength) { 340 if (o < outputLength) {
346 out[o++] = (x >> 8) & 0xFF; 341 out[o++] = (x >> 8) & 0xFF;
347 if (o < outputLength) out[o++] = x & 0xFF; 342 if (o < outputLength) out[o++] = x & 0xFF;
348 } 343 }
349 } 344 }
350 345
351 return out; 346 return out;
352 } 347 }
353 348
354 _Base64DecoderSink startChunkedConversion(Sink<List<int>> sink) { 349 _Base64DecoderSink startChunkedConversion(Sink<List<int>> sink) {
355 if (sink is! ByteConversionSink) { 350 if (sink is! ByteConversionSink) {
356 sink = new ByteConversionSink.from(sink); 351 sink = new ByteConversionSink.from(sink);
357 } 352 }
358 return new _Base64DecoderSink(sink); 353 return new _Base64DecoderSink(sink);
359 } 354 }
360 } 355 }
361 356
362
363 class _Base64DecoderSink extends ChunkedConversionSink<String> { 357 class _Base64DecoderSink extends ChunkedConversionSink<String> {
364
365 final Base64Decoder _decoder = new Base64Decoder(); 358 final Base64Decoder _decoder = new Base64Decoder();
366 final ChunkedConversionSink<List<int>> _outSink; 359 final ChunkedConversionSink<List<int>> _outSink;
367 String _unconverted = ""; 360 String _unconverted = "";
368 361
369 _Base64DecoderSink(this._outSink); 362 _Base64DecoderSink(this._outSink);
370 363
371 void add(String chunk) { 364 void add(String chunk) {
372 if (chunk.isEmpty) return; 365 if (chunk.isEmpty) return;
373 if (_unconverted.isNotEmpty) { 366 if (_unconverted.isNotEmpty) {
374 chunk = _unconverted + chunk; 367 chunk = _unconverted + chunk;
375 } 368 }
376 chunk = chunk.replaceAll(_ENCODED_PAD, _PAD); 369 chunk = chunk.replaceAll(_ENCODED_PAD, _PAD);
377 int decodableLength = chunk.length; 370 int decodableLength = chunk.length;
378 // If chunk ends in "%" or "%3", it may be a partial encoded pad. 371 // If chunk ends in "%" or "%3", it may be a partial encoded pad.
379 // If chunk is smaller than 4 characters, don't bother checking. 372 // If chunk is smaller than 4 characters, don't bother checking.
380 if (chunk.length > 3 && 373 if (chunk.length > 3 && chunk.contains(_ENCODED_PAD[0], chunk.length - 2)) {
381 chunk.contains(_ENCODED_PAD[0], chunk.length - 2)) {
382 decodableLength = chunk.lastIndexOf(_ENCODED_PAD[0]); 374 decodableLength = chunk.lastIndexOf(_ENCODED_PAD[0]);
383 } 375 }
384 decodableLength -= decodableLength % 4; 376 decodableLength -= decodableLength % 4;
385 _unconverted = chunk.substring(decodableLength); 377 _unconverted = chunk.substring(decodableLength);
386 if (decodableLength > 0) { 378 if (decodableLength > 0) {
387 _outSink.add(_decoder.convert(chunk.substring(0, decodableLength))); 379 _outSink.add(_decoder.convert(chunk.substring(0, decodableLength)));
388 } 380 }
389 } 381 }
390 382
391 void close() { 383 void close() {
392 if (_unconverted.isNotEmpty) { 384 if (_unconverted.isNotEmpty) {
393 _outSink.add(_decoder.convert(_unconverted)); 385 _outSink.add(_decoder.convert(_unconverted));
394 } 386 }
395 _outSink.close(); 387 _outSink.close();
396 } 388 }
397 } 389 }
398
399
OLDNEW
« no previous file with comments | « lib/crypto.dart ('k') | lib/src/crypto_utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698