Base64.js 5.0 KB

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