-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBase64.ts
More file actions
100 lines (83 loc) · 3.11 KB
/
Base64.ts
File metadata and controls
100 lines (83 loc) · 3.11 KB
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
import {Utils} from "../QueueITHelpers";
export class Base64 {
private static PADCHAR: string = '=';
private static ALPHA: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
private static getByte64(s: string, i: number): number {
return Base64.ALPHA.indexOf(s.charAt(i));
}
public static decode(s: string): Uint8Array {
s = s.split('-').join('+')
.split('_').join('/');
if (s.length === 0) {
return new Uint8Array(0);
}
let padding = s.length % 4;
if(padding==3) padding = 1;
s = Utils.padRight(s, "=", s.length + padding);
let pads = 0,
i, b10, imax = s.length,
x = [];
if (s.charAt(imax - 1) === Base64.PADCHAR) {
pads = 1;
if (s.charAt(imax - 2) === Base64.PADCHAR) {
pads = 2;
}
imax -= 4;
}
for (i = 0; i < imax; i += 4) {
b10 = (Base64.getByte64(s, i) << 18) | (Base64.getByte64(s, i + 1) << 12) | (Base64.getByte64(s, i + 2) << 6) | Base64.getByte64(s, i + 3);
x.push(b10 >> 16, (b10 >> 8) & 255, b10 & 255);
}
switch (pads) {
case 1:
b10 = (Base64.getByte64(s, i) << 18) | (Base64.getByte64(s, i + 1) << 12) | (Base64.getByte64(s, i + 2) << 6);
x.push(b10 >> 16, (b10 >> 8) & 255);
break;
case 2:
b10 = (Base64.getByte64(s, i) << 18) | (Base64.getByte64(s, i + 1) << 12);
x.push(b10 >> 16);
break;
}
return new Uint8Array(x);
}
public static encode(s: Uint8Array): string {
let i, b10, x = [],
imax = s.length - s.length % 3;
if (s.length === 0) {
return s.toString();
}
for (i = 0; i < imax; i += 3) {
b10 = (s[i] << 16) | (s[i + 1] << 8) | s[i + 2];
x.push(this.ALPHA.charAt(b10 >> 18));
x.push(this.ALPHA.charAt((b10 >> 12) & 63));
x.push(this.ALPHA.charAt((b10 >> 6) & 63));
x.push(this.ALPHA.charAt(b10 & 63));
}
switch (s.length - imax) {
case 1:
b10 = s[i] << 16;
x.push(Base64.ALPHA.charAt(b10 >> 18) + Base64.ALPHA.charAt((b10 >> 12) & 63) + Base64.PADCHAR + Base64.PADCHAR);
break;
case 2:
b10 = (s[i] << 16) | (s[i + 1] << 8);
x.push(Base64.ALPHA.charAt(b10 >> 18) + Base64.ALPHA.charAt((b10 >> 12) & 63) + Base64.ALPHA.charAt((b10 >> 6) & 63) + Base64.PADCHAR);
break;
}
const encoded = x.join('')
.split('+').join('-')
.split('/').join('_');
return trimEnd(encoded, '=');
}
}
function trimEnd(value: string, charsToTrim: string): string {
if (value.length == 0) return "";
let i = value.length;
for (; i >= 0;) {
let contained = charsToTrim.indexOf(value.charAt(i)) != -1;
if (!contained) {
break;
}
i--;
}
return value.substring(0, i + 1);
}