mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 09:34:20 +00:00
inspect-python-pyc and inspect-erlang-beam took their max-* limits through Number(), so 0x10 meant 16 and 1e2 meant 100. Admit decimal text only through cliDecimalField, which now keeps the shared field's description for help.
117 lines
4.1 KiB
TypeScript
117 lines
4.1 KiB
TypeScript
import { z } from "incur";
|
|
|
|
import {
|
|
executableFormatHintSchema,
|
|
type ExecutableFormatHint,
|
|
} from "../domain/dosCom.js";
|
|
import type { BinaryArchitecture } from "../domain/binaryTargetTypes.js";
|
|
import {
|
|
binaryArchitectureSchema,
|
|
rawBinaryEntryPointSchema,
|
|
} from "../domain/rawBinary.js";
|
|
import { analysisProviderSelectorSchema } from "../domain/providerSelection.js";
|
|
import type { AnalysisProviderSelector } from "../domain/providerSelection.js";
|
|
import type { Logger } from "pino";
|
|
|
|
/** Parse decimal integer tokens before Number coercion can erase their syntax. */
|
|
export const cliIntegerArgument = (min: number) =>
|
|
z
|
|
.string()
|
|
.regex(/^[0-9]+$/u, "Expected a decimal integer")
|
|
.pipe(z.coerce.number<string>().safe().min(min));
|
|
|
|
/** Parse a decimal token, then let the canonical numeric field own bounds, defaults and help. */
|
|
export function cliDecimalField(
|
|
field: z.ZodDefault<z.ZodNumber>,
|
|
): z.ZodType<number>;
|
|
export function cliDecimalField(
|
|
field: z.ZodOptional<z.ZodNumber>,
|
|
): z.ZodType<number | undefined>;
|
|
export function cliDecimalField(
|
|
field: z.ZodDefault<z.ZodNumber> | z.ZodOptional<z.ZodNumber>,
|
|
): z.ZodType<number | undefined> {
|
|
const parsed = z
|
|
.string()
|
|
.regex(/^[0-9]+$/u, "Expected a decimal integer")
|
|
.pipe(z.coerce.number<string>())
|
|
.pipe(field.unwrap());
|
|
const omitted = field.parse(undefined);
|
|
const admitted =
|
|
omitted === undefined ? parsed.optional() : parsed.default(omitted);
|
|
return field.description === undefined
|
|
? admitted
|
|
: admitted.describe(field.description);
|
|
}
|
|
|
|
export const providerSelectionOption = analysisProviderSelectorSchema
|
|
.optional()
|
|
.describe(
|
|
"Bind deep analysis to a provider ID or use deterministic auto selection",
|
|
);
|
|
|
|
/** Optional path to the local analysis snapshot loaded or updated by a command. */
|
|
export const snapshotSelectionOption = z
|
|
.string()
|
|
.min(1)
|
|
.optional()
|
|
.describe("Load and update a local analysis snapshot");
|
|
|
|
/**
|
|
* Explicit interpretation for headerless native executable formats. Flash
|
|
* dumps take their offsets on the artifact commands instead.
|
|
*/
|
|
export const formatSelectionOption = executableFormatHintSchema
|
|
.exclude(["esp-flash-image"])
|
|
.optional()
|
|
.describe(
|
|
"Explicit interpretation: dos-com or raw-binary (headerless), or gamecube-dol (GameCube/Wii DOL, never auto-detected); omission preserves header-based detection",
|
|
);
|
|
|
|
export const architectureSelectionOption = binaryArchitectureSchema
|
|
.optional()
|
|
.describe(
|
|
"Caller-supplied CPU family; required with --target-format raw-binary, must be omitted with gamecube-dol",
|
|
);
|
|
|
|
export const entryPointSelectionOption = rawBinaryEntryPointSchema
|
|
.optional()
|
|
.describe(
|
|
"Caller-supplied load and entry address (0xhex); required with --target-format raw-binary, optional with gamecube-dol (must equal the DOL header entry)",
|
|
);
|
|
|
|
/** Shared CLI flags that bind a native target interpretation. */
|
|
export const nativeOpenOptionFields = {
|
|
"target-format": formatSelectionOption,
|
|
architecture: architectureSelectionOption,
|
|
"entry-point": entryPointSelectionOption,
|
|
provider: providerSelectionOption,
|
|
};
|
|
|
|
/** Native CLI flags consumed when opening a target for one-shot analysis. */
|
|
export interface NativeOpenCliFlags {
|
|
readonly snapshot?: string | undefined;
|
|
readonly provider?: AnalysisProviderSelector | undefined;
|
|
readonly "target-format"?: ExecutableFormatHint | undefined;
|
|
readonly architecture?: BinaryArchitecture | undefined;
|
|
readonly "entry-point"?: string | undefined;
|
|
}
|
|
|
|
/** Map native CLI flags onto the shared one-shot analysis options. */
|
|
export const directAnalysisOptionsFromCli = (
|
|
logger: Logger,
|
|
options: NativeOpenCliFlags,
|
|
) => ({
|
|
logger,
|
|
snapshotPath: options.snapshot,
|
|
...(options["target-format"] === undefined
|
|
? {}
|
|
: { formatHint: options["target-format"] }),
|
|
...(options.architecture === undefined
|
|
? {}
|
|
: { architecture: options.architecture }),
|
|
...(options["entry-point"] === undefined
|
|
? {}
|
|
: { entryPoint: options["entry-point"] }),
|
|
...(options.provider === undefined ? {} : { providerId: options.provider }),
|
|
});
|