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