From 01fbaa0016a065ec7cf96a333304eee11da36689 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 2 Aug 2025 16:49:53 +0100 Subject: [PATCH] Split checksum block processing from open to a separate file. --- CMakeLists.txt | 3 +- include/internal.h | 1 + src/blocks/checksum.c | 126 ++++++++++++++++++++++++++++++++++++++++++ src/open.c | 92 +++--------------------------- 4 files changed, 137 insertions(+), 85 deletions(-) create mode 100644 src/blocks/checksum.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 08a1325..1df2b94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,7 +114,8 @@ add_library(aaruformat SHARED include/aaruformat/consts.h include/aaruformat/enu src/ddt/ddt_v1.c src/blocks/metadata.c src/blocks/optical.c - src/blocks/dump.c) + src/blocks/dump.c + src/blocks/checksum.c) include_directories(include include/aaruformat) diff --git a/include/internal.h b/include/internal.h index 29eaa84..0cc9c54 100644 --- a/include/internal.h +++ b/include/internal.h @@ -32,5 +32,6 @@ void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry); void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry); void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry); +void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry); #endif // LIBAARUFORMAT_INTERNAL_H diff --git a/src/blocks/checksum.c b/src/blocks/checksum.c new file mode 100644 index 0000000..0c0c3ed --- /dev/null +++ b/src/blocks/checksum.c @@ -0,0 +1,126 @@ +/* +* This file is part of the Aaru Data Preservation Suite. +* Copyright (c) 2019-2025 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 . +*/ + +#include +#include +#include +#include + +#include "aaruformat.h" + +void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry) +{ + int pos = 0; + size_t readBytes = 0; + ChecksumHeader checksum_header; + ChecksumEntry const *checksum_entry = NULL; + uint8_t *data = NULL; + int j = 0; + + // Check if the context and image stream are valid + if(ctx == NULL || ctx->imageStream == NULL) + { + fprintf(stderr, "Invalid context or image stream.\n"); + return; + } + + // Seek to block + pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); + if(pos < 0 || ftell(ctx->imageStream) != entry->offset) + { + fprintf(stderr, "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); + + return; + } + + // Even if those two checks shall have been done before + 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"); + return; + } + + 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"); + return; + } + + 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"); + return; + } + + 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); +} \ No newline at end of file diff --git a/src/open.c b/src/open.c index e476289..45fc588 100644 --- a/src/open.c +++ b/src/open.c @@ -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: