74 TRACE(
"Entering process_data_block(%p, %p)", ctx, entry);
77 size_t read_bytes = 0;
79 uint8_t *cmp_data = NULL;
80 uint8_t *cst_data = NULL;
87 if(ctx == NULL || entry == NULL || ctx->
imageStream == NULL)
89 FATAL(
"Invalid context or image stream.");
97 FATAL(
"Could not seek to %" PRIu64
" as indicated by index entry...", entry->
offset);
107 TRACE(
"NoData block found, skipping");
108 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
112 TRACE(
"Reading block header at position %" PRIu64, entry->
offset);
117 FATAL(
"Could not read block header at %" PRIu64, entry->
offset);
119 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
130 TRACE(
"Setting sector size to %" PRIu64
" bytes", block_header.
sectorSize);
134 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
140 TRACE(
"Incorrect identifier for data block at position %" PRIu64, entry->
offset);
142 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
148 TRACE(
"Expected block with data type %4.4s at position %" PRIu64
" but found data type %4.4s",
151 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
155 TRACE(
"Found data block with type %4.4s at position %" PRIu64, (
char *)&entry->
blockType, entry->
offset);
162 TRACE(
"Invalid compression type %u for block with data type %u, continuing...", block_header.
compression,
165 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
171 TRACE(
"Compressed block length %" PRIu32
" too small for LZMA properties, continuing...",
173 TRACE(
"Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
179 cmp_data = (lzma_size == 0) ? NULL : (uint8_t *)malloc(lzma_size);
180 if(lzma_size != 0 && cmp_data == NULL)
182 TRACE(
"Cannot allocate memory for block, continuing...");
184 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
188 if(block_header.
length != 0)
190 data = (uint8_t *)malloc(block_header.
length);
193 TRACE(
"Cannot allocate memory for block, continuing...");
196 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
206 TRACE(
"Could not read LZMA properties, continuing...");
210 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
216 read_bytes = fread(cmp_data, 1, lzma_size, ctx->
imageStream);
217 if(read_bytes != lzma_size)
219 TRACE(
"Could not read compressed block, continuing...");
223 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
228 if(block_header.
length != 0)
230 read_bytes = block_header.
length;
236 TRACE(
"Got error %d from LZMA, continuing...", error_no);
240 TRACE(
"Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
244 if(read_bytes != block_header.
length)
246 TRACE(
"Error decompressing block, expected %" PRIu32
" bytes but got %zu bytes, continuing...",
247 block_header.
length, read_bytes);
251 TRACE(
"Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
255 else if(lzma_size != 0)
257 TRACE(
"Compressed payload present for zero-length block, continuing...");
260 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
266 cst_data = (uint8_t *)malloc(block_header.
length);
269 TRACE(
"Cannot allocate memory for block, continuing...");
273 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
289 TRACE(
"Invalid compression type %u for block with data type %u, continuing...", block_header.
compression,
291 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
295 cmp_data = (block_header.
cmpLength == 0) ? NULL : (uint8_t *)malloc(block_header.
cmpLength);
296 if(block_header.
cmpLength != 0 && cmp_data == NULL)
298 TRACE(
"Cannot allocate memory for compressed block, continuing...");
299 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
303 if(block_header.
length != 0)
305 data = (uint8_t *)malloc(block_header.
length);
308 TRACE(
"Cannot allocate memory for block, continuing...");
310 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
322 TRACE(
"Could not read compressed block, continuing...");
325 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
330 if(block_header.
length != 0)
334 if(decoded != block_header.
length)
336 TRACE(
"Error decompressing zstd block, expected %" PRIu32
" bytes but got %zu bytes, continuing...",
337 block_header.
length, decoded);
340 TRACE(
"Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
349 cst_data = (uint8_t *)malloc(block_header.
length);
352 TRACE(
"Cannot allocate memory for CST untransform, continuing...");
354 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
366 if(block_header.
length != 0)
368 data = (uint8_t *)malloc(block_header.
length);
371 TRACE(
"Cannot allocate memory for block, continuing...");
373 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
382 if(block_header.
length != 0 && read_bytes != block_header.
length)
385 TRACE(
"Could not read block, continuing...");
387 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
393 TRACE(
"Found unknown compression type %d, continuing...", block_header.
compression);
395 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
399 if(block_header.
length > 0)
406 if(crc64 != block_header.
crc64)
408 TRACE(
"Incorrect CRC found: 0x%" PRIx64
" found, expected 0x%" PRIx64
", continuing...", crc64,
412 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
490 if(media_tag == NULL)
492 TRACE(
"Cannot allocate memory for media tag entry.");
500 media_tag->
data = data;
503 HASH_REPLACE_INT(ctx->
mediaTags, type, media_tag, old_media_tag);
505 if(old_media_tag != NULL)
507 TRACE(
"Replaced media tag with type %d", old_media_tag->
type);
508 free(old_media_tag->
data);
510 old_media_tag = NULL;
516 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
#define LZMA_PROPERTIES_LENGTH
Size in bytes of the fixed LZMA properties header (lc/lp/pb + dictionary size).
#define AARUF_VERSION_V1
First on‑disk version (C# implementation).
int32_t process_data_block(aaruformat_context *ctx, IndexEntry *entry)
Processes a data block from the image stream.
uint64_t aaruf_crc64_data(const uint8_t *data, uint32_t len)
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.
int32_t aaruf_get_media_tag_type_for_datatype(int32_t type)
Converts an image data type to an Aaru media tag type.
size_t aaruf_zstd_decode_buffer(uint8_t *dst_buffer, size_t dst_size, const uint8_t *src_buffer, size_t src_size)
Decodes a Zstandard-compressed buffer.
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.
@ kDataTypeDvdTitleKeyDecrypted
Decrypted DVD Title Key.
@ kDataTypeDvdSectorId
DVD Identification Data (ID).
@ kDataTypeAppleSonyTag
Apple Sony (12‑byte) tag.
@ kDataTypeCdSectorSuffix
Compact Disc sector suffix (EDC, ECC P, ECC Q).
@ kDataTypeCdSubchannel
Compact Disc subchannel data.
@ kDataTypeCdSectorSuffixCorrected
Compact Disc sector suffix (EDC, ECC P, ECC Q) corrected-only stored.
@ kDataTypeBdSectorEdc
Blu-ray Sector EDC.
@ kDataTypeDvdSectorIed
DVD ID Error Detection Code (IED).
@ kDataTypeDvdSectorCprMai
DVD Copyright Management Information (CPR_MAI).
@ kDataTypePriamDataTowerTag
Priam Data Tower (24‑byte) tag.
@ kDataTypeUserData
User (main) data.
@ kDataTypeDvdSectorEdc
DVD Error Detection Code (EDC).
@ kDataTypeAppleProfileTag
Apple Profile (20‑byte) tag.
@ kDataTypeCdSubHeader
Compact Disc MODE 2 subheader.
@ kDataTypeCdSectorPrefix
Compact Disc sector prefix (sync, header).
@ kDataTypeCdSectorPrefixCorrected
Compact Disc sector prefix (sync, header) corrected-only stored.
@ kCompressionLzmaCst
LZMA applied to Claunia Subchannel Transform processed data.
@ kCompressionLzma
LZMA compression.
@ kCompressionNone
Not compressed.
@ kCompressionZstd
Zstandard compression.
@ kCompressionZstdCst
Zstandard applied to Claunia Subchannel Transform processed data.
#define AARUF_STATUS_OK
Sector present and read without uncorrectable errors.
#define AARUF_ERROR_CANNOT_READ_BLOCK
Generic block read failure (seek/read error).
#define AARUF_ERROR_NOT_AARUFORMAT
Input file/stream failed magic or structural validation.
#define AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK
Decompression routine failed or size mismatch.
static int aaruf_fseek(FILE *stream, aaru_off_t offset, int origin)
static aaru_off_t aaruf_ftell(FILE *stream)
uint32_t SectorSize
Size of each logical sector in bytes (512, 2048, 2352, 4096, etc.).
uint64_t ImageSize
Size of the image payload in bytes (excludes headers/metadata).
Single index entry describing a block's type, (optional) data classification, and file offset.
uint32_t blockType
Block identifier of the referenced block (value from BlockType).
uint64_t offset
Absolute byte offset in the image where the referenced block header begins.
uint16_t dataType
Data classification (value from DataType) or unused for untyped blocks.
Master context representing an open or in‑creation Aaru image.
uint8_t * sector_cpr_mai
DVD sector CPR_MAI (6 bytes) if present.
uint8_t * sector_prefix_corrected
Corrected variant (post error correction) if stored.
uint8_t * sector_ied
DVD sector IED (2 bytes) if present.
uint8_t * sector_prefix
Raw per-sector prefix (e.g., sync+header) uncorrected.
uint8_t * sector_edc
DVD sector EDC (4 bytes) if present.
uint8_t * sector_suffix
Raw per-sector suffix (EDC/ECC) uncorrected.
AaruHeaderV2 header
Parsed container header (v2).
mediaTagEntry * mediaTags
Hash table of extra media tags (uthash root).
uint8_t * sector_decrypted_title_key
DVD decrypted title key (5 bytes) if present.
uint8_t * sector_subchannel
Raw 96-byte subchannel (if captured).
FILE * imageStream
Underlying FILE* stream (binary mode).
uint8_t * mode2_subheaders
MODE2 Form1/Form2 8-byte subheaders (concatenated).
ImageInfo image_info
Exposed high-level image info summary.
uint8_t * sector_id
DVD sector ID (4 bytes) if present.
bool * readableSectorTags
Per-sector boolean array (optical tags read successfully?).
uint8_t * sector_suffix_corrected
Corrected suffix if stored separately.
Hash table entry for an arbitrary media tag (e.g., proprietary drive/medium descriptor).
uint8_t * data
Tag data blob (opaque to library core); length bytes long.
int32_t type
Numeric type identifier.
uint32_t length
Length in bytes of data.