diff --git a/include/internal.h b/include/internal.h index c89a743..e9dd014 100644 --- a/include/internal.h +++ b/include/internal.h @@ -41,11 +41,14 @@ typedef int64_t aaru_off_t; +/* Seek using a 64-bit offset. Returns 0 on success, non-zero on failure, just + * like fseek()/_fseeki64(). Use aaruf_ftell() to retrieve the current offset. */ static inline int aaruf_fseek(FILE *stream, aaru_off_t offset, int origin) { return AARU_FSEEK(stream, offset, origin); } +/* Return the current file offset as a signed 64-bit value. */ static inline aaru_off_t aaruf_ftell(FILE *stream) { return (aaru_off_t)AARU_FTELL(stream); diff --git a/src/blocks/checksum.c b/src/blocks/checksum.c index 16427e4..eb91451 100644 --- a/src/blocks/checksum.c +++ b/src/blocks/checksum.c @@ -57,8 +57,8 @@ void process_checksum_block(aaruformat_context *ctx, const IndexEntry *entry) } // Seek to block - seek_result = fseek(ctx->imageStream, entry->offset, SEEK_SET); - if(seek_result < 0 || ftell(ctx->imageStream) != entry->offset) + seek_result = aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET); + if(seek_result < 0 || aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset); diff --git a/src/blocks/data.c b/src/blocks/data.c index cca5a3e..0c3972c 100644 --- a/src/blocks/data.c +++ b/src/blocks/data.c @@ -91,8 +91,8 @@ int32_t process_data_block(aaruformat_context *ctx, IndexEntry *entry) } // Seek to block - pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); - if(pos < 0 || ftell(ctx->imageStream) != entry->offset) + pos = aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET); + if(pos < 0 || aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset); diff --git a/src/blocks/dump.c b/src/blocks/dump.c index 3225710..2f085dc 100644 --- a/src/blocks/dump.c +++ b/src/blocks/dump.c @@ -124,7 +124,8 @@ void process_dumphw_block(aaruformat_context *ctx, const IndexEntry *entry) return; } - if(fseek(ctx->imageStream, entry->offset, SEEK_SET) < 0 || ftell(ctx->imageStream) != entry->offset) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET) < 0 || + aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset); reset_dump_hardware_context(ctx); @@ -201,7 +202,7 @@ void process_dumphw_block(aaruformat_context *ctx, const IndexEntry *entry) free(payload); - if(fseek(ctx->imageStream, -(aaru_off_t)payload_length, SEEK_CUR) != 0) + if(aaruf_fseek(ctx->imageStream, -(aaru_off_t)payload_length, SEEK_CUR) != 0) { TRACE("Could not rewind after CRC verification"); reset_dump_hardware_context(ctx); diff --git a/src/blocks/flux.c b/src/blocks/flux.c index 17eb5d3..fce1a91 100644 --- a/src/blocks/flux.c +++ b/src/blocks/flux.c @@ -335,8 +335,8 @@ void process_flux_data_block(aaruformat_context *ctx, const IndexEntry *entry) memset(&ctx->flux_data_header, 0, sizeof(FluxHeader)); // Seek to block - pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); - if(pos < 0 || ftell(ctx->imageStream) != entry->offset) + pos = aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET); + if(pos < 0 || aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); return; @@ -851,14 +851,14 @@ static const FluxEntry *find_flux_entry_by_key(const aaruformat_context *ctx, co static int32_t read_flux_payload_header(const aaruformat_context *ctx, uint64_t payload_offset, DataStreamPayloadHeader *header) { - if(fseek(ctx->imageStream, payload_offset, SEEK_SET) < 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)payload_offset, SEEK_SET) < 0) { FATAL("Could not seek to flux payload at offset %" PRIu64, payload_offset); TRACE("Exiting read_flux_payload_header() = AARUF_ERROR_CANNOT_READ_BLOCK\n"); return AARUF_ERROR_CANNOT_READ_BLOCK; } - aaru_off_t file_position = ftell(ctx->imageStream); + aaru_off_t file_position = aaruf_ftell(ctx->imageStream); if(file_position < 0 || (uint64_t)file_position != payload_offset) { FATAL("Invalid flux payload position (expected %" PRIu64 ", got %ld)", payload_offset, file_position); diff --git a/src/blocks/metadata.c b/src/blocks/metadata.c index 2a70063..b0ec145 100644 --- a/src/blocks/metadata.c +++ b/src/blocks/metadata.c @@ -49,8 +49,8 @@ void process_metadata_block(aaruformat_context *ctx, const IndexEntry *entry) // Seek to block TRACE("Seeking to metadata block at position %" PRIu64, entry->offset); - pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); - if(pos < 0 || ftell(ctx->imageStream) != entry->offset) + pos = aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET); + if(pos < 0 || aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset); @@ -96,7 +96,7 @@ void process_metadata_block(aaruformat_context *ctx, const IndexEntry *entry) TRACE("Reading metadata block of size %u at position %" PRIu64, ctx->metadata_block_header.blockSize + sizeof(MetadataBlockHeader), entry->offset); - fseek(ctx->imageStream, entry->offset, SEEK_SET); + aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET); read_bytes = fread(ctx->metadata_block, 1, ctx->metadata_block_header.blockSize + sizeof(MetadataBlockHeader), ctx->imageStream); @@ -264,7 +264,7 @@ void process_geometry_block(aaruformat_context *ctx, const IndexEntry *entry) } // Seek to block - if(fseek(ctx->imageStream, entry->offset, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET) != 0) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset); @@ -326,8 +326,8 @@ void process_cicm_block(aaruformat_context *ctx, const IndexEntry *entry) // Seek to block TRACE("Seeking to CICM XML metadata block at position %" PRIu64, entry->offset); - pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); - if(pos < 0 || ftell(ctx->imageStream) != entry->offset) + pos = aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET); + if(pos < 0 || aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset); @@ -490,8 +490,8 @@ void process_aaru_metadata_json_block(aaruformat_context *ctx, const IndexEntry // Seek to block TRACE("Seeking to Aaru metadata JSON block at position %" PRIu64, entry->offset); - pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); - if(pos < 0 || ftell(ctx->imageStream) != entry->offset) + pos = aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET); + if(pos < 0 || aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset); diff --git a/src/blocks/optical.c b/src/blocks/optical.c index 8203668..e02193e 100644 --- a/src/blocks/optical.c +++ b/src/blocks/optical.c @@ -124,8 +124,8 @@ void process_tracks_block(aaruformat_context *ctx, const IndexEntry *entry) } // Seek to block - pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); - if(pos < 0 || ftell(ctx->imageStream) != entry->offset) + pos = aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET); + if(pos < 0 || aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); diff --git a/src/blocks/tape.c b/src/blocks/tape.c index 4500f23..87055a4 100644 --- a/src/blocks/tape.c +++ b/src/blocks/tape.c @@ -138,8 +138,8 @@ void process_tape_files_block(aaruformat_context *ctx, const IndexEntry *entry) } // Seek to block - pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); - if(pos < 0 || ftell(ctx->imageStream) != entry->offset) + pos = aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET); + if(pos < 0 || aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset); @@ -359,8 +359,8 @@ void process_tape_partitions_block(aaruformat_context *ctx, const IndexEntry *en } // Seek to block - pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); - if(pos < 0 || ftell(ctx->imageStream) != entry->offset) + pos = aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET); + if(pos < 0 || aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset); diff --git a/src/close_write.c b/src/close_write.c index 1b7b06e..c0fa815 100644 --- a/src/close_write.c +++ b/src/close_write.c @@ -77,8 +77,8 @@ static int32_t write_cached_secondary_ddt(aaruformat_context *ctx) TRACE("Writing cached secondary DDT table to file"); - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t end_of_file = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t end_of_file = aaruf_ftell(ctx->imageStream); // Align the position according to block alignment shift uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; @@ -88,11 +88,11 @@ static int32_t write_cached_secondary_ddt(aaruformat_context *ctx) uint64_t aligned_position = end_of_file + alignment_mask & ~alignment_mask; // Seek to the aligned position and pad with zeros if necessary - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, (aaru_off_t)aligned_position, SEEK_SET); end_of_file = aligned_position; TRACE("Aligned DDT write position from %ld to %" PRIu64 " (alignment shift: %d)", - ftell(ctx->imageStream) - (aligned_position - end_of_file), aligned_position, + aaruf_ftell(ctx->imageStream) - (aligned_position - end_of_file), aligned_position, ctx->user_data_ddt_header.blockAlignmentShift); } @@ -241,8 +241,8 @@ static int32_t write_cached_secondary_ddt(aaruformat_context *ctx) TRACE("Added new DDT index entry at offset %" PRIu64, end_of_file); // Write the updated primary table back to its original position in the file - aaru_off_t saved_pos = ftell(ctx->imageStream); - fseek(ctx->imageStream, ctx->primary_ddt_offset + sizeof(DdtHeader2), SEEK_SET); + aaru_off_t saved_pos = aaruf_ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, (aaru_off_t)(ctx->primary_ddt_offset + sizeof(DdtHeader2)), SEEK_SET); size_t primary_table_size = ctx->user_data_ddt_header.entries * sizeof(uint64_t); @@ -255,7 +255,7 @@ static int32_t write_cached_secondary_ddt(aaruformat_context *ctx) return AARUF_ERROR_CANNOT_WRITE_HEADER; } - fseek(ctx->imageStream, saved_pos, SEEK_SET); + aaruf_fseek(ctx->imageStream, saved_pos, SEEK_SET); } else TRACE("Failed to write cached secondary DDT data"); @@ -269,7 +269,7 @@ static int32_t write_cached_secondary_ddt(aaruformat_context *ctx) ctx->cached_ddt_offset = 0; // Set position - fseek(ctx->imageStream, 0, SEEK_END); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); if(ddt_header.compression != kCompressionNone) free(buffer); @@ -329,7 +329,7 @@ static int32_t write_primary_ddt(aaruformat_context *ctx) aaruf_crc64_free(crc64_context); // First write the DDT header - fseek(ctx->imageStream, ctx->primary_ddt_offset, SEEK_SET); + aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->primary_ddt_offset, SEEK_SET); size_t headerWritten = fwrite(&ctx->user_data_ddt_header, sizeof(DdtHeader2), 1, ctx->imageStream); if(headerWritten != 1) @@ -485,14 +485,14 @@ static int32_t write_single_level_ddt(aaruformat_context *ctx) ctx->user_data_ddt_header.cmpLength += LZMA_PROPERTIES_LENGTH; // Write the DDT header first - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t ddt_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t ddt_position = aaruf_ftell(ctx->imageStream); // Align index position to block boundary if needed const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(ddt_position & alignment_mask) { const uint64_t aligned_position = ddt_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); ddt_position = aligned_position; } @@ -759,19 +759,19 @@ static void write_checksum_block(aaruformat_context *ctx) ChecksumHeader checksum_header = {0}; checksum_header.identifier = ChecksumBlock; - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t checksum_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t checksum_position = aaruf_ftell(ctx->imageStream); // Align index position to block boundary if needed alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(checksum_position & alignment_mask) { aligned_position = checksum_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); checksum_position = aligned_position; } // Skip checksum_header - fseek(ctx->imageStream, sizeof(checksum_header), SEEK_CUR); + aaruf_fseek(ctx->imageStream, sizeof(checksum_header), SEEK_CUR); if(ctx->checksums.hasMd5) { @@ -834,7 +834,7 @@ static void write_checksum_block(aaruformat_context *ctx) ctx->header.featureCompatible |= AARU_FEATURE_RW_BLAKE3; } - fseek(ctx->imageStream, checksum_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, checksum_position, SEEK_SET); TRACE("Writing checksum header"); fwrite(&checksum_header, sizeof(ChecksumHeader), 1, ctx->imageStream); @@ -877,14 +877,14 @@ static void write_tracks_block(aaruformat_context *ctx) // Write tracks block if(ctx->tracks_header.entries <= 0 || ctx->track_entries == NULL) return; - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t tracks_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t tracks_position = aaruf_ftell(ctx->imageStream); // Align index position to block boundary if needed uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(tracks_position & alignment_mask) { uint64_t aligned_position = tracks_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); tracks_position = aligned_position; } @@ -943,14 +943,14 @@ static void write_mode2_subheaders_block(aaruformat_context *ctx) // Write MODE 2 subheader data block if(ctx->mode2_subheaders == NULL) return; - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t mode2_subheaders_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t mode2_subheaders_position = aaruf_ftell(ctx->imageStream); // Align index position to block boundary if needed uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(mode2_subheaders_position & alignment_mask) { uint64_t aligned_position = mode2_subheaders_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); mode2_subheaders_position = aligned_position; } @@ -1089,14 +1089,14 @@ static void write_sector_prefix(aaruformat_context *ctx) { if(ctx->sector_prefix == NULL) return; - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t prefix_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t prefix_position = aaruf_ftell(ctx->imageStream); // Align index position to block boundary if needed uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(prefix_position & alignment_mask) { uint64_t aligned_position = prefix_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); prefix_position = aligned_position; } @@ -1242,14 +1242,14 @@ static void write_sector_suffix(aaruformat_context *ctx) { if(ctx->sector_suffix == NULL) return; - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t suffix_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t suffix_position = aaruf_ftell(ctx->imageStream); // Align index position to block boundary if needed const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(suffix_position & alignment_mask) { const uint64_t aligned_position = suffix_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); suffix_position = aligned_position; } @@ -1391,14 +1391,14 @@ static void write_sector_prefix_ddt(aaruformat_context *ctx) { if(ctx->sector_prefix_ddt2 == NULL) return; - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t prefix_ddt_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t prefix_ddt_position = aaruf_ftell(ctx->imageStream); // Align index position to block boundary if needed const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(prefix_ddt_position & alignment_mask) { const uint64_t aligned_position = prefix_ddt_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); prefix_ddt_position = aligned_position; } @@ -1568,14 +1568,14 @@ static void write_sector_suffix_ddt(aaruformat_context *ctx) { if(ctx->sector_suffix_ddt2 == NULL) return; - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t suffix_ddt_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t suffix_ddt_position = aaruf_ftell(ctx->imageStream); // Align index position to block boundary if needed const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(suffix_ddt_position & alignment_mask) { const uint64_t aligned_position = suffix_ddt_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); suffix_ddt_position = aligned_position; } @@ -1759,14 +1759,14 @@ static void write_sector_subchannel(aaruformat_context *ctx) { if(ctx->sector_subchannel == NULL) return; - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t block_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t block_position = aaruf_ftell(ctx->imageStream); // Align index position to block boundary if needed const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(block_position & alignment_mask) { const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); block_position = aligned_position; } @@ -2110,13 +2110,13 @@ static void write_dvd_long_sector_blocks(aaruformat_context *ctx) ctx->user_data_ddt_header.negative + ctx->image_info.Sectors + ctx->user_data_ddt_header.overflow; // Write DVD sector ID block - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t id_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t id_position = aaruf_ftell(ctx->imageStream); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(id_position & alignment_mask) { const uint64_t aligned_position = id_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); id_position = aligned_position; } TRACE("Writing DVD sector ID block at position %ld", id_position); @@ -2226,12 +2226,12 @@ static void write_dvd_long_sector_blocks(aaruformat_context *ctx) if(id_block.compression != kCompressionNone) free(buffer); // Write DVD sector IED block - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t ied_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t ied_position = aaruf_ftell(ctx->imageStream); if(ied_position & alignment_mask) { const uint64_t aligned_position = ied_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); ied_position = aligned_position; } TRACE("Writing DVD sector IED block at position %ld", ied_position); @@ -2339,12 +2339,12 @@ static void write_dvd_long_sector_blocks(aaruformat_context *ctx) if(ied_block.compression != kCompressionNone) free(buffer); // Write DVD sector CPR/MAI block - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t cpr_mai_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t cpr_mai_position = aaruf_ftell(ctx->imageStream); if(cpr_mai_position & alignment_mask) { const uint64_t aligned_position = cpr_mai_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); cpr_mai_position = aligned_position; } TRACE("Writing DVD sector CPR/MAI block at position %ld", cpr_mai_position); @@ -2452,12 +2452,12 @@ static void write_dvd_long_sector_blocks(aaruformat_context *ctx) if(cpr_mai_block.compression != kCompressionNone) free(buffer); // Write DVD sector EDC block - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t edc_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t edc_position = aaruf_ftell(ctx->imageStream); if(edc_position & alignment_mask) { const uint64_t aligned_position = edc_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); edc_position = aligned_position; } TRACE("Writing DVD sector EDC block at position %ld", edc_position); @@ -2666,13 +2666,13 @@ static void write_dvd_title_key_decrypted_block(aaruformat_context *ctx) { if(ctx->sector_decrypted_title_key == NULL) return; - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t block_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t block_position = aaruf_ftell(ctx->imageStream); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(block_position & alignment_mask) { const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); block_position = aligned_position; } TRACE("Writing DVD decrypted title key block at position %ld", block_position); @@ -2868,13 +2868,13 @@ static void write_media_tags(aaruformat_context *ctx) HASH_ITER(hh, ctx->mediaTags, media_tag, tmp_media_tag) { - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t tag_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t tag_position = aaruf_ftell(ctx->imageStream); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(tag_position & alignment_mask) { const uint64_t aligned_position = tag_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); tag_position = aligned_position; } @@ -3040,7 +3040,7 @@ static void write_media_tags(aaruformat_context *ctx) * * **Alignment Strategy:** * Before writing, the file position is: - * 1. Moved to EOF using fseek(SEEK_END) + * 1. Moved to EOF using aaruf_fseek(SEEK_END) * 2. Aligned forward to next boundary: (position + alignment_mask) & ~alignment_mask * 3. Where alignment_mask = (1 << blockAlignmentShift) - 1 * This ensures the tape file block starts on a properly aligned offset for efficient @@ -3184,13 +3184,13 @@ static void write_tape_file_block(aaruformat_context *ctx) tape_file_block.crc64 = aaruf_crc64_data((uint8_t *)buffer, (uint32_t)tape_file_block.length); // Write tape file block to file, block aligned - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t block_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t block_position = aaruf_ftell(ctx->imageStream); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(block_position & alignment_mask) { const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); block_position = aligned_position; } TRACE("Writing tape file block at position %ld", block_position); @@ -3281,7 +3281,7 @@ static void write_tape_file_block(aaruformat_context *ctx) * * **Alignment Strategy:** * Before writing, the file position is: - * 1. Moved to EOF using fseek(SEEK_END) + * 1. Moved to EOF using aaruf_fseek(SEEK_END) * 2. Aligned forward to next boundary: (position + alignment_mask) & ~alignment_mask * 3. Where alignment_mask = (1 << blockAlignmentShift) - 1 * This ensures the tape partition block starts on a properly aligned offset for efficient @@ -3430,13 +3430,13 @@ static void write_tape_partition_block(aaruformat_context *ctx) tape_partition_block.crc64 = aaruf_crc64_data((uint8_t *)buffer, (uint32_t)tape_partition_block.length); // Write tape partition block to partition, block aligned - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t block_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t block_position = aaruf_ftell(ctx->imageStream); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(block_position & alignment_mask) { const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); block_position = aligned_position; } TRACE("Writing tape partition block at position %ld", block_position); @@ -3501,7 +3501,7 @@ static void write_tape_partition_block(aaruformat_context *ctx) * the media type or explicitly stored elsewhere in the image). * * Alignment strategy: - * - The write position is obtained via fseek(SEEK_END) + ftell(). + * - The write position is obtained via aaruf_fseek(SEEK_END) + aaruf_ftell(). * - If the position is not aligned to (1 << blockAlignmentShift), it is advanced to the next * aligned boundary by computing: (position + alignment_mask) & ~alignment_mask. * - This ensures the geometry block starts on a block-aligned offset for efficient access. @@ -3539,13 +3539,13 @@ static void write_geometry_block(aaruformat_context *ctx) { if(ctx->geometry_block.identifier != GeometryBlock) return; - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t block_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t block_position = aaruf_ftell(ctx->imageStream); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(block_position & alignment_mask) { const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); block_position = aligned_position; } @@ -3793,13 +3793,13 @@ static void write_metadata_block(aaruformat_context *ctx) ctx->metadata_block_header.driveFirmwareRevisionOffset = pos; } - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t block_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t block_position = aaruf_ftell(ctx->imageStream); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(block_position & alignment_mask) { const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); block_position = aligned_position; } @@ -4091,13 +4091,13 @@ static void write_dumphw_block(aaruformat_context *ctx) // Copy header memcpy(buffer, &ctx->dump_hardware_header, sizeof(DumpHardwareHeader)); - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t block_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t block_position = aaruf_ftell(ctx->imageStream); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(block_position & alignment_mask) { const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); block_position = aligned_position; } TRACE("Writing dump hardware block at position %ld", block_position); @@ -4230,14 +4230,14 @@ static void write_cicm_block(aaruformat_context *ctx) if(ctx->cicm_block == NULL || ctx->cicm_block_header.length == 0 || ctx->cicm_block_header.identifier != CicmBlock) return; - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t block_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t block_position = aaruf_ftell(ctx->imageStream); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(block_position & alignment_mask) { const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); block_position = aligned_position; } @@ -4381,14 +4381,14 @@ static void write_aaru_json_block(aaruformat_context *ctx) ctx->json_block_header.identifier != AaruMetadataJsonBlock) return; - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t block_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t block_position = aaruf_ftell(ctx->imageStream); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(block_position & alignment_mask) { const uint64_t aligned_position = block_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); block_position = aligned_position; } @@ -4478,7 +4478,7 @@ static void write_aaru_json_block(aaruformat_context *ctx) * * **Alignment Strategy:** * Before writing, the file position is: - * 1. Moved to EOF using fseek(SEEK_END) + * 1. Moved to EOF using aaruf_fseek(SEEK_END) * 2. Aligned forward to next boundary: (position + alignment_mask) & ~alignment_mask * 3. Where alignment_mask = (1 << blockAlignmentShift) - 1 * This ensures the flux payload block starts on a properly aligned offset for efficient @@ -4729,13 +4729,13 @@ static int32_t write_flux_capture_payload(aaruformat_context *ctx, FluxCaptureRe } // Align stream position to block boundary - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t payload_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t payload_position = aaruf_ftell(ctx->imageStream); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(payload_position & alignment_mask) { const uint64_t aligned_position = payload_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); payload_position = aligned_position; } @@ -4855,7 +4855,7 @@ static int32_t write_flux_capture_payload(aaruformat_context *ctx, FluxCaptureRe * * **Alignment Strategy:** * Before writing the FluxDataBlock, the file position is: - * 1. Moved to EOF using fseek(SEEK_END) + * 1. Moved to EOF using aaruf_fseek(SEEK_END) * 2. Aligned forward to next boundary: (position + alignment_mask) & ~alignment_mask * 3. Where alignment_mask = (1 << blockAlignmentShift) - 1 * This ensures the flux data block starts on a properly aligned offset for efficient @@ -5075,13 +5075,13 @@ static int32_t write_flux_blocks(aaruformat_context *ctx) capture_count == 0 ? 0 : aaruf_crc64_data((const uint8_t *)entries, capture_count * sizeof(FluxEntry)); // Align stream position to block boundary - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t metadata_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t metadata_position = aaruf_ftell(ctx->imageStream); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(metadata_position & alignment_mask) { const uint64_t aligned_position = metadata_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); metadata_position = aligned_position; } @@ -5145,15 +5145,15 @@ static int32_t write_index_block(aaruformat_context *ctx) { // Write the complete index at the end of the file TRACE("Writing index at the end of the file"); - fseek(ctx->imageStream, 0, SEEK_END); - aaru_off_t index_position = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + aaru_off_t index_position = aaruf_ftell(ctx->imageStream); // Align index position to block boundary if needed uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; if(index_position & alignment_mask) { uint64_t aligned_position = index_position + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, aligned_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, aligned_position, SEEK_SET); index_position = aligned_position; TRACE("Aligned index position to %" PRIu64, aligned_position); } @@ -5215,7 +5215,7 @@ static int32_t write_index_block(aaruformat_context *ctx) TRACE("Updating header with index offset: %" PRIu64, ctx->header.indexOffset); // Seek back to beginning and rewrite header - fseek(ctx->imageStream, 0, SEEK_SET); + aaruf_fseek(ctx->imageStream, 0, SEEK_SET); if(fwrite(&ctx->header, sizeof(AaruHeaderV2), 1, ctx->imageStream) == 1) TRACE("Successfully updated header with index offset"); else @@ -5261,7 +5261,7 @@ int32_t aaruf_finalize_write(aaruformat_context *ctx) TRACE("Seeking to start of image"); // Write the header at the beginning of the file - fseek(ctx->imageStream, 0, SEEK_SET); + aaruf_fseek(ctx->imageStream, 0, SEEK_SET); TRACE("Writing header at position 0"); if(fwrite(&ctx->header, sizeof(AaruHeaderV2), 1, ctx->imageStream) != 1) diff --git a/src/create.c b/src/create.c index ae4e4e0..090f564 100644 --- a/src/create.c +++ b/src/create.c @@ -511,7 +511,7 @@ AARU_EXPORT void *AARU_CALL aaruf_create(const char *filepath, const uint32_t me TRACE("Data blocks will start at position %" PRIu64, ctx->next_block_position); // Position file pointer at the data start position - if(fseek(ctx->imageStream, ctx->next_block_position, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->next_block_position, SEEK_SET) != 0) { FATAL("Could not seek to data start position"); TRACE("Exiting aaruf_create() = NULL"); diff --git a/src/ddt/ddt_v1.c b/src/ddt/ddt_v1.c index 037c259..c4f275d 100644 --- a/src/ddt/ddt_v1.c +++ b/src/ddt/ddt_v1.c @@ -107,8 +107,8 @@ int32_t process_ddt_v1(aaruformat_context *ctx, IndexEntry *entry, bool *found_u // Seek to block TRACE("Seeking to DDT block at position %" PRIu64, entry->offset); - pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); - if(pos < 0 || ftell(ctx->imageStream) != entry->offset) + pos = aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET); + if(pos < 0 || aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset); diff --git a/src/ddt/ddt_v2.c b/src/ddt/ddt_v2.c index 5d150e0..f1500c1 100644 --- a/src/ddt/ddt_v2.c +++ b/src/ddt/ddt_v2.c @@ -50,7 +50,7 @@ * @retval AARUF_ERROR_NOT_AARUFORMAT (-1) The context or image stream is invalid (NULL pointers). * * @retval AARUF_ERROR_CANNOT_READ_BLOCK (-7) Failed to access the DDT block in the image stream. This occurs when: - * - fseek() fails to position at the DDT block offset + * - aaruf_fseek() fails to position at the DDT block offset * - The file position doesn't match the expected offset after seeking * - Failed to read the DDT header from the image stream * - The number of bytes read for the DDT header is insufficient @@ -118,8 +118,8 @@ int32_t process_ddt_v2(aaruformat_context *ctx, IndexEntry *entry, bool *found_u } // Seek to block - pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); - if(pos < 0 || ftell(ctx->imageStream) != entry->offset) + pos = aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET); + if(pos < 0 || aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset); @@ -927,7 +927,7 @@ int32_t decode_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_addre if(ctx->cached_ddt_offset != secondary_ddt_offset) { int32_t error_no = 0; - fseek(ctx->imageStream, secondary_ddt_offset, SEEK_SET); + aaruf_fseek(ctx->imageStream, (aaru_off_t)secondary_ddt_offset, SEEK_SET); DdtHeader2 ddt_header; size_t read_bytes = fread(&ddt_header, 1, sizeof(DdtHeader2), ctx->imageStream); @@ -1435,13 +1435,13 @@ bool set_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_address, bo if(ctx->writing_buffer != NULL) aaruf_close_current_block(ctx); // Get current position and seek to end of file - fseek(ctx->imageStream, 0, SEEK_END); - end_of_file = ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + end_of_file = aaruf_ftell(ctx->imageStream); // Align to block boundary uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; end_of_file = end_of_file + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, end_of_file, SEEK_SET); + aaruf_fseek(ctx->imageStream, end_of_file, SEEK_SET); // Prepare DDT header for the never-written cached table memset(&ddt_header, 0, sizeof(DdtHeader2)); @@ -1580,8 +1580,8 @@ bool set_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_address, bo ctx->dirty_primary_ddt = true; // Mark primary DDT as dirty // Write the updated primary table back to its original position in the file - aaru_off_t saved_pos = ftell(ctx->imageStream); - fseek(ctx->imageStream, ctx->primary_ddt_offset + sizeof(DdtHeader2), SEEK_SET); + aaru_off_t saved_pos = aaruf_ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, (aaru_off_t)(ctx->primary_ddt_offset + sizeof(DdtHeader2)), SEEK_SET); size_t primary_table_size = ctx->user_data_ddt_header.entries * sizeof(uint64_t); @@ -1611,7 +1611,7 @@ bool set_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_address, bo ctx->cached_ddt_position = 0; // Restore file position - fseek(ctx->imageStream, saved_pos, SEEK_SET); + aaruf_fseek(ctx->imageStream, saved_pos, SEEK_SET); TRACE("Successfully wrote never-written cached secondary DDT to disk"); } @@ -1624,19 +1624,19 @@ bool set_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_address, bo // Step 3: Write the currently in-memory cached secondary level table to the end of the file if(ctx->cached_ddt_offset != 0) { - long current_pos = 0; + aaru_off_t current_pos = 0; // Close the current data block first if(ctx->writing_buffer != NULL) aaruf_close_current_block(ctx); // Get current position and seek to end of file - current_pos = ftell(ctx->imageStream); - fseek(ctx->imageStream, 0, SEEK_END); - end_of_file = ftell(ctx->imageStream); + current_pos = aaruf_ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); + end_of_file = aaruf_ftell(ctx->imageStream); // Align to block boundary uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; end_of_file = end_of_file + alignment_mask & ~alignment_mask; - fseek(ctx->imageStream, end_of_file, SEEK_SET); + aaruf_fseek(ctx->imageStream, end_of_file, SEEK_SET); // Prepare DDT header for the cached table memset(&ddt_header, 0, sizeof(DdtHeader2)); @@ -1802,8 +1802,8 @@ bool set_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_address, bo ctx->dirty_primary_ddt = true; // Mark primary DDT as dirty // Write the updated primary table back to its original position in the file - aaru_off_t saved_pos = ftell(ctx->imageStream); - fseek(ctx->imageStream, ctx->primary_ddt_offset + sizeof(DdtHeader2), SEEK_SET); + aaru_off_t saved_pos = aaruf_ftell(ctx->imageStream); + aaruf_fseek(ctx->imageStream, (aaru_off_t)(ctx->primary_ddt_offset + sizeof(DdtHeader2)), SEEK_SET); size_t primary_table_size = ctx->user_data_ddt_header.entries * sizeof(uint64_t); @@ -1823,7 +1823,7 @@ bool set_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_address, bo offset = 0; TRACE("Updated nextBlockPosition after DDT write to %" PRIu64, ctx->next_block_position); - fseek(ctx->imageStream, saved_pos, SEEK_SET); + aaruf_fseek(ctx->imageStream, saved_pos, SEEK_SET); // Free the cached table @@ -1831,7 +1831,7 @@ bool set_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_address, bo ctx->cached_secondary_ddt2 = NULL; // Restore file position - fseek(ctx->imageStream, current_pos, SEEK_SET); + aaruf_fseek(ctx->imageStream, current_pos, SEEK_SET); } // Step 5: Check if the specified block already has an existing secondary level table @@ -1840,7 +1840,7 @@ bool set_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_address, bo if(!create_new_table && secondary_ddt_offset != 0) { // Load existing table - fseek(ctx->imageStream, secondary_ddt_offset, SEEK_SET); + aaruf_fseek(ctx->imageStream, (aaru_off_t)secondary_ddt_offset, SEEK_SET); size_t read_bytes = fread(&ddt_header, 1, sizeof(DdtHeader2), ctx->imageStream); if(read_bytes != sizeof(DdtHeader2) || ddt_header.identifier != DeDuplicationTable2 || diff --git a/src/identify.c b/src/identify.c index d7f4553..2c13aef 100644 --- a/src/identify.c +++ b/src/identify.c @@ -166,7 +166,7 @@ AARU_EXPORT int AARU_CALL aaruf_identify_stream(FILE *image_stream) { if(image_stream == NULL) return 0; - if(fseek(image_stream, 0, SEEK_SET) != 0) return 0; + if(aaruf_fseek(image_stream, 0, SEEK_SET) != 0) return 0; AaruHeader header; diff --git a/src/index/index_v1.c b/src/index/index_v1.c index 0559cf3..4da4692 100644 --- a/src/index/index_v1.c +++ b/src/index/index_v1.c @@ -100,7 +100,7 @@ UT_array *process_index_v1(aaruformat_context *ctx) // Read the index header TRACE("Reading index header at position %llu", ctx->header.indexOffset); - if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->header.indexOffset, SEEK_SET) != 0) { FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset); utarray_free(index_entries); @@ -242,7 +242,7 @@ int32_t verify_index_v1(aaruformat_context *ctx) // This will traverse all blocks and check their CRC64 without uncompressing them TRACE("Checking index integrity at %llu.", ctx->header.indexOffset); - if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->header.indexOffset, SEEK_SET) != 0) { FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset); diff --git a/src/index/index_v2.c b/src/index/index_v2.c index 12d318c..307e8dd 100644 --- a/src/index/index_v2.c +++ b/src/index/index_v2.c @@ -102,7 +102,7 @@ UT_array *process_index_v2(aaruformat_context *ctx) // Read the index header TRACE("Reading index header at position %llu", ctx->header.indexOffset); - if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->header.indexOffset, SEEK_SET) != 0) { FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset); utarray_free(index_entries); @@ -244,7 +244,7 @@ int32_t verify_index_v2(aaruformat_context *ctx) // This will traverse all blocks and check their CRC64 without uncompressing them TRACE("Checking index integrity at %llu.", ctx->header.indexOffset); - if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->header.indexOffset, SEEK_SET) != 0) { FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset); diff --git a/src/index/index_v3.c b/src/index/index_v3.c index e786a55..1f9ae87 100644 --- a/src/index/index_v3.c +++ b/src/index/index_v3.c @@ -118,7 +118,7 @@ UT_array *process_index_v3(aaruformat_context *ctx) // Read the index header TRACE("Reading index header at position %llu", ctx->header.indexOffset); - if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->header.indexOffset, SEEK_SET) != 0) { FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset); utarray_free(index_entries); @@ -258,7 +258,7 @@ static bool add_subindex_entries(aaruformat_context *ctx, UT_array *index_entrie } // Seek to the subindex - if(fseek(ctx->imageStream, subindex_entry->offset, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)subindex_entry->offset, SEEK_SET) != 0) { FATAL("Could not seek to subindex header at %llu.", (unsigned long long)subindex_entry->offset); TRACE("Exiting add_subindex_entries() = false (seek failed)"); @@ -424,7 +424,7 @@ int32_t verify_index_v3(aaruformat_context *ctx) // This will traverse all blocks and check their CRC64 without uncompressing them TRACE("Checking index integrity at %llu.", ctx->header.indexOffset); - if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->header.indexOffset, SEEK_SET) != 0) { FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset); diff --git a/src/open.c b/src/open.c index 5bebf01..1a245d3 100644 --- a/src/open.c +++ b/src/open.c @@ -238,7 +238,7 @@ AARU_EXPORT void *AARU_CALL aaruf_open(const char *filepath, const bool resume_m aaruformat_context *ctx = NULL; int error_no = 0; size_t read_bytes = 0; - long pos = 0; + aaru_off_t pos = 0; int i = 0; uint32_t signature = 0; UT_array *index_entries = NULL; @@ -286,7 +286,7 @@ AARU_EXPORT void *AARU_CALL aaruf_open(const char *filepath, const bool resume_m } TRACE("Reading header at position 0"); - fseek(ctx->imageStream, 0, SEEK_SET); + aaruf_fseek(ctx->imageStream, 0, SEEK_SET); read_bytes = fread(&ctx->header, 1, sizeof(AaruHeader), ctx->imageStream); if(read_bytes != sizeof(AaruHeader)) @@ -322,7 +322,7 @@ AARU_EXPORT void *AARU_CALL aaruf_open(const char *filepath, const bool resume_m if(ctx->header.imageMajorVersion >= AARUF_VERSION_V2) { TRACE("Reading new header version at position 0"); - fseek(ctx->imageStream, 0, SEEK_SET); + aaruf_fseek(ctx->imageStream, 0, SEEK_SET); read_bytes = fread(&ctx->header, 1, sizeof(AaruHeaderV2), ctx->imageStream); if(read_bytes != sizeof(AaruHeaderV2)) @@ -438,8 +438,7 @@ AARU_EXPORT void *AARU_CALL aaruf_open(const char *filepath, const bool resume_m // Read the index header TRACE("Reading index header at position %" PRIu64, ctx->header.indexOffset); - pos = fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET); - if(pos < 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->header.indexOffset, SEEK_SET) != 0) { cleanup_open_failure(ctx); aaruf_set_open_error(AARUF_ERROR_CANNOT_READ_INDEX); @@ -447,7 +446,7 @@ AARU_EXPORT void *AARU_CALL aaruf_open(const char *filepath, const bool resume_m return NULL; } - pos = ftell(ctx->imageStream); + pos = aaruf_ftell(ctx->imageStream); if(pos != ctx->header.indexOffset) { cleanup_open_failure(ctx); @@ -501,9 +500,9 @@ AARU_EXPORT void *AARU_CALL aaruf_open(const char *filepath, const bool resume_m for(i = 0; i < utarray_len(index_entries); i++) { IndexEntry *entry = utarray_eltptr(index_entries, i); - pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); - if(pos < 0 || ftell(ctx->imageStream) != entry->offset) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET) != 0 || + aaruf_ftell(ctx->imageStream) != (aaru_off_t)entry->offset) { TRACE("Could not seek to %" PRIu64 " as indicated by index entry %d, continuing...", entry->offset, i); @@ -700,15 +699,15 @@ AARU_EXPORT void *AARU_CALL aaruf_open(const char *filepath, const bool resume_m ctx->image_info.LastModificationTime = ctx->header.lastWrittenTime; // Calculate aligned next block position - fseek(ctx->imageStream, 0, SEEK_END); + aaruf_fseek(ctx->imageStream, 0, SEEK_END); const uint64_t alignment_mask = (1ULL << ctx->user_data_ddt_header.blockAlignmentShift) - 1; - ctx->next_block_position = ftell(ctx->imageStream); // Start just after the header + ctx->next_block_position = (uint64_t)aaruf_ftell(ctx->imageStream); // Start just after the header ctx->next_block_position = ctx->next_block_position + alignment_mask & ~alignment_mask; TRACE("Data blocks will start at position %" PRIu64, ctx->next_block_position); // Position file pointer at the data start position - if(fseek(ctx->imageStream, ctx->next_block_position, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->next_block_position, SEEK_SET) != 0) { FATAL("Could not seek to data start position"); TRACE("Exiting aaruf_open() = NULL"); diff --git a/src/read.c b/src/read.c index 2add3da..c3fc8c3 100644 --- a/src/read.c +++ b/src/read.c @@ -575,7 +575,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_read_sector(void *context, const uint64_t se } TRACE("Reading block header"); - if(fseek(ctx->imageStream, block_offset, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)block_offset, SEEK_SET) != 0) { FATAL("Could not seek to block header"); free(block_header); @@ -598,7 +598,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_read_sector(void *context, const uint64_t se TRACE("Adding block header to cache"); add_to_cache_uint64(&ctx->block_header_cache, block_offset, block_header); } - else if(fseek(ctx->imageStream, block_offset + sizeof(BlockHeader), SEEK_SET) != 0) + else if(aaruf_fseek(ctx->imageStream, (aaru_off_t)(block_offset + sizeof(BlockHeader)), SEEK_SET) != 0) { FATAL("Could not seek past cached block header"); @@ -923,7 +923,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_read_sector(void *context, const uint64_t se return AARUF_ERROR_NOT_ENOUGH_MEMORY; } - fseek(ctx->imageStream, (aaru_off_t)(block_offset + sizeof(BlockHeader)), SEEK_SET); + aaruf_fseek(ctx->imageStream, (aaru_off_t)(block_offset + sizeof(BlockHeader)), SEEK_SET); read_bytes = fread(cmp_data, 1, block_header->cmpLength, ctx->imageStream); if(read_bytes != block_header->cmpLength) diff --git a/src/verify.c b/src/verify.c index 0dd9fd1..fec2168 100644 --- a/src/verify.c +++ b/src/verify.c @@ -167,7 +167,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_verify_image(void *context) goto cleanup; } - if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->header.indexOffset, SEEK_SET) != 0) { FATAL("Could not seek to index offset %" PRIu64, ctx->header.indexOffset); status = AARUF_ERROR_CANNOT_READ_HEADER; @@ -233,7 +233,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_verify_image(void *context) IndexEntry *entry = utarray_eltptr(index_entries, i); TRACE("Checking block with type %4.4s at position %" PRIu64, (char *)&entry->blockType, entry->offset); - if(fseek(ctx->imageStream, entry->offset, SEEK_SET) != 0) + if(aaruf_fseek(ctx->imageStream, (aaru_off_t)entry->offset, SEEK_SET) != 0) { FATAL("Could not seek to block at offset %" PRIu64, entry->offset); status = AARUF_ERROR_CANNOT_READ_BLOCK; diff --git a/src/write.c b/src/write.c index 07336e7..f6ed8fc 100644 --- a/src/write.c +++ b/src/write.c @@ -1656,7 +1656,7 @@ int32_t aaruf_close_current_block(aaruformat_context *ctx) // Write block header to file // Move to expected block position - fseek(ctx->imageStream, ctx->next_block_position, SEEK_SET); + aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->next_block_position, SEEK_SET); // Write block header if(fwrite(&ctx->current_block_header, sizeof(BlockHeader), 1, ctx->imageStream) != 1)