feat(patcher): 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.
This commit is contained in:
1 parent
89aa5198c3
commit
c3914ec0d6
44 files changed
+1986
-1
No files matched your search
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Ways to find a patch site. Each method returns every RVA it matched; it never picks a
|
||||
* winner among several matches, the caller requires exactly one.
|
||||
*/
|
||||
import { DefinitionError } from '../errors.ts';
|
||||
import type { PeImage } from '../pe-image.ts';
|
||||
import * as signature from './signature.ts';
|
||||
|
||||
export type Locator = (image: PeImage, spec: Readonly<Record<string, unknown>>) => number[];
|
||||
|
||||
const METHODS: ReadonlyMap<string, Locator> = new Map([[signature.METHOD, signature.locateSignature]]);
|
||||
|
||||
export function locatorFor(method: string): Locator {
|
||||
const locator = METHODS.get(method);
|
||||
if (!locator) throw new DefinitionError(`unknown locate method "${method}"; known: ${[...METHODS.keys()].join(', ')}`);
|
||||
return locator;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Find a patch site by a byte pattern with wildcards, e.g. "41 57 41 56 48 81 ec ?? ?? ?? ??".
|
||||
*
|
||||
* Wildcards cover what legitimately differs between builds: addresses inside rip-relative
|
||||
* operands and the targets of relative calls and jumps. Patterns are produced on the
|
||||
* development side (tools/, which has a disassembler) and stored in the definition.
|
||||
*
|
||||
* The search uses the longest run of fixed bytes as an anchor, then checks the whole pattern
|
||||
* at each anchor match.
|
||||
*/
|
||||
import { DefinitionError } from '../errors.ts';
|
||||
import type { PeImage } from '../pe-image.ts';
|
||||
|
||||
export const METHOD = 'signature';
|
||||
const WILDCARD = '??';
|
||||
const DEFAULT_LIMIT = 8;
|
||||
|
||||
type Token = number | null;
|
||||
|
||||
export function locateSignature(image: PeImage, spec: Readonly<Record<string, unknown>>): number[] {
|
||||
if (typeof spec.pattern !== 'string') throw new DefinitionError('signature locate needs a string "pattern"');
|
||||
const sectionName = typeof spec.section === 'string' ? spec.section : '.text';
|
||||
const limit = typeof spec.limit === 'number' ? spec.limit : DEFAULT_LIMIT;
|
||||
|
||||
const tokens = parsePattern(spec.pattern);
|
||||
const anchor = longestFixedRun(tokens);
|
||||
const data = image.sectionBytes(sectionName);
|
||||
const base = image.section(sectionName).rva;
|
||||
|
||||
const haystack = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
||||
const hits: number[] = [];
|
||||
for (let from = 0; hits.length < limit;) {
|
||||
const found = haystack.indexOf(anchor.bytes, from);
|
||||
if (found < 0) break;
|
||||
const start = found - anchor.position;
|
||||
if (start >= 0 && matchesAt(data, start, tokens)) hits.push(base + start);
|
||||
from = found + 1;
|
||||
}
|
||||
return hits;
|
||||
}
|
||||
|
||||
function parsePattern(pattern: string): Token[] {
|
||||
const parts = pattern.split(/\s+/).filter(Boolean);
|
||||
if (!parts.length) throw new DefinitionError('signature pattern is empty');
|
||||
return parts.map((part) => {
|
||||
if (part === WILDCARD) return null;
|
||||
if (!/^[0-9a-fA-F]{2}$/.test(part)) throw new DefinitionError(`pattern token must be a hex byte or ${WILDCARD}, got "${part}"`);
|
||||
return Number.parseInt(part, 16);
|
||||
});
|
||||
}
|
||||
|
||||
function longestFixedRun(tokens: readonly Token[]): { position: number; bytes: Buffer } {
|
||||
let best = { position: 0, length: 0 };
|
||||
let runStart = 0;
|
||||
tokens.forEach((token, index) => {
|
||||
if (token === null) {
|
||||
runStart = index + 1;
|
||||
return;
|
||||
}
|
||||
if (index + 1 - runStart > best.length) best = { position: runStart, length: index + 1 - runStart };
|
||||
});
|
||||
if (!best.length) throw new DefinitionError('signature pattern has no fixed bytes');
|
||||
return { position: best.position, bytes: Buffer.from(tokens.slice(best.position, best.position + best.length) as number[]) };
|
||||
}
|
||||
|
||||
function matchesAt(data: Uint8Array, start: number, tokens: readonly Token[]): boolean {
|
||||
if (start + tokens.length > data.length) return false;
|
||||
return tokens.every((token, index) => token === null || data[start + index] === token);
|
||||
}
|
||||
Reference in new issue
Block a user