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; @@ -160,6 +169,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 -------------------------------------------------*/ diff --git a/src/lib/util/corefile.h b/src/lib/util/corefile.h index c2dda7ec43e..cf9571cb9be 100644 --- a/src/lib/util/corefile.h +++ b/src/lib/util/corefile.h @@ -49,6 +49,9 @@ file_error core_fopen(const char *filename, UINT32 openflags, core_file **file); /* open a RAM-based "file" using the given data and length (read-only) */ file_error core_fopen_ram(const void *data, size_t length, UINT32 openflags, core_file **file); +/* open a RAM-based "file" using the given data and length (read-only), copying the data */ +file_error core_fopen_ram_copy(const void *data, size_t length, UINT32 openflags, core_file **file); + /* close an open file */ void core_fclose(core_file *file);