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

Side by Side Diff: runtime/bin/base64.dart

Issue 10205012: Initial web socket server implementation (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 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 | runtime/bin/http.dart » ('j') | runtime/bin/http.dart » ('J')
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 class _Base64 {
6 static final List<int> _encodingTable = const [
7 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
8 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
9 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
10 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
11 '8', '9', '+', '/'];
12
13 /**
14 * Base64 transfer encoding for MIME (RFC 2045)
15 */
16 static String _encode(List<int> data) {
17 // Endianness matters?
Mads Ager (google) 2012/04/25 10:41:55 Did you resolve this? If not it should probably be
Søren Gjesse 2012/04/25 13:57:14 Old bogus comment - removed. The input is bytes, s
18 List<String> characters = new List<String>();
19 int i;
20 for (i = 0; i + 3 <= data.length; i += 3) {
21 int value = 0;
22 value |= data[i + 2];
23 value |= data[i + 1] << 8;
24 value |= data[i] << 16;
25 for (int j = 0; j < 4; j++) {
26 int index = (value >> ((3 - j) * 6)) & ((1 << 6) - 1);
27 characters.add(_encodingTable[index]);
28 }
29 }
30 // Remainders.
31 if (i + 2 == data.length) {
32 int value = 0;
33 value |= data[i + 1] << 8;
34 value |= data[i] << 16;
35 for (int j = 0; j < 3; j++) {
36 int index = (value >> ((3 - j) * 6)) & ((1 << 6) - 1);
37 characters.add(_encodingTable[index]);
38 }
39 characters.add("=");
40 } else if (i + 1 == data.length) {
41 int value = 0;
42 value |= data[i] << 16;
43 for (int j = 0; j < 2; j++) {
44 int index = (value >> ((3 - j) * 6)) & ((1 << 6) - 1);
45 characters.add(_encodingTable[index]);
46 }
47 characters.add("=");
48 characters.add("=");
49 }
50 StringBuffer output = new StringBuffer();
51 for (i = 0; i < characters.length; i++) {
52 if (i > 0 && i % 76 == 0) {
53 output.add("\r\n");
54 }
55 output.add(characters[i]);
56 }
57 return output.toString();
58 }
59
60
61 /**
62 * Base64 transfer decoding for MIME (RFC 2045).
63 */
64 static List<int> _decode(String data) {
65 List<int> result = new List<int>();
66 int padCount = 0;
67 int charCount = 0;
68 int value = 0;
69 for (int i = 0; i < data.length; i++) {
70 int char = data.charCodeAt(i);
71 if (65 <= char && char <= 90) { // "A" - "Z".
72 value = (value << 6) | char - 65;
73 charCount++;
74 } else if (97 <= char && char <= 122) { // "a" - "z".
75 value = (value << 6) | char - 97 + 26;
76 charCount++;
77 } else if (48 <= char && char <= 57) { // "0" - "9".
78 value = (value << 6) | char - 48 + 52;
79 charCount++;
80 } else if (char == 43) { // "+".
81 value = (value << 6) | 62;
82 charCount++;
83 } else if (char == 47) { // "/".
84 value = (value << 6) | 63;
85 charCount++;
86 } else if (char == 61) { // "=".
87 value = (value << 6);
88 charCount++;
89 padCount++;
90 }
91 if (charCount == 4) {
92 result.add((value & 0xFF0000) >> 16);
93 if (padCount < 2) {
94 result.add((value & 0xFF00) >> 8);
95 }
96 if (padCount == 0) {
97 result.add(value & 0xFF);
98 }
99 charCount = 0;
100 value = 0;
101 }
102 }
103 return result;
104 }
105 }
OLDNEW
« no previous file with comments | « no previous file | runtime/bin/http.dart » ('j') | runtime/bin/http.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698