base64.js 5.6 KB

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