2017-10-05 23:32:57 -07:00
|
|
|
/* mz_strm_mem.c -- Stream for memory access
|
2021-01-23 16:18:11 -08:00
|
|
|
part of the minizip-ng project
|
2012-01-21 15:10:18 -07:00
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
This interface is designed to access memory rather than files.
|
2018-04-24 10:02:39 +00:00
|
|
|
We do use a region of memory to put data in to and take it out of.
|
2012-01-21 15:10:18 -07:00
|
|
|
|
|
|
|
Based on Unzip ioapi.c version 0.22, May 19th, 2003
|
|
|
|
|
2021-01-23 16:18:11 -08:00
|
|
|
Copyright (C) 2010-2021 Nathan Moinvaziri
|
|
|
|
https://github.com/zlib-ng/minizip-ng
|
2017-09-17 16:52:02 -07:00
|
|
|
Copyright (C) 2003 Justin Fletcher
|
|
|
|
Copyright (C) 1998-2003 Gilles Vollant
|
2018-10-17 22:39:01 +00:00
|
|
|
https://www.winimage.com/zLibDll/minizip.html
|
2012-01-21 15:10:18 -07:00
|
|
|
|
2017-10-16 07:37:11 -07:00
|
|
|
This program is distributed under the terms of the same license as zlib.
|
|
|
|
See the accompanying LICENSE file for the full text of the license.
|
2012-01-21 15:10:18 -07:00
|
|
|
*/
|
|
|
|
|
2017-10-16 07:37:11 -07:00
|
|
|
#include "mz.h"
|
2017-10-04 22:10:11 -07:00
|
|
|
#include "mz_strm.h"
|
|
|
|
#include "mz_strm_mem.h"
|
2012-01-21 15:10:18 -07:00
|
|
|
|
2017-10-03 21:56:07 -07:00
|
|
|
/***************************************************************************/
|
|
|
|
|
2018-05-02 21:34:22 +00:00
|
|
|
static mz_stream_vtbl mz_stream_mem_vtbl = {
|
2017-10-08 21:48:28 -07:00
|
|
|
mz_stream_mem_open,
|
|
|
|
mz_stream_mem_is_open,
|
|
|
|
mz_stream_mem_read,
|
|
|
|
mz_stream_mem_write,
|
|
|
|
mz_stream_mem_tell,
|
|
|
|
mz_stream_mem_seek,
|
|
|
|
mz_stream_mem_close,
|
|
|
|
mz_stream_mem_error,
|
|
|
|
mz_stream_mem_create,
|
2018-05-02 21:34:22 +00:00
|
|
|
mz_stream_mem_delete,
|
|
|
|
NULL,
|
|
|
|
NULL
|
2017-10-08 21:48:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
|
2017-10-01 21:43:24 -07:00
|
|
|
typedef struct mz_stream_mem_s {
|
2017-10-02 00:44:51 -07:00
|
|
|
mz_stream stream;
|
2017-10-20 07:59:39 -07:00
|
|
|
int32_t mode;
|
2018-11-20 16:56:21 -08:00
|
|
|
uint8_t *buffer; /* Memory buffer pointer */
|
|
|
|
int32_t size; /* Size of the memory buffer */
|
|
|
|
int32_t limit; /* Furthest we've written */
|
|
|
|
int32_t position; /* Current position in the memory */
|
|
|
|
int32_t grow_size; /* Size to grow when full */
|
2017-10-01 21:43:24 -07:00
|
|
|
} mz_stream_mem;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-03 21:56:07 -07:00
|
|
|
/***************************************************************************/
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
static int32_t mz_stream_mem_set_size(void *stream, int32_t size) {
|
2017-10-20 08:06:50 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
|
|
|
int32_t new_size = size;
|
2018-08-13 23:20:02 -07:00
|
|
|
uint8_t *new_buf = NULL;
|
2017-10-20 08:06:50 -07:00
|
|
|
|
2018-10-08 23:38:36 -07:00
|
|
|
new_buf = (uint8_t *)MZ_ALLOC((uint32_t)new_size);
|
2018-12-01 08:59:21 -08:00
|
|
|
if (new_buf == NULL)
|
|
|
|
return MZ_BUF_ERROR;
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
if (mem->buffer) {
|
2017-10-20 08:06:50 -07:00
|
|
|
memcpy(new_buf, mem->buffer, mem->size);
|
2018-05-02 20:01:35 -07:00
|
|
|
MZ_FREE(mem->buffer);
|
2017-10-20 08:06:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mem->buffer = new_buf;
|
|
|
|
mem->size = new_size;
|
2018-12-01 08:59:21 -08:00
|
|
|
return MZ_OK;
|
2017-10-20 08:06:50 -07:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
int32_t mz_stream_mem_open(void *stream, const char *path, int32_t mode) {
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2018-12-01 08:59:21 -08:00
|
|
|
int32_t err = MZ_OK;
|
2017-09-17 16:52:02 -07:00
|
|
|
|
2018-05-02 20:07:45 -07:00
|
|
|
MZ_UNUSED(path);
|
2014-01-07 19:57:18 -07:00
|
|
|
|
2017-10-20 07:59:39 -07:00
|
|
|
mem->mode = mode;
|
|
|
|
mem->limit = 0;
|
|
|
|
mem->position = 0;
|
|
|
|
|
2017-10-23 17:22:36 -07:00
|
|
|
if (mem->mode & MZ_OPEN_MODE_CREATE)
|
2018-12-01 08:59:21 -08:00
|
|
|
err = mz_stream_mem_set_size(stream, mem->grow_size);
|
2012-01-21 15:10:18 -07:00
|
|
|
else
|
2014-01-07 19:57:18 -07:00
|
|
|
mem->limit = mem->size;
|
2012-01-21 15:10:18 -07:00
|
|
|
|
2018-12-01 08:59:21 -08:00
|
|
|
return err;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
int32_t mz_stream_mem_is_open(void *stream) {
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (mem->buffer == NULL)
|
2018-10-28 16:15:13 -07:00
|
|
|
return MZ_OPEN_ERROR;
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_OK;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
int32_t mz_stream_mem_read(void *stream, void *buf, int32_t size) {
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2012-01-21 15:10:18 -07:00
|
|
|
|
2017-09-29 21:02:09 -07:00
|
|
|
if (size > mem->size - mem->position)
|
|
|
|
size = mem->size - mem->position;
|
2019-05-02 21:03:40 -07:00
|
|
|
if (mem->position + size > mem->limit)
|
|
|
|
size = mem->limit - mem->position;
|
2012-01-21 15:10:18 -07:00
|
|
|
|
2019-05-02 21:03:40 -07:00
|
|
|
if (size <= 0)
|
2017-10-21 09:37:18 -07:00
|
|
|
return 0;
|
|
|
|
|
2017-09-29 21:02:09 -07:00
|
|
|
memcpy(buf, mem->buffer + mem->position, size);
|
|
|
|
mem->position += size;
|
2012-01-21 15:10:18 -07:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
int32_t mz_stream_mem_write(void *stream, const void *buf, int32_t size) {
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2017-10-05 21:26:34 -07:00
|
|
|
int32_t new_size = 0;
|
2018-12-01 08:59:21 -08:00
|
|
|
int32_t err = MZ_OK;
|
2012-01-21 15:10:18 -07:00
|
|
|
|
2017-10-02 22:11:03 -07:00
|
|
|
if (size == 0)
|
|
|
|
return size;
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
if (size > mem->size - mem->position) {
|
|
|
|
if (mem->mode & MZ_OPEN_MODE_CREATE) {
|
2017-09-29 21:02:09 -07:00
|
|
|
new_size = mem->size;
|
2017-10-02 22:11:03 -07:00
|
|
|
if (size < mem->grow_size)
|
|
|
|
new_size += mem->grow_size;
|
2014-01-07 19:57:18 -07:00
|
|
|
else
|
2017-09-29 21:02:09 -07:00
|
|
|
new_size += size;
|
|
|
|
|
2018-12-01 08:59:21 -08:00
|
|
|
err = mz_stream_mem_set_size(stream, new_size);
|
|
|
|
if (err != MZ_OK)
|
|
|
|
return err;
|
2020-06-14 15:19:14 -07:00
|
|
|
} else {
|
2017-09-29 21:02:09 -07:00
|
|
|
size = mem->size - mem->position;
|
|
|
|
}
|
2014-01-07 19:57:18 -07:00
|
|
|
}
|
2017-09-29 21:02:09 -07:00
|
|
|
|
|
|
|
memcpy(mem->buffer + mem->position, buf, size);
|
|
|
|
|
|
|
|
mem->position += size;
|
|
|
|
if (mem->position > mem->limit)
|
|
|
|
mem->limit = mem->position;
|
2012-01-21 15:10:18 -07:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
int64_t mz_stream_mem_tell(void *stream) {
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
return mem->position;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
int32_t mz_stream_mem_seek(void *stream, int64_t offset, int32_t origin) {
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2017-10-20 07:59:39 -07:00
|
|
|
int64_t new_pos = 0;
|
2018-12-01 08:59:21 -08:00
|
|
|
int32_t err = MZ_OK;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
switch (origin) {
|
|
|
|
case MZ_SEEK_CUR:
|
|
|
|
new_pos = mem->position + offset;
|
|
|
|
break;
|
|
|
|
case MZ_SEEK_END:
|
|
|
|
new_pos = mem->limit + offset;
|
|
|
|
break;
|
|
|
|
case MZ_SEEK_SET:
|
|
|
|
new_pos = offset;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return MZ_SEEK_ERROR;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
if (new_pos > mem->size) {
|
2017-10-23 17:22:36 -07:00
|
|
|
if ((mem->mode & MZ_OPEN_MODE_CREATE) == 0)
|
2018-10-28 16:15:13 -07:00
|
|
|
return MZ_SEEK_ERROR;
|
2017-10-20 07:59:39 -07:00
|
|
|
|
2018-12-01 08:59:21 -08:00
|
|
|
err = mz_stream_mem_set_size(stream, (int32_t)new_pos);
|
|
|
|
if (err != MZ_OK)
|
|
|
|
return err;
|
2020-06-14 15:19:14 -07:00
|
|
|
} else if (new_pos < 0) {
|
2018-11-06 14:39:17 +11:00
|
|
|
return MZ_SEEK_ERROR;
|
|
|
|
}
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2018-10-08 23:38:36 -07:00
|
|
|
mem->position = (int32_t)new_pos;
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_OK;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
int32_t mz_stream_mem_close(void *stream) {
|
2018-05-02 20:07:45 -07:00
|
|
|
MZ_UNUSED(stream);
|
2018-05-02 21:34:22 +00:00
|
|
|
|
2018-11-20 16:56:21 -08:00
|
|
|
/* We never return errors */
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_OK;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
int32_t mz_stream_mem_error(void *stream) {
|
2018-05-02 20:07:45 -07:00
|
|
|
MZ_UNUSED(stream);
|
2018-05-02 21:34:22 +00:00
|
|
|
|
2018-11-20 16:56:21 -08:00
|
|
|
/* We never return errors */
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_OK;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size) {
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2018-08-13 23:07:42 -07:00
|
|
|
mem->buffer = (uint8_t *)buf;
|
2017-09-29 21:02:09 -07:00
|
|
|
mem->size = size;
|
2017-10-21 09:37:18 -07:00
|
|
|
mem->limit = size;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
int32_t mz_stream_mem_get_buffer(void *stream, const void **buf) {
|
2017-10-03 21:56:07 -07:00
|
|
|
return mz_stream_mem_get_buffer_at(stream, 0, buf);
|
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, const void **buf) {
|
2017-10-03 21:56:07 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
|
|
|
if (buf == NULL || position < 0 || mem->size < position || mem->buffer == NULL)
|
2018-10-28 16:15:13 -07:00
|
|
|
return MZ_SEEK_ERROR;
|
2017-10-03 21:56:07 -07:00
|
|
|
*buf = mem->buffer + position;
|
|
|
|
return MZ_OK;
|
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
int32_t mz_stream_mem_get_buffer_at_current(void *stream, const void **buf) {
|
2018-08-14 17:46:36 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
|
|
|
return mz_stream_mem_get_buffer_at(stream, mem->position, buf);
|
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length) {
|
2018-05-31 13:56:32 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
|
|
|
*length = mem->limit;
|
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
void mz_stream_mem_set_buffer_limit(void *stream, int32_t limit) {
|
2018-07-12 12:39:02 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
|
|
|
mem->limit = limit;
|
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size) {
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2017-10-02 22:11:03 -07:00
|
|
|
mem->grow_size = grow_size;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
void *mz_stream_mem_create(void **stream) {
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = NULL;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2018-05-02 20:01:35 -07:00
|
|
|
mem = (mz_stream_mem *)MZ_ALLOC(sizeof(mz_stream_mem));
|
2020-06-14 15:19:14 -07:00
|
|
|
if (mem != NULL) {
|
2017-10-01 21:43:24 -07:00
|
|
|
memset(mem, 0, sizeof(mz_stream_mem));
|
2017-10-08 21:48:28 -07:00
|
|
|
mem->stream.vtbl = &mz_stream_mem_vtbl;
|
2018-08-13 23:07:42 -07:00
|
|
|
mem->grow_size = 4096;
|
2017-10-01 21:43:24 -07:00
|
|
|
}
|
2017-10-02 22:11:03 -07:00
|
|
|
if (stream != NULL)
|
2017-10-01 21:43:24 -07:00
|
|
|
*stream = mem;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
return mem;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
void mz_stream_mem_delete(void **stream) {
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = NULL;
|
|
|
|
if (stream == NULL)
|
|
|
|
return;
|
|
|
|
mem = (mz_stream_mem *)*stream;
|
2020-06-14 15:19:14 -07:00
|
|
|
if (mem != NULL) {
|
2017-10-23 17:22:36 -07:00
|
|
|
if ((mem->mode & MZ_OPEN_MODE_CREATE) && (mem->buffer != NULL))
|
2018-05-02 20:01:35 -07:00
|
|
|
MZ_FREE(mem->buffer);
|
|
|
|
MZ_FREE(mem);
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
2017-10-05 18:45:23 -07:00
|
|
|
*stream = NULL;
|
2017-10-08 21:48:28 -07:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:19:14 -07:00
|
|
|
void *mz_stream_mem_get_interface(void) {
|
2017-10-08 21:48:28 -07:00
|
|
|
return (void *)&mz_stream_mem_vtbl;
|
2018-05-02 19:59:38 +00:00
|
|
|
}
|