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