Files
reverse-engineering-anything/src/il2cpp/Il2cppMetadataV108.ts
T

114 lines
4.0 KiB
TypeScript

import { strictUtf8Decoder } from "../domain/strictUtf8.js";
import { IL2CPP_METADATA_SECTION_NAMES } from "../domain/il2cpp/il2cppMetadata.js";
import {
IL2CPP_METADATA_PREFIX_BYTES,
observeIl2cppMetadataHeaderPrefix,
} from "../domain/il2cpp/il2cppMetadataHeaderPrefix.js";
import {
firstIl2cppSectionIssue,
readIl2cppSectionTable,
} from "./Il2cppMetadataSectionTable.js";
import { Il2cppMetadataFormatFailure } from "./Il2cppMetadataFormat.js";
// Header order follows LibCpp2IL's Il2CppGlobalMetadataHeader (v108):
// https://github.com/SamboyCoding/Cpp2IL/blob/development/LibCpp2IL/Metadata/Il2CppGlobalMetadataHeader.cs
// Record layouts and native registration are deliberately outside this observation.
const SECTION_NAMES = [
...IL2CPP_METADATA_SECTION_NAMES.slice(0, 20),
"typeInlineArrays",
...IL2CPP_METADATA_SECTION_NAMES.slice(20),
"methodSpecsOnGenericType",
"genericMethodSpecsOnType",
"methodSpecs",
"genericMethodFunctionsDefinitions",
"genericMethodFunctionsDefinitionsWithAdjustor",
"invokerIndices",
"rgctxRanges",
"rgctxValues",
"staticConstructorTypeIndices",
] as const;
const HEADER_BYTES = IL2CPP_METADATA_PREFIX_BYTES + SECTION_NAMES.length * 12;
const SAMPLE_STRINGS = 16;
const SAMPLE_BYTES = 128;
const SCAN_BATCH_BYTES = 64 * 1024;
const utf8 = strictUtf8Decoder("preserve");
/** A bounded, header-only observation; never a decoded metadata report. */
export const inspectIl2cppMetadataV108 = async (
bytes: Buffer,
afterBatch?: () => Promise<void>,
) => {
const prefix = observeIl2cppMetadataHeaderPrefix(bytes);
if (prefix.kind !== "il2cpp" || prefix.version !== 108) return null;
if (bytes.length < HEADER_BYTES)
throw new Il2cppMetadataFormatFailure(
"unsupported-version",
`version 108 layout is unverified; the file has ${bytes.length} bytes, fewer than the assumed ${HEADER_BYTES}-byte header`,
);
const sections = readIl2cppSectionTable(bytes, SECTION_NAMES, 12);
const issue = firstIl2cppSectionIssue(sections, HEADER_BYTES, bytes.length);
if (issue !== undefined) {
const detail =
issue.kind === "overlap"
? `assumed sections ${issue.previous.name} and ${issue.current.name} overlap at ${issue.current.offset}`
: `assumed section ${issue.section.name} (offset ${issue.section.offset}, size ${issue.section.size}, count ${String(issue.section.count)}) is invalid for the ${bytes.length}-byte file`;
throw new Il2cppMetadataFormatFailure(
"unsupported-version",
`version 108 layout is unverified; ${detail}`,
);
}
const strings = sections[2]!;
const end = strings.offset + strings.size;
const sample: Array<{
offset: number;
value: string | null;
value_base64: string | null;
truncated: boolean;
}> = [];
let start = strings.offset;
let terminatedCount = 0;
for (let batch = start; batch < end; batch += SCAN_BATCH_BYTES) {
const batchEnd = Math.min(end, batch + SCAN_BATCH_BYTES);
for (let at = batch; at < batchEnd; at++) {
if (bytes[at] !== 0) continue;
terminatedCount++;
if (sample.length < SAMPLE_STRINGS) {
const length = at - start;
const raw = bytes.subarray(
start,
start + Math.min(length, SAMPLE_BYTES),
);
let value: string | null;
try {
value = utf8.decode(raw);
} catch {
value = null;
}
sample.push({
offset: start - strings.offset,
value,
value_base64: value === null ? raw.toString("base64") : null,
truncated: length > SAMPLE_BYTES,
});
}
start = at + 1;
}
await afterBatch?.();
}
return {
magic: prefix.magic,
metadata_version: 108,
header_bytes: HEADER_BYTES,
layout_status: "unverified-with-real-v108-artifact",
sections,
strings: {
terminated_count: terminatedCount,
unterminated_tail_bytes: end - start,
sample,
sample_limit: SAMPLE_STRINGS,
sample_bytes_per_string: SAMPLE_BYTES,
},
coverage: "header-and-bounded-string-sample-only",
} as const;
};