base64.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * base64.js
  3. *
  4. * Licensed under the BSD 3-Clause License.
  5. * http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * References:
  8. * http://en.wikipedia.org/wiki/Base64
  9. */
  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)
  15. }((
  16. typeof self !== 'undefined' ? self
  17. : typeof window !== 'undefined' ? window
  18. : typeof global !== 'undefined' ? global
  19. : this
  20. ), function(global) {
  21. 'use strict';
  22. // existing version for noConflict()
  23. global = global || {};
  24. var _Base64 = global.Base64;
  25. var version = "2.5.2";
  26. // if node.js and NOT React Native, we use Buffer
  27. var buffer;
  28. if (typeof module !== 'undefined' && module.exports) {
  29. try {
  30. buffer = eval("require('buffer').Buffer");
  31. } catch (err) {
  32. buffer = undefined;
  33. }
  34. }
  35. // constants
  36. var b64chars
  37. = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  38. var b64tab = function(bin) {
  39. var t = {};
  40. for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;
  41. return t;
  42. }(b64chars);
  43. var fromCharCode = String.fromCharCode;
  44. // encoder stuff
  45. var cb_utob = function(c) {
  46. if (c.length < 2) {
  47. var cc = c.charCodeAt(0);
  48. return cc < 0x80 ? c
  49. : cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6))
  50. + fromCharCode(0x80 | (cc & 0x3f)))
  51. : (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f))
  52. + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
  53. + fromCharCode(0x80 | ( cc & 0x3f)));
  54. } else {
  55. var cc = 0x10000
  56. + (c.charCodeAt(0) - 0xD800) * 0x400
  57. + (c.charCodeAt(1) - 0xDC00);
  58. return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07))
  59. + fromCharCode(0x80 | ((cc >>> 12) & 0x3f))
  60. + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
  61. + fromCharCode(0x80 | ( cc & 0x3f)));
  62. }
  63. };
  64. var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
  65. var utob = function(u) {
  66. return u.replace(re_utob, cb_utob);
  67. };
  68. var cb_encode = function(ccc) {
  69. var padlen = [0, 2, 1][ccc.length % 3],
  70. ord = ccc.charCodeAt(0) << 16
  71. | ((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8)
  72. | ((ccc.length > 2 ? ccc.charCodeAt(2) : 0)),
  73. chars = [
  74. b64chars.charAt( ord >>> 18),
  75. b64chars.charAt((ord >>> 12) & 63),
  76. padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63),
  77. padlen >= 1 ? '=' : b64chars.charAt(ord & 63)
  78. ];
  79. return chars.join('');
  80. };
  81. var btoa = global.btoa ? function(b) {
  82. return global.btoa(b);
  83. } : function(b) {
  84. return b.replace(/[\s\S]{1,3}/g, cb_encode);
  85. };
  86. var _encode = function(u) {
  87. var isUint8Array = Object.prototype.toString.call(u) === '[object Uint8Array]';
  88. return isUint8Array ? u.toString('base64')
  89. : btoa(utob(String(u)));
  90. }
  91. var encode = function(u, urisafe) {
  92. return !urisafe
  93. ? _encode(u)
  94. : _encode(String(u)).replace(/[+\/]/g, function(m0) {
  95. return m0 == '+' ? '-' : '_';
  96. }).replace(/=/g, '');
  97. };
  98. var encodeURI = function(u) { return encode(u, true) };
  99. // decoder stuff
  100. var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
  101. var cb_btou = function(cccc) {
  102. switch(cccc.length) {
  103. case 4:
  104. var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
  105. | ((0x3f & cccc.charCodeAt(1)) << 12)
  106. | ((0x3f & cccc.charCodeAt(2)) << 6)
  107. | (0x3f & cccc.charCodeAt(3)),
  108. offset = cp - 0x10000;
  109. return (fromCharCode((offset >>> 10) + 0xD800)
  110. + fromCharCode((offset & 0x3FF) + 0xDC00));
  111. case 3:
  112. return fromCharCode(
  113. ((0x0f & cccc.charCodeAt(0)) << 12)
  114. | ((0x3f & cccc.charCodeAt(1)) << 6)
  115. | (0x3f & cccc.charCodeAt(2))
  116. );
  117. default:
  118. return fromCharCode(
  119. ((0x1f & cccc.charCodeAt(0)) << 6)
  120. | (0x3f & cccc.charCodeAt(1))
  121. );
  122. }
  123. };
  124. var btou = function(b) {
  125. return b.replace(re_btou, cb_btou);
  126. };
  127. var cb_decode = function(cccc) {
  128. var len = cccc.length,
  129. padlen = len % 4,
  130. n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0)
  131. | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0)
  132. | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0)
  133. | (len > 3 ? b64tab[cccc.charAt(3)] : 0),
  134. chars = [
  135. fromCharCode( n >>> 16),
  136. fromCharCode((n >>> 8) & 0xff),
  137. fromCharCode( n & 0xff)
  138. ];
  139. chars.length -= [0, 0, 2, 1][padlen];
  140. return chars.join('');
  141. };
  142. var _atob = global.atob ? function(a) {
  143. return global.atob(a);
  144. } : function(a){
  145. return a.replace(/\S{1,4}/g, cb_decode);
  146. };
  147. var atob = function(a) {
  148. return _atob(String(a).replace(/[^A-Za-z0-9\+\/]/g, ''));
  149. };
  150. var _decode = buffer ?
  151. buffer.from && Uint8Array && buffer.from !== Uint8Array.from
  152. ? function(a) {
  153. return (a.constructor === buffer.constructor
  154. ? a : buffer.from(a, 'base64')).toString();
  155. }
  156. : function(a) {
  157. return (a.constructor === buffer.constructor
  158. ? a : new buffer(a, 'base64')).toString();
  159. }
  160. : function(a) { return btou(_atob(a)) };
  161. var decode = function(a){
  162. return _decode(
  163. String(a).replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' })
  164. .replace(/[^A-Za-z0-9\+\/]/g, '')
  165. );
  166. };
  167. var noConflict = function() {
  168. var Base64 = global.Base64;
  169. global.Base64 = _Base64;
  170. return Base64;
  171. };
  172. // export Base64
  173. global.Base64 = {
  174. VERSION: version,
  175. atob: atob,
  176. btoa: btoa,
  177. fromBase64: decode,
  178. toBase64: encode,
  179. utob: utob,
  180. encode: encode,
  181. encodeURI: encodeURI,
  182. btou: btou,
  183. decode: decode,
  184. noConflict: noConflict,
  185. __buffer__: buffer
  186. };
  187. // if ES5 is available, make Base64.extendString() available
  188. if (typeof Object.defineProperty === 'function') {
  189. var noEnum = function(v){
  190. return {value:v,enumerable:false,writable:true,configurable:true};
  191. };
  192. global.Base64.extendString = function () {
  193. Object.defineProperty(
  194. String.prototype, 'fromBase64', noEnum(function () {
  195. return decode(this)
  196. }));
  197. Object.defineProperty(
  198. String.prototype, 'toBase64', noEnum(function (urisafe) {
  199. return encode(this, urisafe)
  200. }));
  201. Object.defineProperty(
  202. String.prototype, 'toBase64URI', noEnum(function () {
  203. return encode(this, true)
  204. }));
  205. };
  206. }
  207. //
  208. // export Base64 to the namespace
  209. //
  210. if (global['Meteor']) { // Meteor.js
  211. Base64 = global.Base64;
  212. }
  213. // module.exports and AMD are mutually exclusive.
  214. // module.exports has precedence.
  215. if (typeof module !== 'undefined' && module.exports) {
  216. module.exports.Base64 = global.Base64;
  217. }
  218. else if (typeof define === 'function' && define.amd) {
  219. // AMD. Register as an anonymous module.
  220. define([], function(){ return global.Base64 });
  221. }
  222. // that's it!
  223. return {Base64: global.Base64}
  224. }));