base64.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Modified from:
  3. * http://lxr.mozilla.org/mozilla/source/extensions/xml-rpc/src/nsXmlRpcClient.js#956
  4. */
  5. /* ***** BEGIN LICENSE BLOCK *****
  6. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  7. *
  8. * The contents of this file are subject to the Mozilla Public License Version
  9. * 1.1 (the "License"); you may not use this file except in compliance with
  10. * the License. You may obtain a copy of the License at
  11. * http://www.mozilla.org/MPL/
  12. *
  13. * Software distributed under the License is distributed on an "AS IS" basis,
  14. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. * for the specific language governing rights and limitations under the
  16. * License.
  17. *
  18. * The Original Code is Mozilla XML-RPC Client component.
  19. *
  20. * The Initial Developer of the Original Code is
  21. * Digital Creations 2, Inc.
  22. * Portions created by the Initial Developer are Copyright (C) 2000
  23. * the Initial Developer. All Rights Reserved.
  24. *
  25. * Contributor(s):
  26. * Martijn Pieters <mj@digicool.com> (original author)
  27. * Samuel Sieb <samuel@sieb.net>
  28. *
  29. * Alternatively, the contents of this file may be used under the terms of
  30. * either the GNU General Public License Version 2 or later (the "GPL"), or
  31. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  32. * in which case the provisions of the GPL or the LGPL are applicable instead
  33. * of those above. If you wish to allow use of your version of this file only
  34. * under the terms of either the GPL or the LGPL, and not to allow others to
  35. * use your version of this file under the terms of the MPL, indicate your
  36. * decision by deleting the provisions above and replace them with the notice
  37. * and other provisions required by the GPL or the LGPL. If you do not delete
  38. * the provisions above, a recipient may use your version of this file under
  39. * the terms of any one of the MPL, the GPL or the LGPL.
  40. *
  41. * ***** END LICENSE BLOCK ***** */
  42. /*jslint white: false, bitwise: false, plusplus: false */
  43. /*global console */
  44. var Base64 = {
  45. /* Convert data (an array of integers) to a Base64 string. */
  46. toBase64Table : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
  47. base64Pad : '=',
  48. encode: function (data) {
  49. "use strict";
  50. var result = '',
  51. chrTable = Base64.toBase64Table.split(''),
  52. pad = Base64.base64Pad,
  53. length = data.length,
  54. i;
  55. // Convert every three bytes to 4 ascii characters.
  56. for (i = 0; i < (length - 2); i += 3) {
  57. result += chrTable[data[i] >> 2];
  58. result += chrTable[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
  59. result += chrTable[((data[i+1] & 0x0f) << 2) + (data[i+2] >> 6)];
  60. result += chrTable[data[i+2] & 0x3f];
  61. }
  62. // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
  63. if (length%3) {
  64. i = length - (length%3);
  65. result += chrTable[data[i] >> 2];
  66. if ((length%3) === 2) {
  67. result += chrTable[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
  68. result += chrTable[(data[i+1] & 0x0f) << 2];
  69. result += pad;
  70. } else {
  71. result += chrTable[(data[i] & 0x03) << 4];
  72. result += pad + pad;
  73. }
  74. }
  75. return result;
  76. },
  77. /* Convert Base64 data to a string */
  78. toBinaryTable : [
  79. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  80. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  81. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
  82. 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
  83. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
  84. 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
  85. -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
  86. 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
  87. ],
  88. decode: function (data, offset) {
  89. "use strict";
  90. offset = typeof(offset) !== 'undefined' ? offset : 0;
  91. var binTable = Base64.toBinaryTable,
  92. pad = Base64.base64Pad,
  93. result, result_length, idx, i, c, padding,
  94. leftbits = 0, // number of bits decoded, but yet to be appended
  95. leftdata = 0, // bits decoded, but yet to be appended
  96. data_length = data.indexOf('=') - offset;
  97. if (data_length < 0) { data_length = data.length - offset; }
  98. /* Every four characters is 3 resulting numbers */
  99. result_length = (data_length >> 2) * 3 + Math.floor((data_length%4)/1.5);
  100. result = new Array(result_length);
  101. // Convert one by one.
  102. for (idx = 0, i = offset; i < data.length; i++) {
  103. c = binTable[data.charCodeAt(i) & 0x7f];
  104. padding = (data.charAt(i) === pad);
  105. // Skip illegal characters and whitespace
  106. if (c === -1) {
  107. console.error("Illegal character '" + data.charCodeAt(i) + "'");
  108. continue;
  109. }
  110. // Collect data into leftdata, update bitcount
  111. leftdata = (leftdata << 6) | c;
  112. leftbits += 6;
  113. // If we have 8 or more bits, append 8 bits to the result
  114. if (leftbits >= 8) {
  115. leftbits -= 8;
  116. // Append if not padding.
  117. if (!padding) {
  118. result[idx++] = (leftdata >> leftbits) & 0xff;
  119. }
  120. leftdata &= (1 << leftbits) - 1;
  121. }
  122. }
  123. // If there are any bits left, the base64 string was corrupted
  124. if (leftbits) {
  125. throw {name: 'Base64-Error',
  126. message: 'Corrupted base64 string'};
  127. }
  128. return result;
  129. }
  130. }; /* End of Base64 namespace */