Refactor Makeself signature and detection logic

Clean up and simplify Makeself detector: combine variable declarations and perform an early size check; replace Binary.isSignaturePresent with Binary.compare and short-circuit when too small; reformat regex matching for readability; remove the separate bHasBinary flag and use bDetected directly with an early-exit loop when scanning the trailer. Overall this streamlines the code and reduces temporary state used during binary-presence checks.
This commit is contained in:
DosX 2026-06-06 21:19:35 +03:00
commit bd8df5c931

View file

@ -4,50 +4,41 @@
meta("sfx", "Makeself SFX archive");
function detect() {
var nTotalSize = Binary.getSize();
var nSize = Math.min(16384, Binary.getSize());
if (nSize < 64) return result();
var nTotalSize = Binary.getSize(),
nSize = Math.min(16384, Binary.getSize());
if (!Binary.isSignaturePresent(0, 2, "2321")) { // #!
if (nSize < 64 || !Binary.compare("2321")) { // #!
return result();
}
var sFileContent = Binary.getString(0, nSize).toLowerCase();
var sVersionMatch = sFileContent.match(/ms_version\s*=\s*"([^"]+)"/) ||
sFileContent.match(/ms_version\s*=\s*([0-9.]+)/) ||
sFileContent.match(/makeself version\s+([0-9.]+)/);
var sVersionMatch =
sFileContent.match(/ms_version\s*=\s*"([^"]+)"/) ||
sFileContent.match(/ms_version\s*=\s*([0-9.]+)/) ||
sFileContent.match(/makeself version\s+([0-9.]+)/);
if (sVersionMatch) {
bDetected = true;
sVersion = sVersionMatch[1];
}
else if (sFileContent.indexOf("ms_version=") !== -1) {
bDetected = true;
} else if (sFileContent.indexOf("ms_version=") !== -1) {
bDetected = true;
}
if (bDetected) {
var bHasBinary = false;
var nCheckSize = Math.min(512, nTotalSize);
var nCheckOffset = nTotalSize - nCheckSize;
if (Binary.findByte(nCheckOffset, nCheckSize, 0x00) !== -1) {
bHasBinary = true;
bDetected = true;
} else {
for (var i = 0; i < nCheckSize; i++) {
for (var i = 0; i < nCheckSize && !bDetected; i++) {
var byteVal = Binary.readByte(nCheckOffset + i);
if (byteVal < 32 && byteVal !== 9 && byteVal !== 10 && byteVal !== 13) {
bHasBinary = true;
break;
bDetected = true;
}
}
}
if (!bHasBinary) {
bDetected = false;
sVersion = "";
}
}
return result();