3 if (global.Base64) return;
5 // if node.js, we use Buffer
7 if (typeof module !== 'undefined' && module.exports) {
8 buffer = require('buffer').Buffer;
12 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
13 var b64tab = function(bin) {
15 for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;
18 var fromCharCode = String.fromCharCode;
20 var cb_utob = function(c) {
22 var cc = c.charCodeAt(0);
24 : cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6))
25 + fromCharCode(0x80 | (cc & 0x3f)))
26 : (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f))
27 + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
28 + fromCharCode(0x80 | ( cc & 0x3f)));
31 + (c.charCodeAt(0) - 0xD800) * 0x400
32 + (c.charCodeAt(1) - 0xDC00);
33 return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07))
34 + fromCharCode(0x80 | ((cc >>> 12) & 0x3f))
35 + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
36 + fromCharCode(0x80 | ( cc & 0x3f)));
39 var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
40 var utob = function(u) {
41 return u.replace(re_utob, cb_utob);
43 var cb_encode = function(ccc) {
44 var padlen = [0, 2, 1][ccc.length % 3],
45 ord = ccc.charCodeAt(0) << 16
46 | ((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8)
47 | ((ccc.length > 2 ? ccc.charCodeAt(2) : 0)),
49 b64chars.charAt( ord >>> 18),
50 b64chars.charAt((ord >>> 12) & 63),
51 padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63),
52 padlen >= 1 ? '=' : b64chars.charAt(ord & 63)
54 return chars.join('');
56 var btoa = global.btoa || function(b) {
57 return b.replace(/[\s\S]{1,3}/g, cb_encode);
60 ? function (u) { return _utf8_encode((new buffer(u)).toString('base64')) }
61 : function (u) { return _utf8_encode(btoa(utob(u))) }
63 var encode = function(u, urisafe) {
66 : _encode(u).replace(/[+\/]/g, function(m0) {
67 return m0 == '+' ? '-' : '_';
70 var encodeURI = function(u) { return encode(u, true) };
72 var re_btou = new RegExp([
73 '[\xC0-\xDF][\x80-\xBF]',
74 '[\xE0-\xEF][\x80-\xBF]{2}',
75 '[\xF0-\xF7][\x80-\xBF]{3}'
77 var cb_btou = function(cccc) {
80 var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
81 | ((0x3f & cccc.charCodeAt(1)) << 12)
82 | ((0x3f & cccc.charCodeAt(2)) << 6)
83 | (0x3f & cccc.charCodeAt(3)),
84 offset = cp - 0x10000;
85 return (fromCharCode((offset >>> 10) + 0xD800)
86 + fromCharCode((offset & 0x3FF) + 0xDC00));
89 ((0x0f & cccc.charCodeAt(0)) << 12)
90 | ((0x3f & cccc.charCodeAt(1)) << 6)
91 | (0x3f & cccc.charCodeAt(2))
95 ((0x1f & cccc.charCodeAt(0)) << 6)
96 | (0x3f & cccc.charCodeAt(1))
100 var _utf8_encode = function ( string ) {
101 string = string.replace(/\r\n/g,"\n");
104 for (var n = 0; n < string.length; n++) {
106 var c = string.charCodeAt(n);
109 utftext += String.fromCharCode(c);
111 else if((c > 127) && (c < 2048)) {
112 utftext += String.fromCharCode((c >> 6) | 192);
113 utftext += String.fromCharCode((c & 63) | 128);
116 utftext += String.fromCharCode((c >> 12) | 224);
117 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
118 utftext += String.fromCharCode((c & 63) | 128);
125 var _utf8_decode = function (utftext) {
130 while ( i < utftext.length ) {
132 c = utftext.charCodeAt(i);
135 string += String.fromCharCode(c);
138 else if((c > 191) && (c < 224)) {
139 c2 = utftext.charCodeAt(i+1);
140 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
144 c2 = utftext.charCodeAt(i+1);
145 c3 = utftext.charCodeAt(i+2);
146 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
154 var btou = function(b) {
155 return b.replace(re_btou, cb_btou);
157 var cb_decode = function(cccc) {
158 var len = cccc.length,
160 n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0)
161 | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0)
162 | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0)
163 | (len > 3 ? b64tab[cccc.charAt(3)] : 0),
165 fromCharCode( n >>> 16),
166 fromCharCode((n >>> 8) & 0xff),
167 fromCharCode( n & 0xff)
169 chars.length -= [0, 0, 2, 1][padlen];
170 return chars.join('');
172 var atob = global.atob || function(a){
173 return a.replace(/[\s\S]{1,4}/g, cb_decode);
176 ? function(a) { return (new buffer(a, 'base64')).toString() }
177 : function(a) { return btou(atob(a)) };
178 var decode = function(a){
179 a = _utf8_decode( a );
181 a.replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' })
182 .replace(/[^A-Za-z0-9\+\/]/g, '')
194 encodeURI: encodeURI,
198 // if ES5 is available, make Base64.extendString() available
199 if (typeof Object.defineProperty === 'function') {
200 var noEnum = function(v){
201 return {value:v,enumerable:false,writable:true,configurable:true};
203 global.Base64.extendString = function () {
204 Object.defineProperty(
205 String.prototype, 'fromBase64', noEnum(function () {
208 Object.defineProperty(
209 String.prototype, 'toBase64', noEnum(function (urisafe) {
210 return encode(this, urisafe)
212 Object.defineProperty(
213 String.prototype, 'toBase64URI', noEnum(function () {
214 return encode(this, true)