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
79UT_array *process_index_v1(aaruformat_context *ctx)
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
225int32_t verify_index_v1(aaruformat_context *ctx)
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");
239 return 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");
249 return 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");
261 return 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");
269 return 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.");
280 return AARUF_ERROR_NOT_ENOUGH_MEMORY;
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");
291 return 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");
305 return 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}
aaruformat.h
AARUF_VERSION_V1
#define AARUF_VERSION_V1
First on‑disk version (C# implementation).
Definition consts.h:71
aaruf_crc64_data
uint64_t aaruf_crc64_data(const uint8_t *data, uint32_t len)
Definition crc64.c:160
bswap_64
#define bswap_64(x)
Definition endian.h:81
IndexBlock
@ IndexBlock
Block containing the index (v1).
Definition enums.h:145
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_v1
UT_array * process_index_v1(aaruformat_context *ctx)
Processes an index block (version 1) from the image stream.
Definition index_v1.c:79
verify_index_v1
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
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
IndexHeader
Index header (version 1) for legacy images (identifier == IndexBlock).
Definition index.h:67
IndexHeader::crc64
uint64_t crc64
CRC64-ECMA of the entries array (legacy byte-swapped for early images).
Definition index.h:70
IndexHeader::identifier
uint32_t identifier
Block identifier (must be BlockType::IndexBlock).
Definition index.h:68
IndexHeader::entries
uint16_t entries
Number of IndexEntry records that follow immediately.
Definition index.h:69
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