base64.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.4.9";
  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\S]{1,4}/g, cb_decode);
  156. };
  157. var _decode = buffer ?
  158. buffer.from && Uint8Array && buffer.from !== Uint8Array.from
  159. ? function(a) {
  160. return (a.constructor === buffer.constructor
  161. ? a : buffer.from(a, 'base64')).toString();
  162. }
  163. : function(a) {
  164. return (a.constructor === buffer.constructor
  165. ? a : new buffer(a, 'base64')).toString();
  166. }
  167. : function(a) { return btou(atob(a)) };
  168. var decode = function(a){
  169. return _decode(
  170. String(a).replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' })
  171. .replace(/[^A-Za-z0-9\+\/]/g, '')
  172. );
  173. };
  174. var noConflict = function() {
  175. var Base64 = global.Base64;
  176. global.Base64 = _Base64;
  177. return Base64;
  178. };
  179. // export Base64
  180. global.Base64 = {
  181. VERSION: version,
  182. atob: atob,
  183. btoa: btoa,
  184. fromBase64: decode,
  185. toBase64: encode,
  186. utob: utob,
  187. encode: encode,
  188. encodeURI: encodeURI,
  189. btou: btou,
  190. decode: decode,
  191. noConflict: noConflict,
  192. __buffer__: buffer
  193. };
  194. // if ES5 is available, make Base64.extendString() available
  195. if (typeof Object.defineProperty === 'function') {
  196. var noEnum = function(v){
  197. return {value:v,enumerable:false,writable:true,configurable:true};
  198. };
  199. global.Base64.extendString = function () {
  200. Object.defineProperty(
  201. String.prototype, 'fromBase64', noEnum(function () {
  202. return decode(this)
  203. }));
  204. Object.defineProperty(
  205. String.prototype, 'toBase64', noEnum(function (urisafe) {
  206. return encode(this, urisafe)
  207. }));
  208. Object.defineProperty(
  209. String.prototype, 'toBase64URI', noEnum(function () {
  210. return encode(this, true)
  211. }));
  212. };
  213. }
  214. //
  215. // export Base64 to the namespace
  216. //
  217. if (global['Meteor']) { // Meteor.js
  218. Base64 = global.Base64;
  219. }
  220. // module.exports and AMD are mutually exclusive.
  221. // module.exports has precedence.
  222. if (typeof module !== 'undefined' && module.exports) {
  223. module.exports.Base64 = global.Base64;
  224. }
  225. else if (typeof define === 'function' && define.amd) {
  226. // AMD. Register as an anonymous module.
  227. define([], function(){ return global.Base64 });
  228. }
  229. // that's it!
  230. return {Base64: global.Base64}
  231. }));