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

Unified Diff: sdk/lib/crypto/crypto_utils.dart

Issue 12534011: Add base64 decoder and change existing base64 encoder. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: minor fix Created 7 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: sdk/lib/crypto/crypto_utils.dart
diff --git a/sdk/lib/crypto/crypto_utils.dart b/sdk/lib/crypto/crypto_utils.dart
index 4c09dff4b00333c7044870a7d3078b5aae78d29a..2b61b0b7eccaa2acc0cbb40a68b24049545c81bc 100644
--- a/sdk/lib/crypto/crypto_utils.dart
+++ b/sdk/lib/crypto/crypto_utils.dart
@@ -4,25 +4,6 @@
part of dart.crypto;
-class _LineWrappingStringBuffer {
- _LineWrappingStringBuffer(int this._lineLength) : _sb = new StringBuffer();
-
- void write(String s) {
- if (_lineLength != null && _currentLineLength == _lineLength) {
- _sb.write('\r\n');
- _currentLineLength = 0;
- }
- _sb.write(s);
- _currentLineLength++;
- }
-
- String toString() => _sb.toString();
-
- int _lineLength;
- StringBuffer _sb;
- int _currentLineLength = 0;
-}
-
abstract class _CryptoUtils {
static String bytesToHex(List<int> bytes) {
var result = new StringBuffer();
@@ -32,45 +13,133 @@ abstract class _CryptoUtils {
return result.toString();
}
- static String bytesToBase64(List<int> bytes, [int lineLength]) {
- final table =
- const [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
- 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
- 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
- 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
- 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', '+', '/' ];
-
- var result = new _LineWrappingStringBuffer(lineLength);
-
- // Encode all full 24-bit blocks.
- var i = 0;
- for (; (i + 2) < bytes.length; i += 3) {
- var b0 = bytes[i] & 0xff;
- var b1 = bytes[i + 1] & 0xff;
- var b2 = bytes[i + 2] & 0xff;
- result.write(table[b0 >> 2]);
- result.write(table[((b0 << 4) | (b1 >> 4)) & 0x3f]);
- result.write(table[((b1 << 2) | (b2 >> 6)) & 0x3f]);
- result.write(table[b2 & 0x3f]);
+ static final int PAD = '='.codeUnitAt(0);
+ static final int CR = '\r'.codeUnitAt(0);
+ static final int LF = '\n'.codeUnitAt(0);
+
+ static final _encodeTable =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".codeUnits;
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Consider not extracting code units here, and just
mdakin1 2013/03/21 15:27:15 Converted to String. But I am curious, I thought "
Lasse Reichstein Nielsen 2013/03/22 09:16:56 It just returns a read-only list view of the code
mdakin1 2013/03/25 17:05:02 Acknowledged.
+
+ static final _encodeTableUrlSafe =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".codeUnits;
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Ditto.
mdakin1 2013/03/21 15:27:15 Done.
+
+ static const List<int> _decodeTable =
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Add a comment, it doesn't have to be dartdoc, to e
mdakin1 2013/03/21 15:27:15 Done.
+ const [ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -1, -2, -2,
+ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
+ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, 62, -2, 63,
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, 0, -2, -2,
+ -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Consider padding with spaces here so every number
mdakin1 2013/03/21 15:27:15 Done.
+ 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, 63, -2, 26,
+ 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
+ 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, -2, -2,
+ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
+ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
+ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
+ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
+ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
+ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
+ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
+ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2 ];
+
+ static String bytesToBase64(List<int> bytes, [bool urlSafe, bool addLineSeparator]) {
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Long line needs to be broken.
mdakin1 2013/03/21 15:27:15 Done.
Lasse Reichstein Nielsen 2013/03/22 09:16:56 Default for urlSafe and addLineSeparator is null.
mdakin1 2013/03/25 17:05:02 Done.
+ int len = bytes != null ? bytes.length : 0;
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Consider not accepting null. It may just be hiding
mdakin1 2013/03/21 15:27:15 I removed the null check, but not sure how to hand
Lasse Reichstein Nielsen 2013/03/22 09:16:56 That would make bytes.length throw, which is perfe
mdakin1 2013/03/25 17:05:02 Acknowledged. So basically, if it accepts null doc
floitsch 2013/03/25 17:45:37 correct.
+ if (len == 0) {
+ return "";
}
+ final List<int> lookup = urlSafe ? _encodeTable : _encodeTableUrlSafe;
+ // Size of 24 bit chunks.
+ final int cLen = len ~/ 3 * 3;
Lasse Reichstein Nielsen 2013/03/21 14:06:38 cLen is a non-obvious abbreviated name. Consider g
mdakin1 2013/03/21 15:27:15 Done.
+ final int padLen = len - cLen;
Lasse Reichstein Nielsen 2013/03/21 14:06:38 padLength. Is this done this way because it's fas
mdakin1 2013/03/21 15:27:15 Done. So obvious :/
+ // Size of base output.
+ int oLen = ((len ~/ 3) * 4) + (padLen > 0 ? 4 : 0);
+ // Add extra for line separators.
+ if (addLineSeparator) {
+ oLen += (oLen - 1) ~/ 76 << 1;
Lasse Reichstein Nielsen 2013/03/21 14:06:38 Parentheses around the left-hand-side of <<. Nobod
mdakin1 2013/03/21 15:27:15 Done.
+ }
+ List<int> out = new List<int>(oLen);
- // Deal with the last non-full block if any and add padding '='.
- if (i == bytes.length - 1) {
- var b0 = bytes[i] & 0xff;
- result.write(table[b0 >> 2]);
- result.write(table[(b0 << 4) & 0x3f]);
- result.write('=');
- result.write('=');
- } else if (i == bytes.length - 2) {
- var b0 = bytes[i] & 0xff;
- var b1 = bytes[i + 1] & 0xff;
- result.write(table[b0 >> 2]);
- result.write(table[((b0 << 4) | (b1 >> 4)) & 0x3f]);
- result.write(table[(b1 << 2) & 0x3f]);
- result.write('=');
+ // Encode 24 bit chunks.
+ int j = 0, i = 0, cc = 0;
+ while (i < cLen) {
+ int x = bytes[i++] << 16 | bytes[i++] << 8 | bytes[i++];
+ out[j++] = lookup[x >> 18];
+ out[j++] = lookup[(x >> 12) & 0x3F];
+ out[j++] = lookup[(x >> 6) & 0x3F];
+ out[j++] = lookup[x & 0x3f];
+ // Add optional line separator for each 76 char output.
+ if (addLineSeparator && ++cc == 19 && j < oLen - 2) {
+ out[j++] = CR;
+ out[j++] = LF;
+ cc = 0;
+ }
}
- return result.toString();
+ // If input length if not a multiple of 3, encode remaining add padding.
+ if (padLen > 0) {
+ int x = bytes[cLen] << 10 | (padLen == 2 ? bytes[len - 1] << 2 : 0);
Lasse Reichstein Nielsen 2013/03/21 14:06:38 More parentheses! Does << bind harder than | ? Who
mdakin1 2013/03/21 15:27:15 Done.
+ out[j++] = lookup[(x >> 12) & 0x3F];
+ out[j++] = lookup[(x >> 6) & 0x3F];
+ out[j++] = padLen == 2 ? lookup[x & 0x3f] : PAD;
+ out[j++] = PAD;
+ }
+
+ return new String.fromCharCodes(out);
}
+
+ static List<int> base64StringToBytes(String input, [bool ignoreErrors]) {
+ int len = input != null ? input.length : 0;
Lasse Reichstein Nielsen 2013/03/22 09:16:56 Again, feel free to throw on null.
mdakin1 2013/03/25 17:05:02 Done.
+ if (len == 0) {
+ return new List<int>(0);
+ }
+
+ // Count \r \n and illegal chars,
Lasse Reichstein Nielsen 2013/03/22 09:16:56 // Count '\r', '\n' and illegal characters. Makes
mdakin1 2013/03/25 17:05:02 Done.
+ // for illegal chars, if [ignoreErrors] is false, throw an exception.
+ int extrasLen = 0;
+ for (int i = 0; i < len; i++) {
+ int c = _decodeTable[input.codeUnitAt(i)];
+ if (c < 0) {
+ if (ignoreErrors) {
+ extrasLen++;
Lasse Reichstein Nielsen 2013/03/22 09:16:56 So if c is -1, we increment extrasLen if we ignore
mdakin1 2013/03/25 17:05:02 Done.
+ } else if (c == -2) {
+ throw new FormatException(
+ 'Invalid character ${new String.fromCharCode(input.codeUnitAt(i))}');
Lasse Reichstein Nielsen 2013/03/22 09:16:56 ${input[i]} should be equivalent, and shorter, wh
mdakin1 2013/03/25 17:05:02 Done.
+ }
+ }
+ }
+
+ if ((len - extrasLen) % 4 != 0) {
Lasse Reichstein Nielsen 2013/03/22 09:16:56 Is this requirement documented anywhere? Is it tes
mdakin1 2013/03/25 17:05:02 Not really documented, but in theory if you count
floitsch 2013/03/25 17:45:37 I would be fine with "ignoreInvalidCharacters". It
mdakin1 2013/03/26 09:52:17 Done.
+ throw new FormatException('Invalid input.');
floitsch 2013/03/25 17:45:37 I wonder if this shouldn't contain the input. In a
mdakin1 2013/03/26 09:52:17 Added input to error, explanation is a little bit
+ }
+
+ // Count pad characters, ignore illeal characters at the end.
+ int pad = 0;
+ for (int i = len; i > 1 && _decodeTable[input.codeUnitAt(--i)] <= 0;) {
+ if (input.codeUnitAt(i) == PAD) {
+ pad++;
+ }
+ }
+ int oLen = ((len - extrasLen) * 6 >> 3) - pad;
Lasse Reichstein Nielsen 2013/03/22 09:16:56 More parentheses. Anything near a >> needs to be p
mdakin1 2013/03/25 17:05:02 Done.
+ List<int> out = new List<int>(oLen);
+
+ for (int i = 0, o = 0; o < oLen;) {
+ int x = 0;
+ int j = 18;
Lasse Reichstein Nielsen 2013/03/22 09:16:56 How about: for (int j = 4; j > 0;) { int c = _de
mdakin1 2013/03/25 17:05:02 Done. Much better. Tested it, this version is inde
+ // Accumulate 4 valid 6 bit base64 characters into an int.
+ while (j >= 0) {
+ int c = _decodeTable[input.codeUnitAt(i++)];
+ if (c >= 0) {
+ x |= c << j;
+ j -= 6;
+ }
+ }
+ out[o++] = (x >> 16);
+ if (o < oLen) {
+ out[o++] = (x >> 8) & 0xFF;
+ if (o < oLen)
+ out[o++] = x & 0xFF;
Lasse Reichstein Nielsen 2013/03/22 09:16:56 Always use braces around then/else branches for a
mdakin1 2013/03/25 17:05:02 Done.
+ }
+ }
+ return out;
+ }
+
}

Powered by Google App Engine
This is Rietveld 408576698