From ac8df6d29d109f8f1ea2b22cf1402a4a37059fee Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Tue, 9 Jul 2024 14:53:20 +0300 Subject: [PATCH] support non-POSIX functions `fseek` and `ftell` --- include/common.h | 12 ++++++++++++ include/file_boundaries.h | 2 +- src/corrupter.c | 17 +++++++++-------- src/file_boundaries.c | 8 ++++---- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/include/common.h b/include/common.h index 11e8f4e..a5a0fde 100644 --- a/include/common.h +++ b/include/common.h @@ -89,6 +89,18 @@ enum configurations { #define STR_YN_YES "yes" #define STR_YN_NO "no" +#ifdef HAVE_NO_FSEEKO_FTELLO +# define FSEEK_MACRO fseek +# define FTELL_MACRO ftell +typedef long file_offset_t; +# define FILE_OFFSET_C(x) ((x)##L) +#else +# define FSEEK_MACRO fseeko +# define FTELL_MACRO ftello +typedef off_t file_offset_t; +# define FILE_OFFSET_C(x) ((off_t) x) +#endif + /* macros: lambdas */ #define STRINGIZE(x) #x #define INT2STR(x) STRINGIZE(x) diff --git a/include/file_boundaries.h b/include/file_boundaries.h index 31072ff..bba61dc 100644 --- a/include/file_boundaries.h +++ b/include/file_boundaries.h @@ -13,7 +13,7 @@ /* structures */ struct _file_boundaries { bool invalid_file; - off_t start, end; + file_offset_t start, end; }; /* typedefs */ diff --git a/src/corrupter.c b/src/corrupter.c index 305e08b..2d16445 100644 --- a/src/corrupter.c +++ b/src/corrupter.c @@ -54,20 +54,21 @@ Corrupter_Result* corrupt_file(Corrupter_Param* param) { // copy and cast parameters FILE* file = param->file; uint8_t probability = param->probability; - off_t threshold = (off_t) param->threshold; + file_offset_t threshold = (file_offset_t) param->threshold; uint8_t config = param->config; // refuse to operate on non-seekable streams - if (fseeko(file, 0L, SEEK_SET) != 0 || ftello(file) != 0) { + if (FSEEK_MACRO(file, FILE_OFFSET_C(0), SEEK_SET) != 0 || + FTELL_MACRO(file) != 0) { errno = ESPIPE; result->error = true; return result; } // determine file size - off_t start = 0, end; - fseeko(file, 0L, SEEK_END); - end = ftello(file); + file_offset_t start = 0, end; + FSEEK_MACRO(file, FILE_OFFSET_C(0), SEEK_END); + end = FTELL_MACRO(file); // determine file boundaries const char* failed_function = NULL; @@ -107,8 +108,8 @@ Corrupter_Result* corrupt_file(Corrupter_Param* param) { rewind(file); // iterate over the file contents - for (off_t i = start; i < end; i++) { - fseeko(file, i, SEEK_SET); + for (file_offset_t i = start; i < end; i++) { + FSEEK_MACRO(file, i, SEEK_SET); byte byte_value; if (fread(&byte_value, sizeof(byte), 1, file) != 1) { result->error = true; @@ -144,7 +145,7 @@ Corrupter_Result* corrupt_file(Corrupter_Param* param) { damaged_byte = true; // write the modified byte back to the file - fseeko(file, i, SEEK_SET); + FSEEK_MACRO(file, i, SEEK_SET); if (fwrite(&byte_value, sizeof(byte), 1, file) == 1) continue; diff --git a/src/file_boundaries.c b/src/file_boundaries.c index 4b8b806..542a493 100644 --- a/src/file_boundaries.c +++ b/src/file_boundaries.c @@ -59,7 +59,7 @@ file_boundaries_t* determine_boundaries_WAV(FILE* file) { DETERMINE_BOUNDARIES_FREAD_ERROR_HANDLING; } - fseeko(file, (off_t) chunk_size - 4, SEEK_CUR); + FSEEK_MACRO(file, (file_offset_t) chunk_size - FILE_OFFSET_C(4), SEEK_CUR); while (true) { if (fread(chunk_id, 1, 4, file) != 4 || @@ -71,12 +71,12 @@ file_boundaries_t* determine_boundaries_WAV(FILE* file) { reorder_b32(&chunk_size); if (!strncmp(chunk_id, WAV_SUBCHUNK_DATA_ID, 4)) { - boundaries->start = ftello(file); - boundaries->end = ftello(file) + (off_t) chunk_size; + boundaries->start = FTELL_MACRO(file); + boundaries->end = FTELL_MACRO(file) + (file_offset_t) chunk_size; break; } - fseeko(file, chunk_size, SEEK_CUR); + FSEEK_MACRO(file, chunk_size, SEEK_CUR); } boundaries->invalid_file = false;