build: fix some emscripten/wasm crashes

This commit is contained in:
bnnm 2021-09-12 20:05:56 +02:00
parent 91e12ab559
commit 518cf8f8b1
2 changed files with 39 additions and 22 deletions

View File

@ -2,6 +2,10 @@
#include "util.h"
#include "vgmstream.h"
/* for dup/fdopen in some systems */
#ifndef _MSC_VER
#include <unistd.h>
#endif
/* MSVC fixes (though mingw uses MSVCRT but not MSC_VER, maybe use AND?) */
#if defined(__MSVCRT__) || defined(_MSC_VER)
@ -15,6 +19,7 @@
#define ftello ftell
#endif
*/
#define fseek_v _fseeki64 //fseek/fseeko
#define ftell_v _ftelli64 //ftell/ftello
@ -34,7 +39,7 @@
#define ftell_v ftell
#else
#define fseek_v fseeko64 //fseeko
#define ftell_v ftello64 //ftelloo
#define ftell_v ftello64 //ftello
#endif
@ -154,12 +159,6 @@ static void stdio_get_name(STDIO_STREAMFILE* sf, char* name, size_t name_size) {
strncpy(name, sf->name, name_size);
name[name_size - 1] = '\0';
}
static void stdio_close(STDIO_STREAMFILE* sf) {
if (sf->infile)
fclose(sf->infile);
free(sf->buf);
free(sf);
}
static STREAMFILE* stdio_open(STDIO_STREAMFILE* sf, const char* const filename, size_t buf_size) {
if (!filename)
@ -192,6 +191,14 @@ static STREAMFILE* stdio_open(STDIO_STREAMFILE* sf, const char* const filename,
return open_stdio_streamfile_buffer(filename, buf_size);
}
static void stdio_close(STDIO_STREAMFILE* sf) {
if (sf->infile)
fclose(sf->infile);
free(sf->buf);
free(sf);
}
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;
@ -360,10 +367,12 @@ static offv_t buffer_get_offset(BUFFER_STREAMFILE* sf) {
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 */
}
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? */
}
static void buffer_close(BUFFER_STREAMFILE* sf) {
sf->inner_sf->close(sf->inner_sf);
free(sf->buf);
@ -435,12 +444,14 @@ static size_t wrap_get_size(WRAP_STREAMFILE* sf) {
static offv_t wrap_get_offset(WRAP_STREAMFILE* sf) {
return sf->inner_sf->get_offset(sf->inner_sf); /* default */
}
static void wrap_get_name(WRAP_STREAMFILE* sf, char* name, size_t name_len) {
sf->inner_sf->get_name(sf->inner_sf, name, name_len); /* default */
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 */
}
static void wrap_open(WRAP_STREAMFILE* sf, const char* const filename, size_t buf_size) {
sf->inner_sf->open(sf->inner_sf, filename, buf_size); /* default (don't wrap) */
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) */
}
static void wrap_close(WRAP_STREAMFILE* sf) {
//sf->inner_sf->close(sf->inner_sf); /* don't close */
free(sf);
@ -503,9 +514,10 @@ static size_t clamp_get_size(CLAMP_STREAMFILE* sf) {
static offv_t clamp_get_offset(CLAMP_STREAMFILE* sf) {
return sf->inner_sf->get_offset(sf->inner_sf) - sf->start;
}
static void clamp_get_name(CLAMP_STREAMFILE* sf, char* name, size_t name_len) {
sf->inner_sf->get_name(sf->inner_sf, name, name_len); /* default */
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 */
}
static STREAMFILE* clamp_open(CLAMP_STREAMFILE* sf, const char* const filename, size_t buf_size) {
char original_filename[PATH_LIMIT];
STREAMFILE* new_inner_sf = NULL;
@ -520,6 +532,7 @@ static STREAMFILE* clamp_open(CLAMP_STREAMFILE* sf, const char* const filename,
return new_inner_sf;
}
}
static void clamp_close(CLAMP_STREAMFILE* sf) {
sf->inner_sf->close(sf->inner_sf);
free(sf);
@ -582,13 +595,15 @@ static size_t io_get_size(IO_STREAMFILE* sf) {
static offv_t io_get_offset(IO_STREAMFILE* sf) {
return sf->inner_sf->get_offset(sf->inner_sf); /* default */
}
static void io_get_name(IO_STREAMFILE* sf, char* name, size_t name_len) {
sf->inner_sf->get_name(sf->inner_sf, name, name_len); /* default */
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 */
}
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);
}
static void io_close(IO_STREAMFILE* sf) {
if (sf->close_callback)
sf->close_callback(sf->inner_sf, sf->data);
@ -676,6 +691,7 @@ static void fakename_get_name(FAKENAME_STREAMFILE* sf, char* name, size_t name_s
strncpy(name,sf->fakename, name_size);
name[name_size - 1] = '\0';
}
static STREAMFILE* fakename_open(FAKENAME_STREAMFILE* sf, const char* const filename, size_t buf_size) {
/* detect re-opening the file */
if (strcmp(filename, sf->fakename) == 0) {
@ -797,6 +813,7 @@ static offv_t multifile_get_offset(MULTIFILE_STREAMFILE* sf) {
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);
}
static STREAMFILE* multifile_open(MULTIFILE_STREAMFILE* sf, const char* const filename, size_t buf_size) {
char original_filename[PATH_LIMIT];
STREAMFILE* new_sf = NULL;

View File

@ -66,16 +66,16 @@ typedef struct _STREAMFILE {
size_t (*get_size)(struct _STREAMFILE* sf);
//todo: DO NOT USE, NOT RESET PROPERLY (remove?)
offv_t (*get_offset)(struct _STREAMFILE*);
offv_t (*get_offset)(struct _STREAMFILE* sf);
/* copy current filename to name buf */
void (*get_name)(struct _STREAMFILE* sf, char* name, size_t name_size);
/* open another streamfile from filename */
struct _STREAMFILE* (*open)(struct _STREAMFILE* sf, const char* const filename, size_t buffer_size);
struct _STREAMFILE* (*open)(struct _STREAMFILE* sf, const char* const filename, size_t buf_size);
/* free current STREAMFILE */
void (*close)(struct _STREAMFILE*);
void (*close)(struct _STREAMFILE* sf);
/* Substream selection for formats with subsongs.
* Not ideal here, but it was the simplest way to pass to all init_vgmstream_x functions. */