libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
index_v3.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 <stdbool.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25#include "aaruformat.h"
26#include "internal.h"
27#include "log.h"
28#include "utarray.h"
29
30static bool add_subindex_entries(aaruformat_context *ctx, UT_array *index_entries, const IndexEntry *subindex_entry);
31
99{
100 TRACE("Entering process_index_v3(%p)", ctx);
101
102 UT_array *index_entries = NULL;
103 IndexEntry entry;
104
105 if(ctx == NULL || ctx->imageStream == NULL) return NULL;
106
107 // Initialize the index entries array
108 const UT_icd index_entry_icd = {sizeof(IndexEntry), NULL, NULL, NULL};
109
110 utarray_new(index_entries, &index_entry_icd);
111
112 if(index_entries == NULL)
113 {
114 FATAL("Could not allocate memory for index entries array.");
115 TRACE("Exiting process_index_v3() = NULL");
116 return NULL;
117 }
118
119 // Read the index header
120 TRACE("Reading index header at position %llu", ctx->header.indexOffset);
121 if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0)
122 {
123 FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset);
124 utarray_free(index_entries);
125
126 TRACE("Exiting process_index_v3() = NULL");
127 return NULL;
128 }
129 IndexHeader3 idx_header;
130 if(fread(&idx_header, sizeof(IndexHeader3), 1, ctx->imageStream) != 1)
131 {
132 FATAL("Could not read index header at %llu.", ctx->header.indexOffset);
133 utarray_free(index_entries);
134
135 TRACE("Exiting process_index_v3() = NULL");
136 return NULL;
137 }
138
139 // Check if the index header is valid
140 if(idx_header.identifier != IndexBlock3)
141 {
142 FATAL("Incorrect index identifier.");
143 utarray_free(index_entries);
144
145 TRACE("Exiting process_index_v3() = NULL");
146 return NULL;
147 }
148
149 for(uint64_t i = 0; i < idx_header.entries; i++)
150 {
151 if(fread(&entry, sizeof(IndexEntry), 1, ctx->imageStream) != 1)
152 {
153 FATAL("Could not read index entry %llu at %llu.", (unsigned long long)i, ctx->header.indexOffset);
154 utarray_free(index_entries);
155
156 TRACE("Exiting process_index_v3() = NULL");
157 return NULL;
158 }
159
160 if(entry.blockType == IndexBlock3)
161 {
162 // If the entry is a subindex, we need to read its entries
163 if(!add_subindex_entries(ctx, index_entries, &entry))
164 {
165 utarray_free(index_entries);
166
167 TRACE("Exiting process_index_v3() = NULL");
168 return NULL;
169 }
170 continue;
171 }
172 utarray_push_back(index_entries, &entry);
173 }
174
175 TRACE("Read %llu index entries from index block at position %llu", (unsigned long long)idx_header.entries,
176 ctx->header.indexOffset);
177 return index_entries;
178}
179
247static bool add_subindex_entries(aaruformat_context *ctx, UT_array *index_entries, const IndexEntry *subindex_entry)
248{
249 TRACE("Entering add_subindex_entries(%p, %p, %p)", ctx, index_entries, subindex_entry);
250
251 IndexHeader3 subindex_header;
252 IndexEntry entry;
253
254 if(ctx == NULL || ctx->imageStream == NULL || index_entries == NULL || subindex_entry == NULL)
255 {
256 TRACE("Exiting add_subindex_entries() = false (invalid parameters)");
257 return false;
258 }
259
260 // Seek to the subindex
261 if(fseek(ctx->imageStream, subindex_entry->offset, SEEK_SET) != 0)
262 {
263 FATAL("Could not seek to subindex header at %llu.", (unsigned long long)subindex_entry->offset);
264 TRACE("Exiting add_subindex_entries() = false (seek failed)");
265 return false;
266 }
267
268 // Read the subindex header
269 if(fread(&subindex_header, sizeof(IndexHeader3), 1, ctx->imageStream) != 1)
270 {
271 FATAL("Could not read subindex header at %llu.", (unsigned long long)subindex_entry->offset);
272 TRACE("Exiting add_subindex_entries() = false (header read failed)");
273 return false;
274 }
275
276 // Check if the subindex header is valid
277 if(subindex_header.identifier != IndexBlock3)
278 {
279 FATAL("Incorrect subindex identifier.");
280
281 TRACE("Exiting add_subindex_entries() = false (invalid identifier)");
282 return false;
283 }
284
285 if(subindex_header.entries != 0 && subindex_header.entries > SIZE_MAX / sizeof(IndexEntry))
286 {
287 FATAL("Subindex entry count %llu is too large to process.", (unsigned long long)subindex_header.entries);
288 TRACE("Exiting add_subindex_entries() = false (entry count overflow)");
289 return false;
290 }
291
292 // Read each entry in the subindex and add it to the main index entries array
293 for(uint64_t i = 0; i < subindex_header.entries; i++)
294 {
295 if(fread(&entry, sizeof(IndexEntry), 1, ctx->imageStream) != 1)
296 {
297 FATAL("Could not read subindex entry %llu at %llu.", (unsigned long long)i,
298 (unsigned long long)subindex_entry->offset);
299 TRACE("Exiting add_subindex_entries() = false (entry read failed)");
300 return false;
301 }
302 if(entry.blockType == IndexBlock3)
303 {
304 // If the entry is a subindex, we need to read its entries
305 if(!add_subindex_entries(ctx, index_entries, &entry))
306 {
307 TRACE("Exiting add_subindex_entries() = false (nested subindex failed)");
308 return false;
309 }
310 continue;
311 }
312 utarray_push_back(index_entries, &entry);
313 }
314
315 TRACE("Exiting add_subindex_entries() = true after appending %llu entries",
316 (unsigned long long)subindex_header.entries);
317 return true;
318}
319
409{
410 TRACE("Entering verify_index_v3(%p)", ctx);
411
412 size_t read_bytes = 0;
413 IndexHeader3 index_header;
414 uint64_t crc64 = 0;
415 IndexEntry *index_entries = NULL;
416
417 if(ctx == NULL || ctx->imageStream == NULL)
418 {
419 FATAL("Invalid context or image stream.");
420
421 TRACE("Exiting verify_index_v3() = AARUF_ERROR_NOT_AARUFORMAT");
423 }
424
425 // This will traverse all blocks and check their CRC64 without uncompressing them
426 TRACE("Checking index integrity at %llu.", ctx->header.indexOffset);
427 if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0)
428 {
429 FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset);
430
431 TRACE("Exiting verify_index_v3() = AARUF_ERROR_CANNOT_READ_HEADER");
433 }
434
435 // Read the index header
436 TRACE("Reading index header at position %llu", ctx->header.indexOffset);
437 read_bytes = fread(&index_header, 1, sizeof(IndexHeader3), ctx->imageStream);
438
439 if(read_bytes != sizeof(IndexHeader3))
440 {
441 FATAL("Could not read index header.");
442
443 TRACE("Exiting verify_index_v3() = AARUF_ERROR_CANNOT_READ_HEADER");
445 }
446
447 if(index_header.identifier != IndexBlock3)
448 {
449 FATAL("Incorrect index identifier.");
450
451 TRACE("Exiting verify_index_v3() = AARUF_ERROR_CANNOT_READ_INDEX");
453 }
454
455 TRACE("Index at %llu contains %llu entries.", ctx->header.indexOffset, (unsigned long long)index_header.entries);
456
457 if(index_header.entries != 0 && index_header.entries > SIZE_MAX / sizeof(IndexEntry))
458 {
459 FATAL("Index entry count %llu is too large to process.", (unsigned long long)index_header.entries);
460
461 TRACE("Exiting verify_index_v3() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
463 }
464
465 const size_t entries_size = (size_t)index_header.entries * sizeof(IndexEntry);
466
467 if(entries_size > 0)
468 {
469 index_entries = malloc(entries_size);
470
471 if(index_entries == NULL)
472 {
473 FATAL("Cannot allocate memory for index entries.");
474
475 TRACE("Exiting verify_index_v3() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
477 }
478
479 read_bytes = fread(index_entries, 1, entries_size, ctx->imageStream);
480
481 if(read_bytes != entries_size)
482 {
483 FATAL("Could not read index entries.");
484 free(index_entries);
485
486 TRACE("Exiting verify_index_v3() = AARUF_ERROR_CANNOT_READ_INDEX");
488 }
489 }
490
491 crc64_ctx *crc_ctx = aaruf_crc64_init();
492
493 if(crc_ctx == NULL)
494 {
495 FATAL("Cannot initialize CRC64 context.");
496 free(index_entries);
497
498 TRACE("Exiting verify_index_v3() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
500 }
501
502 if(entries_size > 0)
503 {
504 const uint8_t *cursor = (const uint8_t *)index_entries;
505 size_t remaining = entries_size;
506
507 while(remaining > 0)
508 {
509 const uint32_t chunk = remaining > UINT32_MAX ? UINT32_MAX : (uint32_t)remaining;
510
511 if(aaruf_crc64_update(crc_ctx, cursor, chunk) != 0)
512 {
513 FATAL("Failed to update index CRC.");
514 aaruf_crc64_free(crc_ctx);
515 free(index_entries);
516
517 TRACE("Exiting verify_index_v3() = AARUF_ERROR_CANNOT_READ_INDEX");
519 }
520
521 cursor += chunk;
522 remaining -= chunk;
523 }
524 }
525
526 if(aaruf_crc64_final(crc_ctx, &crc64) != 0)
527 {
528 FATAL("Failed to finalize index CRC.");
529 aaruf_crc64_free(crc_ctx);
530 free(index_entries);
531
532 TRACE("Exiting verify_index_v3() = AARUF_ERROR_CANNOT_READ_INDEX");
534 }
535
536 aaruf_crc64_free(crc_ctx);
537
538 // Due to how C# wrote it, it is effectively reversed
539 if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
540
541 if(crc64 != index_header.crc64)
542 {
543 FATAL("Expected index CRC 0x%16llX but got 0x%16llX.", index_header.crc64, crc64);
544 free(index_entries);
545
546 TRACE("Exiting verify_index_v3() = AARUF_ERROR_INVALID_BLOCK_CRC");
548 }
549
550 free(index_entries);
551
552 TRACE("Exiting verify_index_v3() = AARUF_OK");
553 return AARUF_STATUS_OK;
554}
#define AARUF_VERSION_V1
First on‑disk version (C# implementation).
Definition consts.h:71
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
void aaruf_crc64_free(crc64_ctx *ctx)
Frees a CRC64 context.
Definition crc64.c:155
crc64_ctx * aaruf_crc64_init()
Initializes a CRC64 context.
Definition crc64.c:32
int aaruf_crc64_final(crc64_ctx *ctx, uint64_t *crc)
Computes the final CRC64 value from the context.
Definition crc64.c:141
#define bswap_64(x)
Definition endian.h:81
@ IndexBlock3
Block containing the index v3.
Definition enums.h:148
#define AARUF_STATUS_OK
Sector present and read without uncorrectable errors.
Definition errors.h:75
#define AARUF_ERROR_CANNOT_READ_HEADER
Failed to read container header.
Definition errors.h:45
#define AARUF_ERROR_NOT_ENOUGH_MEMORY
Memory allocation failure (critical).
Definition errors.h:48
#define AARUF_ERROR_INVALID_BLOCK_CRC
CRC64 mismatch indicating corruption.
Definition errors.h:57
#define AARUF_ERROR_CANNOT_READ_INDEX
Index block unreadable / truncated / bad identifier.
Definition errors.h:43
#define AARUF_ERROR_NOT_AARUFORMAT
Input file/stream failed magic or structural validation.
Definition errors.h:40
static bool add_subindex_entries(aaruformat_context *ctx, UT_array *index_entries, const IndexEntry *subindex_entry)
Adds entries from a subindex block (version 3) to the main index entries array.
Definition index_v3.c:247
int32_t verify_index_v3(aaruformat_context *ctx)
Verifies the integrity of an index block (version 3) in the image stream.
Definition index_v3.c:408
UT_array * process_index_v3(aaruformat_context *ctx)
Processes an index block (version 3) from the image stream.
Definition index_v3.c:98
#define FATAL(fmt,...)
Definition log.h:40
#define TRACE(fmt,...)
Definition log.h:25
uint64_t indexOffset
Absolute byte offset to primary index block (MUST be > 0; 0 => corrupt/unreadable).
Definition header.h:115
uint8_t imageMajorVersion
Container format major version.
Definition header.h:110
Single index entry describing a block's type, (optional) data classification, and file offset.
Definition index.h:109
uint32_t blockType
Block identifier of the referenced block (value from BlockType).
Definition index.h:110
uint64_t offset
Absolute byte offset in the image where the referenced block header begins.
Definition index.h:112
Index header (version 3) adding hierarchical chaining (identifier == IndexBlock3).
Definition index.h:93
uint32_t identifier
Block identifier (must be BlockType::IndexBlock3).
Definition index.h:94
uint64_t crc64
CRC64-ECMA of the local entries array (does NOT cover subindexes or previous chains).
Definition index.h:96
uint64_t entries
Number of IndexEntry records that follow in this (sub)index block.
Definition index.h:95
Master context representing an open or in‑creation Aaru image.
Definition context.h:172
AaruHeaderV2 header
Parsed container header (v2).
Definition context.h:175
FILE * imageStream
Underlying FILE* stream (binary mode).
Definition context.h:176
Minimal ECMA-182 CRC64 incremental state container (running value only).
Definition crc64.h:56