js/src/devtools/jint/sunspider/string-base64.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 // From: http://lxr.mozilla.org/mozilla/source/extensions/xml-rpc/src/nsXmlRpcClient.js#956
     7 /* Convert data (an array of integers) to a Base64 string. */
     8 var toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
     9 var base64Pad = '=';
    11 function toBase64(data) {
    12     var result = '';
    13     var length = data.length;
    14     var i;
    15     // Convert every three bytes to 4 ascii characters.
    16   /* BEGIN LOOP */
    17     for (i = 0; i < (length - 2); i += 3) {
    18         result += toBase64Table[data[i] >> 2];
    19         result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
    20         result += toBase64Table[((data[i+1] & 0x0f) << 2) + (data[i+2] >> 6)];
    21         result += toBase64Table[data[i+2] & 0x3f];
    22     }
    23   /* END LOOP */
    25     // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
    26     if (length%3) {
    27         i = length - (length%3);
    28         result += toBase64Table[data[i] >> 2];
    29         if ((length%3) == 2) {
    30             result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
    31             result += toBase64Table[(data[i+1] & 0x0f) << 2];
    32             result += base64Pad;
    33         } else {
    34             result += toBase64Table[(data[i] & 0x03) << 4];
    35             result += base64Pad + base64Pad;
    36         }
    37     }
    39     return result;
    40 }
    42 /* Convert Base64 data to a string */
    43 var toBinaryTable = [
    44     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    45     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    46     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
    47     52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
    48     -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
    49     15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
    50     -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
    51     41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
    52 ];
    54 function base64ToString(data) {
    55     var result = '';
    56     var leftbits = 0; // number of bits decoded, but yet to be appended
    57     var leftdata = 0; // bits decoded, but yet to be appended
    59     // Convert one by one.
    60   /* BEGIN LOOP */
    61     for (var i = 0; i < data.length; i++) {
    62         var c = toBinaryTable[data.charCodeAt(i) & 0x7f];
    63         var padding = (data[i] == base64Pad);
    64         // Skip illegal characters and whitespace
    65         if (c == -1) continue;
    67         // Collect data into leftdata, update bitcount
    68         leftdata = (leftdata << 6) | c;
    69         leftbits += 6;
    71         // If we have 8 or more bits, append 8 bits to the result
    72         if (leftbits >= 8) {
    73             leftbits -= 8;
    74             // Append if not padding.
    75             if (!padding)
    76                 result += String.fromCharCode((leftdata >> leftbits) & 0xff);
    77             leftdata &= (1 << leftbits) - 1;
    78         }
    79     }
    80   /* END LOOP */
    82     // If there are any bits left, the base64 string was corrupted
    83     if (leftbits)
    84         throw Components.Exception('Corrupted base64 string');
    86     return result;
    87 }
    89 var str = "";
    91   /* BEGIN LOOP */
    92 for ( var i = 0; i < 8192; i++ )
    93         str += String.fromCharCode( (25 * Math.random()) + 97 );
    94   /* END LOOP */
    96   /* BEGIN LOOP */
    97 for ( var i = 8192; i <= 16384; i *= 2 ) {
    99     var base64;
   101     base64 = toBase64(str);
   102     base64ToString(base64);
   104     // Double the string
   105     str += str;
   106 }
   107   /* END LOOP */
   109 toBinaryTable = null;

mercurial