utils: chunk parsing helper

This commit is contained in:
bnnm 2021-08-26 19:52:19 +02:00
parent cf36348b77
commit 374e1d3c32
5 changed files with 75 additions and 43 deletions

View File

@ -168,6 +168,8 @@
<ClInclude Include="coding\tac_decoder_lib_data.h" /> <ClInclude Include="coding\tac_decoder_lib_data.h" />
<ClInclude Include="coding\tac_decoder_lib_ops.h" /> <ClInclude Include="coding\tac_decoder_lib_ops.h" />
<ClInclude Include="layout\layout.h" /> <ClInclude Include="layout\layout.h" />
<ClInclude Include="util\chunks.h" />
<ClInclude Include="util\log.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="coding\at3plus_decoder.c" /> <ClCompile Include="coding\at3plus_decoder.c" />
@ -708,6 +710,7 @@
<ClCompile Include="layout\blocked_xa.c" /> <ClCompile Include="layout\blocked_xa.c" />
<ClCompile Include="layout\blocked_xa_aiff.c" /> <ClCompile Include="layout\blocked_xa_aiff.c" />
<ClCompile Include="layout\blocked_xvas.c" /> <ClCompile Include="layout\blocked_xvas.c" />
<ClCompile Include="util\chunks.c" />
<ClCompile Include="util\log.c" /> <ClCompile Include="util\log.c" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -302,6 +302,12 @@
<ClInclude Include="meta\encrypted_bgm_streamfile.h"> <ClInclude Include="meta\encrypted_bgm_streamfile.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="util\chunks.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="util\log.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="formats.c"> <ClCompile Include="formats.c">
@ -1918,6 +1924,9 @@
<ClCompile Include="meta\sbk.c"> <ClCompile Include="meta\sbk.c">
<Filter>meta\Source Files</Filter> <Filter>meta\Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="util\chunks.c">
<Filter>util\Source Files</Filter>
</ClCompile>
<ClCompile Include="util\log.c"> <ClCompile Include="util\log.c">
<Filter>util\Source Files</Filter> <Filter>util\Source Files</Filter>
</ClCompile> </ClCompile>

View File

@ -1,48 +1,6 @@
#include "meta.h" #include "meta.h"
#include "../coding/coding.h" #include "../coding/coding.h"
#include "../util/chunks.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;
}
enum { enum {
CHUNK_RIFF = 0x52494646, /* "RIFF" */ CHUNK_RIFF = 0x52494646, /* "RIFF" */

35
src/util/chunks.c Normal file
View File

@ -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;
}

27
src/util/chunks.h Normal file
View File

@ -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