2
0

string-base64.js 5.1 KB

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