atob.js 5.1 KB

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