1// Base64 JavaScript decoder
2// Copyright (c) 2008-2022 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
16(typeof define != 'undefined' ? define : function (factory) { 'use strict';
17 if (typeof module == 'object') module.exports = factory();
18 else window.base64 = factory();
19})(function () {
20"use strict";
21
22var Base64 = {},
23 decoder, // populated on first usage
24 haveU8 = (typeof Uint8Array == 'function');
25
26Base64.decode = function (a) {
27 var isString = (typeof a == 'string');
28 var i;
29 if (decoder === undefined) {
30 var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
31 ignore = "= \f\n\r\t\u00A0\u2028\u2029";
32 decoder = [];
33 for (i = 0; i < 64; ++i)
34 decoder[b64.charCodeAt(i)] = i;
35 for (i = 0; i < ignore.length; ++i)
36 decoder[ignore.charCodeAt(i)] = -1;
37 // RFC 3548 URL & file safe encoding
38 decoder['-'.charCodeAt(0)] = decoder['+'.charCodeAt(0)];
39 decoder['_'.charCodeAt(0)] = decoder['/'.charCodeAt(0)];
40 }
41 var out = haveU8 ? new Uint8Array(a.length * 3 >> 2) : [];
42 var bits = 0, char_count = 0, len = 0;
43 for (i = 0; i < a.length; ++i) {
44 var c = isString ? a.charCodeAt(i) : a[i];
45 if (c == 61) // '='.charCodeAt(0)
46 break;
47 c = decoder[c];
48 if (c == -1)
49 continue;
50 if (c === undefined)
51 throw 'Illegal character at offset ' + i;
52 bits |= c;
53 if (++char_count >= 4) {
54 out[len++] = (bits >> 16);
55 out[len++] = (bits >> 8) & 0xFF;
56 out[len++] = bits & 0xFF;
57 bits = 0;
58 char_count = 0;
59 } else {
60 bits <<= 6;
61 }
62 }
63 switch (char_count) {
64 case 1:
65 throw "Base64 encoding incomplete: at least 2 bits missing";
66 case 2:
67 out[len++] = (bits >> 10);
68 break;
69 case 3:
70 out[len++] = (bits >> 16);
71 out[len++] = (bits >> 8) & 0xFF;
72 break;
73 }
74 if (haveU8 && out.length > len) // in case it was originally longer because of ignored characters
75 out = out.subarray(0, len);
76 return out;
77};
78
79Base64.pretty = function (str) {
80 // fix padding
81 if (str.length % 4 > 0)
82 str = (str + '===').slice(0, str.length + str.length % 4);
83 // convert RFC 3548 to standard Base64
84 str = str.replace(/-/g, '+').replace(/_/g, '/');
85 // 80 column width
86 return str.replace(/(.{80})/g, '$1\n');
87};
88
89Base64.re = /-----BEGIN [^-]+-----([A-Za-z0-9+/=\s]+)-----END [^-]+-----|begin-base64[^\n]+\n([A-Za-z0-9+/=\s]+)====|^([A-Za-z0-9+/=\s]+)$/;
90Base64.unarmor = function (a) {
91 var m = Base64.re.exec(a);
92 if (m) {
93 if (m[1])
94 a = m[1];
95 else if (m[2])
96 a = m[2];
97 else if (m[3])
98 a = m[3];
99 else
100 throw "RegExp out of sync";
101 }
102 return Base64.decode(a);
103};
104
105return Base64;
106
107});