base64.js 8.1 KB

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