mirror of
https://github.com/vgmstream/vgmstream.git
synced 2025-02-18 03:26:57 +01:00
Add buffer_streamfile (for custom IO) and simplify stdio reads
This commit is contained in:
parent
a8bb304998
commit
d6ade4123f
224
src/streamfile.c
224
src/streamfile.c
@ -6,12 +6,6 @@
|
||||
#include "vgmstream.h"
|
||||
|
||||
|
||||
/* On EOF reads we can return length 0, or ignore and return the requested length + 0-set the buffer.
|
||||
* Some decoders don't check for EOF and may decode garbage if returned 0, as read_Nbit() funcs return -1.
|
||||
* Only matters for metas that get num_samples wrong (bigger than total data). */
|
||||
#define STREAMFILE_IGNORE_EOF 0
|
||||
|
||||
|
||||
/* a STREAMFILE that operates via standard IO using a buffer */
|
||||
typedef struct {
|
||||
STREAMFILE sf; /* callbacks */
|
||||
@ -27,20 +21,26 @@ typedef struct {
|
||||
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 size_t read_the_rest(uint8_t * dest, off_t offset, size_t length, STDIOSTREAMFILE * streamfile) {
|
||||
size_t length_read_total=0;
|
||||
static size_t read_stdio(STDIOSTREAMFILE *streamfile,uint8_t * dest, off_t offset, size_t length) {
|
||||
size_t length_read_total = 0;
|
||||
|
||||
if (!streamfile || !dest || length <= 0 || offset < 0)
|
||||
return 0;
|
||||
|
||||
/* is the part of the requested length in the buffer? */
|
||||
if (offset >= streamfile->offset && offset < streamfile->offset + streamfile->validsize) {
|
||||
size_t length_read;
|
||||
size_t length_to_read;
|
||||
off_t offset_into_buffer = offset - streamfile->offset;
|
||||
length_read = streamfile->validsize - offset_into_buffer;
|
||||
|
||||
memcpy(dest,streamfile->buffer + offset_into_buffer,length_read);
|
||||
length_read_total += length_read;
|
||||
length -= length_read;
|
||||
offset += length_read;
|
||||
dest += length_read;
|
||||
length_to_read = streamfile->validsize - offset_into_buffer;
|
||||
if (length_to_read > length)
|
||||
length_to_read = 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;
|
||||
}
|
||||
|
||||
/* What would make more sense here is to read the whole request
|
||||
@ -49,25 +49,18 @@ static size_t read_the_rest(uint8_t * dest, off_t offset, size_t length, STDIOST
|
||||
* The destination buffer is supposed to be much smaller than the
|
||||
* STREAMFILE buffer, though. Maybe we should only ever return up
|
||||
* to the buffer size to avoid having to deal with things like this
|
||||
* which are outside of my intended use.
|
||||
*/
|
||||
* which are outside of my intended use. */
|
||||
|
||||
/* read the rest of the requested length */
|
||||
while (length > 0) {
|
||||
size_t length_to_read;
|
||||
size_t length_read;
|
||||
size_t length_to_read, length_read;
|
||||
streamfile->validsize = 0; /* buffer is empty now */
|
||||
|
||||
/* request outside file: ignore to avoid seek/read */
|
||||
if (offset > streamfile->filesize) {
|
||||
streamfile->offset = streamfile->filesize;
|
||||
VGM_LOG_ONCE("ERROR: reading over filesize 0x%x @ 0x%lx + 0x%x (buggy meta?)\n", streamfile->filesize, offset, length);
|
||||
|
||||
#if STREAMFILE_IGNORE_EOF
|
||||
memset(dest,0,length); /* dest is already shifted */
|
||||
return length_read_total + length; /* partially-read + 0-set buffer */
|
||||
#else
|
||||
return length_read_total; /* partially-read buffer */
|
||||
#endif
|
||||
}
|
||||
|
||||
/* position to new offset */
|
||||
@ -90,13 +83,7 @@ static size_t read_the_rest(uint8_t * dest, off_t offset, size_t length, STDIOST
|
||||
/* if we can't get enough to satisfy the request (EOF) we give up */
|
||||
if (length_read < length_to_read) {
|
||||
memcpy(dest,streamfile->buffer,length_read);
|
||||
|
||||
#if STREAMFILE_IGNORE_EOF
|
||||
memset(dest+length_read,0,length-length_read);
|
||||
return length_read_total + length; /* partially-read + 0-set buffer */
|
||||
#else
|
||||
return length_read_total + length_read; /* partially-read buffer */
|
||||
#endif
|
||||
}
|
||||
|
||||
/* use the new buffer */
|
||||
@ -110,38 +97,6 @@ static size_t read_the_rest(uint8_t * dest, off_t offset, size_t length, STDIOST
|
||||
return length_read_total;
|
||||
}
|
||||
|
||||
static size_t read_stdio(STDIOSTREAMFILE *streamfile,uint8_t * dest, off_t offset, size_t length) {
|
||||
|
||||
if (!streamfile || !dest || length<=0)
|
||||
return 0;
|
||||
|
||||
/* request outside file: ignore to avoid seek/read in read_the_rest() */
|
||||
if (offset > streamfile->filesize) {
|
||||
streamfile->offset = streamfile->filesize;
|
||||
VGM_LOG_ONCE("ERROR: offset over filesize 0x%x @ 0x%lx + 0x%x (buggy meta?)\n", streamfile->filesize, offset, length);
|
||||
|
||||
#if STREAMFILE_IGNORE_EOF
|
||||
memset(dest,0,length);
|
||||
return length; /* 0-set buffer */
|
||||
#else
|
||||
return 0; /* nothing to read */
|
||||
#endif
|
||||
}
|
||||
|
||||
/* just copy if entire request is within the buffer */
|
||||
if (offset >= streamfile->offset && offset + length <= streamfile->offset + streamfile->validsize) {
|
||||
off_t offset_into_buffer = offset - streamfile->offset;
|
||||
memcpy(dest,streamfile->buffer + offset_into_buffer,length);
|
||||
return length;
|
||||
}
|
||||
|
||||
/* request outside buffer: new fread */
|
||||
{
|
||||
size_t length_read = read_the_rest(dest,offset,length,streamfile);
|
||||
return length_read;
|
||||
}
|
||||
}
|
||||
|
||||
static void close_stdio(STDIOSTREAMFILE * streamfile) {
|
||||
fclose(streamfile->infile);
|
||||
free(streamfile->buffer);
|
||||
@ -250,6 +205,149 @@ STREAMFILE * open_stdio_streamfile_by_file(FILE * file, const char * filename) {
|
||||
|
||||
/* **************************************************** */
|
||||
|
||||
typedef struct {
|
||||
STREAMFILE sf;
|
||||
|
||||
STREAMFILE *inner_sf;
|
||||
off_t 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 */
|
||||
} BUFFER_STREAMFILE;
|
||||
|
||||
|
||||
static size_t buffer_read(BUFFER_STREAMFILE *streamfile, uint8_t * dest, off_t offset, size_t length) {
|
||||
size_t length_read_total = 0;
|
||||
|
||||
if (!streamfile || !dest || length <= 0 || offset < 0)
|
||||
return 0;
|
||||
|
||||
/* is the part of the requested length in the buffer? */
|
||||
if (offset >= streamfile->offset && offset < streamfile->offset + streamfile->validsize) {
|
||||
size_t length_to_read;
|
||||
off_t offset_into_buffer = offset - streamfile->offset;
|
||||
|
||||
length_to_read = streamfile->validsize - offset_into_buffer;
|
||||
if (length_to_read > length)
|
||||
length_to_read = 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;
|
||||
}
|
||||
|
||||
/* What would make more sense here is to read the whole request
|
||||
* at once into the dest buffer, as it must be large enough, and then
|
||||
* copy some part of that into our own buffer.
|
||||
* The destination buffer is supposed to be much smaller than the
|
||||
* STREAMFILE buffer, though. Maybe we should only ever return up
|
||||
* to the buffer size to avoid having to deal with things like this
|
||||
* which are outside of my intended use. */
|
||||
|
||||
/* read the rest of the requested length */
|
||||
while (length > 0) {
|
||||
size_t length_to_read, length_read;
|
||||
streamfile->validsize = 0; /* buffer is empty now */
|
||||
|
||||
/* request outside file: ignore to avoid seek/read */
|
||||
if (offset > streamfile->filesize) {
|
||||
streamfile->offset = streamfile->filesize;
|
||||
VGM_LOG_ONCE("ERROR: reading over filesize 0x%x @ 0x%lx + 0x%x (buggy meta?)\n", streamfile->filesize, offset, length);
|
||||
return length_read_total; /* partially-read buffer */
|
||||
}
|
||||
|
||||
streamfile->offset = offset;
|
||||
|
||||
/* decide how much must be read this time */
|
||||
if (length > streamfile->buffersize)
|
||||
length_to_read = streamfile->buffersize;
|
||||
else
|
||||
length_to_read = length;
|
||||
|
||||
/* fill the buffer */
|
||||
length_read = streamfile->inner_sf->read(streamfile->inner_sf, streamfile->buffer, streamfile->offset, streamfile->buffersize);
|
||||
streamfile->validsize = length_read;
|
||||
|
||||
/* if we can't get enough to satisfy the request (EOF) we give up */
|
||||
if (length_read < length_to_read) {
|
||||
memcpy(dest,streamfile->buffer,length_read);
|
||||
return length_read_total + length_read; /* partially-read buffer */
|
||||
}
|
||||
|
||||
/* use the new buffer */
|
||||
memcpy(dest,streamfile->buffer,length_to_read);
|
||||
length_read_total += length_to_read;
|
||||
length -= length_to_read;
|
||||
dest += length_to_read;
|
||||
offset += length_to_read;
|
||||
}
|
||||
|
||||
return length_read_total;
|
||||
}
|
||||
static size_t buffer_get_size(BUFFER_STREAMFILE * streamfile) {
|
||||
return streamfile->filesize; /* cache */
|
||||
}
|
||||
static size_t buffer_get_offset(BUFFER_STREAMFILE * streamfile) {
|
||||
return streamfile->offset; /* cache */ //todo internal offset?
|
||||
}
|
||||
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_realname(BUFFER_STREAMFILE *streamfile, char *buffer, size_t length) {
|
||||
streamfile->inner_sf->get_realname(streamfile->inner_sf, buffer, length); /* 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 void buffer_close(BUFFER_STREAMFILE *streamfile) {
|
||||
streamfile->inner_sf->close(streamfile->inner_sf);
|
||||
free(streamfile->buffer);
|
||||
free(streamfile);
|
||||
}
|
||||
|
||||
STREAMFILE *open_buffer_streamfile(STREAMFILE *streamfile, size_t buffer_size) {
|
||||
BUFFER_STREAMFILE *this_sf = NULL;
|
||||
|
||||
if (!streamfile) 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.get_realname = (void*)buffer_get_realname;
|
||||
this_sf->sf.open = (void*)buffer_open;
|
||||
this_sf->sf.close = (void*)buffer_close;
|
||||
this_sf->sf.stream_index = streamfile->stream_index;
|
||||
|
||||
this_sf->inner_sf = streamfile;
|
||||
|
||||
this_sf->filesize = streamfile->get_size(streamfile);
|
||||
|
||||
return &this_sf->sf;
|
||||
|
||||
fail:
|
||||
if (this_sf) free(this_sf->buffer);
|
||||
free(this_sf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* **************************************************** */
|
||||
|
||||
//todo stream_index: copy? pass? funtion? external?
|
||||
//todo use realnames on reopen? simplify?
|
||||
//todo use safe string ops, this ain't easy
|
||||
|
@ -70,6 +70,11 @@ STREAMFILE *open_stdio_streamfile(const char * filename);
|
||||
/* Opens a standard STREAMFILE from a pre-opened FILE. */
|
||||
STREAMFILE *open_stdio_streamfile_by_file(FILE * file, const char * filename);
|
||||
|
||||
/* Opens a STREAMFILE that does buffered IO.
|
||||
* Can be used when the underlying IO may be slow (like when using custom IO).
|
||||
* Buffer size is optional. */
|
||||
STREAMFILE *open_buffer_streamfile(STREAMFILE *streamfile, size_t buffer_size);
|
||||
|
||||
/* Opens a STREAMFILE that doesn't close the underlying streamfile.
|
||||
* Calls to open won't wrap the new SF (assumes it needs to be closed).
|
||||
* Can be used in metas to test custom IO without closing the external SF. */
|
||||
|
Loading…
x
Reference in New Issue
Block a user