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-2026 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 "internal.h"
26#include "log.h"
27#include "utarray.h"
28
83{
84 TRACE("Entering process_index_v2(%p)", ctx);
85
86 UT_array *index_entries = NULL;
87 IndexEntry entry;
88
89 if(ctx == NULL || ctx->imageStream == NULL) { return NULL; }
90
91 // Initialize the index entries array
92 const UT_icd index_entry_icd = {sizeof(IndexEntry), NULL, NULL, NULL};
93
94 utarray_new(index_entries, &index_entry_icd);
95
96 if(index_entries == NULL)
97 {
98 FATAL("Could not allocate memory for index entries array.");
99 TRACE("Exiting process_index_v2() = NULL");
100 return NULL;
101 }
102
103 // Read the index header
104 TRACE("Reading index header at position %llu", ctx->header.indexOffset);
105 if(aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->header.indexOffset, SEEK_SET) != 0)
106 {
107 FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset);
108 utarray_free(index_entries);
109
110 TRACE("Exiting process_index_v2() = NULL");
111 return NULL;
112 }
113 IndexHeader2 idx_header;
114 if(fread(&idx_header, sizeof(IndexHeader2), 1, ctx->imageStream) != 1)
115 {
116 FATAL("Could not read index header at %llu.", ctx->header.indexOffset);
117 utarray_free(index_entries);
118
119 TRACE("Exiting process_index_v2() = NULL");
120 return NULL;
121 }
122
123 // Check if the index header is valid
124 if(idx_header.identifier != IndexBlock2)
125 {
126 FATAL("Incorrect index identifier.");
127 utarray_free(index_entries);
128
129 TRACE("Exiting process_index_v2() = NULL");
130 return NULL;
131 }
132
133 for(uint64_t i = 0; i < idx_header.entries; i++)
134 {
135 if(fread(&entry, sizeof(IndexEntry), 1, ctx->imageStream) != 1)
136 {
137 FATAL("Could not read index entry %llu at %llu.", (unsigned long long)i, ctx->header.indexOffset);
138 utarray_free(index_entries);
139
140 TRACE("Exiting process_index_v2() = NULL");
141 return NULL;
142 }
143
144 utarray_push_back(index_entries, &entry);
145 }
146
147 TRACE("Read %llu index entries from index block at position %llu", (unsigned long long)idx_header.entries,
148 ctx->header.indexOffset);
149 return index_entries;
150}
151
229{
230 TRACE("Entering verify_index_v2(%p)", ctx);
231
232 size_t read_bytes = 0;
233 IndexHeader2 index_header;
234 uint64_t crc64 = 0;
235 IndexEntry *index_entries = NULL;
236
237 if(ctx == NULL || ctx->imageStream == NULL)
238 {
239 FATAL("Invalid context or image stream.");
240
241 TRACE("Exiting verify_index_v2() = AARUF_ERROR_NOT_AARUFORMAT");
243 }
244
245 // This will traverse all blocks and check their CRC64 without uncompressing them
246 TRACE("Checking index integrity at %llu.", ctx->header.indexOffset);
247 if(aaruf_fseek(ctx->imageStream, (aaru_off_t)ctx->header.indexOffset, SEEK_SET) != 0)
248 {
249 FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset);
250
251 TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_HEADER");
253 }
254
255 // Read the index header
256 TRACE("Reading index header at position %llu", ctx->header.indexOffset);
257 read_bytes = fread(&index_header, 1, sizeof(IndexHeader2), ctx->imageStream);
258
259 if(read_bytes != sizeof(IndexHeader2))
260 {
261 FATAL("Could not read index header.");
262
263 TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_HEADER");
265 }
266
267 if(index_header.identifier != IndexBlock2)
268 {
269 FATAL("Incorrect index identifier.");
270
271 TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_INDEX");
273 }
274
275 TRACE("Index at %llu contains %llu entries.", ctx->header.indexOffset, (unsigned long long)index_header.entries);
276
277 if(index_header.entries != 0 && index_header.entries > SIZE_MAX / sizeof(IndexEntry))
278 {
279 FATAL("Index entry count %llu is too large to process.", (unsigned long long)index_header.entries);
280
281 TRACE("Exiting verify_index_v2() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
283 }
284
285 const size_t entries_size = (size_t)index_header.entries * sizeof(IndexEntry);
286
287 if(entries_size > 0)
288 {
289 index_entries = malloc(entries_size);
290
291 if(index_entries == NULL)
292 {
293 FATAL("Cannot allocate memory for index entries.");
294
295 TRACE("Exiting verify_index_v2() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
297 }
298
299 read_bytes = fread(index_entries, 1, entries_size, ctx->imageStream);
300
301 if(read_bytes != entries_size)
302 {
303 FATAL("Could not read index entries.");
304 free(index_entries);
305
306 TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_INDEX");
308 }
309 }
310
311 crc64_ctx *crc_ctx = aaruf_crc64_init();
312
313 if(crc_ctx == NULL)
314 {
315 FATAL("Cannot initialize CRC64 context.");
316 free(index_entries);
317
318 TRACE("Exiting verify_index_v2() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
320 }
321
322 if(entries_size > 0)
323 {
324 const uint8_t *cursor = (const uint8_t *)index_entries;
325 size_t remaining = entries_size;
326
327 while(remaining > 0)
328 {
329 const uint32_t chunk = remaining > UINT32_MAX ? UINT32_MAX : (uint32_t)remaining;
330
331 if(aaruf_crc64_update(crc_ctx, cursor, chunk) != 0)
332 {
333 FATAL("Failed to update index CRC.");
334 aaruf_crc64_free(crc_ctx);
335 free(index_entries);
336
337 TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_INDEX");
339 }
340
341 cursor += chunk;
342 remaining -= chunk;
343 }
344 }
345
346 if(aaruf_crc64_final(crc_ctx, &crc64) != 0)
347 {
348 FATAL("Failed to finalize index CRC.");
349 aaruf_crc64_free(crc_ctx);
350 free(index_entries);
351
352 TRACE("Exiting verify_index_v2() = AARUF_ERROR_CANNOT_READ_INDEX");
354 }
355
356 aaruf_crc64_free(crc_ctx);
357
358 // Due to how C# wrote it, it is effectively reversed
359 if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
360
361 if(crc64 != index_header.crc64)
362 {
363 FATAL("Expected index CRC 0x%16llX but got 0x%16llX.", index_header.crc64, crc64);
364 free(index_entries);
365
366 TRACE("Exiting verify_index_v2() = AARUF_ERROR_INVALID_BLOCK_CRC");
368 }
369
370 free(index_entries);
371
372 TRACE("Exiting verify_index_v2() = AARUF_OK");
373 return AARUF_STATUS_OK;
374}
#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
@ IndexBlock2
Block containing the index v2.
Definition enums.h:170
#define AARUF_STATUS_OK
Sector present and read without uncorrectable errors.
Definition errors.h:81
#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
UT_array * process_index_v2(aaruformat_context *ctx)
Processes an index block (version 2) from the image stream.
Definition index_v2.c:82
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:228
static int aaruf_fseek(FILE *stream, aaru_off_t offset, int origin)
Definition internal.h:46
int64_t aaru_off_t
Definition internal.h:42
#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
Index header (version 2) with 64‑bit entry counter (identifier == IndexBlock2).
Definition index.h:79
uint64_t entries
Number of IndexEntry records that follow immediately.
Definition index.h:81
uint32_t identifier
Block identifier (must be BlockType::IndexBlock2).
Definition index.h:80
uint64_t crc64
CRC64-ECMA of the entries array (legacy byte-swapped rule still applies for old versions).
Definition index.h:82
Master context representing an open or in‑creation Aaru image.
Definition context.h:175
AaruHeaderV2 header
Parsed container header (v2).
Definition context.h:178
FILE * imageStream
Underlying FILE* stream (binary mode).
Definition context.h:179
Minimal ECMA-182 CRC64 incremental state container (running value only).
Definition crc64.h:56