2008-08-02 05:15:24 +02:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include "streamfile.h"
|
|
|
|
#include "util.h"
|
2017-01-14 00:59:54 +01:00
|
|
|
#include "vgmstream.h"
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
|
|
2017-08-13 19:58:01 +02:00
|
|
|
/* a STREAMFILE that operates via standard IO using a buffer */
|
2008-08-02 05:15:24 +02:00
|
|
|
typedef struct {
|
2017-04-29 16:03:11 +02:00
|
|
|
STREAMFILE sf; /* callbacks */
|
|
|
|
FILE * infile; /* actual FILE */
|
2017-01-15 20:58:47 +01:00
|
|
|
char name[PATH_LIMIT];
|
2017-04-29 16:03:11 +02:00
|
|
|
off_t offset; /* current offset */
|
|
|
|
size_t validsize; /* current buffer size */
|
|
|
|
uint8_t * buffer; /* data buffer */
|
|
|
|
size_t buffersize; /* max buffer size */
|
|
|
|
size_t filesize; /* cached file size (max offset) */
|
2008-08-02 05:15:24 +02:00
|
|
|
} STDIOSTREAMFILE;
|
|
|
|
|
2017-08-13 19:58:01 +02:00
|
|
|
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);
|
2008-08-02 05:15:24 +02:00
|
|
|
|
|
|
|
static size_t read_the_rest(uint8_t * dest, off_t offset, size_t length, STDIOSTREAMFILE * streamfile) {
|
|
|
|
size_t length_read_total=0;
|
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
/* is the part of the requested length in the buffer? */
|
|
|
|
if (offset >= streamfile->offset && offset < streamfile->offset + streamfile->validsize) {
|
2008-08-02 05:15:24 +02:00
|
|
|
size_t length_read;
|
2017-04-29 16:03:11 +02:00
|
|
|
off_t offset_into_buffer = offset - streamfile->offset;
|
|
|
|
length_read = streamfile->validsize - offset_into_buffer;
|
|
|
|
|
|
|
|
memcpy(dest,streamfile->buffer + offset_into_buffer,length_read);
|
2008-08-02 05:15:24 +02:00
|
|
|
length_read_total += length_read;
|
|
|
|
length -= length_read;
|
|
|
|
offset += length_read;
|
|
|
|
dest += length_read;
|
|
|
|
}
|
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
/* What would make more sense here is to read the whole request
|
2008-08-02 05:15:24 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
2017-04-29 16:03:11 +02:00
|
|
|
/* read the rest of the requested length */
|
|
|
|
while (length > 0) {
|
2008-08-02 05:15:24 +02:00
|
|
|
size_t length_to_read;
|
2017-01-15 20:58:47 +01:00
|
|
|
size_t length_read;
|
2017-04-29 16:03:11 +02:00
|
|
|
streamfile->validsize = 0; /* buffer is empty now */
|
2017-01-15 20:58:47 +01:00
|
|
|
|
|
|
|
/* request outside file: ignore to avoid seek/read */
|
|
|
|
if (offset > streamfile->filesize) {
|
2017-04-29 16:03:11 +02:00
|
|
|
streamfile->offset = streamfile->filesize;
|
2017-11-23 23:46:33 +01:00
|
|
|
VGM_LOG_ONCE("ERROR: reading over filesize 0x%x @ 0x%lx + 0x%x (buggy meta?)\n", streamfile->filesize, offset, length);
|
2017-04-29 16:03:11 +02:00
|
|
|
|
|
|
|
#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
|
2017-01-15 20:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* position to new offset */
|
|
|
|
if (fseeko(streamfile->infile,offset,SEEK_SET)) {
|
|
|
|
streamfile->offset = streamfile->filesize;
|
2017-04-29 16:03:11 +02:00
|
|
|
return 0; /* fail miserably (fseek shouldn't fail and reach this) */
|
2017-01-15 20:58:47 +01:00
|
|
|
}
|
2017-04-29 16:03:11 +02:00
|
|
|
streamfile->offset = offset;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
|
|
|
/* decide how much must be read this time */
|
2017-04-29 16:03:11 +02:00
|
|
|
if (length > streamfile->buffersize)
|
|
|
|
length_to_read = streamfile->buffersize;
|
|
|
|
else
|
|
|
|
length_to_read = length;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
/* fill the buffer */
|
|
|
|
length_read = fread(streamfile->buffer,sizeof(uint8_t),streamfile->buffersize,streamfile->infile);
|
|
|
|
streamfile->validsize = length_read;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
/* if we can't get enough to satisfy the request (EOF) we give up */
|
2008-08-02 05:15:24 +02:00
|
|
|
if (length_read < length_to_read) {
|
|
|
|
memcpy(dest,streamfile->buffer,length_read);
|
2017-04-29 16:03:11 +02:00
|
|
|
|
|
|
|
#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
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* use the new buffer */
|
|
|
|
memcpy(dest,streamfile->buffer,length_to_read);
|
2017-04-29 16:03:11 +02:00
|
|
|
length_read_total += length_to_read;
|
|
|
|
length -= length_to_read;
|
|
|
|
dest += length_to_read;
|
|
|
|
offset += length_to_read;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return length_read_total;
|
|
|
|
}
|
|
|
|
|
2017-01-15 20:58:47 +01:00
|
|
|
static size_t read_stdio(STDIOSTREAMFILE *streamfile,uint8_t * dest, off_t offset, size_t length) {
|
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
if (!streamfile || !dest || length<=0)
|
|
|
|
return 0;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2017-01-15 20:58:47 +01:00
|
|
|
/* request outside file: ignore to avoid seek/read in read_the_rest() */
|
|
|
|
if (offset > streamfile->filesize) {
|
2017-04-29 16:03:11 +02:00
|
|
|
streamfile->offset = streamfile->filesize;
|
2017-11-23 23:46:33 +01:00
|
|
|
VGM_LOG_ONCE("ERROR: offset over filesize 0x%x @ 0x%lx + 0x%x (buggy meta?)\n", streamfile->filesize, offset, length);
|
2017-01-15 20:58:47 +01:00
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
#if STREAMFILE_IGNORE_EOF
|
2017-01-15 20:58:47 +01:00
|
|
|
memset(dest,0,length);
|
2017-04-29 16:03:11 +02:00
|
|
|
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);
|
2017-01-15 20:58:47 +01:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* request outside buffer: new fread */
|
2008-12-31 19:44:08 +01:00
|
|
|
{
|
|
|
|
size_t length_read = read_the_rest(dest,offset,length,streamfile);
|
|
|
|
return length_read;
|
|
|
|
}
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void close_stdio(STDIOSTREAMFILE * streamfile) {
|
|
|
|
fclose(streamfile->infile);
|
|
|
|
free(streamfile->buffer);
|
|
|
|
free(streamfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_size_stdio(STDIOSTREAMFILE * streamfile) {
|
2017-01-15 20:58:47 +01:00
|
|
|
return streamfile->filesize;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static off_t get_offset_stdio(STDIOSTREAMFILE *streamFile) {
|
|
|
|
return streamFile->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_name_stdio(STDIOSTREAMFILE *streamfile,char *buffer,size_t length) {
|
2012-08-21 17:34:10 +02:00
|
|
|
strncpy(buffer,streamfile->name,length);
|
|
|
|
buffer[length-1]='\0';
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static STREAMFILE *open_stdio(STDIOSTREAMFILE *streamFile,const char * const filename,size_t buffersize) {
|
|
|
|
int newfd;
|
|
|
|
FILE *newfile;
|
|
|
|
STREAMFILE *newstreamFile;
|
|
|
|
|
|
|
|
if (!filename)
|
|
|
|
return NULL;
|
2017-08-13 19:58:01 +02:00
|
|
|
|
2008-08-02 05:15:24 +02:00
|
|
|
// if same name, duplicate the file pointer we already have open
|
|
|
|
if (!strcmp(streamFile->name,filename)) {
|
|
|
|
if (((newfd = dup(fileno(streamFile->infile))) >= 0) &&
|
|
|
|
(newfile = fdopen( newfd, "rb" )))
|
|
|
|
{
|
2017-08-13 19:58:01 +02:00
|
|
|
newstreamFile = open_stdio_streamfile_buffer_by_file(newfile,filename,buffersize);
|
2008-08-02 05:15:24 +02:00
|
|
|
if (newstreamFile) {
|
|
|
|
return newstreamFile;
|
|
|
|
}
|
|
|
|
// failure, close it and try the default path (which will probably fail a second time)
|
|
|
|
fclose(newfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// a normal open, open a new file
|
|
|
|
return open_stdio_streamfile_buffer(filename,buffersize);
|
|
|
|
}
|
|
|
|
|
2017-08-13 19:58:01 +02:00
|
|
|
static STREAMFILE * open_stdio_streamfile_buffer_by_file(FILE *infile,const char * const filename, size_t buffersize) {
|
2008-08-02 05:15:24 +02:00
|
|
|
uint8_t * buffer;
|
|
|
|
STDIOSTREAMFILE * streamfile;
|
|
|
|
|
|
|
|
buffer = calloc(buffersize,1);
|
|
|
|
if (!buffer) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
streamfile = calloc(1,sizeof(STDIOSTREAMFILE));
|
|
|
|
if (!streamfile) {
|
|
|
|
free(buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.get_realname = (void*)get_name_stdio;
|
|
|
|
streamfile->sf.open = (void*)open_stdio;
|
|
|
|
streamfile->sf.close = (void*)close_stdio;
|
|
|
|
|
|
|
|
streamfile->infile = infile;
|
|
|
|
streamfile->buffersize = buffersize;
|
|
|
|
streamfile->buffer = buffer;
|
|
|
|
|
2012-08-21 17:34:10 +02:00
|
|
|
strncpy(streamfile->name,filename,sizeof(streamfile->name));
|
|
|
|
streamfile->name[sizeof(streamfile->name)-1] = '\0';
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2017-01-15 20:58:47 +01:00
|
|
|
/* cache filesize */
|
|
|
|
fseeko(streamfile->infile,0,SEEK_END);
|
|
|
|
streamfile->filesize = ftello(streamfile->infile);
|
|
|
|
|
2008-08-02 05:15:24 +02:00
|
|
|
return &streamfile->sf;
|
|
|
|
}
|
|
|
|
|
2017-08-13 19:58:01 +02:00
|
|
|
static STREAMFILE * open_stdio_streamfile_buffer(const char * const filename, size_t buffersize) {
|
2008-08-02 05:15:24 +02:00
|
|
|
FILE * infile;
|
|
|
|
STREAMFILE *streamFile;
|
|
|
|
|
|
|
|
infile = fopen(filename,"rb");
|
|
|
|
if (!infile) return NULL;
|
|
|
|
|
2017-08-13 19:58:01 +02:00
|
|
|
streamFile = open_stdio_streamfile_buffer_by_file(infile,filename,buffersize);
|
2008-08-02 05:15:24 +02:00
|
|
|
if (!streamFile) {
|
|
|
|
fclose(infile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return streamFile;
|
|
|
|
}
|
2008-07-21 01:28:16 +02:00
|
|
|
|
2017-08-13 19:58:01 +02:00
|
|
|
|
|
|
|
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) {
|
|
|
|
return open_stdio_streamfile_buffer_by_file(file,filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
|
2018-01-20 00:54:08 +01:00
|
|
|
/* **************************************************** */
|
|
|
|
|
|
|
|
//todo stream_index: copy? pass? funtion? external?
|
2018-01-27 13:58:46 +01:00
|
|
|
//todo use realnames on reopen? simplify?
|
|
|
|
//todo use safe string ops, this ain't easy
|
2018-01-20 00:54:08 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
STREAMFILE sf;
|
|
|
|
|
|
|
|
STREAMFILE *inner_sf;
|
|
|
|
} WRAP_STREAMFILE;
|
|
|
|
|
|
|
|
static size_t wrap_read(WRAP_STREAMFILE *streamfile, uint8_t * dest, off_t offset, size_t length) {
|
|
|
|
return streamfile->inner_sf->read(streamfile->inner_sf, dest, 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_offset(WRAP_STREAMFILE * streamfile) {
|
|
|
|
return streamfile->inner_sf->get_offset(streamfile->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_realname(WRAP_STREAMFILE *streamfile, char *buffer, size_t length) {
|
|
|
|
streamfile->inner_sf->get_realname(streamfile->inner_sf, buffer, length); /* 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_close(WRAP_STREAMFILE *streamfile) {
|
|
|
|
//streamfile->inner_sf->close(streamfile->inner_sf); /* don't close */
|
|
|
|
free(streamfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAMFILE *open_wrap_streamfile(STREAMFILE *streamfile) {
|
|
|
|
WRAP_STREAMFILE *this_sf;
|
|
|
|
|
|
|
|
if (!streamfile) return NULL;
|
|
|
|
|
|
|
|
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.get_realname = (void*)wrap_get_realname;
|
|
|
|
this_sf->sf.open = (void*)wrap_open;
|
|
|
|
this_sf->sf.close = (void*)wrap_close;
|
|
|
|
this_sf->sf.stream_index = streamfile->stream_index;
|
|
|
|
|
|
|
|
this_sf->inner_sf = streamfile;
|
|
|
|
|
|
|
|
return &this_sf->sf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
STREAMFILE sf;
|
|
|
|
|
|
|
|
STREAMFILE *inner_sf;
|
|
|
|
off_t start;
|
|
|
|
size_t size;
|
|
|
|
} CLAMP_STREAMFILE;
|
|
|
|
|
|
|
|
static size_t clamp_read(CLAMP_STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length) {
|
|
|
|
off_t inner_offset = streamfile->start + offset;
|
|
|
|
size_t clamp_length = length > (streamfile->size - offset) ? (streamfile->size - offset) : length;
|
|
|
|
return streamfile->inner_sf->read(streamfile->inner_sf, dest, inner_offset, clamp_length);
|
|
|
|
}
|
|
|
|
static size_t clamp_get_size(CLAMP_STREAMFILE *streamfile) {
|
|
|
|
return streamfile->size;
|
|
|
|
}
|
|
|
|
static off_t clamp_get_offset(CLAMP_STREAMFILE *streamfile) {
|
|
|
|
return streamfile->inner_sf->get_offset(streamfile->inner_sf) - streamfile->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_realname(CLAMP_STREAMFILE *streamfile, char *buffer, size_t length) {
|
|
|
|
streamfile->inner_sf->get_realname(streamfile->inner_sf, buffer, length); /* default */
|
|
|
|
}
|
|
|
|
static STREAMFILE *clamp_open(CLAMP_STREAMFILE *streamfile, const char * const filename, size_t buffersize) {
|
2018-01-27 11:07:45 +01:00
|
|
|
char original_filename[PATH_LIMIT];
|
|
|
|
STREAMFILE *new_inner_sf;
|
|
|
|
|
|
|
|
new_inner_sf = streamfile->inner_sf->open(streamfile->inner_sf,filename,buffersize);
|
2018-01-27 13:58:46 +01:00
|
|
|
streamfile->inner_sf->get_name(streamfile->inner_sf, original_filename, PATH_LIMIT);
|
2018-01-27 11:07:45 +01:00
|
|
|
|
2018-01-27 13:58:46 +01:00
|
|
|
/* detect re-opening the file */
|
2018-01-27 11:07:45 +01:00
|
|
|
if (strcmp(filename, original_filename) == 0) {
|
2018-01-27 13:58:46 +01:00
|
|
|
return open_clamp_streamfile(new_inner_sf, streamfile->start, streamfile->size); /* clamp again */
|
2018-01-27 11:07:45 +01:00
|
|
|
} else {
|
2018-01-27 13:58:46 +01:00
|
|
|
return new_inner_sf; /**/
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
|
|
|
static void clamp_close(CLAMP_STREAMFILE *streamfile) {
|
|
|
|
streamfile->inner_sf->close(streamfile->inner_sf);
|
|
|
|
free(streamfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAMFILE *open_clamp_streamfile(STREAMFILE *streamfile, off_t start, size_t size) {
|
|
|
|
CLAMP_STREAMFILE *this_sf;
|
|
|
|
|
2018-01-27 11:53:15 +01:00
|
|
|
if (!streamfile || !size) return NULL;
|
2018-01-27 11:07:45 +01:00
|
|
|
if (start + size > get_streamfile_size(streamfile)) return NULL;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
|
|
|
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.get_realname = (void*)clamp_get_realname;
|
|
|
|
this_sf->sf.open = (void*)clamp_open;
|
|
|
|
this_sf->sf.close = (void*)clamp_close;
|
|
|
|
this_sf->sf.stream_index = streamfile->stream_index;
|
|
|
|
|
|
|
|
this_sf->inner_sf = streamfile;
|
|
|
|
this_sf->start = start;
|
|
|
|
this_sf->size = size;
|
|
|
|
|
|
|
|
return &this_sf->sf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
STREAMFILE sf;
|
|
|
|
|
|
|
|
STREAMFILE *inner_sf;
|
|
|
|
void* data;
|
|
|
|
size_t data_size;
|
|
|
|
size_t (*read_callback)(STREAMFILE *, uint8_t *, off_t, size_t, void*);
|
|
|
|
} IO_STREAMFILE;
|
|
|
|
|
|
|
|
static size_t io_read(IO_STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length) {
|
|
|
|
return streamfile->read_callback(streamfile->inner_sf, dest, offset, length, streamfile->data);
|
|
|
|
}
|
|
|
|
static size_t io_get_size(IO_STREAMFILE *streamfile) {
|
|
|
|
return streamfile->inner_sf->get_size(streamfile->inner_sf); /* default */
|
|
|
|
}
|
|
|
|
static off_t io_get_offset(IO_STREAMFILE *streamfile) {
|
|
|
|
return streamfile->inner_sf->get_offset(streamfile->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_realname(IO_STREAMFILE *streamfile, char *buffer, size_t length) {
|
|
|
|
streamfile->inner_sf->get_realname(streamfile->inner_sf, buffer, length); /* default */
|
|
|
|
}
|
|
|
|
static STREAMFILE *io_open(IO_STREAMFILE *streamfile, const char * const filename, size_t buffersize) {
|
2018-01-27 11:07:45 +01:00
|
|
|
//todo should have some flag to decide if opening other files with IO
|
2018-01-20 00:54:08 +01:00
|
|
|
STREAMFILE *new_inner_sf = streamfile->inner_sf->open(streamfile->inner_sf,filename,buffersize);
|
|
|
|
return open_io_streamfile(new_inner_sf, streamfile->data, streamfile->data_size, streamfile->read_callback);
|
|
|
|
}
|
|
|
|
static void io_close(IO_STREAMFILE *streamfile) {
|
|
|
|
streamfile->inner_sf->close(streamfile->inner_sf);
|
|
|
|
free(streamfile->data);
|
|
|
|
free(streamfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAMFILE *open_io_streamfile(STREAMFILE *streamfile, void* data, size_t data_size, void* read_callback) {
|
|
|
|
IO_STREAMFILE *this_sf;
|
|
|
|
|
|
|
|
if (!streamfile) return NULL;
|
|
|
|
if ((data && !data_size) || (!data && data_size)) return NULL;
|
|
|
|
|
|
|
|
this_sf = calloc(1,sizeof(IO_STREAMFILE));
|
|
|
|
if (!this_sf) return NULL;
|
|
|
|
|
|
|
|
/* 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.get_realname = (void*)io_get_realname;
|
|
|
|
this_sf->sf.open = (void*)io_open;
|
|
|
|
this_sf->sf.close = (void*)io_close;
|
|
|
|
this_sf->sf.stream_index = streamfile->stream_index;
|
|
|
|
|
|
|
|
this_sf->inner_sf = streamfile;
|
|
|
|
if (data) {
|
|
|
|
this_sf->data = malloc(data_size);
|
|
|
|
if (!this_sf->data) {
|
|
|
|
free(this_sf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(this_sf->data, data, data_size);
|
|
|
|
}
|
|
|
|
this_sf->data_size = data_size;
|
|
|
|
this_sf->read_callback = read_callback;
|
|
|
|
|
|
|
|
return &this_sf->sf;
|
|
|
|
}
|
2017-08-13 19:58:01 +02:00
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
2018-01-27 11:07:45 +01:00
|
|
|
typedef struct {
|
|
|
|
STREAMFILE sf;
|
|
|
|
|
|
|
|
STREAMFILE *inner_sf;
|
|
|
|
char fakename[PATH_LIMIT];
|
|
|
|
} FAKENAME_STREAMFILE;
|
|
|
|
|
|
|
|
static size_t fakename_read(FAKENAME_STREAMFILE *streamfile, uint8_t * dest, off_t offset, size_t length) {
|
|
|
|
return streamfile->inner_sf->read(streamfile->inner_sf, dest, 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_offset(FAKENAME_STREAMFILE * streamfile) {
|
|
|
|
return streamfile->inner_sf->get_offset(streamfile->inner_sf); /* default */
|
|
|
|
}
|
|
|
|
static void fakename_get_name(FAKENAME_STREAMFILE *streamfile, char *buffer, size_t length) {
|
2018-01-27 13:58:46 +01:00
|
|
|
strncpy(buffer,streamfile->fakename,length);
|
2018-01-27 11:07:45 +01:00
|
|
|
buffer[length-1]='\0';
|
|
|
|
}
|
|
|
|
static void fakename_get_realname(FAKENAME_STREAMFILE *streamfile, char *buffer, size_t length) {
|
|
|
|
fakename_get_name(streamfile, buffer, length);
|
|
|
|
}
|
|
|
|
static STREAMFILE *fakename_open(FAKENAME_STREAMFILE *streamfile, const char * const filename, size_t buffersize) {
|
|
|
|
/* detect re-opening the file */
|
|
|
|
if (strcmp(filename, streamfile->fakename) == 0) {
|
|
|
|
STREAMFILE *new_inner_sf;
|
|
|
|
char original_filename[PATH_LIMIT];
|
|
|
|
|
2018-01-27 13:58:46 +01:00
|
|
|
streamfile->inner_sf->get_name(streamfile->inner_sf, original_filename, PATH_LIMIT);
|
2018-01-27 11:07:45 +01:00
|
|
|
new_inner_sf = streamfile->inner_sf->open(streamfile->inner_sf, original_filename, buffersize);
|
|
|
|
return open_fakename_streamfile(new_inner_sf, streamfile->fakename, NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return streamfile->inner_sf->open(streamfile->inner_sf, filename, buffersize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void fakename_close(FAKENAME_STREAMFILE *streamfile) {
|
|
|
|
streamfile->inner_sf->close(streamfile->inner_sf);
|
|
|
|
free(streamfile);
|
|
|
|
}
|
|
|
|
|
2018-03-03 19:07:59 +01:00
|
|
|
STREAMFILE *open_fakename_streamfile(STREAMFILE *streamfile, const char * fakename, const char* fakeext) {
|
2018-01-27 11:07:45 +01:00
|
|
|
FAKENAME_STREAMFILE *this_sf;
|
|
|
|
|
|
|
|
if (!streamfile || (!fakename && !fakeext)) return NULL;
|
|
|
|
|
|
|
|
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.get_realname = (void*)fakename_get_realname;
|
|
|
|
this_sf->sf.open = (void*)fakename_open;
|
|
|
|
this_sf->sf.close = (void*)fakename_close;
|
|
|
|
this_sf->sf.stream_index = streamfile->stream_index;
|
|
|
|
|
|
|
|
this_sf->inner_sf = streamfile;
|
|
|
|
|
|
|
|
/* copy passed name or retain current, and swap extension if expected */
|
|
|
|
if (fakename) {
|
|
|
|
strcpy(this_sf->fakename,fakename);
|
|
|
|
} else {
|
2018-01-27 13:58:46 +01:00
|
|
|
streamfile->get_name(streamfile, this_sf->fakename, PATH_LIMIT);
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
|
|
|
if (fakeext) {
|
|
|
|
char * ext = strrchr(this_sf->fakename,'.');
|
|
|
|
if (ext != NULL)
|
|
|
|
ext[1] = '\0'; /* truncate past dot */
|
|
|
|
strcat(this_sf->fakename, fakeext);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &this_sf->sf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
2018-01-27 13:58:46 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
STREAMFILE sf;
|
|
|
|
|
|
|
|
STREAMFILE **inner_sfs;
|
|
|
|
size_t inner_sfs_size;
|
|
|
|
size_t *sizes;
|
|
|
|
off_t size;
|
|
|
|
off_t offset;
|
|
|
|
} MULTIFILE_STREAMFILE;
|
|
|
|
|
|
|
|
static size_t multifile_read(MULTIFILE_STREAMFILE *streamfile, uint8_t * dest, off_t offset, size_t length) {
|
|
|
|
int i, segment = 0;
|
|
|
|
off_t segment_offset = 0;
|
|
|
|
size_t done = 0;
|
|
|
|
|
|
|
|
if (offset > streamfile->size) {
|
|
|
|
streamfile->offset = streamfile->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];
|
|
|
|
/* check if offset falls in this segment */
|
|
|
|
if (offset >= segment_offset && offset < segment_offset + segment_size) {
|
|
|
|
segment = i;
|
|
|
|
segment_offset = offset - segment_offset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
segment_offset += segment_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reads can span multiple segments */
|
|
|
|
while(done < length) {
|
|
|
|
if (segment >= streamfile->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], dest+done, segment_offset, length - done);
|
|
|
|
segment++;
|
|
|
|
segment_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
streamfile->offset = offset + done;
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
static size_t multifile_get_size(MULTIFILE_STREAMFILE *streamfile) {
|
|
|
|
return streamfile->size;
|
|
|
|
}
|
|
|
|
static size_t multifile_get_offset(MULTIFILE_STREAMFILE * streamfile) {
|
|
|
|
return streamfile->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_realname(MULTIFILE_STREAMFILE *streamfile, char *buffer, size_t length) {
|
|
|
|
multifile_get_name(streamfile, buffer, length);
|
|
|
|
}
|
|
|
|
static STREAMFILE *multifile_open(MULTIFILE_STREAMFILE *streamfile, const char * const filename, size_t buffersize) {
|
|
|
|
char original_filename[PATH_LIMIT];
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* detect re-opening the file */
|
|
|
|
if (strcmp(filename, original_filename) == 0) { /* same multifile */
|
|
|
|
new_inner_sfs = calloc(streamfile->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);
|
|
|
|
if (!new_inner_sfs[i]) goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_sf = open_multifile_streamfile(new_inner_sfs, streamfile->inner_sfs_size);
|
|
|
|
if (!new_sf) goto fail;
|
|
|
|
|
|
|
|
return new_sf;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return streamfile->inner_sfs[0]->open(streamfile->inner_sfs[0], filename, buffersize); /* regular file */
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (new_inner_sfs) {
|
|
|
|
for (i = 0; i < streamfile->inner_sfs_size; i++)
|
|
|
|
close_streamfile(new_inner_sfs[i]);
|
|
|
|
}
|
|
|
|
free(new_inner_sfs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
static void multifile_close(MULTIFILE_STREAMFILE *streamfile) {
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
free(streamfile->inner_sfs);
|
|
|
|
free(streamfile->sizes);
|
|
|
|
free(streamfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAMFILE *open_multifile_streamfile(STREAMFILE **streamfiles, size_t streamfiles_size) {
|
|
|
|
MULTIFILE_STREAMFILE *this_sf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!streamfiles || !streamfiles_size) return NULL;
|
|
|
|
for (i = 0; i < streamfiles_size; i++) {
|
|
|
|
if (!streamfiles[i]) return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.get_realname = (void*)multifile_get_realname;
|
|
|
|
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->inner_sfs_size = streamfiles_size;
|
|
|
|
this_sf->inner_sfs = calloc(streamfiles_size, sizeof(STREAMFILE*));
|
|
|
|
if (!this_sf->inner_sfs) goto fail;
|
|
|
|
this_sf->sizes = calloc(streamfiles_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->size += this_sf->sizes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return &this_sf->sf;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (this_sf) {
|
|
|
|
free(this_sf->inner_sfs);
|
|
|
|
free(this_sf->sizes);
|
|
|
|
}
|
|
|
|
free(this_sf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
2017-11-23 22:53:43 +01:00
|
|
|
/* Read a line into dst. The source files are lines separated by CRLF (Windows) / LF (Unux) / CR (Mac).
|
|
|
|
* The line will be null-terminated and CR/LF removed if found.
|
|
|
|
*
|
|
|
|
* Returns the number of bytes read (including CR/LF), note that this is not the string length.
|
|
|
|
* line_done_ptr is set to 1 if the complete line was read into dst; NULL can be passed to ignore.
|
2008-07-21 01:28:16 +02:00
|
|
|
*/
|
2017-11-23 22:53:43 +01:00
|
|
|
size_t get_streamfile_text_line(int dst_length, char * dst, off_t offset, STREAMFILE * streamfile, int *line_done_ptr) {
|
2008-07-21 01:28:16 +02:00
|
|
|
int i;
|
2017-11-23 22:53:43 +01:00
|
|
|
off_t file_length = get_streamfile_size(streamfile);
|
|
|
|
int extra_bytes = 0; /* how many bytes over those put in the buffer were read */
|
2008-07-21 01:28:16 +02:00
|
|
|
|
|
|
|
if (line_done_ptr) *line_done_ptr = 0;
|
|
|
|
|
2017-11-23 22:53:43 +01:00
|
|
|
for (i = 0; i < dst_length-1 && offset+i < file_length; i++) {
|
|
|
|
char in_char = read_8bit(offset+i,streamfile);
|
2008-07-21 01:28:16 +02:00
|
|
|
/* check for end of line */
|
2017-11-23 22:53:43 +01:00
|
|
|
if (in_char == 0x0d && read_8bit(offset+i+1,streamfile) == 0x0a) { /* CRLF */
|
2008-07-21 01:28:16 +02:00
|
|
|
extra_bytes = 2;
|
|
|
|
if (line_done_ptr) *line_done_ptr = 1;
|
|
|
|
break;
|
|
|
|
}
|
2017-11-23 22:53:43 +01:00
|
|
|
else if (in_char == 0x0d || in_char == 0x0a) { /* CR or LF */
|
|
|
|
extra_bytes = 1;
|
|
|
|
if (line_done_ptr) *line_done_ptr = 1;
|
|
|
|
break;
|
|
|
|
}
|
2008-07-21 01:28:16 +02:00
|
|
|
|
2017-11-23 22:53:43 +01:00
|
|
|
dst[i] = in_char;
|
2008-07-21 01:28:16 +02:00
|
|
|
}
|
2017-11-23 22:53:43 +01:00
|
|
|
|
|
|
|
dst[i] = '\0';
|
2008-07-21 01:28:16 +02:00
|
|
|
|
|
|
|
/* did we fill the buffer? */
|
2017-11-23 22:53:43 +01:00
|
|
|
if (i == dst_length) {
|
|
|
|
char in_char = read_8bit(offset+i,streamfile);
|
2008-07-21 01:28:16 +02:00
|
|
|
/* did the bytes we missed just happen to be the end of the line? */
|
2017-11-23 22:53:43 +01:00
|
|
|
if (in_char == 0x0d && read_8bit(offset+i+1,streamfile) == 0x0a) { /* CRLF */
|
2008-07-21 01:28:16 +02:00
|
|
|
extra_bytes = 2;
|
2017-11-23 22:53:43 +01:00
|
|
|
if (line_done_ptr) *line_done_ptr = 1;
|
|
|
|
}
|
|
|
|
else if (in_char == 0x0d || in_char == 0x0a) { /* CR or LF */
|
|
|
|
extra_bytes = 1;
|
2008-07-21 01:28:16 +02:00
|
|
|
if (line_done_ptr) *line_done_ptr = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* did we hit the file end? */
|
2017-11-23 22:53:43 +01:00
|
|
|
if (offset+i == file_length) {
|
2008-07-21 01:28:16 +02:00
|
|
|
/* then we did in fact finish reading the last line */
|
|
|
|
if (line_done_ptr) *line_done_ptr = 1;
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:53:43 +01:00
|
|
|
return i + extra_bytes;
|
2008-07-21 01:28:16 +02:00
|
|
|
}
|
2016-11-27 19:35:26 +01:00
|
|
|
|
|
|
|
|
2017-08-12 11:14:16 +02:00
|
|
|
/* reads a c-string, up to maxsize or NULL, returning size. buf is optional. */
|
|
|
|
int read_string(char * buf, size_t maxsize, off_t offset, STREAMFILE *streamFile) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i < maxsize; i++) {
|
|
|
|
char c = read_8bit(offset + i, streamFile);
|
|
|
|
if (buf) buf[i] = c;
|
|
|
|
if (c == '\0')
|
|
|
|
return i;
|
|
|
|
if (i+1 == maxsize) { /* null at maxsize and don't validate (expected to be garbage) */
|
|
|
|
if (buf) buf[i] = '\0';
|
|
|
|
return maxsize;
|
|
|
|
}
|
|
|
|
if (c < 0x20 || c > 0xA5)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (buf) buf[0] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:53:43 +01:00
|
|
|
/* Opens an stream using the base streamFile name plus a new extension (ex. for headers in a separate file) */
|
2017-01-14 01:37:53 +01:00
|
|
|
STREAMFILE * open_stream_ext(STREAMFILE *streamFile, const char * ext) {
|
|
|
|
char filename_ext[PATH_LIMIT];
|
|
|
|
|
|
|
|
streamFile->get_name(streamFile,filename_ext,sizeof(filename_ext));
|
|
|
|
strcpy(filename_ext + strlen(filename_ext) - strlen(filename_extension(filename_ext)), ext);
|
|
|
|
|
|
|
|
return streamFile->open(streamFile,filename_ext,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:53:43 +01:00
|
|
|
/* Opens an stream using the passed name (in the same folder) */
|
2017-11-05 17:05:39 +01:00
|
|
|
STREAMFILE * open_stream_name(STREAMFILE *streamFile, const char * name) {
|
|
|
|
char foldername[PATH_LIMIT];
|
|
|
|
char filename[PATH_LIMIT];
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
streamFile->get_name(streamFile,foldername,sizeof(foldername));
|
|
|
|
|
|
|
|
path = strrchr(foldername,DIR_SEPARATOR);
|
|
|
|
if (path!=NULL) path = path+1;
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
strcpy(filename, foldername);
|
|
|
|
filename[path-foldername] = '\0';
|
|
|
|
strcat(filename, name);
|
|
|
|
} else {
|
|
|
|
strcpy(filename, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return streamFile->open(streamFile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
|
2018-01-20 20:06:15 +01:00
|
|
|
/* Opens a file containing decryption keys and copies to buffer.
|
|
|
|
* Tries combinations of keynames based on the original filename.
|
|
|
|
* returns size of key if found and copied */
|
|
|
|
size_t read_key_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile) {
|
2016-11-27 19:35:26 +01:00
|
|
|
char keyname[PATH_LIMIT];
|
2016-11-27 22:42:42 +01:00
|
|
|
char filename[PATH_LIMIT];
|
2016-11-27 19:35:26 +01:00
|
|
|
const char *path, *ext;
|
|
|
|
STREAMFILE * streamFileKey = NULL;
|
2018-01-20 20:06:15 +01:00
|
|
|
size_t keysize;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
2016-11-27 22:42:42 +01:00
|
|
|
streamFile->get_name(streamFile,filename,sizeof(filename));
|
|
|
|
|
|
|
|
if (strlen(filename)+4 > sizeof(keyname)) goto fail;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
|
|
|
/* try to open a keyfile using variations:
|
|
|
|
* "(name.ext)key" (per song), "(.ext)key" (per folder) */
|
|
|
|
{
|
2016-11-27 22:42:42 +01:00
|
|
|
ext = strrchr(filename,'.');
|
2016-11-27 19:35:26 +01:00
|
|
|
if (ext!=NULL) ext = ext+1;
|
|
|
|
|
2016-11-27 22:42:42 +01:00
|
|
|
path = strrchr(filename,DIR_SEPARATOR);
|
2016-11-27 19:35:26 +01:00
|
|
|
if (path!=NULL) path = path+1;
|
|
|
|
|
|
|
|
/* "(name.ext)key" */
|
2016-11-27 22:42:42 +01:00
|
|
|
strcpy(keyname, filename);
|
2016-11-27 19:35:26 +01:00
|
|
|
strcat(keyname, "key");
|
2016-11-27 22:42:42 +01:00
|
|
|
streamFileKey = streamFile->open(streamFile,keyname,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2016-11-27 19:35:26 +01:00
|
|
|
if (streamFileKey) goto found;
|
|
|
|
|
|
|
|
/* "(name.ext)KEY" */
|
|
|
|
/*
|
|
|
|
strcpy(keyname+strlen(keyname)-3,"KEY");
|
2016-11-27 22:42:42 +01:00
|
|
|
streamFileKey = streamFile->open(streamFile,keyname,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2016-11-27 19:35:26 +01:00
|
|
|
if (streamFileKey) goto found;
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* "(.ext)key" */
|
|
|
|
if (path) {
|
2016-11-27 22:42:42 +01:00
|
|
|
strcpy(keyname, filename);
|
|
|
|
keyname[path-filename] = '\0';
|
2016-11-27 19:35:26 +01:00
|
|
|
strcat(keyname, ".");
|
|
|
|
} else {
|
|
|
|
strcpy(keyname, ".");
|
|
|
|
}
|
|
|
|
if (ext) strcat(keyname, ext);
|
|
|
|
strcat(keyname, "key");
|
2016-11-27 22:42:42 +01:00
|
|
|
streamFileKey = streamFile->open(streamFile,keyname,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2016-11-27 19:35:26 +01:00
|
|
|
if (streamFileKey) goto found;
|
|
|
|
|
|
|
|
/* "(.ext)KEY" */
|
|
|
|
/*
|
|
|
|
strcpy(keyname+strlen(keyname)-3,"KEY");
|
2016-11-27 22:42:42 +01:00
|
|
|
streamFileKey = streamFile->open(streamFile,keyname,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2016-11-27 19:35:26 +01:00
|
|
|
if (streamFileKey) goto found;
|
|
|
|
*/
|
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
found:
|
2018-01-20 20:06:15 +01:00
|
|
|
keysize = get_streamfile_size(streamFileKey);
|
|
|
|
if (keysize > bufsize) goto fail;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
2018-01-20 20:06:15 +01:00
|
|
|
if (read_streamfile(buf, 0, keysize, streamFileKey) != keysize)
|
|
|
|
goto fail;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
|
|
|
close_streamfile(streamFileKey);
|
2018-01-20 20:06:15 +01:00
|
|
|
return keysize;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
|
|
|
fail:
|
2018-01-20 20:06:15 +01:00
|
|
|
close_streamfile(streamFileKey);
|
2016-11-27 19:35:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:05:54 +01:00
|
|
|
/**
|
|
|
|
* open file containing looping data and copy to buffer
|
|
|
|
*
|
|
|
|
* returns true if found and copied
|
|
|
|
*/
|
|
|
|
int read_pos_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile) {
|
|
|
|
char posname[PATH_LIMIT];
|
|
|
|
char filename[PATH_LIMIT];
|
|
|
|
/*size_t bytes_read;*/
|
|
|
|
STREAMFILE * streamFilePos= NULL;
|
|
|
|
|
|
|
|
streamFile->get_name(streamFile,filename,sizeof(filename));
|
|
|
|
|
|
|
|
if (strlen(filename)+4 > sizeof(posname)) goto fail;
|
|
|
|
|
|
|
|
/* try to open a posfile using variations: "(name.ext).pos" */
|
|
|
|
{
|
|
|
|
strcpy(posname, filename);
|
|
|
|
strcat(posname, ".pos");
|
|
|
|
streamFilePos = streamFile->open(streamFile,posname,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
|
|
if (streamFilePos) goto found;
|
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
found:
|
|
|
|
//if (get_streamfile_size(streamFilePos) != bufsize) goto fail;
|
|
|
|
|
|
|
|
/* allow pos files to be of different sizes in case of new features, just fill all we can */
|
|
|
|
memset(buf, 0, bufsize);
|
|
|
|
read_streamfile(buf, 0, bufsize, streamFilePos);
|
|
|
|
|
|
|
|
close_streamfile(streamFilePos);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (streamFilePos) close_streamfile(streamFilePos);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-14 00:59:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if the stream filename is one of the extensions (comma-separated, ex. "adx" or "adx,aix")
|
|
|
|
*
|
|
|
|
* returns 0 on failure
|
|
|
|
*/
|
|
|
|
int check_extensions(STREAMFILE *streamFile, const char * cmp_exts) {
|
|
|
|
char filename[PATH_LIMIT];
|
|
|
|
const char * ext = NULL;
|
|
|
|
const char * cmp_ext = NULL;
|
2017-05-27 11:38:09 +02:00
|
|
|
const char * ststr_res = NULL;
|
|
|
|
size_t ext_len, cmp_len;
|
2017-01-14 00:59:54 +01:00
|
|
|
|
|
|
|
streamFile->get_name(streamFile,filename,sizeof(filename));
|
|
|
|
ext = filename_extension(filename);
|
|
|
|
ext_len = strlen(ext);
|
|
|
|
|
|
|
|
cmp_ext = cmp_exts;
|
|
|
|
do {
|
2017-05-27 11:38:09 +02:00
|
|
|
ststr_res = strstr(cmp_ext, ",");
|
|
|
|
cmp_len = ststr_res == NULL
|
|
|
|
? strlen(cmp_ext) /* total length if more not found */
|
|
|
|
: (intptr_t)ststr_res - (intptr_t)cmp_ext; /* find next ext; ststr_res should always be greater than cmp_ext, resulting in a positive cmp_len */
|
2017-03-10 17:57:28 +01:00
|
|
|
|
2017-05-27 11:38:09 +02:00
|
|
|
if (ext_len == cmp_len && strncasecmp(ext,cmp_ext, ext_len) == 0)
|
2017-01-14 00:59:54 +01:00
|
|
|
return 1;
|
2017-03-10 17:57:28 +01:00
|
|
|
|
2017-05-27 11:38:09 +02:00
|
|
|
cmp_ext = ststr_res;
|
2017-01-14 00:59:54 +01:00
|
|
|
if (cmp_ext != NULL)
|
|
|
|
cmp_ext = cmp_ext + 1; /* skip comma */
|
2017-03-10 17:57:28 +01:00
|
|
|
|
2017-01-14 00:59:54 +01:00
|
|
|
} while (cmp_ext != NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-14 01:37:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a chunk starting from an offset, and save its offset/size (if not NULL), with offset after id/size.
|
|
|
|
* Works for chunked headers in the form of "chunk_id chunk_size (data)"xN (ex. RIFF).
|
|
|
|
* The start_offset should be the first actual chunk (not "RIFF" or "WAVE" but "fmt ").
|
|
|
|
* "full_chunk_size" signals chunk_size includes 4+4+data.
|
|
|
|
*
|
|
|
|
* returns 0 on failure
|
|
|
|
*/
|
|
|
|
int find_chunk_be(STREAMFILE *streamFile, uint32_t chunk_id, off_t start_offset, int full_chunk_size, off_t *out_chunk_offset, size_t *out_chunk_size) {
|
2017-04-02 12:23:42 +02:00
|
|
|
return find_chunk(streamFile, chunk_id, start_offset, full_chunk_size, out_chunk_offset, out_chunk_size, 1, 0);
|
2017-01-14 01:37:53 +01:00
|
|
|
}
|
|
|
|
int find_chunk_le(STREAMFILE *streamFile, uint32_t chunk_id, off_t start_offset, int full_chunk_size, off_t *out_chunk_offset, size_t *out_chunk_size) {
|
2017-04-02 12:23:42 +02:00
|
|
|
return find_chunk(streamFile, chunk_id, start_offset, full_chunk_size, out_chunk_offset, out_chunk_size, 0, 0);
|
2017-01-14 01:37:53 +01:00
|
|
|
}
|
2017-04-02 12:23:42 +02:00
|
|
|
int find_chunk(STREAMFILE *streamFile, uint32_t chunk_id, off_t start_offset, int full_chunk_size, off_t *out_chunk_offset, size_t *out_chunk_size, int size_big_endian, int zero_size_end) {
|
2017-01-14 01:37:53 +01:00
|
|
|
size_t filesize;
|
|
|
|
off_t current_chunk = start_offset;
|
|
|
|
|
|
|
|
filesize = get_streamfile_size(streamFile);
|
|
|
|
/* read chunks */
|
|
|
|
while (current_chunk < filesize) {
|
|
|
|
uint32_t chunk_type = read_32bitBE(current_chunk,streamFile);
|
|
|
|
off_t chunk_size = size_big_endian ?
|
|
|
|
read_32bitBE(current_chunk+4,streamFile) :
|
|
|
|
read_32bitLE(current_chunk+4,streamFile);
|
|
|
|
|
|
|
|
if (chunk_type == chunk_id) {
|
|
|
|
if (out_chunk_offset) *out_chunk_offset = current_chunk+8;
|
|
|
|
if (out_chunk_size) *out_chunk_size = chunk_size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-02 12:23:42 +02:00
|
|
|
/* empty chunk with 0 size, seen in some formats (XVAG uses it as end marker, Wwise doesn't) */
|
|
|
|
if (chunk_size == 0 && zero_size_end)
|
2017-02-18 15:19:25 +01:00
|
|
|
return 0;
|
|
|
|
|
2017-01-14 01:37:53 +01:00
|
|
|
current_chunk += full_chunk_size ? chunk_size : 4+4+chunk_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-07-15 11:28:42 +02:00
|
|
|
|
|
|
|
int get_streamfile_name(STREAMFILE *streamFile, char * buffer, size_t size) {
|
|
|
|
streamFile->get_name(streamFile,buffer,size);
|
|
|
|
return 1;
|
|
|
|
}
|
2018-02-25 16:27:48 +01:00
|
|
|
int get_streamfile_filename(STREAMFILE *streamFile, char * buffer, size_t size) {
|
|
|
|
char foldername[PATH_LIMIT];
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
streamFile->get_name(streamFile,foldername,sizeof(foldername));
|
|
|
|
|
|
|
|
//todo Windows CMD accepts both \\ and /, better way to handle this?
|
|
|
|
path = strrchr(foldername,'\\');
|
|
|
|
if (!path)
|
|
|
|
path = strrchr(foldername,'/');
|
|
|
|
if (path != NULL)
|
|
|
|
path = path+1;
|
|
|
|
|
|
|
|
//todo validate sizes and copy sensible max
|
|
|
|
if (path) {
|
|
|
|
strcpy(buffer, path);
|
|
|
|
} else {
|
|
|
|
strcpy(buffer, foldername);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2017-07-15 11:28:42 +02:00
|
|
|
int get_streamfile_path(STREAMFILE *streamFile, char * buffer, size_t size) {
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
streamFile->get_name(streamFile,buffer,size);
|
|
|
|
|
|
|
|
path = strrchr(buffer,DIR_SEPARATOR);
|
|
|
|
if (path!=NULL) path = path+1; /* includes "/" */
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
buffer[path - buffer] = '\0';
|
|
|
|
} else {
|
|
|
|
buffer[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int get_streamfile_ext(STREAMFILE *streamFile, char * filename, size_t size) {
|
|
|
|
streamFile->get_name(streamFile,filename,size);
|
|
|
|
strcpy(filename, filename_extension(filename));
|
|
|
|
return 1;
|
|
|
|
}
|