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