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