Skip to content

Commit 4e4afd6

Browse files
committed
fix the base64 codec
1 parent 1837657 commit 4e4afd6

1 file changed

Lines changed: 19 additions & 130 deletions

File tree

src/rewrite/codecs.js

Lines changed: 19 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
// WARNING: this file is used by both the client and the server.
33
// Do not use any browser or node-specific API!
44
// -------------------------------------------------------------
5+
export const none = {
6+
encode: (str) => str,
7+
decode: (str) => str,
8+
};
9+
10+
export const plain = {
11+
encode(str) {
12+
if (!str) return str;
13+
return encodeURIComponent(str);
14+
},
15+
decode(str) {
16+
if (!str) return str;
17+
return decodeURIComponent(str);
18+
},
19+
};
20+
521
export const xor = {
622
encode(str) {
723
if (!str) return str;
@@ -24,144 +40,17 @@ export const xor = {
2440
},
2541
};
2642

27-
export const plain = {
28-
encode(str) {
29-
if (!str) return str;
30-
return encodeURIComponent(str);
31-
},
32-
decode(str) {
33-
if (!str) return str;
34-
return decodeURIComponent(str);
35-
},
36-
};
37-
3843
export const base64 = {
3944
encode(str) {
4045
if (!str) return str;
4146
str = str.toString();
42-
const b64chs = Array.from(
43-
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
44-
);
45-
let u32;
46-
let c0;
47-
let c1;
48-
let c2;
49-
let asc = '';
50-
let pad = str.length % 3;
51-
52-
for (let i = 0; i < str.length; ) {
53-
if (
54-
(c0 = str.charCodeAt(i++)) > 255 ||
55-
(c1 = str.charCodeAt(i++)) > 255 ||
56-
(c2 = str.charCodeAt(i++)) > 255
57-
)
58-
throw new TypeError('invalid character found');
59-
u32 = (c0 << 16) | (c1 << 8) | c2;
60-
asc +=
61-
b64chs[(u32 >> 18) & 63] +
62-
b64chs[(u32 >> 12) & 63] +
63-
b64chs[(u32 >> 6) & 63] +
64-
b64chs[u32 & 63];
65-
}
6647

67-
return encodeURIComponent(
68-
pad ? asc.slice(0, pad - 3) + '==='.substr(pad) : asc
69-
);
48+
return decodeURIComponent(btoa(str));
7049
},
7150
decode(str) {
7251
if (!str) return str;
73-
str = decodeURIComponent(str.toString());
74-
const b64tab = {
75-
0: 52,
76-
1: 53,
77-
2: 54,
78-
3: 55,
79-
4: 56,
80-
5: 57,
81-
6: 58,
82-
7: 59,
83-
8: 60,
84-
9: 61,
85-
A: 0,
86-
B: 1,
87-
C: 2,
88-
D: 3,
89-
E: 4,
90-
F: 5,
91-
G: 6,
92-
H: 7,
93-
I: 8,
94-
J: 9,
95-
K: 10,
96-
L: 11,
97-
M: 12,
98-
N: 13,
99-
O: 14,
100-
P: 15,
101-
Q: 16,
102-
R: 17,
103-
S: 18,
104-
T: 19,
105-
U: 20,
106-
V: 21,
107-
W: 22,
108-
X: 23,
109-
Y: 24,
110-
Z: 25,
111-
a: 26,
112-
b: 27,
113-
c: 28,
114-
d: 29,
115-
e: 30,
116-
f: 31,
117-
g: 32,
118-
h: 33,
119-
i: 34,
120-
j: 35,
121-
k: 36,
122-
l: 37,
123-
m: 38,
124-
n: 39,
125-
o: 40,
126-
p: 41,
127-
q: 42,
128-
r: 43,
129-
s: 44,
130-
t: 45,
131-
u: 46,
132-
v: 47,
133-
w: 48,
134-
x: 49,
135-
y: 50,
136-
z: 51,
137-
'+': 62,
138-
'/': 63,
139-
'=': 64,
140-
};
141-
str = str.replace(/\s+/g, '');
142-
str += '=='.slice(2 - (str.length & 3));
143-
let u24;
144-
let bin = '';
145-
let r1;
146-
let r2;
52+
str = str.toString()
14753

148-
for (let i = 0; i < str.length; ) {
149-
u24 =
150-
(b64tab[str.charAt(i++)] << 18) |
151-
(b64tab[str.charAt(i++)] << 12) |
152-
((r1 = b64tab[str.charAt(i++)]) << 6) |
153-
(r2 = b64tab[str.charAt(i++)]);
154-
bin +=
155-
r1 === 64
156-
? String.fromCharCode((u24 >> 16) & 255)
157-
: r2 === 64
158-
? String.fromCharCode((u24 >> 16) & 255, (u24 >> 8) & 255)
159-
: String.fromCharCode(
160-
(u24 >> 16) & 255,
161-
(u24 >> 8) & 255,
162-
u24 & 255
163-
);
164-
}
165-
return bin;
54+
return atob(str);
16655
},
16756
};

0 commit comments

Comments
 (0)