@yume-chan/adb 0.0.15 → 0.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +33 -0
- package/CHANGELOG.md +18 -1
- package/LICENSE +21 -21
- package/README.md +247 -247
- package/esm/adb.d.ts +15 -3
- package/esm/adb.d.ts.map +1 -1
- package/esm/adb.js +18 -6
- package/esm/adb.js.map +1 -1
- package/esm/auth.d.ts.map +1 -1
- package/esm/auth.js.map +1 -1
- package/esm/backend.d.ts +2 -2
- package/esm/backend.d.ts.map +1 -1
- package/esm/commands/base.d.ts.map +1 -1
- package/esm/commands/base.js.map +1 -1
- package/esm/commands/framebuffer.d.ts +17 -1
- package/esm/commands/framebuffer.d.ts.map +1 -1
- package/esm/commands/framebuffer.js +4 -15
- package/esm/commands/framebuffer.js.map +1 -1
- package/esm/commands/install.d.ts +2 -2
- package/esm/commands/install.d.ts.map +1 -1
- package/esm/commands/install.js +3 -2
- package/esm/commands/install.js.map +1 -1
- package/esm/commands/power.d.ts +1 -1
- package/esm/commands/power.js +1 -1
- package/esm/commands/reverse.d.ts +3 -3
- package/esm/commands/reverse.d.ts.map +1 -1
- package/esm/commands/reverse.js +9 -9
- package/esm/commands/reverse.js.map +1 -1
- package/esm/commands/subprocess/command.d.ts +52 -0
- package/esm/commands/subprocess/command.d.ts.map +1 -0
- package/esm/commands/subprocess/command.js +83 -0
- package/esm/commands/subprocess/command.js.map +1 -0
- package/esm/commands/subprocess/index.d.ts +1 -53
- package/esm/commands/subprocess/index.d.ts.map +1 -1
- package/esm/commands/subprocess/index.js +1 -85
- package/esm/commands/subprocess/index.js.map +1 -1
- package/esm/commands/subprocess/protocols/none.d.ts +5 -4
- package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
- package/esm/commands/subprocess/protocols/none.js +11 -8
- package/esm/commands/subprocess/protocols/none.js.map +1 -1
- package/esm/commands/subprocess/protocols/shell.d.ts +4 -4
- package/esm/commands/subprocess/protocols/shell.d.ts.map +1 -1
- package/esm/commands/subprocess/protocols/shell.js +6 -6
- package/esm/commands/subprocess/protocols/shell.js.map +1 -1
- package/esm/commands/subprocess/protocols/types.d.ts +7 -7
- package/esm/commands/subprocess/protocols/types.d.ts.map +1 -1
- package/esm/commands/sync/index.d.ts +1 -1
- package/esm/commands/sync/index.d.ts.map +1 -1
- package/esm/commands/sync/index.js +1 -1
- package/esm/commands/sync/index.js.map +1 -1
- package/esm/commands/sync/list.d.ts +2 -2
- package/esm/commands/sync/list.d.ts.map +1 -1
- package/esm/commands/sync/list.js +15 -43
- package/esm/commands/sync/list.js.map +1 -1
- package/esm/commands/sync/pull.d.ts +3 -2
- package/esm/commands/sync/pull.d.ts.map +1 -1
- package/esm/commands/sync/pull.js +15 -17
- package/esm/commands/sync/pull.js.map +1 -1
- package/esm/commands/sync/push.d.ts +2 -2
- package/esm/commands/sync/push.d.ts.map +1 -1
- package/esm/commands/sync/push.js +2 -5
- package/esm/commands/sync/push.js.map +1 -1
- package/esm/commands/sync/request.d.ts +1 -1
- package/esm/commands/sync/request.d.ts.map +1 -1
- package/esm/commands/sync/request.js +1 -1
- package/esm/commands/sync/request.js.map +1 -1
- package/esm/commands/sync/response.d.ts +4 -9
- package/esm/commands/sync/response.d.ts.map +1 -1
- package/esm/commands/sync/response.js +32 -23
- package/esm/commands/sync/response.js.map +1 -1
- package/esm/commands/sync/stat.d.ts +3 -3
- package/esm/commands/sync/stat.d.ts.map +1 -1
- package/esm/commands/sync/stat.js +13 -30
- package/esm/commands/sync/stat.js.map +1 -1
- package/esm/commands/sync/sync.d.ts +2 -2
- package/esm/commands/sync/sync.d.ts.map +1 -1
- package/esm/commands/sync/sync.js +7 -5
- package/esm/commands/sync/sync.js.map +1 -1
- package/esm/crypto.js +1 -1
- package/esm/crypto.js.map +1 -1
- package/esm/index.d.ts +0 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +0 -1
- package/esm/index.js.map +1 -1
- package/esm/packet.d.ts +1 -1
- package/esm/packet.d.ts.map +1 -1
- package/esm/packet.js +1 -1
- package/esm/packet.js.map +1 -1
- package/esm/socket/dispatcher.d.ts +11 -3
- package/esm/socket/dispatcher.d.ts.map +1 -1
- package/esm/socket/dispatcher.js +19 -11
- package/esm/socket/dispatcher.js.map +1 -1
- package/esm/socket/index.d.ts +1 -1
- package/esm/socket/index.d.ts.map +1 -1
- package/esm/socket/index.js +1 -1
- package/esm/socket/index.js.map +1 -1
- package/esm/socket/socket.d.ts +9 -4
- package/esm/socket/socket.d.ts.map +1 -1
- package/esm/socket/socket.js +20 -10
- package/esm/socket/socket.js.map +1 -1
- package/esm/utils/base64.d.ts +23 -0
- package/esm/utils/base64.d.ts.map +1 -1
- package/esm/utils/base64.js +6 -0
- package/esm/utils/base64.js.map +1 -1
- package/package.json +13 -14
- package/src/adb.ts +285 -273
- package/src/auth.ts +174 -173
- package/src/backend.ts +12 -11
- package/src/commands/base.ts +12 -11
- package/src/commands/framebuffer.ts +78 -73
- package/src/commands/index.ts +8 -8
- package/src/commands/install.ts +32 -30
- package/src/commands/power.ts +54 -54
- package/src/commands/reverse.ts +141 -140
- package/src/commands/subprocess/command.ts +131 -0
- package/src/commands/subprocess/index.ts +3 -139
- package/src/commands/subprocess/protocols/none.ts +73 -68
- package/src/commands/subprocess/protocols/shell.ts +191 -190
- package/src/commands/subprocess/protocols/types.ts +60 -59
- package/src/commands/subprocess/utils.ts +20 -20
- package/src/commands/sync/index.ts +7 -7
- package/src/commands/sync/list.ts +54 -86
- package/src/commands/sync/pull.ts +45 -46
- package/src/commands/sync/push.ts +38 -41
- package/src/commands/sync/request.ts +51 -50
- package/src/commands/sync/response.ts +68 -64
- package/src/commands/sync/stat.ts +129 -150
- package/src/commands/sync/sync.ts +182 -181
- package/src/commands/tcpip.ts +21 -21
- package/src/crypto.ts +296 -296
- package/src/features.ts +9 -9
- package/src/index.ts +10 -11
- package/src/packet.ts +80 -80
- package/src/socket/dispatcher.ts +310 -304
- package/src/socket/index.ts +2 -2
- package/src/socket/socket.ts +156 -144
- package/src/utils/auto-reset-event.ts +41 -41
- package/src/utils/base64.ts +329 -306
- package/src/utils/index.ts +3 -3
- package/tsconfig.build.json +3 -3
- package/tsconfig.build.tsbuildinfo +1 -1
- package/esm/commands/subprocess/legacy.d.ts +0 -35
- package/esm/commands/subprocess/legacy.d.ts.map +0 -1
- package/esm/commands/subprocess/legacy.js +0 -53
- package/esm/commands/subprocess/legacy.js.map +0 -1
- package/esm/commands/subprocess/protocol.d.ts +0 -39
- package/esm/commands/subprocess/protocol.d.ts.map +0 -1
- package/esm/commands/subprocess/protocol.js +0 -157
- package/esm/commands/subprocess/protocol.js.map +0 -1
- package/esm/commands/subprocess/types.d.ts +0 -50
- package/esm/commands/subprocess/types.d.ts.map +0 -1
- package/esm/commands/subprocess/types.js +0 -2
- package/esm/commands/subprocess/types.js.map +0 -1
- package/esm/socket/controller.d.ts +0 -37
- package/esm/socket/controller.d.ts.map +0 -1
- package/esm/socket/controller.js +0 -66
- package/esm/socket/controller.js.map +0 -1
- package/esm/stream/buffered.d.ts +0 -35
- package/esm/stream/buffered.d.ts.map +0 -1
- package/esm/stream/buffered.js +0 -123
- package/esm/stream/buffered.js.map +0 -1
- package/esm/stream/detect.bak.d.ts +0 -240
- package/esm/stream/detect.bak.d.ts.map +0 -1
- package/esm/stream/detect.bak.js +0 -60
- package/esm/stream/detect.bak.js.map +0 -1
- package/esm/stream/detect.d.ts +0 -2
- package/esm/stream/detect.d.ts.map +0 -1
- package/esm/stream/detect.js +0 -5
- package/esm/stream/detect.js.map +0 -1
- package/esm/stream/detect.native.d.ts +0 -240
- package/esm/stream/detect.native.d.ts.map +0 -1
- package/esm/stream/detect.native.js +0 -60
- package/esm/stream/detect.native.js.map +0 -1
- package/esm/stream/detect.polyfill.d.ts +0 -18
- package/esm/stream/detect.polyfill.d.ts.map +0 -1
- package/esm/stream/detect.polyfill.js +0 -9
- package/esm/stream/detect.polyfill.js.map +0 -1
- package/esm/stream/index.d.ts +0 -4
- package/esm/stream/index.d.ts.map +0 -1
- package/esm/stream/index.js +0 -4
- package/esm/stream/index.js.map +0 -1
- package/esm/stream/transform.d.ts +0 -104
- package/esm/stream/transform.d.ts.map +0 -1
- package/esm/stream/transform.js +0 -373
- package/esm/stream/transform.js.map +0 -1
- package/esm/utils/encoding.d.ts +0 -3
- package/esm/utils/encoding.d.ts.map +0 -1
- package/esm/utils/encoding.js +0 -11
- package/esm/utils/encoding.js.map +0 -1
- package/src/stream/buffered.ts +0 -147
- package/src/stream/detect.native.ts +0 -362
- package/src/stream/detect.polyfill.ts +0 -28
- package/src/stream/detect.ts +0 -5
- package/src/stream/index.ts +0 -3
- package/src/stream/transform.ts +0 -447
- package/tsconfig.test.json +0 -10
package/src/utils/base64.ts
CHANGED
|
@@ -1,306 +1,329 @@
|
|
|
1
|
-
// Prepare maps for O(1) searching
|
|
2
|
-
const charToIndex: Record<string, number> = {};
|
|
3
|
-
const indexToChar: number[] = [];
|
|
4
|
-
const paddingChar = '='.charCodeAt(0);
|
|
5
|
-
|
|
6
|
-
function addRange(start: string, end: string) {
|
|
7
|
-
const charCodeStart = start.charCodeAt(0);
|
|
8
|
-
const charCodeEnd = end.charCodeAt(0);
|
|
9
|
-
|
|
10
|
-
for (let charCode = charCodeStart; charCode <= charCodeEnd; charCode += 1) {
|
|
11
|
-
charToIndex[String.fromCharCode(charCode)] = indexToChar.length;
|
|
12
|
-
indexToChar.push(charCode);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
addRange('A', 'Z');
|
|
17
|
-
addRange('a', 'z');
|
|
18
|
-
addRange('0', '9');
|
|
19
|
-
addRange('+', '+');
|
|
20
|
-
addRange('/', '/');
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
export function
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
outputIndex
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
outputIndex
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
/* cspell: disable-next-line */
|
|
177
|
-
//
|
|
178
|
-
const
|
|
179
|
-
inputIndex
|
|
180
|
-
|
|
181
|
-
output[outputIndex] =
|
|
182
|
-
outputIndex
|
|
183
|
-
|
|
184
|
-
output[outputIndex] =
|
|
185
|
-
outputIndex
|
|
186
|
-
|
|
187
|
-
output[outputIndex] = indexToChar[((
|
|
188
|
-
outputIndex
|
|
189
|
-
|
|
190
|
-
output[outputIndex] =
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
outputIndex
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
outputIndex
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
outputIndex
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
outputIndex
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
output[outputIndex] = indexToChar[
|
|
233
|
-
outputIndex -= 1;
|
|
234
|
-
|
|
235
|
-
output[outputIndex] = indexToChar[
|
|
236
|
-
outputIndex -= 1;
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
const a = charToIndex[input[sIndex]!]!;
|
|
285
|
-
sIndex += 1;
|
|
286
|
-
|
|
287
|
-
const b = charToIndex[input[sIndex]!]!;
|
|
288
|
-
sIndex += 1;
|
|
289
|
-
|
|
290
|
-
const c = charToIndex[input[sIndex]!]!;
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
result[dIndex] = (
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
1
|
+
// Prepare maps for O(1) searching
|
|
2
|
+
const charToIndex: Record<string, number> = {};
|
|
3
|
+
const indexToChar: number[] = [];
|
|
4
|
+
const paddingChar = '='.charCodeAt(0);
|
|
5
|
+
|
|
6
|
+
function addRange(start: string, end: string) {
|
|
7
|
+
const charCodeStart = start.charCodeAt(0);
|
|
8
|
+
const charCodeEnd = end.charCodeAt(0);
|
|
9
|
+
|
|
10
|
+
for (let charCode = charCodeStart; charCode <= charCodeEnd; charCode += 1) {
|
|
11
|
+
charToIndex[String.fromCharCode(charCode)] = indexToChar.length;
|
|
12
|
+
indexToChar.push(charCode);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
addRange('A', 'Z');
|
|
17
|
+
addRange('a', 'z');
|
|
18
|
+
addRange('0', '9');
|
|
19
|
+
addRange('+', '+');
|
|
20
|
+
addRange('/', '/');
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Calculate the required length of the output buffer for the given input length.
|
|
24
|
+
*
|
|
25
|
+
* @param inputLength Length of the input in bytes
|
|
26
|
+
* @returns Length of the output in bytes
|
|
27
|
+
*/
|
|
28
|
+
export function calculateBase64EncodedLength(inputLength: number): [outputLength: number, paddingLength: number] {
|
|
29
|
+
const remainder = inputLength % 3;
|
|
30
|
+
const paddingLength = remainder !== 0 ? 3 - remainder : 0;
|
|
31
|
+
return [(inputLength + paddingLength) / 3 * 4, paddingLength];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Encode the given input buffer into base64.
|
|
36
|
+
*
|
|
37
|
+
* @param input The input buffer
|
|
38
|
+
* @returns The encoded output buffer
|
|
39
|
+
*/
|
|
40
|
+
export function encodeBase64(
|
|
41
|
+
input: Uint8Array,
|
|
42
|
+
): Uint8Array;
|
|
43
|
+
/**
|
|
44
|
+
* Encode the given input into base64 and write it to the output buffer.
|
|
45
|
+
*
|
|
46
|
+
* The output buffer must be at least as long as the value returned by `calculateBase64EncodedLength`.
|
|
47
|
+
* It can points to the same buffer as the input, as long as `output.offset <= input.offset - input.length / 3`,
|
|
48
|
+
* or `output.offset >= input.offset - 1`
|
|
49
|
+
*
|
|
50
|
+
* @param input The input buffer
|
|
51
|
+
* @param output The output buffer
|
|
52
|
+
* @returns The number of bytes written to the output buffer
|
|
53
|
+
*/
|
|
54
|
+
export function encodeBase64(
|
|
55
|
+
input: Uint8Array,
|
|
56
|
+
output: Uint8Array,
|
|
57
|
+
): number;
|
|
58
|
+
export function encodeBase64(
|
|
59
|
+
input: Uint8Array,
|
|
60
|
+
output?: Uint8Array,
|
|
61
|
+
): Uint8Array | number {
|
|
62
|
+
const [outputLength, paddingLength] = calculateBase64EncodedLength(input.length);
|
|
63
|
+
|
|
64
|
+
if (!output) {
|
|
65
|
+
output = new Uint8Array(outputLength);
|
|
66
|
+
encodeForward(input, output, paddingLength);
|
|
67
|
+
return output;
|
|
68
|
+
} else {
|
|
69
|
+
if (output.length < outputLength) {
|
|
70
|
+
throw new Error('output buffer is too small');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
output = output.subarray(0, outputLength);
|
|
74
|
+
|
|
75
|
+
// When input and output are on same ArrayBuffer,
|
|
76
|
+
// we check if it's possible to encode in-place.
|
|
77
|
+
if (input.buffer !== output.buffer) {
|
|
78
|
+
encodeForward(input, output, paddingLength);
|
|
79
|
+
} else if (output.byteOffset + output.length - (paddingLength + 1) <= input.byteOffset + input.length) {
|
|
80
|
+
// Output ends before input ends
|
|
81
|
+
// So output won't catch up with input.
|
|
82
|
+
|
|
83
|
+
// Depends on padding length,
|
|
84
|
+
// it's possible to write 1-3 bytes after input ends.
|
|
85
|
+
// spell: disable-next-line
|
|
86
|
+
// | aaaaaabb | | | |
|
|
87
|
+
// | aaaaaa | bb0000 | = | = |
|
|
88
|
+
//
|
|
89
|
+
// spell: disable-next-line
|
|
90
|
+
// | aaaaaabb | bbbbcccc | | |
|
|
91
|
+
// | aaaaaa | bbbbbb | cccc00 | = |
|
|
92
|
+
//
|
|
93
|
+
// spell: disable-next-line
|
|
94
|
+
// | aaaaaabb | bbbbcccc | ccdddddd | |
|
|
95
|
+
// | aaaaaa | bbbbbb | cccccc | dddddd |
|
|
96
|
+
|
|
97
|
+
// Must encode forwards.
|
|
98
|
+
encodeForward(input, output, paddingLength);
|
|
99
|
+
} else if (output.byteOffset >= input.byteOffset - 1) {
|
|
100
|
+
// Output starts after input starts
|
|
101
|
+
// So in backwards, output can't catch up with input.
|
|
102
|
+
|
|
103
|
+
// Because first 3 bytes becomes 4 bytes,
|
|
104
|
+
// it's possible to write 1 byte before input starts.
|
|
105
|
+
// spell: disable-next-line
|
|
106
|
+
// | | aaaaaabb | bbbbcccc | ccdddddd |
|
|
107
|
+
// | aaaaaa | bbbbbb | cccccc | dddddd |
|
|
108
|
+
|
|
109
|
+
// Must encode backwards.
|
|
110
|
+
encodeBackward(input, output, paddingLength);
|
|
111
|
+
} else {
|
|
112
|
+
// Input is in the middle of output,
|
|
113
|
+
// not possible to read neither first or last three bytes,
|
|
114
|
+
throw new Error('input and output cannot overlap');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return outputLength;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function encodeForward(input: Uint8Array, output: Uint8Array, paddingLength: number) {
|
|
122
|
+
let inputIndex = 0;
|
|
123
|
+
let outputIndex = 0;
|
|
124
|
+
|
|
125
|
+
while (inputIndex < input.length - 2) {
|
|
126
|
+
/* cspell: disable-next-line */
|
|
127
|
+
// aaaaaabb
|
|
128
|
+
const x = input[inputIndex]!;
|
|
129
|
+
inputIndex += 1;
|
|
130
|
+
|
|
131
|
+
/* cspell: disable-next-line */
|
|
132
|
+
// bbbbcccc
|
|
133
|
+
const y = input[inputIndex]!;
|
|
134
|
+
inputIndex += 1;
|
|
135
|
+
|
|
136
|
+
/* cspell: disable-next-line */
|
|
137
|
+
// ccdddddd
|
|
138
|
+
const z = input[inputIndex]!;
|
|
139
|
+
inputIndex += 1;
|
|
140
|
+
|
|
141
|
+
output[outputIndex] = indexToChar[x >> 2]!;
|
|
142
|
+
outputIndex += 1;
|
|
143
|
+
|
|
144
|
+
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
|
|
145
|
+
outputIndex += 1;
|
|
146
|
+
|
|
147
|
+
output[outputIndex] = indexToChar[((y & 0b1111) << 2) | (z >> 6)]!;
|
|
148
|
+
outputIndex += 1;
|
|
149
|
+
|
|
150
|
+
output[outputIndex] = indexToChar[z & 0b111111]!;
|
|
151
|
+
outputIndex += 1;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (paddingLength === 2) {
|
|
155
|
+
/* cspell: disable-next-line */
|
|
156
|
+
// aaaaaabb
|
|
157
|
+
const x = input[inputIndex]!;
|
|
158
|
+
inputIndex += 1;
|
|
159
|
+
|
|
160
|
+
output[outputIndex] = indexToChar[x >> 2]!;
|
|
161
|
+
outputIndex += 1;
|
|
162
|
+
|
|
163
|
+
output[outputIndex] = indexToChar[((x & 0b11) << 4)]!;
|
|
164
|
+
outputIndex += 1;
|
|
165
|
+
|
|
166
|
+
output[outputIndex] = paddingChar;
|
|
167
|
+
outputIndex += 1;
|
|
168
|
+
|
|
169
|
+
output[outputIndex] = paddingChar;
|
|
170
|
+
} else if (paddingLength === 1) {
|
|
171
|
+
/* cspell: disable-next-line */
|
|
172
|
+
// aaaaaabb
|
|
173
|
+
const x = input[inputIndex]!;
|
|
174
|
+
inputIndex += 1;
|
|
175
|
+
|
|
176
|
+
/* cspell: disable-next-line */
|
|
177
|
+
// bbbbcccc
|
|
178
|
+
const y = input[inputIndex]!;
|
|
179
|
+
inputIndex += 1;
|
|
180
|
+
|
|
181
|
+
output[outputIndex] = indexToChar[x >> 2]!;
|
|
182
|
+
outputIndex += 1;
|
|
183
|
+
|
|
184
|
+
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
|
|
185
|
+
outputIndex += 1;
|
|
186
|
+
|
|
187
|
+
output[outputIndex] = indexToChar[((y & 0b1111) << 2)]!;
|
|
188
|
+
outputIndex += 1;
|
|
189
|
+
|
|
190
|
+
output[outputIndex] = paddingChar;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function encodeBackward(input: Uint8Array, output: Uint8Array, paddingLength: number) {
|
|
195
|
+
let inputIndex = input.length - 1;
|
|
196
|
+
let outputIndex = output.length - 1;
|
|
197
|
+
|
|
198
|
+
if (paddingLength === 2) {
|
|
199
|
+
/* cspell: disable-next-line */
|
|
200
|
+
// aaaaaabb
|
|
201
|
+
const x = input[inputIndex]!;
|
|
202
|
+
inputIndex -= 1;
|
|
203
|
+
|
|
204
|
+
output[outputIndex] = paddingChar;
|
|
205
|
+
outputIndex -= 1;
|
|
206
|
+
|
|
207
|
+
output[outputIndex] = paddingChar;
|
|
208
|
+
outputIndex -= 1;
|
|
209
|
+
|
|
210
|
+
output[outputIndex] = indexToChar[((x & 0b11) << 4)]!;
|
|
211
|
+
outputIndex -= 1;
|
|
212
|
+
|
|
213
|
+
output[outputIndex] = indexToChar[x >> 2]!;
|
|
214
|
+
outputIndex -= 1;
|
|
215
|
+
} else if (paddingLength === 1) {
|
|
216
|
+
/* cspell: disable-next-line */
|
|
217
|
+
// bbbbcccc
|
|
218
|
+
const y = input[inputIndex]!;
|
|
219
|
+
inputIndex -= 1;
|
|
220
|
+
|
|
221
|
+
/* cspell: disable-next-line */
|
|
222
|
+
// aaaaaabb
|
|
223
|
+
const x = input[inputIndex]!;
|
|
224
|
+
inputIndex -= 1;
|
|
225
|
+
|
|
226
|
+
output[outputIndex] = paddingChar;
|
|
227
|
+
outputIndex -= 1;
|
|
228
|
+
|
|
229
|
+
output[outputIndex] = indexToChar[((y & 0b1111) << 2)]!;
|
|
230
|
+
outputIndex -= 1;
|
|
231
|
+
|
|
232
|
+
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
|
|
233
|
+
outputIndex -= 1;
|
|
234
|
+
|
|
235
|
+
output[outputIndex] = indexToChar[x >> 2]!;
|
|
236
|
+
outputIndex -= 1;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
while (inputIndex >= 0) {
|
|
240
|
+
/* cspell: disable-next-line */
|
|
241
|
+
// ccdddddd
|
|
242
|
+
const z = input[inputIndex]!;
|
|
243
|
+
inputIndex -= 1;
|
|
244
|
+
|
|
245
|
+
/* cspell: disable-next-line */
|
|
246
|
+
// bbbbcccc
|
|
247
|
+
const y = input[inputIndex]!;
|
|
248
|
+
inputIndex -= 1;
|
|
249
|
+
|
|
250
|
+
/* cspell: disable-next-line */
|
|
251
|
+
// aaaaaabb
|
|
252
|
+
const x = input[inputIndex]!;
|
|
253
|
+
inputIndex -= 1;
|
|
254
|
+
|
|
255
|
+
output[outputIndex] = indexToChar[z & 0b111111]!;
|
|
256
|
+
outputIndex -= 1;
|
|
257
|
+
|
|
258
|
+
output[outputIndex] = indexToChar[((y & 0b1111) << 2) | (z >> 6)]!;
|
|
259
|
+
outputIndex -= 1;
|
|
260
|
+
|
|
261
|
+
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
|
|
262
|
+
outputIndex -= 1;
|
|
263
|
+
|
|
264
|
+
output[outputIndex] = indexToChar[x >> 2]!;
|
|
265
|
+
outputIndex -= 1;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function decodeBase64(input: string): Uint8Array {
|
|
270
|
+
let padding: number;
|
|
271
|
+
if (input[input.length - 2] === '=') {
|
|
272
|
+
padding = 2;
|
|
273
|
+
} else if (input[input.length - 1] === '=') {
|
|
274
|
+
padding = 1;
|
|
275
|
+
} else {
|
|
276
|
+
padding = 0;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const result = new Uint8Array(input.length / 4 * 3 - padding);
|
|
280
|
+
let sIndex = 0;
|
|
281
|
+
let dIndex = 0;
|
|
282
|
+
|
|
283
|
+
while (sIndex < input.length - (padding !== 0 ? 4 : 0)) {
|
|
284
|
+
const a = charToIndex[input[sIndex]!]!;
|
|
285
|
+
sIndex += 1;
|
|
286
|
+
|
|
287
|
+
const b = charToIndex[input[sIndex]!]!;
|
|
288
|
+
sIndex += 1;
|
|
289
|
+
|
|
290
|
+
const c = charToIndex[input[sIndex]!]!;
|
|
291
|
+
sIndex += 1;
|
|
292
|
+
|
|
293
|
+
const d = charToIndex[input[sIndex]!]!;
|
|
294
|
+
sIndex += 1;
|
|
295
|
+
|
|
296
|
+
result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
|
|
297
|
+
dIndex += 1;
|
|
298
|
+
|
|
299
|
+
result[dIndex] = ((b & 0b1111) << 4) | ((c & 0b11_1100) >> 2);
|
|
300
|
+
dIndex += 1;
|
|
301
|
+
|
|
302
|
+
result[dIndex] = ((c & 0b11) << 6) | d;
|
|
303
|
+
dIndex += 1;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (padding === 1) {
|
|
307
|
+
const a = charToIndex[input[sIndex]!]!;
|
|
308
|
+
sIndex += 1;
|
|
309
|
+
|
|
310
|
+
const b = charToIndex[input[sIndex]!]!;
|
|
311
|
+
sIndex += 1;
|
|
312
|
+
|
|
313
|
+
const c = charToIndex[input[sIndex]!]!;
|
|
314
|
+
|
|
315
|
+
result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
|
|
316
|
+
dIndex += 1;
|
|
317
|
+
|
|
318
|
+
result[dIndex] = ((b & 0b1111) << 4) | ((c & 0b11_1100) >> 2);
|
|
319
|
+
} else if (padding === 2) {
|
|
320
|
+
const a = charToIndex[input[sIndex]!]!;
|
|
321
|
+
sIndex += 1;
|
|
322
|
+
|
|
323
|
+
const b = charToIndex[input[sIndex]!]!;
|
|
324
|
+
|
|
325
|
+
result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return result;
|
|
329
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { decodeUtf8, encodeUtf8 } from '@yume-chan/struct';
|
|
2
|
-
export * from './auto-reset-event.js';
|
|
3
|
-
export * from './base64.js';
|
|
1
|
+
export { decodeUtf8, encodeUtf8 } from '@yume-chan/struct';
|
|
2
|
+
export * from './auto-reset-event.js';
|
|
3
|
+
export * from './base64.js';
|
package/tsconfig.build.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./node_modules/@yume-chan/ts-package-builder/tsconfig.base.json"
|
|
3
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"extends": "./node_modules/@yume-chan/ts-package-builder/tsconfig.base.json"
|
|
3
|
+
}
|