Files
WT-Offline/packages/exepatch/src/apply.ts
T
N0VAN0M4LY 5421268fbf Build a loopback client copy with TypeScript
Add the shared BLK, VROMFS, executable patch, and build packages. Preserve
typed fields and shared name tables when reading BLK data. Rewrite the
service endpoints in place so the original environment names remain
available to the client, then build the client copy.
2026-11-24 00:35:13 +08:00

22 lines
1.0 KiB
TypeScript

/**
* Write a verified plan into a copy of the executable's bytes. The expected original bytes
* are checked again right before each write, so a plan made against other bytes is refused
* instead of landing half applied. The input is never modified.
*/
import { VerifyError } from './errors.ts';
import type { PlannedWrite } from './plan.ts';
export function applyWrites(data: Uint8Array, writes: readonly PlannedWrite[]): Uint8Array {
const patched = Uint8Array.from(data);
for (const write of [...writes].sort((left, right) => left.fileOffset - right.fileOffset)) {
const end = write.fileOffset + write.payload.length;
const actual = Buffer.from(patched.subarray(write.fileOffset, end));
if (!actual.equals(write.expectOriginal)) {
throw new VerifyError(`${write.patchId}: bytes at file offset 0x${write.fileOffset.toString(16)} are ${actual.toString('hex')}, ` +
`expected ${Buffer.from(write.expectOriginal).toString('hex')}; nothing was written`);
}
patched.set(write.payload, write.fileOffset);
}
return patched;
}