2017-10-01 22:42:35 -07:00
|
|
|
/* mzstrm_mem.c -- Stream for memory access
|
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.
|
|
|
|
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
|
|
|
|
|
2017-09-17 16:52:02 -07:00
|
|
|
Copyright (C) 2012-2017 Nathan Moinvaziri
|
|
|
|
https://github.com/nmoinvaz/minizip
|
|
|
|
Copyright (C) 2003 Justin Fletcher
|
|
|
|
Copyright (C) 1998-2003 Gilles Vollant
|
|
|
|
http://www.winimage.com/zLibDll/minizip.html
|
2012-01-21 15:10:18 -07:00
|
|
|
|
|
|
|
This file is under the same license as the Unzip tool it is distributed
|
|
|
|
with.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
#include "mzstrm.h"
|
|
|
|
#include "mzstrm_mem.h"
|
2012-01-21 15:10:18 -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;
|
|
|
|
char *buffer; // Memory buffer pointer
|
|
|
|
uint32_t size; // Size of the memory buffer
|
|
|
|
uint32_t limit; // Furthest we've written
|
|
|
|
uint32_t position; // Current positoin in the memory
|
|
|
|
int16_t growable; // Growable memory buffer
|
2017-10-01 21:43:24 -07:00
|
|
|
} mz_stream_mem;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_mem_open(void *stream, const char *path, int mode)
|
2012-01-21 15:10:18 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2017-09-17 16:52:02 -07:00
|
|
|
|
2017-10-01 21:43:24 -07:00
|
|
|
if (mode & MZ_STREAM_MODE_CREATE)
|
2014-01-07 19:57:18 -07:00
|
|
|
{
|
2017-09-29 21:02:09 -07:00
|
|
|
if (mem->growable)
|
2014-01-07 19:57:18 -07:00
|
|
|
{
|
2017-09-29 21:02:09 -07:00
|
|
|
mem->size = UINT16_MAX;
|
|
|
|
mem->buffer = (char *)malloc(mem->size);
|
2014-01-07 19:57:18 -07:00
|
|
|
}
|
|
|
|
|
2017-09-29 21:02:09 -07:00
|
|
|
// When writing we start with 0 bytes written
|
|
|
|
mem->limit = 0;
|
2014-01-07 19:57:18 -07:00
|
|
|
}
|
2012-01-21 15:10:18 -07:00
|
|
|
else
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2014-01-07 19:57:18 -07:00
|
|
|
mem->limit = mem->size;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
2012-01-21 15:10:18 -07:00
|
|
|
|
2017-09-29 21:02:09 -07:00
|
|
|
mem->position = 0;
|
2012-01-21 15:10:18 -07:00
|
|
|
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_OK;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_mem_is_open(void *stream)
|
2012-01-21 15:10:18 -07:00
|
|
|
{
|
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)
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_ERR;
|
|
|
|
return MZ_STREAM_OK;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_mem_read(void *stream, void *buf, uint32_t size)
|
2012-01-21 15:10:18 -07:00
|
|
|
{
|
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;
|
2012-01-21 15:10:18 -07:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_mem_write(void *stream, const void *buf, uint32_t size)
|
2012-01-21 15:10:18 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
uint32_t new_size = 0;
|
|
|
|
char *new_buf = NULL;
|
2012-01-21 15:10:18 -07:00
|
|
|
|
2017-09-29 21:02:09 -07:00
|
|
|
if (size > mem->size - mem->position)
|
2014-01-07 19:57:18 -07:00
|
|
|
{
|
2017-09-29 21:02:09 -07:00
|
|
|
if (mem->growable)
|
2014-01-07 19:57:18 -07:00
|
|
|
{
|
2017-09-29 21:02:09 -07:00
|
|
|
new_size = mem->size;
|
|
|
|
if (size < UINT16_MAX)
|
|
|
|
new_size += UINT16_MAX;
|
2014-01-07 19:57:18 -07:00
|
|
|
else
|
2017-09-29 21:02:09 -07:00
|
|
|
new_size += size;
|
|
|
|
|
|
|
|
new_buf = (char *)malloc(new_size);
|
|
|
|
|
|
|
|
memcpy(new_buf, mem->buffer, mem->size);
|
|
|
|
free(mem->buffer);
|
|
|
|
|
|
|
|
mem->buffer = new_buf;
|
|
|
|
mem->size = new_size;
|
2014-01-07 19:57:18 -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;
|
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int64_t mz_stream_mem_tell(void *stream)
|
2012-01-21 15:10:18 -07:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_mem_seek(void *stream, uint64_t offset, int origin)
|
2012-01-21 15:10:18 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
uint64_t new_pos = 0;
|
|
|
|
|
2012-01-21 15:10:18 -07:00
|
|
|
switch (origin)
|
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
case MZ_STREAM_SEEK_CUR:
|
2017-09-29 21:02:09 -07:00
|
|
|
new_pos = mem->position + offset;
|
2014-01-07 19:57:18 -07:00
|
|
|
break;
|
2017-10-01 21:43:24 -07:00
|
|
|
case MZ_STREAM_SEEK_END:
|
2014-01-07 19:57:18 -07:00
|
|
|
new_pos = mem->limit + offset;
|
|
|
|
break;
|
2017-10-01 21:43:24 -07:00
|
|
|
case MZ_STREAM_SEEK_SET:
|
2014-01-07 19:57:18 -07:00
|
|
|
new_pos = offset;
|
|
|
|
break;
|
2017-09-17 16:52:02 -07:00
|
|
|
default:
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_ERR;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (new_pos > mem->size)
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_ERR;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
|
|
|
mem->position = (uint32_t)new_pos;
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_OK;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_mem_close(void *stream)
|
2012-01-21 15:10:18 -07:00
|
|
|
{
|
2017-09-29 21:02:09 -07:00
|
|
|
// We never return errors
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_OK;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_mem_error(void *stream)
|
2012-01-21 15:10:18 -07:00
|
|
|
{
|
2017-09-29 21:02:09 -07:00
|
|
|
// We never return errors
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_OK;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
void mz_stream_mem_set_buffer(void *stream, void *buf, uint32_t size)
|
2012-01-21 15:10:18 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
mem->buffer = buf;
|
|
|
|
mem->size = size;
|
2012-01-21 15:10:18 -07:00
|
|
|
}
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
void mz_stream_mem_set_growable(void *stream, int growable)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
mem->growable = growable;
|
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
void *mz_stream_mem_create(void **stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = NULL;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-01 21:43:24 -07:00
|
|
|
mem = (mz_stream_mem *)malloc(sizeof(mz_stream_mem));
|
|
|
|
if (mem != NULL)
|
|
|
|
{
|
|
|
|
memset(mem, 0, sizeof(mz_stream_mem));
|
|
|
|
|
|
|
|
mem->stream.open = mz_stream_mem_open;
|
|
|
|
mem->stream.is_open = mz_stream_mem_is_open;
|
|
|
|
mem->stream.read = mz_stream_mem_read;
|
|
|
|
mem->stream.write = mz_stream_mem_write;
|
|
|
|
mem->stream.tell = mz_stream_mem_tell;
|
|
|
|
mem->stream.seek = mz_stream_mem_seek;
|
|
|
|
mem->stream.close = mz_stream_mem_close;
|
|
|
|
mem->stream.error = mz_stream_mem_error;
|
|
|
|
mem->stream.create = mz_stream_mem_create;
|
|
|
|
mem->stream.delete = mz_stream_mem_delete;
|
|
|
|
}
|
|
|
|
if (stream == NULL)
|
|
|
|
*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
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
void mz_stream_mem_delete(void **stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_mem *mem = NULL;
|
|
|
|
if (stream == NULL)
|
|
|
|
return;
|
|
|
|
mem = (mz_stream_mem *)*stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (mem != NULL)
|
|
|
|
{
|
|
|
|
if (mem->growable && mem->buffer != NULL)
|
|
|
|
free(mem->buffer);
|
|
|
|
free(mem);
|
|
|
|
}
|
|
|
|
}
|