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