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