base64.js 2.94 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2 3 4 5 6
/**
 *
 *  Base64 encode / decode
 *  http://www.webtoolkit.info/
 *  http://www.webtoolkit.info/licence
 **/
7

Ad Schellevis's avatar
Ad Schellevis committed
8
var Base64 = {
9

Ad Schellevis's avatar
Ad Schellevis committed
10 11
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
12

Ad Schellevis's avatar
Ad Schellevis committed
13 14 15 16 17
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
18

Ad Schellevis's avatar
Ad Schellevis committed
19
		input = Base64._utf8_encode(input);
20

Ad Schellevis's avatar
Ad Schellevis committed
21
		while (i < input.length) {
22

Ad Schellevis's avatar
Ad Schellevis committed
23 24 25
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
26

Ad Schellevis's avatar
Ad Schellevis committed
27 28 29 30
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
31

Ad Schellevis's avatar
Ad Schellevis committed
32 33 34 35 36
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
37

Ad Schellevis's avatar
Ad Schellevis committed
38 39 40
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
41

Ad Schellevis's avatar
Ad Schellevis committed
42
		}
43

Ad Schellevis's avatar
Ad Schellevis committed
44 45
		return output;
	},
46

Ad Schellevis's avatar
Ad Schellevis committed
47 48 49 50 51 52
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
53

Ad Schellevis's avatar
Ad Schellevis committed
54
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
55

Ad Schellevis's avatar
Ad Schellevis committed
56
		while (i < input.length) {
57

Ad Schellevis's avatar
Ad Schellevis committed
58 59 60 61
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
62

Ad Schellevis's avatar
Ad Schellevis committed
63 64 65
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
66

Ad Schellevis's avatar
Ad Schellevis committed
67
			output = output + String.fromCharCode(chr1);
68

Ad Schellevis's avatar
Ad Schellevis committed
69 70 71 72 73 74
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
75

Ad Schellevis's avatar
Ad Schellevis committed
76
		}
77

Ad Schellevis's avatar
Ad Schellevis committed
78
		output = Base64._utf8_decode(output);
79

Ad Schellevis's avatar
Ad Schellevis committed
80
		return output;
81

Ad Schellevis's avatar
Ad Schellevis committed
82
	},
83

Ad Schellevis's avatar
Ad Schellevis committed
84 85 86 87
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
88

Ad Schellevis's avatar
Ad Schellevis committed
89
		for (var n = 0; n < string.length; n++) {
90

Ad Schellevis's avatar
Ad Schellevis committed
91
			var c = string.charCodeAt(n);
92

Ad Schellevis's avatar
Ad Schellevis committed
93 94 95 96 97 98 99 100 101 102 103 104
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
105

Ad Schellevis's avatar
Ad Schellevis committed
106
		}
107

Ad Schellevis's avatar
Ad Schellevis committed
108 109
		return utftext;
	},
110

Ad Schellevis's avatar
Ad Schellevis committed
111 112 113 114 115
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
116

Ad Schellevis's avatar
Ad Schellevis committed
117
		while ( i < utftext.length ) {
118

Ad Schellevis's avatar
Ad Schellevis committed
119
			c = utftext.charCodeAt(i);
120

Ad Schellevis's avatar
Ad Schellevis committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
136

Ad Schellevis's avatar
Ad Schellevis committed
137
		}
138

Ad Schellevis's avatar
Ad Schellevis committed
139 140
		return string;
	}
141 142

};