-
Notifications
You must be signed in to change notification settings - Fork 0
/
_bytes.ts
99 lines (89 loc) · 2.15 KB
/
_bytes.ts
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
// Copyright (C) 2020-2022 Russell Clarey. All rights reserved. MIT license.
export { equals } from "https://deno.land/[email protected]/bytes/mod.ts#^";
export async function readN(
reader: Deno.Reader,
n: number,
arr?: Uint8Array,
): Promise<Uint8Array> {
const out = arr ?? new Uint8Array(n);
let nRead = 0;
while (nRead < n) {
const m = await reader.read(out.subarray(nRead));
if (m === null) {
throw new Deno.errors.UnexpectedEof(
`reached EOF but we expected to read ${n - nRead} more bytes`,
);
}
nRead += m;
}
return out;
}
export function readInt(
arr: Uint8Array,
nBytes: number,
offset: number,
): number {
let n = 0;
for (const byte of arr.subarray(offset, offset + nBytes)) {
n <<= 8;
n += byte;
}
return n;
}
export function writeInt(
n: number,
arr: Uint8Array,
nBytes: number,
offset: number,
): void {
if (nBytes + offset > arr.length) {
throw new Error(
`attempt to write ${nBytes} bytes with offset ${offset}, but array only has length ${arr.length}`,
);
}
let remaining = n;
let ind = offset + nBytes - 1;
while (ind >= offset) {
const byte = remaining % 256;
remaining = (remaining / 256) | 0;
arr[ind] = byte;
ind -= 1;
}
}
export function decodeBinaryData(s: string): Uint8Array {
const hash: number[] = [];
for (let i = 0; i < s.length;) {
if (s[i] === "%") {
hash.push(parseInt(s.slice(i + 1, i + 3), 16));
i += 3;
} else {
hash.push(s.charCodeAt(i));
i += 1;
}
}
return Uint8Array.from(hash);
}
export function encodeBinaryData(arr: Uint8Array): string {
let str = "";
for (const byte of arr) {
if (
(byte > 44 && byte < 58 && byte !== 47) ||
(byte > 64 && byte < 91) ||
byte === 95 ||
(byte > 96 && byte < 123) ||
byte === 126
) {
str += String.fromCharCode(byte);
} else {
str += `%${byte.toString(16)}`;
}
}
return str;
}
export function partition(arr: Uint8Array, n: number): Uint8Array[] {
const slices: Uint8Array[] = [];
for (let i = 0; i < arr.length; i += n) {
slices.push(arr.subarray(i, i + n));
}
return slices;
}