From ba42d6f732d73cc27e10d51b3098ec16a3b4fe80 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 26 Jan 2022 15:18:16 -0800 Subject: [PATCH] Fix missing pread() and pwrite() 1. configure.ac: Check that the DECLs for `pread()` and `pwrite()` exist in unistd.h. 2. port.c: If `pread()` or `pwrite()` aren't available, use the local versions that are wrappers around `seek()` and `read()` and `write()`. --- configure.ac | 1 + src/port.c | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index f386e6f5..6470db81 100644 --- a/configure.ac +++ b/configure.ac @@ -84,6 +84,7 @@ CPPFLAGS="$CPPFLAGS -I${wcpath}/include" AC_CHECK_LIB([wolfssl],[wolfCrypt_Init],,[AC_MSG_ERROR([libwolfssl is required for ${PACKAGE}. It can be obtained from https://www.wolfssl.com/download.html/ .])]) AC_CHECK_FUNCS([gethostbyname getaddrinfo gettimeofday inet_ntoa memset socket wc_ecc_set_rng]) +AC_CHECK_DECLS([[pread],[pwrite]],,[unistd.h]) # DEBUG DEBUG_CFLAGS="-g -O0" diff --git a/src/port.c b/src/port.c index f21ba153..cdb0463e 100644 --- a/src/port.c +++ b/src/port.c @@ -37,6 +37,13 @@ #include #endif +/* +Flags: + WOLFSSH_LOCAL_PREAD_PWRITE + Defined to use local implementations of pread() and pwrite(). Switched + on automatically if pread() or pwrite() aren't found by configure. +*/ + #if !defined(NO_FILESYSTEM) && !defined(WOLFSSH_USER_FILESYSTEM) int wfopen(WFILE** f, const char* filename, const char* mode) @@ -82,6 +89,16 @@ int wfopen(WFILE** f, const char* filename, const char* mode) #endif } + +/* If either pread() or pwrite() are missing, use the local versions. */ +#if (defined(USE_OSE_API) || \ + !defined(HAVE_DECL_PREAD) || (HAVE_DECL_PREAD == 0) || \ + !defined(HAVE_DECL_PWRITE) || (HAVE_DECL_PWRITE == 0)) + #undef WOLFSSH_LOCAL_PREAD_PWRITE + #define WOLFSSH_LOCAL_PREAD_PWRITE +#endif + + #if (defined(WOLFSSH_SFTP) || defined(WOLFSSH_SCP)) && \ !defined(NO_WOLFSSH_SERVER) @@ -90,7 +107,7 @@ int wfopen(WFILE** f, const char* filename, const char* mode) /* This is current inline in the source. */ - #elif defined(USE_OSE_API) + #elif defined(WOLFSSH_LOCAL_PREAD_PWRITE) int wPwrite(WFD fd, unsigned char* buf, unsigned int sz, const unsigned int* shortOffset) @@ -104,7 +121,6 @@ int wfopen(WFILE** f, const char* filename, const char* mode) return ret; } - int wPread(WFD fd, unsigned char* buf, unsigned int sz, const unsigned int* shortOffset) { @@ -117,7 +133,7 @@ int wfopen(WFILE** f, const char* filename, const char* mode) return ret; } - #else /* USE_WINDOWS_API USE_OSE_API */ + #else /* USE_WINDOWS_API WOLFSSH_LOCAL_PREAD_PWRITE */ int wPwrite(WFD fd, unsigned char* buf, unsigned int sz, const unsigned int* shortOffset)