Skip to content

Commit b4aac9d

Browse files
committed
Binary: support Windows
1 parent cf9bd0f commit b4aac9d

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ elseif(WIN32)
756756
src/util/windows/unicode.c
757757
src/util/windows/wmi.cpp
758758
src/util/platform/FFPlatform_windows.c
759-
src/util/binary_linux.c
759+
src/util/binary_windows.c
760760
)
761761
elseif(SunOS)
762762
list(APPEND LIBFASTFETCH_SRC
@@ -1139,6 +1139,7 @@ elseif(WIN32)
11391139
PRIVATE "setupapi"
11401140
PRIVATE "hid"
11411141
PRIVATE "wtsapi32"
1142+
PRIVATE "imagehlp"
11421143
)
11431144
elseif(FreeBSD)
11441145
target_link_libraries(libfastfetch

src/detection/editor/editor.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ const char* ffDetectEditor(FFEditorResult* result)
5757
else
5858
{
5959
const char* error = ffFindExecutableInPath(result->name.chars, &result->path);
60-
if (error) return error;
60+
if (error) return NULL;
6161
}
6262

63-
if (!instance.config.general.detectVersion) return NULL;
64-
6563
char buf[PATH_MAX + 1];
6664
if (!realpath(result->path.chars, buf))
6765
return NULL;
6866

67+
// WIN32: Should we handle scoop shim exe here?
68+
6969
ffStrbufSetS(&result->path, buf);
7070

7171
{
@@ -88,6 +88,8 @@ const char* ffDetectEditor(FFEditorResult* result)
8888
#endif
8989
}
9090

91+
if (!instance.config.general.detectVersion) return NULL;
92+
9193
if (ffStrbufEqualS(&result->exe, "nvim"))
9294
ffBinaryExtractStrings(buf, extractNvimVersion, &result->version);
9395
else if (ffStrbufEqualS(&result->exe, "vim"))

src/util/binary_windows.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "binary.h"
2+
#include "common/io/io.h"
3+
#include "util/stringUtils.h"
4+
#include "util/mallocHelper.h"
5+
6+
#include <windows.h>
7+
#include <imagehlp.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
11+
const char* ffBinaryExtractStrings(const char *peFile, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata)
12+
{
13+
__attribute__((__cleanup__(UnMapAndLoad))) LOADED_IMAGE loadedImage = {};
14+
if (!MapAndLoad(peFile, NULL, &loadedImage, FALSE, TRUE))
15+
return "File could not be loaded";
16+
17+
for (ULONG i = 0; i < loadedImage.NumberOfSections; ++i)
18+
{
19+
PIMAGE_SECTION_HEADER section = &loadedImage.Sections[i];
20+
if ((section->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) && ffStrEquals((const char*) section->Name, ".rdata"))
21+
{
22+
uint8_t *data = (uint8_t *) loadedImage.MappedAddress + section->PointerToRawData;
23+
24+
for (size_t off = 0; off < section->SizeOfRawData; ++off)
25+
{
26+
const char* p = (const char*) data + off;
27+
if (*p == '\0') continue;
28+
uint32_t len = (uint32_t) strlen(p);
29+
if (*p >= ' ' && *p <= '~') // Ignore control characters
30+
{
31+
if (!cb(p, len, userdata)) break;
32+
}
33+
off += len;
34+
}
35+
}
36+
}
37+
38+
return NULL;
39+
}

0 commit comments

Comments
 (0)