Skip to content

Commit 764875c

Browse files
committed
Create base64.js
1 parent 0a14538 commit 764875c

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

jspdf/libs/base64.js

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

0 commit comments

Comments
 (0)