base64.js 4.0 KB

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