mirror of
https://github.com/morluto/rea.git
synced 2026-11-15 03:02:56 +00:00
refactor: consolidate shared analysis boundaries (#10293)
This commit is contained in:
1 parent
6878a84796
commit
c836f57b7d
70 files changed
+1240
-1131
No files matched your search
@@ -1,3 +1,7 @@
|
||||
import {
|
||||
hashArtifactRange,
|
||||
readArtifactRange as readExact,
|
||||
} from "../ArtifactCarrierRange.js";
|
||||
import { readMachoSegmentHeader } from "../../domain/apple/machoSegmentHeader.js";
|
||||
import { readMachoSectionHeader } from "../../domain/apple/machoSectionHeader.js";
|
||||
import { readElfSectionHeader } from "../../domain/elfSectionHeader.js";
|
||||
@@ -214,8 +218,11 @@ const collapseRanges = async (
|
||||
if (chosen === undefined) return undefined;
|
||||
if (unique.length > 1) {
|
||||
const hashes = new Set<string>();
|
||||
for (const range of unique)
|
||||
hashes.add(await digestRange(carrier, range.offset, range.length));
|
||||
for (const range of unique) {
|
||||
const hash = await hashArtifactRange(carrier, range.offset, range.length);
|
||||
if (hash === undefined) throw formatFailure(NODE_SEA_TRUNCATED);
|
||||
hashes.add(hash);
|
||||
}
|
||||
if (hashes.size !== 1) throw formatFailure(NODE_SEA_DUPLICATE_BLOB);
|
||||
}
|
||||
return {
|
||||
@@ -694,58 +701,6 @@ const locatePeResources = async (carrier: Carrier): Promise<BlobRange[]> => {
|
||||
});
|
||||
};
|
||||
|
||||
const digestRange = async (
|
||||
carrier: Carrier,
|
||||
offset: number,
|
||||
length: number,
|
||||
): Promise<string> => {
|
||||
const hash = createHash("sha256");
|
||||
let remaining = length;
|
||||
let position = offset;
|
||||
while (remaining > 0) {
|
||||
const chunkLength = Math.min(64 * 1024, remaining);
|
||||
const chunk = await readExact(carrier, position, chunkLength);
|
||||
if (chunk === undefined) throw formatFailure(NODE_SEA_TRUNCATED);
|
||||
hash.update(chunk);
|
||||
position += chunkLength;
|
||||
remaining -= chunkLength;
|
||||
}
|
||||
return hash.digest("hex");
|
||||
};
|
||||
|
||||
const readExact = async (
|
||||
carrier: Carrier,
|
||||
offset: number,
|
||||
length: number,
|
||||
): Promise<Buffer | undefined> => {
|
||||
abortIfNeeded(carrier.signal);
|
||||
if (
|
||||
!Number.isSafeInteger(offset) ||
|
||||
!Number.isSafeInteger(length) ||
|
||||
offset < 0 ||
|
||||
length < 0 ||
|
||||
offset > carrier.fileSize ||
|
||||
length > carrier.fileSize - offset
|
||||
)
|
||||
return undefined;
|
||||
if (length === 0) return Buffer.alloc(0);
|
||||
const bytes = Buffer.alloc(length);
|
||||
let filled = 0;
|
||||
while (filled < length) {
|
||||
abortIfNeeded(carrier.signal);
|
||||
const read = await carrier.handle.read(
|
||||
bytes,
|
||||
filled,
|
||||
length - filled,
|
||||
offset + filled,
|
||||
);
|
||||
abortIfNeeded(carrier.signal);
|
||||
if (read.bytesRead === 0) return undefined;
|
||||
filled += read.bytesRead;
|
||||
}
|
||||
return bytes;
|
||||
};
|
||||
|
||||
const cString = (bytes: Buffer, offset: number, length: number): string => {
|
||||
let end = offset;
|
||||
const limit = Math.min(bytes.length, offset + length);
|
||||
|
||||
Reference in new issue
Block a user