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