Base64.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.5";
  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 && Uint8Array && buffer.from !== Uint8Array.from
  85. ? function (u) {
  86. return (u.constructor === buffer.constructor ? u : buffer.from(u))
  87. .toString('base64')
  88. }
  89. : function (u) {
  90. return (u.constructor === buffer.constructor ? u : new buffer(u))
  91. .toString('base64')
  92. }
  93. : function (u) { return btoa(utob(u)) }
  94. ;
  95. var encode = function(u, urisafe) {
  96. return !urisafe
  97. ? _encode(String(u))
  98. : _encode(String(u)).replace(/[+\/]/g, function(m0) {
  99. return m0 == '+' ? '-' : '_';
  100. }).replace(/=/g, '');
  101. };
  102. var encodeURI = function(u) { return encode(u, true) };
  103. // decoder stuff
  104. var re_btou = new RegExp([
  105. '[\xC0-\xDF][\x80-\xBF]',
  106. '[\xE0-\xEF][\x80-\xBF]{2}',
  107. '[\xF0-\xF7][\x80-\xBF]{3}'
  108. ].join('|'), 'g');
  109. var cb_btou = function(cccc) {
  110. switch(cccc.length) {
  111. case 4:
  112. var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
  113. | ((0x3f & cccc.charCodeAt(1)) << 12)
  114. | ((0x3f & cccc.charCodeAt(2)) << 6)
  115. | (0x3f & cccc.charCodeAt(3)),
  116. offset = cp - 0x10000;
  117. return (fromCharCode((offset >>> 10) + 0xD800)
  118. + fromCharCode((offset & 0x3FF) + 0xDC00));
  119. case 3:
  120. return fromCharCode(
  121. ((0x0f & cccc.charCodeAt(0)) << 12)
  122. | ((0x3f & cccc.charCodeAt(1)) << 6)
  123. | (0x3f & cccc.charCodeAt(2))
  124. );
  125. default:
  126. return fromCharCode(
  127. ((0x1f & cccc.charCodeAt(0)) << 6)
  128. | (0x3f & cccc.charCodeAt(1))
  129. );
  130. }
  131. };
  132. var btou = function(b) {
  133. return b.replace(re_btou, cb_btou);
  134. };
  135. var cb_decode = function(cccc) {
  136. var len = cccc.length,
  137. padlen = len % 4,
  138. n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0)
  139. | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0)
  140. | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0)
  141. | (len > 3 ? b64tab[cccc.charAt(3)] : 0),
  142. chars = [
  143. fromCharCode( n >>> 16),
  144. fromCharCode((n >>> 8) & 0xff),
  145. fromCharCode( n & 0xff)
  146. ];
  147. chars.length -= [0, 0, 2, 1][padlen];
  148. return chars.join('');
  149. };
  150. var atob = global.atob ? function(a) {
  151. return global.atob(a);
  152. } : function(a){
  153. return a.replace(/[\s\S]{1,4}/g, cb_decode);
  154. };
  155. var _decode = buffer ?
  156. buffer.from && Uint8Array && buffer.from !== Uint8Array.from
  157. ? function(a) {
  158. return (a.constructor === buffer.constructor
  159. ? a : buffer.from(a, 'base64')).toString();
  160. }
  161. : function(a) {
  162. return (a.constructor === buffer.constructor
  163. ? a : new buffer(a, 'base64')).toString();
  164. }
  165. : function(a) { return btou(atob(a)) };
  166. var decode = function(a){
  167. return _decode(
  168. String(a).replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' })
  169. .replace(/[^A-Za-z0-9\+\/]/g, '')
  170. );
  171. };
  172. var noConflict = function() {
  173. var Base64 = global.Base64;
  174. global.Base64 = _Base64;
  175. return Base64;
  176. };
  177. // export Base64
  178. global.Base64 = {
  179. VERSION: version,
  180. atob: atob,
  181. btoa: btoa,
  182. fromBase64: decode,
  183. toBase64: encode,
  184. utob: utob,
  185. encode: encode,
  186. encodeURI: encodeURI,
  187. btou: btou,
  188. decode: decode,
  189. noConflict: noConflict
  190. };
  191. // if ES5 is available, make Base64.extendString() available
  192. if (typeof Object.defineProperty === 'function') {
  193. var noEnum = function(v){
  194. return {value:v,enumerable:false,writable:true,configurable:true};
  195. };
  196. global.Base64.extendString = function () {
  197. Object.defineProperty(
  198. String.prototype, 'fromBase64', noEnum(function () {
  199. return decode(this)
  200. }));
  201. Object.defineProperty(
  202. String.prototype, 'toBase64', noEnum(function (urisafe) {
  203. return encode(this, urisafe)
  204. }));
  205. Object.defineProperty(
  206. String.prototype, 'toBase64URI', noEnum(function () {
  207. return encode(this, true)
  208. }));
  209. };
  210. }
  211. //
  212. // export Base64 to the namespace
  213. //
  214. if (global['Meteor']) { // Meteor.js
  215. Base64 = global.Base64;
  216. }
  217. // module.exports and AMD are mutually exclusive.
  218. // module.exports has precedence.
  219. if (typeof module !== 'undefined' && module.exports) {
  220. module.exports.Base64 = global.Base64;
  221. }
  222. else if (typeof define === 'function' && define.amd) {
  223. // AMD. Register as an anonymous module.
  224. define([], function(){ return global.Base64 });
  225. }
  226. // that's it!
  227. return {Base64: global.Base64}
  228. }));