UNPKG

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