Loading...
Searching...
No Matches
checksum.c
Go to the documentation of this file.
/*
* This file is part of the Aaru Data Preservation Suite.
* Copyright (c) 2019-2026 Natalia Portillo.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aaruformat/context.h"
#include "aaruformat/enums.h"
#include "aaruformat/structs/checksum.h"
#include "aaruformat/structs/index.h"
#include "log.h"
void process_checksum_block(aaruformat_context *ctx, const IndexEntry *entry)
{
TRACE("Entering process_checksum_block(%p, %p)", ctx, entry);
int seek_result = 0;
size_t read_bytes = 0;
ChecksumHeader checksum_header;
ChecksumEntry const *checksum_entry = NULL;
uint8_t *data = NULL;
int j = 0;
size_t payload_pos = 0;
// Check if the context and image stream are valid
if(ctx == NULL || ctx->imageStream == NULL)
{
FATAL("Invalid context or image stream.");
return;
}
// Seek to block
seek_result = fseek(ctx->imageStream, entry->offset, SEEK_SET);
if(seek_result < 0 || ftell(ctx->imageStream) != entry->offset)
{
FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset);
return;
}
// Even if those two checks shall have been done before
TRACE("Reading checksum block header at position %" PRIu64, entry->offset);
read_bytes = fread(&checksum_header, 1, sizeof(ChecksumHeader), ctx->imageStream);
if(read_bytes != sizeof(ChecksumHeader))
{
memset(&checksum_header, 0, sizeof(ChecksumHeader));
FATAL("Could not read checksums block header, continuing...\n");
return;
}
if(checksum_header.identifier != ChecksumBlock)
{
memset(&checksum_header, 0, sizeof(ChecksumHeader));
FATAL("Incorrect identifier for checksum block at position %" PRIu64 "\n", entry->offset);
return;
}
TRACE("Allocating %u bytes for checksum block", checksum_header.length);
data = (uint8_t *)malloc(checksum_header.length);
if(data == NULL)
{
memset(&checksum_header, 0, sizeof(ChecksumHeader));
FATAL("Could not allocate memory for checksum block, continuing...\n");
return;
}
TRACE("Reading checksum block data at position %" PRIu64, entry->offset + sizeof(ChecksumHeader));
read_bytes = fread(data, 1, checksum_header.length, ctx->imageStream);
if(read_bytes != checksum_header.length)
{
memset(&checksum_header, 0, sizeof(ChecksumHeader));
free(data);
FATAL("Could not read checksums block, continuing...\n");
return;
}
payload_pos = 0;
TRACE("Processing %u checksum entries", checksum_header.entries);
for(j = 0; j < checksum_header.entries; j++)
{
if(payload_pos + sizeof(ChecksumEntry) > checksum_header.length)
{
FATAL("Checksum entry %d exceeds block payload size", j);
break;
}
checksum_entry = (const ChecksumEntry *)&data[payload_pos];
payload_pos += sizeof(ChecksumEntry);
if(payload_pos + checksum_entry->length > checksum_header.length)
{
FATAL("Checksum payload for entry %d exceeds block payload size", j);
break;
}
switch(checksum_entry->type)
{
case Md5:
if(checksum_entry->length != MD5_DIGEST_LENGTH)
{
FATAL("MD5 checksum entry has invalid length %u", checksum_entry->length);
break;
}
TRACE("Found MD5 checksum");
memcpy(ctx->checksums.md5, &data[payload_pos], MD5_DIGEST_LENGTH);
ctx->checksums.hasMd5 = true;
break;
case Sha1:
if(checksum_entry->length != SHA1_DIGEST_LENGTH)
{
FATAL("SHA1 checksum entry has invalid length %u", checksum_entry->length);
break;
}
TRACE("Found SHA1 checksum");
memcpy(ctx->checksums.sha1, &data[payload_pos], SHA1_DIGEST_LENGTH);
ctx->checksums.hasSha1 = true;
break;
case Sha256:
if(checksum_entry->length != SHA256_DIGEST_LENGTH)
{
FATAL("SHA256 checksum entry has invalid length %u", checksum_entry->length);
break;
}
TRACE("Found SHA256 checksum");
memcpy(ctx->checksums.sha256, &data[payload_pos], SHA256_DIGEST_LENGTH);
ctx->checksums.hasSha256 = true;
break;
case SpamSum:
TRACE("Found SpamSum checksum of size %u", checksum_entry->length);
free(ctx->checksums.spamsum);
ctx->checksums.spamsum = NULL;
ctx->checksums.spamsum = malloc(checksum_entry->length + 1);
if(ctx->checksums.spamsum == NULL)
{
FATAL("Could not allocate memory for SpamSum digest");
break;
}
memcpy(ctx->checksums.spamsum, &data[payload_pos], checksum_entry->length);
ctx->checksums.spamsum[checksum_entry->length] = '\0';
ctx->checksums.hasSpamSum = true;
break;
default:
TRACE("Unknown checksum type %u, skipping", checksum_entry->type);
break;
}
payload_pos += checksum_entry->length;
}
checksum_entry = NULL;
free(data);
TRACE("Exiting process_checksum_block()");
}
process_checksum_block
void process_checksum_block(aaruformat_context *ctx, const IndexEntry *entry)
Processes a checksum block from the image stream.
Definition checksum.c:39
checksum.h
On-disk layout definitions for the checksum block (BlockType::ChecksumBlock).
context.h
Central runtime context structures for libaaruformat (image state, caches, checksum buffers).
MD5_DIGEST_LENGTH
#define MD5_DIGEST_LENGTH
Definition context.h:72
enums.h
ChecksumBlock
@ ChecksumBlock
Block containing contents checksums.
Definition enums.h:156
Sha1
@ Sha1
SHA-1 hash.
Definition enums.h:176
Md5
@ Md5
MD5 hash.
Definition enums.h:175
Sha256
@ Sha256
SHA-256 hash.
Definition enums.h:177
SpamSum
@ SpamSum
SpamSum (context-triggered piecewise hash).
Definition enums.h:178
index.h
On‑disk index block header and entry structures (versions 1, 2 and 3).
log.h
FATAL
#define FATAL(fmt,...)
Definition log.h:40
TRACE
#define TRACE(fmt,...)
Definition log.h:25
SHA1_DIGEST_LENGTH
#define SHA1_DIGEST_LENGTH
Definition sha1.h:39
SHA256_DIGEST_LENGTH
#define SHA256_DIGEST_LENGTH
Definition sha256.h:38
ChecksumEntry
Per-checksum metadata immediately followed by the digest / signature bytes.
Definition checksum.h:91
ChecksumEntry::length
uint32_t length
Length in bytes of the digest that immediately follows this structure.
Definition checksum.h:93
ChecksumEntry::type
uint8_t type
Algorithm used (value from ChecksumAlgorithm).
Definition checksum.h:92
ChecksumHeader
Header that precedes the sequence of checksum entries for a checksum block.
Definition checksum.h:74
ChecksumHeader::identifier
uint32_t identifier
Block identifier, must be BlockType::ChecksumBlock.
Definition checksum.h:75
ChecksumHeader::length
uint32_t length
Length in bytes of the payload (all entries + their digest data, excluding this header).
Definition checksum.h:76
ChecksumHeader::entries
uint8_t entries
Number of checksum entries that follow in the payload.
Definition checksum.h:77
Checksums::spamsum
uint8_t * spamsum
SpamSum fuzzy hash (ASCII), allocated length+1 with trailing 0.
Definition context.h:113
Checksums::hasSha256
bool hasSha256
True if sha256[] buffer populated.
Definition context.h:106
Checksums::sha1
uint8_t sha1[20]
SHA-1 digest (20 bytes).
Definition context.h:110
Checksums::sha256
uint8_t sha256[32]
SHA-256 digest (32 bytes).
Definition context.h:111
Checksums::md5
uint8_t md5[16]
MD5 digest (16 bytes).
Definition context.h:109
Checksums::hasSpamSum
bool hasSpamSum
True if spamsum pointer allocated and signature read.
Definition context.h:108
Checksums::hasSha1
bool hasSha1
True if sha1[] buffer populated.
Definition context.h:105
Checksums::hasMd5
bool hasMd5
True if md5[] buffer populated.
Definition context.h:104
IndexEntry
Single index entry describing a block's type, (optional) data classification, and file offset.
Definition index.h:109
IndexEntry::offset
uint64_t offset
Absolute byte offset in the image where the referenced block header begins.
Definition index.h:112
aaruformat_context
Master context representing an open or in‑creation Aaru image.
Definition context.h:175
aaruformat_context::checksums
Checksums checksums
Whole-image checksums discovered.
Definition context.h:272
aaruformat_context::imageStream
FILE * imageStream
Underlying FILE* stream (binary mode).
Definition context.h:179