1
0

base64.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * $Id: base64.js,v 2.6 2012/08/24 05:23:18 dankogai Exp dankogai $
  3. *
  4. * Licensed under the MIT license.
  5. * http://www.opensource.org/licenses/mit-license.php
  6. *
  7. * References:
  8. * http://en.wikipedia.org/wiki/Base64
  9. */
  10. (function(global) {
  11. 'use strict';
  12. // if node.js, we use Buffer
  13. var buffer;
  14. if (typeof module !== 'undefined' && module.exports) {
  15. buffer = require('buffer').Buffer;
  16. }
  17. // constants
  18. var b64chars
  19. = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  20. var b64tab = function(bin) {
  21. var t = {};
  22. for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;
  23. return t;
  24. }(b64chars);
  25. var fromCharCode = String.fromCharCode;
  26. // encoder stuff
  27. var cb_utob = function(c) {
  28. var cc = c.charCodeAt(0);
  29. return cc < 0x80 ? c
  30. : cc < 0x800 ? fromCharCode(0xc0 | (cc >>> 6))
  31. + fromCharCode(0x80 | (cc & 0x3f))
  32. : fromCharCode(0xe0 | ((cc >>> 12) & 0x0f))
  33. + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
  34. + fromCharCode(0x80 | ( cc & 0x3f));
  35. };
  36. var utob = function(u) {
  37. return u.replace(/[^\x00-\x7F]/g, cb_utob);
  38. };
  39. var cb_encode = function(ccc) {
  40. var padlen = [0, 2, 1][ccc.length % 3],
  41. ord = ccc.charCodeAt(0) << 16
  42. | ((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8)
  43. | ((ccc.length > 2 ? ccc.charCodeAt(2) : 0)),
  44. chars = [
  45. b64chars.charAt( ord >>> 18),
  46. b64chars.charAt((ord >>> 12) & 63),
  47. padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63),
  48. padlen >= 1 ? '=' : b64chars.charAt(ord & 63)
  49. ];
  50. return chars.join('');
  51. };
  52. var btoa = global.btoa || function(b) {
  53. return b.replace(/[\s\S]{1,3}/g, cb_encode);
  54. };
  55. var _encode = buffer
  56. ? function (u) { return (new buffer(u)).toString('base64') }
  57. : function (u) { return btoa(utob(u)) }
  58. ;
  59. var encode = function(u, urisafe) {
  60. return !urisafe
  61. ? _encode(u)
  62. : _encode(u).replace(/[+\/]/g, function(m0) {
  63. return m0 == '+' ? '-' : '_';
  64. });
  65. };
  66. var encodeURI = function(u) { return encode(u, true) };
  67. // decoder stuff
  68. var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}/g;
  69. var cb_btou = function(ccc) {
  70. return fromCharCode(
  71. ccc.length < 3 ? ((0x1f & ccc.charCodeAt(0)) << 6)
  72. | (0x3f & ccc.charCodeAt(1))
  73. : ((0x0f & ccc.charCodeAt(0)) << 12)
  74. | ((0x3f & ccc.charCodeAt(1)) << 6)
  75. | (0x3f & ccc.charCodeAt(2))
  76. );
  77. };
  78. var btou = function(b) {
  79. return b.replace(re_btou, cb_btou);
  80. };
  81. var cb_decode = function(cccc) {
  82. var len = cccc.length,
  83. padlen = len % 4,
  84. n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0)
  85. | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0)
  86. | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0)
  87. | (len > 3 ? b64tab[cccc.charAt(3)] : 0),
  88. chars = [
  89. fromCharCode( n >>> 16),
  90. fromCharCode((n >>> 8) & 0xff),
  91. fromCharCode( n & 0xff)
  92. ];
  93. chars.length -= [0, 0, 2, 1][padlen];
  94. return chars.join('');
  95. };
  96. var atob = global.atob || function(a){
  97. return a.replace(/[\s\S]{1,4}/g, cb_decode);
  98. };
  99. var _decode = buffer
  100. ? function(a) { return (new buffer(a, 'base64')).toString() }
  101. : function(a) { return btou(atob(a)) }
  102. ;
  103. var decode = function(a){
  104. return _decode(
  105. a.replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' })
  106. .replace(/[^A-Za-z0-9\+\/]/g, '')
  107. );
  108. };
  109. // export Base64
  110. global.Base64 = {
  111. atob: atob,
  112. btoa: btoa,
  113. fromBase64: decode,
  114. toBase64: encode,
  115. utob: utob,
  116. encode: encode,
  117. encodeURI: encodeURI,
  118. btou: btou,
  119. decode: decode
  120. };
  121. // if ES5 is available, make Base64.extendString() available
  122. if (typeof Object.defineProperty === 'function') {
  123. var noEnum = function(v){
  124. return {value:v,enumerable:false,writable:true,configurable:true};
  125. };
  126. global.Base64.extendString = function () {
  127. Object.defineProperty(
  128. String.prototype, 'fromBase64', noEnum(function () {
  129. return decode(this)
  130. }));
  131. Object.defineProperty(
  132. String.prototype, 'toBase64', noEnum(function (urisafe) {
  133. return encode(this, urisafe)
  134. }));
  135. };
  136. }
  137. // that's it!
  138. })(this);