Implement on-the-fly MD5 checksum calculation

This commit is contained in:
2025-10-03 00:57:14 +01:00
parent 9e128a0ab8
commit 59addd3efc
4 changed files with 23 additions and 1 deletions

View File

@@ -22,6 +22,7 @@
#include "crc64.h"
#include "hash_map.h"
#include "lru.h"
#include "md5.h"
#include "structs.h"
#include "utarray.h"
@@ -212,6 +213,8 @@ typedef struct aaruformatContext
bool rewinded; ///< True if stream has been rewound after open (write path).
uint64_t last_written_block; ///< Last written block number (write path).
bool calculating_md5; ///< True if whole-image MD5 being calculated on-the-fly.
md5_ctx md5_context; ///< Opaque MD5 context for streaming updates
} aaruformatContext;
/** \struct DumpHardwareEntriesWithData

View File

@@ -500,6 +500,13 @@ int aaruf_close(void *context)
uint64_t alignment_mask;
uint64_t aligned_position;
// Finalize pending checksums
if(ctx->calculating_md5)
{
ctx->checksums.hasMd5 = true;
aaruf_md5_final(&ctx->md5_context, ctx->checksums.md5);
}
// Write the checksums block
bool has_checksums =
ctx->checksums.hasMd5 || ctx->checksums.hasSha1 || ctx->checksums.hasSha256 || ctx->checksums.hasSpamSum;

View File

@@ -310,6 +310,12 @@ void *aaruf_create(const char *filepath, const uint32_t media_type, const uint32
ctx->rewinded = false;
ctx->last_written_block = 0;
if(parsed_options.md5)
{
ctx->calculating_md5 = true;
aaruf_md5_init(&ctx->md5_context);
}
// Is writing
ctx->isWriting = true;

View File

@@ -145,18 +145,24 @@ int32_t aaruf_write_sector(void *context, uint64_t sector_address, bool negative
return AARUF_ERROR_SECTOR_OUT_OF_BOUNDS;
}
// TODO: Check rewinded for disabling checksums
if(!ctx->rewinded)
{
if(sector_address <= ctx->last_written_block)
{
TRACE("Rewinded");
ctx->rewinded = true;
// Disable MD5 calculation
if(ctx->calculating_md5) ctx->calculating_md5 = false;
}
else
ctx->last_written_block = sector_address;
}
// Calculate MD5 on-the-fly if requested and sector is within user sectors (not negative or overflow)
if(ctx->calculating_md5 && !negative && sector_address <= ctx->imageInfo.Sectors)
aaruf_md5_update(&ctx->md5_context, data, length);
// TODO: If optical disc check track
// Close current block first