diff --git a/CMakeLists.txt b/CMakeLists.txt index 023c51919..718062d06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -756,7 +756,7 @@ elseif(WIN32) src/util/windows/unicode.c src/util/windows/wmi.cpp src/util/platform/FFPlatform_windows.c - src/util/binary_linux.c + src/util/binary_windows.c ) elseif(SunOS) list(APPEND LIBFASTFETCH_SRC @@ -1139,6 +1139,7 @@ elseif(WIN32) PRIVATE "setupapi" PRIVATE "hid" PRIVATE "wtsapi32" + PRIVATE "imagehlp" ) elseif(FreeBSD) target_link_libraries(libfastfetch diff --git a/src/detection/editor/editor.c b/src/detection/editor/editor.c index 269f567b8..6fd5258c5 100644 --- a/src/detection/editor/editor.c +++ b/src/detection/editor/editor.c @@ -57,15 +57,15 @@ const char* ffDetectEditor(FFEditorResult* result) else { const char* error = ffFindExecutableInPath(result->name.chars, &result->path); - if (error) return error; + if (error) return NULL; } - if (!instance.config.general.detectVersion) return NULL; - char buf[PATH_MAX + 1]; if (!realpath(result->path.chars, buf)) return NULL; + // WIN32: Should we handle scoop shim exe here? + ffStrbufSetS(&result->path, buf); { @@ -88,6 +88,8 @@ const char* ffDetectEditor(FFEditorResult* result) #endif } + if (!instance.config.general.detectVersion) return NULL; + if (ffStrbufEqualS(&result->exe, "nvim")) ffBinaryExtractStrings(buf, extractNvimVersion, &result->version); else if (ffStrbufEqualS(&result->exe, "vim")) diff --git a/src/util/binary_windows.c b/src/util/binary_windows.c new file mode 100644 index 000000000..6582d9e1d --- /dev/null +++ b/src/util/binary_windows.c @@ -0,0 +1,39 @@ +#include "binary.h" +#include "common/io/io.h" +#include "util/stringUtils.h" +#include "util/mallocHelper.h" + +#include +#include +#include +#include + +const char* ffBinaryExtractStrings(const char *peFile, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata) +{ + __attribute__((__cleanup__(UnMapAndLoad))) LOADED_IMAGE loadedImage = {}; + if (!MapAndLoad(peFile, NULL, &loadedImage, FALSE, TRUE)) + return "File could not be loaded"; + + for (ULONG i = 0; i < loadedImage.NumberOfSections; ++i) + { + PIMAGE_SECTION_HEADER section = &loadedImage.Sections[i]; + if ((section->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) && ffStrEquals((const char*) section->Name, ".rdata")) + { + uint8_t *data = (uint8_t *) loadedImage.MappedAddress + section->PointerToRawData; + + for (size_t off = 0; off < section->SizeOfRawData; ++off) + { + const char* p = (const char*) data + off; + if (*p == '\0') continue; + uint32_t len = (uint32_t) strlen(p); + if (*p >= ' ' && *p <= '~') // Ignore control characters + { + if (!cb(p, len, userdata)) break; + } + off += len; + } + } + } + + return NULL; +}