mirror of
https://github.com/aaru-dps/libaaruformat.git
synced 2025-12-16 19:24:40 +00:00
Split checksum block processing from open to a separate file.
This commit is contained in:
@@ -114,7 +114,8 @@ add_library(aaruformat SHARED include/aaruformat/consts.h include/aaruformat/enu
|
|||||||
src/ddt/ddt_v1.c
|
src/ddt/ddt_v1.c
|
||||||
src/blocks/metadata.c
|
src/blocks/metadata.c
|
||||||
src/blocks/optical.c
|
src/blocks/optical.c
|
||||||
src/blocks/dump.c)
|
src/blocks/dump.c
|
||||||
|
src/blocks/checksum.c)
|
||||||
|
|
||||||
include_directories(include include/aaruformat)
|
include_directories(include include/aaruformat)
|
||||||
|
|
||||||
|
|||||||
@@ -32,5 +32,6 @@ void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry
|
|||||||
void process_tracks_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_cicm_block(aaruformatContext *ctx, const IndexEntry *entry);
|
||||||
void process_dumphw_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
|
#endif // LIBAARUFORMAT_INTERNAL_H
|
||||||
|
|||||||
126
src/blocks/checksum.c
Normal file
126
src/blocks/checksum.c
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
92
src/open.c
92
src/open.c
@@ -29,16 +29,13 @@
|
|||||||
|
|
||||||
void *aaruf_open(const char *filepath)
|
void *aaruf_open(const char *filepath)
|
||||||
{
|
{
|
||||||
aaruformatContext *ctx = NULL;
|
aaruformatContext *ctx = NULL;
|
||||||
int errorNo = 0;
|
int errorNo = 0;
|
||||||
size_t readBytes = 0;
|
size_t readBytes = 0;
|
||||||
long pos = 0;
|
long pos = 0;
|
||||||
uint8_t *data = NULL;
|
int i = 0;
|
||||||
int i = 0, j = 0;
|
uint32_t signature = 0;
|
||||||
ChecksumHeader checksum_header;
|
UT_array *index_entries = NULL;
|
||||||
ChecksumEntry const *checksum_entry = NULL;
|
|
||||||
uint32_t signature = 0;
|
|
||||||
UT_array *index_entries = NULL;
|
|
||||||
|
|
||||||
ctx = (aaruformatContext *)malloc(sizeof(aaruformatContext));
|
ctx = (aaruformatContext *)malloc(sizeof(aaruformatContext));
|
||||||
memset(ctx, 0, sizeof(aaruformatContext));
|
memset(ctx, 0, sizeof(aaruformatContext));
|
||||||
@@ -239,80 +236,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case ChecksumBlock:
|
case ChecksumBlock:
|
||||||
readBytes = fread(&checksum_header, 1, sizeof(ChecksumHeader), ctx->imageStream);
|
process_checksum_block(ctx, entry);
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user