base64.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. if (Uint8Array)
  2. {
  3. var _slice = Uint8Array.prototype.slice;
  4. try {
  5. // Can't be used with DOM elements in IE < 9
  6. _slice.call(document.documentElement);
  7. } catch (e) { // Fails in IE < 9
  8. // This will work for genuine arrays, array-like objects,
  9. // NamedNodeMap (attributes, entities, notations),
  10. // NodeList (e.g., getElementsByTagName), HTMLCollection (e.g., childNodes),
  11. // and will not fail on other DOM objects (as do DOM elements in IE < 9)
  12. Uint8Array.prototype.slice = function(begin, end) {
  13. // IE < 9 gets unhappy with an undefined end argument
  14. end = (typeof end !== 'undefined') ? end : this.length;
  15. // For native Array objects, we use the native slice function
  16. if (Object.prototype.toString.call(this) === '[object Array]'){
  17. return _slice.call(this, begin, end);
  18. }
  19. // For array like object we handle it ourselves.
  20. var i, cloned = [],
  21. size, len = this.length;
  22. // Handle negative value for "begin"
  23. var start = begin || 0;
  24. start = (start >= 0) ? start : Math.max(0, len + start);
  25. // Handle negative value for "end"
  26. var upTo = (typeof end == 'number') ? Math.min(end, len) : len;
  27. if (end < 0) {
  28. upTo = len + end;
  29. }
  30. // Actual expected size of the slice
  31. size = upTo - start;
  32. if (size > 0) {
  33. cloned = new Array(size);
  34. if (this.charAt) {
  35. for (i = 0; i < size; i++) {
  36. cloned[i] = this.charAt(start + i);
  37. }
  38. } else {
  39. for (i = 0; i < size; i++) {
  40. cloned[i] = this[start + i];
  41. }
  42. }
  43. }
  44. return cloned;
  45. };
  46. }
  47. if (!Uint8Array.from) {
  48. Uint8Array.from = (function () {
  49. var toStr = Object.prototype.toString;
  50. var isCallable = function (fn) {
  51. return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
  52. };
  53. var toInteger = function (value) {
  54. var number = Number(value);
  55. if (isNaN(number)) { return 0; }
  56. if (number === 0 || !isFinite(number)) { return number; }
  57. return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
  58. };
  59. var maxSafeInteger = Math.pow(2, 53) - 1;
  60. var toLength = function (value) {
  61. var len = toInteger(value);
  62. return Math.min(Math.max(len, 0), maxSafeInteger);
  63. };
  64. var toItems = function (value) {
  65. // support set
  66. if (value.size > 0 && value.values) {
  67. var values = value.values();
  68. var it = values.next();
  69. var o = [];
  70. while (!it.done) {
  71. o.push(it.value);
  72. it = values.next();
  73. }
  74. return o;
  75. }
  76. return Object(value);
  77. };
  78. // The length property of the from method is 1.
  79. return function from(arrayLike/*, mapFn, thisArg */) {
  80. // 1. Let C be the this value.
  81. var C = this;
  82. // 2. Let items be ToObject(arrayLike).
  83. var items = toItems(arrayLike);
  84. // 3. ReturnIfAbrupt(items).
  85. if (arrayLike == null) {
  86. throw new TypeError("Array.from requires an array-like object - not null or undefined");
  87. }
  88. // 4. If mapfn is undefined, then let mapping be false.
  89. var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
  90. var T;
  91. if (typeof mapFn !== 'undefined') {
  92. // 5. else
  93. // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
  94. if (!isCallable(mapFn)) {
  95. throw new TypeError('Array.from: when provided, the second argument must be a function');
  96. }
  97. // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
  98. if (arguments.length > 2) {
  99. T = arguments[2];
  100. }
  101. }
  102. // 10. Let lenValue be Get(items, "length").
  103. // 11. Let len be ToLength(lenValue).
  104. var len = toLength(items.length);
  105. // 13. If IsConstructor(C) is true, then
  106. // 13. a. Let A be the result of calling the [[Construct]] internal method
  107. // of C with an argument list containing the single item len.
  108. // 14. a. Else, Let A be ArrayCreate(len).
  109. var A = isCallable(C) ? Object(new C(len)) : new Array(len);
  110. // 16. Let k be 0.
  111. var k = 0;
  112. // 17. Repeat, while k < len�� (also steps a - h)
  113. var kValue;
  114. while (k < len) {
  115. kValue = items[k];
  116. if (mapFn) {
  117. A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
  118. } else {
  119. A[k] = kValue;
  120. }
  121. k += 1;
  122. }
  123. // 18. Let putStatus be Put(A, "length", len, true).
  124. A.length = len;
  125. // 20. Return A.
  126. return A;
  127. };
  128. }());
  129. }
  130. }
  131. ;(function (global, factory) {
  132. typeof exports === 'object' && typeof module !== 'undefined'
  133. ? module.exports = factory(global)
  134. : typeof define === 'function' && define.amd
  135. ? define(factory) : factory(global)
  136. }((
  137. typeof self !== 'undefined' ? self
  138. : typeof window !== 'undefined' ? window
  139. : typeof global !== 'undefined' ? global
  140. : this
  141. ), function(global) {
  142. 'use strict';
  143. // existing version for noConflict()
  144. global = global || {};
  145. var _Base64 = global.Base64;
  146. var version = "2.6.2";
  147. // constants
  148. var b64chars
  149. = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  150. var b64tab = function(bin) {
  151. var t = {};
  152. for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;
  153. return t;
  154. }(b64chars);
  155. var fromCharCode = String.fromCharCode;
  156. // encoder stuff
  157. var cb_utob = function(c) {
  158. if (c.length < 2) {
  159. var cc = c.charCodeAt(0);
  160. return cc < 0x80 ? c
  161. : cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6))
  162. + fromCharCode(0x80 | (cc & 0x3f)))
  163. : (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f))
  164. + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
  165. + fromCharCode(0x80 | ( cc & 0x3f)));
  166. } else {
  167. var cc = 0x10000
  168. + (c.charCodeAt(0) - 0xD800) * 0x400
  169. + (c.charCodeAt(1) - 0xDC00);
  170. return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07))
  171. + fromCharCode(0x80 | ((cc >>> 12) & 0x3f))
  172. + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
  173. + fromCharCode(0x80 | ( cc & 0x3f)));
  174. }
  175. };
  176. var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
  177. var utob = function(u) {
  178. return u.replace(re_utob, cb_utob);
  179. };
  180. var cb_encode = function(ccc) {
  181. var padlen = [0, 2, 1][ccc.length % 3],
  182. ord = ccc.charCodeAt(0) << 16
  183. | ((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8)
  184. | ((ccc.length > 2 ? ccc.charCodeAt(2) : 0)),
  185. chars = [
  186. b64chars.charAt( ord >>> 18),
  187. b64chars.charAt((ord >>> 12) & 63),
  188. padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63),
  189. padlen >= 1 ? '=' : b64chars.charAt(ord & 63)
  190. ];
  191. return chars.join('');
  192. };
  193. var btoa = global.btoa && typeof global.btoa == 'function'
  194. ? function(b){ return global.btoa(b) } : function(b) {
  195. if (b.match(/[^\x00-\xFF]/)) throw new RangeError(
  196. 'The string contains invalid characters.'
  197. );
  198. return b.replace(/[\s\S]{1,3}/g, cb_encode);
  199. };
  200. var _encode = function(u) {
  201. return btoa(utob(String(u)));
  202. };
  203. var mkUriSafe = function (b64) {
  204. return b64.replace(/[+\/]/g, function(m0) {
  205. return m0 == '+' ? '-' : '_';
  206. }).replace(/=/g, '');
  207. };
  208. var encode = function(u, urisafe) {
  209. return urisafe ? mkUriSafe(_encode(u)) : _encode(u);
  210. };
  211. var encodeURI = function(u) { return encode(u, true) };
  212. var fromUint8Array;
  213. if (global.Uint8Array) fromUint8Array = function(a, urisafe) {
  214. // return btoa(fromCharCode.apply(null, a));
  215. var b64 = '';
  216. for (var i = 0, l = a.length; i < l; i += 3) {
  217. var a0 = a[i], a1 = a[i+1], a2 = a[i+2];
  218. var ord = a0 << 16 | a1 << 8 | a2;
  219. b64 += b64chars.charAt( ord >>> 18)
  220. + b64chars.charAt((ord >>> 12) & 63)
  221. + ( typeof a1 != 'undefined'
  222. ? b64chars.charAt((ord >>> 6) & 63) : '=')
  223. + ( typeof a2 != 'undefined'
  224. ? b64chars.charAt( ord & 63) : '=');
  225. }
  226. return urisafe ? mkUriSafe(b64) : b64;
  227. };
  228. // decoder stuff
  229. var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
  230. var cb_btou = function(cccc) {
  231. switch(cccc.length) {
  232. case 4:
  233. var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
  234. | ((0x3f & cccc.charCodeAt(1)) << 12)
  235. | ((0x3f & cccc.charCodeAt(2)) << 6)
  236. | (0x3f & cccc.charCodeAt(3)),
  237. offset = cp - 0x10000;
  238. return (fromCharCode((offset >>> 10) + 0xD800)
  239. + fromCharCode((offset & 0x3FF) + 0xDC00));
  240. case 3:
  241. return fromCharCode(
  242. ((0x0f & cccc.charCodeAt(0)) << 12)
  243. | ((0x3f & cccc.charCodeAt(1)) << 6)
  244. | (0x3f & cccc.charCodeAt(2))
  245. );
  246. default:
  247. return fromCharCode(
  248. ((0x1f & cccc.charCodeAt(0)) << 6)
  249. | (0x3f & cccc.charCodeAt(1))
  250. );
  251. }
  252. };
  253. var btou = function(b) {
  254. return b.replace(re_btou, cb_btou);
  255. };
  256. var cb_decode = function(cccc) {
  257. var len = cccc.length,
  258. padlen = len % 4,
  259. n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0)
  260. | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0)
  261. | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0)
  262. | (len > 3 ? b64tab[cccc.charAt(3)] : 0),
  263. chars = [
  264. fromCharCode( n >>> 16),
  265. fromCharCode((n >>> 8) & 0xff),
  266. fromCharCode( n & 0xff)
  267. ];
  268. chars.length -= [0, 0, 2, 1][padlen];
  269. return chars.join('');
  270. };
  271. var _atob = global.atob && typeof global.atob == 'function'
  272. ? function(a){ return global.atob(a) } : function(a){
  273. return a.replace(/\S{1,4}/g, cb_decode);
  274. };
  275. var atob = function(a) {
  276. return _atob(String(a).replace(/[^A-Za-z0-9\+\/]/g, ''));
  277. };
  278. var _decode = function(a) { return btou(_atob(a)) };
  279. var decode = function(a){
  280. return _decode(
  281. String(a).replace(/[-_]/g, function(m0) {
  282. return m0 == '-' ? '+' : '/'
  283. }).replace(/[^A-Za-z0-9\+\/]/g, '')
  284. );
  285. };
  286. var toUint8Array;
  287. if (global.Uint8Array) toUint8Array = function(a) {
  288. return Uint8Array.from(atob(a), function(c) {
  289. return c.charCodeAt(0);
  290. });
  291. };
  292. var noConflict = function() {
  293. var Base64 = global.Base64;
  294. global.Base64 = _Base64;
  295. return Base64;
  296. };
  297. // export Base64
  298. global.Base64 = {
  299. VERSION: version,
  300. atob: atob,
  301. btoa: btoa,
  302. fromBase64: decode,
  303. toBase64: encode,
  304. utob: utob,
  305. encode: encode,
  306. encodeURI: encodeURI,
  307. btou: btou,
  308. decode: decode,
  309. noConflict: noConflict,
  310. fromUint8Array: fromUint8Array,
  311. toUint8Array: toUint8Array
  312. };
  313. // if ES5 is available, make Base64.extendString() available
  314. if (typeof Object.defineProperty === 'function') {
  315. var noEnum = function(v){
  316. return {value:v,enumerable:false,writable:true,configurable:true};
  317. };
  318. global.Base64.extendString = function () {
  319. Object.defineProperty(
  320. String.prototype, 'fromBase64', noEnum(function () {
  321. return decode(this)
  322. }));
  323. Object.defineProperty(
  324. String.prototype, 'toBase64', noEnum(function (urisafe) {
  325. return encode(this, urisafe)
  326. }));
  327. Object.defineProperty(
  328. String.prototype, 'toBase64URI', noEnum(function () {
  329. return encode(this, true)
  330. }));
  331. };
  332. }
  333. //
  334. // export Base64 to the namespace
  335. //
  336. if (global['Meteor']) { // Meteor.js
  337. Base64 = global.Base64;
  338. }
  339. // module.exports and AMD are mutually exclusive.
  340. // module.exports has precedence.
  341. if (typeof module !== 'undefined' && module.exports) {
  342. module.exports.Base64 = global.Base64;
  343. }
  344. else if (typeof define === 'function' && define.amd) {
  345. // AMD. Register as an anonymous module.
  346. define([], function(){ return global.Base64 });
  347. }
  348. // that's it!
  349. return {Base64: global.Base64}
  350. }));