1// Base64 JavaScript decoder
2// Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
3
4// Permission to use, copy, modify, and/or distribute this software for any
5// purpose with or without fee is hereby granted, provided that the above
6// copyright notice and this permission notice appear in all copies.
7//
8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16const
17 haveU8 = (typeof Uint8Array == 'function');
18
19let decoder; // populated on first usage
20
21export class Base64 {
22
23 static decode(a) {
24 let isString = (typeof a == 'string');
25 let i;
26 if (decoder === undefined) {
27 let b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
28 ignore = '= \f\n\r\t\u00A0\u2028\u2029';
29 decoder = [];
30 for (i = 0; i < 64; ++i)
31 decoder[b64.charCodeAt(i)] = i;
32 for (i = 0; i < ignore.length; ++i)
33 decoder[ignore.charCodeAt(i)] = -1;
34 // RFC 3548 URL & file safe encoding
35 decoder['-'.charCodeAt(0)] = decoder['+'.charCodeAt(0)];
36 decoder['_'.charCodeAt(0)] = decoder['/'.charCodeAt(0)];
37 }
38 let out = haveU8 ? new Uint8Array(a.length * 3 >> 2) : [];
39 let bits = 0, char_count = 0, len = 0;
40 for (i = 0; i < a.length; ++i) {
41 let c = isString ? a.charCodeAt(i) : a[i];
42 if (c == 61) // '='.charCodeAt(0)
43 break;
44 c = decoder[c];
45 if (c == -1)
46 continue;
47 if (c === undefined)
48 throw 'Illegal character at offset ' + i;
49 bits |= c;
50 if (++char_count >= 4) {
51 out[len++] = (bits >> 16);
52 out[len++] = (bits >> 8) & 0xFF;
53 out[len++] = bits & 0xFF;
54 bits = 0;
55 char_count = 0;
56 } else {
57 bits <<= 6;
58 }
59 }
60 switch (char_count) {
61 case 1:
62 throw 'Base64 encoding incomplete: at least 2 bits missing';
63 case 2:
64 out[len++] = (bits >> 10);
65 break;
66 case 3:
67 out[len++] = (bits >> 16);
68 out[len++] = (bits >> 8) & 0xFF;
69 break;
70 }
71 if (haveU8 && out.length > len) // in case it was originally longer because of ignored characters
72 out = out.subarray(0, len);
73 return out;
74 }
75
76 static pretty(str) {
77 // fix padding
78 if (str.length % 4 > 0)
79 str = (str + '===').slice(0, str.length + str.length % 4);
80 // convert RFC 3548 to standard Base64
81 str = str.replace(/-/g, '+').replace(/_/g, '/');
82 // 80 column width
83 return str.replace(/(.{80})/g, '$1\n');
84 }
85
86 static unarmor(a) {
87 let m = Base64.re.exec(a);
88 if (m) {
89 if (m[1])
90 a = m[1];
91 else if (m[2])
92 a = m[2];
93 else if (m[3])
94 a = m[3];
95 else
96 throw 'RegExp out of sync';
97 }
98 return Base64.decode(a);
99 }
100
101}
102
103Base64.re = /-----BEGIN [^-]+-----([A-Za-z0-9+/=\s]+)-----END [^-]+-----|begin-base64[^\n]+\n([A-Za-z0-9+/=\s]+)====|^([A-Za-z0-9+/=\s]+)$/;