base64.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. Base64 = {
  43. /* Convert data (an array of integers) to a Base64 string. */
  44. toBase64Table : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
  45. base64Pad : '=',
  46. encode: function (data) {
  47. var result = '';
  48. var chrTable = Base64.toBase64Table;
  49. var pad = Base64.base64Pad;
  50. var length = data.length;
  51. var i;
  52. // Convert every three bytes to 4 ascii characters.
  53. for (i = 0; i < (length - 2); i += 3) {
  54. result += chrTable[data[i] >> 2];
  55. result += chrTable[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
  56. result += chrTable[((data[i+1] & 0x0f) << 2) + (data[i+2] >> 6)];
  57. result += chrTable[data[i+2] & 0x3f];
  58. }
  59. // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
  60. if (length%3) {
  61. i = length - (length%3);
  62. result += chrTable[data[i] >> 2];
  63. if ((length%3) == 2) {
  64. result += chrTable[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
  65. result += chrTable[(data[i+1] & 0x0f) << 2];
  66. result += pad;
  67. } else {
  68. result += chrTable[(data[i] & 0x03) << 4];
  69. result += pad + pad;
  70. }
  71. }
  72. return result;
  73. },
  74. /* Convert Base64 data to a string */
  75. toBinaryTable : [
  76. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  77. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  78. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
  79. 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
  80. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
  81. 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
  82. -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
  83. 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
  84. ],
  85. decode: function (data) {
  86. var binTable = Base64.toBinaryTable;
  87. var pad = Base64.base64Pad;
  88. var leftbits = 0; // number of bits decoded, but yet to be appended
  89. var leftdata = 0; // bits decoded, but yet to be appended
  90. /* Every four characters is 3 resulting numbers */
  91. var data_length = data.indexOf('=');
  92. if (data_length == -1) data_length = data.length;
  93. var result_length = (data_length >> 2) * 3 + (data.length % 4 - 1);
  94. var result = new Array(result_length);
  95. // Convert one by one.
  96. var idx = 0;
  97. for (var i = 0; i < data.length; i++) {
  98. var c = binTable[data[i].charCodeAt(0) & 0x7f];
  99. var padding = (data[i] == pad);
  100. // Skip illegal characters and whitespace
  101. if (c == -1) continue;
  102. // Collect data into leftdata, update bitcount
  103. leftdata = (leftdata << 6) | c;
  104. leftbits += 6;
  105. // If we have 8 or more bits, append 8 bits to the result
  106. if (leftbits >= 8) {
  107. leftbits -= 8;
  108. // Append if not padding.
  109. if (!padding)
  110. result[idx++] = (leftdata >> leftbits) & 0xff;
  111. leftdata &= (1 << leftbits) - 1;
  112. }
  113. }
  114. // If there are any bits left, the base64 string was corrupted
  115. if (leftbits)
  116. throw Components.Exception('Corrupted base64 string');
  117. return result;
  118. }
  119. }; /* End of Base64 namespace */