refactor(go): share bounded string header word reads (#12553)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 22:35:22 +00:00
1 parent 57d1d7ca5e
commit 4edbbf0df9
2 files changed
+8 -16

No files matched your search

@@ -16,7 +16,6 @@
"src/dotnet/ManagedMethodBodyReader.ts": 5,
"src/ghidra/GhidraLoadImageValues.ts": 1,
"src/ghidra/extensions/GhidraExtensions.ts": 1,
"src/go/GoStringLiterals.ts": 4,
"src/il2cpp/Il2cppBinaryImage.ts": 2,
"src/il2cpp/Il2cppBinaryRegistration.ts": 8,
"src/il2cpp/Il2cppMetadataFile.ts": 21,
+8 -15
View File
@@ -9,6 +9,7 @@ import {
} from "../domain/go/goBinary.js";
import {
BinaryMappingFailure,
BinaryReader,
createFileMappingIndex,
imageFileMappings,
type BinaryContainer,
@@ -86,7 +87,11 @@ export const recoverGoStringLiterals = (
};
const { bits } = container;
const little = container.byte_order === "little";
const reader = new BinaryReader(
bytes,
container.byte_order === "little",
container.format,
);
const wordBytes = bits === 64 ? 8 : 4;
const firstTarget = targets[0] as FileMapping;
const lastTarget = targets[targets.length - 1] as FileMapping;
@@ -107,7 +112,7 @@ export const recoverGoStringLiterals = (
break;
}
remainingBudget -= wordBytes;
let word0 = readWord(bytes, startOffset, bits, little);
let word0 = reader.word(startOffset, bits);
for (
let headerOffset = startOffset;
@@ -121,7 +126,7 @@ export const recoverGoStringLiterals = (
break outer;
}
remainingBudget -= wordBytes;
const word1 = readWord(bytes, headerOffset + wordBytes, bits, little);
const word1 = reader.word(headerOffset + wordBytes, bits);
const ptr = word0;
const rawLen = word1;
word0 = word1;
@@ -226,18 +231,6 @@ export const recoverGoStringLiterals = (
};
};
const readWord = (
bytes: Buffer,
offset: number,
bits: 32 | 64,
little: boolean,
): bigint =>
bits === 32
? BigInt(little ? bytes.readUInt32LE(offset) : bytes.readUInt32BE(offset))
: little
? bytes.readBigUInt64LE(offset)
: bytes.readBigUInt64BE(offset);
const isPrintableGoUtf8 = (slice: Buffer): boolean => {
let hasNonAscii = false;
for (const byte of slice) {