mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Binary: improve performance
This commit is contained in:
@@ -14,26 +14,23 @@ static inline char* realpath(const char* restrict file_name, char* restrict reso
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool extractNvimVersionFromBinary(const char* str, uint32_t len, void* userdata)
|
||||
static bool extractNvimVersionFromBinary(const char* str, FF_MAYBE_UNUSED uint32_t len, void* userdata)
|
||||
{
|
||||
if (len < strlen("NVIM v0.0.0")) return true;
|
||||
if (!ffStrStartsWith(str, "NVIM v")) return true;
|
||||
ffStrbufSetS((FFstrbuf*) userdata, str + strlen("NVIM v"));
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool extractVimVersionFromBinary(const char* str, uint32_t len, void* userdata)
|
||||
static bool extractVimVersionFromBinary(const char* str, FF_MAYBE_UNUSED uint32_t len, void* userdata)
|
||||
{
|
||||
if (len < strlen("VIM - Vi IMproved 0.0")) return true;
|
||||
if (!ffStrStartsWith(str, "VIM - Vi IMproved ")) return true;
|
||||
ffStrbufSetS((FFstrbuf*) userdata, str + strlen("VIM - Vi IMproved "));
|
||||
ffStrbufSubstrBeforeFirstC(userdata, ' ');
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool extractNanoVersionFromBinary(const char* str, uint32_t len, void* userdata)
|
||||
static bool extractNanoVersionFromBinary(const char* str, FF_MAYBE_UNUSED uint32_t len, void* userdata)
|
||||
{
|
||||
if (len < strlen("GNU nano 0.0")) return true;
|
||||
if (!ffStrStartsWith(str, "GNU nano ")) return true;
|
||||
ffStrbufSetS((FFstrbuf*) userdata, str + strlen("GNU nano "));
|
||||
return false;
|
||||
@@ -97,11 +94,11 @@ const char* ffDetectEditor(FFEditorResult* result)
|
||||
if (!instance.config.general.detectVersion) return NULL;
|
||||
|
||||
if (ffStrbufEqualS(&result->exe, "nvim"))
|
||||
ffBinaryExtractStrings(result->path.chars, extractNvimVersionFromBinary, &result->version);
|
||||
ffBinaryExtractStrings(result->path.chars, extractNvimVersionFromBinary, &result->version, (uint32_t) strlen("NVIM v0.0.0"));
|
||||
else if (ffStrbufEqualS(&result->exe, "vim"))
|
||||
ffBinaryExtractStrings(result->path.chars, extractVimVersionFromBinary, &result->version);
|
||||
ffBinaryExtractStrings(result->path.chars, extractVimVersionFromBinary, &result->version, (uint32_t) strlen("VIM - Vi IMproved 0.0"));
|
||||
else if (ffStrbufEqualS(&result->exe, "nano"))
|
||||
ffBinaryExtractStrings(result->path.chars, extractNanoVersionFromBinary, &result->version);
|
||||
ffBinaryExtractStrings(result->path.chars, extractNanoVersionFromBinary, &result->version, (uint32_t) strlen("GNU nano 0.0"));
|
||||
|
||||
if (result->version.length > 0) return NULL;
|
||||
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
#include "initsystem.h"
|
||||
#include "common/processing.h"
|
||||
#include "util/binary.h"
|
||||
#include "util/stringUtils.h"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
FF_MAYBE_UNUSED static bool elfExtractStringsCallBack(const char* str, uint32_t len, void* data)
|
||||
FF_MAYBE_UNUSED static bool extractSystemdVersion(const char* str, uint32_t len, void* userdata)
|
||||
{
|
||||
if (len > strlen("systemd 0.0 running in ") && memcmp(str, "systemd ", strlen("systemd ")) == 0)
|
||||
{
|
||||
const char* pend = memmem(str + strlen("systemd "), len - strlen("systemd "), " running in ", strlen(" running in "));
|
||||
if (pend)
|
||||
{
|
||||
ffStrbufSetNS((FFstrbuf*) data, (uint32_t) (pend - str) - (uint32_t) strlen("systemd "), str + strlen("systemd "));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
if (!ffStrStartsWith(str, "systemd ")) return true;
|
||||
const char* pstart = str + strlen("systemd ");
|
||||
const char* pend = memmem(pstart, len - strlen("systemd "), " running in ", strlen(" running in "));
|
||||
if (!pend) return true;
|
||||
ffStrbufSetNS((FFstrbuf*) userdata, (uint32_t) (pend - pstart), pstart);
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* ffDetectInitSystem(FFInitSystemResult* result)
|
||||
@@ -49,7 +47,7 @@ const char* ffDetectInitSystem(FFInitSystemResult* result)
|
||||
#if __linux__ && !__ANDROID__
|
||||
if (ffStrbufEqualS(&result->name, "systemd"))
|
||||
{
|
||||
ffBinaryExtractStrings(result->exe.chars, elfExtractStringsCallBack, &result->version);
|
||||
ffBinaryExtractStrings(result->exe.chars, extractSystemdVersion, &result->version, (uint32_t) strlen("systemd 0.0 running in x"));
|
||||
if (result->version.length == 0)
|
||||
{
|
||||
if (ffProcessAppendStdOut(&result->version, (char* const[]) {
|
||||
|
||||
@@ -287,10 +287,10 @@ static void detectXterm(FFTerminalFontResult* terminalFont)
|
||||
ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars);
|
||||
}
|
||||
|
||||
static bool elfExtractStringsCallBack(const char* str, uint32_t len, void* userData)
|
||||
static bool extractStTermFont(const char* str, FF_MAYBE_UNUSED uint32_t len, void* userdata)
|
||||
{
|
||||
if (!ffStrContains(str, "size=")) return true;
|
||||
ffStrbufSetNS((FFstrbuf*) userData, len, str);
|
||||
ffStrbufSetNS((FFstrbuf*) userdata, len, str);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -315,7 +315,7 @@ static void detectSt(FFTerminalFontResult* terminalFont, const FFTerminalResult*
|
||||
{
|
||||
ffStrbufClear(&font);
|
||||
|
||||
const char* error = ffBinaryExtractStrings(terminal->exePath.chars, elfExtractStringsCallBack, &font);
|
||||
const char* error = ffBinaryExtractStrings(terminal->exePath.chars, extractStTermFont, &font, (uint32_t) strlen("size=0"));
|
||||
if (error)
|
||||
{
|
||||
ffStrbufAppendS(&terminalFont->error, error);
|
||||
|
||||
@@ -64,9 +64,8 @@ static bool getExeVersionGeneral(FFstrbuf* exe, FFstrbuf* version)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool extractBashVersion(const char* line, uint32_t len, void *userdata)
|
||||
static bool extractBashVersion(const char* line, FF_MAYBE_UNUSED uint32_t len, void *userdata)
|
||||
{
|
||||
if (len < strlen("@(#)Bash version 0.0.0(0) release GNU")) return true;
|
||||
if (!ffStrStartsWith(line, "@(#)Bash version ")) return true;
|
||||
const char* start = line + strlen("@(#)Bash version ");
|
||||
const char* end = strchr(start, '(');
|
||||
@@ -80,7 +79,7 @@ static bool getShellVersionBash(FFstrbuf* exe, FFstrbuf* exePath, FFstrbuf* vers
|
||||
const char* path = exePath->chars;
|
||||
if (*path == '\0')
|
||||
path = exe->chars;
|
||||
ffBinaryExtractStrings(path, extractBashVersion, version);
|
||||
ffBinaryExtractStrings(path, extractBashVersion, version, (uint32_t) strlen("@(#)Bash version 0.0.0(0) release GNU"));
|
||||
|
||||
if(!getExeVersionRaw(exe, version))
|
||||
return false;
|
||||
@@ -203,9 +202,8 @@ static bool getShellVersionXonsh(FFstrbuf* exe, FFstrbuf* version)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool extractZshVersion(const char* line, uint32_t len, void *userdata)
|
||||
static bool extractZshVersion(const char* line, FF_MAYBE_UNUSED uint32_t len, void *userdata)
|
||||
{
|
||||
if (len < strlen("zsh-0.0-0")) return true;
|
||||
if (!ffStrStartsWith(line, "zsh-")) return true;
|
||||
const char* start = line + strlen("zsh-");
|
||||
const char* end = strchr(start, '-');
|
||||
@@ -221,7 +219,7 @@ static bool getShellVersionZsh(FFstrbuf* exe, FFstrbuf* exePath, FFstrbuf* versi
|
||||
if (*path == '\0')
|
||||
path = exe->chars;
|
||||
|
||||
ffBinaryExtractStrings(path, extractZshVersion, version);
|
||||
ffBinaryExtractStrings(path, extractZshVersion, version, (uint32_t) strlen("zsh-0.0-0"));
|
||||
if (version->length) return true;
|
||||
|
||||
return getExeVersionGeneral(exe, version); //zsh 5.9 (arm-apple-darwin21.3.0)
|
||||
@@ -303,7 +301,7 @@ FF_MAYBE_UNUSED static bool getTerminalVersionGnome(FFstrbuf* exe, FFstrbuf* ver
|
||||
{
|
||||
if (exe->chars[0] == '/')
|
||||
{
|
||||
ffBinaryExtractStrings(exe->chars, extractGnomeTerminalVersion, version);
|
||||
ffBinaryExtractStrings(exe->chars, extractGnomeTerminalVersion, version, (uint32_t) strlen("0.0.0"));
|
||||
if (version->length) return true;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -2,4 +2,4 @@
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
const char* ffBinaryExtractStrings(const char* file, bool (*cb)(const char* str, uint32_t len, void* userdata), void* userdata);
|
||||
const char* ffBinaryExtractStrings(const char* file, bool (*cb)(const char* str, uint32_t len, void* userdata), void* userdata, uint32_t minLength);
|
||||
|
||||
+10
-9
@@ -20,7 +20,7 @@ static inline bool readData(FILE *objFile, void *buf, size_t size, off_t offset)
|
||||
return fread(buf, 1, size, objFile) == size;
|
||||
}
|
||||
|
||||
static bool handleMachSection(FILE *objFile, const char *name, off_t offset, size_t size, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata)
|
||||
static bool handleMachSection(FILE *objFile, const char *name, off_t offset, size_t size, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata, uint32_t minLength)
|
||||
{
|
||||
if (!ffStrEquals(name, "__cstring")) return true;
|
||||
|
||||
@@ -33,6 +33,7 @@ static bool handleMachSection(FILE *objFile, const char *name, off_t offset, siz
|
||||
const char* p = (const char*) data + off;
|
||||
if (*p == '\0') continue;
|
||||
uint32_t len = (uint32_t) strlen(p);
|
||||
if (len < minLength) continue;
|
||||
if (*p >= ' ' && *p <= '~') // Ignore control characters
|
||||
{
|
||||
if (!cb(p, len, userdata)) return false;
|
||||
@@ -42,7 +43,7 @@ static bool handleMachSection(FILE *objFile, const char *name, off_t offset, siz
|
||||
return true;
|
||||
}
|
||||
|
||||
static const char* dumpMachHeader(FILE *objFile, off_t offset, bool is_64, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata)
|
||||
static const char* dumpMachHeader(FILE *objFile, off_t offset, bool is_64, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata, uint32_t minLength)
|
||||
{
|
||||
uint32_t ncmds;
|
||||
off_t loadCommandsOffset = offset;
|
||||
@@ -87,7 +88,7 @@ static const char* dumpMachHeader(FILE *objFile, off_t offset, bool is_64, bool
|
||||
if (!readData(objFile, §ion, sizeof(section), (off_t) ((size_t) commandOffset + sizeof(segment) + j * sizeof(section))))
|
||||
continue;
|
||||
|
||||
if (!handleMachSection(objFile, section.sectname, section.offset, section.size, cb, userdata))
|
||||
if (!handleMachSection(objFile, section.sectname, section.offset, section.size, cb, userdata, minLength))
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -105,7 +106,7 @@ static const char* dumpMachHeader(FILE *objFile, off_t offset, bool is_64, bool
|
||||
if (!readData(objFile, §ion, sizeof(section), (off_t) ((size_t) commandOffset + sizeof(segment) + j * sizeof(section))))
|
||||
continue;
|
||||
|
||||
if (!handleMachSection(objFile, section.sectname, section.offset, section.size, cb, userdata))
|
||||
if (!handleMachSection(objFile, section.sectname, section.offset, section.size, cb, userdata, minLength))
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -116,7 +117,7 @@ static const char* dumpMachHeader(FILE *objFile, off_t offset, bool is_64, bool
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char* dumpFatHeader(FILE *objFile, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata)
|
||||
static const char* dumpFatHeader(FILE *objFile, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata, uint32_t minLength)
|
||||
{
|
||||
struct fat_header header;
|
||||
if (!readData(objFile, &header, sizeof(header), 0))
|
||||
@@ -157,14 +158,14 @@ static const char* dumpFatHeader(FILE *objFile, bool (*cb)(const char *str, uint
|
||||
|
||||
if (magic == MH_MAGIC_64 || magic == MH_MAGIC)
|
||||
{
|
||||
dumpMachHeader(objFile, machHeaderOffset, magic == MH_MAGIC_64, cb, userdata);
|
||||
dumpMachHeader(objFile, machHeaderOffset, magic == MH_MAGIC_64, cb, userdata, minLength);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return "Unsupported fat header";
|
||||
}
|
||||
|
||||
const char *ffBinaryExtractStrings(const char *machoFile, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata)
|
||||
const char *ffBinaryExtractStrings(const char *machoFile, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata, uint32_t minLength)
|
||||
{
|
||||
FF_AUTO_CLOSE_FILE FILE *objFile = fopen(machoFile, "rb");
|
||||
if (objFile == NULL)
|
||||
@@ -180,7 +181,7 @@ const char *ffBinaryExtractStrings(const char *machoFile, bool (*cb)(const char
|
||||
return "Unsupported format or big endian mach-o file";
|
||||
|
||||
if (magic == FAT_MAGIC || magic == FAT_MAGIC_64 || magic == FAT_CIGAM || magic == FAT_CIGAM_64)
|
||||
return dumpFatHeader(objFile, cb, userdata);
|
||||
return dumpFatHeader(objFile, cb, userdata, minLength);
|
||||
else
|
||||
return dumpMachHeader(objFile, 0, magic == MH_MAGIC_64, cb, userdata);
|
||||
return dumpMachHeader(objFile, 0, magic == MH_MAGIC_64, cb, userdata, minLength);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ struct FFElfData {
|
||||
bool inited;
|
||||
} elfData;
|
||||
|
||||
const char* ffBinaryExtractStrings(const char* elfFile, bool (*cb)(const char* str, uint32_t len, void* userdata), void* userdata)
|
||||
const char* ffBinaryExtractStrings(const char* elfFile, bool (*cb)(const char* str, uint32_t len, void* userdata), void* userdata, uint32_t minLength)
|
||||
{
|
||||
if (!elfData.inited)
|
||||
{
|
||||
@@ -82,6 +82,7 @@ const char* ffBinaryExtractStrings(const char* elfFile, bool (*cb)(const char* s
|
||||
const char* p = (const char*) data->d_buf + off;
|
||||
if (*p == '\0') continue;
|
||||
uint32_t len = (uint32_t) strlen(p);
|
||||
if (len < minLength) continue;
|
||||
if (*p >= ' ' && *p <= '~') // Ignore control characters
|
||||
{
|
||||
if (!cb(p, len, userdata)) break;
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
const char* ffBinaryExtractStrings(const char *peFile, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata)
|
||||
const char* ffBinaryExtractStrings(const char *peFile, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata, uint32_t minLength)
|
||||
{
|
||||
__attribute__((__cleanup__(UnMapAndLoad))) LOADED_IMAGE loadedImage = {};
|
||||
if (!MapAndLoad(peFile, NULL, &loadedImage, FALSE, TRUE))
|
||||
@@ -26,6 +26,7 @@ const char* ffBinaryExtractStrings(const char *peFile, bool (*cb)(const char *st
|
||||
const char* p = (const char*) data + off;
|
||||
if (*p == '\0') continue;
|
||||
uint32_t len = (uint32_t) strlen(p);
|
||||
if (len < minLength) continue;
|
||||
if (*p >= ' ' && *p <= '~') // Ignore control characters
|
||||
{
|
||||
if (!cb(p, len, userdata)) break;
|
||||
|
||||
Reference in New Issue
Block a user