base64.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. var _PADCHAR = "=",
  2. _ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
  3. _VERSION = "1.1";//Mr. Ruan fix to 1.1 to support asian char(utf8)
  4. function _getbyte64(s, i) {
  5. // This is oddly fast, except on Chrome/V8.
  6. // Minimal or no improvement in performance by using a
  7. // object with properties mapping chars to value (eg. 'A': 0)
  8. var idx = _ALPHA.indexOf(s.charAt(i));
  9. if (idx === -1) {
  10. throw "Cannot decode base64";
  11. }
  12. return idx;
  13. }
  14. function _decode_chars(y, x) {
  15. while (y.length > 0) {
  16. var ch = y[0];
  17. if (ch < 0x80) {
  18. y.shift();
  19. x.push(String.fromCharCode(ch));
  20. } else if ((ch & 0x80) == 0xc0) {
  21. if (y.length < 2) break;
  22. ch = y.shift();
  23. var ch1 = y.shift();
  24. x.push(String.fromCharCode(((ch & 0x1f) << 6) + (ch1 & 0x3f)));
  25. } else {
  26. if (y.length < 3) break;
  27. ch = y.shift();
  28. var ch1 = y.shift();
  29. var ch2 = y.shift();
  30. x.push(String.fromCharCode(((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f)));
  31. }
  32. }
  33. }
  34. function _decode(s) {
  35. var pads = 0,
  36. i,
  37. b10,
  38. imax = s.length,
  39. x = [],
  40. y = [];
  41. s = String(s);
  42. if (imax === 0) {
  43. return s;
  44. }
  45. if (imax % 4 !== 0) {
  46. throw "Cannot decode base64";
  47. }
  48. if (s.charAt(imax - 1) === _PADCHAR) {
  49. pads = 1;
  50. if (s.charAt(imax - 2) === _PADCHAR) {
  51. pads = 2;
  52. }
  53. // either way, we want to ignore this last block
  54. imax -= 4;
  55. }
  56. for (i = 0; i < imax; i += 4) {
  57. var ch1 = _getbyte64(s, i);
  58. var ch2 = _getbyte64(s, i + 1);
  59. var ch3 = _getbyte64(s, i + 2);
  60. var ch4 = _getbyte64(s, i + 3);
  61. b10 = (_getbyte64(s, i) << 18) | (_getbyte64(s, i + 1) << 12) | (_getbyte64(s, i + 2) << 6) | _getbyte64(s, i + 3);
  62. y.push(b10 >> 16);
  63. y.push((b10 >> 8) & 0xff);
  64. y.push(b10 & 0xff);
  65. _decode_chars(y, x);
  66. }
  67. switch (pads) {
  68. case 1:
  69. b10 = (_getbyte64(s, i) << 18) | (_getbyte64(s, i + 1) << 12) | (_getbyte64(s, i + 2) << 6);
  70. y.push(b10 >> 16);
  71. y.push((b10 >> 8) & 0xff);
  72. break;
  73. case 2:
  74. b10 = (_getbyte64(s, i) << 18) | (_getbyte64(s, i + 1) << 12);
  75. y.push(b10 >> 16);
  76. break;
  77. }
  78. _decode_chars(y, x);
  79. if (y.length > 0) throw "Cannot decode base64";
  80. return x.join("");
  81. }
  82. function _get_chars(ch, y) {
  83. if (ch < 0x80) y.push(ch);
  84. else if (ch < 0x800) {
  85. y.push(0xc0 + ((ch >> 6) & 0x1f));
  86. y.push(0x80 + (ch & 0x3f));
  87. } else {
  88. y.push(0xe0 + ((ch >> 12) & 0xf));
  89. y.push(0x80 + ((ch >> 6) & 0x3f));
  90. y.push(0x80 + (ch & 0x3f));
  91. }
  92. }
  93. function _encode(s) {
  94. if (arguments.length !== 1) {
  95. throw "SyntaxError: exactly one argument required";
  96. }
  97. s = String(s);
  98. if (s.length === 0) {
  99. return s;
  100. }
  101. //s = _encode_utf8(s);
  102. var i,
  103. b10,
  104. y = [],
  105. x = [],
  106. len = s.length;
  107. i = 0;
  108. while (i < len) {
  109. _get_chars(s.charCodeAt(i), y);
  110. while (y.length >= 3) {
  111. var ch1 = y.shift();
  112. var ch2 = y.shift();
  113. var ch3 = y.shift();
  114. b10 = (ch1 << 16) | (ch2 << 8) | ch3;
  115. x.push(_ALPHA.charAt(b10 >> 18));
  116. x.push(_ALPHA.charAt((b10 >> 12) & 0x3F));
  117. x.push(_ALPHA.charAt((b10 >> 6) & 0x3f));
  118. x.push(_ALPHA.charAt(b10 & 0x3f));
  119. }
  120. i++;
  121. }
  122. switch (y.length) {
  123. case 1:
  124. var ch = y.shift();
  125. b10 = ch << 16;
  126. x.push(_ALPHA.charAt(b10 >> 18) + _ALPHA.charAt((b10 >> 12) & 0x3F) + _PADCHAR + _PADCHAR);
  127. break;
  128. case 2:
  129. var ch1 = y.shift();
  130. var ch2 = y.shift();
  131. b10 = (ch1 << 16) | (ch2 << 8);
  132. x.push(_ALPHA.charAt(b10 >> 18) + _ALPHA.charAt((b10 >> 12) & 0x3F) + _ALPHA.charAt((b10 >> 6) & 0x3f) + _PADCHAR);
  133. break;
  134. }
  135. return x.join("");
  136. }
  137. // return {
  138. // decode: _decode,
  139. // encode: _encode,
  140. // VERSION: _VERSION
  141. // };
  142. function transformCode(params) {
  143. return encodeURIComponent(_encode(params))
  144. }
  145. export default transformCode