From 0bfe10c2ed0b4c17fb7d54418c439001b58229d7 Mon Sep 17 00:00:00 2001 From: Pierre Wendling Date: Wed, 7 Jun 2023 16:37:58 -0400 Subject: [PATCH] Add a strndup fallback implementation for Windows. --- src/CMakeLists.txt | 21 ++++++++++++++++++++- src/dupstring.c | 24 ++++++++++++++++++++++++ src/dupstring.h | 9 +++++++++ src/elf-create-argp.c | 4 +++- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/dupstring.c create mode 100644 src/dupstring.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 910b559..021c0d8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -27,6 +27,25 @@ else() set(runtime_destination lib) endif() +# Check if strndup is available, Windows toolchains typically don't provide it +include(CheckCSourceCompiles) +check_c_source_compiles(" +#include +#include +int main() { + free(strndup(\"foo\", 4)); +}" +HAVE_STRNDUP) + +if(HAVE_STRNDUP) + set(STRNDUP_DEF "HAVE_STRNDUP=1") +else() + set(STRNDUP_DEF "HAVE_STRNDUP=0") +endif() + +set_source_files_properties(dupstring.c PROPERTIES + COMPILE_DEFINITIONS ${STRNDUP_DEF}) + add_library(vita-yaml yamltree.c yamltreeutil.c) add_library(vita-export vita-export-parse.c sha256.c) add_library(vita-import vita-import.c vita-import-parse.c) @@ -53,7 +72,7 @@ target_include_directories(vita-import PUBLIC $) add_executable(vita-libs-gen vita-libs-gen.c) -add_executable(vita-elf-create vita-elf-create.c elf-create-argp.c vita-elf.c elf-defs.c sce-elf.c varray.c elf-utils.c yamlemitter.c) +add_executable(vita-elf-create vita-elf-create.c elf-create-argp.c vita-elf.c elf-defs.c sce-elf.c varray.c elf-utils.c yamlemitter.c dupstring.c) add_executable(vita-mksfoex vita-mksfoex.c getopt_long.c) add_executable(vita-make-fself vita-make-fself.c) add_executable(vita-pack-vpk vita-pack-vpk.c) diff --git a/src/dupstring.c b/src/dupstring.c new file mode 100644 index 0000000..0145702 --- /dev/null +++ b/src/dupstring.c @@ -0,0 +1,24 @@ +#include "dupstring.h" + +#include + +#if defined(HAVE_STRNDUP) && HAVE_STRNDUP + +char *dupstring(const char *source, size_t maxlen) { + return strndup(source, maxlen); +} + +#else /* Fallback implementation */ +#include + +char *dupstring(const char *source, size_t maxlen) { + size_t dest_size = strnlen(source, maxlen); + char *dest = malloc(dest_size + 1); + if (dest == NULL) { + return NULL; + } + memcmp(dest, source, dest_size); + dest[dest_size] = '\0'; + return dest; +} +#endif diff --git a/src/dupstring.h b/src/dupstring.h new file mode 100644 index 0000000..2bd5ed1 --- /dev/null +++ b/src/dupstring.h @@ -0,0 +1,9 @@ +#ifndef DUPSTRING_H +#define DUPSTRING_H + +#include + +/* Wrapper around strndup */ +char *dupstring(const char *source, size_t maxlen); + +#endif /* DUPSTRING_H */ diff --git a/src/elf-create-argp.c b/src/elf-create-argp.c index 2c91834..e8d86c0 100644 --- a/src/elf-create-argp.c +++ b/src/elf-create-argp.c @@ -5,6 +5,8 @@ #include #include +#include "dupstring.h" + int parse_arguments(int argc, char *argv[], elf_create_args *arguments) { int c; @@ -76,7 +78,7 @@ int parse_arguments(int argc, char *argv[], elf_create_args *arguments) } size_t entrypoint_length = (size_t)(entrypoint_end - entrypoint); if (entrypoint_length > 0) - arguments->entrypoint_funcs[i] = strndup(entrypoint, entrypoint_length); + arguments->entrypoint_funcs[i] = dupstring(entrypoint, entrypoint_length); i++; entrypoint = entrypoint_end + 1;