1
0
mirror of https://git.disroot.org/80486DX2-66/polonium.git synced 2024-12-26 23:20:22 +05:30

support non-POSIX functions fseek and ftell

This commit is contained in:
Intel A80486DX2-66 2024-07-09 14:53:20 +03:00
parent c9bf4d2b45
commit ac8df6d29d
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
4 changed files with 26 additions and 13 deletions

View File

@ -89,6 +89,18 @@ enum configurations {
#define STR_YN_YES "yes" #define STR_YN_YES "yes"
#define STR_YN_NO "no" #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 */ /* macros: lambdas */
#define STRINGIZE(x) #x #define STRINGIZE(x) #x
#define INT2STR(x) STRINGIZE(x) #define INT2STR(x) STRINGIZE(x)

View File

@ -13,7 +13,7 @@
/* structures */ /* structures */
struct _file_boundaries { struct _file_boundaries {
bool invalid_file; bool invalid_file;
off_t start, end; file_offset_t start, end;
}; };
/* typedefs */ /* typedefs */

View File

@ -54,20 +54,21 @@ Corrupter_Result* corrupt_file(Corrupter_Param* param) {
// copy and cast parameters // copy and cast parameters
FILE* file = param->file; FILE* file = param->file;
uint8_t probability = param->probability; 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; uint8_t config = param->config;
// refuse to operate on non-seekable streams // 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; errno = ESPIPE;
result->error = true; result->error = true;
return result; return result;
} }
// determine file size // determine file size
off_t start = 0, end; file_offset_t start = 0, end;
fseeko(file, 0L, SEEK_END); FSEEK_MACRO(file, FILE_OFFSET_C(0), SEEK_END);
end = ftello(file); end = FTELL_MACRO(file);
// determine file boundaries // determine file boundaries
const char* failed_function = NULL; const char* failed_function = NULL;
@ -107,8 +108,8 @@ Corrupter_Result* corrupt_file(Corrupter_Param* param) {
rewind(file); rewind(file);
// iterate over the file contents // iterate over the file contents
for (off_t i = start; i < end; i++) { for (file_offset_t i = start; i < end; i++) {
fseeko(file, i, SEEK_SET); FSEEK_MACRO(file, i, SEEK_SET);
byte byte_value; byte byte_value;
if (fread(&byte_value, sizeof(byte), 1, file) != 1) { if (fread(&byte_value, sizeof(byte), 1, file) != 1) {
result->error = true; result->error = true;
@ -144,7 +145,7 @@ Corrupter_Result* corrupt_file(Corrupter_Param* param) {
damaged_byte = true; damaged_byte = true;
// write the modified byte back to the file // 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) if (fwrite(&byte_value, sizeof(byte), 1, file) == 1)
continue; continue;

View File

@ -59,7 +59,7 @@ file_boundaries_t* determine_boundaries_WAV(FILE* file) {
DETERMINE_BOUNDARIES_FREAD_ERROR_HANDLING; 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) { while (true) {
if (fread(chunk_id, 1, 4, file) != 4 || if (fread(chunk_id, 1, 4, file) != 4 ||
@ -71,12 +71,12 @@ file_boundaries_t* determine_boundaries_WAV(FILE* file) {
reorder_b32(&chunk_size); reorder_b32(&chunk_size);
if (!strncmp(chunk_id, WAV_SUBCHUNK_DATA_ID, 4)) { if (!strncmp(chunk_id, WAV_SUBCHUNK_DATA_ID, 4)) {
boundaries->start = ftello(file); boundaries->start = FTELL_MACRO(file);
boundaries->end = ftello(file) + (off_t) chunk_size; boundaries->end = FTELL_MACRO(file) + (file_offset_t) chunk_size;
break; break;
} }
fseeko(file, chunk_size, SEEK_CUR); FSEEK_MACRO(file, chunk_size, SEEK_CUR);
} }
boundaries->invalid_file = false; boundaries->invalid_file = false;