minizip-ng/mz_strm.c

561 lines
16 KiB
C
Raw Normal View History

/* mz_strm.c -- Stream interface
2021-01-23 16:18:11 -08:00
part of the minizip-ng project
2012-02-25 23:28:28 -07:00
2021-01-23 16:18:11 -08:00
Copyright (C) 2010-2021 Nathan Moinvaziri
https://github.com/zlib-ng/minizip-ng
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-02-25 23:28:28 -07:00
*/
#include "mz.h"
2017-10-04 22:10:11 -07:00
#include "mz_strm.h"
2012-10-21 15:20:13 -07:00
/***************************************************************************/
#define MZ_STREAM_FIND_SIZE (1024)
/***************************************************************************/
2020-06-14 15:19:14 -07:00
int32_t mz_stream_open(void *stream, const char *path, int32_t mode) {
2017-10-01 21:43:24 -07:00
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->vtbl == NULL || strm->vtbl->open == NULL)
2017-10-03 21:56:07 -07:00
return MZ_STREAM_ERROR;
return strm->vtbl->open(strm, path, mode);
2012-02-25 23:28:28 -07:00
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_is_open(void *stream) {
2017-10-01 21:43:24 -07:00
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->vtbl == NULL || strm->vtbl->is_open == NULL)
2017-10-03 21:56:07 -07:00
return MZ_STREAM_ERROR;
return strm->vtbl->is_open(strm);
2012-02-25 23:28:28 -07:00
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_read(void *stream, void *buf, int32_t size) {
2017-10-01 21:43:24 -07:00
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->vtbl == NULL || strm->vtbl->read == NULL)
return MZ_PARAM_ERROR;
if (mz_stream_is_open(stream) != MZ_OK)
2017-10-03 21:56:07 -07:00
return MZ_STREAM_ERROR;
return strm->vtbl->read(strm, buf, size);
2012-02-25 23:28:28 -07:00
}
2020-06-14 15:19:14 -07:00
static int32_t mz_stream_read_value(void *stream, uint64_t *value, int32_t len) {
uint8_t buf[8];
int32_t n = 0;
int32_t i = 0;
*value = 0;
2020-06-14 15:19:14 -07:00
if (mz_stream_read(stream, buf, len) == len) {
for (n = 0; n < len; n += 1, i += 8)
*value += ((uint64_t)buf[n]) << i;
2020-06-14 15:19:14 -07:00
} else if (mz_stream_error(stream))
2017-10-03 21:56:07 -07:00
return MZ_STREAM_ERROR;
else
return MZ_END_OF_STREAM;
2017-10-02 00:44:51 -07:00
2017-10-03 21:56:07 -07:00
return MZ_OK;
2017-10-02 00:44:51 -07:00
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_read_uint8(void *stream, uint8_t *value) {
int32_t err = MZ_OK;
uint64_t value64 = 0;
2017-10-02 00:44:51 -07:00
*value = 0;
err = mz_stream_read_value(stream, &value64, sizeof(uint8_t));
if (err == MZ_OK)
*value = (uint8_t)value64;
return err;
}
2017-10-02 00:44:51 -07:00
2020-06-14 15:19:14 -07:00
int32_t mz_stream_read_uint16(void *stream, uint16_t *value) {
int32_t err = MZ_OK;
uint64_t value64 = 0;
*value = 0;
err = mz_stream_read_value(stream, &value64, sizeof(uint16_t));
if (err == MZ_OK)
*value = (uint16_t)value64;
return err;
2017-10-02 00:44:51 -07:00
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_read_uint32(void *stream, uint32_t *value) {
int32_t err = MZ_OK;
uint64_t value64 = 0;
2017-10-02 00:44:51 -07:00
*value = 0;
err = mz_stream_read_value(stream, &value64, sizeof(uint32_t));
if (err == MZ_OK)
*value = (uint32_t)value64;
return err;
2017-10-02 00:44:51 -07:00
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_read_int64(void *stream, int64_t *value) {
2018-10-08 23:31:21 -07:00
return mz_stream_read_value(stream, (uint64_t *)value, sizeof(uint64_t));
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_read_uint64(void *stream, uint64_t *value) {
return mz_stream_read_value(stream, value, sizeof(uint64_t));
2017-10-02 00:44:51 -07:00
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_write(void *stream, const void *buf, int32_t size) {
2017-10-01 21:43:24 -07:00
mz_stream *strm = (mz_stream *)stream;
if (size == 0)
return size;
if (strm == NULL || strm->vtbl == NULL || strm->vtbl->write == NULL)
return MZ_PARAM_ERROR;
if (mz_stream_is_open(stream) != MZ_OK)
2017-10-03 21:56:07 -07:00
return MZ_STREAM_ERROR;
return strm->vtbl->write(strm, buf, size);
2012-02-25 23:28:28 -07:00
}
2020-06-14 15:19:14 -07:00
static int32_t mz_stream_write_value(void *stream, uint64_t value, int32_t len) {
uint8_t buf[8];
int32_t n = 0;
2020-06-14 15:19:14 -07:00
for (n = 0; n < len; n += 1) {
buf[n] = (uint8_t)(value & 0xff);
value >>= 8;
}
2020-06-14 15:19:14 -07:00
if (value != 0) {
/* Data overflow - hack for ZIP64 (X Roche) */
for (n = 0; n < len; n += 1)
buf[n] = 0xff;
}
if (mz_stream_write(stream, buf, len) != len)
2017-10-03 21:56:07 -07:00
return MZ_STREAM_ERROR;
2017-10-03 21:56:07 -07:00
return MZ_OK;
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_write_uint8(void *stream, uint8_t value) {
return mz_stream_write_value(stream, value, sizeof(uint8_t));
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_write_uint16(void *stream, uint16_t value) {
return mz_stream_write_value(stream, value, sizeof(uint16_t));
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_write_uint32(void *stream, uint32_t value) {
return mz_stream_write_value(stream, value, sizeof(uint32_t));
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_write_int64(void *stream, int64_t value) {
2018-10-08 23:31:21 -07:00
return mz_stream_write_value(stream, (uint64_t)value, sizeof(uint64_t));
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_write_uint64(void *stream, uint64_t value) {
return mz_stream_write_value(stream, value, sizeof(uint64_t));
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_copy(void *target, void *source, int32_t len) {
return mz_stream_copy_stream(target, NULL, source, NULL, len);
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_copy_to_end(void *target, void *source) {
2019-04-28 09:23:56 -07:00
return mz_stream_copy_stream_to_end(target, NULL, source, NULL);
}
2019-07-04 10:32:02 -07:00
int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source,
2020-06-14 15:19:14 -07:00
mz_stream_read_cb read_cb, int32_t len) {
2018-08-30 16:22:25 -07:00
uint8_t buf[16384];
int32_t bytes_to_copy = 0;
int32_t read = 0;
int32_t written = 0;
if (write_cb == NULL)
write_cb = mz_stream_write;
if (read_cb == NULL)
read_cb = mz_stream_read;
2020-06-14 15:19:14 -07:00
while (len > 0) {
bytes_to_copy = len;
2018-05-09 00:31:25 -07:00
if (bytes_to_copy > (int32_t)sizeof(buf))
2017-10-15 22:22:19 -07:00
bytes_to_copy = sizeof(buf);
read = read_cb(source, buf, bytes_to_copy);
if (read <= 0)
2017-10-03 21:56:07 -07:00
return MZ_STREAM_ERROR;
written = write_cb(target, buf, read);
if (written != read)
2017-10-03 21:56:07 -07:00
return MZ_STREAM_ERROR;
len -= read;
}
2017-10-03 21:56:07 -07:00
return MZ_OK;
}
2019-07-04 10:32:02 -07:00
int32_t mz_stream_copy_stream_to_end(void *target, mz_stream_write_cb write_cb, void *source,
2020-06-14 15:19:14 -07:00
mz_stream_read_cb read_cb) {
2019-04-28 09:23:56 -07:00
uint8_t buf[16384];
int32_t read = 0;
int32_t written = 0;
if (write_cb == NULL)
write_cb = mz_stream_write;
if (read_cb == NULL)
read_cb = mz_stream_read;
read = read_cb(source, buf, sizeof(buf));
2020-06-14 15:19:14 -07:00
while (read > 0) {
2019-04-28 09:23:56 -07:00
written = write_cb(target, buf, read);
if (written != read)
return MZ_STREAM_ERROR;
read = read_cb(source, buf, sizeof(buf));
}
if (read < 0)
return MZ_STREAM_ERROR;
return MZ_OK;
}
2020-06-14 15:19:14 -07:00
int64_t mz_stream_tell(void *stream) {
2017-10-01 21:43:24 -07:00
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->vtbl == NULL || strm->vtbl->tell == NULL)
return MZ_PARAM_ERROR;
if (mz_stream_is_open(stream) != MZ_OK)
2017-10-03 21:56:07 -07:00
return MZ_STREAM_ERROR;
return strm->vtbl->tell(strm);
2012-02-25 23:28:28 -07:00
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_seek(void *stream, int64_t offset, int32_t origin) {
2017-10-01 21:43:24 -07:00
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->vtbl == NULL || strm->vtbl->seek == NULL)
return MZ_PARAM_ERROR;
if (mz_stream_is_open(stream) != MZ_OK)
2017-10-03 21:56:07 -07:00
return MZ_STREAM_ERROR;
if (origin == MZ_SEEK_SET && offset < 0)
return MZ_SEEK_ERROR;
return strm->vtbl->seek(strm, offset, origin);
2017-09-29 21:02:09 -07:00
}
2012-02-25 23:28:28 -07:00
2020-06-14 15:19:14 -07:00
int32_t mz_stream_find(void *stream, const void *find, int32_t find_size, int64_t max_seek, int64_t *position) {
uint8_t buf[MZ_STREAM_FIND_SIZE];
int32_t buf_pos = 0;
int32_t read_size = sizeof(buf);
int32_t read = 0;
int64_t read_pos = 0;
int64_t start_pos = 0;
int64_t disk_pos = 0;
int32_t i = 0;
uint8_t first = 1;
int32_t err = MZ_OK;
if (stream == NULL || find == NULL || position == NULL)
return MZ_PARAM_ERROR;
if (find_size < 0 || find_size >= (int32_t)sizeof(buf))
return MZ_PARAM_ERROR;
*position = -1;
start_pos = mz_stream_tell(stream);
2020-06-14 15:19:14 -07:00
while (read_pos < max_seek) {
2019-10-19 17:35:22 -07:00
if (read_size > (int32_t)(max_seek - read_pos - buf_pos) && (max_seek - read_pos - buf_pos) < (int64_t)sizeof(buf))
read_size = (int32_t)(max_seek - read_pos - buf_pos);
read = mz_stream_read(stream, buf + buf_pos, read_size);
if ((read <= 0) || (read + buf_pos < find_size))
break;
2020-06-14 15:19:14 -07:00
for (i = 0; i <= read + buf_pos - find_size; i += 1) {
if (memcmp(&buf[i], find, find_size) != 0)
continue;
disk_pos = mz_stream_tell(stream);
/* Seek to position on disk where the data was found */
2018-12-01 08:59:21 -08:00
err = mz_stream_seek(stream, disk_pos - ((int64_t)read + buf_pos - i), MZ_SEEK_SET);
if (err != MZ_OK)
return MZ_EXIST_ERROR;
*position = start_pos + read_pos + i;
return MZ_OK;
}
2020-06-14 15:19:14 -07:00
if (first) {
read -= find_size;
read_size -= find_size;
buf_pos = find_size;
first = 0;
}
memmove(buf, buf + read, find_size);
read_pos += read;
}
return MZ_EXIST_ERROR;
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_find_reverse(void *stream, const void *find, int32_t find_size, int64_t max_seek, int64_t *position) {
uint8_t buf[MZ_STREAM_FIND_SIZE];
int32_t buf_pos = 0;
int32_t read_size = MZ_STREAM_FIND_SIZE;
int64_t read_pos = 0;
int32_t read = 0;
int64_t start_pos = 0;
int64_t disk_pos = 0;
uint8_t first = 1;
int32_t i = 0;
int32_t err = MZ_OK;
if (stream == NULL || find == NULL || position == NULL)
return MZ_PARAM_ERROR;
if (find_size < 0 || find_size >= (int32_t)sizeof(buf))
return MZ_PARAM_ERROR;
*position = -1;
start_pos = mz_stream_tell(stream);
2020-06-14 15:19:14 -07:00
while (read_pos < max_seek) {
2019-10-19 17:35:22 -07:00
if (read_size > (int32_t)(max_seek - read_pos) && (max_seek - read_pos) < (int64_t)sizeof(buf))
read_size = (int32_t)(max_seek - read_pos);
2019-07-04 10:32:02 -07:00
if (mz_stream_seek(stream, start_pos - (read_pos + read_size), MZ_SEEK_SET) != MZ_OK)
break;
read = mz_stream_read(stream, buf, read_size);
if ((read <= 0) || (read + buf_pos < find_size))
break;
if (read + buf_pos < MZ_STREAM_FIND_SIZE)
memmove(buf + MZ_STREAM_FIND_SIZE - (read + buf_pos), buf, read);
2020-06-14 15:19:14 -07:00
for (i = find_size; i <= (read + buf_pos); i += 1) {
if (memcmp(&buf[MZ_STREAM_FIND_SIZE - i], find, find_size) != 0)
continue;
2019-07-04 10:32:02 -07:00
disk_pos = mz_stream_tell(stream);
/* Seek to position on disk where the data was found */
err = mz_stream_seek(stream, disk_pos + buf_pos - i, MZ_SEEK_SET);
if (err != MZ_OK)
return MZ_EXIST_ERROR;
*position = start_pos - (read_pos - buf_pos + i);
return MZ_OK;
}
2020-06-14 15:19:14 -07:00
if (first) {
read -= find_size;
read_size -= find_size;
buf_pos = find_size;
first = 0;
}
if (read == 0)
break;
memmove(buf + read_size, buf, find_size);
read_pos += read;
}
return MZ_EXIST_ERROR;
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_close(void *stream) {
2017-10-01 21:43:24 -07:00
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->vtbl == NULL || strm->vtbl->close == NULL)
return MZ_PARAM_ERROR;
if (mz_stream_is_open(stream) != MZ_OK)
2017-10-03 21:56:07 -07:00
return MZ_STREAM_ERROR;
return strm->vtbl->close(strm);
2012-02-25 23:28:28 -07:00
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_error(void *stream) {
2017-10-01 21:43:24 -07:00
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->vtbl == NULL || strm->vtbl->error == NULL)
return MZ_PARAM_ERROR;
return strm->vtbl->error(strm);
2012-02-25 23:28:28 -07:00
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_set_base(void *stream, void *base) {
2017-10-01 21:43:24 -07:00
mz_stream *strm = (mz_stream *)stream;
strm->base = (mz_stream *)base;
2017-10-03 21:56:07 -07:00
return MZ_OK;
2012-02-25 23:28:28 -07:00
}
2020-06-14 15:19:14 -07:00
void* mz_stream_get_interface(void *stream) {
2018-07-22 09:06:16 -07:00
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->vtbl == NULL)
return NULL;
return (void *)strm->vtbl;
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_get_prop_int64(void *stream, int32_t prop, int64_t *value) {
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->vtbl == NULL || strm->vtbl->get_prop_int64 == NULL)
return MZ_PARAM_ERROR;
return strm->vtbl->get_prop_int64(stream, prop, value);
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_set_prop_int64(void *stream, int32_t prop, int64_t value) {
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->vtbl == NULL || strm->vtbl->set_prop_int64 == NULL)
return MZ_PARAM_ERROR;
return strm->vtbl->set_prop_int64(stream, prop, value);
}
2020-06-14 15:19:14 -07:00
void *mz_stream_create(void **stream, mz_stream_vtbl *vtbl) {
2017-10-02 00:44:51 -07:00
if (stream == NULL)
return NULL;
if (vtbl == NULL || vtbl->create == NULL)
return NULL;
return vtbl->create(stream);
2017-10-02 00:44:51 -07:00
}
2020-06-14 15:19:14 -07:00
void mz_stream_delete(void **stream) {
2017-10-02 00:44:51 -07:00
mz_stream *strm = NULL;
if (stream == NULL)
return;
strm = (mz_stream *)*stream;
2017-10-25 11:12:03 -07:00
if (strm != NULL && strm->vtbl != NULL && strm->vtbl->destroy != NULL)
strm->vtbl->destroy(stream);
*stream = NULL;
2017-10-02 00:44:51 -07:00
}
/***************************************************************************/
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;
/***************************************************************************/
2020-06-14 15:19:14 -07:00
int32_t mz_stream_raw_open(void *stream, const char *path, int32_t mode) {
2018-05-02 20:07:45 -07:00
MZ_UNUSED(stream);
MZ_UNUSED(path);
MZ_UNUSED(mode);
return MZ_OK;
}
2020-06-14 15:19:14 -07:00
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);
}
2020-06-14 15:19:14 -07:00
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;
2020-06-14 15:19:14 -07:00
if (raw->max_total_in > 0) {
if ((int64_t)bytes_to_read > (raw->max_total_in - raw->total_in))
bytes_to_read = (int32_t)(raw->max_total_in - raw->total_in);
}
read = mz_stream_read(raw->stream.base, buf, bytes_to_read);
2020-06-14 15:19:14 -07:00
if (read > 0) {
raw->total_in += read;
raw->total_out += read;
}
return read;
}
2020-06-14 15:19:14 -07:00
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 = 0;
written = mz_stream_write(raw->stream.base, buf, size);
2020-06-14 15:19:14 -07:00
if (written > 0) {
raw->total_out += written;
raw->total_in += written;
}
return written;
}
2020-06-14 15:19:14 -07:00
int64_t mz_stream_raw_tell(void *stream) {
mz_stream_raw *raw = (mz_stream_raw *)stream;
return mz_stream_tell(raw->stream.base);
}
2020-06-14 15:19:14 -07:00
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);
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_raw_close(void *stream) {
2018-05-02 20:07:45 -07:00
MZ_UNUSED(stream);
return MZ_OK;
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_raw_error(void *stream) {
mz_stream_raw *raw = (mz_stream_raw *)stream;
return mz_stream_error(raw->stream.base);
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_raw_get_prop_int64(void *stream, int32_t prop, int64_t *value) {
mz_stream_raw *raw = (mz_stream_raw *)stream;
2020-06-14 15:19:14 -07:00
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;
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_raw_set_prop_int64(void *stream, int32_t prop, int64_t value) {
mz_stream_raw *raw = (mz_stream_raw *)stream;
2020-06-14 15:19:14 -07:00
switch (prop) {
case MZ_STREAM_PROP_TOTAL_IN_MAX:
raw->max_total_in = value;
return MZ_OK;
}
return MZ_EXIST_ERROR;
}
/***************************************************************************/
static 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
};
/***************************************************************************/
2020-06-14 15:19:14 -07:00
void *mz_stream_raw_create(void **stream) {
mz_stream_raw *raw = NULL;
raw = (mz_stream_raw *)MZ_ALLOC(sizeof(mz_stream_raw));
2020-06-14 15:19:14 -07:00
if (raw != NULL) {
memset(raw, 0, sizeof(mz_stream_raw));
raw->stream.vtbl = &mz_stream_raw_vtbl;
}
if (stream != NULL)
*stream = raw;
return raw;
}
2020-06-14 15:19:14 -07:00
void mz_stream_raw_delete(void **stream) {
mz_stream_raw *raw = NULL;
if (stream == NULL)
return;
raw = (mz_stream_raw *)*stream;
if (raw != NULL)
MZ_FREE(raw);
*stream = NULL;
}