mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 13:45:30 +00:00
refactor(pe): read optional header fields through BinaryReader (#12351)
This commit is contained in:
1 parent
9a3aec331b
commit
5a27508ae3
2 files changed
+43
-27
No files matched your search
@@ -39,7 +39,6 @@
|
||||
"src/domain/native/readBinaryContainer.ts": 1,
|
||||
"src/domain/pe/peResourceInventory.ts": 7,
|
||||
"src/domain/pe/peResourceTreeFacts.ts": 9,
|
||||
"src/domain/peOptionalHeader.ts": 12,
|
||||
"src/domain/peSectionHeader.ts": 5,
|
||||
"src/domain/pngHeader.ts": 4,
|
||||
"src/domain/pngVisualDiff.ts": 7,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { BinaryReader } from "./binaryContainer.js";
|
||||
import { err, ok, type Result } from "./result.js";
|
||||
|
||||
export const PE_SIZE_OF_HEADERS_OFFSET = 60;
|
||||
@@ -51,18 +52,31 @@ export interface PeOptionalHeaderFailure {
|
||||
readonly message: string;
|
||||
}
|
||||
|
||||
const optionalHeaderReader = (bytes: Uint8Array): BinaryReader =>
|
||||
new BinaryReader(
|
||||
Buffer.isBuffer(bytes)
|
||||
? bytes
|
||||
: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
|
||||
true,
|
||||
"pe",
|
||||
);
|
||||
|
||||
/** Decode format identity without requiring mapping fields or directories. */
|
||||
export const readPeOptionalHeaderIdentity = (
|
||||
bytes: Uint8Array,
|
||||
): Result<PeOptionalHeaderIdentity, PeOptionalHeaderFailure> =>
|
||||
readIdentity(optionalHeaderReader(bytes));
|
||||
|
||||
const readIdentity = (
|
||||
reader: BinaryReader,
|
||||
): Result<PeOptionalHeaderIdentity, PeOptionalHeaderFailure> => {
|
||||
if (bytes.byteLength < 2)
|
||||
if (reader.bytes.length < 2)
|
||||
return err({
|
||||
kind: "malformed",
|
||||
reason: "truncated-magic",
|
||||
message: "truncated PE optional header",
|
||||
});
|
||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
const magic = view.getUint16(0, true);
|
||||
const magic = reader.u16(0);
|
||||
if (magic !== 0x10b && magic !== 0x20b)
|
||||
return err({
|
||||
kind: "unsupported",
|
||||
@@ -76,41 +90,44 @@ export const readPeOptionalHeaderIdentity = (
|
||||
/** Decode mapping fields without requiring a directory count or table. */
|
||||
export const readPeOptionalHeaderFixedFields = (
|
||||
bytes: Uint8Array,
|
||||
): Result<PeOptionalHeaderFixedFields, PeOptionalHeaderFailure> =>
|
||||
readFixedFields(optionalHeaderReader(bytes));
|
||||
|
||||
const readFixedFields = (
|
||||
reader: BinaryReader,
|
||||
): Result<PeOptionalHeaderFixedFields, PeOptionalHeaderFailure> => {
|
||||
const identity = readPeOptionalHeaderIdentity(bytes);
|
||||
const identity = readIdentity(reader);
|
||||
if (!identity.ok) return identity;
|
||||
const { bits } = identity.value;
|
||||
const directoryOffset = bits === 64 ? 112 : 96;
|
||||
// SizeOfHeaders ends at byte 64. DllCharacteristics, at offset 70, is only
|
||||
// present when the declared header covers that word. NativeAOT and other
|
||||
// mapping-field consumers admit a 64-byte header.
|
||||
if (bytes.byteLength < PE_SIZE_OF_HEADERS_OFFSET + 4)
|
||||
if (reader.bytes.length < PE_SIZE_OF_HEADERS_OFFSET + 4)
|
||||
return err({
|
||||
kind: "malformed",
|
||||
reason: "truncated-fixed-fields",
|
||||
message: "truncated PE optional header",
|
||||
});
|
||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
return ok({
|
||||
...identity.value,
|
||||
imageBase:
|
||||
bits === 64
|
||||
? view.getBigUint64(24, true)
|
||||
: BigInt(view.getUint32(28, true)),
|
||||
sectionAlignment: view.getUint32(32, true),
|
||||
fileAlignment: view.getUint32(36, true),
|
||||
sizeOfImage: view.getUint32(56, true),
|
||||
sizeOfHeaders: view.getUint32(PE_SIZE_OF_HEADERS_OFFSET, true),
|
||||
dllCharacteristics:
|
||||
bytes.byteLength >= 72 ? view.getUint16(70, true) : null,
|
||||
imageBase: reader.word(bits === 64 ? 24 : 28, bits),
|
||||
sectionAlignment: reader.u32(32),
|
||||
fileAlignment: reader.u32(36),
|
||||
sizeOfImage: reader.u32(56),
|
||||
sizeOfHeaders: reader.u32(PE_SIZE_OF_HEADERS_OFFSET),
|
||||
dllCharacteristics: reader.bytes.length >= 72 ? reader.u16(70) : null,
|
||||
directoryOffset,
|
||||
directoryCountOffset: directoryOffset - 4,
|
||||
});
|
||||
};
|
||||
|
||||
const readDirectory = (view: DataView, offset: number): PeDataDirectory => ({
|
||||
address: view.getUint32(offset, true),
|
||||
size: view.getUint32(offset + 4, true),
|
||||
const readDirectory = (
|
||||
reader: BinaryReader,
|
||||
offset: number,
|
||||
): PeDataDirectory => ({
|
||||
address: reader.u32(offset),
|
||||
size: reader.u32(offset + 4),
|
||||
offset,
|
||||
});
|
||||
|
||||
@@ -132,15 +149,15 @@ export const readPeOptionalHeaderDirectory = (
|
||||
): PeOptionalHeaderDirectoryObservation => {
|
||||
if (bytes.byteLength < fixed.directoryOffset)
|
||||
return { state: "count-unavailable" };
|
||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
const count = view.getUint32(fixed.directoryCountOffset, true);
|
||||
const reader = optionalHeaderReader(bytes);
|
||||
const count = reader.u32(fixed.directoryCountOffset);
|
||||
if (index >= count) return { state: "absent", count };
|
||||
const offset = fixed.directoryOffset + index * 8;
|
||||
if (offset > bytes.byteLength - 8) return { state: "truncated", count };
|
||||
return {
|
||||
state: "present",
|
||||
count,
|
||||
directory: readDirectory(view, offset),
|
||||
directory: readDirectory(reader, offset),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -148,7 +165,8 @@ export const readPeOptionalHeaderDirectory = (
|
||||
export const readPeOptionalHeaderStructure = (
|
||||
bytes: Uint8Array,
|
||||
): Result<PeOptionalHeaderStructure, PeOptionalHeaderFailure> => {
|
||||
const parsed = readPeOptionalHeaderFixedFields(bytes);
|
||||
const reader = optionalHeaderReader(bytes);
|
||||
const parsed = readFixedFields(reader);
|
||||
if (!parsed.ok) return parsed;
|
||||
const fixed = parsed.value;
|
||||
if (bytes.byteLength < fixed.directoryOffset)
|
||||
@@ -157,8 +175,7 @@ export const readPeOptionalHeaderStructure = (
|
||||
reason: "truncated-fixed-fields",
|
||||
message: "truncated PE optional header",
|
||||
});
|
||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
const directoryCount = view.getUint32(fixed.directoryCountOffset, true);
|
||||
const directoryCount = reader.u32(fixed.directoryCountOffset);
|
||||
if (
|
||||
directoryCount > Math.floor((bytes.byteLength - fixed.directoryOffset) / 8)
|
||||
)
|
||||
@@ -168,7 +185,7 @@ export const readPeOptionalHeaderStructure = (
|
||||
message: "truncated PE data-directory table",
|
||||
});
|
||||
const dataDirectories = Array.from({ length: directoryCount }, (_, index) =>
|
||||
readDirectory(view, fixed.directoryOffset + index * 8),
|
||||
readDirectory(reader, fixed.directoryOffset + index * 8),
|
||||
);
|
||||
// A directory table starts at byte 96 or 112, so the word at offset 70 is covered.
|
||||
const dllCharacteristics = fixed.dllCharacteristics;
|
||||
|
||||
Reference in new issue
Block a user