75 TRACE(
"Entering process_data_block(%p, %p)", ctx, entry);
78 size_t read_bytes = 0;
80 uint8_t *cmp_data = NULL;
81 uint8_t *cst_data = NULL;
88 if(ctx == NULL || entry == NULL || ctx->
imageStream == NULL)
90 FATAL(
"Invalid context or image stream.");
98 FATAL(
"Could not seek to %" PRIu64
" as indicated by index entry...", entry->
offset);
108 TRACE(
"NoData block found, skipping");
109 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
113 TRACE(
"Reading block header at position %" PRIu64, entry->
offset);
118 FATAL(
"Could not read block header at %" PRIu64, entry->
offset);
120 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
131 TRACE(
"Setting sector size to %" PRIu64
" bytes", block_header.
sectorSize);
135 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
141 TRACE(
"Incorrect identifier for data block at position %" PRIu64, entry->
offset);
143 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
149 TRACE(
"Expected block with data type %4.4s at position %" PRIu64
" but found data type %4.4s",
152 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
156 TRACE(
"Found data block with type %4.4s at position %" PRIu64, (
char *)&entry->
blockType, entry->
offset);
163 TRACE(
"Invalid compression type %u for block with data type %u, continuing...", block_header.
compression,
166 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
172 TRACE(
"Compressed block length %" PRIu32
" too small for LZMA properties, continuing...",
174 TRACE(
"Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
180 cmp_data = (lzma_size == 0) ? NULL : (uint8_t *)malloc(lzma_size);
181 if(lzma_size != 0 && cmp_data == NULL)
183 TRACE(
"Cannot allocate memory for block, continuing...");
185 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
189 if(block_header.
length != 0)
191 data = (uint8_t *)malloc(block_header.
length);
194 TRACE(
"Cannot allocate memory for block, continuing...");
197 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
207 TRACE(
"Could not read LZMA properties, continuing...");
211 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
217 read_bytes = fread(cmp_data, 1, lzma_size, ctx->
imageStream);
218 if(read_bytes != lzma_size)
220 TRACE(
"Could not read compressed block, continuing...");
224 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
229 if(block_header.
length != 0)
231 read_bytes = block_header.
length;
237 TRACE(
"Got error %d from LZMA, continuing...", error_no);
241 TRACE(
"Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
245 if(read_bytes != block_header.
length)
247 TRACE(
"Error decompressing block, expected %" PRIu32
" bytes but got %zu bytes, continuing...",
248 block_header.
length, read_bytes);
252 TRACE(
"Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
256 else if(lzma_size != 0)
258 TRACE(
"Compressed payload present for zero-length block, continuing...");
261 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
267 cst_data = (uint8_t *)malloc(block_header.
length);
270 TRACE(
"Cannot allocate memory for block, continuing...");
274 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
290 TRACE(
"Invalid compression type %u for block with data type %u, continuing...", block_header.
compression,
292 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
296 cmp_data = (block_header.
cmpLength == 0) ? NULL : (uint8_t *)malloc(block_header.
cmpLength);
297 if(block_header.
cmpLength != 0 && cmp_data == NULL)
299 TRACE(
"Cannot allocate memory for compressed block, continuing...");
300 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
304 if(block_header.
length != 0)
306 data = (uint8_t *)malloc(block_header.
length);
309 TRACE(
"Cannot allocate memory for block, continuing...");
311 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
323 TRACE(
"Could not read compressed block, continuing...");
326 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
331 if(block_header.
length != 0)
335 if(decoded != block_header.
length)
337 TRACE(
"Error decompressing zstd block, expected %" PRIu32
" bytes but got %zu bytes, continuing...",
338 block_header.
length, decoded);
341 TRACE(
"Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
350 cst_data = (uint8_t *)malloc(block_header.
length);
353 TRACE(
"Cannot allocate memory for CST untransform, continuing...");
355 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
367 if(block_header.
length != 0)
369 data = (uint8_t *)malloc(block_header.
length);
372 TRACE(
"Cannot allocate memory for block, continuing...");
374 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
383 if(block_header.
length != 0 && read_bytes != block_header.
length)
386 TRACE(
"Could not read block, continuing...");
388 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
394 TRACE(
"Found unknown compression type %d, continuing...", block_header.
compression);
396 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
400 if(block_header.
length > 0)
407 if(crc64 != block_header.
crc64)
409 TRACE(
"Incorrect CRC found: 0x%" PRIx64
" found, expected 0x%" PRIx64, crc64, block_header.
crc64);
414 uint8_t *recovered = NULL;
415 uint32_t rec_size = 0;
418 TRACE(
"EC recovery succeeded for block at offset %" PRIu64, entry->
offset);
429 uint8_t *rec_payload = recovered + payload_offset;
430 uint32_t rec_payload_size = rec_size - payload_offset;
435 data = (uint8_t *)malloc(rec_hdr.
length);
436 if(data) memcpy(data, rec_payload, rec_hdr.
length);
440 data = (uint8_t *)malloc(rec_hdr.
length);
443 size_t out_sz = rec_hdr.
length;
444 size_t src_sz = rec_payload_size;
451 data = (uint8_t *)malloc(rec_hdr.
length);
457 if(data) memcpy(&block_header, &rec_hdr,
sizeof(
BlockHeader));
464 TRACE(
"Exiting process_data_block() = AARUF_STATUS_OK");
549 if(media_tag == NULL)
551 TRACE(
"Cannot allocate memory for media tag entry.");
559 media_tag->
data = data;
562 HASH_REPLACE_INT(ctx->
mediaTags, type, media_tag, old_media_tag);
564 if(old_media_tag != NULL)
566 TRACE(
"Replaced media tag with type %d", old_media_tag->
type);
567 free(old_media_tag->
data);
569 old_media_tag = NULL;
575 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).
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.
int32_t ec_recover_meta_block(aaruformat_context *ctx, uint64_t block_offset, uint8_t **recovered_data, uint32_t *recovered_size)
Attempt to recover a metadata/media-tag block at a given file offset.
#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.
int32_t process_data_block(aaruformat_context *ctx, IndexEntry *entry)
Processes a data block from the image stream.
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.
size_t sector_suffix_length
Length of sector_suffix.
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 or Blu-ray sector EDC (4 bytes) if present (see DvdSectorEdc / BdSectorEdc data types).
uint8_t * sector_suffix
Raw per-sector suffix (EDC/ECC) uncorrected.
AaruHeaderV2 header
Parsed container header (v2).
size_t sector_prefix_length
Length of sector_prefix.
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.