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