diff options
Diffstat (limited to 'src/lib/util/corefile.c')
-rw-r--r-- | src/lib/util/corefile.c | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/src/lib/util/corefile.c b/src/lib/util/corefile.c index 565886a54a9..f28edbbff78 100644 --- a/src/lib/util/corefile.c +++ b/src/lib/util/corefile.c @@ -133,11 +133,12 @@ file_error core_fopen(const char *filename, UINT32 openflags, core_file **file) /*------------------------------------------------- - core_fopen_ram - open a RAM-based buffer for - file-like access and return an error code + core_fopen_ram_internal - open a RAM-based buffer + for file-like access, possibly copying the data, + and return an error code -------------------------------------------------*/ -file_error core_fopen_ram(const void *data, size_t length, UINT32 openflags, core_file **file) +static file_error core_fopen_ram_internal(const void *data, size_t length, int copy_buffer, UINT32 openflags, core_file **file) { /* can only do this for read access */ if ((openflags & OPEN_FLAG_WRITE) != 0) @@ -146,11 +147,19 @@ file_error core_fopen_ram(const void *data, size_t length, UINT32 openflags, cor return FILERR_INVALID_ACCESS; /* allocate the file itself */ - *file = malloc(sizeof(**file)); + *file = malloc(sizeof(**file) + (copy_buffer ? length : 0)); if (*file == NULL) return FILERR_OUT_OF_MEMORY; memset(*file, 0, sizeof(**file)); + /* copy the buffer, if we're asked to */ + if (copy_buffer) + { + void *dest = ((UINT8 *) *file) + sizeof(**file); + memcpy(dest, data, length); + data = dest; + } + /* claim the buffer */ (*file)->data = (UINT8 *)data; (*file)->length = length; @@ -161,6 +170,28 @@ file_error core_fopen_ram(const void *data, size_t length, UINT32 openflags, cor /*------------------------------------------------- + core_fopen_ram - open a RAM-based buffer for + file-like access and return an error code +-------------------------------------------------*/ + +file_error core_fopen_ram(const void *data, size_t length, UINT32 openflags, core_file **file) +{ + return core_fopen_ram_internal(data, length, FALSE, openflags, file); +} + + +/*------------------------------------------------- + core_fopen_ram_copy - open a copy of a RAM-based + buffer for file-like access and return an error code +-------------------------------------------------*/ + +file_error core_fopen_ram_copy(const void *data, size_t length, UINT32 openflags, core_file **file) +{ + return core_fopen_ram_internal(data, length, TRUE, openflags, file); +} + + +/*------------------------------------------------- core_fclose - closes a file -------------------------------------------------*/ |