From cbecfb1e2238b7c4261074725d25cbb297656c20 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 31 Dec 2020 09:25:43 -0800 Subject: [PATCH] replace naughty fgetpos/poke/fsetpos with fseeko fpos_t is intended to be an opaque type so reaching inside it is not right. Switch to fseeko/ftello which are the off_t equivalents of fseek/ftell. closes: https://github.com/wez/atomicparsley/issues/13 --- CMakeLists.txt | 4 ++++ src/parsley.cpp | 23 +++-------------------- src/util.cpp | 31 +++++++++++++++++-------------- src/util.h | 6 ++++-- 4 files changed, 28 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index af58e3e..0c3e4fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,10 @@ check_symbol_exists(strsep "string.h" HAVE_STRSEP) if(HAVE_STRSEP) add_definitions(-DHAVE_STRSEP) endif() +check_symbol_exists(fseeko "stdio.h" HAVE_FSEEKO) +if(HAVE_FSEEKO) + add_definitions(-DHAVE_FSEEKO) +endif() add_definitions( -DPACKAGE_VERSION="${PACKAGE_VERSION}" diff --git a/src/parsley.cpp b/src/parsley.cpp index e9d49bd..b218f44 100644 --- a/src/parsley.cpp +++ b/src/parsley.cpp @@ -1598,7 +1598,7 @@ void APar_ScanAtoms(const char *path, bool deepscan_REQ) { APar_TestCompatibleBrand(file, 0, dataSize); } - fseek(file, jump, SEEK_SET); + fseeko(file, jump, SEEK_SET); while (jump < file_size) { @@ -5718,31 +5718,14 @@ void APar_MergeTempFile(FILE *dest_file, if (file_pos + max_buffer <= src_file_size) { APar_readX(buffer, src_file, file_pos, max_buffer); - // fseek(dest_file, dest_position + file_pos, SEEK_SET); -#if defined(_WIN32) - fpos_t file_offset = dest_position + file_pos; -#elif defined(__GLIBC__) - fpos_t file_offset = {0}; - file_offset.__pos = dest_position + file_pos; -#else - off_t file_offset = dest_position + file_pos; -#endif - fsetpos(dest_file, &file_offset); + fseeko(dest_file, dest_position + file_pos, SEEK_SET); fwrite(buffer, max_buffer, 1, dest_file); file_pos += max_buffer; } else { APar_readX(buffer, src_file, file_pos, src_file_size - file_pos); // fprintf(stdout, "buff starts with %s\n", buffer+4); -#if defined(_WIN32) - fpos_t file_offset = dest_position + file_pos; -#elif defined(__GLIBC__) - fpos_t file_offset = {0}; - file_offset.__pos = dest_position + file_pos; -#else - off_t file_offset = dest_position + file_pos; -#endif - fsetpos(dest_file, &file_offset); + fseeko(dest_file, dest_position + file_pos, SEEK_SET); fwrite(buffer, src_file_size - file_pos, 1, dest_file); file_pos += src_file_size - file_pos; break; diff --git a/src/util.cpp b/src/util.cpp index 98e302e..cb2c4b6 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -155,26 +155,29 @@ void TestFileExistence(const char *filePath, bool errorOut) { } } -#if defined(_WIN32) - -/////////////////////////////////////////////////////////////////////////////////////// -// Win32 functions // -/////////////////////////////////////////////////////////////////////////////////////// - #ifndef HAVE_FSEEKO +#ifdef _WIN32 +int fseeko(FILE *stream, off_t pos, int whence) { + return _fseeki64(stream, pos, whence); +} -int fseeko(FILE *stream, uint64_t pos, int whence) { // only using SEEK_SET here - if (whence == SEEK_SET) { - fpos_t fpos = pos; - return fsetpos(stream, &fpos); - } else { - return -1; - } - return -1; +off_t ftello(FILE *stream) { return _ftelli64(stream); } + +#else +int fseeko(FILE *stream, off_t pos, int whence) { + return fseek(stream, pos, whence); } +off_t ftello(FILE *stream) { return ftell(stream); } +#endif #endif +#if defined(_WIN32) + +/////////////////////////////////////////////////////////////////////////////////////// +// Win32 functions // +/////////////////////////////////////////////////////////////////////////////////////// + /*---------------------- APar_OpenFileWin32 utf8_filepath - a pointer to a string (possibly utf8) of the full path diff --git a/src/util.h b/src/util.h index 616bbbc..674309a 100644 --- a/src/util.h +++ b/src/util.h @@ -47,10 +47,12 @@ FILE *APar_OpenFile(const char *utf8_filepath, const char *file_flags); FILE *APar_OpenISOBaseMediaFile(const char *file, bool open); // openSomeFile void TestFileExistence(const char *filePath, bool errorOut); -#if defined(_WIN32) #ifndef HAVE_FSEEKO -int fseeko(FILE *stream, uint64_t pos, int whence); +int fseeko(FILE *stream, off_t pos, int whence); +off_t ftello(FILE *stream); #endif + +#if defined(_WIN32) HANDLE APar_OpenFileWin32(const char *utf8_filepath, DWORD dwDesiredAccess, DWORD dwShareMode,