diff --git a/Makefile b/Makefile index 93104181..797e0595 100644 --- a/Makefile +++ b/Makefile @@ -257,16 +257,18 @@ sourceball: # git archive --format zip --output bin/vgmstream-$(VGMSTREAM_VERSION)-src.zip master rm -rf vgmstream-$(VGMSTREAM_VERSION) -bin: vgmstream_cli winamp xmplay +bin: vgmstream-cli winamp xmplay mkdir -p bin zip -FS -j "bin/$(BIN_FILE).zip" $(ZIP_FILES) #separate since vgmstream123 is kinda untested -bin-ex: vgmstream_cli winamp xmplay vgmstream123 +bin-ex: vgmstream-cli winamp xmplay vgmstream123 mkdir -p bin zip -FS -j "bin/$(BIN_FILE).zip" $(ZIP_FILES) $(ZIP_FILES_AO) -vgmstream_cli: version +vgmstream_cli: vgmstream-cli + +vgmstream-cli: version $(MAKE) -C cli vgmstream_cli vgmstream123: version @@ -289,4 +291,4 @@ clean: $(MAKE) -C xmplay clean $(MAKE) -C ext_libs clean -.PHONY: clean buildfullrelease buildrelease sourceball bin vgmstream_cli winamp +.PHONY: clean buildfullrelease buildrelease sourceball bin vgmstream-cli vgmstream_cli vgmstream123 winamp xmplay version diff --git a/doc/BUILD.md b/doc/BUILD.md index 14f79576..84863fe3 100644 --- a/doc/BUILD.md +++ b/doc/BUILD.md @@ -285,7 +285,7 @@ sudo apt-get install gcc g++ make git sudo apt-get install autoconf automake libtool # vgmstream dependencies sudo apt-get install libmpg123-dev libvorbis-dev libspeex-dev -#sudo apt-get install libavformat-dev libavcodec-dev libavutil-dev libswresample-dev +sudo apt-get install libavformat-dev libavcodec-dev libavutil-dev libswresample-dev # Audacious player and dependencies sudo apt-get install audacious sudo apt-get install audacious-dev libglib2.0-dev libgtk2.0-dev libpango1.0-dev diff --git a/fb2k/foo_streamfile.cpp b/fb2k/foo_streamfile.cpp index 14c1364a..6a00f3ea 100644 --- a/fb2k/foo_streamfile.cpp +++ b/fb2k/foo_streamfile.cpp @@ -10,7 +10,8 @@ #include extern "C" { -#include "../src/vgmstream.h" +#include "../src/streamfile.h" +//#include "../src/vgmstream.h" #include "../src/util.h" } #include "foo_vgmstream.h" @@ -18,184 +19,183 @@ extern "C" { /* a STREAMFILE that operates via foobar's file service using a buffer */ typedef struct { - STREAMFILE sf; /* callbacks */ + STREAMFILE vt; /* callbacks */ bool m_file_opened; /* if foobar IO service opened the file */ service_ptr_t m_file; /* foobar IO service */ abort_callback * p_abort; /* foobar error stuff */ - char * name; /* IO filename */ - offv_t offset; /* last read offset (info) */ - offv_t buffer_offset; /* current buffer data start */ - uint8_t * buffer; /* data buffer */ - size_t buffersize; /* max buffer size */ - size_t validsize; /* current buffer size */ - size_t filesize; /* buffered file size */ + char* name; /* IO filename */ + 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 */ } FOO_STREAMFILE; -static STREAMFILE * open_foo_streamfile_buffer(const char * const filename, size_t buffersize, abort_callback * p_abort, t_filestats * stats); -static STREAMFILE * open_foo_streamfile_buffer_by_file(service_ptr_t m_file, bool m_file_opened, const char * const filename, size_t buffersize, abort_callback * p_abort); +static STREAMFILE* open_foo_streamfile_buffer(const char* const filename, size_t buf_size, abort_callback* p_abort, t_filestats* stats); +static STREAMFILE* open_foo_streamfile_buffer_by_file(service_ptr_t m_file, bool m_file_opened, const char* const filename, size_t buf_size, abort_callback* p_abort); -static size_t read_foo(FOO_STREAMFILE *streamfile, uint8_t * dest, offv_t offset, size_t length) { - size_t length_read_total = 0; +static size_t foo_read(FOO_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) { + size_t read_total = 0; - if (!streamfile || !streamfile->m_file_opened || !dest || length <= 0 || offset < 0) + if (!sf || !sf->m_file_opened || !dst || length <= 0 || offset < 0) return 0; /* is the part of the requested length in the buffer? */ - if (offset >= streamfile->buffer_offset && offset < streamfile->buffer_offset + streamfile->validsize) { - size_t length_to_read; - int offset_into_buffer = (offset - streamfile->buffer_offset); + if (offset >= sf->buf_offset && offset < sf->buf_offset + sf->valid_size) { + size_t buf_limit; + int buf_into = (int)(offset - sf->buf_offset); - length_to_read = streamfile->validsize - offset_into_buffer; - if (length_to_read > length) - length_to_read = length; + buf_limit = sf->valid_size - buf_into; + if (buf_limit > length) + buf_limit = length; - memcpy(dest, streamfile->buffer + offset_into_buffer, length_to_read); - length_read_total += length_to_read; - length -= length_to_read; - offset += length_to_read; - dest += length_to_read; + memcpy(dst, sf->buf + buf_into, buf_limit); + read_total += buf_limit; + length -= buf_limit; + offset += buf_limit; + dst += buf_limit; } /* read the rest of the requested length */ while (length > 0) { - size_t length_to_read; + size_t buf_limit; /* ignore requests at EOF */ - if (offset >= streamfile->filesize) { - //offset = streamfile->filesize; /* seems fseek doesn't clamp offset */ - //VGM_ASSERT_ONCE(offset > streamfile->filesize, "STDIO: reading over filesize 0x%x @ 0x%lx + 0x%x\n", streamfile->filesize, offset, length); + 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%lx + 0x%x\n", sf->file_size, offset, length); break; } /* position to new offset */ try { - streamfile->m_file->seek(offset,*streamfile->p_abort); + sf->m_file->seek(offset, *sf->p_abort); } catch (...) { break; /* this shouldn't happen in our code */ } - /* fill the buffer (offset now is beyond buffer_offset) */ + /* fill the buffer (offset now is beyond buf_offset) */ try { - streamfile->buffer_offset = offset; - streamfile->validsize = streamfile->m_file->read(streamfile->buffer,streamfile->buffersize,*streamfile->p_abort); + sf->buf_offset = offset; + sf->valid_size = sf->m_file->read(sf->buf, sf->buf_size, *sf->p_abort); } catch(...) { break; /* improbable? */ } /* decide how much must be read this time */ - if (length > streamfile->buffersize) - length_to_read = streamfile->buffersize; + if (length > sf->buf_size) + buf_limit = sf->buf_size; else - length_to_read = length; + buf_limit = length; /* give up on partial reads (EOF) */ - if (streamfile->validsize < length_to_read) { - memcpy(dest,streamfile->buffer,streamfile->validsize); - offset += streamfile->validsize; - length_read_total += streamfile->validsize; + if (sf->valid_size < buf_limit) { + memcpy(dst, sf->buf, sf->valid_size); + offset += sf->valid_size; + read_total += sf->valid_size; break; } /* use the new buffer */ - memcpy(dest,streamfile->buffer,length_to_read); - offset += length_to_read; - length_read_total += length_to_read; - length -= length_to_read; - dest += length_to_read; + memcpy(dst, sf->buf, buf_limit); + offset += buf_limit; + read_total += buf_limit; + length -= buf_limit; + dst += buf_limit; } - streamfile->offset = offset; /* last fread offset */ - return length_read_total; + sf->offset = offset; /* last fread offset */ + return read_total; } -static size_t get_size_foo(FOO_STREAMFILE * streamfile) { - return streamfile->filesize; +static size_t foo_get_size(FOO_STREAMFILE* sf) { + return sf->file_size; } -static offv_t get_offset_foo(FOO_STREAMFILE *streamfile) { - return streamfile->offset; +static offv_t foo_get_offset(FOO_STREAMFILE* sf) { + return sf->offset; } -static void get_name_foo(FOO_STREAMFILE *streamfile,char *buffer,size_t length) { - /* Most crap only cares about the filename itself */ - size_t ourlen = strlen(streamfile->name); - if (ourlen > length) { - if (length) strcpy(buffer, streamfile->name + ourlen - length + 1); - } else { - strcpy(buffer, streamfile->name); - } +static void foo_get_name(FOO_STREAMFILE* sf, char* name, size_t name_size) { + /* Most crap only cares about the filename itself */ + size_t ourlen = strlen(sf->name); + if (ourlen > name_size) { + if (name_size) strcpy(name, sf->name + ourlen - name_size + 1); + } + else { + strcpy(name, sf->name); + } } -static void close_foo(FOO_STREAMFILE * streamfile) { - streamfile->m_file.release(); //release alloc'ed ptr - free(streamfile->name); - free(streamfile->buffer); - free(streamfile); +static void foo_close(FOO_STREAMFILE* sf) { + sf->m_file.release(); //release alloc'ed ptr + free(sf->name); + free(sf->buf); + free(sf); } -static STREAMFILE *open_foo(FOO_STREAMFILE *streamFile,const char * const filename,size_t buffersize) { +static STREAMFILE* foo_open(FOO_STREAMFILE* sf, const char* const filename,size_t buf_size) { service_ptr_t m_file; - STREAMFILE *newstreamFile; - if (!filename) return NULL; // if same name, duplicate the file pointer we already have open - if (streamFile->m_file_opened && !strcmp(streamFile->name,filename)) { - m_file = streamFile->m_file; //copy? + if (sf->m_file_opened && !strcmp(sf->name,filename)) { + m_file = sf->m_file; //copy? { - newstreamFile = open_foo_streamfile_buffer_by_file(m_file, streamFile->m_file_opened, filename, buffersize, streamFile->p_abort); - if (newstreamFile) { - return newstreamFile; + STREAMFILE* new_sf = open_foo_streamfile_buffer_by_file(m_file, sf->m_file_opened, filename, buf_size, sf->p_abort); + if (new_sf) { + return new_sf; } // failure, close it and try the default path (which will probably fail a second time) } } // a normal open, open a new file - return open_foo_streamfile_buffer(filename,buffersize,streamFile->p_abort,NULL); + return open_foo_streamfile_buffer(filename,buf_size,sf->p_abort,NULL); } -static STREAMFILE * open_foo_streamfile_buffer_by_file(service_ptr_t m_file, bool m_file_opened, const char * const filename, size_t buffersize, abort_callback * p_abort) { - uint8_t * buffer; - FOO_STREAMFILE * streamfile; +static STREAMFILE* open_foo_streamfile_buffer_by_file(service_ptr_t m_file, bool m_file_opened, const char* const filename, size_t buf_size, abort_callback* p_abort) { + uint8_t* buf; + FOO_STREAMFILE* this_sf; - buffer = (uint8_t *) calloc(buffersize,1); - if (!buffer) goto fail; + buf = (uint8_t*) calloc(buf_size, sizeof(uint8_t)); + if (!buf) goto fail; - streamfile = (FOO_STREAMFILE *) calloc(1,sizeof(FOO_STREAMFILE)); - if (!streamfile) goto fail; + this_sf = (FOO_STREAMFILE*) calloc(1, sizeof(FOO_STREAMFILE)); + if (!this_sf) goto fail; - streamfile->sf.read = (size_t (__cdecl *)(_STREAMFILE *,uint8_t *,offv_t,size_t)) read_foo; - streamfile->sf.get_size = (size_t (__cdecl *)(_STREAMFILE *)) get_size_foo; - streamfile->sf.get_offset = (offv_t (__cdecl *)(_STREAMFILE *)) get_offset_foo; - streamfile->sf.get_name = (void (__cdecl *)(_STREAMFILE *,char *,size_t)) get_name_foo; - streamfile->sf.open = (_STREAMFILE *(__cdecl *)(_STREAMFILE *,const char *const ,size_t)) open_foo; - streamfile->sf.close = (void (__cdecl *)(_STREAMFILE *)) close_foo; + this_sf->vt.read = (size_t (__cdecl *)(_STREAMFILE*, uint8_t*, offv_t, size_t)) foo_read; + this_sf->vt.get_size = (size_t (__cdecl *)(_STREAMFILE*)) foo_get_size; + this_sf->vt.get_offset = (offv_t (__cdecl *)(_STREAMFILE*)) foo_get_offset; + this_sf->vt.get_name = (void (__cdecl *)(_STREAMFILE*, char*, size_t)) foo_get_name; + this_sf->vt.open = (_STREAMFILE* (__cdecl *)(_STREAMFILE* ,const char* const, size_t)) foo_open; + this_sf->vt.close = (void (__cdecl *)(_STREAMFILE* )) foo_close; - streamfile->m_file_opened = m_file_opened; - streamfile->m_file = m_file; - streamfile->p_abort = p_abort; - streamfile->buffersize = buffersize; - streamfile->buffer = buffer; + this_sf->m_file_opened = m_file_opened; + this_sf->m_file = m_file; + this_sf->p_abort = p_abort; + this_sf->buf_size = buf_size; + this_sf->buf = buf; - streamfile->name = strdup(filename); - if (!streamfile->name) goto fail; + this_sf->name = strdup(filename); + if (!this_sf->name) goto fail; - /* cache filesize */ - if (streamfile->m_file_opened) - streamfile->filesize = streamfile->m_file->get_size(*streamfile->p_abort); + /* cache file_size */ + if (this_sf->m_file_opened) + this_sf->file_size = this_sf->m_file->get_size(*this_sf->p_abort); else - streamfile->filesize = 0; + this_sf->file_size = 0; - return &streamfile->sf; + return &this_sf->vt; fail: - free(buffer); - free(streamfile); + free(buf); + free(this_sf); return NULL; } -static STREAMFILE* open_foo_streamfile_buffer(const char* const filename, size_t buffersize, abort_callback* p_abort, t_filestats* stats) { +static STREAMFILE* open_foo_streamfile_buffer(const char* const filename, size_t buf_size, abort_callback* p_abort, t_filestats* stats) { STREAMFILE* sf = NULL; service_ptr_t infile; bool infile_exists; @@ -213,7 +213,7 @@ static STREAMFILE* open_foo_streamfile_buffer(const char* const filename, size_t if(stats) *stats = infile->get_stats(*p_abort); } - sf = open_foo_streamfile_buffer_by_file(infile, infile_exists, filename, buffersize, p_abort); + sf = open_foo_streamfile_buffer_by_file(infile, infile_exists, filename, buf_size, p_abort); if (!sf) { //m_file.release(); //refcounted and cleaned after it goes out of scope } diff --git a/fb2k/foo_vgmstream.h b/fb2k/foo_vgmstream.h index 4f53bc17..f452ad6e 100644 --- a/fb2k/foo_vgmstream.h +++ b/fb2k/foo_vgmstream.h @@ -3,6 +3,10 @@ #define SAMPLE_BUFFER_SIZE 1024 +extern "C" { +#include "../src/vgmstream.h" +} + class input_vgmstream : public input_stubs { public: @@ -69,18 +73,18 @@ class input_vgmstream : public input_stubs { //bool exts_unknown_on; /* helpers */ - VGMSTREAM * init_vgmstream_foo(t_uint32 p_subsong, const char * const filename, abort_callback & p_abort); - void setup_vgmstream(abort_callback & p_abort); + VGMSTREAM* init_vgmstream_foo(t_uint32 p_subsong, const char* const filename, abort_callback& p_abort); + void setup_vgmstream(abort_callback& p_abort); void load_settings(); - void get_subsong_info(t_uint32 p_subsong, pfc::string_base & title, int *length_in_ms, int *total_samples, int *loop_flag, int *loop_start, int *loop_end, int *sample_rate, int *channels, int *bitrate, pfc::string_base & description, abort_callback & p_abort); - bool get_description_tag(pfc::string_base & temp, pfc::string_base const& description, const char *tag, char delimiter = '\n'); - void apply_config(VGMSTREAM * vgmstream); + void get_subsong_info(t_uint32 p_subsong, pfc::string_base& title, int* length_in_ms, int* total_samples, int* loop_flag, int *loop_start, int* loop_end, int* sample_rate, int* channels, int* bitrate, pfc::string_base& description, abort_callback& p_abort); + bool get_description_tag(pfc::string_base& temp, pfc::string_base const& description, const char* tag, char delimiter = '\n'); + void apply_config(VGMSTREAM* vgmstream); - static void g_load_cfg(int *accept_unknown, int *accept_common); + static void g_load_cfg(int* accept_unknown, int* accept_common); }; /* foo_streamfile.cpp */ -STREAMFILE * open_foo_streamfile(const char * const filename, abort_callback * p_abort, t_filestats * stats); +STREAMFILE* open_foo_streamfile(const char* const filename, abort_callback* p_abort, t_filestats* stats); #endif /*_FOO_VGMSTREAM_*/ diff --git a/src/streamfile.c b/src/streamfile.c index 3bdbc434..54399eed 100644 --- a/src/streamfile.c +++ b/src/streamfile.c @@ -1,6 +1,3 @@ -#ifndef _MSC_VER -#include -#endif #include "streamfile.h" #include "util.h" #include "vgmstream.h" @@ -10,9 +7,6 @@ #if defined(__MSVCRT__) || defined(_MSC_VER) #include - #define fseek_v _fseeki64 //fseek - #define ftell_v _ftelli64 //ftell - /* #ifndef fseeko #define fseeko fseek @@ -21,6 +15,8 @@ #define ftello ftell #endif */ + #define fseek_v _fseeki64 //fseek/fseeko + #define ftell_v _ftelli64 //ftell/ftello #ifdef fileno #undef fileno @@ -44,51 +40,51 @@ /* a STREAMFILE that operates via standard IO using a buffer */ typedef struct { - STREAMFILE sf; /* callbacks */ + STREAMFILE vt; /* callbacks */ - FILE * infile; /* actual FILE */ + FILE* infile; /* actual FILE */ char name[PATH_LIMIT]; /* FILE filename */ - offv_t offset; /* last read offset (info) */ - offv_t buffer_offset; /* current buffer data start */ - uint8_t* buffer; /* data buffer */ - size_t buffersize; /* max buffer size */ - size_t validsize; /* current buffer size */ - size_t filesize; /* buffered file size */ + 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 */ } STDIO_STREAMFILE; -static STREAMFILE* open_stdio_streamfile_buffer(const char * const filename, size_t buffersize); -static STREAMFILE* open_stdio_streamfile_buffer_by_file(FILE *infile, const char * const filename, size_t buffersize); +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); -static size_t read_stdio(STDIO_STREAMFILE *streamfile, uint8_t *dst, offv_t offset, size_t length) { - size_t length_read_total = 0; +static size_t stdio_read(STDIO_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) { + size_t read_total = 0; - if (!streamfile->infile || !dst || length <= 0 || offset < 0) + if (!sf->infile || !dst || length <= 0 || offset < 0) return 0; - //;VGM_LOG("STDIO: read %lx + %x (buf %lx + %x)\n", offset, length, streamfile->buffer_offset, streamfile->validsize); + //;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? */ - if (offset >= streamfile->buffer_offset && offset < streamfile->buffer_offset + streamfile->validsize) { - size_t length_to_read; - int offset_into_buffer = (offset - streamfile->buffer_offset); + if (offset >= sf->buf_offset && offset < sf->buf_offset + sf->valid_size) { + size_t buf_limit; + int buf_into = (int)(offset - sf->buf_offset); - length_to_read = streamfile->validsize - offset_into_buffer; - if (length_to_read > length) - length_to_read = length; + buf_limit = sf->valid_size - buf_into; + if (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), streamfile->buffer_offset, streamfile->validsize); + //;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, streamfile->buffer + offset_into_buffer, length_to_read); - length_read_total += length_to_read; - length -= length_to_read; - offset += length_to_read; - dst += length_to_read; + memcpy(dst, sf->buf + buf_into, buf_limit); + read_total += buf_limit; + length -= buf_limit; + offset += buf_limit; + dst += buf_limit; } #ifdef VGM_DEBUG_OUTPUT - if (offset < streamfile->buffer_offset && length > 0) { - VGM_LOG("STDIO: rebuffer, requested %lx vs %lx (sf %x)\n", offset, streamfile->buffer_offset, (uint32_t)streamfile); - //streamfile->rebuffer++; + 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); + //sf->rebuffer++; //if (rebuffer > N) ... } #endif @@ -98,14 +94,14 @@ static size_t read_stdio(STDIO_STREAMFILE *streamfile, uint8_t *dst, offv_t offs size_t length_to_read; /* ignore requests at EOF */ - if (offset >= streamfile->filesize) { - //offset = streamfile->filesize; /* seems fseek doesn't clamp offset */ - VGM_ASSERT_ONCE(offset > streamfile->filesize, "STDIO: reading over filesize 0x%x @ 0x%x + 0x%x\n", streamfile->filesize, (uint32_t)offset, length); + 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); break; } /* position to new offset */ - if (fseek_v(streamfile->infile, offset, SEEK_SET)) { + if (fseek_v(sf->infile, offset, SEEK_SET)) { break; /* this shouldn't happen in our code */ } @@ -114,58 +110,58 @@ static size_t read_stdio(STDIO_STREAMFILE *streamfile, uint8_t *dst, offv_t offs * This bug is deterministic and seemingly appears randomly after seeking. * It results in fread returning data from the wrong area of the file. * HPS is one format that is almost always affected by this. - * May be related/same as open_stdio's fixed bug when using dup(), try disabling */ - fseek_v(streamfile->infile, ftell_v(streamfile->infile), SEEK_SET); + * 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); #endif - /* fill the buffer (offset now is beyond buffer_offset) */ - streamfile->buffer_offset = offset; - streamfile->validsize = fread(streamfile->buffer, sizeof(uint8_t), streamfile->buffersize, streamfile->infile); - //;VGM_LOG("STDIO: read buf %lx + %x\n", streamfile->buffer_offset, streamfile->validsize); + /* 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); + //;VGM_LOG("STDIO: read buf %lx + %x\n", sf->buf_offset, sf->valid_size); /* decide how much must be read this time */ - if (length > streamfile->buffersize) - length_to_read = streamfile->buffersize; + if (length > sf->buf_size) + length_to_read = sf->buf_size; else length_to_read = length; /* give up on partial reads (EOF) */ - if (streamfile->validsize < length_to_read) { - memcpy(dst, streamfile->buffer, streamfile->validsize); - offset += streamfile->validsize; - length_read_total += streamfile->validsize; + if (sf->valid_size < length_to_read) { + memcpy(dst, sf->buf, sf->valid_size); + offset += sf->valid_size; + read_total += sf->valid_size; break; } /* use the new buffer */ - memcpy(dst, streamfile->buffer, length_to_read); + memcpy(dst, sf->buf, length_to_read); offset += length_to_read; - length_read_total += length_to_read; + read_total += length_to_read; length -= length_to_read; dst += length_to_read; } - streamfile->offset = offset; /* last fread offset */ - return length_read_total; + sf->offset = offset; /* last fread offset */ + return read_total; } -static size_t get_size_stdio(STDIO_STREAMFILE *streamfile) { - return streamfile->filesize; +static size_t stdio_get_size(STDIO_STREAMFILE* sf) { + return sf->file_size; } -static offv_t get_offset_stdio(STDIO_STREAMFILE *streamfile) { - return streamfile->offset; +static offv_t stdio_get_offset(STDIO_STREAMFILE* sf) { + return sf->offset; } -static void get_name_stdio(STDIO_STREAMFILE *streamfile, char *buffer, size_t length) { - strncpy(buffer, streamfile->name, length); - buffer[length-1]='\0'; +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 close_stdio(STDIO_STREAMFILE *streamfile) { - if (streamfile->infile) - fclose(streamfile->infile); - free(streamfile->buffer); - free(streamfile); +static void stdio_close(STDIO_STREAMFILE* sf) { + if (sf->infile) + fclose(sf->infile); + free(sf->buf); + free(sf); } -static STREAMFILE* open_stdio(STDIO_STREAMFILE *streamfile, const char * const filename, size_t buffersize) { +static STREAMFILE* stdio_open(STDIO_STREAMFILE* sf, const char* const filename, size_t buf_size) { if (!filename) return NULL; @@ -176,12 +172,12 @@ static STREAMFILE* open_stdio(STDIO_STREAMFILE *streamfile, const char * const f * this reportedly this causes issues in Android too */ /* if same name, duplicate the file descriptor we already have open */ - if (streamfile->infile && !strcmp(streamfile->name,filename)) { + if (sf->infile && !strcmp(sf->name,filename)) { int new_fd; FILE *new_file = NULL; - if (((new_fd = dup(fileno(streamfile->infile))) >= 0) && (new_file = fdopen(new_fd, "rb"))) { - STREAMFILE *new_sf = open_stdio_streamfile_buffer_by_file(new_file, filename, buffersize); + 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); if (new_sf) return new_sf; fclose(new_file); @@ -193,61 +189,61 @@ static STREAMFILE* open_stdio(STDIO_STREAMFILE *streamfile, const char * const f } #endif // a normal open, open a new file - return open_stdio_streamfile_buffer(filename, buffersize); + return open_stdio_streamfile_buffer(filename, buf_size); } -static STREAMFILE* open_stdio_streamfile_buffer_by_file(FILE *infile, const char * const filename, size_t buffersize) { - uint8_t *buffer = NULL; - STDIO_STREAMFILE *streamfile = NULL; +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; - buffer = calloc(buffersize,1); - if (!buffer) goto fail; + buf = calloc(buf_size, sizeof(uint8_t)); + if (!buf) goto fail; - streamfile = calloc(1,sizeof(STDIO_STREAMFILE)); - if (!streamfile) goto fail; + this_sf = calloc(1, sizeof(STDIO_STREAMFILE)); + if (!this_sf) goto fail; - streamfile->sf.read = (void*)read_stdio; - streamfile->sf.get_size = (void*)get_size_stdio; - streamfile->sf.get_offset = (void*)get_offset_stdio; - streamfile->sf.get_name = (void*)get_name_stdio; - streamfile->sf.open = (void*)open_stdio; - streamfile->sf.close = (void*)close_stdio; + 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; - streamfile->infile = infile; - streamfile->buffersize = buffersize; - streamfile->buffer = buffer; + this_sf->infile = infile; + this_sf->buf_size = buf_size; + this_sf->buf = buf; - strncpy(streamfile->name, filename, sizeof(streamfile->name)); - streamfile->name[sizeof(streamfile->name)-1] = '\0'; + strncpy(this_sf->name, filename, sizeof(this_sf->name)); + this_sf->name[sizeof(this_sf->name)-1] = '\0'; - /* cache filesize */ + /* cache file_size */ if (infile) { - fseek_v(streamfile->infile, 0x00, SEEK_END); - streamfile->filesize = ftell_v(streamfile->infile); - fseek_v(streamfile->infile, 0x00, SEEK_SET); + 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); } else { - streamfile->filesize = 0; /* allow virtual, non-existing files */ + this_sf->file_size = 0; /* allow virtual, non-existing files */ } /* Typically fseek(o)/ftell(o) may only handle up to ~2.14GB, signed 32b = 0x7FFFFFFF * (happens in banks like FSB, though rarely). Should work if configured properly, log otherwise. */ - if (streamfile->filesize == 0xFFFFFFFF) { /* -1 on error */ + if (this_sf->file_size == 0xFFFFFFFF) { /* -1 on error */ vgm_logi("STREAMFILE: file size too big (report)\n"); goto fail; /* can be ignored but may result in strange/unexpected behaviors */ } - return &streamfile->sf; + return &this_sf->vt; fail: - free(buffer); - free(streamfile); + free(buf); + free(this_sf); return NULL; } -static STREAMFILE* open_stdio_streamfile_buffer(const char * const filename, size_t bufsize) { - FILE *infile = NULL; - STREAMFILE *streamfile = NULL; +static STREAMFILE* open_stdio_streamfile_buffer(const char* const filename, size_t bufsize) { + FILE* infile = NULL; + STREAMFILE* sf = NULL; infile = fopen(filename,"rb"); if (!infile) { @@ -256,163 +252,165 @@ static STREAMFILE* open_stdio_streamfile_buffer(const char * const filename, siz return NULL; } - streamfile = open_stdio_streamfile_buffer_by_file(infile, filename, bufsize); - if (!streamfile) { + sf = open_stdio_streamfile_buffer_by_file(infile, filename, bufsize); + if (!sf) { if (infile) fclose(infile); } - return streamfile; + return sf; } -STREAMFILE* open_stdio_streamfile(const char *filename) { +STREAMFILE* open_stdio_streamfile(const char* filename) { return open_stdio_streamfile_buffer(filename, STREAMFILE_DEFAULT_BUFFER_SIZE); } -STREAMFILE* open_stdio_streamfile_by_file(FILE *file, const char *filename) { +STREAMFILE* open_stdio_streamfile_by_file(FILE* file, const char* filename) { return open_stdio_streamfile_buffer_by_file(file, filename, STREAMFILE_DEFAULT_BUFFER_SIZE); } /* **************************************************** */ typedef struct { - STREAMFILE sf; + STREAMFILE vt; - STREAMFILE *inner_sf; - offv_t offset; /* last read offset (info) */ - offv_t buffer_offset; /* current buffer data start */ - uint8_t* buffer; /* data buffer */ - size_t buffersize; /* max buffer size */ - size_t validsize; /* current buffer size */ - size_t filesize; /* buffered file size */ + 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 */ } BUFFER_STREAMFILE; -static size_t buffer_read(BUFFER_STREAMFILE *streamfile, uint8_t *dst, offv_t offset, size_t length) { - size_t length_read_total = 0; +static size_t buffer_read(BUFFER_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) { + size_t read_total = 0; if (!dst || length <= 0 || offset < 0) return 0; /* is the part of the requested length in the buffer? */ - if (offset >= streamfile->buffer_offset && offset < streamfile->buffer_offset + streamfile->validsize) { - size_t length_to_read; - int offset_into_buffer = (offset - streamfile->buffer_offset); + if (offset >= sf->buf_offset && offset < sf->buf_offset + sf->valid_size) { + size_t buf_limit; + int buf_into = (int)(offset - sf->buf_offset); - length_to_read = streamfile->validsize - offset_into_buffer; - if (length_to_read > length) - length_to_read = length; + buf_limit = sf->valid_size - buf_into; + if (buf_limit > length) + buf_limit = length; - memcpy(dst, streamfile->buffer + offset_into_buffer, length_to_read); - length_read_total += length_to_read; - length -= length_to_read; - offset += length_to_read; - dst += length_to_read; + memcpy(dst, sf->buf + buf_into, buf_limit); + read_total += buf_limit; + length -= buf_limit; + offset += buf_limit; + dst += buf_limit; } #ifdef VGM_DEBUG_OUTPUT - if (offset < streamfile->buffer_offset) { - VGM_LOG("BUFFER: rebuffer, requested %lx vs %lx (sf %x)\n", offset, streamfile->buffer_offset, (uint32_t)streamfile); + if (offset < sf->buf_offset) { + VGM_LOG("BUFFER: rebuffer, requested %lx vs %lx (sf %x)\n", offset, sf->buf_offset, (uint32_t)sf); } #endif /* read the rest of the requested length */ while (length > 0) { - size_t length_to_read; + size_t buf_limit; /* ignore requests at EOF */ - if (offset >= streamfile->filesize) { - //offset = streamfile->filesize; /* seems fseek doesn't clamp offset */ - VGM_ASSERT_ONCE(offset > streamfile->filesize, "BUFFER: reading over filesize 0x%x @ 0x%x + 0x%x\n", streamfile->filesize, (uint32_t)offset, length); + if (offset >= sf->file_size) { + //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); break; } - /* fill the buffer (offset now is beyond buffer_offset) */ - streamfile->buffer_offset = offset; - streamfile->validsize = streamfile->inner_sf->read(streamfile->inner_sf, streamfile->buffer, streamfile->buffer_offset, streamfile->buffersize); + /* 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); /* decide how much must be read this time */ - if (length > streamfile->buffersize) - length_to_read = streamfile->buffersize; + if (length > sf->buf_size) + buf_limit = sf->buf_size; else - length_to_read = length; + buf_limit = length; /* give up on partial reads (EOF) */ - if (streamfile->validsize < length_to_read) { - memcpy(dst, streamfile->buffer, streamfile->validsize); - offset += streamfile->validsize; - length_read_total += streamfile->validsize; + if (sf->valid_size < buf_limit) { + memcpy(dst, sf->buf, sf->valid_size); + offset += sf->valid_size; + read_total += sf->valid_size; break; } /* use the new buffer */ - memcpy(dst, streamfile->buffer, length_to_read); - offset += length_to_read; - length_read_total += length_to_read; - length -= length_to_read; - dst += length_to_read; + memcpy(dst, sf->buf, buf_limit); + offset += buf_limit; + read_total += buf_limit; + length -= buf_limit; + dst += buf_limit; } - streamfile->offset = offset; /* last fread offset */ - return length_read_total; + sf->offset = offset; /* last fread offset */ + return read_total; } -static size_t buffer_get_size(BUFFER_STREAMFILE *streamfile) { - return streamfile->filesize; /* cache */ +static size_t buffer_get_size(BUFFER_STREAMFILE* sf) { + return sf->file_size; /* cache */ } -static offv_t buffer_get_offset(BUFFER_STREAMFILE *streamfile) { - return streamfile->offset; /* cache */ +static offv_t buffer_get_offset(BUFFER_STREAMFILE* sf) { + return sf->offset; /* cache */ } -static void buffer_get_name(BUFFER_STREAMFILE *streamfile, char *buffer, size_t length) { - streamfile->inner_sf->get_name(streamfile->inner_sf, buffer, length); /* default */ +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 *streamfile, const char * const filename, size_t buffersize) { - STREAMFILE *new_inner_sf = streamfile->inner_sf->open(streamfile->inner_sf,filename,buffersize); - return open_buffer_streamfile(new_inner_sf, buffersize); /* original buffer size is preferable? */ +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 *streamfile) { - streamfile->inner_sf->close(streamfile->inner_sf); - free(streamfile->buffer); - free(streamfile); +static void buffer_close(BUFFER_STREAMFILE* sf) { + sf->inner_sf->close(sf->inner_sf); + free(sf->buf); + free(sf); } -STREAMFILE* open_buffer_streamfile(STREAMFILE *streamfile, size_t buffer_size) { - BUFFER_STREAMFILE *this_sf = NULL; +STREAMFILE* open_buffer_streamfile(STREAMFILE* sf, size_t buf_size) { + uint8_t* buf = NULL; + BUFFER_STREAMFILE* this_sf = NULL; - if (!streamfile) goto fail; + if (!sf) goto fail; + + if (buf_size == 0) + buf_size = STREAMFILE_DEFAULT_BUFFER_SIZE; + + buf = calloc(buf_size, sizeof(uint8_t)); + if (!buf) goto fail; this_sf = calloc(1, sizeof(BUFFER_STREAMFILE)); if (!this_sf) goto fail; - this_sf->buffersize = buffer_size; - if (this_sf->buffersize == 0) - this_sf->buffersize = STREAMFILE_DEFAULT_BUFFER_SIZE; - - this_sf->buffer = calloc(this_sf->buffersize,1); - if (!this_sf->buffer) goto fail; - /* set callbacks and internals */ - this_sf->sf.read = (void*)buffer_read; - this_sf->sf.get_size = (void*)buffer_get_size; - this_sf->sf.get_offset = (void*)buffer_get_offset; - this_sf->sf.get_name = (void*)buffer_get_name; - this_sf->sf.open = (void*)buffer_open; - this_sf->sf.close = (void*)buffer_close; - this_sf->sf.stream_index = streamfile->stream_index; + 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; - this_sf->inner_sf = streamfile; + this_sf->inner_sf = sf; + this_sf->buf_size = buf_size; + this_sf->buf = buf; - this_sf->filesize = streamfile->get_size(streamfile); + this_sf->file_size = sf->get_size(sf); - return &this_sf->sf; + return &this_sf->vt; fail: - if (this_sf) free(this_sf->buffer); + if (this_sf) free(this_sf->buf); free(this_sf); return NULL; } -STREAMFILE* open_buffer_streamfile_f(STREAMFILE *streamfile, size_t buffer_size) { - STREAMFILE *new_sf = open_buffer_streamfile(streamfile, buffer_size); +STREAMFILE* open_buffer_streamfile_f(STREAMFILE* sf, size_t buffer_size) { + STREAMFILE* new_sf = open_buffer_streamfile(sf, buffer_size); if (!new_sf) - close_streamfile(streamfile); + close_streamfile(sf); return new_sf; } @@ -423,201 +421,201 @@ STREAMFILE* open_buffer_streamfile_f(STREAMFILE *streamfile, size_t buffer_size) //todo use safe string ops, this ain't easy typedef struct { - STREAMFILE sf; + STREAMFILE vt; - STREAMFILE *inner_sf; + STREAMFILE* inner_sf; } WRAP_STREAMFILE; -static size_t wrap_read(WRAP_STREAMFILE *streamfile, uint8_t *dst, offv_t offset, size_t length) { - return streamfile->inner_sf->read(streamfile->inner_sf, dst, offset, length); /* default */ +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 */ } -static size_t wrap_get_size(WRAP_STREAMFILE *streamfile) { - return streamfile->inner_sf->get_size(streamfile->inner_sf); /* default */ +static size_t wrap_get_size(WRAP_STREAMFILE* sf) { + return sf->inner_sf->get_size(sf->inner_sf); /* default */ } -static offv_t wrap_get_offset(WRAP_STREAMFILE *streamfile) { - return streamfile->inner_sf->get_offset(streamfile->inner_sf); /* default */ +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 *streamfile, char *buffer, size_t length) { - streamfile->inner_sf->get_name(streamfile->inner_sf, buffer, length); /* 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_open(WRAP_STREAMFILE *streamfile, const char * const filename, size_t buffersize) { - streamfile->inner_sf->open(streamfile->inner_sf, filename, buffersize); /* default (don't wrap) */ +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 void wrap_close(WRAP_STREAMFILE *streamfile) { - //streamfile->inner_sf->close(streamfile->inner_sf); /* don't close */ - free(streamfile); +static void wrap_close(WRAP_STREAMFILE* sf) { + //sf->inner_sf->close(sf->inner_sf); /* don't close */ + free(sf); } -STREAMFILE* open_wrap_streamfile(STREAMFILE *streamfile) { - WRAP_STREAMFILE *this_sf = NULL; +STREAMFILE* open_wrap_streamfile(STREAMFILE* sf) { + WRAP_STREAMFILE* this_sf = NULL; - if (!streamfile) return NULL; + if (!sf) return NULL; - this_sf = calloc(1,sizeof(WRAP_STREAMFILE)); + this_sf = calloc(1, sizeof(WRAP_STREAMFILE)); if (!this_sf) return NULL; /* set callbacks and internals */ - this_sf->sf.read = (void*)wrap_read; - this_sf->sf.get_size = (void*)wrap_get_size; - this_sf->sf.get_offset = (void*)wrap_get_offset; - this_sf->sf.get_name = (void*)wrap_get_name; - this_sf->sf.open = (void*)wrap_open; - this_sf->sf.close = (void*)wrap_close; - this_sf->sf.stream_index = streamfile->stream_index; + 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; - this_sf->inner_sf = streamfile; + this_sf->inner_sf = sf; - return &this_sf->sf; + return &this_sf->vt; } -STREAMFILE* open_wrap_streamfile_f(STREAMFILE* streamfile) { - STREAMFILE* new_sf = open_wrap_streamfile(streamfile); +STREAMFILE* open_wrap_streamfile_f(STREAMFILE* sf) { + STREAMFILE* new_sf = open_wrap_streamfile(sf); if (!new_sf) - close_streamfile(streamfile); + close_streamfile(sf); return new_sf; } /* **************************************************** */ typedef struct { - STREAMFILE sf; + STREAMFILE vt; STREAMFILE* inner_sf; offv_t start; size_t size; } CLAMP_STREAMFILE; -static size_t clamp_read(CLAMP_STREAMFILE* streamfile, uint8_t* dst, offv_t offset, size_t length) { - offv_t inner_offset = streamfile->start + offset; +static size_t clamp_read(CLAMP_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) { + offv_t inner_offset = sf->start + offset; size_t clamp_length = length; - if (offset + length > streamfile->size) { - if (offset >= streamfile->size) + if (offset + length > sf->size) { + if (offset >= sf->size) clamp_length = 0; else - clamp_length = streamfile->size - offset; + clamp_length = sf->size - offset; } - return streamfile->inner_sf->read(streamfile->inner_sf, dst, inner_offset, clamp_length); + return sf->inner_sf->read(sf->inner_sf, dst, inner_offset, clamp_length); } -static size_t clamp_get_size(CLAMP_STREAMFILE* streamfile) { - return streamfile->size; +static size_t clamp_get_size(CLAMP_STREAMFILE* sf) { + return sf->size; } -static offv_t clamp_get_offset(CLAMP_STREAMFILE* streamfile) { - return streamfile->inner_sf->get_offset(streamfile->inner_sf) - streamfile->start; +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* streamfile, char* buffer, size_t length) { - streamfile->inner_sf->get_name(streamfile->inner_sf, buffer, length); /* default */ +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 STREAMFILE* clamp_open(CLAMP_STREAMFILE* streamfile, const char* const filename, size_t buffersize) { +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; - new_inner_sf = streamfile->inner_sf->open(streamfile->inner_sf,filename,buffersize); - streamfile->inner_sf->get_name(streamfile->inner_sf, original_filename, PATH_LIMIT); + 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); /* detect re-opening the file */ if (strcmp(filename, original_filename) == 0) { - return open_clamp_streamfile(new_inner_sf, streamfile->start, streamfile->size); /* clamp again */ + return open_clamp_streamfile(new_inner_sf, sf->start, sf->size); /* clamp again */ } else { return new_inner_sf; } } -static void clamp_close(CLAMP_STREAMFILE* streamfile) { - streamfile->inner_sf->close(streamfile->inner_sf); - free(streamfile); +static void clamp_close(CLAMP_STREAMFILE* sf) { + sf->inner_sf->close(sf->inner_sf); + free(sf); } -STREAMFILE* open_clamp_streamfile(STREAMFILE* streamfile, offv_t start, size_t size) { +STREAMFILE* open_clamp_streamfile(STREAMFILE* sf, offv_t start, size_t size) { CLAMP_STREAMFILE* this_sf = NULL; - if (!streamfile || size == 0) return NULL; - if (start + size > get_streamfile_size(streamfile)) return NULL; + if (!sf || size == 0) return NULL; + if (start + size > get_streamfile_size(sf)) return NULL; - this_sf = calloc(1,sizeof(CLAMP_STREAMFILE)); + this_sf = calloc(1, sizeof(CLAMP_STREAMFILE)); if (!this_sf) return NULL; /* set callbacks and internals */ - this_sf->sf.read = (void*)clamp_read; - this_sf->sf.get_size = (void*)clamp_get_size; - this_sf->sf.get_offset = (void*)clamp_get_offset; - this_sf->sf.get_name = (void*)clamp_get_name; - this_sf->sf.open = (void*)clamp_open; - this_sf->sf.close = (void*)clamp_close; - this_sf->sf.stream_index = streamfile->stream_index; + 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 = streamfile; + this_sf->inner_sf = sf; this_sf->start = start; this_sf->size = size; - return &this_sf->sf; + return &this_sf->vt; } -STREAMFILE* open_clamp_streamfile_f(STREAMFILE* streamfile, offv_t start, size_t size) { - STREAMFILE* new_sf = open_clamp_streamfile(streamfile, start, size); +STREAMFILE* open_clamp_streamfile_f(STREAMFILE* sf, offv_t start, size_t size) { + STREAMFILE* new_sf = open_clamp_streamfile(sf, start, size); if (!new_sf) - close_streamfile(streamfile); + close_streamfile(sf); return new_sf; } /* **************************************************** */ typedef struct { - STREAMFILE sf; + STREAMFILE vt; - STREAMFILE *inner_sf; + STREAMFILE* inner_sf; void* data; /* state for custom reads, malloc'ed + copied on open (to re-open streamfiles cleanly) */ 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 (*size_callback)(STREAMFILE *, void*); /* size when custom reads make data smaller/bigger than underlying streamfile */ + size_t (*read_callback)(STREAMFILE*, uint8_t*, offv_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 */ 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 */ } IO_STREAMFILE; -static size_t io_read(IO_STREAMFILE *streamfile, uint8_t *dst, offv_t offset, size_t length) { - return streamfile->read_callback(streamfile->inner_sf, dst, offset, length, streamfile->data); +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); } -static size_t io_get_size(IO_STREAMFILE *streamfile) { - if (streamfile->size_callback) - return streamfile->size_callback(streamfile->inner_sf, streamfile->data); +static size_t io_get_size(IO_STREAMFILE* sf) { + if (sf->size_callback) + return sf->size_callback(sf->inner_sf, sf->data); else - return streamfile->inner_sf->get_size(streamfile->inner_sf); /* default */ + return sf->inner_sf->get_size(sf->inner_sf); /* default */ } -static offv_t io_get_offset(IO_STREAMFILE *streamfile) { - return streamfile->inner_sf->get_offset(streamfile->inner_sf); /* default */ +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 *streamfile, char *buffer, size_t length) { - streamfile->inner_sf->get_name(streamfile->inner_sf, buffer, length); /* 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 STREAMFILE* io_open(IO_STREAMFILE *streamfile, const char * const filename, size_t buffersize) { - STREAMFILE *new_inner_sf = streamfile->inner_sf->open(streamfile->inner_sf,filename,buffersize); - return open_io_streamfile_ex(new_inner_sf, streamfile->data, streamfile->data_size, streamfile->read_callback, streamfile->size_callback, streamfile->init_callback, streamfile->close_callback); +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 *streamfile) { - if (streamfile->close_callback) - streamfile->close_callback(streamfile->inner_sf, streamfile->data); - streamfile->inner_sf->close(streamfile->inner_sf); - free(streamfile->data); - free(streamfile); +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); } -STREAMFILE* open_io_streamfile_ex(STREAMFILE *streamfile, void *data, size_t data_size, void *read_callback, void *size_callback, void* init_callback, void* close_callback) { - IO_STREAMFILE *this_sf = NULL; +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; - if (!streamfile) goto fail; + if (!sf) goto fail; if ((data && !data_size) || (!data && data_size)) goto fail; - this_sf = calloc(1,sizeof(IO_STREAMFILE)); + this_sf = calloc(1, sizeof(IO_STREAMFILE)); if (!this_sf) goto fail; /* set callbacks and internals */ - this_sf->sf.read = (void*)io_read; - this_sf->sf.get_size = (void*)io_get_size; - this_sf->sf.get_offset = (void*)io_get_offset; - this_sf->sf.get_name = (void*)io_get_name; - this_sf->sf.open = (void*)io_open; - this_sf->sf.close = (void*)io_close; - this_sf->sf.stream_index = streamfile->stream_index; + 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 = streamfile; + this_sf->inner_sf = sf; if (data) { this_sf->data = malloc(data_size); if (!this_sf->data) goto fail; @@ -634,7 +632,7 @@ STREAMFILE* open_io_streamfile_ex(STREAMFILE *streamfile, void *data, size_t dat if (ok < 0) goto fail; } - return &this_sf->sf; + return &this_sf->vt; fail: if (this_sf) free(this_sf->data); @@ -642,89 +640,89 @@ fail: return NULL; } -STREAMFILE* open_io_streamfile_ex_f(STREAMFILE *streamfile, 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(streamfile, data, data_size, read_callback, size_callback, init_callback, close_callback); +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); if (!new_sf) - close_streamfile(streamfile); + close_streamfile(sf); return new_sf; } -STREAMFILE* open_io_streamfile(STREAMFILE *streamfile, void *data, size_t data_size, void *read_callback, void *size_callback) { - return open_io_streamfile_ex(streamfile, data, data_size, read_callback, size_callback, NULL, NULL); +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); } -STREAMFILE* open_io_streamfile_f(STREAMFILE *streamfile, void *data, size_t data_size, void *read_callback, void *size_callback) { - return open_io_streamfile_ex_f(streamfile, data, data_size, read_callback, size_callback, NULL, NULL); +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); } /* **************************************************** */ typedef struct { - STREAMFILE sf; + STREAMFILE vt; - STREAMFILE *inner_sf; + STREAMFILE* inner_sf; char fakename[PATH_LIMIT]; } FAKENAME_STREAMFILE; -static size_t fakename_read(FAKENAME_STREAMFILE *streamfile, uint8_t *dst, offv_t offset, size_t length) { - return streamfile->inner_sf->read(streamfile->inner_sf, dst, offset, length); /* default */ +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 */ } -static size_t fakename_get_size(FAKENAME_STREAMFILE *streamfile) { - return streamfile->inner_sf->get_size(streamfile->inner_sf); /* default */ +static size_t fakename_get_size(FAKENAME_STREAMFILE* sf) { + return sf->inner_sf->get_size(sf->inner_sf); /* default */ } -static offv_t fakename_get_offset(FAKENAME_STREAMFILE *streamfile) { - return streamfile->inner_sf->get_offset(streamfile->inner_sf); /* default */ +static offv_t fakename_get_offset(FAKENAME_STREAMFILE* sf) { + return sf->inner_sf->get_offset(sf->inner_sf); /* default */ } -static void fakename_get_name(FAKENAME_STREAMFILE *streamfile, char *buffer, size_t length) { - strncpy(buffer,streamfile->fakename,length); - buffer[length-1]='\0'; +static void fakename_get_name(FAKENAME_STREAMFILE* sf, char* name, size_t name_size) { + strncpy(name,sf->fakename, name_size); + name[name_size - 1] = '\0'; } -static STREAMFILE* fakename_open(FAKENAME_STREAMFILE *streamfile, const char * const filename, size_t buffersize) { +static STREAMFILE* fakename_open(FAKENAME_STREAMFILE* sf, const char* const filename, size_t buf_size) { /* detect re-opening the file */ - if (strcmp(filename, streamfile->fakename) == 0) { - STREAMFILE *new_inner_sf; + if (strcmp(filename, sf->fakename) == 0) { + STREAMFILE* new_inner_sf; char original_filename[PATH_LIMIT]; - streamfile->inner_sf->get_name(streamfile->inner_sf, original_filename, PATH_LIMIT); - new_inner_sf = streamfile->inner_sf->open(streamfile->inner_sf, original_filename, buffersize); - return open_fakename_streamfile(new_inner_sf, streamfile->fakename, NULL); + 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); } else { - return streamfile->inner_sf->open(streamfile->inner_sf, filename, buffersize); + return sf->inner_sf->open(sf->inner_sf, filename, buf_size); } } -static void fakename_close(FAKENAME_STREAMFILE *streamfile) { - streamfile->inner_sf->close(streamfile->inner_sf); - free(streamfile); +static void fakename_close(FAKENAME_STREAMFILE* sf) { + sf->inner_sf->close(sf->inner_sf); + free(sf); } -STREAMFILE* open_fakename_streamfile(STREAMFILE *streamfile, const char *fakename, const char *fakeext) { - FAKENAME_STREAMFILE *this_sf = NULL; +STREAMFILE* open_fakename_streamfile(STREAMFILE* sf, const char* fakename, const char* fakeext) { + FAKENAME_STREAMFILE* this_sf = NULL; - if (!streamfile || (!fakename && !fakeext)) return NULL; + if (!sf || (!fakename && !fakeext)) return NULL; - this_sf = calloc(1,sizeof(FAKENAME_STREAMFILE)); + this_sf = calloc(1, sizeof(FAKENAME_STREAMFILE)); if (!this_sf) return NULL; /* set callbacks and internals */ - this_sf->sf.read = (void*)fakename_read; - this_sf->sf.get_size = (void*)fakename_get_size; - this_sf->sf.get_offset = (void*)fakename_get_offset; - this_sf->sf.get_name = (void*)fakename_get_name; - this_sf->sf.open = (void*)fakename_open; - this_sf->sf.close = (void*)fakename_close; - this_sf->sf.stream_index = streamfile->stream_index; + 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; - this_sf->inner_sf = streamfile; + this_sf->inner_sf = sf; /* copy passed name or retain current, and swap extension if expected */ if (fakename) { strcpy(this_sf->fakename,fakename); } else { - streamfile->get_name(streamfile, this_sf->fakename, PATH_LIMIT); + sf->get_name(sf, this_sf->fakename, PATH_LIMIT); } if (fakeext) { - char* ext = strrchr(this_sf->fakename,'.'); + char* ext = strrchr(this_sf->fakename, '.'); if (ext != NULL) { ext[1] = '\0'; /* truncate past dot */ } else { @@ -733,40 +731,40 @@ STREAMFILE* open_fakename_streamfile(STREAMFILE *streamfile, const char *fakenam strcat(this_sf->fakename, fakeext); } - return &this_sf->sf; + return &this_sf->vt; } -STREAMFILE* open_fakename_streamfile_f(STREAMFILE *streamfile, const char *fakename, const char *fakeext) { - STREAMFILE *new_sf = open_fakename_streamfile(streamfile, fakename, fakeext); +STREAMFILE* open_fakename_streamfile_f(STREAMFILE* sf, const char* fakename, const char* fakeext) { + STREAMFILE* new_sf = open_fakename_streamfile(sf, fakename, fakeext); if (!new_sf) - close_streamfile(streamfile); + close_streamfile(sf); return new_sf; } /* **************************************************** */ typedef struct { - STREAMFILE sf; + STREAMFILE vt; - STREAMFILE **inner_sfs; + STREAMFILE** inner_sfs; size_t inner_sfs_size; size_t *sizes; offv_t size; offv_t offset; } MULTIFILE_STREAMFILE; -static size_t multifile_read(MULTIFILE_STREAMFILE *streamfile, uint8_t *dst, offv_t offset, size_t length) { +static size_t multifile_read(MULTIFILE_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) { int i, segment = 0; offv_t segment_offset = 0; size_t done = 0; - if (offset > streamfile->size) { - streamfile->offset = streamfile->size; + if (offset > sf->size) { + sf->offset = sf->size; return 0; } /* map external offset to multifile offset */ - for (i = 0; i < streamfile->inner_sfs_size; i++) { - size_t segment_size = streamfile->sizes[i]; + for (i = 0; i < sf->inner_sfs_size; i++) { + size_t segment_size = sf->sizes[i]; /* check if offset falls in this segment */ if (offset >= segment_offset && offset < segment_offset + segment_size) { segment = i; @@ -779,110 +777,110 @@ static size_t multifile_read(MULTIFILE_STREAMFILE *streamfile, uint8_t *dst, off /* reads can span multiple segments */ while(done < length) { - if (segment >= streamfile->inner_sfs_size) /* over last segment, not fully done */ + if (segment >= sf->inner_sfs_size) /* over last segment, not fully done */ break; /* reads over segment size are ok, will return smaller value and continue next segment */ - done += streamfile->inner_sfs[segment]->read(streamfile->inner_sfs[segment], dst + done, segment_offset, length - done); + done += sf->inner_sfs[segment]->read(sf->inner_sfs[segment], dst + done, segment_offset, length - done); segment++; segment_offset = 0; } - streamfile->offset = offset + done; + sf->offset = offset + done; return done; } -static size_t multifile_get_size(MULTIFILE_STREAMFILE *streamfile) { - return streamfile->size; +static size_t multifile_get_size(MULTIFILE_STREAMFILE* sf) { + return sf->size; } -static offv_t multifile_get_offset(MULTIFILE_STREAMFILE * streamfile) { - return streamfile->offset; +static offv_t multifile_get_offset(MULTIFILE_STREAMFILE* sf) { + return sf->offset; } -static void multifile_get_name(MULTIFILE_STREAMFILE *streamfile, char *buffer, size_t length) { - streamfile->inner_sfs[0]->get_name(streamfile->inner_sfs[0], buffer, length); +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 *streamfile, const char * const filename, size_t buffersize) { +static STREAMFILE* multifile_open(MULTIFILE_STREAMFILE* sf, const char* const filename, size_t buf_size) { char original_filename[PATH_LIMIT]; - STREAMFILE *new_sf = NULL; - STREAMFILE **new_inner_sfs = NULL; + STREAMFILE* new_sf = NULL; + STREAMFILE** new_inner_sfs = NULL; int i; - streamfile->inner_sfs[0]->get_name(streamfile->inner_sfs[0], original_filename, PATH_LIMIT); + sf->inner_sfs[0]->get_name(sf->inner_sfs[0], original_filename, PATH_LIMIT); /* detect re-opening the file */ if (strcmp(filename, original_filename) == 0) { /* same multifile */ - new_inner_sfs = calloc(streamfile->inner_sfs_size, sizeof(STREAMFILE*)); + new_inner_sfs = calloc(sf->inner_sfs_size, sizeof(STREAMFILE*)); if (!new_inner_sfs) goto fail; - for (i = 0; i < streamfile->inner_sfs_size; i++) { - streamfile->inner_sfs[i]->get_name(streamfile->inner_sfs[i], original_filename, PATH_LIMIT); - new_inner_sfs[i] = streamfile->inner_sfs[i]->open(streamfile->inner_sfs[i], original_filename, buffersize); + 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); if (!new_inner_sfs[i]) goto fail; } - new_sf = open_multifile_streamfile(new_inner_sfs, streamfile->inner_sfs_size); + new_sf = open_multifile_streamfile(new_inner_sfs, sf->inner_sfs_size); if (!new_sf) goto fail; free(new_inner_sfs); return new_sf; } else { - return streamfile->inner_sfs[0]->open(streamfile->inner_sfs[0], filename, buffersize); /* regular file */ + return sf->inner_sfs[0]->open(sf->inner_sfs[0], filename, buf_size); /* regular file */ } fail: if (new_inner_sfs) { - for (i = 0; i < streamfile->inner_sfs_size; i++) + for (i = 0; i < sf->inner_sfs_size; i++) close_streamfile(new_inner_sfs[i]); } free(new_inner_sfs); return NULL; } -static void multifile_close(MULTIFILE_STREAMFILE *streamfile) { +static void multifile_close(MULTIFILE_STREAMFILE* sf) { int i; - for (i = 0; i < streamfile->inner_sfs_size; i++) { - for (i = 0; i < streamfile->inner_sfs_size; i++) { - close_streamfile(streamfile->inner_sfs[i]); + for (i = 0; i < sf->inner_sfs_size; i++) { + for (i = 0; i < sf->inner_sfs_size; i++) { + close_streamfile(sf->inner_sfs[i]); } } - free(streamfile->inner_sfs); - free(streamfile->sizes); - free(streamfile); + free(sf->inner_sfs); + free(sf->sizes); + free(sf); } -STREAMFILE* open_multifile_streamfile(STREAMFILE **streamfiles, size_t streamfiles_size) { - MULTIFILE_STREAMFILE *this_sf = NULL; +STREAMFILE* open_multifile_streamfile(STREAMFILE** sfs, size_t sfs_size) { + MULTIFILE_STREAMFILE* this_sf = NULL; int i; - if (!streamfiles || !streamfiles_size) return NULL; + if (!sfs || !sfs_size) return NULL; - for (i = 0; i < streamfiles_size; i++) { - if (!streamfiles[i]) return NULL; + for (i = 0; i < sfs_size; i++) { + if (!sfs[i]) return NULL; } - this_sf = calloc(1,sizeof(MULTIFILE_STREAMFILE)); + this_sf = calloc(1, sizeof(MULTIFILE_STREAMFILE)); if (!this_sf) goto fail; /* set callbacks and internals */ - this_sf->sf.read = (void*)multifile_read; - this_sf->sf.get_size = (void*)multifile_get_size; - this_sf->sf.get_offset = (void*)multifile_get_offset; - this_sf->sf.get_name = (void*)multifile_get_name; - this_sf->sf.open = (void*)multifile_open; - this_sf->sf.close = (void*)multifile_close; - this_sf->sf.stream_index = streamfiles[0]->stream_index; + 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 = streamfiles_size; - this_sf->inner_sfs = calloc(streamfiles_size, sizeof(STREAMFILE*)); + this_sf->inner_sfs_size = sfs_size; + this_sf->inner_sfs = calloc(sfs_size, sizeof(STREAMFILE*)); if (!this_sf->inner_sfs) goto fail; - this_sf->sizes = calloc(streamfiles_size, sizeof(size_t)); + this_sf->sizes = calloc(sfs_size, sizeof(size_t)); if (!this_sf->sizes) goto fail; for (i = 0; i < this_sf->inner_sfs_size; i++) { - this_sf->inner_sfs[i] = streamfiles[i]; - this_sf->sizes[i] = streamfiles[i]->get_size(streamfiles[i]); + this_sf->inner_sfs[i] = sfs[i]; + this_sf->sizes[i] = sfs[i]->get_size(sfs[i]); this_sf->size += this_sf->sizes[i]; } - return &this_sf->sf; + return &this_sf->vt; fail: if (this_sf) { @@ -892,12 +890,12 @@ fail: free(this_sf); return NULL; } -STREAMFILE* open_multifile_streamfile_f(STREAMFILE **streamfiles, size_t streamfiles_size) { - STREAMFILE *new_sf = open_multifile_streamfile(streamfiles, streamfiles_size); +STREAMFILE* open_multifile_streamfile_f(STREAMFILE** sfs, size_t sfs_size) { + STREAMFILE* new_sf = open_multifile_streamfile(sfs, sfs_size); if (!new_sf) { int i; - for (i = 0; i < streamfiles_size; i++) { - close_streamfile(streamfiles[i]); + for (i = 0; i < sfs_size; i++) { + close_streamfile(sfs[i]); } } return new_sf; @@ -961,7 +959,7 @@ STREAMFILE* open_streamfile_by_filename(STREAMFILE* sf, const char* filename) { name = partname + 2; /* ignore './' */ } else if (partname[0] == '.' && partname[1] == '.' && partname[2] == DIR_SEPARATOR) { /* '../name' */ - char *pathprev; + char* pathprev; path[0] = '\0'; /* remove last separator so next call works */ pathprev = strrchr(fullname,DIR_SEPARATOR); @@ -1277,7 +1275,7 @@ int check_extensions(STREAMFILE* sf, const char* cmp_exts) { const char* ststr_res = NULL; size_t ext_len, cmp_len; - sf->get_name(sf,filename,sizeof(filename)); + sf->get_name(sf, filename, sizeof(filename)); ext = filename_extension(filename); ext_len = strlen(ext); diff --git a/winamp/in_streamfile.c b/winamp/in_streamfile.c index 7ddd384a..33652c85 100644 --- a/winamp/in_streamfile.c +++ b/winamp/in_streamfile.c @@ -11,7 +11,7 @@ /* ************************************* */ /* opens a utf16 (unicode) path */ -static FILE* wa_fopen(const in_char *wpath) { +static FILE* wa_fopen(const in_char* wpath) { #ifdef UNICODE_INPUT_PLUGIN return _wfopen(wpath,L"rb"); #else @@ -34,16 +34,16 @@ static FILE* wa_fdopen(int fd) { /* a STREAMFILE that operates via STDIOSTREAMFILE but handles Winamp's unicode (in_char) paths */ typedef struct { - STREAMFILE sf; - STREAMFILE *stdiosf; - FILE *infile_ref; /* pointer to the infile in stdiosf (partially handled by stdiosf) */ + STREAMFILE vt; + STREAMFILE* stdiosf; + FILE* infile_ref; /* pointer to the infile in stdiosf (partially handled by stdiosf) */ } WINAMP_STREAMFILE; -static STREAMFILE *open_winamp_streamfile_by_file(FILE *infile, const char * path); -//static STREAMFILE *open_winamp_streamfile_by_ipath(const in_char *wpath); +static STREAMFILE* open_winamp_streamfile_by_file(FILE* infile, const char* path); +//static STREAMFILE* open_winamp_streamfile_by_ipath(const in_char* wpath); -static size_t wasf_read(WINAMP_STREAMFILE* sf, uint8_t* dest, offv_t offset, size_t length) { - return sf->stdiosf->read(sf->stdiosf, dest, offset, length); +static size_t wasf_read(WINAMP_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) { + return sf->stdiosf->read(sf->stdiosf, dst, offset, length); } static size_t wasf_get_size(WINAMP_STREAMFILE* sf) { @@ -58,7 +58,7 @@ static void wasf_get_name(WINAMP_STREAMFILE* sf, char* buffer, size_t length) { sf->stdiosf->get_name(sf->stdiosf, buffer, length); } -static STREAMFILE *wasf_open(WINAMP_STREAMFILE* sf, const char* const filename, size_t buffersize) { +static STREAMFILE* wasf_open(WINAMP_STREAMFILE* sf, const char* const filename, size_t buffersize) { in_char wpath[PATH_LIMIT]; if (!filename) @@ -77,7 +77,7 @@ static STREAMFILE *wasf_open(WINAMP_STREAMFILE* sf, const char* const filename, FILE *new_file; if (((new_fd = dup(fileno(sf->infile_ref))) >= 0) && (new_file = wa_fdopen(new_fd))) { - STREAMFILE *new_sf = open_winamp_streamfile_by_file(new_file, filename); + STREAMFILE* new_sf = open_winamp_streamfile_by_file(new_file, filename); if (new_sf) return new_sf; fclose(new_file); @@ -101,7 +101,7 @@ static void wasf_close(WINAMP_STREAMFILE* sf) { free(sf); /* and the current struct */ } -static STREAMFILE *open_winamp_streamfile_by_file(FILE* file, const char* path) { +static STREAMFILE* open_winamp_streamfile_by_file(FILE* file, const char* path) { WINAMP_STREAMFILE* this_sf = NULL; STREAMFILE* stdiosf = NULL; @@ -111,17 +111,17 @@ static STREAMFILE *open_winamp_streamfile_by_file(FILE* file, const char* path) stdiosf = open_stdio_streamfile_by_file(file, path); if (!stdiosf) goto fail; - this_sf->sf.read = (void*)wasf_read; - this_sf->sf.get_size = (void*)wasf_get_size; - this_sf->sf.get_offset = (void*)wasf_get_offset; - this_sf->sf.get_name = (void*)wasf_get_name; - this_sf->sf.open = (void*)wasf_open; - this_sf->sf.close = (void*)wasf_close; + this_sf->vt.read = (void*)wasf_read; + this_sf->vt.get_size = (void*)wasf_get_size; + this_sf->vt.get_offset = (void*)wasf_get_offset; + this_sf->vt.get_name = (void*)wasf_get_name; + this_sf->vt.open = (void*)wasf_open; + this_sf->vt.close = (void*)wasf_close; this_sf->stdiosf = stdiosf; this_sf->infile_ref = file; - return &this_sf->sf; /* pointer to STREAMFILE start = rest of the custom data follows */ + return &this_sf->vt; fail: close_streamfile(stdiosf);