streamfiles: fix off_t in MSVC/custom IO callback

This commit is contained in:
bnnm 2021-09-15 23:09:19 +02:00
parent 402530fc6a
commit af2f88993a
2 changed files with 14 additions and 27 deletions

View File

@ -16,22 +16,13 @@
* - Clang: seems only defined on Linux/GNU environments, somehow emscripten is out * - Clang: seems only defined on Linux/GNU environments, somehow emscripten is out
* (unsure about Clang Win since apparently they define _MSC_VER) * (unsure about Clang Win since apparently they define _MSC_VER)
* - Android: API +24 if not using __USE_FILE_OFFSET64 * - Android: API +24 if not using __USE_FILE_OFFSET64
* Not sure if fopen64 is needed in some cases. May be work adding some compiler flag to control this manually. * Not sure if fopen64 is needed in some cases. May be worth adding some compiler flag to enable 64 versions manually.
*/ */
/* MSVC fixes (though mingw uses MSVCRT but not MSC_VER, maybe use AND?) */ /* MSVC fixes (though mingw uses MSVCRT but not MSC_VER, maybe use AND?) */
#if defined(__MSVCRT__) || defined(_MSC_VER) #if defined(__MSVCRT__) || defined(_MSC_VER)
#include <io.h> #include <io.h>
/*
#ifndef fseeko
#define fseeko fseek
#endif
#ifndef ftello
#define ftello ftell
#endif
*/
#define fopen_v fopen #define fopen_v fopen
#if (_MSC_VER >= 1400) #if (_MSC_VER >= 1400)
#define fseek_v _fseeki64 #define fseek_v _fseeki64
@ -48,9 +39,9 @@
#define fdopen _fdopen #define fdopen _fdopen
#define dup _dup #define dup _dup
#ifndef off64_t //#ifndef off64_t
#define off_t __int64 // #define off_t/off64_t __int64
#endif //#endif
#elif defined(XBMC) || defined(__EMSCRIPTEN__) || defined (__ANDROID__) #elif defined(XBMC) || defined(__EMSCRIPTEN__) || defined (__ANDROID__)
#define fopen_v fopen #define fopen_v fopen
@ -87,7 +78,7 @@ static size_t stdio_read(STDIO_STREAMFILE* sf, uint8_t* dst, offv_t offset, size
if (!sf->infile || !dst || length <= 0 || offset < 0) if (!sf->infile || !dst || length <= 0 || offset < 0)
return 0; return 0;
//;VGM_LOG("STDIO: read %lx + %x (buf %lx + %x)\n", offset, length, sf->buf_offset, sf->valid_size); //;VGM_LOG("stdio: read %lx + %x (buf %lx + %x)\n", offset, length, sf->buf_offset, sf->valid_size);
/* is the part of the requested length in the buffer? */ /* is the part of the requested length in the buffer? */
if (offset >= sf->buf_offset && offset < sf->buf_offset + sf->valid_size) { if (offset >= sf->buf_offset && offset < sf->buf_offset + sf->valid_size) {
@ -98,7 +89,7 @@ static size_t stdio_read(STDIO_STREAMFILE* sf, uint8_t* dst, offv_t offset, size
if (buf_limit > length) if (buf_limit > length)
buf_limit = length; buf_limit = length;
//;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); //;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);
memcpy(dst, sf->buf + buf_into, buf_limit); memcpy(dst, sf->buf + buf_into, buf_limit);
read_total += buf_limit; read_total += buf_limit;
@ -109,7 +100,7 @@ static size_t stdio_read(STDIO_STREAMFILE* sf, uint8_t* dst, offv_t offset, size
#ifdef VGM_DEBUG_OUTPUT #ifdef VGM_DEBUG_OUTPUT
if (offset < sf->buf_offset && length > 0) { if (offset < sf->buf_offset && length > 0) {
VGM_LOG("STDIO: rebuffer, requested %lx vs %lx (sf %x)\n", offset, sf->buf_offset, (uint32_t)sf); VGM_LOG("stdio: rebuffer, requested %x vs %x (sf %x)\n", (uint32_t)offset, (uint32_t)sf->buf_offset, (uint32_t)sf);
//sf->rebuffer++; //sf->rebuffer++;
//if (rebuffer > N) ... //if (rebuffer > N) ...
} }
@ -143,7 +134,7 @@ static size_t stdio_read(STDIO_STREAMFILE* sf, uint8_t* dst, offv_t offset, size
/* fill the buffer (offset now is beyond buf_offset) */ /* fill the buffer (offset now is beyond buf_offset) */
sf->buf_offset = offset; sf->buf_offset = offset;
sf->valid_size = fread(sf->buf, sizeof(uint8_t), sf->buf_size, sf->infile); sf->valid_size = fread(sf->buf, sizeof(uint8_t), sf->buf_size, sf->infile);
//;VGM_LOG("STDIO: read buf %lx + %x\n", sf->buf_offset, sf->valid_size); //;VGM_LOG("stdio: read buf %lx + %x\n", sf->buf_offset, sf->valid_size);
/* decide how much must be read this time */ /* decide how much must be read this time */
if (length > sf->buf_size) if (length > sf->buf_size)
@ -335,7 +326,7 @@ static size_t buffer_read(BUFFER_STREAMFILE* sf, uint8_t* dst, offv_t offset, si
#ifdef VGM_DEBUG_OUTPUT #ifdef VGM_DEBUG_OUTPUT
if (offset < sf->buf_offset) { if (offset < sf->buf_offset) {
VGM_LOG("BUFFER: rebuffer, requested %lx vs %lx (sf %x)\n", offset, sf->buf_offset, (uint32_t)sf); VGM_LOG("buffer: rebuffer, requested %x vs %x (sf %x)\n", (uint32_t)offset, (uint32_t)sf->buf_offset, (uint32_t)sf);
} }
#endif #endif
@ -346,7 +337,7 @@ static size_t buffer_read(BUFFER_STREAMFILE* sf, uint8_t* dst, offv_t offset, si
/* ignore requests at EOF */ /* ignore requests at EOF */
if (offset >= sf->file_size) { if (offset >= sf->file_size) {
//offset = sf->file_size; /* seems fseek doesn't clamp offset */ //offset = sf->file_size; /* seems fseek doesn't clamp offset */
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); 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);
break; break;
} }
@ -598,14 +589,15 @@ typedef struct {
STREAMFILE* inner_sf; STREAMFILE* inner_sf;
void* data; /* state for custom reads, malloc'ed + copied on open (to re-open streamfiles cleanly) */ void* data; /* state for custom reads, malloc'ed + copied on open (to re-open streamfiles cleanly) */
size_t data_size; size_t data_size;
size_t (*read_callback)(STREAMFILE*, uint8_t*, offv_t, size_t, void*); /* custom read to modify data before copying into buffer */ size_t (*read_callback)(STREAMFILE*, uint8_t*, off_t, size_t, void*); /* custom read to modify data before copying into buffer */
size_t (*size_callback)(STREAMFILE*, void*); /* size when custom reads make data smaller/bigger than underlying streamfile */ size_t (*size_callback)(STREAMFILE*, void*); /* size when custom reads make data smaller/bigger than underlying streamfile */
int (*init_callback)(STREAMFILE*, void*); /* init the data struct members somehow, return >= 0 if ok */ 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 */ void (*close_callback)(STREAMFILE*, void*); /* close the data struct members somehow */
/* read doesn't use offv_t since callbacks would need to be modified */
} IO_STREAMFILE; } IO_STREAMFILE;
static size_t io_read(IO_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) { static size_t io_read(IO_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
return sf->read_callback(sf->inner_sf, dst, offset, length, sf->data); return sf->read_callback(sf->inner_sf, dst, (off_t)offset, length, sf->data);
} }
static size_t io_get_size(IO_STREAMFILE* sf) { static size_t io_get_size(IO_STREAMFILE* sf) {
if (sf->size_callback) if (sf->size_callback)
@ -1365,7 +1357,6 @@ static int find_chunk_internal(STREAMFILE* sf, uint32_t chunk_id, off_t start_of
while (offset < max_offset) { while (offset < max_offset) {
uint32_t chunk_type = read_32bit_type(offset + 0x00,sf); uint32_t chunk_type = read_32bit_type(offset + 0x00,sf);
uint32_t chunk_size = read_32bit_size(offset + 0x04,sf); uint32_t chunk_size = read_32bit_size(offset + 0x04,sf);
//;VGM_LOG("CHUNK: type=%x, size=%x at %lx\n", chunk_type, chunk_size, offset);
if (chunk_type == 0xFFFFFFFF || chunk_size == 0xFFFFFFFF) if (chunk_type == 0xFFFFFFFF || chunk_size == 0xFFFFFFFF)
return 0; return 0;
@ -1503,7 +1494,7 @@ void dump_streamfile(STREAMFILE* sf, int num) {
bytes = read_streamfile(buf, offset, sizeof(buf), sf); bytes = read_streamfile(buf, offset, sizeof(buf), sf);
if(!bytes) { if(!bytes) {
VGM_LOG("dump streamfile: can't read at %lx\n", offset); VGM_LOG("dump streamfile: can't read at %x\n", (uint32_t)offset);
break; break;
} }

View File

@ -20,10 +20,6 @@
/* MSVC fixes (though mingw uses MSVCRT but not MSC_VER, maybe use AND?) */ /* MSVC fixes (though mingw uses MSVCRT but not MSC_VER, maybe use AND?) */
#if defined(__MSVCRT__) || defined(_MSC_VER) #if defined(__MSVCRT__) || defined(_MSC_VER)
#include <io.h> #include <io.h>
#ifndef off64_t
#define off_t __int64
#endif
#endif #endif
#ifndef DIR_SEPARATOR #ifndef DIR_SEPARATOR