blob: 9ef9797e81d3e47b38381d9061193665e81cf3f5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
export class DataFormatError extends Error {};
/*
* TODO: remove once node 24 is EOL (2028-05-01?)
* node <25 doesn't have Uint8Array.fromHex, so we use Buffer.from(_, "hex") instead
*/
type fromHex = (s: string) => Uint8Array | Buffer;
const fromHex: fromHex = "fromHex" in Uint8Array
? Uint8Array.fromHex as fromHex
: (s: string) => Buffer.from(s, "hex");
export function loadHex(source: string, target: Uint8Array) {
for (const line of source.split('\n')) {
if (line[0] !== ":") continue;
if (line.length < 11) throw new DataFormatError("line too short");
const data = fromHex(line.slice(1).trimEnd());
if (data[3]) return new DataFormatError("unexpected data[3] !== 0");
const n = data[0];
const addr = (data[1] << 8) | data[2];
if (addr + n > target.length) new DataFormatError("target address out of bounds");
if (n + 4 !== data.length) new DataFormatError("inconsistent data length");
target.set(data.subarray(4), addr);
}
}
|