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 class _Base64 { | |
6 static const 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 List<String> characters = new List<String>(); | |
18 int i; | |
19 for (i = 0; i + 3 <= data.length; i += 3) { | |
20 int value = 0; | |
21 value |= data[i + 2]; | |
22 value |= data[i + 1] << 8; | |
23 value |= data[i] << 16; | |
24 for (int j = 0; j < 4; j++) { | |
25 int index = (value >> ((3 - j) * 6)) & ((1 << 6) - 1); | |
26 characters.add(_encodingTable[index]); | |
27 } | |
28 } | |
29 // Remainders. | |
30 if (i + 2 == data.length) { | |
31 int value = 0; | |
32 value |= data[i + 1] << 8; | |
33 value |= data[i] << 16; | |
34 for (int j = 0; j < 3; j++) { | |
35 int index = (value >> ((3 - j) * 6)) & ((1 << 6) - 1); | |
36 characters.add(_encodingTable[index]); | |
37 } | |
38 characters.add("="); | |
39 } else if (i + 1 == data.length) { | |
40 int value = 0; | |
41 value |= data[i] << 16; | |
42 for (int j = 0; j < 2; j++) { | |
43 int index = (value >> ((3 - j) * 6)) & ((1 << 6) - 1); | |
44 characters.add(_encodingTable[index]); | |
45 } | |
46 characters.add("="); | |
47 characters.add("="); | |
48 } | |
49 StringBuffer output = new StringBuffer(); | |
50 for (i = 0; i < characters.length; i++) { | |
51 if (i > 0 && i % 76 == 0) { | |
52 output.add("\r\n"); | |
53 } | |
54 output.add(characters[i]); | |
55 } | |
56 return output.toString(); | |
57 } | |
58 | |
59 | |
60 /** | |
61 * Base64 transfer decoding for MIME (RFC 2045). | |
62 */ | |
63 static List<int> _decode(String data) { | |
64 List<int> result = new List<int>(); | |
65 int padCount = 0; | |
66 int charCount = 0; | |
67 int value = 0; | |
68 for (int i = 0; i < data.length; i++) { | |
69 int char = data.charCodeAt(i); | |
70 if (65 <= char && char <= 90) { // "A" - "Z". | |
71 value = (value << 6) | char - 65; | |
72 charCount++; | |
73 } else if (97 <= char && char <= 122) { // "a" - "z". | |
74 value = (value << 6) | char - 97 + 26; | |
75 charCount++; | |
76 } else if (48 <= char && char <= 57) { // "0" - "9". | |
77 value = (value << 6) | char - 48 + 52; | |
78 charCount++; | |
79 } else if (char == 43) { // "+". | |
80 value = (value << 6) | 62; | |
81 charCount++; | |
82 } else if (char == 47) { // "/". | |
83 value = (value << 6) | 63; | |
84 charCount++; | |
85 } else if (char == 61) { // "=". | |
86 value = (value << 6); | |
87 charCount++; | |
88 padCount++; | |
89 } | |
90 if (charCount == 4) { | |
91 result.add((value & 0xFF0000) >> 16); | |
92 if (padCount < 2) { | |
93 result.add((value & 0xFF00) >> 8); | |
94 } | |
95 if (padCount == 0) { | |
96 result.add(value & 0xFF); | |
97 } | |
98 charCount = 0; | |
99 value = 0; | |
100 } | |
101 } | |
102 return result; | |
103 } | |
104 } | |
OLD | NEW |