4 * Licensed under the BSD 3-Clause License.
5 * http://opensource.org/licenses/BSD-3-Clause
8 * http://en.wikipedia.org/wiki/Base64
10 ;(function (global, factory) {
11 typeof exports === 'object' && typeof module !== 'undefined'
12 ? module.exports = factory(global)
13 : typeof define === 'function' && define.amd
14 ? define(factory) : factory(global)
16 typeof self !== 'undefined' ? self
17 : typeof window !== 'undefined' ? window
18 : typeof global !== 'undefined' ? global
22 // existing version for noConflict()
23 global = global || {};
24 var _Base64 = global.Base64;
25 var version = "2.6.2";
28 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
29 var b64tab = function(bin) {
31 for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;
34 var fromCharCode = String.fromCharCode;
36 var cb_utob = function(c) {
38 var cc = c.charCodeAt(0);
40 : cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6))
41 + fromCharCode(0x80 | (cc & 0x3f)))
42 : (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f))
43 + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
44 + fromCharCode(0x80 | ( cc & 0x3f)));
47 + (c.charCodeAt(0) - 0xD800) * 0x400
48 + (c.charCodeAt(1) - 0xDC00);
49 return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07))
50 + fromCharCode(0x80 | ((cc >>> 12) & 0x3f))
51 + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
52 + fromCharCode(0x80 | ( cc & 0x3f)));
55 var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
56 var utob = function(u) {
57 return u.replace(re_utob, cb_utob);
59 var cb_encode = function(ccc) {
60 var padlen = [0, 2, 1][ccc.length % 3],
61 ord = ccc.charCodeAt(0) << 16
62 | ((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8)
63 | ((ccc.length > 2 ? ccc.charCodeAt(2) : 0)),
65 b64chars.charAt( ord >>> 18),
66 b64chars.charAt((ord >>> 12) & 63),
67 padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63),
68 padlen >= 1 ? '=' : b64chars.charAt(ord & 63)
70 return chars.join('');
72 var btoa = global.btoa && typeof global.btoa == 'function'
73 ? function(b){ return global.btoa(b) } : function(b) {
74 if (b.match(/[^\x00-\xFF]/)) throw new RangeError(
75 'The string contains invalid characters.'
77 return b.replace(/[\s\S]{1,3}/g, cb_encode);
79 var _encode = function(u) {
80 return btoa(utob(String(u)));
82 var mkUriSafe = function (b64) {
83 return b64.replace(/[+\/]/g, function(m0) {
84 return m0 == '+' ? '-' : '_';
87 var encode = function(u, urisafe) {
88 return urisafe ? mkUriSafe(_encode(u)) : _encode(u);
90 var encodeURI = function(u) { return encode(u, true) };
92 if (global.Uint8Array) fromUint8Array = function(a, urisafe) {
93 // return btoa(fromCharCode.apply(null, a));
95 for (var i = 0, l = a.length; i < l; i += 3) {
96 var a0 = a[i], a1 = a[i+1], a2 = a[i+2];
97 var ord = a0 << 16 | a1 << 8 | a2;
98 b64 += b64chars.charAt( ord >>> 18)
99 + b64chars.charAt((ord >>> 12) & 63)
100 + ( typeof a1 != 'undefined'
101 ? b64chars.charAt((ord >>> 6) & 63) : '=')
102 + ( typeof a2 != 'undefined'
103 ? b64chars.charAt( ord & 63) : '=');
105 return urisafe ? mkUriSafe(b64) : b64;
108 var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
109 var cb_btou = function(cccc) {
110 switch(cccc.length) {
112 var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
113 | ((0x3f & cccc.charCodeAt(1)) << 12)
114 | ((0x3f & cccc.charCodeAt(2)) << 6)
115 | (0x3f & cccc.charCodeAt(3)),
116 offset = cp - 0x10000;
117 return (fromCharCode((offset >>> 10) + 0xD800)
118 + fromCharCode((offset & 0x3FF) + 0xDC00));
121 ((0x0f & cccc.charCodeAt(0)) << 12)
122 | ((0x3f & cccc.charCodeAt(1)) << 6)
123 | (0x3f & cccc.charCodeAt(2))
127 ((0x1f & cccc.charCodeAt(0)) << 6)
128 | (0x3f & cccc.charCodeAt(1))
132 var btou = function(b) {
133 return b.replace(re_btou, cb_btou);
135 var cb_decode = function(cccc) {
136 var len = cccc.length,
138 n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0)
139 | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0)
140 | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0)
141 | (len > 3 ? b64tab[cccc.charAt(3)] : 0),
143 fromCharCode( n >>> 16),
144 fromCharCode((n >>> 8) & 0xff),
145 fromCharCode( n & 0xff)
147 chars.length -= [0, 0, 2, 1][padlen];
148 return chars.join('');
150 var _atob = global.atob && typeof global.atob == 'function'
151 ? function(a){ return global.atob(a) } : function(a){
152 return a.replace(/\S{1,4}/g, cb_decode);
154 var atob = function(a) {
155 return _atob(String(a).replace(/[^A-Za-z0-9\+\/]/g, ''));
157 var _decode = function(a) { return btou(_atob(a)) };
158 var decode = function(a){
160 String(a).replace(/[-_]/g, function(m0) {
161 return m0 == '-' ? '+' : '/'
162 }).replace(/[^A-Za-z0-9\+\/]/g, '')
166 if (global.Uint8Array) toUint8Array = function(a) {
167 return Uint8Array.from(atob(a), function(c) {
168 return c.charCodeAt(0);
171 var noConflict = function() {
172 var Base64 = global.Base64;
173 global.Base64 = _Base64;
185 encodeURI: encodeURI,
188 noConflict: noConflict,
189 fromUint8Array: fromUint8Array,
190 toUint8Array: toUint8Array
192 // if ES5 is available, make Base64.extendString() available
193 if (typeof Object.defineProperty === 'function') {
194 var noEnum = function(v){
195 return {value:v,enumerable:false,writable:true,configurable:true};
197 global.Base64.extendString = function () {
198 Object.defineProperty(
199 String.prototype, 'fromBase64', noEnum(function () {
202 Object.defineProperty(
203 String.prototype, 'toBase64', noEnum(function (urisafe) {
204 return encode(this, urisafe)
206 Object.defineProperty(
207 String.prototype, 'toBase64URI', noEnum(function () {
208 return encode(this, true)
213 // export Base64 to the namespace
215 if (global['Meteor']) { // Meteor.js
216 Base64 = global.Base64;
218 // module.exports and AMD are mutually exclusive.
219 // module.exports has precedence.
220 if (typeof module !== 'undefined' && module.exports) {
221 module.exports.Base64 = global.Base64;
223 else if (typeof define === 'function' && define.amd) {
224 // AMD. Register as an anonymous module.
225 define([], function(){ return global.Base64 });
228 return {Base64: global.Base64}