4 Copyright (C) 2007 Apple Inc. All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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.
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.
28 <title>SunSpider string-base64</title>
29 <link rel="stylesheet" href="sunspider.css">
33 <h3>string-base64</h3>
36 <script src="sunspider-record-result.js"></script>
39 var _sunSpiderStartDate = new Date();
41 /* ***** BEGIN LICENSE BLOCK *****
42 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
44 * The contents of this file are subject to the Mozilla Public License Version
45 * 1.1 (the "License"); you may not use this file except in compliance with
46 * the License. You may obtain a copy of the License at
47 * http://www.mozilla.org/MPL/
49 * Software distributed under the License is distributed on an "AS IS" basis,
50 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
51 * for the specific language governing rights and limitations under the
54 * The Original Code is Mozilla XML-RPC Client component.
56 * The Initial Developer of the Original Code is
57 * Digital Creations 2, Inc.
58 * Portions created by the Initial Developer are Copyright (C) 2000
59 * the Initial Developer. All Rights Reserved.
62 * Martijn Pieters <mj@digicool.com> (original author)
63 * Samuel Sieb <samuel@sieb.net>
65 * Alternatively, the contents of this file may be used under the terms of
66 * either the GNU General Public License Version 2 or later (the "GPL"), or
67 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
68 * in which case the provisions of the GPL or the LGPL are applicable instead
69 * of those above. If you wish to allow use of your version of this file only
70 * under the terms of either the GPL or the LGPL, and not to allow others to
71 * use your version of this file under the terms of the MPL, indicate your
72 * decision by deleting the provisions above and replace them with the notice
73 * and other provisions required by the GPL or the LGPL. If you do not delete
74 * the provisions above, a recipient may use your version of this file under
75 * the terms of any one of the MPL, the GPL or the LGPL.
77 * ***** END LICENSE BLOCK ***** */
79 // From: http://lxr.mozilla.org/mozilla/source/extensions/xml-rpc/src/nsXmlRpcClient.js#956
81 /* Convert data (an array of integers) to a Base64 string. */
82 var toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
85 function toBase64(data) {
87 var length = data.length;
89 // Convert every three bytes to 4 ascii characters.
90 for (i = 0; i < (length - 2); i += 3) {
91 result += toBase64Table[data[i] >> 2];
92 result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
93 result += toBase64Table[((data[i+1] & 0x0f) << 2) + (data[i+2] >> 6)];
94 result += toBase64Table[data[i+2] & 0x3f];
97 // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
99 i = length - (length%3);
100 result += toBase64Table[data[i] >> 2];
101 if ((length%3) == 2) {
102 result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
103 result += toBase64Table[(data[i+1] & 0x0f) << 2];
106 result += toBase64Table[(data[i] & 0x03) << 4];
107 result += base64Pad + base64Pad;
114 /* Convert Base64 data to a string */
115 var toBinaryTable = [
116 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
117 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
118 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
119 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
120 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
121 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
122 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
123 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
126 function base64ToString(data) {
128 var leftbits = 0; // number of bits decoded, but yet to be appended
129 var leftdata = 0; // bits decoded, but yet to be appended
131 // Convert one by one.
132 for (var i = 0; i < data.length; i++) {
133 var c = toBinaryTable[data.charCodeAt(i) & 0x7f];
134 var padding = (data[i] == base64Pad);
135 // Skip illegal characters and whitespace
136 if (c == -1) continue;
138 // Collect data into leftdata, update bitcount
139 leftdata = (leftdata << 6) | c;
142 // If we have 8 or more bits, append 8 bits to the result
145 // Append if not padding.
147 result += String.fromCharCode((leftdata >> leftbits) & 0xff);
148 leftdata &= (1 << leftbits) - 1;
152 // If there are any bits left, the base64 string was corrupted
154 throw Components.Exception('Corrupted base64 string');
161 for ( var i = 0; i < 8192; i++ )
162 str += String.fromCharCode( (25 * Math.random()) + 97 );
164 for ( var i = 8192; i <= 16384; i *= 2 ) {
168 base64 = toBase64(str);
169 base64ToString(base64);
175 toBinaryTable = null;
178 var _sunSpiderInterval = new Date() - _sunSpiderStartDate;
180 record(_sunSpiderInterval);