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
|
|
|
|
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 */
|
2018-08-24 18:48:42 +02:00
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
FILE * infile; /* actual FILE */
|
2018-08-24 18:48:42 +02:00
|
|
|
char name[PATH_LIMIT]; /* FILE filename */
|
|
|
|
off_t offset; /* last read offset (info) */
|
|
|
|
off_t buffer_offset; /* current buffer data start */
|
2017-04-29 16:03:11 +02:00
|
|
|
uint8_t * buffer; /* data buffer */
|
|
|
|
size_t buffersize; /* max buffer size */
|
2018-08-24 18:48:42 +02:00
|
|
|
size_t validsize; /* current buffer size */
|
|
|
|
size_t filesize; /* buffered file size */
|
2019-09-29 20:09:28 +02:00
|
|
|
} STDIO_STREAMFILE;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2019-10-19 11:07:28 +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
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t read_stdio(STDIO_STREAMFILE *streamfile, uint8_t *dst, off_t offset, size_t length) {
|
2018-04-19 22:19:30 +02:00
|
|
|
size_t length_read_total = 0;
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!streamfile->infile || !dst || length <= 0 || offset < 0)
|
2018-04-19 22:19:30 +02:00
|
|
|
return 0;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2019-11-03 17:55:47 +01:00
|
|
|
//;VGM_LOG("STDIO: read %lx + %x (buf %lx + %x)\n", offset, length, streamfile->buffer_offset, streamfile->validsize);
|
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
/* is the part of the requested length in the buffer? */
|
2018-08-24 18:48:42 +02:00
|
|
|
if (offset >= streamfile->buffer_offset && offset < streamfile->buffer_offset + streamfile->validsize) {
|
2018-04-19 22:19:30 +02:00
|
|
|
size_t length_to_read;
|
2018-08-24 18:48:42 +02:00
|
|
|
off_t offset_into_buffer = offset - streamfile->buffer_offset;
|
2017-04-29 16:03:11 +02:00
|
|
|
|
2018-04-19 22:19:30 +02:00
|
|
|
length_to_read = streamfile->validsize - offset_into_buffer;
|
|
|
|
if (length_to_read > length)
|
|
|
|
length_to_read = length;
|
|
|
|
|
2019-11-03 17:55:47 +01:00
|
|
|
//;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);
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
memcpy(dst, streamfile->buffer + offset_into_buffer, length_to_read);
|
2018-04-19 22:19:30 +02:00
|
|
|
length_read_total += length_to_read;
|
|
|
|
length -= length_to_read;
|
|
|
|
offset += length_to_read;
|
2019-10-19 11:07:28 +02:00
|
|
|
dst += length_to_read;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 20:09:28 +02:00
|
|
|
#ifdef VGM_DEBUG_OUTPUT
|
2019-11-03 17:55:47 +01:00
|
|
|
if (offset < streamfile->buffer_offset && length > 0) {
|
2019-09-29 20:09:28 +02:00
|
|
|
VGM_LOG("STDIO: rebuffer, requested %lx vs %lx (sf %x)\n", offset, streamfile->buffer_offset, (uint32_t)streamfile);
|
2019-11-03 17:55:47 +01:00
|
|
|
//streamfile->rebuffer++;
|
|
|
|
//if (rebuffer > N) ...
|
2019-09-29 20:09:28 +02:00
|
|
|
}
|
|
|
|
#endif
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
/* read the rest of the requested length */
|
|
|
|
while (length > 0) {
|
2018-08-24 18:48:42 +02:00
|
|
|
size_t length_to_read;
|
|
|
|
|
|
|
|
/* ignore requests at EOF */
|
|
|
|
if (offset >= streamfile->filesize) {
|
|
|
|
//offset = streamfile->filesize; /* seems fseek doesn't clamp offset */
|
2018-12-01 18:52:30 +01:00
|
|
|
VGM_ASSERT_ONCE(offset > streamfile->filesize, "STDIO: reading over filesize 0x%x @ 0x%x + 0x%x\n", streamfile->filesize, (uint32_t)offset, length);
|
2018-08-24 18:48:42 +02:00
|
|
|
break;
|
2017-01-15 20:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* position to new offset */
|
|
|
|
if (fseeko(streamfile->infile,offset,SEEK_SET)) {
|
2018-08-24 18:48:42 +02:00
|
|
|
break; /* this shouldn't happen in our code */
|
2017-01-15 20:58:47 +01:00
|
|
|
}
|
2018-08-24 18:48:42 +02:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
/* Workaround a bug that appears when compiling with MSVC (later versions).
|
|
|
|
* 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. */
|
|
|
|
fseek(streamfile->infile, ftell(streamfile->infile), SEEK_SET);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* fill the buffer (offset now is beyond buffer_offset) */
|
|
|
|
streamfile->buffer_offset = offset;
|
2019-10-19 11:07:28 +02:00
|
|
|
streamfile->validsize = fread(streamfile->buffer, sizeof(uint8_t), streamfile->buffersize, streamfile->infile);
|
2019-11-03 17:55:47 +01:00
|
|
|
//;VGM_LOG("STDIO: read buf %lx + %x\n", streamfile->buffer_offset, streamfile->validsize);
|
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
|
|
|
|
2018-08-24 18:48:42 +02:00
|
|
|
/* give up on partial reads (EOF) */
|
|
|
|
if (streamfile->validsize < length_to_read) {
|
2019-10-19 11:07:28 +02:00
|
|
|
memcpy(dst, streamfile->buffer, streamfile->validsize);
|
2018-08-24 18:48:42 +02:00
|
|
|
offset += streamfile->validsize;
|
|
|
|
length_read_total += streamfile->validsize;
|
|
|
|
break;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* use the new buffer */
|
2019-10-19 11:07:28 +02:00
|
|
|
memcpy(dst, streamfile->buffer, length_to_read);
|
2018-08-24 18:48:42 +02:00
|
|
|
offset += length_to_read;
|
2017-04-29 16:03:11 +02:00
|
|
|
length_read_total += length_to_read;
|
|
|
|
length -= length_to_read;
|
2019-10-19 11:07:28 +02:00
|
|
|
dst += length_to_read;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 18:48:42 +02:00
|
|
|
streamfile->offset = offset; /* last fread offset */
|
2008-08-02 05:15:24 +02:00
|
|
|
return length_read_total;
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t get_size_stdio(STDIO_STREAMFILE *streamfile) {
|
2017-01-15 20:58:47 +01:00
|
|
|
return streamfile->filesize;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
2019-09-29 20:09:28 +02:00
|
|
|
static off_t get_offset_stdio(STDIO_STREAMFILE *streamfile) {
|
2018-08-19 22:31:12 +02:00
|
|
|
return streamfile->offset;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
static void get_name_stdio(STDIO_STREAMFILE *streamfile, char *buffer, size_t length) {
|
|
|
|
strncpy(buffer, streamfile->name, length);
|
2012-08-21 17:34:10 +02:00
|
|
|
buffer[length-1]='\0';
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
static void close_stdio(STDIO_STREAMFILE *streamfile) {
|
2019-09-15 15:47:41 +02:00
|
|
|
if (streamfile->infile)
|
|
|
|
fclose(streamfile->infile);
|
2018-08-24 18:48:42 +02:00
|
|
|
free(streamfile->buffer);
|
|
|
|
free(streamfile);
|
|
|
|
}
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
static STREAMFILE* open_stdio(STDIO_STREAMFILE *streamfile, const char * const filename, size_t buffersize) {
|
2008-08-02 05:15:24 +02:00
|
|
|
if (!filename)
|
|
|
|
return NULL;
|
2019-09-29 18:25:24 +02:00
|
|
|
|
2018-03-30 07:22:03 +02:00
|
|
|
#if !defined (__ANDROID__)
|
2019-11-03 17:55:47 +01:00
|
|
|
/* if same name, duplicate the file descriptor we already have open */
|
2019-10-19 11:07:28 +02:00
|
|
|
if (streamfile->infile && !strcmp(streamfile->name,filename)) {
|
2019-11-03 17:55:47 +01:00
|
|
|
int new_fd;
|
|
|
|
FILE *new_file = NULL;
|
2019-09-29 18:25:24 +02:00
|
|
|
|
2019-11-03 17:55:47 +01:00
|
|
|
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_sf)
|
2019-10-19 11:07:28 +02:00
|
|
|
return new_sf;
|
2019-11-03 17:55:47 +01:00
|
|
|
fclose(new_file);
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
2019-11-03 17:55:47 +01:00
|
|
|
if (new_fd >= 0 && !new_file)
|
|
|
|
close(new_fd); /* fdopen may fail when opening too many files */
|
|
|
|
|
|
|
|
/* on failure just close and try the default path (which will probably fail a second time) */
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
2018-03-30 07:22:03 +02:00
|
|
|
#endif
|
2008-08-02 05:15:24 +02:00
|
|
|
// a normal open, open a new file
|
2019-10-19 11:07:28 +02:00
|
|
|
return open_stdio_streamfile_buffer(filename, buffersize);
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
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;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
|
|
|
buffer = calloc(buffersize,1);
|
2018-08-02 17:17:58 +02:00
|
|
|
if (!buffer) goto fail;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2019-09-29 20:09:28 +02:00
|
|
|
streamfile = calloc(1,sizeof(STDIO_STREAMFILE));
|
2018-08-02 17:17:58 +02:00
|
|
|
if (!streamfile) goto fail;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
streamfile->infile = infile;
|
|
|
|
streamfile->buffersize = buffersize;
|
|
|
|
streamfile->buffer = buffer;
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
strncpy(streamfile->name, filename, sizeof(streamfile->name));
|
2012-08-21 17:34:10 +02:00
|
|
|
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 */
|
2019-09-15 15:47:41 +02:00
|
|
|
if (infile) {
|
|
|
|
fseeko(streamfile->infile,0,SEEK_END);
|
|
|
|
streamfile->filesize = ftello(streamfile->infile);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
streamfile->filesize = 0; /* allow virtual, non-existing files */
|
|
|
|
}
|
2017-01-15 20:58:47 +01:00
|
|
|
|
2018-09-23 03:09:46 +02:00
|
|
|
/* Typically fseek(o)/ftell(o) may only handle up to ~2.14GB, signed 32b = 0x7FFFFFFF
|
|
|
|
* (happens in banks like FSB, though rarely). Can be remedied with the
|
|
|
|
* preprocessor (-D_FILE_OFFSET_BITS=64 in GCC) but it's not well tested. */
|
2018-08-02 17:17:58 +02:00
|
|
|
if (streamfile->filesize == 0xFFFFFFFF) { /* -1 on error */
|
|
|
|
VGM_LOG("STREAMFILE: ftell error\n");
|
|
|
|
goto fail; /* can be ignored but may result in strange/unexpected behaviors */
|
|
|
|
}
|
|
|
|
|
2008-08-02 05:15:24 +02:00
|
|
|
return &streamfile->sf;
|
2018-08-02 17:17:58 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
free(buffer);
|
|
|
|
free(streamfile);
|
|
|
|
return NULL;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
static STREAMFILE* open_stdio_streamfile_buffer(const char * const filename, size_t bufsize) {
|
|
|
|
FILE *infile = NULL;
|
|
|
|
STREAMFILE *streamfile = NULL;
|
2008-08-02 05:15:24 +02:00
|
|
|
|
|
|
|
infile = fopen(filename,"rb");
|
2019-09-15 15:47:41 +02:00
|
|
|
if (!infile) {
|
|
|
|
/* allow non-existing files in some cases */
|
|
|
|
if (!vgmstream_is_virtual_filename(filename))
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-02 05:15:24 +02:00
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
streamfile = open_stdio_streamfile_buffer_by_file(infile, filename, bufsize);
|
|
|
|
if (!streamfile) {
|
2019-09-15 15:47:41 +02:00
|
|
|
if (infile) fclose(infile);
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
return streamfile;
|
2008-08-02 05:15:24 +02:00
|
|
|
}
|
2008-07-21 01:28:16 +02:00
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_stdio_streamfile(const char *filename) {
|
|
|
|
return open_stdio_streamfile_buffer(filename, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2017-08-13 19:58:01 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_stdio_streamfile_by_file(FILE *file, const char *filename) {
|
|
|
|
return open_stdio_streamfile_buffer_by_file(file, filename, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2017-08-13 19:58:01 +02:00
|
|
|
}
|
|
|
|
|
2018-01-20 00:54:08 +01:00
|
|
|
/* **************************************************** */
|
|
|
|
|
2018-04-19 22:19:30 +02:00
|
|
|
typedef struct {
|
|
|
|
STREAMFILE sf;
|
|
|
|
|
|
|
|
STREAMFILE *inner_sf;
|
2018-08-19 22:31:12 +02:00
|
|
|
off_t offset; /* last read offset (info) */
|
|
|
|
off_t buffer_offset; /* current buffer data start */
|
2018-04-19 22:19:30 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t buffer_read(BUFFER_STREAMFILE *streamfile, uint8_t *dst, off_t offset, size_t length) {
|
2018-04-19 22:19:30 +02:00
|
|
|
size_t length_read_total = 0;
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!dst || length <= 0 || offset < 0)
|
2018-04-19 22:19:30 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* is the part of the requested length in the buffer? */
|
2018-08-19 22:31:12 +02:00
|
|
|
if (offset >= streamfile->buffer_offset && offset < streamfile->buffer_offset + streamfile->validsize) {
|
2018-04-19 22:19:30 +02:00
|
|
|
size_t length_to_read;
|
2018-08-19 22:31:12 +02:00
|
|
|
off_t offset_into_buffer = offset - streamfile->buffer_offset;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
|
|
|
length_to_read = streamfile->validsize - offset_into_buffer;
|
|
|
|
if (length_to_read > length)
|
|
|
|
length_to_read = length;
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
memcpy(dst, streamfile->buffer + offset_into_buffer, length_to_read);
|
2018-04-19 22:19:30 +02:00
|
|
|
length_read_total += length_to_read;
|
|
|
|
length -= length_to_read;
|
|
|
|
offset += length_to_read;
|
2019-10-19 11:07:28 +02:00
|
|
|
dst += length_to_read;
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 20:09:28 +02:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-19 22:19:30 +02:00
|
|
|
|
|
|
|
/* read the rest of the requested length */
|
|
|
|
while (length > 0) {
|
2018-08-19 22:31:12 +02:00
|
|
|
size_t length_to_read;
|
2018-04-19 22:19:30 +02:00
|
|
|
|
2018-08-19 22:31:12 +02:00
|
|
|
/* ignore requests at EOF */
|
|
|
|
if (offset >= streamfile->filesize) {
|
2018-08-24 18:48:42 +02:00
|
|
|
//offset = streamfile->filesize; /* seems fseek doesn't clamp offset */
|
2018-12-01 18:52:30 +01:00
|
|
|
VGM_ASSERT_ONCE(offset > streamfile->filesize, "BUFFER: reading over filesize 0x%x @ 0x%x + 0x%x\n", streamfile->filesize, (uint32_t)offset, length);
|
2018-08-19 22:31:12 +02:00
|
|
|
break;
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 18:48:42 +02:00
|
|
|
/* fill the buffer (offset now is beyond buffer_offset) */
|
2018-08-19 22:31:12 +02:00
|
|
|
streamfile->buffer_offset = offset;
|
|
|
|
streamfile->validsize = streamfile->inner_sf->read(streamfile->inner_sf, streamfile->buffer, streamfile->buffer_offset, streamfile->buffersize);
|
2018-04-19 22:19:30 +02:00
|
|
|
|
|
|
|
/* decide how much must be read this time */
|
|
|
|
if (length > streamfile->buffersize)
|
|
|
|
length_to_read = streamfile->buffersize;
|
|
|
|
else
|
|
|
|
length_to_read = length;
|
|
|
|
|
2018-08-19 22:31:12 +02:00
|
|
|
/* give up on partial reads (EOF) */
|
|
|
|
if (streamfile->validsize < length_to_read) {
|
2019-10-19 11:07:28 +02:00
|
|
|
memcpy(dst, streamfile->buffer, streamfile->validsize);
|
2018-08-19 22:31:12 +02:00
|
|
|
offset += streamfile->validsize;
|
|
|
|
length_read_total += streamfile->validsize;
|
|
|
|
break;
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* use the new buffer */
|
2019-10-19 11:07:28 +02:00
|
|
|
memcpy(dst, streamfile->buffer, length_to_read);
|
2018-08-19 22:31:12 +02:00
|
|
|
offset += length_to_read;
|
2018-04-19 22:19:30 +02:00
|
|
|
length_read_total += length_to_read;
|
|
|
|
length -= length_to_read;
|
2019-10-19 11:07:28 +02:00
|
|
|
dst += length_to_read;
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
|
|
|
|
2018-08-19 22:31:12 +02:00
|
|
|
streamfile->offset = offset; /* last fread offset */
|
2018-04-19 22:19:30 +02:00
|
|
|
return length_read_total;
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t buffer_get_size(BUFFER_STREAMFILE *streamfile) {
|
2018-04-19 22:19:30 +02:00
|
|
|
return streamfile->filesize; /* cache */
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t buffer_get_offset(BUFFER_STREAMFILE *streamfile) {
|
2018-08-19 22:31:12 +02:00
|
|
|
return streamfile->offset; /* cache */
|
2018-04-19 22:19:30 +02:00
|
|
|
}
|
|
|
|
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 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);
|
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_buffer_streamfile(STREAMFILE *streamfile, size_t buffer_size) {
|
2018-04-19 22:19:30 +02:00
|
|
|
BUFFER_STREAMFILE *this_sf = NULL;
|
|
|
|
|
|
|
|
if (!streamfile) goto fail;
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
this_sf = calloc(1, sizeof(BUFFER_STREAMFILE));
|
2018-04-19 22:19:30 +02:00
|
|
|
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->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;
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_buffer_streamfile_f(STREAMFILE *streamfile, size_t buffer_size) {
|
|
|
|
STREAMFILE *new_sf = open_buffer_streamfile(streamfile, buffer_size);
|
|
|
|
if (!new_sf)
|
|
|
|
close_streamfile(streamfile);
|
|
|
|
return new_sf;
|
|
|
|
}
|
2018-04-19 22:19:30 +02:00
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
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;
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t wrap_read(WRAP_STREAMFILE *streamfile, uint8_t *dst, off_t offset, size_t length) {
|
|
|
|
return streamfile->inner_sf->read(streamfile->inner_sf, dst, offset, length); /* default */
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t wrap_get_size(WRAP_STREAMFILE *streamfile) {
|
2018-01-20 00:54:08 +01:00
|
|
|
return streamfile->inner_sf->get_size(streamfile->inner_sf); /* default */
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t wrap_get_offset(WRAP_STREAMFILE *streamfile) {
|
2018-01-20 00:54:08 +01:00
|
|
|
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_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);
|
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_wrap_streamfile(STREAMFILE *streamfile) {
|
|
|
|
WRAP_STREAMFILE *this_sf = NULL;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
|
|
|
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.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;
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_wrap_streamfile_f(STREAMFILE *streamfile) {
|
|
|
|
STREAMFILE *new_sf = open_wrap_streamfile(streamfile);
|
|
|
|
if (!new_sf)
|
|
|
|
close_streamfile(streamfile);
|
|
|
|
return new_sf;
|
|
|
|
}
|
2018-01-20 00:54:08 +01:00
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
STREAMFILE sf;
|
|
|
|
|
|
|
|
STREAMFILE *inner_sf;
|
|
|
|
off_t start;
|
|
|
|
size_t size;
|
|
|
|
} CLAMP_STREAMFILE;
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t clamp_read(CLAMP_STREAMFILE *streamfile, uint8_t *dst, off_t offset, size_t length) {
|
2018-01-20 00:54:08 +01:00
|
|
|
off_t inner_offset = streamfile->start + offset;
|
|
|
|
size_t clamp_length = length > (streamfile->size - offset) ? (streamfile->size - offset) : length;
|
2019-10-19 11:07:28 +02:00
|
|
|
return streamfile->inner_sf->read(streamfile->inner_sf, dst, inner_offset, clamp_length);
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
|
|
|
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 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];
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE *new_inner_sf = NULL;
|
2018-01-27 11:07:45 +01:00
|
|
|
|
|
|
|
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 {
|
2019-10-19 11:07:28 +02: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);
|
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_clamp_streamfile(STREAMFILE *streamfile, off_t start, size_t size) {
|
|
|
|
CLAMP_STREAMFILE *this_sf = NULL;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!streamfile || size == 0) 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.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;
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_clamp_streamfile_f(STREAMFILE *streamfile, off_t start, size_t size) {
|
|
|
|
STREAMFILE *new_sf = open_clamp_streamfile(streamfile, start, size);
|
|
|
|
if (!new_sf)
|
|
|
|
close_streamfile(streamfile);
|
|
|
|
return new_sf;
|
|
|
|
}
|
2018-01-20 00:54:08 +01:00
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
STREAMFILE sf;
|
|
|
|
|
|
|
|
STREAMFILE *inner_sf;
|
2018-03-30 18:10:23 +02:00
|
|
|
void* data; /* state for custom reads, malloc'ed + copied on open (to re-open streamfiles cleanly) */
|
2018-01-20 00:54:08 +01:00
|
|
|
size_t data_size;
|
2018-03-30 18:10:23 +02:00
|
|
|
size_t (*read_callback)(STREAMFILE *, uint8_t *, off_t, size_t, void*); /* custom read to modify data before copying into buffer */
|
|
|
|
size_t (*size_callback)(STREAMFILE *, void*); /* size when custom reads make data smaller/bigger than underlying streamfile */
|
2020-03-07 10:15:02 +01:00
|
|
|
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 */
|
2018-01-20 00:54:08 +01:00
|
|
|
} IO_STREAMFILE;
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t io_read(IO_STREAMFILE *streamfile, uint8_t *dst, off_t offset, size_t length) {
|
|
|
|
return streamfile->read_callback(streamfile->inner_sf, dst, offset, length, streamfile->data);
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
|
|
|
static size_t io_get_size(IO_STREAMFILE *streamfile) {
|
2018-03-30 18:10:23 +02:00
|
|
|
if (streamfile->size_callback)
|
|
|
|
return streamfile->size_callback(streamfile->inner_sf, streamfile->data);
|
|
|
|
else
|
|
|
|
return streamfile->inner_sf->get_size(streamfile->inner_sf); /* default */
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
|
|
|
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 */
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
static STREAMFILE* io_open(IO_STREAMFILE *streamfile, const char * const filename, size_t buffersize) {
|
2018-01-20 00:54:08 +01:00
|
|
|
STREAMFILE *new_inner_sf = streamfile->inner_sf->open(streamfile->inner_sf,filename,buffersize);
|
2020-03-07 10:15:02 +01:00
|
|
|
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);
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
|
|
|
static void io_close(IO_STREAMFILE *streamfile) {
|
2020-03-07 10:15:02 +01:00
|
|
|
if (streamfile->close_callback)
|
|
|
|
streamfile->close_callback(streamfile->inner_sf, streamfile->data);
|
2018-01-20 00:54:08 +01:00
|
|
|
streamfile->inner_sf->close(streamfile->inner_sf);
|
|
|
|
free(streamfile->data);
|
|
|
|
free(streamfile);
|
|
|
|
}
|
|
|
|
|
2020-03-07 10:15:02 +01:00
|
|
|
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) {
|
2019-10-19 11:07:28 +02:00
|
|
|
IO_STREAMFILE *this_sf = NULL;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
2020-03-07 10:15:02 +01:00
|
|
|
if (!streamfile) goto fail;
|
|
|
|
if ((data && !data_size) || (!data && data_size)) goto fail;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
|
|
|
this_sf = calloc(1,sizeof(IO_STREAMFILE));
|
2020-03-07 10:15:02 +01:00
|
|
|
if (!this_sf) goto fail;
|
2018-01-20 00:54:08 +01:00
|
|
|
|
|
|
|
/* 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->inner_sf = streamfile;
|
|
|
|
if (data) {
|
|
|
|
this_sf->data = malloc(data_size);
|
2020-03-07 10:15:02 +01:00
|
|
|
if (!this_sf->data) goto fail;
|
2018-01-20 00:54:08 +01:00
|
|
|
memcpy(this_sf->data, data, data_size);
|
|
|
|
}
|
|
|
|
this_sf->data_size = data_size;
|
|
|
|
this_sf->read_callback = read_callback;
|
2018-03-30 18:10:23 +02:00
|
|
|
this_sf->size_callback = size_callback;
|
2020-03-07 10:15:02 +01:00
|
|
|
this_sf->init_callback = init_callback;
|
|
|
|
this_sf->close_callback = close_callback;
|
|
|
|
|
|
|
|
if (this_sf->init_callback) {
|
|
|
|
int ok = this_sf->init_callback(this_sf->inner_sf, this_sf->data);
|
|
|
|
if (ok < 0) goto fail;
|
|
|
|
}
|
2018-01-20 00:54:08 +01:00
|
|
|
|
|
|
|
return &this_sf->sf;
|
2020-03-07 10:15:02 +01:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (this_sf) free(this_sf->data);
|
|
|
|
free(this_sf);
|
|
|
|
return NULL;
|
2018-01-20 00:54:08 +01:00
|
|
|
}
|
2020-03-07 10:15:02 +01:00
|
|
|
|
|
|
|
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);
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!new_sf)
|
|
|
|
close_streamfile(streamfile);
|
|
|
|
return new_sf;
|
|
|
|
}
|
2017-08-13 19:58:01 +02:00
|
|
|
|
2020-03-07 10:15:02 +01:00
|
|
|
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_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);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t fakename_read(FAKENAME_STREAMFILE *streamfile, uint8_t *dst, off_t offset, size_t length) {
|
|
|
|
return streamfile->inner_sf->read(streamfile->inner_sf, dst, offset, length); /* default */
|
2018-01-27 11:07:45 +01:00
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t fakename_get_size(FAKENAME_STREAMFILE *streamfile) {
|
2018-01-27 11:07:45 +01:00
|
|
|
return streamfile->inner_sf->get_size(streamfile->inner_sf); /* default */
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t fakename_get_offset(FAKENAME_STREAMFILE *streamfile) {
|
2018-01-27 11:07:45 +01:00
|
|
|
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';
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
static STREAMFILE* fakename_open(FAKENAME_STREAMFILE *streamfile, const char * const filename, size_t buffersize) {
|
2018-01-27 11:07:45 +01:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_fakename_streamfile(STREAMFILE *streamfile, const char *fakename, const char *fakeext) {
|
|
|
|
FAKENAME_STREAMFILE *this_sf = NULL;
|
2018-01-27 11:07:45 +01:00
|
|
|
|
|
|
|
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.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
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
|
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;
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_fakename_streamfile_f(STREAMFILE *streamfile, const char *fakename, const char *fakeext) {
|
|
|
|
STREAMFILE *new_sf = open_fakename_streamfile(streamfile, fakename, fakeext);
|
|
|
|
if (!new_sf)
|
|
|
|
close_streamfile(streamfile);
|
|
|
|
return new_sf;
|
|
|
|
}
|
2018-01-27 11:07:45 +01:00
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
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;
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
static size_t multifile_read(MULTIFILE_STREAMFILE *streamfile, uint8_t *dst, off_t offset, size_t length) {
|
2018-01-27 13:58:46 +01:00
|
|
|
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 */
|
2019-10-19 11:07:28 +02:00
|
|
|
done += streamfile->inner_sfs[segment]->read(streamfile->inner_sfs[segment], dst + done, segment_offset, length - done);
|
2018-01-27 13:58:46 +01:00
|
|
|
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);
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
static STREAMFILE* multifile_open(MULTIFILE_STREAMFILE *streamfile, const char * const filename, size_t buffersize) {
|
2018-01-27 13:58:46 +01:00
|
|
|
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;
|
|
|
|
|
2019-09-29 18:25:24 +02:00
|
|
|
free(new_inner_sfs);
|
2018-01-27 13:58:46 +01:00
|
|
|
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++) {
|
2018-03-25 19:59:58 +02:00
|
|
|
for (i = 0; i < streamfile->inner_sfs_size; i++) {
|
2018-01-27 13:58:46 +01:00
|
|
|
close_streamfile(streamfile->inner_sfs[i]);
|
2018-03-25 19:59:58 +02:00
|
|
|
}
|
2018-01-27 13:58:46 +01:00
|
|
|
}
|
|
|
|
free(streamfile->inner_sfs);
|
|
|
|
free(streamfile->sizes);
|
|
|
|
free(streamfile);
|
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_multifile_streamfile(STREAMFILE **streamfiles, size_t streamfiles_size) {
|
|
|
|
MULTIFILE_STREAMFILE *this_sf = NULL;
|
2018-01-27 13:58:46 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!streamfiles || !streamfiles_size) return NULL;
|
2019-10-19 11:07:28 +02:00
|
|
|
|
2018-01-27 13:58:46 +01:00
|
|
|
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.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;
|
|
|
|
}
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_multifile_streamfile_f(STREAMFILE **streamfiles, size_t streamfiles_size) {
|
|
|
|
STREAMFILE *new_sf = open_multifile_streamfile(streamfiles, streamfiles_size);
|
|
|
|
if (!new_sf) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < streamfiles_size; i++) {
|
|
|
|
close_streamfile(streamfiles[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new_sf;
|
|
|
|
}
|
2018-01-27 13:58:46 +01:00
|
|
|
|
|
|
|
/* **************************************************** */
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_streamfile(STREAMFILE *streamfile, const char *pathname) {
|
|
|
|
return streamfile->open(streamfile, pathname, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2018-08-04 20:42:00 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_streamfile_by_ext(STREAMFILE *streamfile, const char *ext) {
|
2019-08-02 17:30:23 +02:00
|
|
|
char filename[PATH_LIMIT];
|
|
|
|
int filename_len, fileext_len;
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
streamfile->get_name(streamfile, filename, sizeof(filename));
|
2018-03-29 22:34:21 +02:00
|
|
|
|
2019-08-02 17:30:23 +02:00
|
|
|
filename_len = strlen(filename);
|
|
|
|
fileext_len = strlen(filename_extension(filename));
|
2018-03-29 22:34:21 +02:00
|
|
|
|
2019-08-02 17:30:23 +02:00
|
|
|
if (fileext_len == 0) {/* extensionless */
|
|
|
|
strcat(filename,".");
|
|
|
|
strcat(filename,ext);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcpy(filename + filename_len - fileext_len, ext);
|
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
return streamfile->open(streamfile, filename, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2018-03-29 22:34:21 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* open_streamfile_by_filename(STREAMFILE *streamfile, const char * filename) {
|
2019-09-24 00:51:12 +02:00
|
|
|
char fullname[PATH_LIMIT];
|
|
|
|
char partname[PATH_LIMIT];
|
|
|
|
char *path, *name;
|
2018-03-29 22:34:21 +02:00
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!streamfile || !filename) return NULL;
|
|
|
|
|
|
|
|
streamfile->get_name(streamfile, fullname, sizeof(fullname));
|
2018-03-29 22:34:21 +02:00
|
|
|
|
2019-09-24 00:51:12 +02:00
|
|
|
//todo normalize separators in a better way, safeops, improve copying
|
|
|
|
path = strrchr(fullname,DIR_SEPARATOR);
|
2018-03-29 22:34:21 +02:00
|
|
|
if (path) {
|
2019-09-24 00:51:12 +02:00
|
|
|
path[1] = '\0'; /* remove name after separator */
|
|
|
|
|
|
|
|
strcpy(partname, filename);
|
|
|
|
fix_dir_separators(partname);
|
|
|
|
|
|
|
|
/* normalize relative paths as don't work ok in some plugins */
|
|
|
|
if (partname[0]=='.' && partname[1] == DIR_SEPARATOR) { /* './name' */
|
|
|
|
name = partname + 2; /* ignore './' */
|
|
|
|
}
|
|
|
|
else if (partname[0]=='.' && partname[1]=='.' && partname[2] == DIR_SEPARATOR) { /* '../name' */
|
|
|
|
char *pathprev;
|
|
|
|
|
|
|
|
path[0] = '\0'; /* remove last separator so next call works */
|
|
|
|
pathprev = strrchr(fullname,DIR_SEPARATOR);
|
|
|
|
if (pathprev) {
|
|
|
|
pathprev[1] = '\0'; /* remove prev dir after separator */
|
|
|
|
name = partname + 3; /* ignore '../' */
|
|
|
|
}
|
|
|
|
else { /* let plugin handle? */
|
|
|
|
path[0] = DIR_SEPARATOR;
|
|
|
|
name = partname;
|
|
|
|
}
|
|
|
|
/* could work with more relative paths but whatevs */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
name = partname;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcat(fullname, name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcpy(fullname, filename);
|
2018-03-29 22:34:21 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
return streamfile->open(streamfile, fullname, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2018-03-29 22:34:21 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
STREAMFILE* reopen_streamfile(STREAMFILE *streamfile, size_t buffer_size) {
|
2019-01-01 23:21:08 +01:00
|
|
|
char pathname[PATH_LIMIT];
|
|
|
|
|
2019-10-19 11:07:28 +02:00
|
|
|
if (!streamfile) return NULL;
|
|
|
|
|
2019-01-01 23:21:08 +01:00
|
|
|
if (buffer_size == 0)
|
|
|
|
buffer_size = STREAMFILE_DEFAULT_BUFFER_SIZE;
|
2019-10-19 11:07:28 +02:00
|
|
|
streamfile->get_name(streamfile,pathname,sizeof(pathname));
|
|
|
|
return streamfile->open(streamfile,pathname,buffer_size);
|
2019-01-01 23:21:08 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
/* **************************************************** */
|
2018-03-29 22:34:21 +02:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
size_t read_line(char *buf, int buf_size, off_t offset, STREAMFILE *sf, int *p_line_ok) {
|
2008-07-21 01:28:16 +02:00
|
|
|
int i;
|
2019-10-20 11:55:05 +02:00
|
|
|
off_t file_size = get_streamfile_size(sf);
|
2017-11-23 22:53:43 +01:00
|
|
|
int extra_bytes = 0; /* how many bytes over those put in the buffer were read */
|
2008-07-21 01:28:16 +02:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
if (p_line_ok) *p_line_ok = 0;
|
2008-07-21 01:28:16 +02:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
for (i = 0; i < buf_size-1 && offset+i < file_size; i++) {
|
|
|
|
char in_char = read_8bit(offset+i, sf);
|
2008-07-21 01:28:16 +02:00
|
|
|
/* check for end of line */
|
2019-10-20 11:55:05 +02:00
|
|
|
if (in_char == 0x0d && read_8bit(offset+i+1, sf) == 0x0a) { /* CRLF */
|
2008-07-21 01:28:16 +02:00
|
|
|
extra_bytes = 2;
|
2019-10-20 11:55:05 +02:00
|
|
|
if (p_line_ok) *p_line_ok = 1;
|
2008-07-21 01:28:16 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-11-23 22:53:43 +01:00
|
|
|
else if (in_char == 0x0d || in_char == 0x0a) { /* CR or LF */
|
|
|
|
extra_bytes = 1;
|
2019-10-20 11:55:05 +02:00
|
|
|
if (p_line_ok) *p_line_ok = 1;
|
2017-11-23 22:53:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2008-07-21 01:28:16 +02:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
buf[i] = in_char;
|
2008-07-21 01:28:16 +02:00
|
|
|
}
|
2017-11-23 22:53:43 +01:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
buf[i] = '\0';
|
2008-07-21 01:28:16 +02:00
|
|
|
|
|
|
|
/* did we fill the buffer? */
|
2019-10-20 11:55:05 +02:00
|
|
|
if (i == buf_size) {
|
|
|
|
char in_char = read_8bit(offset+i, sf);
|
2008-07-21 01:28:16 +02:00
|
|
|
/* did the bytes we missed just happen to be the end of the line? */
|
2019-10-20 11:55:05 +02:00
|
|
|
if (in_char == 0x0d && read_8bit(offset+i+1, sf) == 0x0a) { /* CRLF */
|
2008-07-21 01:28:16 +02:00
|
|
|
extra_bytes = 2;
|
2019-10-20 11:55:05 +02:00
|
|
|
if (p_line_ok) *p_line_ok = 1;
|
2017-11-23 22:53:43 +01:00
|
|
|
}
|
|
|
|
else if (in_char == 0x0d || in_char == 0x0a) { /* CR or LF */
|
|
|
|
extra_bytes = 1;
|
2019-10-20 11:55:05 +02:00
|
|
|
if (p_line_ok) *p_line_ok = 1;
|
2008-07-21 01:28:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* did we hit the file end? */
|
2019-10-20 11:55:05 +02:00
|
|
|
if (offset+i == file_size) {
|
2008-07-21 01:28:16 +02:00
|
|
|
/* then we did in fact finish reading the last line */
|
2019-10-20 11:55:05 +02:00
|
|
|
if (p_line_ok) *p_line_ok = 1;
|
2008-07-21 01:28:16 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
size_t read_string(char *buf, size_t buf_size, off_t offset, STREAMFILE *sf) {
|
2018-03-29 20:42:52 +02:00
|
|
|
size_t pos;
|
2017-08-12 11:14:16 +02:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
for (pos = 0; pos < buf_size; pos++) {
|
|
|
|
char c = read_8bit(offset + pos, sf);
|
2018-03-29 20:42:52 +02:00
|
|
|
if (buf) buf[pos] = c;
|
2017-08-12 11:14:16 +02:00
|
|
|
if (c == '\0')
|
2018-03-29 20:42:52 +02:00
|
|
|
return pos;
|
2019-10-20 11:55:05 +02:00
|
|
|
if (pos+1 == buf_size) { /* null at maxsize and don't validate (expected to be garbage) */
|
2018-03-29 20:42:52 +02:00
|
|
|
if (buf) buf[pos] = '\0';
|
2019-10-20 11:55:05 +02:00
|
|
|
return buf_size;
|
2017-08-12 11:14:16 +02:00
|
|
|
}
|
2019-08-15 22:15:37 +02:00
|
|
|
if (c < 0x20 || (uint8_t)c > 0xA5)
|
2017-08-12 11:14:16 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (buf) buf[0] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
2020-04-05 22:07:51 +02:00
|
|
|
|
|
|
|
size_t read_string_utf16(char *buf, size_t buf_size, off_t offset, STREAMFILE *sf, int big_endian) {
|
2019-11-03 22:56:37 +01:00
|
|
|
size_t pos, offpos;
|
2020-04-05 22:07:51 +02:00
|
|
|
uint16_t (*read_u16)(off_t,STREAMFILE*) = big_endian ? read_u16be : read_u16le;
|
|
|
|
|
2019-11-03 22:56:37 +01:00
|
|
|
|
|
|
|
for (pos = 0, offpos = 0; pos < buf_size; pos++, offpos += 2) {
|
2020-04-05 22:07:51 +02:00
|
|
|
char c = read_u16(offset + offpos, sf) & 0xFF; /* lower byte for now */
|
2019-11-03 22:56:37 +01:00
|
|
|
if (buf) buf[pos] = c;
|
|
|
|
if (c == '\0')
|
|
|
|
return pos;
|
|
|
|
if (pos+1 == buf_size) { /* null at maxsize and don't validate (expected to be garbage) */
|
|
|
|
if (buf) buf[pos] = '\0';
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
if (c < 0x20 || (uint8_t)c > 0xA5)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (buf) buf[0] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:07:51 +02:00
|
|
|
size_t read_string_utf16le(char* buf, size_t buf_size, off_t offset, STREAMFILE* sf) {
|
|
|
|
return read_string_utf16(buf, buf_size, offset, sf, 0);
|
|
|
|
}
|
|
|
|
size_t read_string_utf16be(char* buf, size_t buf_size, off_t offset, STREAMFILE* sf) {
|
|
|
|
return read_string_utf16(buf, buf_size, offset, sf, 1);
|
|
|
|
}
|
|
|
|
|
2017-08-12 11:14:16 +02:00
|
|
|
|
2017-11-05 17:05:39 +01:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
size_t read_key_file(uint8_t *buf, size_t buf_size, STREAMFILE *sf) {
|
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
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
sf->get_name(sf,filename,sizeof(filename));
|
2016-11-27 22:42:42 +01:00
|
|
|
|
|
|
|
if (strlen(filename)+4 > sizeof(keyname)) goto fail;
|
2016-11-27 19:35:26 +01:00
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
/* try to open a keyfile using variations */
|
2016-11-27 19:35:26 +01:00
|
|
|
{
|
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");
|
2019-10-20 11:55:05 +02:00
|
|
|
streamFileKey = sf->open(sf,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");
|
2019-10-20 11:55:05 +02:00
|
|
|
streamFileKey = sf->open(sf,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);
|
2019-10-20 11:55:05 +02:00
|
|
|
if (keysize > buf_size) 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;
|
|
|
|
}
|
|
|
|
|
2018-11-24 00:44:17 +01:00
|
|
|
void fix_dir_separators(char * filename) {
|
|
|
|
char c;
|
|
|
|
int i = 0;
|
|
|
|
while ((c = filename[i]) != '\0') {
|
|
|
|
if ((c == '\\' && DIR_SEPARATOR == '/') || (c == '/' && DIR_SEPARATOR == '\\'))
|
|
|
|
filename[i] = DIR_SEPARATOR;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
int check_extensions(STREAMFILE *sf, const char * cmp_exts) {
|
2017-01-14 00:59:54 +01:00
|
|
|
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
|
|
|
|
2019-10-20 11:55:05 +02:00
|
|
|
sf->get_name(sf,filename,sizeof(filename));
|
2017-01-14 00:59:54 +01:00
|
|
|
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
|
|
|
|
*/
|
2019-09-08 20:23:59 +02:00
|
|
|
static int find_chunk_internal(STREAMFILE *streamFile, uint32_t chunk_id, off_t start_offset, size_t max_size, int full_chunk_size, off_t *out_chunk_offset, size_t *out_chunk_size, int big_endian_type, int big_endian_size, int zero_size_end) {
|
|
|
|
int32_t (*read_32bit_type)(off_t,STREAMFILE*) = big_endian_type ? read_32bitBE : read_32bitLE;
|
|
|
|
int32_t (*read_32bit_size)(off_t,STREAMFILE*) = big_endian_size ? read_32bitBE : read_32bitLE;
|
|
|
|
off_t offset, max_offset;
|
|
|
|
size_t file_size = get_streamfile_size(streamFile);
|
|
|
|
|
|
|
|
if (max_size == 0)
|
|
|
|
max_size = file_size;
|
|
|
|
|
|
|
|
offset = start_offset;
|
|
|
|
max_offset = offset + max_size;
|
|
|
|
if (max_offset > file_size)
|
|
|
|
max_offset = file_size;
|
|
|
|
|
2017-01-14 01:37:53 +01:00
|
|
|
|
|
|
|
/* read chunks */
|
2019-09-08 20:23:59 +02:00
|
|
|
while (offset < max_offset) {
|
|
|
|
uint32_t chunk_type = read_32bit_type(offset + 0x00,streamFile);
|
|
|
|
uint32_t chunk_size = read_32bit_size(offset + 0x04,streamFile);
|
2019-09-14 12:39:47 +02:00
|
|
|
//;VGM_LOG("CHUNK: type=%x, size=%x at %lx\n", chunk_type, chunk_size, offset);
|
2017-01-14 01:37:53 +01:00
|
|
|
|
|
|
|
if (chunk_type == chunk_id) {
|
2019-09-08 20:23:59 +02:00
|
|
|
if (out_chunk_offset) *out_chunk_offset = offset + 0x08;
|
2017-01-14 01:37:53 +01:00
|
|
|
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;
|
|
|
|
|
2019-09-08 20:23:59 +02:00
|
|
|
offset += full_chunk_size ? chunk_size : 0x08 + chunk_size;
|
2017-01-14 01:37:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-09-08 20:23:59 +02:00
|
|
|
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) {
|
|
|
|
return find_chunk(streamFile, chunk_id, start_offset, full_chunk_size, out_chunk_offset, out_chunk_size, 1, 0);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
return find_chunk(streamFile, chunk_id, start_offset, full_chunk_size, out_chunk_offset, out_chunk_size, 0, 0);
|
|
|
|
}
|
|
|
|
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 big_endian_size, int zero_size_end) {
|
|
|
|
return find_chunk_internal(streamFile, chunk_id, start_offset, 0, full_chunk_size, out_chunk_offset, out_chunk_size, 1, big_endian_size, zero_size_end);
|
|
|
|
}
|
2019-09-14 12:39:47 +02:00
|
|
|
int find_chunk_riff_le(STREAMFILE *streamFile, uint32_t chunk_id, off_t start_offset, size_t max_size, off_t *out_chunk_offset, size_t *out_chunk_size) {
|
|
|
|
return find_chunk_internal(streamFile, chunk_id, start_offset, max_size, 0, out_chunk_offset, out_chunk_size, 1, 0, 0);
|
|
|
|
}
|
|
|
|
int find_chunk_riff_be(STREAMFILE *streamFile, uint32_t chunk_id, off_t start_offset, size_t max_size, off_t *out_chunk_offset, size_t *out_chunk_size) {
|
|
|
|
return find_chunk_internal(streamFile, chunk_id, start_offset, max_size, 0, out_chunk_offset, out_chunk_size, 1, 1, 0);
|
|
|
|
}
|
2019-09-08 20:23:59 +02:00
|
|
|
int find_chunk_riff_ve(STREAMFILE *streamFile, uint32_t chunk_id, off_t start_offset, size_t max_size, off_t *out_chunk_offset, size_t *out_chunk_size, int big_endian) {
|
|
|
|
return find_chunk_internal(streamFile, chunk_id, start_offset, max_size, 0, out_chunk_offset, out_chunk_size, big_endian, big_endian, 0);
|
|
|
|
}
|
2017-07-15 11:28:42 +02:00
|
|
|
|
2018-08-04 20:42:00 +02:00
|
|
|
/* copies name as-is (may include full path included) */
|
2018-03-29 21:03:25 +02:00
|
|
|
void get_streamfile_name(STREAMFILE *streamFile, char * buffer, size_t size) {
|
2017-07-15 11:28:42 +02:00
|
|
|
streamFile->get_name(streamFile,buffer,size);
|
|
|
|
}
|
2018-03-29 21:03:25 +02:00
|
|
|
/* copies the filename without path */
|
|
|
|
void get_streamfile_filename(STREAMFILE *streamFile, char * buffer, size_t size) {
|
2018-02-25 16:27:48 +01:00
|
|
|
char foldername[PATH_LIMIT];
|
|
|
|
const char *path;
|
|
|
|
|
2018-03-29 21:03:25 +02:00
|
|
|
|
2018-02-25 16:27:48 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-08-04 20:42:00 +02:00
|
|
|
/* copies the filename without path or extension */
|
|
|
|
void get_streamfile_basename(STREAMFILE *streamFile, char * buffer, size_t size) {
|
|
|
|
char *ext;
|
|
|
|
|
|
|
|
get_streamfile_filename(streamFile,buffer,size);
|
|
|
|
|
|
|
|
ext = strrchr(buffer,'.');
|
|
|
|
if (ext) {
|
|
|
|
ext[0] = '\0'; /* remove .ext from buffer */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* copies path removing name (NULL when if filename has no path) */
|
2018-03-29 21:03:25 +02:00
|
|
|
void get_streamfile_path(STREAMFILE *streamFile, char * buffer, size_t size) {
|
2017-07-15 11:28:42 +02:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|
2018-03-29 21:03:25 +02:00
|
|
|
void get_streamfile_ext(STREAMFILE *streamFile, char * filename, size_t size) {
|
2017-07-15 11:28:42 +02:00
|
|
|
streamFile->get_name(streamFile,filename,size);
|
|
|
|
strcpy(filename, filename_extension(filename));
|
|
|
|
}
|
2018-08-11 17:58:16 +02:00
|
|
|
|
|
|
|
/* debug util, mainly for custom IO testing */
|
2019-02-22 23:52:39 +01:00
|
|
|
void dump_streamfile(STREAMFILE *streamFile, int num) {
|
2018-08-12 13:58:48 +02:00
|
|
|
#ifdef VGM_DEBUG_OUTPUT
|
2018-08-11 17:58:16 +02:00
|
|
|
off_t offset = 0;
|
|
|
|
FILE *f = NULL;
|
|
|
|
|
2019-02-22 23:52:39 +01:00
|
|
|
if (num >= 0) {
|
|
|
|
char filename[PATH_LIMIT];
|
|
|
|
char dumpname[PATH_LIMIT];
|
|
|
|
|
|
|
|
get_streamfile_filename(streamFile, filename, PATH_LIMIT);
|
2019-02-23 15:23:00 +01:00
|
|
|
snprintf(dumpname,PATH_LIMIT, "%s_%02i.dump", filename, num);
|
2019-02-22 23:52:39 +01:00
|
|
|
|
|
|
|
f = fopen(dumpname,"wb");
|
2018-08-11 17:58:16 +02:00
|
|
|
if (!f) return;
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:27:41 +01:00
|
|
|
VGM_LOG("dump streamfile: size %x\n", get_streamfile_size(streamFile));
|
2018-08-11 17:58:16 +02:00
|
|
|
while (offset < get_streamfile_size(streamFile)) {
|
|
|
|
uint8_t buffer[0x8000];
|
|
|
|
size_t read;
|
|
|
|
|
|
|
|
read = read_streamfile(buffer,offset,0x8000,streamFile);
|
2019-03-16 00:27:41 +01:00
|
|
|
if(!read) {
|
|
|
|
VGM_LOG("dump streamfile: can't read at %lx\n", offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-02-22 23:52:39 +01:00
|
|
|
if (f)
|
2018-08-11 17:58:16 +02:00
|
|
|
fwrite(buffer,sizeof(uint8_t),read, f);
|
|
|
|
else
|
|
|
|
VGM_LOGB(buffer,read,0);
|
|
|
|
offset += read;
|
|
|
|
}
|
|
|
|
|
2019-02-22 23:52:39 +01:00
|
|
|
if (f) {
|
2018-08-11 17:58:16 +02:00
|
|
|
fclose(f);
|
|
|
|
}
|
2018-08-12 13:58:48 +02:00
|
|
|
#endif
|
2018-08-11 17:58:16 +02:00
|
|
|
}
|