From 5c7610c1c094238f537d14a89c2a2cb1ad9d20ef Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Thu, 2 Oct 2025 04:40:16 +0100 Subject: [PATCH] Use heuristics to setup table shift. --- include/aaruformat/structs/options.h | 2 +- src/create.c | 13 ++++++++++++- src/options.c | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/aaruformat/structs/options.h b/include/aaruformat/structs/options.h index 7c7c4c6..9748b35 100644 --- a/include/aaruformat/structs/options.h +++ b/include/aaruformat/structs/options.h @@ -219,7 +219,7 @@ typedef struct bool deduplicate; ///< Storage dedup flag (DDT always exists). true=share identical sector content, false=store ///< each instance. uint32_t dictionary; ///< LZMA dictionary size in bytes (>= 4096 recommended). Default: 33554432 (32 MiB). - uint8_t table_shift; ///< DDT table shift (multi-level fan-out exponent). Default: 9. + int8_t table_shift; ///< DDT table shift (multi-level fan-out exponent). Default: heuristically calculated. uint8_t data_shift; ///< Global data shift: low bits encode sector offset inside a block (2^data_shift span). uint8_t block_alignment; ///< log2 underlying block alignment (2^n bytes). Default: 9 (512 bytes). bool md5; ///< Generate MD5 checksum (ChecksumAlgorithm::Md5) when finalizing image. diff --git a/src/create.c b/src/create.c index 94cb84a..f70a0c8 100644 --- a/src/create.c +++ b/src/create.c @@ -231,10 +231,21 @@ void *aaruf_create(const char *filepath, const uint32_t media_type, const uint32 ctx->userDataDdtHeader.start = 0; ctx->userDataDdtHeader.blockAlignmentShift = parsed_options.block_alignment; ctx->userDataDdtHeader.dataShift = parsed_options.data_shift; - ctx->userDataDdtHeader.tableShift = parsed_options.table_shift; ctx->userDataDdtHeader.sizeType = 1; ctx->userDataDdtHeader.entries = ctx->userDataDdtHeader.blocks / (1 << ctx->userDataDdtHeader.tableShift); + if(parsed_options.table_shift == -1) + { + uint64_t total_sectors = user_sectors + overflow_sectors + negative_sectors; + + if(total_sectors < 0x8388608ULL) + ctx->userDataDdtHeader.tableShift = 0; + else + ctx->userDataDdtHeader.tableShift = 22; + } + else + ctx->userDataDdtHeader.tableShift = parsed_options.table_shift; + if(ctx->userDataDdtHeader.blocks % (1 << ctx->userDataDdtHeader.tableShift) != 0) ctx->userDataDdtHeader.entries++; TRACE("Initializing primary/single DDT"); diff --git a/src/options.c b/src/options.c index d760de7..50234f4 100644 --- a/src/options.c +++ b/src/options.c @@ -42,7 +42,7 @@ aaru_options parse_options(const char *options) aaru_options parsed = {.compress = true, .deduplicate = true, .dictionary = 33554432, - .table_shift = 9, + .table_shift = -1, .data_shift = 12, .block_alignment = 9, .md5 = false,