base64.js 6.6 KB

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