home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / firefox / base64.js < prev    next >
Text File  |  2006-08-07  |  5KB  |  159 lines

  1. function G_Base64() {
  2. this.byteToCharMap_ = {};
  3. this.charToByteMap_ = {};
  4. this.byteToCharMapWebSafe_ = {};
  5. this.charToByteMapWebSafe_ = {};
  6. this.init_();
  7. }
  8. G_Base64.ENCODED_VALS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
  9. "abcdefghijklmnopqrstuvwxyz" +
  10. "0123456789+/=";
  11. G_Base64.ENCODED_VALS_WEBSAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
  12. "abcdefghijklmnopqrstuvwxyz" +
  13. "0123456789-_=";
  14. G_Base64.prototype.init_ = function() {
  15. for (var i = 0; i < G_Base64.ENCODED_VALS.length; i++) {
  16. this.byteToCharMap_[i] = G_Base64.ENCODED_VALS.charAt(i);
  17. this.charToByteMap_[this.byteToCharMap_[i]] = i;
  18. this.byteToCharMapWebSafe_[i] = G_Base64.ENCODED_VALS_WEBSAFE.charAt(i);
  19. this.charToByteMapWebSafe_[this.byteToCharMapWebSafe_[i]] = i;
  20. }
  21. }
  22. G_Base64.prototype.encodeByteArray = function(input, opt_webSafe) {
  23. if (!(input instanceof Array))
  24. throw new Error("encodeByteArray takes an array as a parameter");
  25. var byteToCharMap = opt_webSafe ?
  26. this.byteToCharMapWebSafe_ :
  27. this.byteToCharMap_;
  28. var output = [];
  29. var i = 0;
  30. while (i < input.length) {
  31. var byte1 = input[i];
  32. var haveByte2 = i + 1 < input.length;
  33. var byte2 = haveByte2 ? input[i + 1] : 0;
  34. var haveByte3 = i + 2 < input.length;
  35. var byte3 = haveByte3 ? input[i + 2] : 0;
  36. var outByte1 = byte1 >> 2;
  37. var outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4);
  38. var outByte3 = ((byte2 & 0x0F) << 2) | (byte3 >> 6);
  39. var outByte4 = byte3 & 0x3F;
  40. if (!haveByte3) {
  41. outByte4 = 64;
  42. if (!haveByte2)
  43. outByte3 = 64;
  44. }
  45. output.push(byteToCharMap[outByte1]);
  46. output.push(byteToCharMap[outByte2]);
  47. output.push(byteToCharMap[outByte3]);
  48. output.push(byteToCharMap[outByte4]);
  49. i += 3;
  50. }
  51. return output.join("");
  52. }
  53. G_Base64.prototype.decodeString = function(input, opt_webSafe) {
  54. if (input.length % 4)
  55. throw new Error("Length of b64-encoded data must be zero mod four");
  56. var charToByteMap = opt_webSafe ?
  57. this.charToByteMapWebSafe_ :
  58. this.charToByteMap_;
  59. var output = [];
  60. var i = 0;
  61. while (i < input.length) {
  62. var byte1 = charToByteMap[input.charAt(i)];
  63. var byte2 = charToByteMap[input.charAt(i + 1)];
  64. var byte3 = charToByteMap[input.charAt(i + 2)];
  65. var byte4 = charToByteMap[input.charAt(i + 3)];
  66. if (byte1 === undefined || byte2 === undefined ||
  67. byte3 === undefined || byte4 === undefined)
  68. throw new Error("String contains characters not in our alphabet: " +
  69. input);
  70. var outByte1 = (byte1 << 2) | (byte2 >> 4);
  71. output.push(outByte1);
  72. if (byte3 != 64) {
  73. var outByte2 = ((byte2 << 4) & 0xF0) | (byte3 >> 2);
  74. output.push(outByte2);
  75. if (byte4 != 64) {
  76. var outByte3 = ((byte3 << 6) & 0xC0) | byte4;
  77. output.push(outByte3);
  78. }
  79. }
  80. i += 4;
  81. }
  82. return output;
  83. }
  84. G_Base64.prototype.arrayifyString = function(str) {
  85. var output = [];
  86. for (var i = 0; i < str.length; i++) {
  87. var c = str.charCodeAt(i);
  88. while (c > 0xff) {
  89. output.push(c & 0xff);
  90. c >>= 8;
  91. }
  92. output.push(c);
  93. }
  94. return output;
  95. }
  96. G_Base64.prototype.stringifyArray = function(array) {
  97. var output = [];
  98. for (var i = 0; i < array.length; i++)
  99. output[i] = String.fromCharCode(array[i]);
  100. return output.join("");
  101. }
  102. function TEST_G_Base64() {
  103. if (G_GDEBUG) {
  104. var z = "base64 UNITTEST";
  105. G_debugService.enableZone(z);
  106. G_Debug(z, "Starting");
  107. var b = new G_Base64();
  108. var tests =
  109. [ "", "",
  110. "f", "Zg==",
  111. "fo", "Zm8=",
  112. "foo", "Zm9v",
  113. "foob", "Zm9vYg==",
  114. "fooba", "Zm9vYmE=",
  115. "foobar", "Zm9vYmFy",
  116. "\xe4\xb8\x80\xe4\xba\x8c\xe4\xb8\x89\xe5\x9b\x9b\xe4\xba\x94\xe5" +
  117. "\x85\xad\xe4\xb8\x83\xe5\x85\xab\xe4\xb9\x9d\xe5\x8d\x81",
  118. "5LiA5LqM5LiJ5Zub5LqU5YWt5LiD5YWr5Lmd5Y2B"];
  119. for (var i = 0; i < tests.length; i += 2) {
  120. var enc = b.encodeByteArray(b.arrayifyString(tests[i]));
  121. G_Assert(z, enc === tests[i + 1],
  122. "Error encoding: " + tests[i] + " (got " + enc +
  123. " but wanted " + tests[i + 1] + ")");
  124. var dec = b.stringifyArray(b.decodeString(enc));
  125. G_Assert(z, dec === tests[i],
  126. "Error deocding " + enc + " (got " + dec +
  127. " but wanted " + tests[i] + ")");
  128. }
  129. var numIterations = 100;
  130. for (var i = 0; i < numIterations; i++) {
  131. var input = [];
  132. for (var j = 0; j < i; j++)
  133. input[j] = j % 256;
  134. var encoded = b.encodeByteArray(input);
  135. var decoded = b.decodeString(encoded);
  136. G_Assert(z, !(encoded.length % 4), "Encoded length not a multiple of 4?");
  137. G_Assert(z, input.length == decoded.length,
  138. "Decoded length not equal to input length?");
  139. for (var j = 0; j < i; j++)
  140. G_Assert(z, input[j] === decoded[j], "Values differ at position " + j);
  141. }
  142. var test = ">>>???>>>???";
  143. var enc = b.encodeByteArray(b.arrayifyString(test));
  144. G_Assert(z, enc == "Pj4+Pz8/Pj4+Pz8/", "Non-websafe broken?");
  145. enc = b.encodeByteArray(b.arrayifyString(test), true /* websafe */);
  146. G_Assert(z, enc == "Pj4-Pz8_Pj4-Pz8_", "Websafe encoding broken");
  147. var dec = b.stringifyArray(b.decodeString(enc, true /* websafe */));
  148. G_Assert(z, dec === test, "Websafe dencoding broken");
  149. var caught = false;
  150. try {
  151. b.decodeString("foooooo+oooo", true /*websafe*/);
  152. } catch(e) {
  153. caught = true;
  154. }
  155. G_Assert(z, caught, "Didn't throw on malformed input");
  156. G_Debug(z, "PASSED");
  157. }
  158. }
  159.