support non-POSIX functions fseek
and ftell
This commit is contained in:
parent
c9bf4d2b45
commit
ac8df6d29d
4 changed files with 26 additions and 13 deletions
|
@ -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)
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
/* structures */
|
||||
struct _file_boundaries {
|
||||
bool invalid_file;
|
||||
off_t start, end;
|
||||
file_offset_t start, end;
|
||||
};
|
||||
|
||||
/* typedefs */
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue