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