2008-08-02 05:15:24 +02:00
|
|
|
#include "streamfile.h"
|
|
|
|
#include "util.h"
|
2017-01-14 00:59:54 +01:00
|
|
|
#include "vgmstream.h"
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2021-09-12 20:05:56 +02:00
|
|
|
/* for dup/fdopen in some systems */
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2017-04-29 16:03:11 +02:00
|
|
|
|
2021-09-12 21:29:07 +02:00
|
|
|
/* For (rarely needed) +2GB file support we use fseek64/ftell64. Those are usually available
|
|
|
|
* but may depend on compiler.
|
|
|
|
* - MSVC: +VS2008 should work
|
|
|
|
* - GCC/MingW: should be available
|
|
|
|
* - GCC/Linux: should be available but some systems may need __USE_FILE_OFFSET64,
|
|
|
|
* that we (probably) don't want since that turns off_t to off64_t
|
|
|
|
* - Clang: seems only defined on Linux/GNU environments, somehow emscripten is out
|
|
|
|
* (unsure about Clang Win since apparently they define _MSC_VER)
|
|
|
|
* - Android: API +24 if not using __USE_FILE_OFFSET64
|
2021-09-15 23:09:19 +02:00
|
|
|
* Not sure if fopen64 is needed in some cases. May be worth adding some compiler flag to enable 64 versions manually.
|
2021-09-12 21:29:07 +02:00
|
|
|
*/
|
|
|
|
|
2021-09-04 21:57:23 +02:00
|
|
|
/* MSVC fixes (though mingw uses MSVCRT but not MSC_VER, maybe use AND?) */
|
|
|
|
#if defined(__MSVCRT__) || defined(_MSC_VER)
|
|
|
|
#include <io.h>
|
|
|
|
|
2021-09-12 21:29:07 +02:00
|
|
|
#define fopen_v fopen
|
|
|
|
#if (_MSC_VER >= 1400)
|
|
|
|
#define fseek_v _fseeki64
|
|
|
|
#define ftell_v _ftelli64
|
|
|
|
#else
|
|
|
|
#define fseek_v fseek
|
|
|
|
#define ftell_v ftell
|
|
|
|
#endif
|
2021-09-04 21:57:23 +02:00
|
|
|
|
|
|
|
#ifdef fileno
|
|
|
|
#undef fileno
|
|
|
|
#endif
|
|
|
|
#define fileno _fileno
|
|
|
|
#define fdopen _fdopen
|
|
|
|
#define dup _dup
|
|
|
|
|
2021-09-15 23:09:19 +02:00
|
|
|
//#ifndef off64_t
|
|
|
|
// #define off_t/off64_t __int64
|
|
|
|
//#endif
|
2021-09-04 21:57:23 +02:00
|
|
|
|
2021-09-12 21:29:07 +02:00
|
|
|
#elif defined(XBMC) || defined(__EMSCRIPTEN__) || defined (__ANDROID__)
|
|
|
|
#define fopen_v fopen
|
2021-09-04 21:57:23 +02:00
|
|
|
#define fseek_v fseek
|
|
|
|
#define ftell_v ftell
|
2021-09-12 21:29:07 +02:00
|
|
|
|
2021-09-04 21:57:23 +02:00
|
|
|
#else
|
2021-09-12 21:29:07 +02:00
|
|
|
#define fopen_v fopen
|
2021-09-04 21:57:23 +02:00
|
|
|
#define fseek_v fseeko64 //fseeko
|
2021-09-12 20:05:56 +02:00
|
|
|
#define ftell_v ftello64 //ftello
|
2021-09-04 21:57:23 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-08-13 19:58:01 +02:00
|
|
|
/* a STREAMFILE that operates via standard IO using a buffer */
|
2008-08-02 05:15:24 +02:00
|
|
|
typedef struct {
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE vt; /* callbacks */
|
2018-08-24 18:48:42 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
FILE* infile; /* actual FILE */
|
2018-08-24 18:48:42 +02:00
|
|
|
char name[PATH_LIMIT]; /* FILE filename */
|
2021-09-16 00:18:52 +02:00
|
|
|
int name_len; /* cache */
|
2021-09-05 17:53:47 +02:00
|
|
|
offv_t offset; /* last read offset (info) */
|
|
|
|
offv_t buf_offset; /* current buffer data start */
|
|
|
|
uint8_t* buf; /* data buffer */
|
|
|
|
size_t buf_size; /* max buffer size */
|
2021-09-12 20:05:56 +02:00
|
|
|
size_t valid_size; /* current buffer size */
|
2021-09-05 17:53:47 +02:00
|
|
|
size_t file_size; /* buffered file size */
|
2019-09-29 20:09:28 +02:00
|
|
|
} STDIO_STREAMFILE;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* open_stdio_streamfile_buffer(const char* const filename, size_t buf_size);
|
|
|
|
static STREAMFILE* open_stdio_streamfile_buffer_by_file(FILE *infile, const char* const filename, size_t buf_size);
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t stdio_read(STDIO_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
|
|
|
|
size_t read_total = 0;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
if (!sf->infile || !dst || length <= 0 || offset < 0)
|
2018-04-19 22:19:30 +02:00
|
|
|
return 0;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2021-09-15 23:09:19 +02:00
|
|
|
//;VGM_LOG("stdio: read %lx + %x (buf %lx + %x)\n", offset, length, sf->buf_offset, sf->valid_size);
|
2019-11-03 17:55:47 +01:00
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
/* is the part of the requested length in the buffer? */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (offset >= sf->buf_offset && offset < sf->buf_offset + sf->valid_size) {
|
|
|
|
size_t buf_limit;
|
|
|
|
int buf_into = (int)(offset - sf->buf_offset);
|
2017-04-29 16:03:11 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
buf_limit = sf->valid_size - buf_into;
|
|
|
|
if (buf_limit > length)
|
|
|
|
buf_limit = length;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2021-09-15 23:09:19 +02:00
|
|
|
//;VGM_LOG("stdio: copy buf %lx + %x (+ %x) (buf %lx + %x)\n", offset, length_to_read, (length - length_to_read), sf->buf_offset, sf->valid_size);
|
2019-11-03 17:55:47 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
memcpy(dst, sf->buf + buf_into, buf_limit);
|
|
|
|
read_total += buf_limit;
|
|
|
|
length -= buf_limit;
|
|
|
|
offset += buf_limit;
|
|
|
|
dst += buf_limit;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 20:09:28 +02:00
|
|
|
#ifdef VGM_DEBUG_OUTPUT
|
2021-09-05 17:53:47 +02:00
|
|
|
if (offset < sf->buf_offset && length > 0) {
|
2021-09-15 23:09:19 +02:00
|
|
|
VGM_LOG("stdio: rebuffer, requested %x vs %x (sf %x)\n", (uint32_t)offset, (uint32_t)sf->buf_offset, (uint32_t)sf);
|
2021-09-05 17:53:47 +02:00
|
|
|
//sf->rebuffer++;
|
2019-11-03 17:55:47 +01:00
|
|
|
//if (rebuffer > N) ...
|
2019-09-29 20:09:28 +02:00
|
|
|
}
|
|
|
|
#endif
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
/* read the rest of the requested length */
|
|
|
|
while (length > 0) {
|
2018-08-24 18:48:42 +02:00
|
|
|
size_t length_to_read;
|
|
|
|
|
|
|
|
/* ignore requests at EOF */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (offset >= sf->file_size) {
|
|
|
|
//offset = sf->file_size; /* seems fseek doesn't clamp offset */
|
|
|
|
VGM_ASSERT_ONCE(offset > sf->file_size, "STDIO: reading over file_size 0x%x @ 0x%x + 0x%x\n", sf->file_size, (uint32_t)offset, length);
|
2018-08-24 18:48:42 +02:00
|
|
|
break;
|
2017-01-15 20:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* position to new offset */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (fseek_v(sf->infile, offset, SEEK_SET)) {
|
2018-08-24 18:48:42 +02:00
|
|
|
break; /* this shouldn't happen in our code */
|
2017-01-15 20:58:47 +01:00
|
|
|
}
|
2018-08-24 18:48:42 +02:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
/* Workaround a bug that appears when compiling with MSVC (later versions).
|
|
|
|
* This bug is deterministic and seemingly appears randomly after seeking.
|
|
|
|
* It results in fread returning data from the wrong area of the file.
|
2020-04-16 00:27:28 +02:00
|
|
|
* HPS is one format that is almost always affected by this.
|
2021-09-05 17:53:47 +02:00
|
|
|
* May be related/same as stdio_open's fixed bug when using dup(), try disabling */
|
|
|
|
fseek_v(sf->infile, ftell_v(sf->infile), SEEK_SET);
|
2018-08-24 18:48:42 +02:00
|
|
|
#endif
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
/* fill the buffer (offset now is beyond buf_offset) */
|
|
|
|
sf->buf_offset = offset;
|
|
|
|
sf->valid_size = fread(sf->buf, sizeof(uint8_t), sf->buf_size, sf->infile);
|
2021-09-15 23:09:19 +02:00
|
|
|
//;VGM_LOG("stdio: read buf %lx + %x\n", sf->buf_offset, sf->valid_size);
|
2008-08-02 05:15:24 +02:00
|
|
|
|
|
|
|
/* decide how much must be read this time */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (length > sf->buf_size)
|
|
|
|
length_to_read = sf->buf_size;
|
2017-04-29 16:03:11 +02:00
|
|
|
else
|
|
|
|
length_to_read = length;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2018-08-24 18:48:42 +02:00
|
|
|
/* give up on partial reads (EOF) */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (sf->valid_size < length_to_read) {
|
|
|
|
memcpy(dst, sf->buf, sf->valid_size);
|
|
|
|
offset += sf->valid_size;
|
|
|
|
read_total += sf->valid_size;
|
2018-08-24 18:48:42 +02:00
|
|
|
break;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* use the new buffer */
|
2021-09-05 17:53:47 +02:00
|
|
|
memcpy(dst, sf->buf, length_to_read);
|
2018-08-24 18:48:42 +02:00
|
|
|
offset += length_to_read;
|
2021-09-05 17:53:47 +02:00
|
|
|
read_total += length_to_read;
|
2017-04-29 16:03:11 +02:00
|
|
|
length -= length_to_read;
|
2019-10-19 11:07:28 +02:00
|
|
|
dst += length_to_read;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
sf->offset = offset; /* last fread offset */
|
|
|
|
return read_total;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t stdio_get_size(STDIO_STREAMFILE* sf) {
|
|
|
|
return sf->file_size;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static offv_t stdio_get_offset(STDIO_STREAMFILE* sf) {
|
|
|
|
return sf->offset;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static void stdio_get_name(STDIO_STREAMFILE* sf, char* name, size_t name_size) {
|
2021-09-16 00:18:52 +02:00
|
|
|
int copy_size = sf->name_len + 1;
|
|
|
|
if (copy_size > name_size)
|
|
|
|
copy_size = name_size;
|
|
|
|
|
|
|
|
memcpy(name, sf->name, copy_size);
|
|
|
|
name[copy_size - 1] = '\0';
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* stdio_open(STDIO_STREAMFILE* sf, const char* const filename, size_t buf_size) {
|
2008-08-02 05:15:24 +02:00
|
|
|
if (!filename)
|
|
|
|
return NULL;
|
2019-09-29 18:25:24 +02:00
|
|
|
|
2020-04-16 00:27:28 +02:00
|
|
|
#if !defined (__ANDROID__) && !defined (_MSC_VER)
|
|
|
|
/* when enabling this for MSVC it'll seemingly work, but there are issues possibly related to underlying
|
|
|
|
* IO buffers when using dup(), noticeable by re-opening the same streamfile with small buffer sizes
|
|
|
|
* (reads garbage). fseek bug in line 81 may be related/same thing and may be removed.
|
|
|
|
* this reportedly this causes issues in Android too */
|
|
|
|
|
2019-11-03 17:55:47 +01:00
|
|
|
/* if same name, duplicate the file descriptor we already have open */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (sf->infile && !strcmp(sf->name,filename)) {
|
2019-11-03 17:55:47 +01:00
|
|
|
int new_fd;
|
|
|
|
FILE *new_file = NULL;
|
2019-09-29 18:25:24 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
if (((new_fd = dup(fileno(sf->infile))) >= 0) && (new_file = fdopen(new_fd, "rb"))) {
|
|
|
|
STREAMFILE* new_sf = open_stdio_streamfile_buffer_by_file(new_file, filename, buf_size);
|
2019-11-03 17:55:47 +01:00
|
|
|
if (new_sf)
|
2019-10-19 11:07:28 +02:00
|
|
|
return new_sf;
|
2019-11-03 17:55:47 +01:00
|
|
|
fclose(new_file);
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
2019-11-03 17:55:47 +01:00
|
|
|
if (new_fd >= 0 && !new_file)
|
|
|
|
close(new_fd); /* fdopen may fail when opening too many files */
|
|
|
|
|
|
|
|
/* on failure just close and try the default path (which will probably fail a second time) */
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
#endif
|
2008-08-02 05:15:24 +02:00
|
|
|
// a normal open, open a new file
|
2021-09-05 17:53:47 +02:00
|
|
|
return open_stdio_streamfile_buffer(filename, buf_size);
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-12 20:05:56 +02:00
|
|
|
static void stdio_close(STDIO_STREAMFILE* sf) {
|
|
|
|
if (sf->infile)
|
|
|
|
fclose(sf->infile);
|
|
|
|
free(sf->buf);
|
|
|
|
free(sf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* open_stdio_streamfile_buffer_by_file(FILE* infile, const char* const filename, size_t buf_size) {
|
|
|
|
uint8_t* buf = NULL;
|
|
|
|
STDIO_STREAMFILE* this_sf = NULL;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
buf = calloc(buf_size, sizeof(uint8_t));
|
|
|
|
if (!buf) goto fail;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf = calloc(1, sizeof(STDIO_STREAMFILE));
|
|
|
|
if (!this_sf) goto fail;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->vt.read = (void*)stdio_read;
|
|
|
|
this_sf->vt.get_size = (void*)stdio_get_size;
|
|
|
|
this_sf->vt.get_offset = (void*)stdio_get_offset;
|
|
|
|
this_sf->vt.get_name = (void*)stdio_get_name;
|
|
|
|
this_sf->vt.open = (void*)stdio_open;
|
|
|
|
this_sf->vt.close = (void*)stdio_close;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->infile = infile;
|
|
|
|
this_sf->buf_size = buf_size;
|
|
|
|
this_sf->buf = buf;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2021-09-16 00:18:52 +02:00
|
|
|
this_sf->name_len = strlen(filename);
|
|
|
|
if (this_sf->name_len >= sizeof(this_sf->name))
|
|
|
|
goto fail;
|
|
|
|
memcpy(this_sf->name, filename, this_sf->name_len);
|
|
|
|
this_sf->name[this_sf->name_len] = '\0';
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
/* cache file_size */
|
2019-09-15 15:47:41 +02:00
|
|
|
if (infile) {
|
2021-09-05 17:53:47 +02:00
|
|
|
fseek_v(this_sf->infile, 0x00, SEEK_END);
|
|
|
|
this_sf->file_size = ftell_v(this_sf->infile);
|
|
|
|
fseek_v(this_sf->infile, 0x00, SEEK_SET);
|
2019-09-15 15:47:41 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->file_size = 0; /* allow virtual, non-existing files */
|
2019-09-15 15:47:41 +02:00
|
|
|
}
|
2017-01-15 20:58:47 +01:00
|
|
|
|
2018-09-23 03:09:46 +02:00
|
|
|
/* Typically fseek(o)/ftell(o) may only handle up to ~2.14GB, signed 32b = 0x7FFFFFFF
|
2021-09-04 21:57:23 +02:00
|
|
|
* (happens in banks like FSB, though rarely). Should work if configured properly, log otherwise. */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (this_sf->file_size == 0xFFFFFFFF) { /* -1 on error */
|
2021-09-04 21:57:23 +02:00
|
|
|
vgm_logi("STREAMFILE: file size too big (report)\n");
|
2018-08-02 17:17:58 +02:00
|
|
|
goto fail; /* can be ignored but may result in strange/unexpected behaviors */
|
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
return &this_sf->vt;
|
2018-08-02 17:17:58 +02:00
|
|
|
|
|
|
|
fail:
|
2021-09-05 17:53:47 +02:00
|
|
|
free(buf);
|
|
|
|
free(this_sf);
|
2018-08-02 17:17:58 +02:00
|
|
|
return NULL;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* open_stdio_streamfile_buffer(const char* const filename, size_t bufsize) {
|
|
|
|
FILE* infile = NULL;
|
|
|
|
STREAMFILE* sf = NULL;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2021-09-12 21:29:07 +02:00
|
|
|
infile = fopen_v(filename,"rb");
|
2019-09-15 15:47:41 +02:00
|
|
|
if (!infile) {
|
|
|
|
/* allow non-existing files in some cases */
|
|
|
|
if (!vgmstream_is_virtual_filename(filename))
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
sf = open_stdio_streamfile_buffer_by_file(infile, filename, bufsize);
|
|
|
|
if (!sf) {
|
2019-09-15 15:47:41 +02:00
|
|
|
if (infile) fclose(infile);
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
return sf;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
2008-07-21 01:28:16 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_stdio_streamfile(const char* filename) {
|
2019-10-19 11:07:28 +02:00
|
|
|
return open_stdio_streamfile_buffer(filename, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2017-08-13 19:58:01 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_stdio_streamfile_by_file(FILE* file, const char* filename) {
|
2019-10-19 11:07:28 +02:00
|
|
|
return open_stdio_streamfile_buffer_by_file(file, filename, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2017-08-13 19:58:01 +02:00
|
|
|
}
|
|
|
|
|
2018-01-20 00:54:08 +01:00
|
|
|
/* **************************************************** */
|
|
|
|
|
2018-04-19 22:19:30 +02:00
|
|
|
typedef struct {
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE vt;
|
|
|
|
|
|
|
|
STREAMFILE* inner_sf;
|
|
|
|
offv_t offset; /* last read offset (info) */
|
|
|
|
offv_t buf_offset; /* current buffer data start */
|
|
|
|
uint8_t* buf; /* data buffer */
|
|
|
|
size_t buf_size; /* max buffer size */
|
|
|
|
size_t valid_size; /* current buffer size */
|
|
|
|
size_t file_size; /* buffered file size */
|
2018-04-19 22:19:30 +02:00
|
|
|
} BUFFER_STREAMFILE;
|
|
|
|
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t buffer_read(BUFFER_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
|
|
|
|
size_t read_total = 0;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!dst || length <= 0 || offset < 0)
|
2018-04-19 22:19:30 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* is the part of the requested length in the buffer? */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (offset >= sf->buf_offset && offset < sf->buf_offset + sf->valid_size) {
|
|
|
|
size_t buf_limit;
|
|
|
|
int buf_into = (int)(offset - sf->buf_offset);
|
|
|
|
|
|
|
|
buf_limit = sf->valid_size - buf_into;
|
|
|
|
if (buf_limit > length)
|
|
|
|
buf_limit = length;
|
|
|
|
|
|
|
|
memcpy(dst, sf->buf + buf_into, buf_limit);
|
|
|
|
read_total += buf_limit;
|
|
|
|
length -= buf_limit;
|
|
|
|
offset += buf_limit;
|
|
|
|
dst += buf_limit;
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 20:09:28 +02:00
|
|
|
#ifdef VGM_DEBUG_OUTPUT
|
2021-09-05 17:53:47 +02:00
|
|
|
if (offset < sf->buf_offset) {
|
2021-09-15 23:09:19 +02:00
|
|
|
VGM_LOG("buffer: rebuffer, requested %x vs %x (sf %x)\n", (uint32_t)offset, (uint32_t)sf->buf_offset, (uint32_t)sf);
|
2019-09-29 20:09:28 +02:00
|
|
|
}
|
|
|
|
#endif
|
2018-04-19 22:19:30 +02:00
|
|
|
|
|
|
|
/* read the rest of the requested length */
|
|
|
|
while (length > 0) {
|
2021-09-05 17:53:47 +02:00
|
|
|
size_t buf_limit;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2018-08-19 22:31:12 +02:00
|
|
|
/* ignore requests at EOF */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (offset >= sf->file_size) {
|
|
|
|
//offset = sf->file_size; /* seems fseek doesn't clamp offset */
|
2021-09-15 23:09:19 +02:00
|
|
|
VGM_ASSERT_ONCE(offset > sf->file_size, "buffer: reading over file_size 0x%x @ 0x%x + 0x%x\n", sf->file_size, (uint32_t)offset, length);
|
2018-08-19 22:31:12 +02:00
|
|
|
break;
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
/* fill the buffer (offset now is beyond buf_offset) */
|
|
|
|
sf->buf_offset = offset;
|
|
|
|
sf->valid_size = sf->inner_sf->read(sf->inner_sf, sf->buf, sf->buf_offset, sf->buf_size);
|
2018-04-19 22:19:30 +02:00
|
|
|
|
|
|
|
/* decide how much must be read this time */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (length > sf->buf_size)
|
|
|
|
buf_limit = sf->buf_size;
|
2018-04-19 22:19:30 +02:00
|
|
|
else
|
2021-09-05 17:53:47 +02:00
|
|
|
buf_limit = length;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2018-08-19 22:31:12 +02:00
|
|
|
/* give up on partial reads (EOF) */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (sf->valid_size < buf_limit) {
|
|
|
|
memcpy(dst, sf->buf, sf->valid_size);
|
|
|
|
offset += sf->valid_size;
|
|
|
|
read_total += sf->valid_size;
|
2018-08-19 22:31:12 +02:00
|
|
|
break;
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* use the new buffer */
|
2021-09-05 17:53:47 +02:00
|
|
|
memcpy(dst, sf->buf, buf_limit);
|
|
|
|
offset += buf_limit;
|
|
|
|
read_total += buf_limit;
|
|
|
|
length -= buf_limit;
|
|
|
|
dst += buf_limit;
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
sf->offset = offset; /* last fread offset */
|
|
|
|
return read_total;
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t buffer_get_size(BUFFER_STREAMFILE* sf) {
|
|
|
|
return sf->file_size; /* cache */
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static offv_t buffer_get_offset(BUFFER_STREAMFILE* sf) {
|
|
|
|
return sf->offset; /* cache */
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static void buffer_get_name(BUFFER_STREAMFILE* sf, char* name, size_t name_size) {
|
|
|
|
sf->inner_sf->get_name(sf->inner_sf, name, name_size); /* default */
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* buffer_open(BUFFER_STREAMFILE* sf, const char* const filename, size_t buf_size) {
|
|
|
|
STREAMFILE* new_inner_sf = sf->inner_sf->open(sf->inner_sf,filename,buf_size);
|
|
|
|
return open_buffer_streamfile(new_inner_sf, buf_size); /* original buffer size is preferable? */
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static void buffer_close(BUFFER_STREAMFILE* sf) {
|
|
|
|
sf->inner_sf->close(sf->inner_sf);
|
|
|
|
free(sf->buf);
|
|
|
|
free(sf);
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_buffer_streamfile(STREAMFILE* sf, size_t buf_size) {
|
|
|
|
uint8_t* buf = NULL;
|
|
|
|
BUFFER_STREAMFILE* this_sf = NULL;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
if (!sf) goto fail;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
if (buf_size == 0)
|
|
|
|
buf_size = STREAMFILE_DEFAULT_BUFFER_SIZE;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
buf = calloc(buf_size, sizeof(uint8_t));
|
|
|
|
if (!buf) goto fail;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf = calloc(1, sizeof(BUFFER_STREAMFILE));
|
|
|
|
if (!this_sf) goto fail;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
|
|
|
/* set callbacks and internals */
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->vt.read = (void*)buffer_read;
|
|
|
|
this_sf->vt.get_size = (void*)buffer_get_size;
|
|
|
|
this_sf->vt.get_offset = (void*)buffer_get_offset;
|
|
|
|
this_sf->vt.get_name = (void*)buffer_get_name;
|
|
|
|
this_sf->vt.open = (void*)buffer_open;
|
|
|
|
this_sf->vt.close = (void*)buffer_close;
|
|
|
|
this_sf->vt.stream_index = sf->stream_index;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->inner_sf = sf;
|
|
|
|
this_sf->buf_size = buf_size;
|
|
|
|
this_sf->buf = buf;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->file_size = sf->get_size(sf);
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
return &this_sf->vt;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
|
|
|
fail:
|
2021-09-05 17:53:47 +02:00
|
|
|
if (this_sf) free(this_sf->buf);
|
2018-04-19 22:19:30 +02:00
|
|
|
free(this_sf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_buffer_streamfile_f(STREAMFILE* sf, size_t buffer_size) {
|
|
|
|
STREAMFILE* new_sf = open_buffer_streamfile(sf, buffer_size);
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!new_sf)
|
2021-09-05 17:53:47 +02:00
|
|
|
close_streamfile(sf);
|
2019-10-19 11:07:28 +02:00
|
|
|
return new_sf;
|
|
|
|
}
|
2018-04-19 22:19:30 +02:00
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
2018-01-20 00:54:08 +01:00
|
|
|
//todo stream_index: copy? pass? funtion? external?
|
2018-01-27 13:58:46 +01:00
|
|
|
//todo use realnames on reopen? simplify?
|
|
|
|
//todo use safe string ops, this ain't easy
|
2018-01-20 00:54:08 +01:00
|
|
|
|
|
|
|
typedef struct {
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE vt;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* inner_sf;
|
2018-01-20 00:54:08 +01:00
|
|
|
} WRAP_STREAMFILE;
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t wrap_read(WRAP_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
|
|
|
|
return sf->inner_sf->read(sf->inner_sf, dst, offset, length); /* default */
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t wrap_get_size(WRAP_STREAMFILE* sf) {
|
|
|
|
return sf->inner_sf->get_size(sf->inner_sf); /* default */
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static offv_t wrap_get_offset(WRAP_STREAMFILE* sf) {
|
|
|
|
return sf->inner_sf->get_offset(sf->inner_sf); /* default */
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
static void wrap_get_name(WRAP_STREAMFILE* sf, char* name, size_t name_size) {
|
|
|
|
sf->inner_sf->get_name(sf->inner_sf, name, name_size); /* default */
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
|
|
|
|
static STREAMFILE* wrap_open(WRAP_STREAMFILE* sf, const char* const filename, size_t buf_size) {
|
|
|
|
return sf->inner_sf->open(sf->inner_sf, filename, buf_size); /* default (don't call open_wrap_streamfile) */
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static void wrap_close(WRAP_STREAMFILE* sf) {
|
|
|
|
//sf->inner_sf->close(sf->inner_sf); /* don't close */
|
|
|
|
free(sf);
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_wrap_streamfile(STREAMFILE* sf) {
|
|
|
|
WRAP_STREAMFILE* this_sf = NULL;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
if (!sf) return NULL;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf = calloc(1, sizeof(WRAP_STREAMFILE));
|
2018-01-20 00:54:08 +01:00
|
|
|
if (!this_sf) return NULL;
|
|
|
|
|
|
|
|
/* set callbacks and internals */
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->vt.read = (void*)wrap_read;
|
|
|
|
this_sf->vt.get_size = (void*)wrap_get_size;
|
|
|
|
this_sf->vt.get_offset = (void*)wrap_get_offset;
|
|
|
|
this_sf->vt.get_name = (void*)wrap_get_name;
|
|
|
|
this_sf->vt.open = (void*)wrap_open;
|
|
|
|
this_sf->vt.close = (void*)wrap_close;
|
|
|
|
this_sf->vt.stream_index = sf->stream_index;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->inner_sf = sf;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
return &this_sf->vt;
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_wrap_streamfile_f(STREAMFILE* sf) {
|
|
|
|
STREAMFILE* new_sf = open_wrap_streamfile(sf);
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!new_sf)
|
2021-09-05 17:53:47 +02:00
|
|
|
close_streamfile(sf);
|
2019-10-19 11:07:28 +02:00
|
|
|
return new_sf;
|
|
|
|
}
|
2018-01-20 00:54:08 +01:00
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
|
|
|
typedef struct {
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE vt;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2021-07-17 19:00:40 +02:00
|
|
|
STREAMFILE* inner_sf;
|
2021-09-04 21:57:23 +02:00
|
|
|
offv_t start;
|
2018-01-20 00:54:08 +01:00
|
|
|
size_t size;
|
|
|
|
} CLAMP_STREAMFILE;
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t clamp_read(CLAMP_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
|
|
|
|
offv_t inner_offset = sf->start + offset;
|
2021-07-17 19:00:40 +02:00
|
|
|
size_t clamp_length = length;
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
if (offset + length > sf->size) {
|
|
|
|
if (offset >= sf->size)
|
2021-07-17 19:00:40 +02:00
|
|
|
clamp_length = 0;
|
|
|
|
else
|
2021-09-05 17:53:47 +02:00
|
|
|
clamp_length = sf->size - offset;
|
2021-07-17 19:00:40 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
return sf->inner_sf->read(sf->inner_sf, dst, inner_offset, clamp_length);
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t clamp_get_size(CLAMP_STREAMFILE* sf) {
|
|
|
|
return sf->size;
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static offv_t clamp_get_offset(CLAMP_STREAMFILE* sf) {
|
|
|
|
return sf->inner_sf->get_offset(sf->inner_sf) - sf->start;
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
static void clamp_get_name(CLAMP_STREAMFILE* sf, char* name, size_t name_size) {
|
|
|
|
sf->inner_sf->get_name(sf->inner_sf, name, name_size); /* default */
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* clamp_open(CLAMP_STREAMFILE* sf, const char* const filename, size_t buf_size) {
|
2018-01-27 11:07:45 +01:00
|
|
|
char original_filename[PATH_LIMIT];
|
2021-07-17 19:00:40 +02:00
|
|
|
STREAMFILE* new_inner_sf = NULL;
|
2018-01-27 11:07:45 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
new_inner_sf = sf->inner_sf->open(sf->inner_sf,filename,buf_size);
|
|
|
|
sf->inner_sf->get_name(sf->inner_sf, original_filename, PATH_LIMIT);
|
2018-01-27 11:07:45 +01:00
|
|
|
|
2018-01-27 13:58:46 +01:00
|
|
|
/* detect re-opening the file */
|
2018-01-27 11:07:45 +01:00
|
|
|
if (strcmp(filename, original_filename) == 0) {
|
2021-09-05 17:53:47 +02:00
|
|
|
return open_clamp_streamfile(new_inner_sf, sf->start, sf->size); /* clamp again */
|
2018-01-27 11:07:45 +01:00
|
|
|
} else {
|
2019-10-19 11:07:28 +02:00
|
|
|
return new_inner_sf;
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static void clamp_close(CLAMP_STREAMFILE* sf) {
|
|
|
|
sf->inner_sf->close(sf->inner_sf);
|
|
|
|
free(sf);
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_clamp_streamfile(STREAMFILE* sf, offv_t start, size_t size) {
|
2021-07-17 19:00:40 +02:00
|
|
|
CLAMP_STREAMFILE* this_sf = NULL;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
if (!sf || size == 0) return NULL;
|
|
|
|
if (start + size > get_streamfile_size(sf)) return NULL;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf = calloc(1, sizeof(CLAMP_STREAMFILE));
|
2018-01-20 00:54:08 +01:00
|
|
|
if (!this_sf) return NULL;
|
|
|
|
|
|
|
|
/* set callbacks and internals */
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->vt.read = (void*)clamp_read;
|
|
|
|
this_sf->vt.get_size = (void*)clamp_get_size;
|
|
|
|
this_sf->vt.get_offset = (void*)clamp_get_offset;
|
|
|
|
this_sf->vt.get_name = (void*)clamp_get_name;
|
|
|
|
this_sf->vt.open = (void*)clamp_open;
|
|
|
|
this_sf->vt.close = (void*)clamp_close;
|
|
|
|
this_sf->vt.stream_index = sf->stream_index;
|
|
|
|
|
|
|
|
this_sf->inner_sf = sf;
|
2018-01-20 00:54:08 +01:00
|
|
|
this_sf->start = start;
|
|
|
|
this_sf->size = size;
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
return &this_sf->vt;
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_clamp_streamfile_f(STREAMFILE* sf, offv_t start, size_t size) {
|
|
|
|
STREAMFILE* new_sf = open_clamp_streamfile(sf, start, size);
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!new_sf)
|
2021-09-05 17:53:47 +02:00
|
|
|
close_streamfile(sf);
|
2019-10-19 11:07:28 +02:00
|
|
|
return new_sf;
|
|
|
|
}
|
2018-01-20 00:54:08 +01:00
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
|
|
|
typedef struct {
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE vt;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* inner_sf;
|
2018-03-30 18:10:23 +02:00
|
|
|
void* data; /* state for custom reads, malloc'ed + copied on open (to re-open streamfiles cleanly) */
|
2018-01-20 00:54:08 +01:00
|
|
|
size_t data_size;
|
2021-09-15 23:09:19 +02:00
|
|
|
size_t (*read_callback)(STREAMFILE*, uint8_t*, off_t, size_t, void*); /* custom read to modify data before copying into buffer */
|
2021-09-05 17:53:47 +02:00
|
|
|
size_t (*size_callback)(STREAMFILE*, void*); /* size when custom reads make data smaller/bigger than underlying streamfile */
|
2020-03-07 10:15:02 +01:00
|
|
|
int (*init_callback)(STREAMFILE*, void*); /* init the data struct members somehow, return >= 0 if ok */
|
|
|
|
void (*close_callback)(STREAMFILE*, void*); /* close the data struct members somehow */
|
2021-09-15 23:09:19 +02:00
|
|
|
/* read doesn't use offv_t since callbacks would need to be modified */
|
2018-01-20 00:54:08 +01:00
|
|
|
} IO_STREAMFILE;
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t io_read(IO_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
|
2021-09-15 23:09:19 +02:00
|
|
|
return sf->read_callback(sf->inner_sf, dst, (off_t)offset, length, sf->data);
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t io_get_size(IO_STREAMFILE* sf) {
|
|
|
|
if (sf->size_callback)
|
|
|
|
return sf->size_callback(sf->inner_sf, sf->data);
|
2018-03-30 18:10:23 +02:00
|
|
|
else
|
2021-09-05 17:53:47 +02:00
|
|
|
return sf->inner_sf->get_size(sf->inner_sf); /* default */
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static offv_t io_get_offset(IO_STREAMFILE* sf) {
|
|
|
|
return sf->inner_sf->get_offset(sf->inner_sf); /* default */
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
static void io_get_name(IO_STREAMFILE* sf, char* name, size_t name_size) {
|
|
|
|
sf->inner_sf->get_name(sf->inner_sf, name, name_size); /* default */
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* io_open(IO_STREAMFILE* sf, const char* const filename, size_t buf_size) {
|
|
|
|
STREAMFILE* new_inner_sf = sf->inner_sf->open(sf->inner_sf,filename,buf_size);
|
|
|
|
return open_io_streamfile_ex(new_inner_sf, sf->data, sf->data_size, sf->read_callback, sf->size_callback, sf->init_callback, sf->close_callback);
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static void io_close(IO_STREAMFILE* sf) {
|
|
|
|
if (sf->close_callback)
|
|
|
|
sf->close_callback(sf->inner_sf, sf->data);
|
|
|
|
sf->inner_sf->close(sf->inner_sf);
|
|
|
|
free(sf->data);
|
|
|
|
free(sf);
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_io_streamfile_ex(STREAMFILE* sf, void* data, size_t data_size, void* read_callback, void* size_callback, void* init_callback, void* close_callback) {
|
|
|
|
IO_STREAMFILE* this_sf = NULL;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
if (!sf) goto fail;
|
2020-03-07 10:15:02 +01:00
|
|
|
if ((data && !data_size) || (!data && data_size)) goto fail;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf = calloc(1, sizeof(IO_STREAMFILE));
|
2020-03-07 10:15:02 +01:00
|
|
|
if (!this_sf) goto fail;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
|
|
|
/* set callbacks and internals */
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->vt.read = (void*)io_read;
|
|
|
|
this_sf->vt.get_size = (void*)io_get_size;
|
|
|
|
this_sf->vt.get_offset = (void*)io_get_offset;
|
|
|
|
this_sf->vt.get_name = (void*)io_get_name;
|
|
|
|
this_sf->vt.open = (void*)io_open;
|
|
|
|
this_sf->vt.close = (void*)io_close;
|
|
|
|
this_sf->vt.stream_index = sf->stream_index;
|
|
|
|
|
|
|
|
this_sf->inner_sf = sf;
|
2018-01-20 00:54:08 +01:00
|
|
|
if (data) {
|
|
|
|
this_sf->data = malloc(data_size);
|
2020-03-07 10:15:02 +01:00
|
|
|
if (!this_sf->data) goto fail;
|
2018-01-20 00:54:08 +01:00
|
|
|
memcpy(this_sf->data, data, data_size);
|
|
|
|
}
|
|
|
|
this_sf->data_size = data_size;
|
|
|
|
this_sf->read_callback = read_callback;
|
2018-03-30 18:10:23 +02:00
|
|
|
this_sf->size_callback = size_callback;
|
2020-03-07 10:15:02 +01:00
|
|
|
this_sf->init_callback = init_callback;
|
|
|
|
this_sf->close_callback = close_callback;
|
|
|
|
|
|
|
|
if (this_sf->init_callback) {
|
|
|
|
int ok = this_sf->init_callback(this_sf->inner_sf, this_sf->data);
|
|
|
|
if (ok < 0) goto fail;
|
|
|
|
}
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
return &this_sf->vt;
|
2021-09-12 20:05:56 +02:00
|
|
|
|
2020-03-07 10:15:02 +01:00
|
|
|
fail:
|
|
|
|
if (this_sf) free(this_sf->data);
|
|
|
|
free(this_sf);
|
|
|
|
return NULL;
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2020-03-07 10:15:02 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_io_streamfile_ex_f(STREAMFILE* sf, void* data, size_t data_size, void* read_callback, void* size_callback, void* init_callback, void* close_callback) {
|
|
|
|
STREAMFILE* new_sf = open_io_streamfile_ex(sf, data, data_size, read_callback, size_callback, init_callback, close_callback);
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!new_sf)
|
2021-09-05 17:53:47 +02:00
|
|
|
close_streamfile(sf);
|
2019-10-19 11:07:28 +02:00
|
|
|
return new_sf;
|
|
|
|
}
|
2017-08-13 19:58:01 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_io_streamfile(STREAMFILE* sf, void* data, size_t data_size, void* read_callback, void* size_callback) {
|
|
|
|
return open_io_streamfile_ex(sf, data, data_size, read_callback, size_callback, NULL, NULL);
|
2020-03-07 10:15:02 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_io_streamfile_f(STREAMFILE* sf, void* data, size_t data_size, void* read_callback, void* size_callback) {
|
|
|
|
return open_io_streamfile_ex_f(sf, data, data_size, read_callback, size_callback, NULL, NULL);
|
2020-03-07 10:15:02 +01:00
|
|
|
}
|
|
|
|
|
2017-08-13 19:58:01 +02:00
|
|
|
/* **************************************************** */
|
|
|
|
|
2018-01-27 11:07:45 +01:00
|
|
|
typedef struct {
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE vt;
|
2018-01-27 11:07:45 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* inner_sf;
|
2018-01-27 11:07:45 +01:00
|
|
|
char fakename[PATH_LIMIT];
|
2021-09-16 00:18:52 +02:00
|
|
|
int fakename_len;
|
2018-01-27 11:07:45 +01:00
|
|
|
} FAKENAME_STREAMFILE;
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t fakename_read(FAKENAME_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
|
|
|
|
return sf->inner_sf->read(sf->inner_sf, dst, offset, length); /* default */
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t fakename_get_size(FAKENAME_STREAMFILE* sf) {
|
|
|
|
return sf->inner_sf->get_size(sf->inner_sf); /* default */
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static offv_t fakename_get_offset(FAKENAME_STREAMFILE* sf) {
|
|
|
|
return sf->inner_sf->get_offset(sf->inner_sf); /* default */
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static void fakename_get_name(FAKENAME_STREAMFILE* sf, char* name, size_t name_size) {
|
2021-09-16 00:18:52 +02:00
|
|
|
int copy_size = sf->fakename_len + 1;
|
|
|
|
if (copy_size > name_size)
|
|
|
|
copy_size = name_size;
|
|
|
|
memcpy(name, sf->fakename, copy_size);
|
|
|
|
name[copy_size - 1] = '\0';
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* fakename_open(FAKENAME_STREAMFILE* sf, const char* const filename, size_t buf_size) {
|
2018-01-27 11:07:45 +01:00
|
|
|
/* detect re-opening the file */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (strcmp(filename, sf->fakename) == 0) {
|
|
|
|
STREAMFILE* new_inner_sf;
|
2018-01-27 11:07:45 +01:00
|
|
|
char original_filename[PATH_LIMIT];
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
sf->inner_sf->get_name(sf->inner_sf, original_filename, PATH_LIMIT);
|
|
|
|
new_inner_sf = sf->inner_sf->open(sf->inner_sf, original_filename, buf_size);
|
|
|
|
return open_fakename_streamfile(new_inner_sf, sf->fakename, NULL);
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-09-05 17:53:47 +02:00
|
|
|
return sf->inner_sf->open(sf->inner_sf, filename, buf_size);
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static void fakename_close(FAKENAME_STREAMFILE* sf) {
|
|
|
|
sf->inner_sf->close(sf->inner_sf);
|
|
|
|
free(sf);
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_fakename_streamfile(STREAMFILE* sf, const char* fakename, const char* fakeext) {
|
|
|
|
FAKENAME_STREAMFILE* this_sf = NULL;
|
2018-01-27 11:07:45 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
if (!sf || (!fakename && !fakeext)) return NULL;
|
2018-01-27 11:07:45 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf = calloc(1, sizeof(FAKENAME_STREAMFILE));
|
2018-01-27 11:07:45 +01:00
|
|
|
if (!this_sf) return NULL;
|
|
|
|
|
|
|
|
/* set callbacks and internals */
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->vt.read = (void*)fakename_read;
|
|
|
|
this_sf->vt.get_size = (void*)fakename_get_size;
|
|
|
|
this_sf->vt.get_offset = (void*)fakename_get_offset;
|
|
|
|
this_sf->vt.get_name = (void*)fakename_get_name;
|
|
|
|
this_sf->vt.open = (void*)fakename_open;
|
|
|
|
this_sf->vt.close = (void*)fakename_close;
|
|
|
|
this_sf->vt.stream_index = sf->stream_index;
|
2018-01-27 11:07:45 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->inner_sf = sf;
|
2018-01-27 11:07:45 +01:00
|
|
|
|
|
|
|
/* copy passed name or retain current, and swap extension if expected */
|
|
|
|
if (fakename) {
|
2021-09-16 00:18:52 +02:00
|
|
|
strcpy(this_sf->fakename, fakename);
|
2018-01-27 11:07:45 +01:00
|
|
|
} else {
|
2021-09-05 17:53:47 +02:00
|
|
|
sf->get_name(sf, this_sf->fakename, PATH_LIMIT);
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
|
2018-01-27 11:07:45 +01:00
|
|
|
if (fakeext) {
|
2021-09-05 17:53:47 +02:00
|
|
|
char* ext = strrchr(this_sf->fakename, '.');
|
2020-05-24 12:45:14 +02:00
|
|
|
if (ext != NULL) {
|
2018-01-27 11:07:45 +01:00
|
|
|
ext[1] = '\0'; /* truncate past dot */
|
2020-05-24 12:45:14 +02:00
|
|
|
} else {
|
|
|
|
strcat(this_sf->fakename, "."); /* no extension = add dot */
|
|
|
|
}
|
2018-01-27 11:07:45 +01:00
|
|
|
strcat(this_sf->fakename, fakeext);
|
|
|
|
}
|
|
|
|
|
2021-09-16 00:18:52 +02:00
|
|
|
this_sf->fakename_len = strlen(this_sf->fakename);
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
return &this_sf->vt;
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_fakename_streamfile_f(STREAMFILE* sf, const char* fakename, const char* fakeext) {
|
|
|
|
STREAMFILE* new_sf = open_fakename_streamfile(sf, fakename, fakeext);
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!new_sf)
|
2021-09-05 17:53:47 +02:00
|
|
|
close_streamfile(sf);
|
2019-10-19 11:07:28 +02:00
|
|
|
return new_sf;
|
|
|
|
}
|
2018-01-27 11:07:45 +01:00
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
2018-01-27 13:58:46 +01:00
|
|
|
typedef struct {
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE vt;
|
2018-01-27 13:58:46 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE** inner_sfs;
|
2018-01-27 13:58:46 +01:00
|
|
|
size_t inner_sfs_size;
|
|
|
|
size_t *sizes;
|
2021-09-04 21:57:23 +02:00
|
|
|
offv_t size;
|
|
|
|
offv_t offset;
|
2018-01-27 13:58:46 +01:00
|
|
|
} MULTIFILE_STREAMFILE;
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t multifile_read(MULTIFILE_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
|
2018-01-27 13:58:46 +01:00
|
|
|
int i, segment = 0;
|
2021-09-04 21:57:23 +02:00
|
|
|
offv_t segment_offset = 0;
|
2018-01-27 13:58:46 +01:00
|
|
|
size_t done = 0;
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
if (offset > sf->size) {
|
|
|
|
sf->offset = sf->size;
|
2018-01-27 13:58:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* map external offset to multifile offset */
|
2021-09-05 17:53:47 +02:00
|
|
|
for (i = 0; i < sf->inner_sfs_size; i++) {
|
|
|
|
size_t segment_size = sf->sizes[i];
|
2018-01-27 13:58:46 +01:00
|
|
|
/* check if offset falls in this segment */
|
|
|
|
if (offset >= segment_offset && offset < segment_offset + segment_size) {
|
|
|
|
segment = i;
|
|
|
|
segment_offset = offset - segment_offset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
segment_offset += segment_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reads can span multiple segments */
|
|
|
|
while(done < length) {
|
2021-09-05 17:53:47 +02:00
|
|
|
if (segment >= sf->inner_sfs_size) /* over last segment, not fully done */
|
2018-01-27 13:58:46 +01:00
|
|
|
break;
|
|
|
|
/* reads over segment size are ok, will return smaller value and continue next segment */
|
2021-09-05 17:53:47 +02:00
|
|
|
done += sf->inner_sfs[segment]->read(sf->inner_sfs[segment], dst + done, segment_offset, length - done);
|
2018-01-27 13:58:46 +01:00
|
|
|
segment++;
|
|
|
|
segment_offset = 0;
|
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
sf->offset = offset + done;
|
2018-01-27 13:58:46 +01:00
|
|
|
return done;
|
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t multifile_get_size(MULTIFILE_STREAMFILE* sf) {
|
|
|
|
return sf->size;
|
2018-01-27 13:58:46 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static offv_t multifile_get_offset(MULTIFILE_STREAMFILE* sf) {
|
|
|
|
return sf->offset;
|
2018-01-27 13:58:46 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static void multifile_get_name(MULTIFILE_STREAMFILE* sf, char* name, size_t name_size) {
|
|
|
|
sf->inner_sfs[0]->get_name(sf->inner_sfs[0], name, name_size);
|
2018-01-27 13:58:46 +01:00
|
|
|
}
|
2021-09-12 20:05:56 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* multifile_open(MULTIFILE_STREAMFILE* sf, const char* const filename, size_t buf_size) {
|
2018-01-27 13:58:46 +01:00
|
|
|
char original_filename[PATH_LIMIT];
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* new_sf = NULL;
|
|
|
|
STREAMFILE** new_inner_sfs = NULL;
|
2018-01-27 13:58:46 +01:00
|
|
|
int i;
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
sf->inner_sfs[0]->get_name(sf->inner_sfs[0], original_filename, PATH_LIMIT);
|
2018-01-27 13:58:46 +01:00
|
|
|
|
|
|
|
/* detect re-opening the file */
|
|
|
|
if (strcmp(filename, original_filename) == 0) { /* same multifile */
|
2021-09-05 17:53:47 +02:00
|
|
|
new_inner_sfs = calloc(sf->inner_sfs_size, sizeof(STREAMFILE*));
|
2018-01-27 13:58:46 +01:00
|
|
|
if (!new_inner_sfs) goto fail;
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
for (i = 0; i < sf->inner_sfs_size; i++) {
|
|
|
|
sf->inner_sfs[i]->get_name(sf->inner_sfs[i], original_filename, PATH_LIMIT);
|
|
|
|
new_inner_sfs[i] = sf->inner_sfs[i]->open(sf->inner_sfs[i], original_filename, buf_size);
|
2018-01-27 13:58:46 +01:00
|
|
|
if (!new_inner_sfs[i]) goto fail;
|
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
new_sf = open_multifile_streamfile(new_inner_sfs, sf->inner_sfs_size);
|
2018-01-27 13:58:46 +01:00
|
|
|
if (!new_sf) goto fail;
|
|
|
|
|
2019-09-29 18:25:24 +02:00
|
|
|
free(new_inner_sfs);
|
2018-01-27 13:58:46 +01:00
|
|
|
return new_sf;
|
|
|
|
}
|
|
|
|
else {
|
2021-09-05 17:53:47 +02:00
|
|
|
return sf->inner_sfs[0]->open(sf->inner_sfs[0], filename, buf_size); /* regular file */
|
2018-01-27 13:58:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (new_inner_sfs) {
|
2021-09-05 17:53:47 +02:00
|
|
|
for (i = 0; i < sf->inner_sfs_size; i++)
|
2018-01-27 13:58:46 +01:00
|
|
|
close_streamfile(new_inner_sfs[i]);
|
|
|
|
}
|
|
|
|
free(new_inner_sfs);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static void multifile_close(MULTIFILE_STREAMFILE* sf) {
|
2018-01-27 13:58:46 +01:00
|
|
|
int i;
|
2021-09-05 17:53:47 +02:00
|
|
|
for (i = 0; i < sf->inner_sfs_size; i++) {
|
|
|
|
for (i = 0; i < sf->inner_sfs_size; i++) {
|
|
|
|
close_streamfile(sf->inner_sfs[i]);
|
2018-03-25 19:59:58 +02:00
|
|
|
}
|
2018-01-27 13:58:46 +01:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
free(sf->inner_sfs);
|
|
|
|
free(sf->sizes);
|
|
|
|
free(sf);
|
2018-01-27 13:58:46 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_multifile_streamfile(STREAMFILE** sfs, size_t sfs_size) {
|
|
|
|
MULTIFILE_STREAMFILE* this_sf = NULL;
|
2018-01-27 13:58:46 +01:00
|
|
|
int i;
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
if (!sfs || !sfs_size) return NULL;
|
2019-10-19 11:07:28 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
for (i = 0; i < sfs_size; i++) {
|
|
|
|
if (!sfs[i]) return NULL;
|
2018-01-27 13:58:46 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf = calloc(1, sizeof(MULTIFILE_STREAMFILE));
|
2018-01-27 13:58:46 +01:00
|
|
|
if (!this_sf) goto fail;
|
|
|
|
|
|
|
|
/* set callbacks and internals */
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->vt.read = (void*)multifile_read;
|
|
|
|
this_sf->vt.get_size = (void*)multifile_get_size;
|
|
|
|
this_sf->vt.get_offset = (void*)multifile_get_offset;
|
|
|
|
this_sf->vt.get_name = (void*)multifile_get_name;
|
|
|
|
this_sf->vt.open = (void*)multifile_open;
|
|
|
|
this_sf->vt.close = (void*)multifile_close;
|
|
|
|
this_sf->vt.stream_index = sfs[0]->stream_index;
|
|
|
|
|
|
|
|
this_sf->inner_sfs_size = sfs_size;
|
|
|
|
this_sf->inner_sfs = calloc(sfs_size, sizeof(STREAMFILE*));
|
2018-01-27 13:58:46 +01:00
|
|
|
if (!this_sf->inner_sfs) goto fail;
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->sizes = calloc(sfs_size, sizeof(size_t));
|
2018-01-27 13:58:46 +01:00
|
|
|
if (!this_sf->sizes) goto fail;
|
|
|
|
|
|
|
|
for (i = 0; i < this_sf->inner_sfs_size; i++) {
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->inner_sfs[i] = sfs[i];
|
|
|
|
this_sf->sizes[i] = sfs[i]->get_size(sfs[i]);
|
2018-01-27 13:58:46 +01:00
|
|
|
this_sf->size += this_sf->sizes[i];
|
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
return &this_sf->vt;
|
2018-01-27 13:58:46 +01:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (this_sf) {
|
|
|
|
free(this_sf->inner_sfs);
|
|
|
|
free(this_sf->sizes);
|
|
|
|
}
|
|
|
|
free(this_sf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* open_multifile_streamfile_f(STREAMFILE** sfs, size_t sfs_size) {
|
|
|
|
STREAMFILE* new_sf = open_multifile_streamfile(sfs, sfs_size);
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!new_sf) {
|
|
|
|
int i;
|
2021-09-05 17:53:47 +02:00
|
|
|
for (i = 0; i < sfs_size; i++) {
|
|
|
|
close_streamfile(sfs[i]);
|
2019-10-19 11:07:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return new_sf;
|
|
|
|
}
|
2018-01-27 13:58:46 +01:00
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
STREAMFILE* open_streamfile(STREAMFILE* sf, const char* pathname) {
|
|
|
|
return sf->open(sf, pathname, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2018-08-04 20:42:00 +02:00
|
|
|
}
|
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
STREAMFILE* open_streamfile_by_ext(STREAMFILE* sf, const char* ext) {
|
2019-08-02 17:30:23 +02:00
|
|
|
char filename[PATH_LIMIT];
|
|
|
|
|
2021-09-19 23:48:33 +02:00
|
|
|
get_streamfile_name(sf, filename, sizeof(filename));
|
2018-03-29 22:34:21 +02:00
|
|
|
|
2021-09-19 23:48:33 +02:00
|
|
|
swap_extension(filename, sizeof(filename), ext);
|
2018-03-29 22:34:21 +02:00
|
|
|
|
2021-09-19 23:48:33 +02:00
|
|
|
return open_streamfile(sf, filename);
|
2018-03-29 22:34:21 +02:00
|
|
|
}
|
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
STREAMFILE* open_streamfile_by_filename(STREAMFILE* sf, const char* filename) {
|
2019-09-24 00:51:12 +02:00
|
|
|
char fullname[PATH_LIMIT];
|
|
|
|
char partname[PATH_LIMIT];
|
2021-07-09 10:06:28 +02:00
|
|
|
char *path, *name, *otherpath;
|
2018-03-29 22:34:21 +02:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
if (!sf || !filename || !filename[0]) return NULL;
|
2019-10-19 11:07:28 +02:00
|
|
|
|
2021-09-19 23:48:33 +02:00
|
|
|
get_streamfile_name(sf, fullname, sizeof(fullname));
|
2018-03-29 22:34:21 +02:00
|
|
|
|
2019-09-24 00:51:12 +02:00
|
|
|
//todo normalize separators in a better way, safeops, improve copying
|
2021-07-08 22:08:57 +02:00
|
|
|
|
|
|
|
/* check for non-normalized paths first (ex. txth) */
|
|
|
|
path = strrchr(fullname, '/');
|
2021-07-09 10:06:28 +02:00
|
|
|
otherpath = strrchr(fullname, '\\');
|
2021-07-17 19:00:40 +02:00
|
|
|
if (otherpath > path) { //todo cast to ptr?
|
|
|
|
/* foobar makes paths like "(fake protocol)://(windows path with \)".
|
|
|
|
* Hack to work around both separators, though probably foo_streamfile
|
|
|
|
* should just return and handle normalized paths without protocol. */
|
2021-07-09 10:06:28 +02:00
|
|
|
path = otherpath;
|
2021-07-17 19:00:40 +02:00
|
|
|
}
|
2021-07-08 22:08:57 +02:00
|
|
|
|
2018-03-29 22:34:21 +02:00
|
|
|
if (path) {
|
2019-09-24 00:51:12 +02:00
|
|
|
path[1] = '\0'; /* remove name after separator */
|
|
|
|
|
|
|
|
strcpy(partname, filename);
|
2021-07-08 22:08:57 +02:00
|
|
|
fix_dir_separators(partname); /* normalize to DIR_SEPARATOR */
|
2019-09-24 00:51:12 +02:00
|
|
|
|
|
|
|
/* normalize relative paths as don't work ok in some plugins */
|
2021-01-03 16:07:17 +01:00
|
|
|
if (partname[0] == '.' && partname[1] == DIR_SEPARATOR) { /* './name' */
|
2019-09-24 00:51:12 +02:00
|
|
|
name = partname + 2; /* ignore './' */
|
|
|
|
}
|
2021-01-03 16:07:17 +01:00
|
|
|
else if (partname[0] == '.' && partname[1] == '.' && partname[2] == DIR_SEPARATOR) { /* '../name' */
|
2021-09-05 17:53:47 +02:00
|
|
|
char* pathprev;
|
2019-09-24 00:51:12 +02:00
|
|
|
|
|
|
|
path[0] = '\0'; /* remove last separator so next call works */
|
|
|
|
pathprev = strrchr(fullname,DIR_SEPARATOR);
|
|
|
|
if (pathprev) {
|
|
|
|
pathprev[1] = '\0'; /* remove prev dir after separator */
|
|
|
|
name = partname + 3; /* ignore '../' */
|
|
|
|
}
|
|
|
|
else { /* let plugin handle? */
|
|
|
|
path[0] = DIR_SEPARATOR;
|
|
|
|
name = partname;
|
|
|
|
}
|
|
|
|
/* could work with more relative paths but whatevs */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
name = partname;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcat(fullname, name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcpy(fullname, filename);
|
2018-03-29 22:34:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 23:48:33 +02:00
|
|
|
return open_streamfile(sf, fullname);
|
2018-03-29 22:34:21 +02:00
|
|
|
}
|
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
STREAMFILE* reopen_streamfile(STREAMFILE* sf, size_t buffer_size) {
|
2019-01-01 23:21:08 +01:00
|
|
|
char pathname[PATH_LIMIT];
|
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
if (!sf) return NULL;
|
2019-10-19 11:07:28 +02:00
|
|
|
|
2019-01-01 23:21:08 +01:00
|
|
|
if (buffer_size == 0)
|
|
|
|
buffer_size = STREAMFILE_DEFAULT_BUFFER_SIZE;
|
2021-09-19 23:48:33 +02:00
|
|
|
get_streamfile_name(sf, pathname, sizeof(pathname));
|
2021-01-03 16:07:17 +01:00
|
|
|
return sf->open(sf, pathname, buffer_size);
|
2019-01-01 23:21:08 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
/* **************************************************** */
|
2018-03-29 22:34:21 +02:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
size_t read_line(char* buf, int buf_size, off_t offset, STREAMFILE* sf, int* p_line_ok) {
|
2008-07-21 01:28:16 +02:00
|
|
|
int i;
|
2019-10-20 11:55:05 +02:00
|
|
|
off_t file_size = get_streamfile_size(sf);
|
2017-11-23 22:53:43 +01:00
|
|
|
int extra_bytes = 0; /* how many bytes over those put in the buffer were read */
|
2008-07-21 01:28:16 +02:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
if (p_line_ok) *p_line_ok = 0;
|
2008-07-21 01:28:16 +02:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
for (i = 0; i < buf_size-1 && offset+i < file_size; i++) {
|
|
|
|
char in_char = read_8bit(offset+i, sf);
|
2008-07-21 01:28:16 +02:00
|
|
|
/* check for end of line */
|
2019-10-20 11:55:05 +02:00
|
|
|
if (in_char == 0x0d && read_8bit(offset+i+1, sf) == 0x0a) { /* CRLF */
|
2008-07-21 01:28:16 +02:00
|
|
|
extra_bytes = 2;
|
2019-10-20 11:55:05 +02:00
|
|
|
if (p_line_ok) *p_line_ok = 1;
|
2008-07-21 01:28:16 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-11-23 22:53:43 +01:00
|
|
|
else if (in_char == 0x0d || in_char == 0x0a) { /* CR or LF */
|
|
|
|
extra_bytes = 1;
|
2019-10-20 11:55:05 +02:00
|
|
|
if (p_line_ok) *p_line_ok = 1;
|
2017-11-23 22:53:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2008-07-21 01:28:16 +02:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
buf[i] = in_char;
|
2008-07-21 01:28:16 +02:00
|
|
|
}
|
2017-11-23 22:53:43 +01:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
buf[i] = '\0';
|
2008-07-21 01:28:16 +02:00
|
|
|
|
|
|
|
/* did we fill the buffer? */
|
2019-10-20 11:55:05 +02:00
|
|
|
if (i == buf_size) {
|
|
|
|
char in_char = read_8bit(offset+i, sf);
|
2008-07-21 01:28:16 +02:00
|
|
|
/* did the bytes we missed just happen to be the end of the line? */
|
2019-10-20 11:55:05 +02:00
|
|
|
if (in_char == 0x0d && read_8bit(offset+i+1, sf) == 0x0a) { /* CRLF */
|
2008-07-21 01:28:16 +02:00
|
|
|
extra_bytes = 2;
|
2019-10-20 11:55:05 +02:00
|
|
|
if (p_line_ok) *p_line_ok = 1;
|
2017-11-23 22:53:43 +01:00
|
|
|
}
|
|
|
|
else if (in_char == 0x0d || in_char == 0x0a) { /* CR or LF */
|
|
|
|
extra_bytes = 1;
|
2019-10-20 11:55:05 +02:00
|
|
|
if (p_line_ok) *p_line_ok = 1;
|
2008-07-21 01:28:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* did we hit the file end? */
|
2019-10-20 11:55:05 +02:00
|
|
|
if (offset+i == file_size) {
|
2008-07-21 01:28:16 +02:00
|
|
|
/* then we did in fact finish reading the last line */
|
2019-10-20 11:55:05 +02:00
|
|
|
if (p_line_ok) *p_line_ok = 1;
|
2008-07-21 01:28:16 +02:00
|
|
|
}
|
|
|
|
|
2017-11-23 22:53:43 +01:00
|
|
|
return i + extra_bytes;
|
2008-07-21 01:28:16 +02:00
|
|
|
}
|
2016-11-27 19:35:26 +01:00
|
|
|
|
2021-07-08 22:26:21 +02:00
|
|
|
size_t read_bom(STREAMFILE* sf) {
|
|
|
|
if (read_u16le(0x00, sf) == 0xFFFE ||
|
|
|
|
read_u16le(0x00, sf) == 0xFEFF) {
|
|
|
|
return 0x02;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((read_u32be(0x00, sf) & 0xFFFFFF00) == 0xEFBBBF00) {
|
|
|
|
return 0x03;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0x00;
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
size_t read_string(char* buf, size_t buf_size, off_t offset, STREAMFILE* sf) {
|
2018-03-29 20:42:52 +02:00
|
|
|
size_t pos;
|
2017-08-12 11:14:16 +02:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
for (pos = 0; pos < buf_size; pos++) {
|
2020-08-01 15:28:41 +02:00
|
|
|
uint8_t byte = read_u8(offset + pos, sf);
|
|
|
|
char c = (char)byte;
|
2018-03-29 20:42:52 +02:00
|
|
|
if (buf) buf[pos] = c;
|
2017-08-12 11:14:16 +02:00
|
|
|
if (c == '\0')
|
2018-03-29 20:42:52 +02:00
|
|
|
return pos;
|
2019-10-20 11:55:05 +02:00
|
|
|
if (pos+1 == buf_size) { /* null at maxsize and don't validate (expected to be garbage) */
|
2018-03-29 20:42:52 +02:00
|
|
|
if (buf) buf[pos] = '\0';
|
2019-10-20 11:55:05 +02:00
|
|
|
return buf_size;
|
2017-08-12 11:14:16 +02:00
|
|
|
}
|
2020-08-01 15:28:41 +02:00
|
|
|
/* UTF-8 only goes to 0x7F, but allow a bunch of Windows-1252 codes that some games use */
|
|
|
|
if (byte < 0x20 || byte > 0xF0)
|
2017-08-12 11:14:16 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (buf) buf[0] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
2020-04-05 22:07:51 +02:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
size_t read_string_utf16(char* buf, size_t buf_size, off_t offset, STREAMFILE* sf, int big_endian) {
|
2019-11-03 22:56:37 +01:00
|
|
|
size_t pos, offpos;
|
2020-04-05 22:07:51 +02:00
|
|
|
uint16_t (*read_u16)(off_t,STREAMFILE*) = big_endian ? read_u16be : read_u16le;
|
|
|
|
|
2019-11-03 22:56:37 +01:00
|
|
|
|
|
|
|
for (pos = 0, offpos = 0; pos < buf_size; pos++, offpos += 2) {
|
2020-04-05 22:07:51 +02:00
|
|
|
char c = read_u16(offset + offpos, sf) & 0xFF; /* lower byte for now */
|
2019-11-03 22:56:37 +01:00
|
|
|
if (buf) buf[pos] = c;
|
|
|
|
if (c == '\0')
|
|
|
|
return pos;
|
|
|
|
if (pos+1 == buf_size) { /* null at maxsize and don't validate (expected to be garbage) */
|
|
|
|
if (buf) buf[pos] = '\0';
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
if (c < 0x20 || (uint8_t)c > 0xA5)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (buf) buf[0] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:07:51 +02:00
|
|
|
size_t read_string_utf16le(char* buf, size_t buf_size, off_t offset, STREAMFILE* sf) {
|
|
|
|
return read_string_utf16(buf, buf_size, offset, sf, 0);
|
|
|
|
}
|
|
|
|
size_t read_string_utf16be(char* buf, size_t buf_size, off_t offset, STREAMFILE* sf) {
|
|
|
|
return read_string_utf16(buf, buf_size, offset, sf, 1);
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
/* ************************************************************************* */
|
2017-08-12 11:14:16 +02:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
size_t read_key_file(uint8_t* buf, size_t buf_size, STREAMFILE* sf) {
|
2016-11-27 19:35:26 +01:00
|
|
|
char keyname[PATH_LIMIT];
|
2016-11-27 22:42:42 +01:00
|
|
|
char filename[PATH_LIMIT];
|
2016-11-27 19:35:26 +01:00
|
|
|
const char *path, *ext;
|
2021-01-03 16:07:17 +01:00
|
|
|
STREAMFILE* sf_key = NULL;
|
2018-01-20 20:06:15 +01:00
|
|
|
size_t keysize;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
get_streamfile_name(sf, filename, sizeof(filename));
|
2016-11-27 22:42:42 +01:00
|
|
|
|
|
|
|
if (strlen(filename)+4 > sizeof(keyname)) goto fail;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
/* try to open a keyfile using variations */
|
2016-11-27 19:35:26 +01:00
|
|
|
{
|
2016-11-27 22:42:42 +01:00
|
|
|
ext = strrchr(filename,'.');
|
2016-11-27 19:35:26 +01:00
|
|
|
if (ext!=NULL) ext = ext+1;
|
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
path = strrchr(filename, DIR_SEPARATOR);
|
2016-11-27 19:35:26 +01:00
|
|
|
if (path!=NULL) path = path+1;
|
|
|
|
|
|
|
|
/* "(name.ext)key" */
|
2016-11-27 22:42:42 +01:00
|
|
|
strcpy(keyname, filename);
|
2016-11-27 19:35:26 +01:00
|
|
|
strcat(keyname, "key");
|
2021-01-03 16:07:17 +01:00
|
|
|
sf_key = sf->open(sf, keyname, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
|
|
if (sf_key) goto found;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
|
|
|
/* "(name.ext)KEY" */
|
|
|
|
/*
|
|
|
|
strcpy(keyname+strlen(keyname)-3,"KEY");
|
2021-01-03 16:07:17 +01:00
|
|
|
sf_key = sf->open(sf, keyname, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
|
|
if (sf_key) goto found;
|
2016-11-27 19:35:26 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* "(.ext)key" */
|
|
|
|
if (path) {
|
2016-11-27 22:42:42 +01:00
|
|
|
strcpy(keyname, filename);
|
|
|
|
keyname[path-filename] = '\0';
|
2016-11-27 19:35:26 +01:00
|
|
|
strcat(keyname, ".");
|
|
|
|
} else {
|
|
|
|
strcpy(keyname, ".");
|
|
|
|
}
|
|
|
|
if (ext) strcat(keyname, ext);
|
|
|
|
strcat(keyname, "key");
|
2021-01-03 16:07:17 +01:00
|
|
|
sf_key = sf->open(sf, keyname, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
|
|
if (sf_key) goto found;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
|
|
|
/* "(.ext)KEY" */
|
|
|
|
/*
|
|
|
|
strcpy(keyname+strlen(keyname)-3,"KEY");
|
2021-01-03 16:07:17 +01:00
|
|
|
sf_key = sf->open(sf, keyname, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
|
|
if (sf_key) goto found;
|
2016-11-27 19:35:26 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
found:
|
2021-01-03 16:07:17 +01:00
|
|
|
keysize = get_streamfile_size(sf_key);
|
2019-10-20 11:55:05 +02:00
|
|
|
if (keysize > buf_size) goto fail;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
if (read_streamfile(buf, 0, keysize, sf_key) != keysize)
|
2018-01-20 20:06:15 +01:00
|
|
|
goto fail;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
close_streamfile(sf_key);
|
2018-01-20 20:06:15 +01:00
|
|
|
return keysize;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
|
|
|
fail:
|
2021-01-03 16:07:17 +01:00
|
|
|
close_streamfile(sf_key);
|
2016-11-27 19:35:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
STREAMFILE* read_filemap_file(STREAMFILE* sf, int file_num) {
|
2021-06-20 13:42:06 +02:00
|
|
|
return read_filemap_file_pos(sf, file_num, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAMFILE* read_filemap_file_pos(STREAMFILE* sf, int file_num, int* p_pos) {
|
2020-11-08 20:42:57 +01:00
|
|
|
char filename[PATH_LIMIT];
|
|
|
|
off_t txt_offset, file_size;
|
2021-01-03 16:07:17 +01:00
|
|
|
STREAMFILE* sf_map = NULL;
|
2021-06-20 13:42:06 +02:00
|
|
|
int file_pos = 0;
|
2020-11-08 20:42:57 +01:00
|
|
|
|
|
|
|
sf_map = open_streamfile_by_filename(sf, ".txtm");
|
|
|
|
if (!sf_map) goto fail;
|
|
|
|
|
|
|
|
get_streamfile_filename(sf, filename, sizeof(filename));
|
|
|
|
|
2021-07-08 22:26:21 +02:00
|
|
|
txt_offset = read_bom(sf_map);
|
2020-11-08 20:42:57 +01:00
|
|
|
file_size = get_streamfile_size(sf_map);
|
|
|
|
|
|
|
|
/* read lines and find target filename, format is (filename): value1, ... valueN */
|
|
|
|
while (txt_offset < file_size) {
|
|
|
|
char line[0x2000];
|
|
|
|
char key[PATH_LIMIT] = { 0 }, val[0x2000] = { 0 };
|
|
|
|
int ok, bytes_read, line_ok;
|
|
|
|
|
|
|
|
bytes_read = read_line(line, sizeof(line), txt_offset, sf_map, &line_ok);
|
|
|
|
if (!line_ok) goto fail;
|
|
|
|
|
|
|
|
txt_offset += bytes_read;
|
|
|
|
|
2021-04-03 17:44:45 +02:00
|
|
|
/* get key/val (ignores lead/trailing spaces, stops at comment/separator) */
|
|
|
|
ok = sscanf(line, " %[^\t#:] : %[^\t#\r\n] ", key, val);
|
2020-11-08 20:42:57 +01:00
|
|
|
if (ok != 2) { /* ignore line if no key=val (comment or garbage) */
|
2021-09-12 20:05:56 +02:00
|
|
|
continue;
|
2020-11-08 20:42:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(key, filename) == 0) {
|
|
|
|
int n;
|
|
|
|
char subval[PATH_LIMIT];
|
2021-01-03 16:07:17 +01:00
|
|
|
const char* current = val;
|
2020-11-08 20:42:57 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i <= file_num; i++) {
|
|
|
|
if (current[0] == '\0')
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ok = sscanf(current, " %[^\t#\r\n,]%n ", subval, &n);
|
|
|
|
if (ok != 1)
|
|
|
|
goto fail;
|
|
|
|
|
2021-06-20 13:42:06 +02:00
|
|
|
if (i == file_num) {
|
|
|
|
if (p_pos) *p_pos = file_pos;
|
|
|
|
|
2020-11-08 20:42:57 +01:00
|
|
|
close_streamfile(sf_map);
|
|
|
|
return open_streamfile_by_filename(sf, subval);
|
|
|
|
}
|
|
|
|
|
|
|
|
current += n;
|
|
|
|
if (current[0] == ',')
|
|
|
|
current++;
|
|
|
|
}
|
|
|
|
}
|
2021-06-20 13:42:06 +02:00
|
|
|
file_pos++;
|
2020-11-08 20:42:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close_streamfile(sf_map);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
void fix_dir_separators(char* filename) {
|
2018-11-24 00:44:17 +01:00
|
|
|
char c;
|
|
|
|
int i = 0;
|
|
|
|
while ((c = filename[i]) != '\0') {
|
|
|
|
if ((c == '\\' && DIR_SEPARATOR == '/') || (c == '/' && DIR_SEPARATOR == '\\'))
|
|
|
|
filename[i] = DIR_SEPARATOR;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
/* ************************************************************************* */
|
|
|
|
|
|
|
|
int check_extensions(STREAMFILE* sf, const char* cmp_exts) {
|
2017-01-14 00:59:54 +01:00
|
|
|
char filename[PATH_LIMIT];
|
2021-01-03 16:07:17 +01:00
|
|
|
const char* ext = NULL;
|
|
|
|
const char* cmp_ext = NULL;
|
|
|
|
const char* ststr_res = NULL;
|
2017-05-27 11:38:09 +02:00
|
|
|
size_t ext_len, cmp_len;
|
2017-01-14 00:59:54 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
sf->get_name(sf, filename, sizeof(filename));
|
2017-01-14 00:59:54 +01:00
|
|
|
ext = filename_extension(filename);
|
|
|
|
ext_len = strlen(ext);
|
|
|
|
|
|
|
|
cmp_ext = cmp_exts;
|
|
|
|
do {
|
2017-05-27 11:38:09 +02:00
|
|
|
ststr_res = strstr(cmp_ext, ",");
|
|
|
|
cmp_len = ststr_res == NULL
|
|
|
|
? strlen(cmp_ext) /* total length if more not found */
|
|
|
|
: (intptr_t)ststr_res - (intptr_t)cmp_ext; /* find next ext; ststr_res should always be greater than cmp_ext, resulting in a positive cmp_len */
|
2017-03-10 17:57:28 +01:00
|
|
|
|
2017-05-27 11:38:09 +02:00
|
|
|
if (ext_len == cmp_len && strncasecmp(ext,cmp_ext, ext_len) == 0)
|
2017-01-14 00:59:54 +01:00
|
|
|
return 1;
|
2017-03-10 17:57:28 +01:00
|
|
|
|
2017-05-27 11:38:09 +02:00
|
|
|
cmp_ext = ststr_res;
|
2017-01-14 00:59:54 +01:00
|
|
|
if (cmp_ext != NULL)
|
|
|
|
cmp_ext = cmp_ext + 1; /* skip comma */
|
2017-03-10 17:57:28 +01:00
|
|
|
|
2017-01-14 00:59:54 +01:00
|
|
|
} while (cmp_ext != NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-14 01:37:53 +01:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
/* ************************************************************************* */
|
2017-01-14 01:37:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a chunk starting from an offset, and save its offset/size (if not NULL), with offset after id/size.
|
|
|
|
* Works for chunked headers in the form of "chunk_id chunk_size (data)"xN (ex. RIFF).
|
|
|
|
* The start_offset should be the first actual chunk (not "RIFF" or "WAVE" but "fmt ").
|
|
|
|
* "full_chunk_size" signals chunk_size includes 4+4+data.
|
|
|
|
*
|
|
|
|
* returns 0 on failure
|
|
|
|
*/
|
2021-01-03 16:07:17 +01:00
|
|
|
static int find_chunk_internal(STREAMFILE* sf, uint32_t chunk_id, off_t start_offset, size_t max_size, int full_chunk_size, off_t *out_chunk_offset, size_t *out_chunk_size, int big_endian_type, int big_endian_size, int zero_size_end) {
|
2019-09-08 20:23:59 +02:00
|
|
|
int32_t (*read_32bit_type)(off_t,STREAMFILE*) = big_endian_type ? read_32bitBE : read_32bitLE;
|
|
|
|
int32_t (*read_32bit_size)(off_t,STREAMFILE*) = big_endian_size ? read_32bitBE : read_32bitLE;
|
|
|
|
off_t offset, max_offset;
|
2021-01-03 16:07:17 +01:00
|
|
|
size_t file_size = get_streamfile_size(sf);
|
2019-09-08 20:23:59 +02:00
|
|
|
|
|
|
|
if (max_size == 0)
|
|
|
|
max_size = file_size;
|
|
|
|
|
|
|
|
offset = start_offset;
|
|
|
|
max_offset = offset + max_size;
|
|
|
|
if (max_offset > file_size)
|
|
|
|
max_offset = file_size;
|
|
|
|
|
2017-01-14 01:37:53 +01:00
|
|
|
|
|
|
|
/* read chunks */
|
2019-09-08 20:23:59 +02:00
|
|
|
while (offset < max_offset) {
|
2021-01-03 16:07:17 +01:00
|
|
|
uint32_t chunk_type = read_32bit_type(offset + 0x00,sf);
|
|
|
|
uint32_t chunk_size = read_32bit_size(offset + 0x04,sf);
|
2017-01-14 01:37:53 +01:00
|
|
|
|
2020-06-06 16:12:20 +02:00
|
|
|
if (chunk_type == 0xFFFFFFFF || chunk_size == 0xFFFFFFFF)
|
|
|
|
return 0;
|
|
|
|
|
2017-01-14 01:37:53 +01:00
|
|
|
if (chunk_type == chunk_id) {
|
2019-09-08 20:23:59 +02:00
|
|
|
if (out_chunk_offset) *out_chunk_offset = offset + 0x08;
|
2017-01-14 01:37:53 +01:00
|
|
|
if (out_chunk_size) *out_chunk_size = chunk_size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-02 12:23:42 +02:00
|
|
|
/* empty chunk with 0 size, seen in some formats (XVAG uses it as end marker, Wwise doesn't) */
|
|
|
|
if (chunk_size == 0 && zero_size_end)
|
2017-02-18 15:19:25 +01:00
|
|
|
return 0;
|
|
|
|
|
2019-09-08 20:23:59 +02:00
|
|
|
offset += full_chunk_size ? chunk_size : 0x08 + chunk_size;
|
2017-01-14 01:37:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-03 16:07:17 +01:00
|
|
|
int find_chunk_be(STREAMFILE* sf, uint32_t chunk_id, off_t start_offset, int full_chunk_size, off_t *out_chunk_offset, size_t *out_chunk_size) {
|
|
|
|
return find_chunk(sf, chunk_id, start_offset, full_chunk_size, out_chunk_offset, out_chunk_size, 1, 0);
|
2019-09-08 20:23:59 +02:00
|
|
|
}
|
2021-01-03 16:07:17 +01:00
|
|
|
int find_chunk_le(STREAMFILE* sf, uint32_t chunk_id, off_t start_offset, int full_chunk_size, off_t *out_chunk_offset, size_t *out_chunk_size) {
|
|
|
|
return find_chunk(sf, chunk_id, start_offset, full_chunk_size, out_chunk_offset, out_chunk_size, 0, 0);
|
2019-09-08 20:23:59 +02:00
|
|
|
}
|
2021-01-03 16:07:17 +01:00
|
|
|
int find_chunk(STREAMFILE* sf, uint32_t chunk_id, off_t start_offset, int full_chunk_size, off_t *out_chunk_offset, size_t *out_chunk_size, int big_endian_size, int zero_size_end) {
|
|
|
|
return find_chunk_internal(sf, chunk_id, start_offset, 0, full_chunk_size, out_chunk_offset, out_chunk_size, 1, big_endian_size, zero_size_end);
|
2019-09-08 20:23:59 +02:00
|
|
|
}
|
2021-01-03 16:07:17 +01:00
|
|
|
int find_chunk_riff_le(STREAMFILE* sf, uint32_t chunk_id, off_t start_offset, size_t max_size, off_t *out_chunk_offset, size_t *out_chunk_size) {
|
|
|
|
return find_chunk_internal(sf, chunk_id, start_offset, max_size, 0, out_chunk_offset, out_chunk_size, 1, 0, 0);
|
2019-09-14 12:39:47 +02:00
|
|
|
}
|
2021-01-03 16:07:17 +01:00
|
|
|
int find_chunk_riff_be(STREAMFILE* sf, uint32_t chunk_id, off_t start_offset, size_t max_size, off_t *out_chunk_offset, size_t *out_chunk_size) {
|
|
|
|
return find_chunk_internal(sf, chunk_id, start_offset, max_size, 0, out_chunk_offset, out_chunk_size, 1, 1, 0);
|
2019-09-14 12:39:47 +02:00
|
|
|
}
|
2021-01-03 16:07:17 +01:00
|
|
|
int find_chunk_riff_ve(STREAMFILE* sf, uint32_t chunk_id, off_t start_offset, size_t max_size, off_t *out_chunk_offset, size_t *out_chunk_size, int big_endian) {
|
|
|
|
return find_chunk_internal(sf, chunk_id, start_offset, max_size, 0, out_chunk_offset, out_chunk_size, big_endian, big_endian, 0);
|
2019-09-08 20:23:59 +02:00
|
|
|
}
|
2017-07-15 11:28:42 +02:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
/* ************************************************************************* */
|
|
|
|
|
2018-08-04 20:42:00 +02:00
|
|
|
/* copies name as-is (may include full path included) */
|
2021-01-03 16:07:17 +01:00
|
|
|
void get_streamfile_name(STREAMFILE* sf, char* buffer, size_t size) {
|
|
|
|
sf->get_name(sf, buffer, size);
|
2017-07-15 11:28:42 +02:00
|
|
|
}
|
2021-01-03 16:07:17 +01:00
|
|
|
|
2018-03-29 21:03:25 +02:00
|
|
|
/* copies the filename without path */
|
2021-01-03 16:07:17 +01:00
|
|
|
void get_streamfile_filename(STREAMFILE* sf, char* buffer, size_t size) {
|
2018-02-25 16:27:48 +01:00
|
|
|
char foldername[PATH_LIMIT];
|
2021-01-03 16:07:17 +01:00
|
|
|
const char* path;
|
2018-02-25 16:27:48 +01:00
|
|
|
|
2018-03-29 21:03:25 +02:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
get_streamfile_name(sf, foldername, sizeof(foldername));
|
2018-02-25 16:27:48 +01:00
|
|
|
|
|
|
|
//todo Windows CMD accepts both \\ and /, better way to handle this?
|
|
|
|
path = strrchr(foldername,'\\');
|
|
|
|
if (!path)
|
|
|
|
path = strrchr(foldername,'/');
|
|
|
|
if (path != NULL)
|
|
|
|
path = path+1;
|
|
|
|
|
|
|
|
//todo validate sizes and copy sensible max
|
|
|
|
if (path) {
|
|
|
|
strcpy(buffer, path);
|
|
|
|
} else {
|
|
|
|
strcpy(buffer, foldername);
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 16:07:17 +01:00
|
|
|
|
2018-08-04 20:42:00 +02:00
|
|
|
/* copies the filename without path or extension */
|
2021-01-03 16:07:17 +01:00
|
|
|
void get_streamfile_basename(STREAMFILE* sf, char* buffer, size_t size) {
|
|
|
|
char* ext;
|
2018-08-04 20:42:00 +02:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
get_streamfile_filename(sf, buffer, size);
|
2018-08-04 20:42:00 +02:00
|
|
|
|
|
|
|
ext = strrchr(buffer,'.');
|
|
|
|
if (ext) {
|
|
|
|
ext[0] = '\0'; /* remove .ext from buffer */
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 16:07:17 +01:00
|
|
|
|
2018-08-04 20:42:00 +02:00
|
|
|
/* copies path removing name (NULL when if filename has no path) */
|
2021-01-03 16:07:17 +01:00
|
|
|
void get_streamfile_path(STREAMFILE* sf, char* buffer, size_t size) {
|
|
|
|
const char* path;
|
2017-07-15 11:28:42 +02:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
get_streamfile_name(sf, buffer, size);
|
2017-07-15 11:28:42 +02:00
|
|
|
|
|
|
|
path = strrchr(buffer,DIR_SEPARATOR);
|
|
|
|
if (path!=NULL) path = path+1; /* includes "/" */
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
buffer[path - buffer] = '\0';
|
|
|
|
} else {
|
|
|
|
buffer[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 16:07:17 +01:00
|
|
|
|
|
|
|
/* copies extension only */
|
|
|
|
void get_streamfile_ext(STREAMFILE* sf, char* buffer, size_t size) {
|
|
|
|
char filename[PATH_LIMIT];
|
|
|
|
const char* extension = NULL;
|
|
|
|
|
|
|
|
get_streamfile_name(sf, filename, sizeof(filename));
|
|
|
|
extension = filename_extension(filename);
|
|
|
|
if (!extension) {
|
|
|
|
buffer[0] = '\n';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strncpy(buffer, extension, size); //todo use something better
|
|
|
|
}
|
2017-07-15 11:28:42 +02:00
|
|
|
}
|
2018-08-11 17:58:16 +02:00
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
/* ************************************************************************* */
|
|
|
|
|
2018-08-11 17:58:16 +02:00
|
|
|
/* debug util, mainly for custom IO testing */
|
2021-01-03 16:07:17 +01:00
|
|
|
void dump_streamfile(STREAMFILE* sf, int num) {
|
2018-08-12 13:58:48 +02:00
|
|
|
#ifdef VGM_DEBUG_OUTPUT
|
2021-09-04 21:57:23 +02:00
|
|
|
offv_t offset = 0;
|
2021-01-03 16:07:17 +01:00
|
|
|
FILE* f = NULL;
|
2018-08-11 17:58:16 +02:00
|
|
|
|
2019-02-22 23:52:39 +01:00
|
|
|
if (num >= 0) {
|
|
|
|
char filename[PATH_LIMIT];
|
|
|
|
char dumpname[PATH_LIMIT];
|
|
|
|
|
2021-09-04 21:57:23 +02:00
|
|
|
get_streamfile_filename(sf, filename, sizeof(filename));
|
|
|
|
snprintf(dumpname, sizeof(dumpname), "%s_%02i.dump", filename, num);
|
2019-02-22 23:52:39 +01:00
|
|
|
|
2021-09-12 21:29:07 +02:00
|
|
|
f = fopen_v(dumpname,"wb");
|
2018-08-11 17:58:16 +02:00
|
|
|
if (!f) return;
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:07:17 +01:00
|
|
|
VGM_LOG("dump streamfile: size %x\n", get_streamfile_size(sf));
|
|
|
|
while (offset < get_streamfile_size(sf)) {
|
2021-09-04 21:57:23 +02:00
|
|
|
uint8_t buf[0x8000];
|
|
|
|
size_t bytes;
|
2018-08-11 17:58:16 +02:00
|
|
|
|
2021-09-04 21:57:23 +02:00
|
|
|
bytes = read_streamfile(buf, offset, sizeof(buf), sf);
|
|
|
|
if(!bytes) {
|
2021-09-15 23:09:19 +02:00
|
|
|
VGM_LOG("dump streamfile: can't read at %x\n", (uint32_t)offset);
|
2019-03-16 00:27:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-02-22 23:52:39 +01:00
|
|
|
if (f)
|
2021-09-04 21:57:23 +02:00
|
|
|
fwrite(buf, sizeof(uint8_t), bytes, f);
|
2018-08-11 17:58:16 +02:00
|
|
|
else
|
2021-09-04 21:57:23 +02:00
|
|
|
VGM_LOGB(buf, bytes, 0);
|
|
|
|
offset += bytes;
|
2018-08-11 17:58:16 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 23:52:39 +01:00
|
|
|
if (f) {
|
2018-08-11 17:58:16 +02:00
|
|
|
fclose(f);
|
|
|
|
}
|
2018-08-12 13:58:48 +02:00
|
|
|
#endif
|
2018-08-11 17:58:16 +02:00
|
|
|
}
|