ie_base64.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2010 Nick Galbreath
  3. * http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript
  4. *
  5. * Permission is hereby granted, free of charge, to any person
  6. * obtaining a copy of this software and associated documentation
  7. * files (the "Software"), to deal in the Software without
  8. * restriction, including without limitation the rights to use,
  9. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following
  12. * conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. /* base64 encode/decode compatible with window.btoa/atob
  27. *
  28. * window.atob/btoa is a Firefox extension to convert binary data (the "b")
  29. * to base64 (ascii, the "a").
  30. *
  31. * It is also found in Safari and Chrome. It is not available in IE.
  32. *
  33. * if (!window.btoa) window.btoa = base64.encode
  34. * if (!window.atob) window.atob = base64.decode
  35. *
  36. * The original spec's for atob/btoa are a bit lacking
  37. * https://developer.mozilla.org/en/DOM/window.atob
  38. * https://developer.mozilla.org/en/DOM/window.btoa
  39. *
  40. * window.btoa and base64.encode takes a string where charCodeAt is [0,255]
  41. * If any character is not [0,255], then an DOMException(5) is thrown.
  42. *
  43. * window.atob and base64.decode take a base64-encoded string
  44. * If the input length is not a multiple of 4, or contains invalid characters
  45. * then an DOMException(5) is thrown.
  46. */
  47. var base64 = {};
  48. base64.PADCHAR = '=';
  49. base64.ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  50. base64.makeDOMException = function() {
  51. // sadly in FF,Safari,Chrome you can't make a DOMException
  52. var e, tmp;
  53. try {
  54. return new DOMException(DOMException.INVALID_CHARACTER_ERR);
  55. } catch (tmp) {
  56. // not available, just passback a duck-typed equiv
  57. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error
  58. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error/prototype
  59. var ex = new Error("DOM Exception 5");
  60. // ex.number and ex.description is IE-specific.
  61. ex.code = ex.number = 5;
  62. ex.name = ex.description = "INVALID_CHARACTER_ERR";
  63. // Safari/Chrome output format
  64. ex.toString = function() { return 'Error: ' + ex.name + ': ' + ex.message; };
  65. return ex;
  66. }
  67. }
  68. base64.getbyte64 = function(s,i) {
  69. // This is oddly fast, except on Chrome/V8.
  70. // Minimal or no improvement in performance by using a
  71. // object with properties mapping chars to value (eg. 'A': 0)
  72. var idx = base64.ALPHA.indexOf(s.charAt(i));
  73. if (idx === -1) {
  74. throw base64.makeDOMException();
  75. }
  76. return idx;
  77. }
  78. base64.decode = function(s) {
  79. // convert to string
  80. s = '' + s;
  81. var getbyte64 = base64.getbyte64;
  82. var pads, i, b10;
  83. var imax = s.length
  84. if (imax === 0) {
  85. return s;
  86. }
  87. if (imax % 4 !== 0) {
  88. throw base64.makeDOMException();
  89. }
  90. pads = 0
  91. if (s.charAt(imax - 1) === base64.PADCHAR) {
  92. pads = 1;
  93. if (s.charAt(imax - 2) === base64.PADCHAR) {
  94. pads = 2;
  95. }
  96. // either way, we want to ignore this last block
  97. imax -= 4;
  98. }
  99. var x = [];
  100. for (i = 0; i < imax; i += 4) {
  101. b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) |
  102. (getbyte64(s,i+2) << 6) | getbyte64(s,i+3);
  103. x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff, b10 & 0xff));
  104. }
  105. switch (pads) {
  106. case 1:
  107. b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) | (getbyte64(s,i+2) << 6);
  108. x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff));
  109. break;
  110. case 2:
  111. b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12);
  112. x.push(String.fromCharCode(b10 >> 16));
  113. break;
  114. }
  115. return x.join('');
  116. }
  117. base64.getbyte = function(s,i) {
  118. var x = s.charCodeAt(i);
  119. if (x > 255) {
  120. throw base64.makeDOMException();
  121. }
  122. return x;
  123. }
  124. base64.encode = function(s) {
  125. if (arguments.length !== 1) {
  126. throw new SyntaxError("Not enough arguments");
  127. }
  128. var padchar = base64.PADCHAR;
  129. var alpha = base64.ALPHA;
  130. var getbyte = base64.getbyte;
  131. var i, b10;
  132. var x = [];
  133. // convert to string
  134. s = '' + s;
  135. var imax = s.length - s.length % 3;
  136. if (s.length === 0) {
  137. return s;
  138. }
  139. for (i = 0; i < imax; i += 3) {
  140. b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8) | getbyte(s,i+2);
  141. x.push(alpha.charAt(b10 >> 18));
  142. x.push(alpha.charAt((b10 >> 12) & 0x3F));
  143. x.push(alpha.charAt((b10 >> 6) & 0x3f));
  144. x.push(alpha.charAt(b10 & 0x3f));
  145. }
  146. switch (s.length - imax) {
  147. case 1:
  148. b10 = getbyte(s,i) << 16;
  149. x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) +
  150. padchar + padchar);
  151. break;
  152. case 2:
  153. b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8);
  154. x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) +
  155. alpha.charAt((b10 >> 6) & 0x3f) + padchar);
  156. break;
  157. }
  158. return x.join('');
  159. }