-
Notifications
You must be signed in to change notification settings - Fork 53
/
bytes.ts
128 lines (104 loc) · 3.51 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
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
import { BytesLike } from "./base";
import { assertHexString, assertUtf8String } from "./utils";
export function bytifyRawString(rawString: string): Uint8Array {
assertUtf8String(rawString);
const buffer = new ArrayBuffer(rawString.length);
const view = new DataView(buffer);
for (let i = 0; i < rawString.length; i++) {
const c = rawString.charCodeAt(i);
view.setUint8(i, c);
}
return new Uint8Array(buffer);
}
const CHAR_0 = "0".charCodeAt(0); // 48
const CHAR_9 = "9".charCodeAt(0); // 57
const CHAR_A = "A".charCodeAt(0); // 65
const CHAR_F = "F".charCodeAt(0); // 70
const CHAR_a = "a".charCodeAt(0); // 97
// const CHAR_f = "f".charCodeAt(0); // 102
function bytifyHex(hex: string): Uint8Array {
assertHexString(hex);
const u8a = Uint8Array.from({ length: hex.length / 2 - 1 });
for (let i = 2, j = 0; i < hex.length; i = i + 2, j++) {
const c1 = hex.charCodeAt(i);
const c2 = hex.charCodeAt(i + 1);
// prettier-ignore
const n1 = c1 <= CHAR_9 ? c1 - CHAR_0 : c1 <= CHAR_F ? c1 - CHAR_A + 10 : c1 - CHAR_a + 10
// prettier-ignore
const n2 = c2 <= CHAR_9 ? c2 - CHAR_0 : c2 <= CHAR_F ? c2 - CHAR_A + 10 : c2 - CHAR_a + 10
u8a[j] = (n1 << 4) | n2;
}
return u8a;
}
function bytifyArrayLike(xs: ArrayLike<number>): Uint8Array {
for (let i = 0; i < xs.length; i++) {
const v = xs[i];
if (v < 0 || v > 255 || !Number.isInteger(v)) {
throw new Error("invalid ArrayLike, all elements must be 0-255");
}
}
return Uint8Array.from(xs);
}
/**
* convert a {@link BytesLike} to an Uint8Array
* @param bytesLike
*/
export function bytify(bytesLike: BytesLike): Uint8Array {
if (bytesLike instanceof ArrayBuffer) return new Uint8Array(bytesLike);
if (bytesLike instanceof Uint8Array) return Uint8Array.from(bytesLike);
if (typeof bytesLike === "string") return bytifyHex(bytesLike);
if (Array.isArray(bytesLike)) return bytifyArrayLike(bytesLike);
throw new Error(`Cannot convert ${bytesLike}`);
}
export function equal(a: BytesLike, b: BytesLike): boolean {
const aUint8Array = bytify(a);
const bUint8Array = bytify(b);
return equalUint8Array(aUint8Array, bUint8Array);
}
function equalUint8Array(a: Uint8Array, b: Uint8Array): boolean {
if (a.length !== b.length) return false;
for (let i = a.length; -1 < i; i -= 1) {
if (a[i] !== b[i]) return false;
}
return true;
}
const HEX_CACHE = Array.from({ length: 256 }).map((_, i) =>
i.toString(16).padStart(2, "0")
);
/**
* convert a {@link BytesLike} to an even length hex string prefixed with "0x"
* @param buf
* @example
* hexify([0,1,2,3]) // "0x010203"
* hexify(Buffer.from([1, 2, 3])) // "0x010203"
*/
export function hexify(buf: BytesLike): string {
let hex = "";
const u8a = bytify(buf);
for (let i = 0; i < u8a.length; i++) {
hex += HEX_CACHE[u8a[i]];
}
return "0x" + hex;
}
export function concat(...bytesLikes: BytesLike[]): Uint8Array {
const unmerged = bytesLikes.map(bytify);
const totalSize = unmerged.reduce((size, item) => size + item.length, 0);
const merged = new Uint8Array(totalSize);
let offset = 0;
unmerged.forEach((item) => {
merged.set(item, offset);
offset += item.length;
});
return merged;
}
// export function split(bytes: BytesLike, points: number[]): Uint8Array[] {
// const u8vec = bytify(bytes);
// const result: Uint8Array[] = [];
// let offset = 0;
// for (const point of points) {
// result.push(u8vec.slice(offset, offset + point));
// offset += point;
// }
// result.push(u8vec.slice(offset));
// return result;
// }