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.

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

mercurial