libaaruformat 1.0 Aaru Data Preservation Suite - Format Library

Loading...
Searching...
No Matches
index_v2.c
Go to the documentation of this file.
1/*
2 * This file is part of the Aaru Data Preservation Suite.
3 * Copyright (c) 2019-2025 Natalia Portillo.
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <limits.h>
20#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24#include "aaruformat.h"
25#include "log.h"
26#include "utarray.h"
27
81UT_array *process_index_v2(aaruformat_context *ctx)
82{
83 TRACE("Entering process_index_v2(%p)", ctx);
84
85 UT_array *index_entries = NULL;
86 IndexEntry entry;
87
88 if(ctx == NULL || ctx->imageStream == NULL) { return NULL; }
89
90 // Initialize the index entries array
91 const UT_icd index_entry_icd = {sizeof(IndexEntry), NULL, NULL, NULL};
92
93 utarray_new(index_entries, &index_entry_icd);
94
95 if(index_entries == NULL)
96 {
97 FATAL("Could not allocate memory for index entries array.");
98 TRACE("Exiting process_index_v2() = NULL");
99 return NULL;
100 }
101
102 // Read the index header
103 TRACE("Reading index header at position %llu", ctx->header.indexOffset);
104 if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0)
105 {
106 FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset);
107 utarray_free(index_entries);
108
109 TRACE("Exiting process_index_v2() = NULL");
110 return NULL;
111 }
112 IndexHeader2 idx_header;
113 if(fread(&idx_header, sizeof(IndexHeader2), 1, ctx->imageStream) != 1)
114 {
115 FATAL("Could not read index header at %llu.", ctx->header.indexOffset);
116 utarray_free(index_entries);
117
118 TRACE("Exiting process_index_v2() = NULL");
119 return NULL;
120 }
121
122 // Check if the index header is valid
123 if(idx_header.identifier != IndexBlock2)
124 {
125 FATAL("Incorrect index identifier.");
126 utarray_free(index_entries);
127
128 TRACE("Exiting process_index_v2() = NULL");
129 return NULL;
130 }
131
132 for(uint64_t i = 0; i < idx_header.entries; i++)
133 {
134 if(fread(&entry, sizeof(IndexEntry), 1, ctx->imageStream) != 1)
135 {
136 FATAL("Could not read index entry %llu at %llu.", (unsigned long long)i, ctx->header.indexOffset);
137 utarray_free(index_entries);
138
139 TRACE("Exiting process_index_v2() = NULL");
140 return NULL;
141 }
142
143 utarray_push_back(index_entries, &entry);
144 }
145
146 TRACE("Read %llu index entries from index block at position %llu", (unsigned long long)idx_header.entries,
147 ctx->header.indexOffset);
148 return index_entries;
149}
150
227int32_t verify_index_v2(aaruformat_context *ctx)
228{
229 TRACE("Entering verify_index_v2(%p)", ctx);
230
231 size_t read_bytes = 0;
232 IndexHeader2 index_header;
233 uint64_t crc64 = 0;
234 IndexEntry *index_entries = NULL;
235
236 if(ctx == NULL || ctx->imageStream == NULL)
237 {
238 FATAL("Invalid context or image stream.");
239
240 TRACE("Exiting verify_index_v2() = AARUF_ERROR_NOT_AARUFORMAT");
241 return AARUF_ERROR_NOT_AARUFORMAT;
242 }
243
244 // This will traverse all blocks and check their CRC64 without uncompressing them
245 TRACE("Checking index integrity at %llu.", ctx->header.indexOffset);
246 if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0)
247 {
248 FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset);
249
250 TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_HEADER");
251 return AARUF_ERROR_CANNOT_READ_HEADER;
252 }
253
254 // Read the index header
255 TRACE("Reading index header at position %llu", ctx->header.indexOffset);
256 read_bytes = fread(&index_header, 1, sizeof(IndexHeader2), ctx->imageStream);
257
258 if(read_bytes != sizeof(IndexHeader2))
259 {
260 FATAL("Could not read index header.");
261
262 TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_HEADER");
263 return AARUF_ERROR_CANNOT_READ_HEADER;
264 }
265
266 if(index_header.identifier != IndexBlock2)
267 {
268 FATAL("Incorrect index identifier.");
269
270 TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_INDEX");
271 return AARUF_ERROR_CANNOT_READ_INDEX;
272 }
273
274 TRACE("Index at %llu contains %llu entries.", ctx->header.indexOffset, (unsigned long long)index_header.entries);
275
276 if(index_header.entries != 0 && index_header.entries > SIZE_MAX / sizeof(IndexEntry))
277 {
278 FATAL("Index entry count %llu is too large to process.", (unsigned long long)index_header.entries);
279
280 TRACE("Exiting verify_index_v2() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
281 return AARUF_ERROR_NOT_ENOUGH_MEMORY;
282 }
283
284 const size_t entries_size = (size_t)index_header.entries * sizeof(IndexEntry);
285
286 if(entries_size > 0)
287 {
288 index_entries = malloc(entries_size);
289
290 if(index_entries == NULL)
291 {
292 FATAL("Cannot allocate memory for index entries.");
293
294 TRACE("Exiting verify_index_v2() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
295 return AARUF_ERROR_NOT_ENOUGH_MEMORY;
296 }
297
298 read_bytes = fread(index_entries, 1, entries_size, ctx->imageStream);
299
300 if(read_bytes != entries_size)
301 {
302 FATAL("Could not read index entries.");
303 free(index_entries);
304
305 TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_INDEX");
306 return AARUF_ERROR_CANNOT_READ_INDEX;
307 }
308 }
309
310 crc64_ctx *crc_ctx = aaruf_crc64_init();
311
312 if(crc_ctx == NULL)
313 {
314 FATAL("Cannot initialize CRC64 context.");
315 free(index_entries);
316
317 TRACE("Exiting verify_index_v2() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
318 return AARUF_ERROR_NOT_ENOUGH_MEMORY;
319 }
320
321 if(entries_size > 0)
322 {
323 const uint8_t *cursor = (const uint8_t *)index_entries;
324 size_t remaining = entries_size;
325
326 while(remaining > 0)
327 {
328 const uint32_t chunk = remaining > UINT32_MAX ? UINT32_MAX : (uint32_t)remaining;
329
330 if(aaruf_crc64_update(crc_ctx, cursor, chunk) != 0)
331 {
332 FATAL("Failed to update index CRC.");
333 aaruf_crc64_free(crc_ctx);
334 free(index_entries);
335
336 TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_INDEX");
337 return AARUF_ERROR_CANNOT_READ_INDEX;
338 }
339
340 cursor += chunk;
341 remaining -= chunk;
342 }
343 }
344
345 if(aaruf_crc64_final(crc_ctx, &crc64) != 0)
346 {
347 FATAL("Failed to finalize index CRC.");
348 aaruf_crc64_free(crc_ctx);
349 free(index_entries);
350
351 TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_INDEX");
352 return AARUF_ERROR_CANNOT_READ_INDEX;
353 }
354
355 aaruf_crc64_free(crc_ctx);
356
357 // Due to how C# wrote it, it is effectively reversed
358 if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
359
360 if(crc64 != index_header.crc64)
361 {
362 FATAL("Expected index CRC 0x%16llX but got 0x%16llX.", index_header.crc64, crc64);
363 free(index_entries);
364
365 TRACE("Exiting verify_index_v2() = AARUF_ERROR_INVALID_BLOCK_CRC");
366 return AARUF_ERROR_INVALID_BLOCK_CRC;
367 }
368
369 free(index_entries);
370
371 TRACE("Exiting verify_index_v2() = AARUF_OK");
372 return AARUF_STATUS_OK;
373}
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:146
AARUF_STATUS_OK
#define AARUF_STATUS_OK
Sector present and read without uncorrectable errors.
Definition errors.h:75
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:172
aaruformat_context::header
AaruHeaderV2 header
Parsed container header (v2).
Definition context.h:175
aaruformat_context::imageStream
FILE * imageStream
Underlying FILE* stream (binary mode).
Definition context.h:176
crc64_ctx
Minimal ECMA-182 CRC64 incremental state container (running value only).
Definition crc64.h:56