mirror of
https://github.com/zlib-ng/minizip-ng
synced 2025-03-28 21:13:18 +00:00
Updated readme.
Moved raw stream to mz_strm.
This commit is contained in:
parent
5f9e5d3aee
commit
e220ac2679
@ -68,7 +68,6 @@ To create a zip file in memory first create a growable memory stream and pass it
|
||||
void *mem_stream = NULL;
|
||||
|
||||
mz_stream_mem_create(&mem_stream);
|
||||
mz_stream_mem_set_grow(mem_stream, 1);
|
||||
mz_stream_mem_set_grow_size(mem_stream, (128 * 1024));
|
||||
mz_stream_open(mem_stream, NULL, MZ_STREAM_MODE_CREATE);
|
||||
|
||||
|
147
src/mz_strm.c
147
src/mz_strm.c
@ -286,3 +286,150 @@ void mz_stream_delete(void **stream)
|
||||
strm->vtbl->delete(stream);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_raw_s {
|
||||
mz_stream stream;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
} mz_stream_raw;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_raw_open(void *stream, const char *path, int32_t mode)
|
||||
{
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_is_open(void *stream)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
return mz_stream_is_open(raw->stream.base);
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_read(void *stream, void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
int32_t bytes_to_read = size;
|
||||
int32_t read = 0;
|
||||
|
||||
if (raw->max_total_in > 0)
|
||||
{
|
||||
if ((raw->max_total_in - raw->total_in) < size)
|
||||
bytes_to_read = (int32_t)(raw->max_total_in - raw->total_in);
|
||||
}
|
||||
|
||||
read = mz_stream_read(raw->stream.base, buf, bytes_to_read);
|
||||
|
||||
if (read > 0)
|
||||
raw->total_in += read;
|
||||
|
||||
return read;
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_write(void *stream, const void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
int32_t written = mz_stream_write(raw->stream.base, buf, size);
|
||||
if (written > 0)
|
||||
raw->total_out += written;
|
||||
return written;
|
||||
}
|
||||
|
||||
int64_t mz_stream_raw_tell(void *stream)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
return mz_stream_tell(raw->stream.base);
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_seek(void *stream, int64_t offset, int32_t origin)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
return mz_stream_seek(raw->stream.base, offset, origin);
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_close(void *stream)
|
||||
{
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_error(void *stream)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
return mz_stream_error(raw->stream.base);
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_get_prop_int64(void *stream, int32_t prop, int64_t *value)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
switch (prop)
|
||||
{
|
||||
case MZ_STREAM_PROP_TOTAL_IN:
|
||||
*value = raw->total_in;
|
||||
return MZ_OK;
|
||||
case MZ_STREAM_PROP_TOTAL_OUT:
|
||||
*value = raw->total_out;
|
||||
return MZ_OK;
|
||||
}
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_set_prop_int64(void *stream, int32_t prop, int64_t value)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
switch (prop)
|
||||
{
|
||||
case MZ_STREAM_PROP_TOTAL_IN_MAX:
|
||||
raw->max_total_in = value;
|
||||
return MZ_OK;
|
||||
}
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
mz_stream_vtbl mz_stream_raw_vtbl = {
|
||||
mz_stream_raw_open,
|
||||
mz_stream_raw_is_open,
|
||||
mz_stream_raw_read,
|
||||
mz_stream_raw_write,
|
||||
mz_stream_raw_tell,
|
||||
mz_stream_raw_seek,
|
||||
mz_stream_raw_close,
|
||||
mz_stream_raw_error,
|
||||
mz_stream_raw_create,
|
||||
mz_stream_raw_delete,
|
||||
mz_stream_raw_get_prop_int64,
|
||||
mz_stream_raw_set_prop_int64
|
||||
};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void *mz_stream_raw_create(void **stream)
|
||||
{
|
||||
mz_stream_raw *raw = NULL;
|
||||
|
||||
raw = (mz_stream_raw *)malloc(sizeof(mz_stream_raw));
|
||||
if (raw != NULL)
|
||||
{
|
||||
memset(raw, 0, sizeof(mz_stream_raw));
|
||||
raw->stream.vtbl = &mz_stream_raw_vtbl;
|
||||
}
|
||||
if (stream != NULL)
|
||||
*stream = raw;
|
||||
|
||||
return raw;
|
||||
}
|
||||
|
||||
void mz_stream_raw_delete(void **stream)
|
||||
{
|
||||
mz_stream_raw *raw = NULL;
|
||||
if (stream == NULL)
|
||||
return;
|
||||
raw = (mz_stream_raw *)*stream;
|
||||
if (raw != NULL)
|
||||
free(raw);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
@ -110,6 +110,11 @@ void mz_stream_delete(void **stream);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void* mz_stream_raw_create(void **stream);
|
||||
void mz_stream_raw_delete(void **stream);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -55,6 +55,24 @@ typedef struct mz_stream_mem_s {
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static void mz_stream_mem_set_size(void *stream, int32_t size)
|
||||
{
|
||||
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
||||
int32_t new_size = size;
|
||||
char *new_buf = NULL;
|
||||
|
||||
|
||||
new_buf = (char *)malloc(new_size);
|
||||
if (mem->buffer)
|
||||
{
|
||||
memcpy(new_buf, mem->buffer, mem->size);
|
||||
free(mem->buffer);
|
||||
}
|
||||
|
||||
mem->buffer = new_buf;
|
||||
mem->size = new_size;
|
||||
}
|
||||
|
||||
int32_t mz_stream_mem_open(void *stream, const char *path, int32_t mode)
|
||||
{
|
||||
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
||||
@ -159,7 +177,7 @@ int32_t mz_stream_mem_seek(void *stream, int64_t offset, int32_t origin)
|
||||
if ((mem->mode & MZ_STREAM_MODE_CREATE) == 0)
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
mz_stream_mem_set_size(stream, new_pos);
|
||||
mz_stream_mem_set_size(stream, (int32_t)new_pos);
|
||||
}
|
||||
|
||||
mem->position = (uint32_t)new_pos;
|
||||
@ -199,24 +217,6 @@ int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, void **buf)
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void mz_stream_mem_set_size(void *stream, int32_t size)
|
||||
{
|
||||
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
||||
int32_t new_size = size;
|
||||
char *new_buf = NULL;
|
||||
|
||||
|
||||
new_buf = (char *)malloc(new_size);
|
||||
if (mem->buffer)
|
||||
{
|
||||
memcpy(new_buf, mem->buffer, mem->size);
|
||||
free(mem->buffer);
|
||||
}
|
||||
|
||||
mem->buffer = new_buf;
|
||||
mem->size = new_size;
|
||||
}
|
||||
|
||||
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size)
|
||||
{
|
||||
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
||||
|
@ -33,7 +33,6 @@ int32_t mz_stream_mem_error(void *stream);
|
||||
void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size);
|
||||
int32_t mz_stream_mem_get_buffer(void *stream, void **buf);
|
||||
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, void **buf);
|
||||
void mz_stream_mem_set_size(void *stream, int32_t size);
|
||||
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size);
|
||||
|
||||
void* mz_stream_mem_create(void **stream);
|
||||
|
@ -511,150 +511,3 @@ void *mz_stream_crc32_get_interface(void)
|
||||
{
|
||||
return (void *)&mz_stream_crc32_vtbl;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_raw_s {
|
||||
mz_stream stream;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
} mz_stream_raw;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_raw_open(void *stream, const char *path, int32_t mode)
|
||||
{
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_is_open(void *stream)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
return mz_stream_is_open(raw->stream.base);
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_read(void *stream, void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
int32_t bytes_to_read = size;
|
||||
int32_t read = 0;
|
||||
|
||||
if (raw->max_total_in > 0)
|
||||
{
|
||||
if ((raw->max_total_in - raw->total_in) < size)
|
||||
bytes_to_read = (int32_t)(raw->max_total_in - raw->total_in);
|
||||
}
|
||||
|
||||
read = mz_stream_read(raw->stream.base, buf, bytes_to_read);
|
||||
|
||||
if (read > 0)
|
||||
raw->total_in += read;
|
||||
|
||||
return read;
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_write(void *stream, const void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
int32_t written = mz_stream_write(raw->stream.base, buf, size);
|
||||
if (written > 0)
|
||||
raw->total_out += written;
|
||||
return written;
|
||||
}
|
||||
|
||||
int64_t mz_stream_raw_tell(void *stream)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
return mz_stream_tell(raw->stream.base);
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_seek(void *stream, int64_t offset, int32_t origin)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
return mz_stream_seek(raw->stream.base, offset, origin);
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_close(void *stream)
|
||||
{
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_error(void *stream)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
return mz_stream_error(raw->stream.base);
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_get_prop_int64(void *stream, int32_t prop, int64_t *value)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
switch (prop)
|
||||
{
|
||||
case MZ_STREAM_PROP_TOTAL_IN:
|
||||
*value = raw->total_in;
|
||||
return MZ_OK;
|
||||
case MZ_STREAM_PROP_TOTAL_OUT:
|
||||
*value = raw->total_out;
|
||||
return MZ_OK;
|
||||
}
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
int32_t mz_stream_raw_set_prop_int64(void *stream, int32_t prop, int64_t value)
|
||||
{
|
||||
mz_stream_raw *raw = (mz_stream_raw *)stream;
|
||||
switch (prop)
|
||||
{
|
||||
case MZ_STREAM_PROP_TOTAL_IN_MAX:
|
||||
raw->max_total_in = value;
|
||||
return MZ_OK;
|
||||
}
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
mz_stream_vtbl mz_stream_raw_vtbl = {
|
||||
mz_stream_raw_open,
|
||||
mz_stream_raw_is_open,
|
||||
mz_stream_raw_read,
|
||||
mz_stream_raw_write,
|
||||
mz_stream_raw_tell,
|
||||
mz_stream_raw_seek,
|
||||
mz_stream_raw_close,
|
||||
mz_stream_raw_error,
|
||||
mz_stream_raw_create,
|
||||
mz_stream_raw_delete,
|
||||
mz_stream_raw_get_prop_int64,
|
||||
mz_stream_raw_set_prop_int64
|
||||
};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void *mz_stream_raw_create(void **stream)
|
||||
{
|
||||
mz_stream_raw *raw = NULL;
|
||||
|
||||
raw = (mz_stream_raw *)malloc(sizeof(mz_stream_raw));
|
||||
if (raw != NULL)
|
||||
{
|
||||
memset(raw, 0, sizeof(mz_stream_raw));
|
||||
raw->stream.vtbl = &mz_stream_raw_vtbl;
|
||||
}
|
||||
if (stream != NULL)
|
||||
*stream = raw;
|
||||
|
||||
return raw;
|
||||
}
|
||||
|
||||
void mz_stream_raw_delete(void **stream)
|
||||
{
|
||||
mz_stream_raw *raw = NULL;
|
||||
if (stream == NULL)
|
||||
return;
|
||||
raw = (mz_stream_raw *)*stream;
|
||||
if (raw != NULL)
|
||||
free(raw);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
@ -59,11 +59,6 @@ void* mz_stream_crc32_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void* mz_stream_raw_create(void **stream);
|
||||
void mz_stream_raw_delete(void **stream);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user