Loading...
Searching...
No Matches
index_v2.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 <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "aaruformat.h"
#include "log.h"
#include "utarray.h"
UT_array *process_index_v2(aaruformat_context *ctx)
{
TRACE("Entering process_index_v2(%p)", ctx);
UT_array *index_entries = NULL;
IndexEntry entry;
if(ctx == NULL || ctx->imageStream == NULL) { return NULL; }
// Initialize the index entries array
const UT_icd index_entry_icd = {sizeof(IndexEntry), NULL, NULL, NULL};
utarray_new(index_entries, &index_entry_icd);
if(index_entries == NULL)
{
FATAL("Could not allocate memory for index entries array.");
TRACE("Exiting process_index_v2() = NULL");
return NULL;
}
// Read the index header
TRACE("Reading index header at position %llu", ctx->header.indexOffset);
if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0)
{
FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset);
utarray_free(index_entries);
TRACE("Exiting process_index_v2() = NULL");
return NULL;
}
IndexHeader2 idx_header;
if(fread(&idx_header, sizeof(IndexHeader2), 1, ctx->imageStream) != 1)
{
FATAL("Could not read index header at %llu.", ctx->header.indexOffset);
utarray_free(index_entries);
TRACE("Exiting process_index_v2() = NULL");
return NULL;
}
// Check if the index header is valid
if(idx_header.identifier != IndexBlock2)
{
FATAL("Incorrect index identifier.");
utarray_free(index_entries);
TRACE("Exiting process_index_v2() = NULL");
return NULL;
}
for(uint64_t i = 0; i < idx_header.entries; i++)
{
if(fread(&entry, sizeof(IndexEntry), 1, ctx->imageStream) != 1)
{
FATAL("Could not read index entry %llu at %llu.", (unsigned long long)i, ctx->header.indexOffset);
utarray_free(index_entries);
TRACE("Exiting process_index_v2() = NULL");
return NULL;
}
utarray_push_back(index_entries, &entry);
}
TRACE("Read %llu index entries from index block at position %llu", (unsigned long long)idx_header.entries,
ctx->header.indexOffset);
return index_entries;
}
int32_t verify_index_v2(aaruformat_context *ctx)
{
TRACE("Entering verify_index_v2(%p)", ctx);
size_t read_bytes = 0;
IndexHeader2 index_header;
uint64_t crc64 = 0;
IndexEntry *index_entries = NULL;
if(ctx == NULL || ctx->imageStream == NULL)
{
FATAL("Invalid context or image stream.");
TRACE("Exiting verify_index_v2() = AARUF_ERROR_NOT_AARUFORMAT");
return AARUF_ERROR_NOT_AARUFORMAT;
}
// This will traverse all blocks and check their CRC64 without uncompressing them
TRACE("Checking index integrity at %llu.", ctx->header.indexOffset);
if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0)
{
FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset);
TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_HEADER");
return AARUF_ERROR_CANNOT_READ_HEADER;
}
// Read the index header
TRACE("Reading index header at position %llu", ctx->header.indexOffset);
read_bytes = fread(&index_header, 1, sizeof(IndexHeader2), ctx->imageStream);
if(read_bytes != sizeof(IndexHeader2))
{
FATAL("Could not read index header.");
TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_HEADER");
return AARUF_ERROR_CANNOT_READ_HEADER;
}
if(index_header.identifier != IndexBlock2)
{
FATAL("Incorrect index identifier.");
TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_INDEX");
return AARUF_ERROR_CANNOT_READ_INDEX;
}
TRACE("Index at %llu contains %llu entries.", ctx->header.indexOffset, (unsigned long long)index_header.entries);
if(index_header.entries != 0 && index_header.entries > SIZE_MAX / sizeof(IndexEntry))
{
FATAL("Index entry count %llu is too large to process.", (unsigned long long)index_header.entries);
TRACE("Exiting verify_index_v2() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
}
const size_t entries_size = (size_t)index_header.entries * sizeof(IndexEntry);
if(entries_size > 0)
{
index_entries = malloc(entries_size);
if(index_entries == NULL)
{
FATAL("Cannot allocate memory for index entries.");
TRACE("Exiting verify_index_v2() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
}
read_bytes = fread(index_entries, 1, entries_size, ctx->imageStream);
if(read_bytes != entries_size)
{
FATAL("Could not read index entries.");
free(index_entries);
TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_INDEX");
return AARUF_ERROR_CANNOT_READ_INDEX;
}
}
crc64_ctx *crc_ctx = aaruf_crc64_init();
if(crc_ctx == NULL)
{
FATAL("Cannot initialize CRC64 context.");
free(index_entries);
TRACE("Exiting verify_index_v2() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
}
if(entries_size > 0)
{
const uint8_t *cursor = (const uint8_t *)index_entries;
size_t remaining = entries_size;
while(remaining > 0)
{
const uint32_t chunk = remaining > UINT32_MAX ? UINT32_MAX : (uint32_t)remaining;
if(aaruf_crc64_update(crc_ctx, cursor, chunk) != 0)
{
FATAL("Failed to update index CRC.");
aaruf_crc64_free(crc_ctx);
free(index_entries);
TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_INDEX");
return AARUF_ERROR_CANNOT_READ_INDEX;
}
cursor += chunk;
remaining -= chunk;
}
}
if(aaruf_crc64_final(crc_ctx, &crc64) != 0)
{
FATAL("Failed to finalize index CRC.");
aaruf_crc64_free(crc_ctx);
free(index_entries);
TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_INDEX");
return AARUF_ERROR_CANNOT_READ_INDEX;
}
aaruf_crc64_free(crc_ctx);
// Due to how C# wrote it, it is effectively reversed
if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
if(crc64 != index_header.crc64)
{
FATAL("Expected index CRC 0x%16llX but got 0x%16llX.", index_header.crc64, crc64);
free(index_entries);
TRACE("Exiting verify_index_v2() = AARUF_ERROR_INVALID_BLOCK_CRC");
return AARUF_ERROR_INVALID_BLOCK_CRC;
}
free(index_entries);
TRACE("Exiting verify_index_v2() = AARUF_OK");
return AARUF_STATUS_OK;
}
aaruformat.h
AARUF_VERSION_V1
#define AARUF_VERSION_V1
First on‑disk version (C# implementation).
Definition consts.h:71
aaruf_crc64_update
int aaruf_crc64_update(crc64_ctx *ctx, const uint8_t *data, uint32_t len)
Updates the CRC64 context with new data.
Definition crc64.c:55
aaruf_crc64_free
void aaruf_crc64_free(crc64_ctx *ctx)
Frees a CRC64 context.
Definition crc64.c:155
aaruf_crc64_init
crc64_ctx * aaruf_crc64_init()
Initializes a CRC64 context.
Definition crc64.c:32
aaruf_crc64_final
int aaruf_crc64_final(crc64_ctx *ctx, uint64_t *crc)
Computes the final CRC64 value from the context.
Definition crc64.c:141
bswap_64
#define bswap_64(x)
Definition endian.h:81
IndexBlock2
@ IndexBlock2
Block containing the index v2.
Definition enums.h:150
AARUF_STATUS_OK
#define AARUF_STATUS_OK
Sector present and read without uncorrectable errors.
Definition errors.h:77
AARUF_ERROR_CANNOT_READ_HEADER
#define AARUF_ERROR_CANNOT_READ_HEADER
Failed to read container header.
Definition errors.h:45
AARUF_ERROR_NOT_ENOUGH_MEMORY
#define AARUF_ERROR_NOT_ENOUGH_MEMORY
Memory allocation failure (critical).
Definition errors.h:48
AARUF_ERROR_INVALID_BLOCK_CRC
#define AARUF_ERROR_INVALID_BLOCK_CRC
CRC64 mismatch indicating corruption.
Definition errors.h:57
AARUF_ERROR_CANNOT_READ_INDEX
#define AARUF_ERROR_CANNOT_READ_INDEX
Index block unreadable / truncated / bad identifier.
Definition errors.h:43
AARUF_ERROR_NOT_AARUFORMAT
#define AARUF_ERROR_NOT_AARUFORMAT
Input file/stream failed magic or structural validation.
Definition errors.h:40
process_index_v2
UT_array * process_index_v2(aaruformat_context *ctx)
Processes an index block (version 2) from the image stream.
Definition index_v2.c:81
verify_index_v2
int32_t verify_index_v2(aaruformat_context *ctx)
Verifies the integrity of an index block (version 2) in the image stream.
Definition index_v2.c:227
log.h
FATAL
#define FATAL(fmt,...)
Definition log.h:40
TRACE
#define TRACE(fmt,...)
Definition log.h:25
AaruHeaderV2::indexOffset
uint64_t indexOffset
Absolute byte offset to primary index block (MUST be > 0; 0 => corrupt/unreadable).
Definition header.h:115
AaruHeaderV2::imageMajorVersion
uint8_t imageMajorVersion
Container format major version.
Definition header.h:110
IndexEntry
Single index entry describing a block's type, (optional) data classification, and file offset.
Definition index.h:109
IndexHeader2
Index header (version 2) with 64‑bit entry counter (identifier == IndexBlock2).
Definition index.h:79
IndexHeader2::entries
uint64_t entries
Number of IndexEntry records that follow immediately.
Definition index.h:81
IndexHeader2::identifier
uint32_t identifier
Block identifier (must be BlockType::IndexBlock2).
Definition index.h:80
IndexHeader2::crc64
uint64_t crc64
CRC64-ECMA of the entries array (legacy byte-swapped rule still applies for old versions).
Definition index.h:82
aaruformat_context
Master context representing an open or in‑creation Aaru image.
Definition context.h:175
aaruformat_context::header
AaruHeaderV2 header
Parsed container header (v2).
Definition context.h:178
aaruformat_context::imageStream
FILE * imageStream
Underlying FILE* stream (binary mode).
Definition context.h:179
crc64_ctx
Minimal ECMA-182 CRC64 incremental state container (running value only).
Definition crc64.h:56