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

Side by Side Diff: sdk/lib/io/base64.dart

Issue 15298004: Use dart:crypto's Base64 methods in dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/io/io.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 dart.io;
6
7 class _Base64 {
8 static const List<String> _encodingTable = const [
9 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
10 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
11 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
12 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
13 '8', '9', '+', '/'];
14
15 /**
16 * Base64 transfer encoding for MIME (RFC 2045)
17 */
18 static String _encode(List<int> data) {
19 List<String> characters = new List<String>();
20 int i;
21 for (i = 0; i + 3 <= data.length; i += 3) {
22 int value = 0;
23 value |= data[i + 2];
24 value |= data[i + 1] << 8;
25 value |= data[i] << 16;
26 for (int j = 0; j < 4; j++) {
27 int index = (value >> ((3 - j) * 6)) & ((1 << 6) - 1);
28 characters.add(_encodingTable[index]);
29 }
30 }
31 // Remainders.
32 if (i + 2 == data.length) {
33 int value = 0;
34 value |= data[i + 1] << 8;
35 value |= data[i] << 16;
36 for (int j = 0; j < 3; j++) {
37 int index = (value >> ((3 - j) * 6)) & ((1 << 6) - 1);
38 characters.add(_encodingTable[index]);
39 }
40 characters.add("=");
41 } else if (i + 1 == data.length) {
42 int value = 0;
43 value |= data[i] << 16;
44 for (int j = 0; j < 2; j++) {
45 int index = (value >> ((3 - j) * 6)) & ((1 << 6) - 1);
46 characters.add(_encodingTable[index]);
47 }
48 characters.add("=");
49 characters.add("=");
50 }
51 StringBuffer output = new StringBuffer();
52 for (i = 0; i < characters.length; i++) {
53 if (i > 0 && i % 76 == 0) {
54 output.write("\r\n");
55 }
56 output.write(characters[i]);
57 }
58 return output.toString();
59 }
60
61
62 /**
63 * Base64 transfer decoding for MIME (RFC 2045).
64 */
65 static List<int> _decode(String data) {
66 List<int> result = new List<int>();
67 int padCount = 0;
68 int charCount = 0;
69 int value = 0;
70 for (int i = 0; i < data.length; i++) {
71 int char = data.codeUnitAt(i);
72 if (65 <= char && char <= 90) { // "A" - "Z".
73 value = (value << 6) | char - 65;
74 charCount++;
75 } else if (97 <= char && char <= 122) { // "a" - "z".
76 value = (value << 6) | char - 97 + 26;
77 charCount++;
78 } else if (48 <= char && char <= 57) { // "0" - "9".
79 value = (value << 6) | char - 48 + 52;
80 charCount++;
81 } else if (char == 43) { // "+".
82 value = (value << 6) | 62;
83 charCount++;
84 } else if (char == 47) { // "/".
85 value = (value << 6) | 63;
86 charCount++;
87 } else if (char == 61) { // "=".
88 value = (value << 6);
89 charCount++;
90 padCount++;
91 }
92 if (charCount == 4) {
93 result.add((value & 0xFF0000) >> 16);
94 if (padCount < 2) {
95 result.add((value & 0xFF00) >> 8);
96 }
97 if (padCount == 0) {
98 result.add(value & 0xFF);
99 }
100 charCount = 0;
101 value = 0;
102 }
103 }
104 return result;
105 }
106 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/io/io.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698