Skip to content

Commit 6bd7105

Browse files
committed
windows fmemopen implementation
1 parent 4de199d commit 6bd7105

3 files changed

Lines changed: 61 additions & 5 deletions

File tree

software/src/CMakeLists.txt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ cmake_minimum_required (VERSION 3.5)
33
project (mifare C)
44

55
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../script/bin)
6-
set(SRC_DIR ./)
6+
set(SRC_DIR ./) # Assuming source files are in the same directory as CMakeLists.txt
7+
8+
# Define a variable for the compatibility code directory
9+
set(COMPAT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/compat)
710

811
set(COMMON_FILES
912
${SRC_DIR}/common.c
@@ -229,27 +232,37 @@ if (CMAKE_SYSTEM_NAME MATCHES "Windows")
229232
endif()
230233

231234

235+
# --- hardnested Executable ---
232236
add_executable(hardnested ${COMMON_FILES} ${HARDNESTED_SOURCES})
233237
# Add dependency: hardnested depends on the *imported library target*
234-
# CMake handles the transitive dependency: hardnested -> liblzma_imported -> build_liblzma
235238
add_dependencies(hardnested liblzma_imported) # Keep this explicit dependency
236239

237240
target_include_directories(hardnested PRIVATE
238241
${SRC_DIR}
239242
${HARDNESTED_RECOVERY_DIR}
240243
${HARDNESTED_RECOVERY_DIR}/pm3
241244
${HARDNESTED_RECOVERY_DIR}/hardnested
242-
# Include dir now comes from linking liblzma_imported via INTERFACE_INCLUDE_DIRECTORIES
245+
# liblzma include dir comes via INTERFACE property of liblzma_imported
243246
)
244247
target_compile_options(hardnested PRIVATE -Wall)
248+
245249
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
246250
target_compile_definitions(hardnested PRIVATE _GNU_SOURCE)
247251
endif()
252+
248253
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
249-
# NOTE: LZMA_API_STATIC definition was NOT added here, as requested
254+
# --- Add fmemopen compatibility layer for Windows ---
255+
target_sources(hardnested PRIVATE
256+
${COMPAT_DIR}/fmemopen/fmemopen.c # Compile the source file
257+
)
258+
target_include_directories(hardnested PRIVATE
259+
${COMPAT_DIR}/fmemopen # Add include directory for fmemopen.h
260+
)
261+
# --- End fmemopen changes ---
262+
250263
target_compile_definitions(hardnested PRIVATE
251264
HAVE_STRUCT_TIMESPEC
252-
# LZMA_API_STATIC # <-- Kept commented out per request
265+
# LZMA_API_STATIC # Add this back if you get lzma linker errors
253266
)
254267
target_link_libraries(hardnested PRIVATE pthread)
255268
endif()
@@ -262,3 +275,4 @@ target_link_libraries(hardnested PRIVATE
262275

263276
# Set the output directory for all executables at the end
264277
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
278+

software/src/compat/libfmemopen.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <windows.h>
2+
#include <share.h>
3+
#include <io.h>
4+
#include <fcntl.h>
5+
#include <sys/stat.h>
6+
#include "libfmemopen.h"
7+
/* https://github.com/Arryboom/fmemopen_windows */
8+
9+
FILE *fmemopen(void *buf, size_t len, const char *type)
10+
{
11+
int fd;
12+
FILE *fp;
13+
char tp[MAX_PATH - 13];
14+
char fn[MAX_PATH + 1];
15+
int * pfd = &fd;
16+
int retner = -1;
17+
char tfname[] = "MemTF_";
18+
if (!GetTempPathA(sizeof(tp), tp))
19+
return NULL;
20+
if (!GetTempFileNameA(tp, tfname, 0, fn))
21+
return NULL;
22+
retner = _sopen_s(pfd, fn, _O_CREAT | _O_SHORT_LIVED | _O_TEMPORARY | _O_RDWR | _O_BINARY | _O_NOINHERIT, _SH_DENYRW, _S_IREAD | _S_IWRITE);
23+
if (retner != 0)
24+
return NULL;
25+
if (fd == -1)
26+
return NULL;
27+
fp = _fdopen(fd, "wb+");
28+
if (!fp) {
29+
_close(fd);
30+
return NULL;
31+
}
32+
/*File descriptors passed into _fdopen are owned by the returned FILE * stream.If _fdopen is successful, do not call _close on the file descriptor.Calling fclose on the returned FILE * also closes the file descriptor.*/
33+
fwrite(buf, len, 1, fp);
34+
rewind(fp);
35+
return fp;
36+
}

software/src/compat/libfmemopen.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef libfmemopen_windows
2+
#define libfmemopen_windows
3+
#include <stdio.h>
4+
/* https://github.com/Arryboom/fmemopen_windows */
5+
FILE *fmemopen(void *buf, size_t len, const char *type);
6+
#endif

0 commit comments

Comments
 (0)