From 204d1f560fc3bd001f2c7ae91905d165b6d55f61 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sat, 8 Jul 2023 10:53:12 +0200 Subject: [PATCH] Fix TXTH dynamic chunks in some cases --- src/meta/txth.c | 9 ++++++++- src/meta/txth_streamfile.h | 8 +++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/meta/txth.c b/src/meta/txth.c index 73995bf8..6cc2a04e 100644 --- a/src/meta/txth.c +++ b/src/meta/txth.c @@ -142,6 +142,8 @@ typedef struct { int chunk_start_set; int chunk_size_set; int chunk_count_set; + int chunk_bsize_set; + int chunk_dsize_set; uint32_t base_offset; uint32_t is_offset_absolute; @@ -827,7 +829,8 @@ static void set_body_chunk(txth_header* txth) { //todo maybe should only be done once, or have some count to retrigger to simplify? if (!txth->chunk_start_set || !txth->chunk_size_set || !txth->chunk_count_set) return; - if (txth->chunk_size == 0 && !(txth->chunk_bsize_offset || txth->chunk_dsize_offset)) + + if (txth->chunk_size == 0 && !(txth->chunk_bsize_set || txth->chunk_dsize_set)) return; if (txth->chunk_start > txth->data_size || txth->chunk_count == 0) return; @@ -853,6 +856,8 @@ static void set_body_chunk(txth_header* txth) { cfg.chunk_bsize_offset = txth->chunk_bsize_offset; cfg.chunk_dsize_offset = txth->chunk_dsize_offset; cfg.chunk_be = txth->chunk_big_endian; + cfg.chunk_bsize_set = txth->chunk_bsize_set; + cfg.chunk_dsize_set = txth->chunk_dsize_set; cfg.chunk_start = txth->chunk_start; cfg.chunk_size = txth->chunk_size; @@ -1473,10 +1478,12 @@ static int parse_keyval(STREAMFILE* sf_, txth_header* txth, const char* key, cha else if (is_string(key,"chunk_size_offset")) { if (!parse_num(txth->sf_head,txth,val, &txth->chunk_bsize_offset)) goto fail; txth->chunk_size_set = 1; + txth->chunk_bsize_set = 1; } else if (is_string(key,"chunk_data_size_offset")) { if (!parse_num(txth->sf_head,txth,val, &txth->chunk_dsize_offset)) goto fail; txth->chunk_size_set = 1; + txth->chunk_dsize_set = 1; } else if (is_string(key,"chunk_endianness")) { if (!parse_endianness(txth, val, &txth->chunk_big_endian, NULL)) goto fail; diff --git a/src/meta/txth_streamfile.h b/src/meta/txth_streamfile.h index 7816ea3b..604550eb 100644 --- a/src/meta/txth_streamfile.h +++ b/src/meta/txth_streamfile.h @@ -17,6 +17,8 @@ typedef struct { uint32_t chunk_bsize_offset; uint32_t chunk_dsize_offset; int chunk_be; + int chunk_bsize_set; + int chunk_dsize_set; } txth_io_config_data; typedef struct { @@ -72,15 +74,15 @@ static size_t txth_io_read(STREAMFILE* sf, uint8_t* dest, off_t offset, size_t l } /* chunk size reader (overwrites the above) */ - if (data->cfg.chunk_header_size && (data->cfg.chunk_bsize_offset || data->cfg.chunk_dsize_offset)) { + if (data->cfg.chunk_header_size && (data->cfg.chunk_bsize_set || data->cfg.chunk_dsize_set)) { read_u32_t read_u32 = data->cfg.chunk_be ? read_u32be : read_u32le; data->block_size = 0; data->data_size = 0; - if (data->cfg.chunk_bsize_offset) + if (data->cfg.chunk_bsize_set) data->block_size = read_u32(data->physical_offset + data->cfg.chunk_bsize_offset, sf); - if (data->cfg.chunk_dsize_offset) + if (data->cfg.chunk_dsize_set) data->data_size = read_u32(data->physical_offset + data->cfg.chunk_dsize_offset, sf); if (!data->block_size && !data->data_size) { /* bad read? */