base64.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  2. var base64DecodeChars = new Array(
  3. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  4. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  5. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  6. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  7. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  8. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  9. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  10. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
  11. function encode(str) {
  12. var out, i, len;
  13. var c1, c2, c3;
  14. len = str.length;
  15. i = 0;
  16. out = "";
  17. while (i < len) {
  18. c1 = str.charCodeAt(i++) & 0xff;
  19. if (i == len) {
  20. out += base64EncodeChars.charAt(c1 >> 2);
  21. out += base64EncodeChars.charAt((c1 & 0x3) << 4);
  22. out += "==";
  23. break;
  24. }
  25. c2 = str.charCodeAt(i++);
  26. if (i == len) {
  27. out += base64EncodeChars.charAt(c1 >> 2);
  28. out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  29. out += base64EncodeChars.charAt((c2 & 0xF) << 2);
  30. out += "=";
  31. break;
  32. }
  33. c3 = str.charCodeAt(i++);
  34. out += base64EncodeChars.charAt(c1 >> 2);
  35. out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  36. out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
  37. out += base64EncodeChars.charAt(c3 & 0x3F);
  38. }
  39. return out;
  40. }
  41. function decode(str) {
  42. var c1, c2, c3, c4;
  43. var i, len, out;
  44. len = str.length;
  45. i = 0;
  46. out = "";
  47. while (i < len) {
  48. /* c1 */
  49. do {
  50. c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
  51. } while (i < len && c1 == -1);
  52. if (c1 == -1)
  53. break;
  54. /* c2 */
  55. do {
  56. c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
  57. } while (i < len && c2 == -1);
  58. if (c2 == -1)
  59. break;
  60. out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
  61. /* c3 */
  62. do {
  63. c3 = str.charCodeAt(i++) & 0xff;
  64. if (c3 == 61)
  65. return out;
  66. c3 = base64DecodeChars[c3];
  67. } while (i < len && c3 == -1);
  68. if (c3 == -1)
  69. break;
  70. out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
  71. /* c4 */
  72. do {
  73. c4 = str.charCodeAt(i++) & 0xff;
  74. if (c4 == 61)
  75. return out;
  76. c4 = base64DecodeChars[c4];
  77. } while (i < len && c4 == -1);
  78. if (c4 == -1)
  79. break;
  80. out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
  81. }
  82. return out;
  83. }
  84. function utf16to8(str) {
  85. var out, i, len, c;
  86. out = "";
  87. len = str.length;
  88. for (i = 0; i < len; i++) {
  89. c = str.charCodeAt(i);
  90. if ((c >= 0x0001) && (c <= 0x007F)) {
  91. out += str.charAt(i);
  92. } else if (c > 0x07FF) {
  93. out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
  94. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
  95. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  96. } else {
  97. out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
  98. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  99. }
  100. }
  101. return out;
  102. }
  103. function utf8to16(str) {
  104. var out, i, len, c;
  105. var char2, char3;
  106. out = "";
  107. len = str.length;
  108. i = 0;
  109. while (i < len) {
  110. c = str.charCodeAt(i++);
  111. switch (c >> 4) {
  112. case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  113. // 0xxxxxxx
  114. out += str.charAt(i - 1);
  115. break;
  116. case 12: case 13:
  117. // 110x xxxx 10xx xxxx
  118. char2 = str.charCodeAt(i++);
  119. out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
  120. break;
  121. case 14:
  122. // 1110 xxxx 10xx xxxx 10xx xxxx
  123. char2 = str.charCodeAt(i++);
  124. char3 = str.charCodeAt(i++);
  125. out += String.fromCharCode(((c & 0x0F) << 12) |
  126. ((char2 & 0x3F) << 6) |
  127. ((char3 & 0x3F) << 0));
  128. break;
  129. }
  130. }
  131. return out;
  132. }
  133. module.exports = {
  134. encode: encode,
  135. decode: decode,
  136. utf16to8: utf16to8,
  137. utf8to16: utf8to16
  138. }