mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 21:23:45 +00:00
* fix(elf): share dynamic table singleton validation * fix(elf): share note storage and framing across readers Refs #12130 * fix(elf): retain zero-filled load mapping ambiguity Refs #12136 * fix(pe): share header extent admission Refs #12143 * fix(macho): share embedded section backing checks Refs #12142 * fix(pe): share header facts and validate resource directory order * fix(macho): require complete carrier load command framing * fix(pe): probe declared resource headers by file offset * fix(elf): preserve zero-fill observations across mapping gaps * refactor(pe): share positional COFF header observation (#12173) * fix(elf): check load-map agreement across section extents (#12175) * fix(elf): check load-map agreement across section extents * fix(elf): preserve agreed section links across unreadable tables
67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import { err, ok } from "./result.js";
|
|
|
|
export const PE_SECTION_HEADER_BYTES = 40;
|
|
|
|
/** Locate the COFF section table from the declared optional-header span. */
|
|
export const peSectionTableExtent = (
|
|
optionalHeaderOffset: number,
|
|
optionalHeaderSize: number,
|
|
sectionCount: number,
|
|
) => ({
|
|
sectionTableOffset: optionalHeaderOffset + optionalHeaderSize,
|
|
sectionTableBytes: sectionCount * PE_SECTION_HEADER_BYTES,
|
|
});
|
|
|
|
/** Observe the declared combined header extent separately from the available prefix. */
|
|
export const observePeHeaderExtent = ({
|
|
optionalHeaderOffset,
|
|
optionalHeaderSize,
|
|
sectionCount,
|
|
sizeOfHeaders,
|
|
fileSize,
|
|
}: {
|
|
readonly optionalHeaderOffset: number;
|
|
readonly optionalHeaderSize: number;
|
|
readonly sectionCount: number;
|
|
readonly sizeOfHeaders: number;
|
|
readonly fileSize: number;
|
|
}) => {
|
|
// These offsets and counts come from the fixed-width PE and COFF readers.
|
|
const { sectionTableOffset, sectionTableBytes } = peSectionTableExtent(
|
|
optionalHeaderOffset,
|
|
optionalHeaderSize,
|
|
sectionCount,
|
|
);
|
|
const sizeOfHeadersState =
|
|
sizeOfHeaders < sectionTableOffset + sectionTableBytes
|
|
? "before-section-table-end"
|
|
: sizeOfHeaders > fileSize
|
|
? "past-file-end"
|
|
: "valid";
|
|
return { sectionTableOffset, sectionTableBytes, sizeOfHeadersState };
|
|
};
|
|
|
|
/** Fixed COFF section facts; callers own file/range and mapping admission. */
|
|
export const readPeSectionHeader = (bytes: Buffer) => {
|
|
if (bytes.length < PE_SECTION_HEADER_BYTES)
|
|
return err({
|
|
kind: "malformed" as const,
|
|
message: "PE section header is truncated",
|
|
});
|
|
const name = bytes.subarray(0, 8);
|
|
const nul = name.indexOf(0);
|
|
const virtual_size = bytes.readUInt32LE(8);
|
|
const raw_size = bytes.readUInt32LE(16);
|
|
return ok({
|
|
name: name.toString("latin1", 0, nul < 0 ? name.length : nul),
|
|
virtual_size,
|
|
virtual_address: bytes.readUInt32LE(12),
|
|
raw_size,
|
|
offset: bytes.readUInt32LE(20),
|
|
characteristics: bytes.readUInt32LE(36),
|
|
// Content excludes file-alignment padding; raw_size remains available to
|
|
// consumers that validate physical file ranges.
|
|
size: virtual_size === 0 ? raw_size : Math.min(virtual_size, raw_size),
|
|
});
|
|
};
|