Files
reverse-engineering-anything/src/cli/decompileDirectoryCommand.ts
Phonandmorluto 2fe0c9eb6a fix(cli): align local paths and SQLite selection validation (#9882)
fix(cli): batch of small fixes for #9714, #9718, #9759, #9856 and #9809

#9714: replace bare z.string().min(1) local paths in ADB CLI commands with localPathStringSchema so NUL bytes are rejected at the CLI boundary.
#9718: decode-android-resources path uses localPathStringSchema, matching apktoolInputSchemas.
#9759: inspect-sqlite-database CLI options now reject an empty column array and reject rowLimit/rowOffset/column/schemaScope=selected_table without --table via superRefine, mirroring inspectSqliteDatabaseInputSchema.
#9856 and #9809: shared snapshotSelectionOption is now localPathStringSchema.optional(), so every analyze/inspect/decompile/xrefs/trace/function/instructions/search --snapshot rejects NUL bytes.

Tests: tests/boundary/cli/overnightBatch1CliSchemas.test.ts (15 tests) proves empty/NUL input returns VALIDATION_ERROR before any provider runs and valid input is admitted.

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-11 23:40:01 +08:00

120 lines
4.2 KiB
TypeScript

import { projectCliAnalysisResult } from "./projectCliAnalysisResult.js";
import { z } from "incur";
import type { DirectAnalysis } from "../composition/directAnalysis.js";
import { CLI_COMMANDS } from "../cliCommandNames.js";
import { logCliCommand } from "../cliLogging.js";
import type { Logger } from "pino";
import {
cliDecimalField,
directAnalysisOptionsFromCli,
nativeOpenOptionFields,
snapshotSelectionOption,
} from "./options.js";
import { decompileDirectoryInputSchema } from "../contracts/decompileDirectory.js";
import type { CliInstance } from "./types.js";
import { localPathStringSchema } from "../domain/localPath.js";
import { absoluteCallerPath } from "../filesystem/callerPath.js";
import type { JsonValue } from "../domain/jsonValue.js";
/** Register `rea decompile-all`, the directory export of provider pseudocode. */
export const registerDecompileDirectoryCommand = (
cli: CliInstance,
logger: Logger,
runDirectAnalysis: DirectAnalysis["runDirectAnalysis"],
): void => {
cli.command(CLI_COMMANDS.decompileAll, {
description:
"Write every function's pseudocode into a directory and return a manifest",
args: z.object({
path: localPathStringSchema.describe("App or program path"),
}),
options: z.object({
output: localPathStringSchema.describe(
"Output directory. A relative path is resolved from the working directory. The parent must exist.",
),
layout: z
.enum(["address", "dwarf"])
.default("address")
.describe(
"address names files by function address. dwarf reports that compilation-unit paths are unavailable.",
),
resume: z
.boolean()
.default(false)
.describe(
"Continue a matching export. Never overwrites a function file.",
),
retryFailed: decompileDirectoryInputSchema.shape.retry_failed,
name: z
.string()
.min(1)
.optional()
.describe("Case-sensitive literal substring of the function name"),
namespace: z
.string()
.min(1)
.optional()
.describe(
"Case-sensitive function-name prefix, including any separator",
),
addressStart: z
.string()
.min(1)
.optional()
.describe("Inclusive lower bound, 0xoffset or space:0xoffset"),
addressEnd: z
.string()
.min(1)
.optional()
.describe("Inclusive upper bound, 0xoffset or space:0xoffset"),
limit: cliDecimalField(
decompileDirectoryInputSchema.shape.limit,
).describe(
"Maximum functions to attempt after skipping verified files and recorded external functions",
),
snapshot: snapshotSelectionOption.describe(
"Load a local analysis snapshot. decompile-all does not update it.",
),
...nativeOpenOptionFields,
}),
run: ({ args, options }) =>
logCliCommand(logger, "decompile-all", () =>
runDirectAnalysis(
args.path,
"decompile_all",
decompileAllArguments(options),
directAnalysisOptionsFromCli(logger, options),
).then(projectCliAnalysisResult),
),
});
};
const decompileAllArguments = (options: {
readonly output: string;
readonly layout: "address" | "dwarf";
readonly resume: boolean;
readonly retryFailed?: "independent_fresh" | undefined;
readonly name?: string | undefined;
readonly namespace?: string | undefined;
readonly addressStart?: string | undefined;
readonly addressEnd?: string | undefined;
readonly limit?: number | undefined;
}): Readonly<Record<string, JsonValue>> => ({
output_directory: absoluteCallerPath(options.output),
layout: options.layout,
resume: options.resume,
...(options.retryFailed === undefined
? {}
: { retry_failed: options.retryFailed }),
...(options.name === undefined ? {} : { name: options.name }),
...(options.namespace === undefined ? {} : { namespace: options.namespace }),
...(options.addressStart === undefined
? {}
: { address_start: options.addressStart }),
...(options.addressEnd === undefined
? {}
: { address_end: options.addressEnd }),
...(options.limit === undefined ? {} : { limit: options.limit }),
});