2008-05-26 15:29:21 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define _CRT_SECURE_NO_DEPRECATE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <io.h>
|
|
|
|
|
|
|
|
#include <foobar2000.h>
|
2017-12-31 04:15:34 +01:00
|
|
|
#include <ATLHelpers/ATLHelpersLean.h>
|
2008-05-26 15:29:21 +02:00
|
|
|
#include <shared.h>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "../src/vgmstream.h"
|
|
|
|
#include "../src/util.h"
|
|
|
|
}
|
|
|
|
#include "foo_vgmstream.h"
|
|
|
|
|
|
|
|
|
2017-08-13 22:26:40 +02:00
|
|
|
/* a STREAMFILE that operates via foobar's file service using a buffer */
|
2018-08-24 18:48:42 +02:00
|
|
|
typedef struct {
|
|
|
|
STREAMFILE sf; /* callbacks */
|
|
|
|
|
2019-09-15 15:47:41 +02:00
|
|
|
bool m_file_opened; /* if foobar IO service opened the file */
|
2018-08-24 18:48:42 +02:00
|
|
|
service_ptr_t<file> m_file; /* foobar IO service */
|
|
|
|
abort_callback * p_abort; /* foobar error stuff */
|
|
|
|
char * name; /* IO filename */
|
|
|
|
off_t offset; /* last read offset (info) */
|
|
|
|
off_t buffer_offset; /* current buffer data start */
|
|
|
|
uint8_t * buffer; /* data buffer */
|
|
|
|
size_t buffersize; /* max buffer size */
|
|
|
|
size_t validsize; /* current buffer size */
|
|
|
|
size_t filesize; /* buffered file size */
|
|
|
|
} FOO_STREAMFILE;
|
2017-08-13 22:26:40 +02:00
|
|
|
|
|
|
|
static STREAMFILE * open_foo_streamfile_buffer(const char * const filename, size_t buffersize, abort_callback * p_abort, t_filestats * stats);
|
2019-09-15 15:47:41 +02:00
|
|
|
static STREAMFILE * open_foo_streamfile_buffer_by_file(service_ptr_t<file> m_file, bool m_file_opened, const char * const filename, size_t buffersize, abort_callback * p_abort);
|
2017-08-13 22:26:40 +02:00
|
|
|
|
2018-08-24 18:48:42 +02:00
|
|
|
static size_t read_foo(FOO_STREAMFILE *streamfile, uint8_t * dest, off_t offset, size_t length) {
|
|
|
|
size_t length_read_total = 0;
|
|
|
|
|
2019-09-15 15:47:41 +02:00
|
|
|
if (!streamfile || !streamfile->m_file_opened || !dest || length <= 0 || offset < 0)
|
2018-08-24 18:48:42 +02:00
|
|
|
return 0;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
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) {
|
|
|
|
size_t length_to_read;
|
|
|
|
off_t offset_into_buffer = offset - streamfile->buffer_offset;
|
|
|
|
|
|
|
|
length_to_read = streamfile->validsize - offset_into_buffer;
|
|
|
|
if (length_to_read > length)
|
|
|
|
length_to_read = length;
|
|
|
|
|
|
|
|
memcpy(dest,streamfile->buffer + offset_into_buffer,length_to_read);
|
|
|
|
length_read_total += length_to_read;
|
|
|
|
length -= length_to_read;
|
|
|
|
offset += length_to_read;
|
|
|
|
dest += length_to_read;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 18:48:42 +02:00
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
/* read the rest of the requested length */
|
|
|
|
while (length > 0) {
|
2008-05-26 15:29:21 +02:00
|
|
|
size_t length_to_read;
|
2018-08-24 18:48:42 +02:00
|
|
|
|
|
|
|
/* ignore requests at EOF */
|
|
|
|
if (offset >= streamfile->filesize) {
|
|
|
|
//offset = streamfile->filesize; /* seems fseek doesn't clamp offset */
|
|
|
|
//VGM_ASSERT_ONCE(offset > streamfile->filesize, "STDIO: reading over filesize 0x%x @ 0x%lx + 0x%x\n", streamfile->filesize, offset, length);
|
|
|
|
break;
|
2017-01-15 20:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* position to new offset */
|
2010-02-26 08:45:06 +01:00
|
|
|
try {
|
2017-01-15 20:58:47 +01:00
|
|
|
streamfile->m_file->seek(offset,*streamfile->p_abort);
|
|
|
|
} catch (...) {
|
2018-08-24 18:48:42 +02:00
|
|
|
break; /* this shouldn't happen in our code */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fill the buffer (offset now is beyond buffer_offset) */
|
|
|
|
try {
|
|
|
|
streamfile->buffer_offset = offset;
|
|
|
|
streamfile->validsize = streamfile->m_file->read(streamfile->buffer,streamfile->buffersize,*streamfile->p_abort);
|
|
|
|
} catch(...) {
|
|
|
|
break; /* improbable? */
|
2017-01-15 20:58:47 +01:00
|
|
|
}
|
2008-05-26 15:29:21 +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-05-26 15:29:21 +02:00
|
|
|
|
2018-08-24 18:48:42 +02:00
|
|
|
/* give up on partial reads (EOF) */
|
|
|
|
if (streamfile->validsize < length_to_read) {
|
|
|
|
memcpy(dest,streamfile->buffer,streamfile->validsize);
|
|
|
|
offset += streamfile->validsize;
|
|
|
|
length_read_total += streamfile->validsize;
|
|
|
|
break;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* use the new buffer */
|
|
|
|
memcpy(dest,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;
|
|
|
|
dest += length_to_read;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 18:48:42 +02:00
|
|
|
streamfile->offset = offset; /* last fread offset */
|
2008-05-26 15:29:21 +02:00
|
|
|
return length_read_total;
|
|
|
|
}
|
2018-08-24 18:48:42 +02:00
|
|
|
static size_t get_size_foo(FOO_STREAMFILE * streamfile) {
|
|
|
|
return streamfile->filesize;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
2018-08-24 18:48:42 +02:00
|
|
|
static off_t get_offset_foo(FOO_STREAMFILE *streamfile) {
|
|
|
|
return streamfile->offset;
|
|
|
|
}
|
|
|
|
static void get_name_foo(FOO_STREAMFILE *streamfile,char *buffer,size_t length) {
|
|
|
|
/* Most crap only cares about the filename itself */
|
|
|
|
size_t ourlen = strlen(streamfile->name);
|
|
|
|
if (ourlen > length) {
|
|
|
|
if (length) strcpy(buffer, streamfile->name + ourlen - length + 1);
|
|
|
|
} else {
|
|
|
|
strcpy(buffer, streamfile->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void close_foo(FOO_STREAMFILE * streamfile) {
|
2019-09-21 13:29:12 +02:00
|
|
|
streamfile->m_file.release(); //release alloc'ed ptr
|
2018-08-24 18:48:42 +02:00
|
|
|
free(streamfile->name);
|
|
|
|
free(streamfile->buffer);
|
|
|
|
free(streamfile);
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static STREAMFILE *open_foo(FOO_STREAMFILE *streamFile,const char * const filename,size_t buffersize) {
|
2017-01-15 20:58:47 +01:00
|
|
|
service_ptr_t<file> m_file;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2017-01-15 20:58:47 +01:00
|
|
|
STREAMFILE *newstreamFile;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
|
|
|
if (!filename)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// if same name, duplicate the file pointer we already have open
|
2019-09-15 15:47:41 +02:00
|
|
|
if (streamFile->m_file_opened && !strcmp(streamFile->name,filename)) {
|
|
|
|
m_file = streamFile->m_file; //copy?
|
2018-08-24 18:48:42 +02:00
|
|
|
{
|
2019-09-15 15:47:41 +02:00
|
|
|
newstreamFile = open_foo_streamfile_buffer_by_file(m_file, streamFile->m_file_opened, filename, buffersize, streamFile->p_abort);
|
2008-05-26 15:29:21 +02:00
|
|
|
if (newstreamFile) {
|
|
|
|
return newstreamFile;
|
|
|
|
}
|
|
|
|
// failure, close it and try the default path (which will probably fail a second time)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 15:47:41 +02:00
|
|
|
// a normal open, open a new file
|
2017-01-15 20:58:47 +01:00
|
|
|
return open_foo_streamfile_buffer(filename,buffersize,streamFile->p_abort,NULL);
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 15:47:41 +02:00
|
|
|
static STREAMFILE * open_foo_streamfile_buffer_by_file(service_ptr_t<file> m_file, bool m_file_opened, const char * const filename, size_t buffersize, abort_callback * p_abort) {
|
2008-05-26 15:29:21 +02:00
|
|
|
uint8_t * buffer;
|
|
|
|
FOO_STREAMFILE * streamfile;
|
|
|
|
|
|
|
|
buffer = (uint8_t *) calloc(buffersize,1);
|
2018-08-24 18:48:42 +02:00
|
|
|
if (!buffer) goto fail;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
|
|
|
streamfile = (FOO_STREAMFILE *) calloc(1,sizeof(FOO_STREAMFILE));
|
2018-08-24 18:48:42 +02:00
|
|
|
if (!streamfile) goto fail;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
|
|
|
streamfile->sf.read = (size_t (__cdecl *)(_STREAMFILE *,uint8_t *,off_t,size_t)) read_foo;
|
|
|
|
streamfile->sf.get_size = (size_t (__cdecl *)(_STREAMFILE *)) get_size_foo;
|
|
|
|
streamfile->sf.get_offset = (off_t (__cdecl *)(_STREAMFILE *)) get_offset_foo;
|
|
|
|
streamfile->sf.get_name = (void (__cdecl *)(_STREAMFILE *,char *,size_t)) get_name_foo;
|
|
|
|
streamfile->sf.open = (_STREAMFILE *(__cdecl *)(_STREAMFILE *,const char *const ,size_t)) open_foo;
|
|
|
|
streamfile->sf.close = (void (__cdecl *)(_STREAMFILE *)) close_foo;
|
|
|
|
|
2019-09-15 15:47:41 +02:00
|
|
|
streamfile->m_file_opened = m_file_opened;
|
2008-05-26 15:29:21 +02:00
|
|
|
streamfile->m_file = m_file;
|
2018-08-24 18:48:42 +02:00
|
|
|
streamfile->p_abort = p_abort;
|
2008-05-26 15:29:21 +02:00
|
|
|
streamfile->buffersize = buffersize;
|
|
|
|
streamfile->buffer = buffer;
|
|
|
|
|
2013-05-27 05:08:54 +02:00
|
|
|
streamfile->name = strdup(filename);
|
2018-08-24 18:48:42 +02:00
|
|
|
if (!streamfile->name) goto fail;
|
2017-01-15 20:58:47 +01:00
|
|
|
|
|
|
|
/* cache filesize */
|
2019-09-15 15:47:41 +02:00
|
|
|
if (streamfile->m_file_opened)
|
|
|
|
streamfile->filesize = streamfile->m_file->get_size(*streamfile->p_abort);
|
|
|
|
else
|
|
|
|
streamfile->filesize = 0;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
|
|
|
return &streamfile->sf;
|
2018-08-24 18:48:42 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
free(buffer);
|
|
|
|
free(streamfile);
|
|
|
|
return NULL;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
2017-08-13 22:26:40 +02:00
|
|
|
static STREAMFILE * open_foo_streamfile_buffer(const char * const filename, size_t buffersize, abort_callback * p_abort, t_filestats * stats) {
|
2008-05-26 15:29:21 +02:00
|
|
|
STREAMFILE *streamFile;
|
|
|
|
service_ptr_t<file> infile;
|
2019-09-15 15:47:41 +02:00
|
|
|
bool infile_exists;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2019-09-15 15:47:41 +02:00
|
|
|
infile_exists = filesystem::g_exists(filename, *p_abort);
|
|
|
|
if(!infile_exists) {
|
|
|
|
/* allow non-existing files in some cases */
|
|
|
|
if (!vgmstream_is_virtual_filename(filename))
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2019-09-15 15:47:41 +02:00
|
|
|
if (infile_exists) {
|
|
|
|
filesystem::g_open_read(infile,filename,*p_abort);
|
|
|
|
if(stats) *stats = infile->get_stats(*p_abort);
|
|
|
|
}
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2019-09-15 15:47:41 +02:00
|
|
|
streamFile = open_foo_streamfile_buffer_by_file(infile, infile_exists, filename, buffersize, p_abort);
|
2008-05-26 15:29:21 +02:00
|
|
|
if (!streamFile) {
|
2019-09-21 13:29:12 +02:00
|
|
|
//m_file.release(); //refcounted and cleaned after it goes out of scope
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return streamFile;
|
|
|
|
}
|
2018-08-24 18:48:42 +02:00
|
|
|
|
|
|
|
STREAMFILE * open_foo_streamfile(const char * const filename, abort_callback * p_abort, t_filestats * stats) {
|
|
|
|
return open_foo_streamfile_buffer(filename,STREAMFILE_DEFAULT_BUFFER_SIZE, p_abort, stats);
|
|
|
|
}
|