10 Commits

7 changed files with 375 additions and 205 deletions

View File

@@ -277,7 +277,7 @@ if(USE_SLOG)
target_compile_definitions(aaruformat PRIVATE ENABLE_TRACE ENABLE_FATAL USE_SLOG)
# Link slog
target_link_libraries(aaruformat PRIVATE slog)
target_link_libraries(aaruformat slog)
message(STATUS "slog logging enabled")
else()

View File

@@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>libaaruformat</id>
<version>1.0.0-alpha.8</version>
<version>1.0.0-alpha.11</version>
<description>Library for management of AaruFormat images.</description>
<authors>claunia</authors>
<projectUrl>https://github.com/aaru-dps/libaaruformat</projectUrl>

View File

@@ -605,7 +605,7 @@ int32_t decode_ddt_single_level_v2(aaruformat_context *ctx, uint64_t sector_addr
// Calculate positive or negative sector
if(negative)
sector_address -= ctx->user_data_ddt_header.negative;
sector_address = ctx->user_data_ddt_header.negative - sector_address;
else
sector_address += ctx->user_data_ddt_header.negative;
@@ -757,7 +757,7 @@ int32_t decode_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_addre
// Calculate positive or negative sector
if(negative)
sector_address -= ctx->user_data_ddt_header.negative;
sector_address = ctx->user_data_ddt_header.negative - sector_address;
else
sector_address += ctx->user_data_ddt_header.negative;
@@ -985,7 +985,7 @@ int32_t decode_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_addre
* @return Returns one of the following status codes:
* @retval true if the entry was set successfully, false otherwise.
*/
bool set_ddt_entry_v2(aaruformat_context *ctx, const uint64_t sector_address, bool negative, const uint64_t offset,
bool set_ddt_entry_v2(aaruformat_context *ctx, const uint64_t sector_address, const bool negative, const uint64_t offset,
const uint64_t block_offset, const uint8_t sector_status, uint64_t *ddt_entry)
{
TRACE("Entering set_ddt_entry_v2(%p, %" PRIu64 ", %d, %llu, %llu, %d)", ctx, sector_address, negative, offset,
@@ -999,9 +999,9 @@ bool set_ddt_entry_v2(aaruformat_context *ctx, const uint64_t sector_address, bo
}
if(ctx->user_data_ddt_header.tableShift > 0)
return set_ddt_multi_level_v2(ctx, sector_address, false, offset, block_offset, sector_status, ddt_entry);
return set_ddt_multi_level_v2(ctx, sector_address, negative, offset, block_offset, sector_status, ddt_entry);
return set_ddt_single_level_v2(ctx, sector_address, false, offset, block_offset, sector_status, ddt_entry);
return set_ddt_single_level_v2(ctx, sector_address, negative, offset, block_offset, sector_status, ddt_entry);
}
/**
@@ -1045,7 +1045,7 @@ bool set_ddt_single_level_v2(aaruformat_context *ctx, uint64_t sector_address, c
// Calculate positive or negative sector
if(negative)
sector_address -= ctx->user_data_ddt_header.negative;
sector_address = ctx->user_data_ddt_header.negative - sector_address;
else
sector_address += ctx->user_data_ddt_header.negative;
@@ -1062,10 +1062,12 @@ bool set_ddt_single_level_v2(aaruformat_context *ctx, uint64_t sector_address, c
TRACE("Exiting set_ddt_single_level_v2() = false");
return false;
}
*ddt_entry |= (uint64_t)sector_status << 60;
}
// Sector status can be different from previous deduplicated sector
*ddt_entry &= 0x0FFFFFFFFFFFFFFF;
*ddt_entry |= (uint64_t)sector_status << 60;
TRACE("Setting big single-level DDT entry %d to %ull", sector_address, (uint64_t)*ddt_entry);
ctx->user_data_ddt2[sector_address] = *ddt_entry;
@@ -1125,7 +1127,7 @@ bool set_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_address, bo
// Calculate positive or negative sector
if(negative)
sector_address -= ctx->user_data_ddt_header.negative;
sector_address = ctx->user_data_ddt_header.negative - sector_address;
else
sector_address += ctx->user_data_ddt_header.negative;
@@ -1154,10 +1156,12 @@ bool set_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_address, bo
TRACE("Exiting set_ddt_multi_level_v2() = false");
return false;
}
*ddt_entry |= (uint64_t)sector_status << 60;
}
// Sector status can be different from previous deduplicated sector
*ddt_entry &= 0x0FFFFFFFFFFFFFFF;
*ddt_entry |= (uint64_t)sector_status << 60;
TRACE("Setting small secondary DDT entry %d to %ull", sector_address % items_per_ddt_entry,
(uint64_t)*ddt_entry);
ctx->cached_secondary_ddt2[sector_address % items_per_ddt_entry] = *ddt_entry;
@@ -1637,10 +1641,12 @@ bool set_ddt_multi_level_v2(aaruformat_context *ctx, uint64_t sector_address, bo
TRACE("Exiting set_ddt_multi_level_v2() = false");
return false;
}
*ddt_entry |= (uint64_t)sector_status << 60;
}
// Sector status can be different from previous deduplicated sector
*ddt_entry &= 0x0FFFFFFFFFFFFFFF;
*ddt_entry |= (uint64_t)sector_status << 60;
TRACE("Setting big secondary DDT entry %d to %ull", sector_address % items_per_ddt_entry, (uint64_t)*ddt_entry);
ctx->cached_secondary_ddt2[sector_address % items_per_ddt_entry] = *ddt_entry;

View File

@@ -704,7 +704,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_read_track_sector(void *context, uint8_t *da
if(ctx->image_info.MetadataMediaType != OpticalDisc)
{
FATAL("Incorrect media type %d, expected OpticalDisc", ctx->imageInfo.XmlMediaType);
FATAL("Incorrect media type %d, expected OpticalDisc", ctx->image_info.MetadataMediaType);
TRACE("Exiting aaruf_read_track_sector() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
return AARUF_ERROR_INCORRECT_MEDIA_TYPE;
@@ -888,7 +888,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_read_sector_long(void *context, const uint64
// Calculate positive or negative sector
if(negative)
corrected_sector_address -= ctx->user_data_ddt_header.negative;
corrected_sector_address = ctx->user_data_ddt_header.negative - sector_address;
else
corrected_sector_address += ctx->user_data_ddt_header.negative;
@@ -1282,7 +1282,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_read_sector_long(void *context, const uint64
tag_length = 24;
break;
default:
FATAL("Unsupported media type %d", ctx->imageInfo.MediaType);
FATAL("Unsupported media type %d", ctx->image_info.MediaType);
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
return AARUF_ERROR_INCORRECT_MEDIA_TYPE;
@@ -1339,13 +1339,13 @@ AARU_EXPORT int32_t AARU_CALL aaruf_read_sector_long(void *context, const uint64
TRACE("Exiting aaruf_read_sector_long() = AARUF_STATUS_OK");
return AARUF_STATUS_OK;
default:
FATAL("Incorrect media type %d for long sector reading", ctx->imageInfo.MediaType);
FATAL("Incorrect media type %d for long sector reading", ctx->image_info.MediaType);
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
return AARUF_ERROR_INCORRECT_MEDIA_TYPE;
}
default:
FATAL("Incorrect media type %d for long sector reading", ctx->imageInfo.MediaType);
FATAL("Incorrect media type %d for long sector reading", ctx->image_info.MediaType);
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
return AARUF_ERROR_INCORRECT_MEDIA_TYPE;
@@ -1529,7 +1529,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_read_sector_tag(const void *context, const u
// Calculate positive or negative sector
if(negative)
corrected_sector_address -= ctx->user_data_ddt_header.negative;
corrected_sector_address = ctx->user_data_ddt_header.negative - sector_address;
else
corrected_sector_address += ctx->user_data_ddt_header.negative;

View File

@@ -225,195 +225,242 @@ AARU_EXPORT int32_t AARU_CALL aaruf_verify_image(void *context)
goto cleanup;
}
uint64_t crc_length;
const unsigned int entry_count = utarray_len(index_entries);
for(unsigned int i = 0; i < entry_count; i++)
{
const unsigned int entry_count = utarray_len(index_entries);
IndexEntry *entry = utarray_eltptr(index_entries, i);
TRACE("Checking block with type %4.4s at position %" PRIu64, (char *)&entry->blockType, entry->offset);
for(unsigned int i = 0; i < entry_count; i++)
if(fseek(ctx->imageStream, entry->offset, SEEK_SET) != 0)
{
IndexEntry *entry = utarray_eltptr(index_entries, i);
TRACE("Checking block with type %4.4s at position %" PRIu64, (char *)&entry->blockType, entry->offset);
FATAL("Could not seek to block at offset %" PRIu64, entry->offset);
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
if(fseek(ctx->imageStream, entry->offset, SEEK_SET) != 0)
{
FATAL("Could not seek to block at offset %" PRIu64, entry->offset);
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
switch(entry->blockType)
{
case DataBlock:
read_bytes = fread(&block_header, 1, sizeof(BlockHeader), ctx->imageStream);
if(read_bytes != sizeof(BlockHeader))
{
FATAL("Could not read block header");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
crc64_context = aaruf_crc64_init();
if(crc64_context == NULL)
{
FATAL("Could not initialize CRC64 context");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
status = update_crc64_from_stream(ctx->imageStream, block_header.cmpLength, buffer, VERIFY_SIZE,
crc64_context, "data block");
if(status != AARUF_STATUS_OK) goto cleanup;
if(aaruf_crc64_final(crc64_context, &crc64) != 0)
{
FATAL("Could not finalize CRC64 for data block");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
if(crc64 != block_header.cmpCrc64)
{
FATAL("Expected block CRC 0x%16llX but got 0x%16llX", block_header.cmpCrc64, crc64);
status = AARUF_ERROR_INVALID_BLOCK_CRC;
goto cleanup;
}
aaruf_crc64_free(crc64_context);
crc64_context = NULL;
break;
case DeDuplicationTable:
read_bytes = fread(&ddt_header, 1, sizeof(DdtHeader), ctx->imageStream);
if(read_bytes != sizeof(DdtHeader))
{
FATAL("Could not read DDT header");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
crc64_context = aaruf_crc64_init();
if(crc64_context == NULL)
{
FATAL("Could not initialize CRC64 context");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
status = update_crc64_from_stream(ctx->imageStream, ddt_header.cmpLength, buffer, VERIFY_SIZE,
crc64_context, "DDT block");
if(status != AARUF_STATUS_OK) goto cleanup;
if(aaruf_crc64_final(crc64_context, &crc64) != 0)
{
FATAL("Could not finalize CRC64 for DDT block");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
if(crc64 != ddt_header.cmpCrc64)
{
FATAL("Expected DDT CRC 0x%16llX but got 0x%16llX", ddt_header.cmpCrc64, crc64);
status = AARUF_ERROR_INVALID_BLOCK_CRC;
goto cleanup;
}
aaruf_crc64_free(crc64_context);
crc64_context = NULL;
break;
case DeDuplicationTable2:
read_bytes = fread(&ddt2_header, 1, sizeof(DdtHeader2), ctx->imageStream);
if(read_bytes != sizeof(DdtHeader2))
{
FATAL("Could not read DDT2 header");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
crc64_context = aaruf_crc64_init();
if(crc64_context == NULL)
{
FATAL("Could not initialize CRC64 context");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
status = update_crc64_from_stream(ctx->imageStream, ddt2_header.cmpLength, buffer, VERIFY_SIZE,
crc64_context, "DDT2 block");
if(status != AARUF_STATUS_OK) goto cleanup;
if(aaruf_crc64_final(crc64_context, &crc64) != 0)
{
FATAL("Could not finalize CRC64 for DDT2 block");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
if(crc64 != ddt2_header.cmpCrc64)
{
FATAL("Expected DDT2 CRC 0x%16llX but got 0x%16llX", ddt2_header.cmpCrc64, crc64);
status = AARUF_ERROR_INVALID_BLOCK_CRC;
goto cleanup;
}
aaruf_crc64_free(crc64_context);
crc64_context = NULL;
break;
case TracksBlock:
switch(entry->blockType)
{
case DataBlock:
read_bytes = fread(&block_header, 1, sizeof(BlockHeader), ctx->imageStream);
if(read_bytes != sizeof(BlockHeader))
{
read_bytes = fread(&tracks_header, 1, sizeof(TracksHeader), ctx->imageStream);
if(read_bytes != sizeof(TracksHeader))
{
FATAL("Could not read tracks header");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
const uint64_t tracks_bytes = (uint64_t)tracks_header.entries * sizeof(TrackEntry);
if(tracks_header.entries != 0 && tracks_bytes / sizeof(TrackEntry) != tracks_header.entries)
{
FATAL("Tracks header length overflow (entries=%u)", tracks_header.entries);
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
crc64_context = aaruf_crc64_init();
if(crc64_context == NULL)
{
FATAL("Could not initialize CRC64 context");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
status = update_crc64_from_stream(ctx->imageStream, tracks_bytes, buffer, VERIFY_SIZE,
crc64_context, "tracks block");
if(status != AARUF_STATUS_OK) goto cleanup;
if(aaruf_crc64_final(crc64_context, &crc64) != 0)
{
FATAL("Could not finalize CRC64 for tracks block");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
if(crc64 != tracks_header.crc64)
{
FATAL("Expected tracks CRC 0x%16llX but got 0x%16llX", tracks_header.crc64, crc64);
status = AARUF_ERROR_INVALID_BLOCK_CRC;
goto cleanup;
}
aaruf_crc64_free(crc64_context);
crc64_context = NULL;
break;
FATAL("Could not read block header");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
default:
TRACE("Ignoring block type %4.4s", (char *)&entry->blockType);
break;
crc64_context = aaruf_crc64_init();
if(crc64_context == NULL)
{
FATAL("Could not initialize CRC64 context");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
// For LZMA compression, skip the 5-byte properties header
crc_length = block_header.cmpLength;
if(block_header.compression == Lzma || block_header.compression == LzmaClauniaSubchannelTransform)
{
// Skip LZMA properties
uint8_t props[LZMA_PROPERTIES_LENGTH];
size_t read_props = fread(props, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
if(read_props != LZMA_PROPERTIES_LENGTH)
{
FATAL("Could not read LZMA properties");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
crc_length -= LZMA_PROPERTIES_LENGTH;
}
status = update_crc64_from_stream(ctx->imageStream, crc_length, buffer, VERIFY_SIZE, crc64_context,
"data block");
if(status != AARUF_STATUS_OK) goto cleanup;
if(aaruf_crc64_final(crc64_context, &crc64) != 0)
{
FATAL("Could not finalize CRC64 for data block");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
if(crc64 != block_header.cmpCrc64)
{
FATAL("Expected block CRC 0x%16llX but got 0x%16llX", block_header.cmpCrc64, crc64);
status = AARUF_ERROR_INVALID_BLOCK_CRC;
goto cleanup;
}
aaruf_crc64_free(crc64_context);
crc64_context = NULL;
break;
case DeDuplicationTable:
read_bytes = fread(&ddt_header, 1, sizeof(DdtHeader), ctx->imageStream);
if(read_bytes != sizeof(DdtHeader))
{
FATAL("Could not read DDT header");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
crc64_context = aaruf_crc64_init();
if(crc64_context == NULL)
{
FATAL("Could not initialize CRC64 context");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
// For LZMA compression, skip the 5-byte properties header
crc_length = ddt_header.cmpLength;
if(ddt_header.compression == Lzma || ddt_header.compression == LzmaClauniaSubchannelTransform)
{
// Skip LZMA properties
uint8_t props[LZMA_PROPERTIES_LENGTH];
size_t read_props = fread(props, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
if(read_props != LZMA_PROPERTIES_LENGTH)
{
FATAL("Could not read LZMA properties");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
crc_length -= LZMA_PROPERTIES_LENGTH;
}
status = update_crc64_from_stream(ctx->imageStream, crc_length, buffer, VERIFY_SIZE, crc64_context,
"data block");
if(status != AARUF_STATUS_OK) goto cleanup;
if(aaruf_crc64_final(crc64_context, &crc64) != 0)
{
FATAL("Could not finalize CRC64 for DDT block");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
if(crc64 != ddt_header.cmpCrc64)
{
FATAL("Expected DDT CRC 0x%16llX but got 0x%16llX", ddt_header.cmpCrc64, crc64);
status = AARUF_ERROR_INVALID_BLOCK_CRC;
goto cleanup;
}
aaruf_crc64_free(crc64_context);
crc64_context = NULL;
break;
case DeDuplicationTable2:
read_bytes = fread(&ddt2_header, 1, sizeof(DdtHeader2), ctx->imageStream);
if(read_bytes != sizeof(DdtHeader2))
{
FATAL("Could not read DDT2 header");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
crc64_context = aaruf_crc64_init();
if(crc64_context == NULL)
{
FATAL("Could not initialize CRC64 context");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
// For LZMA compression, skip the 5-byte properties header
crc_length = ddt2_header.cmpLength;
if(ddt2_header.compression == Lzma || ddt2_header.compression == LzmaClauniaSubchannelTransform)
{
// Skip LZMA properties
uint8_t props[LZMA_PROPERTIES_LENGTH];
size_t read_props = fread(props, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
if(read_props != LZMA_PROPERTIES_LENGTH)
{
FATAL("Could not read LZMA properties");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
crc_length -= LZMA_PROPERTIES_LENGTH;
}
status = update_crc64_from_stream(ctx->imageStream, crc_length, buffer, VERIFY_SIZE, crc64_context,
"data block");
if(status != AARUF_STATUS_OK) goto cleanup;
if(aaruf_crc64_final(crc64_context, &crc64) != 0)
{
FATAL("Could not finalize CRC64 for DDT2 block");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
if(crc64 != ddt2_header.cmpCrc64)
{
FATAL("Expected DDT2 CRC 0x%16llX but got 0x%16llX", ddt2_header.cmpCrc64, crc64);
status = AARUF_ERROR_INVALID_BLOCK_CRC;
goto cleanup;
}
aaruf_crc64_free(crc64_context);
crc64_context = NULL;
break;
case TracksBlock:
{
read_bytes = fread(&tracks_header, 1, sizeof(TracksHeader), ctx->imageStream);
if(read_bytes != sizeof(TracksHeader))
{
FATAL("Could not read tracks header");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
const uint64_t tracks_bytes = (uint64_t)tracks_header.entries * sizeof(TrackEntry);
if(tracks_header.entries != 0 && tracks_bytes / sizeof(TrackEntry) != tracks_header.entries)
{
FATAL("Tracks header length overflow (entries=%u)", tracks_header.entries);
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
crc64_context = aaruf_crc64_init();
if(crc64_context == NULL)
{
FATAL("Could not initialize CRC64 context");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
status = update_crc64_from_stream(ctx->imageStream, tracks_bytes, buffer, VERIFY_SIZE, crc64_context,
"tracks block");
if(status != AARUF_STATUS_OK) goto cleanup;
if(aaruf_crc64_final(crc64_context, &crc64) != 0)
{
FATAL("Could not finalize CRC64 for tracks block");
status = AARUF_ERROR_CANNOT_READ_BLOCK;
goto cleanup;
}
if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
if(crc64 != tracks_header.crc64)
{
FATAL("Expected tracks CRC 0x%16llX but got 0x%16llX", tracks_header.crc64, crc64);
status = AARUF_ERROR_INVALID_BLOCK_CRC;
goto cleanup;
}
aaruf_crc64_free(crc64_context);
crc64_context = NULL;
break;
}
default:
TRACE("Ignoring block type %4.4s", (char *)&entry->blockType);
break;
}
}

View File

@@ -604,7 +604,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_write_sector_long(void *context, uint64_t se
// Calculate positive or negative sector
if(negative)
corrected_sector_address -= ctx->user_data_ddt_header.negative;
corrected_sector_address = ctx->user_data_ddt_header.negative - sector_address;
else
corrected_sector_address += ctx->user_data_ddt_header.negative;
@@ -2127,7 +2127,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_write_sector_tag(void *context, const uint64
// Calculate positive or negative sector
if(negative)
corrected_sector_address -= ctx->user_data_ddt_header.negative;
corrected_sector_address = ctx->user_data_ddt_header.negative - sector_address;
else
corrected_sector_address += ctx->user_data_ddt_header.negative;

View File

@@ -738,3 +738,120 @@ TEST_F(CreateImageFixture, create_audio_image)
close_result = aaruf_close(context);
EXPECT_EQ(close_result, AARUF_STATUS_OK) << "Failed to close image";
}
TEST_F(CreateImageFixture, create_image_negative_sectors)
{
char path[PATH_MAX];
char filename[PATH_MAX];
getcwd(path, PATH_MAX);
snprintf(filename, PATH_MAX, "%s/data/random", path);
// Open random file
FILE *f = fopen(filename, "rb");
ASSERT_NE(f, nullptr) << "Failed to open random data file";
uint8_t *buffer = static_cast<uint8_t *>(malloc(1048576));
fread(buffer, 1, 1048576, f);
fclose(f);
constexpr size_t total_sectors = 128 * 1024 * 1024 / 512; // 128 MiB / 512 bytes
// Create image
void *context = aaruf_create("test.aif", 1, 512, total_sectors, 300, 0, "deduplicate=true;compress=true",
reinterpret_cast<const uint8_t *>("gtest"), 10, 0, 0, false);
// Verify that the file was successfully opened
ASSERT_NE(context, nullptr) << "Failed to create test.aif";
crc64_ctx *ctx = aaruf_crc64_init();
uint64_t generated_crc = 0;
// Write data in sectors of 512 bytes
for(size_t sector = 1; sector <= 150; ++sector)
{
const size_t buffer_offset = sector * 512 % 1048576; // Roll back to start when reaching end
const int32_t write_result =
aaruf_write_sector(context, sector, true, buffer + buffer_offset, SectorStatusDumped, 512);
ASSERT_EQ(write_result, AARUF_STATUS_OK) << "Failed to write negative sector " << sector;
aaruf_crc64_update(ctx, buffer + buffer_offset, 512);
}
// Write data in sectors of 512 bytes
for(size_t sector = 0; sector < total_sectors; ++sector)
{
const size_t buffer_offset = sector * 512 % 1048576; // Roll back to start when reaching end
const int32_t write_result =
aaruf_write_sector(context, sector, false, buffer + buffer_offset, SectorStatusDumped, 512);
ASSERT_EQ(write_result, AARUF_STATUS_OK) << "Failed to write sector " << sector;
aaruf_crc64_update(ctx, buffer + buffer_offset, 512);
}
aaruf_crc64_final(ctx, &generated_crc);
aaruf_crc64_free(ctx);
// Close the image
int32_t close_result = aaruf_close(context);
ASSERT_EQ(close_result, AARUF_STATUS_OK) << "Failed to close image";
free(buffer);
// Reopen the image
context = aaruf_open("test.aif", false, NULL);
ASSERT_NE(context, nullptr) << "Failed to open test.aif";
// Get image info to verify it's a valid image
ImageInfo image_info;
const int32_t result = aaruf_get_image_info(context, &image_info);
ASSERT_EQ(result, AARUF_STATUS_OK) << "Failed to get image info";
// Basic sanity checks on the image info
ASSERT_EQ(image_info.HasPartitions, false) << "Image should not have partitions";
ASSERT_EQ(image_info.HasSessions, false) << "Image should not have sessions";
ASSERT_EQ(image_info.Sectors, total_sectors) << "Unexpected number of sectors";
ASSERT_EQ(image_info.SectorSize, 512) << "Unexpected sector size";
ASSERT_STREQ(image_info.Version, "2.0") << "Unexpected image version";
ASSERT_STREQ(image_info.Application, "gtest") << "Unexpected application name";
ASSERT_STREQ(image_info.ApplicationVersion, "0.0") << "Unexpected application version";
ASSERT_EQ(image_info.MediaType, 1) << "Unexpected media type";
ASSERT_EQ(image_info.MetadataMediaType, 1) << "Unexpected metadata media type";
ctx = aaruf_crc64_init();
uint64_t crc = 0;
for(int i = 1; i <= 150; i++)
{
uint8_t sector_buffer[512];
uint32_t length = sizeof(sector_buffer);
uint8_t sector_status = 0;
const int32_t read_result = aaruf_read_sector(context, i, true, sector_buffer, &length, &sector_status);
EXPECT_EQ(read_result, AARUF_STATUS_OK) << "Failed to read negative sector " << i;
EXPECT_EQ(length, 512U) << "Unexpected length for negative sector " << i;
aaruf_crc64_update(ctx, sector_buffer, 512);
}
for(int i = 0; i < total_sectors; i++)
{
uint8_t sector_buffer[512];
uint32_t length = sizeof(sector_buffer);
uint8_t sector_status = 0;
const int32_t read_result = aaruf_read_sector(context, i, false, sector_buffer, &length, &sector_status);
EXPECT_EQ(read_result, AARUF_STATUS_OK) << "Failed to read sector " << i;
EXPECT_EQ(length, 512U) << "Unexpected length for sector " << i;
aaruf_crc64_update(ctx, sector_buffer, 512);
}
aaruf_crc64_final(ctx, &crc);
aaruf_crc64_free(ctx);
ASSERT_EQ(crc, generated_crc) << "Unexpected CRC64 for image data";
// Close the image
close_result = aaruf_close(context);
EXPECT_EQ(close_result, AARUF_STATUS_OK) << "Failed to close image";
}