]> git.proxmox.com Git - mirror_novnc.git/blame - core/base64.js
Style all input types for consistent UI
[mirror_novnc.git] / core / base64.js
CommitLineData
ab7674ff
JM
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/. */
4
5// From: http://hg.mozilla.org/mozilla-central/raw-file/ec10630b1a54/js/src/devtools/jint/sunspider/string-base64.js
b5537b60 6
a0035359 7import * as Log from './util/logging.js';
d595e656 8
6d6f0db0 9export default {
f8e9b9f1 10 /* Convert data (an array of integers) to a Base64 string. */
942a3127
PO
11 toBase64Table: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split(''),
12 base64Pad: '=',
f8e9b9f1 13
0e4808bf 14 encode(data) {
f8e9b9f1 15 "use strict";
2b5f94fa
JD
16 let result = '';
17 const length = data.length;
18 const lengthpad = (length % 3);
f8e9b9f1
SR
19 // Convert every three bytes to 4 ascii characters.
20
2b5f94fa
JD
21 for (let i = 0; i < (length - 2); i += 3) {
22 result += this.toBase64Table[data[i] >> 2];
23 result += this.toBase64Table[((data[i] & 0x03) << 4) + (data[i + 1] >> 4)];
24 result += this.toBase64Table[((data[i + 1] & 0x0f) << 2) + (data[i + 2] >> 6)];
25 result += this.toBase64Table[data[i + 2] & 0x3f];
f8e9b9f1 26 }
b5537b60 27
f8e9b9f1 28 // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
2b5f94fa 29 const j = length - lengthpad;
f8e9b9f1 30 if (lengthpad === 2) {
2b5f94fa
JD
31 result += this.toBase64Table[data[j] >> 2];
32 result += this.toBase64Table[((data[j] & 0x03) << 4) + (data[j + 1] >> 4)];
33 result += this.toBase64Table[(data[j + 1] & 0x0f) << 2];
34 result += this.toBase64Table[64];
f8e9b9f1 35 } else if (lengthpad === 1) {
2b5f94fa
JD
36 result += this.toBase64Table[data[j] >> 2];
37 result += this.toBase64Table[(data[j] & 0x03) << 4];
38 result += this.toBase64Table[64];
39 result += this.toBase64Table[64];
5d8e7ec0 40 }
b5537b60 41
f8e9b9f1
SR
42 return result;
43 },
44
45 /* Convert Base64 data to a string */
6786fd87 46 /* eslint-disable comma-spacing */
942a3127 47 toBinaryTable: [
f8e9b9f1
SR
48 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
49 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
50 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
51 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
52 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
53 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
54 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
55 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
56 ],
6786fd87 57 /* eslint-enable comma-spacing */
f8e9b9f1 58
9d2c9d1a 59 decode(data, offset = 0) {
2b5f94fa 60 let data_length = data.indexOf('=') - offset;
f8e9b9f1
SR
61 if (data_length < 0) { data_length = data.length - offset; }
62
63 /* Every four characters is 3 resulting numbers */
2b5f94fa
JD
64 const result_length = (data_length >> 2) * 3 + Math.floor((data_length % 4) / 1.5);
65 const result = new Array(result_length);
f8e9b9f1
SR
66
67 // Convert one by one.
2b5f94fa
JD
68
69 let leftbits = 0; // number of bits decoded, but yet to be appended
70 let leftdata = 0; // bits decoded, but yet to be appended
71 for (let idx = 0, i = offset; i < data.length; i++) {
72 const c = this.toBinaryTable[data.charCodeAt(i) & 0x7f];
73 const padding = (data.charAt(i) === this.base64Pad);
f8e9b9f1
SR
74 // Skip illegal characters and whitespace
75 if (c === -1) {
a0035359 76 Log.Error("Illegal character code " + data.charCodeAt(i) + " at position " + i);
f8e9b9f1
SR
77 continue;
78 }
ae510306 79
f8e9b9f1
SR
80 // Collect data into leftdata, update bitcount
81 leftdata = (leftdata << 6) | c;
82 leftbits += 6;
83
84 // If we have 8 or more bits, append 8 bits to the result
85 if (leftbits >= 8) {
86 leftbits -= 8;
87 // Append if not padding.
88 if (!padding) {
89 result[idx++] = (leftdata >> leftbits) & 0xff;
90 }
91 leftdata &= (1 << leftbits) - 1;
d595e656 92 }
cc0410a3 93 }
b5537b60 94
f8e9b9f1
SR
95 // If there are any bits left, the base64 string was corrupted
96 if (leftbits) {
2b5f94fa 97 const err = new Error('Corrupted base64 string');
f8e9b9f1
SR
98 err.name = 'Base64-Error';
99 throw err;
100 }
b5537b60 101
f8e9b9f1
SR
102 return result;
103 }
b5537b60 104}; /* End of Base64 namespace */