libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
index_v1.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 <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22
23#include "aaruformat.h"
24#include "log.h"
25#include "utarray.h"
26
80{
81 TRACE("Entering process_index_v1(%p)", ctx);
82
83 UT_array *index_entries = NULL;
84 IndexEntry entry;
85
86 if(ctx == NULL || ctx->imageStream == NULL) return NULL;
87
88 // Initialize the index entries array
89 const UT_icd index_entry_icd = {sizeof(IndexEntry), NULL, NULL, NULL};
90
91 utarray_new(index_entries, &index_entry_icd);
92
93 if(index_entries == NULL)
94 {
95 FATAL("Could not allocate memory for index entries array.");
96 TRACE("Exiting process_index_v1() = NULL");
97 return NULL;
98 }
99
100 // Read the index header
101 TRACE("Reading index header at position %llu", ctx->header.indexOffset);
102 if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0)
103 {
104 FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset);
105 utarray_free(index_entries);
106
107 TRACE("Exiting process_index_v1() = NULL");
108 return NULL;
109 }
110
111 IndexHeader idx_header;
112 if(fread(&idx_header, sizeof(IndexHeader), 1, ctx->imageStream) != 1)
113 {
114 FATAL("Could not read index header at %llu.", ctx->header.indexOffset);
115 utarray_free(index_entries);
116
117 TRACE("Exiting process_index_v1() = NULL");
118 return NULL;
119 }
120
121 // Check if the index header is valid
122 if(idx_header.identifier != IndexBlock)
123 {
124 FATAL("Incorrect index identifier.");
125 utarray_free(index_entries);
126
127 TRACE("Exiting process_index_v1() = NULL");
128 return NULL;
129 }
130
131 for(int i = 0; i < idx_header.entries; i++)
132 {
133 if(fread(&entry, sizeof(IndexEntry), 1, ctx->imageStream) != 1)
134 {
135 FATAL("Could not read index entry %d at %llu.", i, ctx->header.indexOffset);
136 utarray_free(index_entries);
137
138 TRACE("Exiting process_index_v1() = NULL");
139 return NULL;
140 }
141
142 utarray_push_back(index_entries, &entry);
143 }
144
145 TRACE("Read %d index entries from index block at position %llu", idx_header.entries, ctx->header.indexOffset);
146 return index_entries;
147}
148
226{
227 TRACE("Entering verify_index_v1(%p)", ctx);
228
229 size_t read_bytes = 0;
230 IndexHeader index_header;
231 uint64_t crc64 = 0;
232 IndexEntry *index_entries = NULL;
233
234 if(ctx == NULL || ctx->imageStream == NULL)
235 {
236 FATAL("Invalid context or image stream.");
237
238 TRACE("Exiting verify_index_v1() = AARUF_ERROR_NOT_AARUFORMAT");
240 }
241
242 // This will traverse all blocks and check their CRC64 without uncompressing them
243 TRACE("Checking index integrity at %llu.", ctx->header.indexOffset);
244 if(fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET) != 0)
245 {
246 FATAL("Could not seek to index header at %llu.", ctx->header.indexOffset);
247
248 TRACE("Exiting verify_index_v1() = AARUF_ERROR_CANNOT_READ_HEADER");
250 }
251
252 // Read the index header
253 TRACE("Reading index header at position %llu", ctx->header.indexOffset);
254 read_bytes = fread(&index_header, 1, sizeof(IndexHeader), ctx->imageStream);
255
256 if(read_bytes != sizeof(IndexHeader))
257 {
258 FATAL("Could not read index header.");
259
260 TRACE("Exiting verify_index_v1() = AARUF_ERROR_CANNOT_READ_HEADER");
262 }
263
264 if(index_header.identifier != IndexBlock)
265 {
266 FATAL("Incorrect index identifier.");
267
268 TRACE("Exiting verify_index_v1() = AARUF_ERROR_CANNOT_READ_INDEX");
270 }
271
272 TRACE("Index at %llu contains %d entries.", ctx->header.indexOffset, index_header.entries);
273 fprintf(stderr, "Index at %llu contains %d entries.", ctx->header.indexOffset, index_header.entries);
274
275 index_entries = malloc(sizeof(IndexEntry) * index_header.entries);
276
277 if(index_entries == NULL)
278 {
279 FATAL("Cannot allocate memory for index entries.");
281 }
282
283 read_bytes = fread(index_entries, 1, sizeof(IndexEntry) * index_header.entries, ctx->imageStream);
284
285 if(read_bytes != sizeof(IndexEntry) * index_header.entries)
286 {
287 FATAL("Could not read index entries.");
288 free(index_entries);
289
290 TRACE("Exiting verify_index_v1() = AARUF_ERROR_CANNOT_READ_INDEX");
292 }
293
294 crc64 = aaruf_crc64_data((const uint8_t *)index_entries, sizeof(IndexEntry) * index_header.entries);
295
296 // Due to how C# wrote it, it is effectively reversed
297 if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
298
299 if(crc64 != index_header.crc64)
300 {
301 FATAL("Expected index CRC 0x%16llX but got 0x%16llX.", index_header.crc64, crc64);
302 free(index_entries);
303
304 TRACE("Exiting verify_index_v1() = AARUF_ERROR_INVALID_BLOCK_CRC");
306 }
307
308 free(index_entries);
309
310 TRACE("Exiting verify_index_v1() = AARUF_OK");
311 return AARUF_STATUS_OK;
312}
#define AARUF_VERSION_V1
First on‑disk version (C# implementation).
Definition consts.h:71
uint64_t aaruf_crc64_data(const uint8_t *data, uint32_t len)
Definition crc64.c:160
#define bswap_64(x)
Definition endian.h:81
@ IndexBlock
Block containing the index (v1).
Definition enums.h:145
#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
UT_array * process_index_v1(aaruformat_context *ctx)
Processes an index block (version 1) from the image stream.
Definition index_v1.c:79
int32_t verify_index_v1(aaruformat_context *ctx)
Verifies the integrity of an index block (version 1) in the image stream.
Definition index_v1.c:225
#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 1) for legacy images (identifier == IndexBlock).
Definition index.h:67
uint64_t crc64
CRC64-ECMA of the entries array (legacy byte-swapped for early images).
Definition index.h:70
uint32_t identifier
Block identifier (must be BlockType::IndexBlock).
Definition index.h:68
uint16_t entries
Number of IndexEntry records that follow immediately.
Definition index.h:69
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