string-base64.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE html>
  2. <head>
  3. <!--
  4. Copyright (C) 2007 Apple Inc. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. -->
  25. <title>SunSpider string-base64</title>
  26. </head>
  27. <body>
  28. <h3>string-base64</h3>
  29. <div id="console">
  30. </div>
  31. <script>
  32. var _sunSpiderStartDate = new Date();
  33. /* This Source Code Form is subject to the terms of the Mozilla Public
  34. * License, v. 2.0. If a copy of the MPL was not distributed with this
  35. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  36. // From: http://lxr.mozilla.org/mozilla/source/extensions/xml-rpc/src/nsXmlRpcClient.js#956
  37. /* Convert data (an array of integers) to a Base64 string. */
  38. var toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  39. var base64Pad = '=';
  40. function toBase64(data) {
  41. var result = '';
  42. var length = data.length;
  43. var i;
  44. // Convert every three bytes to 4 ascii characters.
  45. for (i = 0; i < (length - 2); i += 3) {
  46. result += toBase64Table[data[i] >> 2];
  47. result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
  48. result += toBase64Table[((data[i+1] & 0x0f) << 2) + (data[i+2] >> 6)];
  49. result += toBase64Table[data[i+2] & 0x3f];
  50. }
  51. // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
  52. if (length%3) {
  53. i = length - (length%3);
  54. result += toBase64Table[data[i] >> 2];
  55. if ((length%3) == 2) {
  56. result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
  57. result += toBase64Table[(data[i+1] & 0x0f) << 2];
  58. result += base64Pad;
  59. } else {
  60. result += toBase64Table[(data[i] & 0x03) << 4];
  61. result += base64Pad + base64Pad;
  62. }
  63. }
  64. return result;
  65. }
  66. /* Convert Base64 data to a string */
  67. var toBinaryTable = [
  68. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  69. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  70. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
  71. 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
  72. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
  73. 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
  74. -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
  75. 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
  76. ];
  77. function base64ToString(data) {
  78. var result = '';
  79. var leftbits = 0; // number of bits decoded, but yet to be appended
  80. var leftdata = 0; // bits decoded, but yet to be appended
  81. // Convert one by one.
  82. for (var i = 0; i < data.length; i++) {
  83. var c = toBinaryTable[data.charCodeAt(i) & 0x7f];
  84. var padding = (data[i] == base64Pad);
  85. // Skip illegal characters and whitespace
  86. if (c == -1) continue;
  87. // Collect data into leftdata, update bitcount
  88. leftdata = (leftdata << 6) | c;
  89. leftbits += 6;
  90. // If we have 8 or more bits, append 8 bits to the result
  91. if (leftbits >= 8) {
  92. leftbits -= 8;
  93. // Append if not padding.
  94. if (!padding)
  95. result += String.fromCharCode((leftdata >> leftbits) & 0xff);
  96. leftdata &= (1 << leftbits) - 1;
  97. }
  98. }
  99. // If there are any bits left, the base64 string was corrupted
  100. if (leftbits)
  101. throw Components.Exception('Corrupted base64 string');
  102. return result;
  103. }
  104. var str = "";
  105. for ( var i = 0; i < 8192; i++ )
  106. str += String.fromCharCode( (25 * Math.random()) + 97 );
  107. for ( var i = 8192; i <= 16384; i *= 2 ) {
  108. var base64;
  109. base64 = toBase64(str);
  110. base64ToString(base64);
  111. // Double the string
  112. str += str;
  113. }
  114. toBinaryTable = null;
  115. var _sunSpiderInterval = new Date() - _sunSpiderStartDate;
  116. document.getElementById("console").innerHTML = _sunSpiderInterval;
  117. </script>
  118. </body>
  119. </html>