]>
git.proxmox.com Git - mirror_novnc.git/blob - include/base64.js
3 * http://lxr.mozilla.org/mozilla/source/extensions/xml-rpc/src/nsXmlRpcClient.js#956
6 /* ***** BEGIN LICENSE BLOCK *****
7 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
19 * The Original Code is Mozilla XML-RPC Client component.
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.
27 * Martijn Pieters <mj@digicool.com> (original author)
28 * Samuel Sieb <samuel@sieb.net>
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.
42 * ***** END LICENSE BLOCK ***** */
46 /* Convert data (an array of integers) to a Base64 string. */
47 toBase64Table
: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
50 encode: function (data
) {
52 var chrTable
= Base64
.toBase64Table
.split('');
53 var pad
= Base64
.base64Pad
;
54 var length
= data
.length
;
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];
64 // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
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];
73 result
+= chrTable
[(data
[i
] & 0x03) << 4];
81 /* Convert Base64 data to a string */
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
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
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
;
104 var result_length
= (data_length
>> 2) * 3 + Math
.floor((data_length
%4)/1.5);
105 var result
= new Array(result_length
);
107 // Convert one by one.
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
114 console
.log("Illegal character '" + data
.charCodeAt(i
) + "'");
118 // Collect data into leftdata, update bitcount
119 leftdata
= (leftdata
<< 6) | c
;
122 // If we have 8 or more bits, append 8 bits to the result
125 // Append if not padding.
127 result
[idx
++] = (leftdata
>> leftbits
) & 0xff;
128 leftdata
&= (1 << leftbits
) - 1;
132 // If there are any bits left, the base64 string was corrupted
134 throw {name
: 'Base64-Error',
135 message
: 'Corrupted base64 string'};
140 }; /* End of Base64 namespace */