Loading...
Searching...
No Matches
ddt_v1.c File Reference
`#include <inttypes.h>`
`#include <stdint.h>`
`#include <stdio.h>`
`#include <stdlib.h>`
`#include "errors.h"`
`#include "aaruformat.h"`
`#include "log.h"`

Go to the source code of this file.

## Functions

int32_t | process_ddt_v1 (aaruformat_context *ctx, IndexEntry *entry, bool *found_user_data_ddt)
| Processes a DDT v1 block from the image stream.
int32_t | decode_ddt_entry_v1 (aaruformat_context *ctx, const uint64_t sector_address, uint64_t *offset, uint64_t *block_offset, uint8_t *sector_status)
| Decodes a DDT v1 entry for a given sector address.

## Function Documentation

## decode_ddt_entry_v1()

int32_t decode_ddt_entry_v1 | ( | aaruformat_context * | ctx,
|  | const uint64_t | sector_address,
|  | uint64_t * | offset,
|  | uint64_t * | block_offset,
|  | uint8_t * | sector_status )

Decodes a DDT v1 entry for a given sector address.

Determines the offset and block offset for a sector using the DDT v1 table. This function performs bit manipulation on the DDT entry to extract the sector offset within a block and the block offset, and determines whether the sector was dumped or not based on the DDT entry value.

Parameters

  ctx | Pointer to the aaruformat context containing the loaded DDT table.
  sector_address | Logical sector address to decode (must be within valid range).
  offset | Pointer to store the resulting sector offset within the block.
  block_offset | Pointer to store the resulting block offset in the image.
  sector_status | Pointer to store the sector status (dumped or not dumped).

Returns
  Returns one of the following status codes:

Return values

  AARUF_STATUS_OK

  (0) Successfully decoded the DDT entry. This is always returned when:
  - The context and image stream are valid
  - The DDT entry is successfully extracted and decoded
  - The offset, block_offset, and sector_status are successfully populated
  - A zero DDT entry is encountered (indicates sector not dumped)
  - A non-zero DDT entry is encountered (indicates sector was dumped)

  AARUF_ERROR_NOT_AARUFORMAT | (-1) The context or image stream is invalid (NULL pointers). This is the only error condition that can occur in this function.

Note
  This is a lightweight function that performs only basic validation and bit manipulation. It does not perform bounds checking on the sector_address parameter.
  DDT Entry Decoding:
  - Uses a bit mask derived from ctx->shift to extract the offset within block
  - Right-shifts the DDT entry by ctx->shift bits to get the block offset
  - A zero DDT entry indicates the sector was not dumped (SectorStatusNotDumped)
  - A non-zero DDT entry indicates the sector was dumped (SectorStatusDumped)

Warning
  The function assumes:
  - The DDT table (ctx->userDataDdt) has been properly loaded by process_ddt_v1()
  - The sector_address is within the valid range of the DDT table
  - The shift value (ctx->shift) has been properly initialized
  - All output parameters (offset, block_offset, sector_status) are valid pointers
  No bounds checking is performed on sector_address. Accessing beyond the DDT table boundaries will result in undefined behavior.

Definition at line 405 of file ddt_v1.c.

References AARUF_ERROR_INCORRECT_DATA_SIZE, AARUF_ERROR_NOT_AARUFORMAT, AARUF_STATUS_OK, FATAL, aaruformat_context::imageStream, SectorStatusDumped, SectorStatusNotDumped, aaruformat_context::shift, TRACE, and aaruformat_context::user_data_ddt.

Referenced by aaruf_read_sector().

## process_ddt_v1()

int32_t process_ddt_v1 | ( | aaruformat_context * | ctx,
|  | IndexEntry * | entry,
|  | bool * | found_user_data_ddt )

Processes a DDT v1 block from the image stream.

Reads and decompresses (if needed) a DDT v1 block, verifies its integrity, and loads it into memory or maps it. This function handles both user data DDT blocks and CD sector prefix/suffix corrected DDT blocks, supporting both LZMA compression and uncompressed formats. On Linux, uncompressed blocks can be memory-mapped for improved performance.

Parameters

  ctx | Pointer to the aaruformat context.
  entry | Pointer to the index entry describing the DDT block.
  found_user_data_ddt | Pointer to a boolean that will be set to true if a user data DDT was found and loaded.

Returns
  Returns one of the following status codes:

Return values

  AARUF_STATUS_OK

  (0) Successfully processed the DDT block. This is returned when:
  - The DDT block is successfully read, decompressed (if needed), and loaded into memory
  - Memory mapping of uncompressed DDT succeeds on Linux systems
  - CD sector prefix/suffix corrected DDT blocks are processed successfully
  - Memory allocation failures occur for non-critical operations (processing continues)
  - File reading errors occur for compressed data or LZMA properties (processing continues)
  - Unknown compression types are encountered (block is skipped)
  - Memory mapping fails on Linux (sets found_user_data_ddt to false but continues)
  - Uncompressed DDT is encountered on non-Linux systems (not yet implemented, continues)

  AARUF_ERROR_NOT_AARUFORMAT | (-1) The context or image stream is invalid (NULL pointers).

  AARUF_ERROR_CANNOT_READ_BLOCK

  (-7) Failed to access the DDT block in the image stream. This occurs when:
  - fseek() fails to position at the DDT block offset
  - The file position doesn't match the expected offset after seeking
  - Failed to read the DDT header from the image stream
  - The number of bytes read for the DDT header is insufficient

  AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK

  (-17) LZMA decompression failed. This can happen when:
  - The LZMA decoder returns a non-zero error code during decompression
  - The decompressed data size doesn't match the expected DDT block length
  - This error causes immediate function termination and memory cleanup

Note
  The function exhibits different error handling strategies depending on the operation:
  - Critical errors (seek failures, header read failures, decompression failures) cause immediate return
  - Non-critical errors (memory allocation failures, unknown compression types) allow processing to continue
  - The found_user_data_ddt flag is updated to reflect the success of user data DDT loading
  Memory Management:
  - Allocated DDT data is stored in the context (ctx->userDataDdt, ctx->sectorPrefixDdt, ctx->sectorSuffixDdt)
  - On Linux, uncompressed DDTs may be memory-mapped instead of allocated
  - Memory is automatically cleaned up on decompression errors
  Platform-specific behavior:
  - Linux: Supports memory mapping of uncompressed DDT blocks for better performance
  - Non-Linux: Uncompressed DDT processing is not yet implemented

Warning
  The function modifies context state including sector count, shift value, and DDT version. Ensure proper context cleanup when the function completes.

Definition at line 85 of file ddt_v1.c.

References AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK, AARUF_ERROR_CANNOT_READ_BLOCK, AARUF_ERROR_NOT_AARUFORMAT, aaruf_lzma_decode_buffer(), AARUF_STATUS_OK, CdSectorPrefixCorrected, CdSectorSuffixCorrected, DdtHeader::cmpLength, DdtHeader::compression, IndexEntry::dataType, aaruformat_context::ddt_version, DdtHeader::entries, FATAL, aaruformat_context::image_info, ImageInfo::ImageSize, aaruformat_context::imageStream, aaruformat_context::in_memory_ddt, DdtHeader::length, Lzma, LZMA_PROPERTIES_LENGTH, None, IndexEntry::offset, aaruformat_context::sector_prefix_ddt, aaruformat_context::sector_suffix_ddt, ImageInfo::Sectors, aaruformat_context::shift, DdtHeader::shift, TRACE, aaruformat_context::user_data_ddt, and UserData.

Referenced by aaruf_open().