summaryrefslogtreecommitdiff
path: root/src/util.ts
diff options
context:
space:
mode:
authorApexo2026-03-28 23:40:53 +0100
committerApexo2026-03-28 23:40:53 +0100
commit1b194ac4578dea8e71b0d61d1cb4d875f435ba71 (patch)
tree786019a0c6f34b458f3272bf2ecbde0de1976e0a /src/util.ts
downloadanduril-sim-1b194ac4578dea8e71b0d61d1cb4d875f435ba71.tar.gz
anduril-sim-1b194ac4578dea8e71b0d61d1cb4d875f435ba71.tar.bz2
anduril-sim-1b194ac4578dea8e71b0d61d1cb4d875f435ba71.zip
D3AA simulator
Diffstat (limited to 'src/util.ts')
-rw-r--r--src/util.ts24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/util.ts b/src/util.ts
new file mode 100644
index 0000000..9ef9797
--- /dev/null
+++ b/src/util.ts
@@ -0,0 +1,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);
+ }
+}