OLD | NEW |
---|---|
(Empty) | |
1 part of crypto; | |
2 | |
3 const Base64Codec BASE64 = const Base64Codec(); | |
4 | |
5 const List<int> _decodeTable = | |
6 const [ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -1, -2, -2, | |
7 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
8 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, 62, -2, 63, | |
9 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, 0, -2, -2, | |
10 -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, | |
11 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, 63, | |
12 -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | |
13 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, | |
14 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
15 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
16 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
17 -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, | |
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 ]; | |
22 | |
23 const String _encodeTableUrlSafe = | |
24 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; | |
25 | |
26 const String _encodeTable = | |
27 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
28 | |
29 const List<String> _URL_SAFE_CHARACTERS = const ['+', '/']; | |
30 const List<String> _URL_UNSAFE_CHARACTERS = const ['-', '_']; | |
31 | |
32 const int _LINE_LENGTH = 76; | |
33 const int _CR = 13; // '\r' | |
34 const int _LF = 10; // '\n' | |
35 const List<int> _PAD_BYTES = const [61]; // '=' | |
36 const List<int> _ENCODED_PAD_BYTES = const [37, 51, 68]; // '%3D' | |
37 const String _PAD = "="; | |
38 const String _ENCODED_PAD = "%3D"; | |
39 | |
40 class Base64Codec extends Codec<List<int>, String> { | |
41 | |
42 final bool _urlSafe; | |
43 final bool _addLineSeparator; | |
44 final bool _encodePaddingCharacter; | |
45 | |
46 /** | |
47 * Instantiates a new [Base64Codec]. | |
48 * | |
49 * The optional [urlSafe] argument specifies if [encoder] and [encode] | |
50 * should generate a string, that is safe to use in an URL. | |
51 * | |
52 * If [urlSafe] is `true` (and not overriden at the method invocation) | |
53 * the [encoder] and [encode] use '-' instead of '+' and '_' instead of '/'. | |
54 * | |
55 * The default value of [urlSafe] is `false`. | |
56 * | |
57 * The optional [addLineSeparator] argument specifies if the [encoder] and | |
58 * [encode] should add line separators. | |
59 * | |
60 * If `addLineSeparator` is `true` [encode] adds an | |
61 * optional line separator (CR + LF) for each 76 char output. | |
62 * | |
63 * The default value of [addLineSeparator] if `false`. | |
64 * | |
65 * If [encodePaddingCharacter] is `true` `encode` converts `=` to `%3D`. | |
66 * The default value of [encodePaddingCharacter] is `false`. | |
67 */ | |
68 const Base64Codec({bool urlSafe: false, | |
69 bool addLineSeparator: false, | |
70 bool encodePaddingCharacter: false}) | |
71 : _urlSafe = urlSafe, | |
72 _addLineSeparator = addLineSeparator, | |
73 _encodePaddingCharacter = encodePaddingCharacter; | |
74 | |
75 String get name => "base64"; | |
76 | |
77 String encode(List<int> bytes, | |
78 {bool urlSafe, | |
79 bool addLineSeparator, | |
80 bool encodePaddingCharacter}) { | |
81 if (urlSafe == null) urlSafe = _urlSafe; | |
82 if (addLineSeparator == null) addLineSeparator = _addLineSeparator; | |
83 if (encodePaddingCharacter == null) { | |
84 encodePaddingCharacter = _encodePaddingCharacter; | |
85 } | |
86 return new Base64Encoder( | |
87 urlSafe: urlSafe, | |
88 addLineSeparator: addLineSeparator, | |
89 encodePaddingCharacter: encodePaddingCharacter) | |
90 .convert(bytes); | |
91 | |
92 | |
93 } | |
94 | |
95 Base64Encoder get encoder => new Base64Encoder( | |
96 urlSafe: _urlSafe, | |
97 addLineSeparator: _addLineSeparator, | |
98 encodePaddingCharacter: _encodePaddingChara cter); | |
Søren Gjesse
2015/06/03 11:32:03
Long line. I will probably format it like this:
B
| |
99 | |
100 Base64Decoder get decoder => new Base64Decoder(); | |
101 | |
102 } | |
103 | |
104 /** | |
105 * This class encodes byte strings (lists of unsigned | |
106 * 8-bit integers) to strings according to Base64. | |
107 */ | |
108 class Base64Encoder extends Converter<List<int>, String> { | |
109 final bool _urlSafe; | |
110 final bool _addLineSeparator; | |
111 final bool _encodePaddingCharacter; | |
112 final List<int> _pad; | |
113 | |
114 /** | |
115 * Instantiates a new [Base64Encoder]. | |
116 * | |
117 * The optional [urlSafe] argument specifies if [convert] | |
118 * should generate a string, that is safe to use in an URL. | |
119 * | |
120 * If it is `true` the [convert] use | |
121 * '-' instead of '+' and '_' instead of '/'. | |
122 * | |
123 * The default value of [urlSafe] is `false`. | |
124 * | |
125 * The optional [addLineSeparator] argument specifies if [convert] | |
126 * should add line separators. | |
127 * | |
128 * If it is `true` [convert] adds an optional line separator(CR + LF) | |
129 * for each 76 char output. | |
130 * | |
131 * The default value of [addLineSeparator] if `false`. | |
132 * | |
133 * If [encodePaddingCharacter] is `true` `encode` converts `=` to `%3D`. | |
134 * The default value of [encodePaddingCharacter] is `false`. | |
135 */ | |
136 | |
137 const Base64Encoder({bool urlSafe: false, | |
138 bool addLineSeparator: false, | |
139 bool encodePaddingCharacter: false}) | |
140 : _urlSafe = urlSafe, | |
141 _addLineSeparator = addLineSeparator, | |
142 _encodePaddingCharacter = encodePaddingCharacter, | |
143 _pad = encodePaddingCharacter == true ? _ENCODED_PAD_BYTES : _PAD_BYTES; | |
144 | |
145 /** | |
146 * Converts [bytes] to its Base64 representation as a string. | |
147 * | |
148 * if [start] and [end] are provided, only the sublist | |
149 * `bytes.sublist(start, end)` is converted. | |
150 */ | |
151 | |
152 String convert(List<int> bytes, [int start = 0, int end]) { | |
153 int bytes_length = bytes.length; | |
154 RangeError.checkValidRange(start, end, bytes_length); | |
155 if (end == null) end = bytes_length; | |
156 int length = end - start; | |
157 if (length == 0) { | |
158 return ""; | |
159 } | |
160 final String lookup = _urlSafe ? _encodeTableUrlSafe : _encodeTable; | |
161 // Size of 24 bit chunks. | |
162 final int remainderLength = length.remainder(3); | |
163 final int chunkLength = length - remainderLength; | |
164 // Size of base output. | |
165 int baseOutputLength = ((length ~/ 3) * 4); | |
166 int remainderOutputLength; | |
167 if(_encodePaddingCharacter) { | |
168 remainderOutputLength = ((remainderLength > 0) ? 6 : 0); | |
169 } else { | |
170 remainderOutputLength = ((remainderLength > 0) ? 4 : 0); | |
171 } | |
172 | |
173 int outputLength = baseOutputLength + remainderOutputLength; | |
174 // Add extra for line separators. | |
175 if (_addLineSeparator) { | |
176 outputLength += ((outputLength - 1) ~/ _LINE_LENGTH) << 1; | |
177 } | |
178 List<int> out = new List<int>(outputLength); | |
179 | |
180 // Encode 24 bit chunks. | |
181 int j = 0, i = start, c = 0; | |
182 while (i < chunkLength) { | |
183 int x = ((bytes[i++] << 16) & 0x00FFFFFF) | | |
184 ((bytes[i++] << 8) & 0x00FFFFFF) | | |
185 bytes[i++]; | |
186 out[j++] = lookup.codeUnitAt(x >> 18); | |
187 out[j++] = lookup.codeUnitAt((x >> 12) & 0x3F); | |
188 out[j++] = lookup.codeUnitAt((x >> 6) & 0x3F); | |
189 out[j++] = lookup.codeUnitAt(x & 0x3F); | |
190 // Add optional line separator for each 76 char output. | |
191 if (_addLineSeparator && ++c == 19 && j < outputLength - 2) { | |
192 out[j++] = _CR; | |
193 out[j++] = _LF; | |
194 c = 0; | |
195 } | |
196 } | |
197 | |
198 // If input length if not a multiple of 3, encode remaining bytes and | |
199 // add padding. | |
200 if (remainderLength == 1) { | |
201 int x = bytes[i]; | |
202 out[j++] = lookup.codeUnitAt(x >> 2); | |
203 out[j++] = lookup.codeUnitAt((x << 4) & 0x3F); | |
204 out.setRange(j, j + _pad.length, _pad); | |
205 out.setRange(j + _pad.length, j + 2 * _pad.length, _pad); | |
206 } else if (remainderLength == 2) { | |
207 int x = bytes[i]; | |
208 int y = bytes[i + 1]; | |
209 out[j++] = lookup.codeUnitAt(x >> 2); | |
210 out[j++] = lookup.codeUnitAt(((x << 4) | (y >> 4)) & 0x3F); | |
211 out[j++] = lookup.codeUnitAt((y << 2) & 0x3F); | |
212 out.setRange(j, j + _pad.length, _pad); | |
213 } | |
214 | |
215 return new String.fromCharCodes(out); | |
216 } | |
217 | |
218 _Base64EncoderSink startChunkedConversion(Sink<String> sink) { | |
219 StringConversionSink stringSink; | |
220 if (sink is StringConversionSink) { | |
221 stringSink = sink; | |
222 } else { | |
223 stringSink = new StringConversionSink.from(sink); | |
224 } | |
225 return new _Base64EncoderSink(stringSink, _urlSafe, _addLineSeparator); | |
226 } | |
227 } | |
228 | |
229 class _Base64EncoderSink extends ChunkedConversionSink<List<int>> { | |
230 | |
231 final Base64Encoder _encoder; | |
232 final ChunkedConversionSink<String> _outSink; | |
233 final List<int> _buffer = new List<int>(); | |
234 int _bufferCount = 0; | |
235 | |
236 _Base64EncoderSink(this._outSink, urlSafe, addLineSeparator) | |
237 : _encoder = new Base64Encoder(urlSafe: urlSafe, | |
238 addLineSeparator: addLineSeparator); | |
239 | |
240 | |
241 void add(List<int> chunk) { | |
242 var nextBufferCount = (chunk.length + _bufferCount) % 3; | |
243 | |
244 int decodableLength = _bufferCount + chunk.length - nextBufferCount; | |
245 | |
246 if (_bufferCount + chunk.length > _buffer.length) { | |
247 _buffer.replaceRange(_bufferCount, | |
248 _buffer.length, | |
249 chunk.sublist(0, _buffer.length - _bufferCount)); | |
250 _buffer.addAll(chunk.sublist(_buffer.length - _bufferCount)); | |
251 } else { | |
252 _buffer.replaceRange(_bufferCount, _bufferCount + chunk.length, chunk); | |
253 } | |
254 | |
255 _outSink.add(_encoder.convert(_buffer, 0, decodableLength)); | |
256 _buffer.removeRange(0, decodableLength); | |
257 _bufferCount = nextBufferCount; | |
258 } | |
259 | |
260 void close() { | |
261 if (_bufferCount > 0) { | |
262 _outSink.add(_encoder.convert(_buffer.sublist(0, _bufferCount))); | |
263 } | |
264 _outSink.close(); | |
265 } | |
266 } | |
267 | |
268 /** | |
269 * This class decodes strings to lists of bytes(lists of | |
270 * unsigned 8-bit integers) according to Base64. | |
271 */ | |
272 class Base64Decoder extends Converter<String, List<int>> { | |
273 | |
274 /** | |
275 * Instantiates a new [Base64Decoder] | |
276 */ | |
277 | |
278 const Base64Decoder(); | |
279 | |
280 List<int> convert(String input) { | |
281 int length = input.length; | |
282 if (length == 0) { | |
283 return new List<int>(0); | |
284 } | |
285 | |
286 bool expectedSafe = false; | |
287 bool expectedUnsafe = false; | |
288 | |
289 int normalLength = 0; | |
290 int i = 0; | |
291 // Count '\r', '\n' and illegal characters, check if | |
292 // '/', '+' / '-', '_' are used consistently, for illegal characters, | |
293 // throw an exception. | |
294 | |
295 while (i < length) { | |
296 int codeUnit = input.codeUnitAt(i); | |
297 int c = _decodeTable[codeUnit]; | |
298 if (c == -2) { | |
299 if (codeUnit == _ENCODED_PAD_BYTES[0] && | |
300 i < length - 2 && | |
301 input.codeUnitAt(i + 1) == _ENCODED_PAD_BYTES[1] && | |
302 input.codeUnitAt(i + 2) == _ENCODED_PAD_BYTES[2]) { | |
303 normalLength++; | |
304 i += 2; | |
305 } else { | |
306 throw new FormatException('Invalid character', input, i); | |
307 } | |
308 } else if (input[i] == _URL_UNSAFE_CHARACTERS[0] || | |
309 input[i] == _URL_UNSAFE_CHARACTERS[1]) { | |
310 | |
311 if (expectedSafe) { | |
312 throw new FormatException('Unsafe character in URL-safe string', | |
313 input, i); | |
314 } | |
315 expectedUnsafe = true; | |
316 } else if (input[i] == _URL_SAFE_CHARACTERS[0] || | |
317 input[i] == _URL_SAFE_CHARACTERS[1]) { | |
318 if (expectedUnsafe) { | |
319 throw new FormatException('Invalid character', input, i); | |
320 } | |
321 expectedSafe = true; | |
322 } | |
323 if (c >= 0) normalLength++; | |
324 i++; | |
325 } | |
326 | |
327 if (normalLength % 4 != 0) { | |
328 throw new FormatException('''Size of Base 64 characters in Input | |
329 must be a multiple of 4''', input, normalLength); | |
330 } | |
331 | |
332 // Count pad characters. | |
333 int padLength = 0; | |
334 i = length - 1; | |
335 while(i >= 0) { | |
336 int currentCodeUnit = input.codeUnitAt(i); | |
337 if (currentCodeUnit == _ENCODED_PAD_BYTES[2] && | |
338 i >= 2 && | |
339 input.codeUnitAt(i - 1) == _ENCODED_PAD_BYTES[1] && | |
340 input.codeUnitAt(i - 2) == _ENCODED_PAD_BYTES[0]) { | |
341 padLength++; | |
342 i -= 2; | |
343 } else if (_decodeTable[currentCodeUnit] > 0) { | |
344 break; | |
345 } else if (currentCodeUnit == _PAD_BYTES[0]) { | |
346 padLength++; | |
347 } | |
348 i--; | |
349 } | |
350 int outputLength = ((normalLength * 6) >> 3) - padLength; | |
351 List<int> out = new List<int>(outputLength); | |
352 | |
353 for (int i = 0, o = 0; o < outputLength; ) { | |
354 // Accumulate 4 valid 6 bit Base 64 characters into an int. | |
355 int x = 0; | |
356 for (int j = 4; j > 0; ) { | |
357 int c = _decodeTable[input.codeUnitAt(i++)]; | |
358 if (c >= 0) { | |
359 x = ((x << 6) & 0x00FFFFFF) | c; | |
360 j--; | |
361 } | |
362 } | |
363 out[o++] = x >> 16; | |
364 if (o < outputLength) { | |
365 out[o++] = (x >> 8) & 0xFF; | |
366 if (o < outputLength) out[o++] = x & 0xFF; | |
367 } | |
368 } | |
369 | |
370 return out; | |
371 } | |
372 | |
373 _Base64DecoderSink startChunkedConversion(Sink<List<int>> sink) { | |
374 if (sink is! ByteConversionSink) { | |
375 sink = new ByteConversionSink.from(sink); | |
376 } | |
377 return new _Base64DecoderSink(sink); | |
378 } | |
379 } | |
380 | |
381 | |
382 class _Base64DecoderSink extends ChunkedConversionSink<String> { | |
383 | |
384 final Base64Decoder _decoder = new Base64Decoder(); | |
385 final ChunkedConversionSink<List<int>> _outSink; | |
386 String _buffer = ""; | |
387 bool _isSafe = false; | |
388 bool _isUnsafe = false; | |
389 int _expectPaddingCount = 3; | |
390 | |
391 _Base64DecoderSink(this._outSink); | |
392 | |
393 void add(String chunk) { | |
394 if (chunk.isEmpty) return; | |
395 | |
396 int nextBufferLength = (chunk.length + _buffer.length) % 4; | |
397 | |
398 if (chunk.length >= _expectPaddingCount && | |
399 chunk.substring(0, _expectPaddingCount) == | |
400 _ENCODED_PAD.substring(3 - _expectPaddingCount, 3)) { | |
401 chunk = _PAD + chunk.substring(_expectPaddingCount); | |
402 _expectPaddingCount = 3; | |
403 } else if(chunk.length < _expectPaddingCount && | |
404 chunk == _ENCODED_PAD.substring( | |
405 3 - _expectPaddingCount, | |
406 3 - _expectPaddingCount + chunk.length)) { | |
407 _expectPaddingCount -= chunk.length; | |
408 chunk = ""; | |
409 } | |
410 | |
411 if (chunk.length > 1 && | |
412 chunk[chunk.length - 2] == _ENCODED_PAD[0] && | |
413 chunk[chunk.length - 1] == _ENCODED_PAD[1]) { | |
414 _expectPaddingCount = 1; | |
415 chunk = chunk.substring(0, chunk.length - 2); | |
416 } else if (!chunk.isEmpty && chunk[chunk.length - 1] == _ENCODED_PAD[0]) { | |
417 _expectPaddingCount = 2; | |
418 chunk = chunk.substring(0, chunk.length - 1); | |
419 } | |
420 | |
421 chunk = chunk.replaceAll(_ENCODED_PAD, _PAD); | |
422 | |
423 if (chunk.length + _buffer.length >= 4) { | |
424 int remainder = chunk.length - nextBufferLength; | |
425 String decodable = _buffer + chunk.substring(0, remainder); | |
426 _buffer = chunk.substring(remainder); | |
427 | |
428 for (int i = 0;i < decodable.length; i++) { | |
429 if (decodable[i] == _URL_UNSAFE_CHARACTERS[0] || | |
430 decodable[i] == _URL_UNSAFE_CHARACTERS[1]) { | |
431 if (_isSafe) { | |
432 throw new FormatException('Unsafe character in URL-safe string', | |
433 decodable, i); | |
434 } | |
435 _isUnsafe = true; | |
436 } else if (decodable[i] == _URL_SAFE_CHARACTERS[0] || | |
437 decodable[i] == _URL_SAFE_CHARACTERS[1]) { | |
438 if (_isUnsafe) { | |
439 throw new FormatException('Invalid character', decodable, i); | |
440 } | |
441 _isSafe = true; | |
442 } | |
443 } | |
444 | |
445 _outSink.add(_decoder.convert(decodable)); | |
446 } else { | |
447 _buffer += chunk; | |
448 } | |
449 } | |
450 | |
451 void close() { | |
452 if (_expectPaddingCount == 0 && | |
453 _buffer.length == 3) { | |
454 _outSink.add(_buffer + _PAD); | |
455 } else if (_expectPaddingCount < 3 && | |
456 _buffer.length + 3 - _expectPaddingCount == 4) { | |
457 _outSink.add(_buffer + _ENCODED_PAD.substring(0, 3 - _expectPaddingCount)) ; | |
458 } else if (_expectPaddingCount != 3 || !_buffer.isEmpty) { | |
459 throw new FormatException( | |
460 "Size of Base 64 input must be a multiple of 4", | |
461 _buffer + _PAD.substring(0, 3 - _expectPaddingCount), | |
462 _buffer.length + 3 - _expectPaddingCount); | |
463 } | |
464 _outSink.close(); | |
465 } | |
466 } | |
467 | |
OLD | NEW |