mirror of
https://github.com/aaru-dps/libaaruformat.git
synced 2025-12-16 19:24:40 +00:00
Decode checksums block on opening.
This commit is contained in:
80
src/open.c
80
src/open.c
@@ -45,6 +45,8 @@ void* aaruf_open(const char* filepath)
|
||||
uint16_t e;
|
||||
uint8_t lzmaProperties[LZMA_PROPERTIES_LENGTH];
|
||||
size_t lzmaSize;
|
||||
ChecksumHeader checksum_header;
|
||||
ChecksumEntry* checksum_entry;
|
||||
|
||||
ctx = (aaruformatContext*)malloc(sizeof(aaruformatContext));
|
||||
memset(ctx, 0, sizeof(aaruformatContext));
|
||||
@@ -1405,6 +1407,84 @@ void* aaruf_open(const char* filepath)
|
||||
// TODO: qsort()
|
||||
}
|
||||
|
||||
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",
|
||||
idxEntries[i].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);
|
||||
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr,
|
||||
|
||||
Reference in New Issue
Block a user