From 374e1d3c3264593b1ebbdaaacbd0d8660470a7d9 Mon Sep 17 00:00:00 2001 From: bnnm Date: Thu, 26 Aug 2021 19:52:19 +0200 Subject: [PATCH] utils: chunk parsing helper --- src/libvgmstream.vcxproj | 3 +++ src/libvgmstream.vcxproj.filters | 9 +++++++ src/meta/dmsg_segh.c | 44 +------------------------------- src/util/chunks.c | 35 +++++++++++++++++++++++++ src/util/chunks.h | 27 ++++++++++++++++++++ 5 files changed, 75 insertions(+), 43 deletions(-) create mode 100644 src/util/chunks.c create mode 100644 src/util/chunks.h diff --git a/src/libvgmstream.vcxproj b/src/libvgmstream.vcxproj index 7f89668a..1dfd82c5 100644 --- a/src/libvgmstream.vcxproj +++ b/src/libvgmstream.vcxproj @@ -168,6 +168,8 @@ + + @@ -708,6 +710,7 @@ + diff --git a/src/libvgmstream.vcxproj.filters b/src/libvgmstream.vcxproj.filters index d3f751ef..179448be 100644 --- a/src/libvgmstream.vcxproj.filters +++ b/src/libvgmstream.vcxproj.filters @@ -302,6 +302,12 @@ Header Files + + Header Files + + + Header Files + @@ -1918,6 +1924,9 @@ meta\Source Files + + util\Source Files + util\Source Files diff --git a/src/meta/dmsg_segh.c b/src/meta/dmsg_segh.c index 900d7188..91142a9e 100644 --- a/src/meta/dmsg_segh.c +++ b/src/meta/dmsg_segh.c @@ -1,48 +1,6 @@ #include "meta.h" #include "../coding/coding.h" - -typedef struct { - uint32_t type; - uint32_t size; - uint32_t offset; - off_t current; - off_t max; - int le_type; - int be_size; - int full_size; -} chunk_t; - -static int next_chunk(chunk_t* chunk, STREAMFILE* sf) { - uint32_t (*read_u32type)(off_t,STREAMFILE*) = !chunk->le_type ? read_u32be : read_u32le; - uint32_t (*read_u32size)(off_t,STREAMFILE*) = chunk->be_size ? read_u32be : read_u32le; - - if (chunk->max == 0) - chunk->max = get_streamfile_size(sf); - - if (chunk->current >= chunk->max) - return 0; - /* can be used to signal "stop" */ - if (chunk->current < 0) - return 0; - - chunk->type = read_u32type(chunk->current + 0x00,sf); - chunk->size = read_u32size(chunk->current + 0x04,sf); - - chunk->offset = chunk->current + 0x04 + 0x04; - chunk->current += chunk->full_size ? chunk->size : 0x08 + chunk->size; - //;VGM_LOG("CHUNK: %x, %x, %x\n", dc.offset, chunk->type, chunk->size); - - /* read past data */ - if (chunk->type == 0xFFFFFFFF || chunk->size == 0xFFFFFFFF) - return 0; - - /* empty chunk with 0 size, seen in some formats (XVAG uses it as end marker, Wwise doesn't) */ - if (chunk->type == 0 || chunk->size == 0) - return 0; - - /* more chunks remain */ - return 1; -} +#include "../util/chunks.h" enum { CHUNK_RIFF = 0x52494646, /* "RIFF" */ diff --git a/src/util/chunks.c b/src/util/chunks.c new file mode 100644 index 00000000..21486586 --- /dev/null +++ b/src/util/chunks.c @@ -0,0 +1,35 @@ +#include "chunks.h" +//#include "log.h" + + +int next_chunk(chunk_t* chunk, STREAMFILE* sf) { + uint32_t (*read_u32type)(off_t,STREAMFILE*) = !chunk->le_type ? read_u32be : read_u32le; + uint32_t (*read_u32size)(off_t,STREAMFILE*) = chunk->be_size ? read_u32be : read_u32le; + + if (chunk->max == 0) + chunk->max = get_streamfile_size(sf); + + if (chunk->current >= chunk->max) + return 0; + /* can be used to signal "stop" */ + if (chunk->current < 0) + return 0; + + chunk->type = read_u32type(chunk->current + 0x00,sf); + chunk->size = read_u32size(chunk->current + 0x04,sf); + + chunk->offset = chunk->current + 0x04 + 0x04; + chunk->current += chunk->full_size ? chunk->size : 0x08 + chunk->size; + //;VGM_LOG("CHUNK: %x, %x, %x\n", dc.offset, chunk->type, chunk->size); + + /* read past data */ + if (chunk->type == 0xFFFFFFFF || chunk->size == 0xFFFFFFFF) + return 0; + + /* empty chunk with 0 size, seen in some formats (XVAG uses it as end marker, Wwise doesn't) */ + if (chunk->type == 0 || chunk->size == 0) + return 0; + + /* more chunks remain */ + return 1; +} diff --git a/src/util/chunks.h b/src/util/chunks.h new file mode 100644 index 00000000..0df77cc5 --- /dev/null +++ b/src/util/chunks.h @@ -0,0 +1,27 @@ +#ifndef _UTIL_CHUNKS_H +#define _UTIL_CHUNKS_H + +#include "../streamfile.h" + +typedef struct { + uint32_t type; /* chunk id/fourcc */ + uint32_t size; /* chunk size */ + uint32_t offset; /* chunk offset (after type/size) */ + off_t current; /* start position, or next chunk after size (set to -1 to break) */ + off_t max; /* max offset, or filesize if not set */ + + int le_type; /* read type as LE instead of more common BE */ + int be_size; /* read type as BE instead of more common LE */ + int full_size; /* chunk size includes type+size */ +} chunk_t; + +int next_chunk(chunk_t* chunk, STREAMFILE* sf); + +#if 0 +enum { + CHUNK_RIFF = 0x52494646, /* "RIFF" */ + CHUNK_LIST = 0x4C495354, /* "LIST" */ +}; +#endif + +#endif