/** * Ways to find a site. Each method returns every RVA it matched; it never picks a winner among * several matches, the caller requires exactly one. * * signature a byte pattern with wildcards in a section * string_ref the code that loads the address of a given string * within a byte pattern inside the function of an already located site */ import { DefinitionError } from '../errors.ts'; import type { PeImage } from '../pe-image.ts'; import * as signature from './signature.ts'; import * as stringRef from './string-ref.ts'; import * as within from './within.ts'; /** Sites located before this one, by name, for methods that search relative to them. */ export interface LocateContext { readonly sites: ReadonlyMap; } export const NO_CONTEXT: LocateContext = { sites: new Map() }; export interface LocateMethod { /** Keys of a locate block this method reads, besides "method" and "captures". */ readonly keys: readonly string[]; locate(image: PeImage, spec: Readonly>, context: LocateContext): number[]; /** Number of bytes from the site that the method checked; captures must lie inside them. */ length(spec: Readonly>): number; } const METHODS: ReadonlyMap = new Map([ [signature.METHOD, { keys: signature.KEYS, locate: signature.locateSignature, length: signature.signatureLength }], [stringRef.METHOD, { keys: stringRef.KEYS, locate: stringRef.locateStringReference, length: stringRef.stringReferenceLength }], [within.METHOD, { keys: within.KEYS, locate: within.locateWithin, length: within.withinLength }], ]); export function locateMethod(method: unknown): LocateMethod { const found = typeof method === 'string' ? METHODS.get(method) : undefined; if (!found) throw new DefinitionError(`unknown locate method "${String(method)}"; known: ${[...METHODS.keys()].join(', ')}`); return found; }