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