libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
data.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 <inttypes.h>
20#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24#include "aaruformat.h"
25#include "log.h"
26#include "uthash.h"
27
70
72{
73 TRACE("Entering process_data_block(%p, %p)", ctx, entry);
74 BlockHeader block_header;
75 int pos = 0;
76 size_t read_bytes = 0;
77 size_t lzma_size = 0;
78 uint8_t *cmp_data = NULL;
79 uint8_t *cst_data = NULL;
80 mediaTagEntry *old_media_tag = NULL;
81 mediaTagEntry *media_tag = NULL;
82 uint8_t *data = NULL;
83 uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH];
84
85 // Check if the context, index entry, and image stream are valid
86 if(ctx == NULL || entry == NULL || ctx->imageStream == NULL)
87 {
88 FATAL("Invalid context or image stream.");
90 }
91
92 // Seek to block
93 pos = fseek(ctx->imageStream, entry->offset, SEEK_SET);
94 if(pos < 0 || ftell(ctx->imageStream) != entry->offset)
95 {
96 FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset);
97
99 }
100
101 // Even if those two checks shall have been done before
102
103 // NOP block, skip
104 if(entry->dataType == NoData)
105 {
106 TRACE("NoData block found, skipping");
107 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
108 return AARUF_STATUS_OK;
109 }
110
111 TRACE("Reading block header at position %" PRIu64, entry->offset);
112 read_bytes = fread(&block_header, 1, sizeof(BlockHeader), ctx->imageStream);
113
114 if(read_bytes != sizeof(BlockHeader))
115 {
116 FATAL("Could not read block header at %" PRIu64, entry->offset);
117
118 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
119 return AARUF_STATUS_OK;
120 }
121
122 ctx->image_info.ImageSize += block_header.cmpLength;
123
124 // Unused, skip
125 if(entry->dataType == UserData)
126 {
127 if(block_header.sectorSize > ctx->image_info.SectorSize)
128 {
129 TRACE("Setting sector size to %" PRIu64 " bytes", block_header.sectorSize);
130 ctx->image_info.SectorSize = block_header.sectorSize;
131 }
132
133 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
134 return AARUF_STATUS_OK;
135 }
136
137 if(block_header.identifier != entry->blockType)
138 {
139 TRACE("Incorrect identifier for data block at position %" PRIu64, entry->offset);
140
141 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
142 return AARUF_STATUS_OK;
143 }
144
145 if(block_header.type != entry->dataType)
146 {
147 TRACE("Expected block with data type %4.4s at position %" PRIu64 " but found data type %4.4s",
148 (char *)&entry->blockType, entry->offset, (char *)&block_header.type);
149
150 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
151 return AARUF_STATUS_OK;
152 }
153
154 TRACE("Found data block with type %4.4s at position %" PRIu64, (char *)&entry->blockType, entry->offset);
155
156 if(block_header.compression == Lzma || block_header.compression == LzmaClauniaSubchannelTransform)
157 {
158 int error_no = 0;
159 if(block_header.compression == LzmaClauniaSubchannelTransform && block_header.type != CdSectorSubchannel)
160 {
161 TRACE("Invalid compression type %u for block with data type %u, continuing...", block_header.compression,
162 block_header.type);
163
164 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
165 return AARUF_STATUS_OK;
166 }
167
168 if(block_header.cmpLength < LZMA_PROPERTIES_LENGTH)
169 {
170 TRACE("Compressed block length %" PRIu32 " too small for LZMA properties, continuing...",
171 block_header.cmpLength);
172 TRACE("Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
174 }
175
176 lzma_size = block_header.cmpLength - LZMA_PROPERTIES_LENGTH;
177
178 cmp_data = (lzma_size == 0) ? NULL : (uint8_t *)malloc(lzma_size);
179 if(lzma_size != 0 && cmp_data == NULL)
180 {
181 TRACE("Cannot allocate memory for block, continuing...");
182
183 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
184 return AARUF_STATUS_OK;
185 }
186
187 if(block_header.length != 0)
188 {
189 data = (uint8_t *)malloc(block_header.length);
190 if(data == NULL)
191 {
192 TRACE("Cannot allocate memory for block, continuing...");
193 free(cmp_data);
194
195 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
196 return AARUF_STATUS_OK;
197 }
198 }
199 else
200 data = NULL;
201
202 read_bytes = fread(lzma_properties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
203 if(read_bytes != LZMA_PROPERTIES_LENGTH)
204 {
205 TRACE("Could not read LZMA properties, continuing...");
206 free(cmp_data);
207 free(data);
208
209 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
210 return AARUF_STATUS_OK;
211 }
212
213 if(lzma_size != 0)
214 {
215 read_bytes = fread(cmp_data, 1, lzma_size, ctx->imageStream);
216 if(read_bytes != lzma_size)
217 {
218 TRACE("Could not read compressed block, continuing...");
219 free(cmp_data);
220 free(data);
221
222 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
223 return AARUF_STATUS_OK;
224 }
225 }
226
227 if(block_header.length != 0)
228 {
229 read_bytes = block_header.length;
230 error_no = aaruf_lzma_decode_buffer(data, &read_bytes, cmp_data, &lzma_size, lzma_properties,
232
233 if(error_no != 0)
234 {
235 TRACE("Got error %d from LZMA, continuing...", error_no);
236 free(cmp_data);
237 free(data);
238
239 TRACE("Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
241 }
242
243 if(read_bytes != block_header.length)
244 {
245 TRACE("Error decompressing block, expected %" PRIu32 " bytes but got %zu bytes, continuing...",
246 block_header.length, read_bytes);
247 free(cmp_data);
248 free(data);
249
250 TRACE("Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
252 }
253 }
254 else if(lzma_size != 0)
255 {
256 TRACE("Compressed payload present for zero-length block, continuing...");
257 free(cmp_data);
258
259 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
260 return AARUF_STATUS_OK;
261 }
262
263 if(block_header.compression == LzmaClauniaSubchannelTransform && block_header.length != 0)
264 {
265 cst_data = (uint8_t *)malloc(block_header.length);
266 if(cst_data == NULL)
267 {
268 TRACE("Cannot allocate memory for block, continuing...");
269 free(cmp_data);
270 free(data);
271
272 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
273 return AARUF_STATUS_OK;
274 }
275
276 aaruf_cst_untransform(data, cst_data, block_header.length);
277 free(data);
278 data = cst_data;
279 cst_data = NULL;
280 }
281
282 free(cmp_data);
283 }
284 else if(block_header.compression == None)
285 {
286 if(block_header.length != 0)
287 {
288 data = (uint8_t *)malloc(block_header.length);
289 if(data == NULL)
290 {
291 TRACE("Cannot allocate memory for block, continuing...");
292
293 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
294 return AARUF_STATUS_OK;
295 }
296 }
297 else
298 data = NULL;
299
300 read_bytes = (block_header.length == 0) ? 0 : fread(data, 1, block_header.length, ctx->imageStream);
301
302 if(block_header.length != 0 && read_bytes != block_header.length)
303 {
304 free(data);
305 TRACE("Could not read block, continuing...");
306
307 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
308 return AARUF_STATUS_OK;
309 }
310 }
311 else
312 {
313 TRACE("Found unknown compression type %d, continuing...", block_header.compression);
314
315 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
316 return AARUF_STATUS_OK;
317 }
318
319 if(block_header.length > 0)
320 {
321 uint64_t crc64 = aaruf_crc64_data(data, block_header.length);
322
323 // Due to how C# wrote it, it is effectively reversed
324 if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
325
326 if(crc64 != block_header.crc64)
327 {
328 TRACE("Incorrect CRC found: 0x%" PRIx64 " found, expected 0x%" PRIx64 ", continuing...", crc64,
329 block_header.crc64);
330 free(data);
331
332 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
333 return AARUF_STATUS_OK;
334 }
335 }
336
337 // Check if it's not a media tag, but a sector tag, and fill the appropriate table then
338 switch(entry->dataType)
339 {
340 case CdSectorPrefix:
342 if(entry->dataType == CdSectorPrefixCorrected) { ctx->sector_prefix_corrected = data; }
343 else
344 ctx->sector_prefix = data;
345
346 ctx->readableSectorTags[CdSectorSync] = true;
348
349 break;
350 case CdSectorSuffix:
353 ctx->sector_suffix_corrected = data;
354 else
355 ctx->sector_suffix = data;
356
358 ctx->readableSectorTags[CdSectorEcc] = true;
359 ctx->readableSectorTags[CdSectorEccP] = true;
360 ctx->readableSectorTags[CdSectorEccQ] = true;
361 ctx->readableSectorTags[CdSectorEdc] = true;
362 break;
364 ctx->sector_subchannel = data;
366 break;
367 case AppleProfileTag:
368 case AppleSonyTag:
370 ctx->sector_subchannel = data;
372 break;
374 ctx->mode2_subheaders = data;
375 break;
376 case DvdSectorId:
377 ctx->sector_id = data;
380 break;
381 case DvdSectorIed:
382 ctx->sector_ied = data;
384 break;
385 case DvdSectorCprMai:
386 ctx->sector_cpr_mai = data;
387 ctx->readableSectorTags[DvdCmi] = true;
388 break;
389 case DvdSectorEdc:
390 ctx->sector_edc = data;
392 break;
394 ctx->sector_decrypted_title_key = data;
396 break;
397 default:
398 media_tag = (mediaTagEntry *)malloc(sizeof(mediaTagEntry));
399
400 if(media_tag == NULL)
401 {
402 TRACE("Cannot allocate memory for media tag entry.");
403 free(data);
404 data = NULL;
405 break;
406 }
407 memset(media_tag, 0, sizeof(mediaTagEntry));
408
409 media_tag->type = aaruf_get_media_tag_type_for_datatype(block_header.type);
410 media_tag->data = data;
411 media_tag->length = block_header.length;
412
413 HASH_REPLACE_INT(ctx->mediaTags, type, media_tag, old_media_tag);
414
415 if(old_media_tag != NULL)
416 {
417 TRACE("Replaced media tag with type %d", old_media_tag->type);
418 free(old_media_tag->data);
419 free(old_media_tag);
420 old_media_tag = NULL;
421 }
422
423 break;
424 }
425
426 TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
427 return AARUF_STATUS_OK;
428}
#define LZMA_PROPERTIES_LENGTH
Size in bytes of the fixed LZMA properties header (lc/lp/pb + dictionary size).
Definition consts.h:82
#define AARUF_VERSION_V1
First on‑disk version (C# implementation).
Definition consts.h:71
int32_t process_data_block(aaruformat_context *ctx, IndexEntry *entry)
Processes a data block from the image stream.
Definition data.c:71
uint64_t aaruf_crc64_data(const uint8_t *data, uint32_t len)
Definition crc64.c:160
int32_t aaruf_lzma_decode_buffer(uint8_t *dst_buffer, size_t *dst_size, const uint8_t *src_buffer, size_t *src_size, const uint8_t *props, size_t props_size)
Decodes an LZMA-compressed buffer.
Definition lzma.c:39
int32_t aaruf_get_media_tag_type_for_datatype(int32_t type)
Converts an image data type to an Aaru media tag type.
Definition helpers.c:39
int32_t aaruf_cst_untransform(const uint8_t *sequential, uint8_t *interleaved, size_t length)
Reverses the CST (Claunia's Subchannel Transform) transformation from sequential to interleaved data.
Definition cst.c:193
#define bswap_64(x)
Definition endian.h:81
@ CdSectorSubchannel
Compact Disc subchannel data.
Definition enums.h:116
@ AppleProfileTag
Apple Profile (20‑byte) tag.
Definition enums.h:117
@ DvdSectorIed
DVD ID Error Detection Code (IED)
Definition enums.h:129
@ CdSectorPrefixCorrected
Compact Disc sector prefix (sync, header) corrected-only stored.
Definition enums.h:121
@ DvdSectorCprMai
DVD Copyright Management Information (CPR_MAI)
Definition enums.h:126
@ AppleSonyTag
Apple Sony (12‑byte) tag.
Definition enums.h:118
@ CdSectorPrefix
Compact Disc sector prefix (sync, header).
Definition enums.h:114
@ CdSectorSuffixCorrected
Compact Disc sector suffix (EDC, ECC P, ECC Q) corrected-only stored.
Definition enums.h:122
@ PriamDataTowerTag
Priam Data Tower (24‑byte) tag.
Definition enums.h:119
@ UserData
User (main) data.
Definition enums.h:46
@ NoData
No data.
Definition enums.h:45
@ DvdSectorEdc
DVD Error Detection Code (EDC)
Definition enums.h:130
@ DvdSectorTitleKeyDecrypted
Decrypted DVD Title Key.
Definition enums.h:127
@ DvdSectorId
DVD Identification Data (ID)
Definition enums.h:128
@ CdSectorSuffix
Compact Disc sector suffix (EDC, ECC P, ECC Q).
Definition enums.h:115
@ CompactDiscMode2Subheader
Compact Disc MODE 2 subheader.
Definition enums.h:123
@ Lzma
LZMA compression.
Definition enums.h:34
@ LzmaClauniaSubchannelTransform
LZMA applied to Claunia Subchannel Transform processed data.
Definition enums.h:36
@ None
Not compressed.
Definition enums.h:33
#define AARUF_STATUS_OK
Sector present and read without uncorrectable errors.
Definition errors.h:75
#define AARUF_ERROR_CANNOT_READ_BLOCK
Generic block read failure (seek/read error).
Definition errors.h:46
#define AARUF_ERROR_NOT_AARUFORMAT
Input file/stream failed magic or structural validation.
Definition errors.h:40
#define AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK
Decompression routine failed or size mismatch.
Definition errors.h:56
@ DvdSectorIedAaru
DVD sector ID error detection, 2 bytes.
Definition aaru.h:914
@ CdSectorHeader
4-byte CD header (minute, second, frame, mode)
Definition aaru.h:898
@ CdSectorEdc
32-bit CRC (EDC)
Definition aaru.h:900
@ CdSectorEccP
172 bytes Reed-Solomon ECC (P)
Definition aaru.h:901
@ DvdSectorEdcAaru
DVD sector EDC, 4 bytes.
Definition aaru.h:915
@ DvdCmi
DVD Copyright Management Information (CSS)
Definition aaru.h:908
@ CdSectorSubHeader
Mode 2 Form subheader (8 bytes: copy, submode, channel)
Definition aaru.h:899
@ DvdSectorNumber
DVD sector number, 3 bytes.
Definition aaru.h:913
@ DvdSectorInformation
DVD sector information, 1 bytes.
Definition aaru.h:912
@ AppleSectorTag
Apple's GCR sector tags, 12 bytes (address prolog + checksum)
Definition aaru.h:896
@ CdSectorEcc
Combined P+Q ECC (276 bytes)
Definition aaru.h:903
@ DvdTitleKeyDecrypted
Decrypted DVD sector title key, 5 bytes.
Definition aaru.h:911
@ CdSectorSync
12-byte CD sync pattern (00 FF*10 00)
Definition aaru.h:897
@ CdSectorEccQ
104 bytes Reed-Solomon ECC (Q)
Definition aaru.h:902
#define FATAL(fmt,...)
Definition log.h:40
#define TRACE(fmt,...)
Definition log.h:25
uint8_t imageMajorVersion
Container format major version.
Definition header.h:110
Header preceding the compressed data payload of a data block (BlockType::DataBlock).
Definition data.h:71
uint32_t cmpLength
Size in bytes of the compressed payload immediately following this header.
Definition data.h:76
uint32_t length
Size in bytes of the uncompressed payload resulting after decompression.
Definition data.h:77
uint32_t identifier
Block identifier, must be BlockType::DataBlock.
Definition data.h:72
uint32_t sectorSize
Size in bytes of each logical sector represented in this block.
Definition data.h:75
uint64_t crc64
CRC64-ECMA of the uncompressed payload (length bytes).
Definition data.h:79
uint16_t type
Logical data classification (value from DataType).
Definition data.h:73
uint16_t compression
Compression algorithm used (value from CompressionType).
Definition data.h:74
uint32_t SectorSize
Size of each logical sector in bytes (512, 2048, 2352, 4096, etc.)
Definition aaru.h:875
uint64_t ImageSize
Size of the image payload in bytes (excludes headers/metadata)
Definition aaru.h:873
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
uint16_t dataType
Data classification (value from DataType) or unused for untyped blocks.
Definition index.h:111
Master context representing an open or in‑creation Aaru image.
Definition context.h:172
uint8_t * sector_cpr_mai
DVD sector CPR_MAI (6 bytes) if present.
Definition context.h:207
uint8_t * sector_prefix_corrected
Corrected variant (post error correction) if stored.
Definition context.h:200
uint8_t * sector_ied
DVD sector IED (2 bytes) if present.
Definition context.h:206
uint8_t * sector_prefix
Raw per-sector prefix (e.g., sync+header) uncorrected.
Definition context.h:199
uint8_t * sector_edc
DVD sector EDC (4 bytes) if present.
Definition context.h:208
uint8_t * sector_suffix
Raw per-sector suffix (EDC/ECC) uncorrected.
Definition context.h:201
AaruHeaderV2 header
Parsed container header (v2).
Definition context.h:175
mediaTagEntry * mediaTags
Hash table of extra media tags (uthash root).
Definition context.h:264
uint8_t * sector_decrypted_title_key
DVD decrypted title key (5 bytes) if present.
Definition context.h:209
uint8_t * sector_subchannel
Raw 96-byte subchannel (if captured).
Definition context.h:203
FILE * imageStream
Underlying FILE* stream (binary mode).
Definition context.h:176
uint8_t * mode2_subheaders
MODE2 Form1/Form2 8-byte subheaders (concatenated).
Definition context.h:204
ImageInfo image_info
Exposed high-level image info summary.
Definition context.h:260
uint8_t * sector_id
DVD sector ID (4 bytes) if present.
Definition context.h:205
bool * readableSectorTags
Per-sector boolean array (optical tags read successfully?).
Definition context.h:263
uint8_t * sector_suffix_corrected
Corrected suffix if stored separately.
Definition context.h:202
Hash table entry for an arbitrary media tag (e.g., proprietary drive/medium descriptor).
Definition context.h:119
uint8_t * data
Tag data blob (opaque to library core); length bytes long.
Definition context.h:120
int32_t type
Numeric type identifier.
Definition context.h:121
uint32_t length
Length in bytes of data.
Definition context.h:122