base64.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright (c) 2008 Fred Palmer fred.palmer_at_gmail.com
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. function StringBuffer()
  23. {
  24. this.buffer = [];
  25. }
  26. StringBuffer.prototype.append = function append(string)
  27. {
  28. this.buffer.push(string);
  29. return this;
  30. };
  31. StringBuffer.prototype.toString = function toString()
  32. {
  33. return this.buffer.join("");
  34. };
  35. window.Base64 =
  36. {
  37. codex : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  38. encode : function (input)
  39. {
  40. var output = new StringBuffer();
  41. var enumerator = new Utf8EncodeEnumerator(input);
  42. while (enumerator.moveNext())
  43. {
  44. var chr1 = enumerator.current;
  45. enumerator.moveNext();
  46. var chr2 = enumerator.current;
  47. enumerator.moveNext();
  48. var chr3 = enumerator.current;
  49. var enc1 = chr1 >> 2;
  50. var enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  51. var enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  52. var enc4 = chr3 & 63;
  53. if (isNaN(chr2))
  54. {
  55. enc3 = enc4 = 64;
  56. }
  57. else if (isNaN(chr3))
  58. {
  59. enc4 = 64;
  60. }
  61. output.append(this.codex.charAt(enc1) + this.codex.charAt(enc2) + this.codex.charAt(enc3) + this.codex.charAt(enc4));
  62. }
  63. return output.toString();
  64. },
  65. decode : function (input)
  66. {
  67. var output = new StringBuffer();
  68. var outputBytes = [];
  69. var enumerator = new Base64DecodeEnumerator(input);
  70. while (enumerator.moveNext())
  71. {
  72. var charCode = enumerator.current;
  73. outputBytes.push(charCode);
  74. if (charCode < 128)
  75. output.append(String.fromCharCode(charCode));
  76. else if ((charCode > 191) && (charCode < 224))
  77. {
  78. enumerator.moveNext();
  79. var charCode2 = enumerator.current;
  80. outputBytes.push(charCode2);
  81. output.append(String.fromCharCode(((charCode & 31) << 6) | (charCode2 & 63)));
  82. }
  83. else
  84. {
  85. enumerator.moveNext();
  86. var charCode2 = enumerator.current;
  87. outputBytes.push(charCode2);
  88. enumerator.moveNext();
  89. var charCode3 = enumerator.current;
  90. outputBytes.push(charCode3);
  91. output.append(String.fromCharCode(((charCode & 15) << 12) | ((charCode2 & 63) << 6) | (charCode3 & 63)));
  92. }
  93. }
  94. return {
  95. "bytes": outputBytes,
  96. "text": output.toString()
  97. };
  98. }
  99. };
  100. function Utf8EncodeEnumerator(input)
  101. {
  102. this._input = input;
  103. this._index = -1;
  104. this._buffer = [];
  105. }
  106. Utf8EncodeEnumerator.prototype =
  107. {
  108. current: Number.NaN,
  109. moveNext: function()
  110. {
  111. if (this._buffer.length > 0)
  112. {
  113. this.current = this._buffer.shift();
  114. return true;
  115. }
  116. else if (this._index >= (this._input.length - 1))
  117. {
  118. this.current = Number.NaN;
  119. return false;
  120. }
  121. else
  122. {
  123. var charCode = this._input.charCodeAt(++this._index);
  124. // "\r\n" -> "\n"
  125. //
  126. if ((charCode == 13) && (this._input.charCodeAt(this._index + 1) == 10))
  127. {
  128. charCode = 10;
  129. this._index += 2;
  130. }
  131. if (charCode < 128)
  132. {
  133. this.current = charCode;
  134. }
  135. else if ((charCode > 127) && (charCode < 2048))
  136. {
  137. this.current = (charCode >> 6) | 192;
  138. this._buffer.push((charCode & 63) | 128);
  139. }
  140. else
  141. {
  142. this.current = (charCode >> 12) | 224;
  143. this._buffer.push(((charCode >> 6) & 63) | 128);
  144. this._buffer.push((charCode & 63) | 128);
  145. }
  146. return true;
  147. }
  148. }
  149. }
  150. function Base64DecodeEnumerator(input)
  151. {
  152. this._input = input;
  153. this._index = -1;
  154. this._buffer = [];
  155. }
  156. Base64DecodeEnumerator.prototype =
  157. {
  158. current: 64,
  159. moveNext: function()
  160. {
  161. if (this._buffer.length > 0)
  162. {
  163. this.current = this._buffer.shift();
  164. return true;
  165. }
  166. else if (this._index >= (this._input.length - 1))
  167. {
  168. this.current = 64;
  169. return false;
  170. }
  171. else
  172. {
  173. var enc1 = Base64.codex.indexOf(this._input.charAt(++this._index));
  174. var enc2 = Base64.codex.indexOf(this._input.charAt(++this._index));
  175. var enc3 = Base64.codex.indexOf(this._input.charAt(++this._index));
  176. var enc4 = Base64.codex.indexOf(this._input.charAt(++this._index));
  177. var chr1 = (enc1 << 2) | (enc2 >> 4);
  178. var chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  179. var chr3 = ((enc3 & 3) << 6) | enc4;
  180. this.current = chr1;
  181. if (enc3 != 64)
  182. this._buffer.push(chr2);
  183. if (enc4 != 64)
  184. this._buffer.push(chr3);
  185. return true;
  186. }
  187. }
  188. };