Split checksum block processing from open to a separate file.

This commit is contained in:
2025-08-02 16:49:53 +01:00
parent e081f09a71
commit 01fbaa0016
4 changed files with 137 additions and 85 deletions

View File

@@ -29,16 +29,13 @@
void *aaruf_open(const char *filepath)
{
aaruformatContext *ctx = NULL;
int errorNo = 0;
size_t readBytes = 0;
long pos = 0;
uint8_t *data = NULL;
int i = 0, j = 0;
ChecksumHeader checksum_header;
ChecksumEntry const *checksum_entry = NULL;
uint32_t signature = 0;
UT_array *index_entries = NULL;
aaruformatContext *ctx = NULL;
int errorNo = 0;
size_t readBytes = 0;
long pos = 0;
int i = 0;
uint32_t signature = 0;
UT_array *index_entries = NULL;
ctx = (aaruformatContext *)malloc(sizeof(aaruformatContext));
memset(ctx, 0, sizeof(aaruformatContext));
@@ -239,80 +236,7 @@ void *aaruf_open(const char *filepath)
break;
case ChecksumBlock:
readBytes = fread(&checksum_header, 1, sizeof(ChecksumHeader), ctx->imageStream);
if(readBytes != sizeof(ChecksumHeader))
{
memset(&checksum_header, 0, sizeof(ChecksumHeader));
fprintf(stderr, "libaaruformat: Could not read checksums block header, continuing...\n");
break;
}
if(checksum_header.identifier != ChecksumBlock)
{
memset(&checksum_header, 0, sizeof(ChecksumHeader));
fprintf(stderr, "libaaruformat: Incorrect identifier for checksum block at position %" PRIu64 "\n",
entry->offset);
}
data = (uint8_t *)malloc(checksum_header.length);
if(data == NULL)
{
memset(&checksum_header, 0, sizeof(ChecksumHeader));
fprintf(stderr, "libaaruformat: Could not allocate memory for checksum block, continuing...\n");
break;
}
readBytes = fread(data, 1, checksum_header.length, ctx->imageStream);
if(readBytes != checksum_header.length)
{
memset(&checksum_header, 0, sizeof(ChecksumHeader));
free(data);
fprintf(stderr, "libaaruformat: Could not read checksums block, continuing...\n");
break;
}
pos = 0;
for(j = 0; j < checksum_header.entries; j++)
{
checksum_entry = (ChecksumEntry *)(&data[pos]);
pos += sizeof(ChecksumEntry);
if(checksum_entry->type == Md5)
{
memcpy(ctx->checksums.md5, &data[pos], MD5_DIGEST_LENGTH);
ctx->checksums.hasMd5 = true;
}
else if(checksum_entry->type == Sha1)
{
memcpy(ctx->checksums.sha1, &data[pos], SHA1_DIGEST_LENGTH);
ctx->checksums.hasSha1 = true;
}
else if(checksum_entry->type == Sha256)
{
memcpy(ctx->checksums.sha256, &data[pos], SHA256_DIGEST_LENGTH);
ctx->checksums.hasSha256 = true;
}
else if(checksum_entry->type == SpamSum)
{
ctx->checksums.spamsum = malloc(checksum_entry->length + 1);
if(ctx->checksums.spamsum != NULL)
{
memcpy(ctx->checksums.spamsum, &data[pos], checksum_entry->length);
ctx->checksums.hasSpamSum = true;
}
ctx->checksums.spamsum[checksum_entry->length] = 0;
}
pos += checksum_entry->length;
}
checksum_entry = NULL;
free(data);
process_checksum_block(ctx, entry);
break;
default: