base64.js 7.8 KB

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