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
@ -48,7 +53,7 @@ typedef struct {
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 valid_size; /* current buffer size */
size_t file_size; /* buffered file size */
} STDIO_STREAMFILE;
@ -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)
@ -187,11 +186,19 @@ static STREAMFILE* stdio_open(STDIO_STREAMFILE* sf, const char* const filename,
/* on failure just close and try the default path (which will probably fail a second time) */
}
#endif
#endif
// a normal open, open a new file
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);
@ -633,7 +648,7 @@ STREAMFILE* open_io_streamfile_ex(STREAMFILE* sf, void* data, size_t data_size,
}
return &this_sf->vt;
fail:
if (this_sf) free(this_sf->data);
free(this_sf);
@ -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;
@ -1219,7 +1236,7 @@ STREAMFILE* read_filemap_file_pos(STREAMFILE* sf, int file_num, int* p_pos) {
/* get key/val (ignores lead/trailing spaces, stops at comment/separator) */
ok = sscanf(line, " %[^\t#:] : %[^\t#\r\n] ", key, val);
if (ok != 2) { /* ignore line if no key=val (comment or garbage) */
continue;
continue;
}
if (strcmp(key, filename) == 0) {

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. */