blob: 8ab9944859770d01d0f5f25e56fa89a2ff09d292 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001var Base64 = {
2
3 // private property
4 _basekey : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
5
6 // public method for decoding
7 decode : function (input, isBinaryData) {
8 if(!input) return "";
9 var output = "";
10 var chr1, chr2, chr3;
11 var enc1, enc2, enc3, enc4;
12 var i = 0;
13
14 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
15
16 while (i < input.length) {
17
18 enc1 = this._basekey.indexOf(input.charAt(i++));
19 enc2 = this._basekey.indexOf(input.charAt(i++));
20 enc3 = this._basekey.indexOf(input.charAt(i++));
21 enc4 = this._basekey.indexOf(input.charAt(i++));
22
23 chr1 = (enc1 << 2) | (enc2 >> 4);
24 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
25 chr3 = ((enc3 & 3) << 6) | enc4;
26
27 output = output + String.fromCharCode(chr1);
28
29 if (enc3 != 64) {
30 output = output + String.fromCharCode(chr2);
31 }
32 if (enc4 != 64) {
33 output = output + String.fromCharCode(chr3);
34 }
35
36 }
37
38 if (!isBinaryData) {
39 output = Base64._utf8_decode(output);
40 }
41
42 return output;
43
44 },
45
46// public method for encoding
47 encode : function (input, isBinaryData) {
48 if(!input) return "";
49 var output = "";
50 var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
51 var i = 0;
52
53 if (!isBinaryData) {
54 input = Base64._utf8_encode(input);
55 }
56
57 while (i < input.length) {
58
59 chr1 = input.charCodeAt(i++);
60 chr2 = input.charCodeAt(i++);
61 chr3 = input.charCodeAt(i++);
62
63 enc1 = chr1 >> 2;
64 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
65 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
66 enc4 = chr3 & 63;
67
68 if (isNaN(chr2)) {
69 enc3 = enc4 = 64;
70 } else if (isNaN(chr3)) {
71 enc4 = 64;
72 }
73
74 output = output +
75 this._basekey.charAt(enc1) + this._basekey.charAt(enc2) +
76 this._basekey.charAt(enc3) + this._basekey.charAt(enc4);
77
78 }
79
80 return output;
81 },
82
83// private method for UTF-8 decoding
84 _utf8_decode : function (utftext) {
85 var string = "";
86 var i = 0;
87 //var c = c1 = c2 = 0; cov_2
88 var c = 0;
89 var c1 = 0;
90 var c2 = 0;
91
92 while ( i < utftext.length ) {
93
94 c = utftext.charCodeAt(i);
95
96 if (c < 128) {
97 string += String.fromCharCode(c);
98 i++;
99 }
100 else if((c > 191) && (c < 224)) {
101 c2 = utftext.charCodeAt(i+1);
102 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
103 i += 2;
104 }
105 else {
106 c2 = utftext.charCodeAt(i+1);
107 c3 = utftext.charCodeAt(i+2);
108 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
109 i += 3;
110 }
111
112 }
113
114 return string;
115 },
116
117 // private method for UTF-8 encoding
118 _utf8_encode : function (string) {
119 string = string.replace(/\r\n/g,"\n");
120 var utftext = "";
121
122 for (var n = 0; n < string.length; n++) {
123
124 var c = string.charCodeAt(n);
125
126 if (c < 128) {
127 utftext += String.fromCharCode(c);
128 }
129 else if((c > 127) && (c < 2048)) {
130 utftext += String.fromCharCode((c >> 6) | 192);
131 utftext += String.fromCharCode((c & 63) | 128);
132 }
133 else {
134 utftext += String.fromCharCode((c >> 12) | 224);
135 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
136 utftext += String.fromCharCode((c & 63) | 128);
137 }
138
139 }
140
141 return utftext;
142 }
143
144
145}