|
1 // Base64 encoder/decoder - pulled from http://ostermiller.org/calc/encode.html. Licensed under the GPL. |
|
2 |
|
3 var END_OF_INPUT = -1; |
|
4 |
|
5 var base64Chars = new Array( |
|
6 'A','B','C','D','E','F','G','H', |
|
7 'I','J','K','L','M','N','O','P', |
|
8 'Q','R','S','T','U','V','W','X', |
|
9 'Y','Z','a','b','c','d','e','f', |
|
10 'g','h','i','j','k','l','m','n', |
|
11 'o','p','q','r','s','t','u','v', |
|
12 'w','x','y','z','0','1','2','3', |
|
13 '4','5','6','7','8','9','+','/' |
|
14 ); |
|
15 |
|
16 var reverseBase64Chars = new Array(); |
|
17 for (var i=0; i < base64Chars.length; i++){ |
|
18 reverseBase64Chars[base64Chars[i]] = i; |
|
19 } |
|
20 |
|
21 var base64Str; |
|
22 var base64Count; |
|
23 |
|
24 function setBase64Str(str){ |
|
25 base64Str = str; |
|
26 base64Count = 0; |
|
27 } |
|
28 function readBase64(){ |
|
29 if (!base64Str) return END_OF_INPUT; |
|
30 if (base64Count >= base64Str.length) return END_OF_INPUT; |
|
31 var c = base64Str.charCodeAt(base64Count) & 0xff; |
|
32 base64Count++; |
|
33 return c; |
|
34 } |
|
35 function encodeBase64(str){ |
|
36 setBase64Str(str); |
|
37 var result = ''; |
|
38 var inBuffer = new Array(3); |
|
39 var lineCount = 0; |
|
40 var done = false; |
|
41 while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT){ |
|
42 inBuffer[1] = readBase64(); |
|
43 inBuffer[2] = readBase64(); |
|
44 result += (base64Chars[ inBuffer[0] >> 2 ]); |
|
45 if (inBuffer[1] != END_OF_INPUT){ |
|
46 result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]); |
|
47 if (inBuffer[2] != END_OF_INPUT){ |
|
48 result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]); |
|
49 result += (base64Chars [inBuffer[2] & 0x3F]); |
|
50 } else { |
|
51 result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]); |
|
52 result += ('='); |
|
53 done = true; |
|
54 } |
|
55 } else { |
|
56 result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]); |
|
57 result += ('='); |
|
58 result += ('='); |
|
59 done = true; |
|
60 } |
|
61 lineCount += 4; |
|
62 if (lineCount >= 76){ |
|
63 result += ('\n'); |
|
64 lineCount = 0; |
|
65 } |
|
66 } |
|
67 return result; |
|
68 } |
|
69 function readReverseBase64(){ |
|
70 if (!base64Str) return END_OF_INPUT; |
|
71 while (true){ |
|
72 if (base64Count >= base64Str.length) return END_OF_INPUT; |
|
73 var nextCharacter = base64Str.charAt(base64Count); |
|
74 base64Count++; |
|
75 if (reverseBase64Chars[nextCharacter]){ |
|
76 return reverseBase64Chars[nextCharacter]; |
|
77 } |
|
78 if (nextCharacter == 'A') return 0; |
|
79 } |
|
80 return END_OF_INPUT; |
|
81 } |
|
82 |
|
83 function ntos(n){ |
|
84 n=n.toString(16); |
|
85 if (n.length == 1) n="0"+n; |
|
86 n="%"+n; |
|
87 return unescape(n); |
|
88 } |
|
89 |
|
90 function decodeBase64(str){ |
|
91 setBase64Str(str); |
|
92 var result = ""; |
|
93 var inBuffer = new Array(4); |
|
94 var done = false; |
|
95 while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT |
|
96 && (inBuffer[1] = readReverseBase64()) != END_OF_INPUT){ |
|
97 inBuffer[2] = readReverseBase64(); |
|
98 inBuffer[3] = readReverseBase64(); |
|
99 result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4)); |
|
100 if (inBuffer[2] != END_OF_INPUT){ |
|
101 result += ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2)); |
|
102 if (inBuffer[3] != END_OF_INPUT){ |
|
103 result += ntos((((inBuffer[2] << 6) & 0xff) | inBuffer[3])); |
|
104 } else { |
|
105 done = true; |
|
106 } |
|
107 } else { |
|
108 done = true; |
|
109 } |
|
110 } |
|
111 return result; |
|
112 } |
|
113 |