6 base64
.ALPHA
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
7 base64
.getbyte64
= function(s
,i
) {
8 // This is oddly fast, except on Chrome/V8.
9 // Minimal or no improvement in performance by using a
10 // object with properties mapping chars to value (eg. 'A': 0)
11 var idx
= base64
.ALPHA
.indexOf(s
.charAt(i
));
13 throw "Cannot decode base64";
18 base64
.decode
= function(s
) {
21 var getbyte64
= base64
.getbyte64
;
29 throw "Cannot decode base64";
33 if (s
.charAt(imax
-1) == base64
.PADCHAR
) {
35 if (s
.charAt(imax
-2) == base64
.PADCHAR
) {
38 // either way, we want to ignore this last block
43 for (i
= 0; i
< imax
; i
+= 4) {
44 b10
= (getbyte64(s
,i
) << 18) |
(getbyte64(s
,i
+1) << 12) |
45 (getbyte64(s
,i
+2) << 6) |
getbyte64(s
,i
+3);
46 x
.push(String
.fromCharCode(b10
>> 16, (b10
>> 8) & 0xff, b10
& 0xff));
51 b10
= (getbyte64(s
,i
) << 18) |
(getbyte64(s
,i
+1) << 12) |
(getbyte64(s
,i
+2) << 6)
52 x
.push(String
.fromCharCode(b10
>> 16, (b10
>> 8) & 0xff));
55 b10
= (getbyte64(s
,i
) << 18) |
(getbyte64(s
,i
+1) << 12);
56 x
.push(String
.fromCharCode(b10
>> 16));
62 base64
.getbyte
= function(s
,i
) {
63 var x
= s
.charCodeAt(i
);
65 throw "INVALID_CHARACTER_ERR: DOM Exception 5";
71 base64
.encode
= function(s
) {
72 if (arguments
.length
!= 1) {
73 throw "SyntaxError: Not enough arguments";
75 var padchar
= base64
.PADCHAR
;
76 var alpha
= base64
.ALPHA
;
77 var getbyte
= base64
.getbyte
;
85 var imax
= s
.length
- s
.length
% 3;
90 for (i
= 0; i
< imax
; i
+= 3) {
91 b10
= (getbyte(s
,i
) << 16) |
(getbyte(s
,i
+1) << 8) |
getbyte(s
,i
+2);
92 x
.push(alpha
.charAt(b10
>> 18));
93 x
.push(alpha
.charAt((b10
>> 12) & 0x3F));
94 x
.push(alpha
.charAt((b10
>> 6) & 0x3f));
95 x
.push(alpha
.charAt(b10
& 0x3f));
97 switch (s
.length
- imax
) {
99 b10
= getbyte(s
,i
) << 16;
100 x
.push(alpha
.charAt(b10
>> 18) + alpha
.charAt((b10
>> 12) & 0x3F) +
104 b10
= (getbyte(s
,i
) << 16) |
(getbyte(s
,i
+1) << 8);
105 x
.push(alpha
.charAt(b10
>> 18) + alpha
.charAt((b10
>> 12) & 0x3F) +
106 alpha
.charAt((b10
>> 6) & 0x3f) + padchar
);