base64.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * UTF16和UTF8转换对照表
  3. * U+00000000 – U+0000007F 0xxxxxxx
  4. * U+00000080 – U+000007FF 110xxxxx 10xxxxxx
  5. * U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx
  6. * U+00010000 – U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  7. * U+00200000 – U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  8. * U+04000000 – U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  9. */
  10. var Base64 = {
  11. // 转码表
  12. table : [
  13. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  14. 'I', 'J', 'K', 'L', 'M', 'N', 'O' ,'P',
  15. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  16. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  17. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  18. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  19. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  20. '4', '5', '6', '7', '8', '9', '+', '/'
  21. ],
  22. UTF16ToUTF8 : function(str) {
  23. var res = [], len = str.length;
  24. for (var i = 0; i < len; i++) {
  25. var code = str.charCodeAt(i);
  26. if (code > 0x0000 && code <= 0x007F) {
  27. // 单字节,这里并不考虑0x0000,因为它是空字节
  28. // U+00000000 – U+0000007F 0xxxxxxx
  29. res.push(str.charAt(i));
  30. } else if (code >= 0x0080 && code <= 0x07FF) {
  31. // 双字节
  32. // U+00000080 – U+000007FF 110xxxxx 10xxxxxx
  33. // 110xxxxx
  34. var byte1 = 0xC0 | ((code >> 6) & 0x1F);
  35. // 10xxxxxx
  36. var byte2 = 0x80 | (code & 0x3F);
  37. res.push(
  38. String.fromCharCode(byte1),
  39. String.fromCharCode(byte2)
  40. );
  41. } else if (code >= 0x0800 && code <= 0xFFFF) {
  42. // 三字节
  43. // U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx
  44. // 1110xxxx
  45. var byte1 = 0xE0 | ((code >> 12) & 0x0F);
  46. // 10xxxxxx
  47. var byte2 = 0x80 | ((code >> 6) & 0x3F);
  48. // 10xxxxxx
  49. var byte3 = 0x80 | (code & 0x3F);
  50. res.push(
  51. String.fromCharCode(byte1),
  52. String.fromCharCode(byte2),
  53. String.fromCharCode(byte3)
  54. );
  55. } else if (code >= 0x00010000 && code <= 0x001FFFFF) {
  56. // 四字节
  57. // U+00010000 – U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  58. } else if (code >= 0x00200000 && code <= 0x03FFFFFF) {
  59. // 五字节
  60. // U+00200000 – U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  61. } else /** if (code >= 0x04000000 && code <= 0x7FFFFFFF)*/ {
  62. // 六字节
  63. // U+04000000 – U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  64. }
  65. }
  66. return res.join('');
  67. },
  68. UTF8ToUTF16 : function(str) {
  69. var res = [], len = str.length;
  70. var i = 0;
  71. for (var i = 0; i < len; i++) {
  72. var code = str.charCodeAt(i);
  73. // 对第一个字节进行判断
  74. if (((code >> 7) & 0xFF) == 0x0) {
  75. // 单字节
  76. // 0xxxxxxx
  77. res.push(str.charAt(i));
  78. } else if (((code >> 5) & 0xFF) == 0x6) {
  79. // 双字节
  80. // 110xxxxx 10xxxxxx
  81. var code2 = str.charCodeAt(++i);
  82. var byte1 = (code & 0x1F) << 6;
  83. var byte2 = code2 & 0x3F;
  84. var utf16 = byte1 | byte2;
  85. res.push(Sting.fromCharCode(utf16));
  86. } else if (((code >> 4) & 0xFF) == 0xE) {
  87. // 三字节
  88. // 1110xxxx 10xxxxxx 10xxxxxx
  89. var code2 = str.charCodeAt(++i);
  90. var code3 = str.charCodeAt(++i);
  91. var byte1 = (code << 4) | ((code2 >> 2) & 0x0F);
  92. var byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F);
  93. var utf16 = ((byte1 & 0x00FF) << 8) | byte2
  94. res.push(String.fromCharCode(utf16));
  95. } else if (((code >> 3) & 0xFF) == 0x1E) {
  96. // 四字节
  97. // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  98. } else if (((code >> 2) & 0xFF) == 0x3E) {
  99. // 五字节
  100. // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  101. } else /** if (((code >> 1) & 0xFF) == 0x7E)*/ {
  102. // 六字节
  103. // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  104. }
  105. }
  106. return res.join('');
  107. },
  108. encode : function(str) {
  109. if (!str) {
  110. return '';
  111. }
  112. var utf8 = this.UTF16ToUTF8(str); // 转成UTF8
  113. var i = 0; // 遍历索引
  114. var len = utf8.length;
  115. var res = [];
  116. while (i < len) {
  117. var c1 = utf8.charCodeAt(i++) & 0xFF;
  118. res.push(this.table[c1 >> 2]);
  119. // 需要补2个=
  120. if (i == len) {
  121. res.push(this.table[(c1 & 0x3) << 4]);
  122. res.push('==');
  123. break;
  124. }
  125. var c2 = utf8.charCodeAt(i++);
  126. // 需要补1个=
  127. if (i == len) {
  128. res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
  129. res.push(this.table[(c2 & 0x0F) << 2]);
  130. res.push('=');
  131. break;
  132. }
  133. var c3 = utf8.charCodeAt(i++);
  134. res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
  135. res.push(this.table[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]);
  136. res.push(this.table[c3 & 0x3F]);
  137. }
  138. return res.join('');
  139. },
  140. decode : function(str) {
  141. if (!str) {
  142. return '';
  143. }
  144. var len = str.length;
  145. var i = 0;
  146. var res = [];
  147. while (i < len) {
  148. code1 = this.table.indexOf(str.charAt(i++));
  149. code2 = this.table.indexOf(str.charAt(i++));
  150. code3 = this.table.indexOf(str.charAt(i++));
  151. code4 = this.table.indexOf(str.charAt(i++));
  152. c1 = (code1 << 2) | (code2 >> 4);
  153. c2 = ((code2 & 0xF) << 4) | (code3 >> 2);
  154. c3 = ((code3 & 0x3) << 6) | code4;
  155. res.push(String.fromCharCode(c1));
  156. if (code3 != 64) {
  157. res.push(String.fromCharCode(c2));
  158. }
  159. if (code4 != 64) {
  160. res.push(String.fromCharCode(c3));
  161. }
  162. }
  163. return this.UTF8ToUTF16(res.join(''));
  164. }
  165. };
  166. /*
  167. console.group('Test Base64: ');
  168. var b64 = Base64.encode('Hello, oschina!又是一年春来到~');
  169. console.log(b64);
  170. console.log(Base64.decode(b64));
  171. console.groupEnd();
  172. */