]>
git.proxmox.com Git - mirror_novnc.git/blob - core/base64.js
48e28c313c640a2a1bc5bc6ef9bc3cb9bd2e09ed
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://hg.mozilla.org/mozilla-central/raw-file/ec10630b1a54/js/src/devtools/jint/sunspider/string-base64.js
7 import * as Log
from './util/logging.js';
10 /* Convert data (an array of integers) to a Base64 string. */
11 toBase64Table
: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split(''),
14 encode: function (data
) {
17 var toBase64Table
= this.toBase64Table
;
18 var length
= data
.length
;
19 var lengthpad
= (length
% 3);
20 // Convert every three bytes to 4 ascii characters.
22 for (var i
= 0; i
< (length
- 2); i
+= 3) {
23 result
+= toBase64Table
[data
[i
] >> 2];
24 result
+= toBase64Table
[((data
[i
] & 0x03) << 4) + (data
[i
+ 1] >> 4)];
25 result
+= toBase64Table
[((data
[i
+ 1] & 0x0f) << 2) + (data
[i
+ 2] >> 6)];
26 result
+= toBase64Table
[data
[i
+ 2] & 0x3f];
29 // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
31 if (lengthpad
=== 2) {
32 j
= length
- lengthpad
;
33 result
+= toBase64Table
[data
[j
] >> 2];
34 result
+= toBase64Table
[((data
[j
] & 0x03) << 4) + (data
[j
+ 1] >> 4)];
35 result
+= toBase64Table
[(data
[j
+ 1] & 0x0f) << 2];
36 result
+= toBase64Table
[64];
37 } else if (lengthpad
=== 1) {
38 j
= length
- lengthpad
;
39 result
+= toBase64Table
[data
[j
] >> 2];
40 result
+= toBase64Table
[(data
[j
] & 0x03) << 4];
41 result
+= toBase64Table
[64];
42 result
+= toBase64Table
[64];
48 /* Convert Base64 data to a string */
50 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
51 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
52 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
53 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
54 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
55 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
56 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
57 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
60 decode: function (data
, offset
) {
62 offset
= typeof(offset
) !== 'undefined' ? offset
: 0;
63 var toBinaryTable
= this.toBinaryTable
;
64 var base64Pad
= this.base64Pad
;
65 var result
, result_length
;
66 var leftbits
= 0; // number of bits decoded, but yet to be appended
67 var leftdata
= 0; // bits decoded, but yet to be appended
68 var data_length
= data
.indexOf('=') - offset
;
70 if (data_length
< 0) { data_length
= data
.length
- offset
; }
72 /* Every four characters is 3 resulting numbers */
73 result_length
= (data_length
>> 2) * 3 + Math
.floor((data_length
% 4) / 1.5);
74 result
= new Array(result_length
);
76 // Convert one by one.
77 for (var idx
= 0, i
= offset
; i
< data
.length
; i
++) {
78 var c
= toBinaryTable
[data
.charCodeAt(i
) & 0x7f];
79 var padding
= (data
.charAt(i
) === base64Pad
);
80 // Skip illegal characters and whitespace
82 Log
.Error("Illegal character code " + data
.charCodeAt(i
) + " at position " + i
);
86 // Collect data into leftdata, update bitcount
87 leftdata
= (leftdata
<< 6) | c
;
90 // If we have 8 or more bits, append 8 bits to the result
93 // Append if not padding.
95 result
[idx
++] = (leftdata
>> leftbits
) & 0xff;
97 leftdata
&= (1 << leftbits
) - 1;
101 // If there are any bits left, the base64 string was corrupted
103 var err
= new Error('Corrupted base64 string');
104 err
.name
= 'Base64-Error';
110 }; /* End of Base64 namespace */