streamfile cleanup

This commit is contained in:
bnnm 2021-10-10 15:09:58 +02:00
parent ce7ae456ce
commit 86c31d1481
5 changed files with 50 additions and 69 deletions

View File

@ -162,73 +162,57 @@ static int build_header_setup(uint8_t* buf, size_t bufsize, uint32_t setup_id, S
static int load_fvs_file_single(uint8_t* buf, size_t bufsize, uint32_t setup_id, STREAMFILE* sf) {
STREAMFILE* sf_setup = NULL;
/* get from artificial external file (used if compiled without codebooks) */
{
char setupname[PATH_LIMIT];
char pathname[PATH_LIMIT];
char *path;
char setupname[0x20];
/* read "(dir/).fvs_{setup_id}" */
sf->get_name(sf,pathname,sizeof(pathname));
path = strrchr(pathname,DIR_SEPARATOR);
if (path)
*(path+1) = '\0';
else
pathname[0] = '\0';
snprintf(setupname,PATH_LIMIT,"%s.fvs_%08x", pathname, setup_id);
sf_setup = sf->open(sf,setupname,STREAMFILE_DEFAULT_BUFFER_SIZE);
snprintf(setupname, sizeof(setupname), ".fvs_%08x", setup_id);
sf_setup = open_streamfile_by_filename(sf, setupname);
}
/* get codebook and copy to buffer */
if (sf_setup) {
/* file found, get contents into the buffer */
size_t bytes = sf_setup->get_size(sf_setup);
if (bytes > bufsize) goto fail;
if (read_streamfile(buf, 0, bytes, sf_setup) != bytes)
goto fail;
sf_setup->close(sf_setup);
close_streamfile(sf_setup);
return bytes;
}
fail:
if (sf_setup) sf_setup->close(sf_setup);
close_streamfile(sf_setup);
return 0;
}
static int load_fvs_file_multi(uint8_t* buf, size_t bufsize, uint32_t setup_id, STREAMFILE* sf) {
STREAMFILE* sf_setup = NULL;
/* from to get from artificial external file (used if compiled without codebooks) */
{
char setupname[PATH_LIMIT];
char pathname[PATH_LIMIT];
char* path;
char setupname[0x20];
/* read "(dir/).fvs" */
sf->get_name(sf,pathname,sizeof(pathname));
path = strrchr(pathname,DIR_SEPARATOR);
if (path)
*(path+1) = '\0';
else
pathname[0] = '\0';
snprintf(setupname,PATH_LIMIT,"%s.fvs", pathname);
sf_setup = sf->open(sf,setupname,STREAMFILE_DEFAULT_BUFFER_SIZE);
snprintf(setupname, sizeof(setupname), ".fvs");
sf_setup = open_streamfile_by_filename(sf, setupname);
}
/* find codebook in mini-header (format by bnnm, feel free to change) */
if (sf_setup) {
/* file found: read mini-header (format by bnnm, feel free to change) and locate FVS */
int entries, i;
uint32_t offset = 0, size = 0;
if (read_32bitBE(0x0, sf_setup) != 0x56465653) goto fail; /* "VFVS" */
entries = read_32bitLE(0x08, sf_setup); /* 0x04=v0, 0x0c-0x20: reserved */
if (!is_id32be(0x00, sf_setup, "VFVS"))
goto fail;
entries = read_u32le(0x08, sf_setup); /* 0x04=v0, 0x0c-0x20: reserved */
if (entries <= 0) goto fail;
for (i=0; i < entries; i++) { /* entry = id, offset, size, reserved */
if ((uint32_t)read_32bitLE(0x20 + i*0x10, sf_setup) == setup_id) {
offset = read_32bitLE(0x24 + i*0x10, sf_setup);
size = read_32bitLE(0x28 + i*0x10, sf_setup);
for (i = 0; i < entries; i++) { /* entry = id, offset, size, reserved */
if (read_u32le(0x20 + i*0x10, sf_setup) == setup_id) {
offset = read_u32le(0x24 + i*0x10, sf_setup);
size = read_u32le(0x28 + i*0x10, sf_setup);
break;
}
}

View File

@ -1144,27 +1144,18 @@ static int load_wvc(uint8_t* ibuf, size_t ibufsize, uint32_t codebook_id, wwise_
}
static int load_wvc_file(uint8_t* buf, size_t bufsize, uint32_t codebook_id, STREAMFILE* sf) {
STREAMFILE* sfWvc = NULL;
STREAMFILE* sf_setup = NULL;
size_t wvc_size = 0;
/* get from artificial external file (used if compiled without codebooks) */
{
char setupname[PATH_LIMIT];
char pathname[PATH_LIMIT];
char *path;
char setupname[0x20];
/* read "(dir/).wvc" */
sf->get_name(sf,pathname,sizeof(pathname));
path = strrchr(pathname,DIR_SEPARATOR);
if (path)
*(path+1) = '\0';
else
pathname[0] = '\0';
snprintf(setupname, sizeof(setupname), ".wvc");
sf_setup = open_streamfile_by_filename(sf, setupname);
if (!sf_setup) goto fail;
snprintf(setupname,PATH_LIMIT,"%s.wvc", pathname);
sfWvc = sf->open(sf,setupname,STREAMFILE_DEFAULT_BUFFER_SIZE);
if (!sfWvc) goto fail;
wvc_size = sfWvc->get_size(sfWvc);
wvc_size = get_streamfile_size(sf_setup);
}
/* find codebook and copy to buffer */
@ -1174,24 +1165,24 @@ static int load_wvc_file(uint8_t* buf, size_t bufsize, uint32_t codebook_id, STR
int codebook_count;
/* at the end of the WVC is an offset table, and we need to find codebook id (number) offset */
table_start = read_u32le(wvc_size - 4, sfWvc); /* last offset */
table_start = read_u32le(wvc_size - 4, sf_setup); /* last offset */
codebook_count = ((wvc_size - table_start) / 4) - 1;
if (table_start > wvc_size || codebook_id >= codebook_count) goto fail;
codebook_offset = read_u32le(table_start + codebook_id*4, sfWvc);
codebook_size = read_u32le(table_start + codebook_id*4 + 4, sfWvc) - codebook_offset;
codebook_offset = read_u32le(table_start + codebook_id*4, sf_setup);
codebook_size = read_u32le(table_start + codebook_id*4 + 4, sf_setup) - codebook_offset;
if (codebook_size > bufsize) goto fail;
if (read_streamfile(buf, codebook_offset, codebook_size, sfWvc) != codebook_size)
if (read_streamfile(buf, codebook_offset, codebook_size, sf_setup) != codebook_size)
goto fail;
sfWvc->close(sfWvc);
close_streamfile(sf_setup);
return codebook_size;
}
fail:
if (sfWvc) sfWvc->close(sfWvc);
close_streamfile(sf_setup);
return 0;
}

View File

@ -21,7 +21,7 @@ typedef struct _BARSTREAMFILE {
/*static*/ STREAMFILE *wrap_bar_STREAMFILE(STREAMFILE *file);
static size_t read_bar(BARSTREAMFILE *streamFile, uint8_t *dest, off_t offset, size_t length) {
static size_t read_bar(BARSTREAMFILE *streamFile, uint8_t *dest, offv_t offset, size_t length) {
off_t i;
size_t read_length = streamFile->real_file->read(streamFile->real_file, dest, offset, length);
@ -36,7 +36,7 @@ static size_t get_size_bar(BARSTREAMFILE *streamFile) {
return streamFile->real_file->get_size(streamFile->real_file);
}
static size_t get_offset_bar(BARSTREAMFILE *streamFile) {
static offv_t get_offset_bar(BARSTREAMFILE *streamFile) {
return streamFile->real_file->get_offset(streamFile->real_file);
}

View File

@ -1,12 +1,22 @@
#include "streamfile.h"
#include "util.h"
#include "vgmstream.h"
#include <string.h>
/* for dup/fdopen in some systems */
#ifndef _MSC_VER
#include <unistd.h>
#endif
//TODO: move
#ifndef DIR_SEPARATOR
#if defined (_WIN32) || defined (WIN32)
#define DIR_SEPARATOR '\\'
#else
#define DIR_SEPARATOR '/'
#endif
#endif
/* For (rarely needed) +2GB file support we use fseek64/ftell64. Those are usually available
* but may depend on compiler.
* - MSVC: +VS2008 should work

View File

@ -1,7 +1,6 @@
/*
* streamfile.h - definitions for buffered file reading with STREAMFILE
*/
#ifndef _STREAMFILE_H
#define _STREAMFILE_H
@ -9,9 +8,14 @@
#define _CRT_SECURE_NO_DEPRECATE
#endif
//TODO cleanup
//NULL, allocs
#include <stdlib.h>
//FILE
#include <stdio.h>
//string functions in meta and so on
#include <string.h>
//off_t
#include <sys/types.h>
#include "streamtypes.h"
#include "util.h"
@ -22,14 +26,6 @@
#include <io.h>
#endif
#ifndef DIR_SEPARATOR
#if defined (_WIN32) || defined (WIN32)
#define DIR_SEPARATOR '\\'
#else
#define DIR_SEPARATOR '/'
#endif
#endif
/* 64-bit offset is needed for banks that hit +2.5GB (like .fsb or .ktsl2stbin).
* Leave as typedef to toggle since it's theoretically slower when compiled as 32-bit.
* ATM it's only used in choice places until more performance tests are done.