minizip-ng/mz_strm_buf.c

386 lines
13 KiB
C
Raw Normal View History

/* mz_strm_buf.c -- Stream for buffering reads/writes
2021-01-23 16:18:11 -08:00
part of the minizip-ng project
2012-05-06 13:04:17 -07:00
This version of ioapi is designed to buffer IO.
2023-02-16 13:14:21 -08:00
Copyright (C) Nathan Moinvaziri
2021-01-23 16:18:11 -08:00
https://github.com/zlib-ng/minizip-ng
2012-05-06 13:04:17 -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-05-06 13:04:17 -07:00
*/
#include "mz.h"
2017-10-04 22:10:11 -07:00
#include "mz_strm.h"
#include "mz_strm_buf.h"
2012-05-06 13:04:17 -07:00
2017-10-03 21:56:07 -07:00
/***************************************************************************/
static mz_stream_vtbl mz_stream_buffered_vtbl = {
mz_stream_buffered_open,
mz_stream_buffered_is_open,
mz_stream_buffered_read,
mz_stream_buffered_write,
mz_stream_buffered_tell,
mz_stream_buffered_seek,
mz_stream_buffered_close,
mz_stream_buffered_error,
mz_stream_buffered_create,
mz_stream_buffered_delete,
NULL,
NULL
};
/***************************************************************************/
2017-10-01 21:43:24 -07:00
typedef struct mz_stream_buffered_s {
mz_stream stream;
int32_t error;
char readbuf[INT16_MAX];
int32_t readbuf_len;
int32_t readbuf_pos;
int32_t readbuf_hits;
int32_t readbuf_misses;
char writebuf[INT16_MAX];
int32_t writebuf_len;
int32_t writebuf_pos;
int32_t writebuf_hits;
int32_t writebuf_misses;
int64_t position;
2017-10-01 21:43:24 -07:00
} mz_stream_buffered;
2012-06-28 02:02:57 -07:00
2017-10-03 21:56:07 -07:00
/***************************************************************************/
2012-06-28 02:02:57 -07:00
#if 0
# define mz_stream_buffered_print printf
#else
# define mz_stream_buffered_print(fmt,...)
#endif
/***************************************************************************/
2020-06-14 15:19:14 -07:00
static int32_t mz_stream_buffered_reset(void *stream) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
buffered->readbuf_len = 0;
buffered->readbuf_pos = 0;
buffered->writebuf_len = 0;
buffered->writebuf_pos = 0;
buffered->position = 0;
return MZ_OK;
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_buffered_open(void *stream, const char *path, int32_t mode) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
mz_stream_buffered_print("Buffered - Open (mode %" PRId32 ")\n", mode);
mz_stream_buffered_reset(buffered);
return mz_stream_open(buffered->stream.base, path, mode);
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_buffered_is_open(void *stream) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
return mz_stream_is_open(buffered->stream.base);
2017-11-10 15:04:39 +03:00
}
2020-06-14 15:19:14 -07:00
static int32_t mz_stream_buffered_flush(void *stream, int32_t *written) {
2017-10-01 21:43:24 -07:00
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int32_t total_bytes_written = 0;
int32_t bytes_to_write = buffered->writebuf_len;
int32_t bytes_left_to_write = buffered->writebuf_len;
int32_t bytes_written = 0;
2012-05-06 13:04:17 -07:00
2017-09-29 21:02:09 -07:00
*written = 0;
2020-06-14 15:19:14 -07:00
while (bytes_left_to_write > 0) {
2018-04-24 10:02:39 +00:00
bytes_written = mz_stream_write(buffered->stream.base,
buffered->writebuf + (bytes_to_write - bytes_left_to_write), bytes_left_to_write);
2017-09-29 21:02:09 -07:00
if (bytes_written != bytes_left_to_write)
return MZ_WRITE_ERROR;
2017-09-29 21:02:09 -07:00
buffered->writebuf_misses += 1;
mz_stream_buffered_print("Buffered - Write flush (%" PRId32 ":%" PRId32 " len %" PRId32 ")\n",
bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
2017-09-29 21:02:09 -07:00
total_bytes_written += bytes_written;
bytes_left_to_write -= bytes_written;
2017-09-29 21:02:09 -07:00
buffered->position += bytes_written;
}
2017-09-29 21:02:09 -07:00
buffered->writebuf_len = 0;
buffered->writebuf_pos = 0;
*written = total_bytes_written;
2017-10-03 21:56:07 -07:00
return MZ_OK;
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size) {
2017-10-01 21:43:24 -07:00
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int32_t buf_len = 0;
int32_t bytes_to_read = 0;
int32_t bytes_to_copy = 0;
int32_t bytes_left_to_read = size;
int32_t bytes_read = 0;
int32_t bytes_flushed = 0;
2012-05-06 13:04:17 -07:00
mz_stream_buffered_print("Buffered - Read (size %" PRId32 " pos %" PRId64 ")\n", size, buffered->position);
2012-05-06 13:04:17 -07:00
2020-06-14 15:19:14 -07:00
if (buffered->writebuf_len > 0) {
2020-12-22 17:24:11 -08:00
int64_t position = buffered->position + buffered->writebuf_pos
2020-12-22 17:24:11 -08:00
mz_stream_buffered_print("Buffered - Switch from write to read, flushing (pos %" PRId64 ")\n", position);
mz_stream_buffered_flush(stream, &bytes_flushed);
mz_stream_buffered_seek(stream, position, MZ_SEEK_SET);
}
2020-06-14 15:19:14 -07:00
while (bytes_left_to_read > 0) {
if ((buffered->readbuf_len == 0) || (buffered->readbuf_pos == buffered->readbuf_len)) {
if (buffered->readbuf_len == sizeof(buffered->readbuf)) {
2017-09-29 21:02:09 -07:00
buffered->readbuf_pos = 0;
buffered->readbuf_len = 0;
2012-06-28 02:02:57 -07:00
}
2018-10-08 23:32:38 -07:00
bytes_to_read = (int32_t)sizeof(buffered->readbuf) - (buffered->readbuf_len - buffered->readbuf_pos);
bytes_read = mz_stream_read(buffered->stream.base, buffered->readbuf + buffered->readbuf_pos, bytes_to_read);
if (bytes_read < 0)
return bytes_read;
2012-06-28 02:02:57 -07:00
2017-09-29 21:02:09 -07:00
buffered->readbuf_misses += 1;
buffered->readbuf_len += bytes_read;
buffered->position += bytes_read;
2012-06-28 02:02:57 -07:00
mz_stream_buffered_print("Buffered - Filled (read %" PRId32 "/%" PRId32 " buf %" PRId32 ":%" PRId32 " pos %" PRId64 ")\n",
bytes_read, bytes_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
if (bytes_read == 0)
2012-05-10 15:59:36 -07:00
break;
2012-05-06 13:04:17 -07:00
}
2020-06-14 15:19:14 -07:00
if ((buffered->readbuf_len - buffered->readbuf_pos) > 0) {
2018-10-08 23:32:38 -07:00
bytes_to_copy = buffered->readbuf_len - buffered->readbuf_pos;
2017-10-02 22:17:35 -07:00
if (bytes_to_copy > bytes_left_to_read)
bytes_to_copy = bytes_left_to_read;
2017-09-29 21:02:09 -07:00
memcpy((char *)buf + buf_len, buffered->readbuf + buffered->readbuf_pos, bytes_to_copy);
2012-05-06 13:04:17 -07:00
buf_len += bytes_to_copy;
bytes_left_to_read -= bytes_to_copy;
2012-05-06 13:04:17 -07:00
2017-09-29 21:02:09 -07:00
buffered->readbuf_hits += 1;
buffered->readbuf_pos += bytes_to_copy;
2012-06-28 02:02:57 -07:00
mz_stream_buffered_print("Buffered - Emptied (copied %" PRId32 " remaining %" PRId32 " buf %" PRId32 ":%" PRId32 " pos %" PRId64 ")\n",
bytes_to_copy, bytes_left_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
2012-05-06 13:04:17 -07:00
}
}
return size - bytes_left_to_read;
2012-05-06 13:04:17 -07:00
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size) {
2017-10-01 21:43:24 -07:00
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int32_t bytes_to_write = size;
int32_t bytes_left_to_write = size;
int32_t bytes_to_copy = 0;
int32_t bytes_used = 0;
int32_t bytes_flushed = 0;
int32_t err = MZ_OK;
2012-05-06 13:04:17 -07:00
mz_stream_buffered_print("Buffered - Write (size %" PRId32 " len %" PRId32 " pos %" PRId64 ")\n",
size, buffered->writebuf_len, buffered->position);
2020-06-14 15:19:14 -07:00
if (buffered->readbuf_len > 0) {
2017-09-29 21:02:09 -07:00
buffered->position -= buffered->readbuf_len;
buffered->position += buffered->readbuf_pos;
2017-09-29 21:02:09 -07:00
buffered->readbuf_len = 0;
buffered->readbuf_pos = 0;
mz_stream_buffered_print("Buffered - Switch from read to write (pos %" PRId64 ")\n", buffered->position);
err = mz_stream_seek(buffered->stream.base, buffered->position, MZ_SEEK_SET);
if (err != MZ_OK)
return err;
}
2020-06-14 15:19:14 -07:00
while (bytes_left_to_write > 0) {
2017-10-02 22:17:35 -07:00
bytes_used = buffered->writebuf_len;
if (bytes_used > buffered->writebuf_pos)
bytes_used = buffered->writebuf_pos;
2018-10-08 23:32:38 -07:00
bytes_to_copy = (int32_t)sizeof(buffered->writebuf) - bytes_used;
2017-10-02 22:17:35 -07:00
if (bytes_to_copy > bytes_left_to_write)
bytes_to_copy = bytes_left_to_write;
2020-06-14 15:19:14 -07:00
if (bytes_to_copy == 0) {
err = mz_stream_buffered_flush(stream, &bytes_flushed);
if (err != MZ_OK)
return err;
2017-09-29 21:02:09 -07:00
if (bytes_flushed == 0)
2012-05-10 15:59:36 -07:00
return 0;
continue;
2012-05-06 13:04:17 -07:00
}
2019-07-04 10:32:02 -07:00
memcpy(buffered->writebuf + buffered->writebuf_pos,
(const char *)buf + (bytes_to_write - bytes_left_to_write), bytes_to_copy);
2012-05-06 13:04:17 -07:00
mz_stream_buffered_print("Buffered - Write copy (remaining %" PRId32 " write %" PRId32 ":%" PRId32 " len %" PRId32 ")\n",
bytes_to_copy, bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
2012-05-06 13:04:17 -07:00
bytes_left_to_write -= bytes_to_copy;
2012-05-06 13:04:17 -07:00
2017-09-29 21:02:09 -07:00
buffered->writebuf_pos += bytes_to_copy;
buffered->writebuf_hits += 1;
if (buffered->writebuf_pos > buffered->writebuf_len)
buffered->writebuf_len += buffered->writebuf_pos - buffered->writebuf_len;
2012-05-06 13:04:17 -07:00
}
return size - bytes_left_to_write;
2012-05-06 13:04:17 -07:00
}
2020-06-14 15:19:14 -07:00
int64_t mz_stream_buffered_tell(void *stream) {
2017-10-01 21:43:24 -07:00
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int64_t position = mz_stream_tell(buffered->stream.base);
2017-09-29 21:02:09 -07:00
buffered->position = position;
mz_stream_buffered_print("Buffered - Tell (pos %" PRId64 " readpos %" PRId32 " writepos %" PRId32 ")\n",
2018-11-19 21:34:35 -08:00
buffered->position, buffered->readbuf_pos, buffered->writebuf_pos);
2017-09-29 21:02:09 -07:00
if (buffered->readbuf_len > 0)
2018-08-30 16:22:25 -07:00
position -= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos);
2017-09-29 21:02:09 -07:00
if (buffered->writebuf_len > 0)
position += buffered->writebuf_pos;
2012-06-28 02:02:57 -07:00
return position;
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin) {
2017-10-01 21:43:24 -07:00
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int32_t bytes_flushed = 0;
int32_t err = MZ_OK;
2012-05-06 13:04:17 -07:00
mz_stream_buffered_print("Buffered - Seek (origin %" PRId32 " offset %" PRId64 " pos %" PRId64 ")\n",
origin, offset, buffered->position);
2012-06-28 02:02:57 -07:00
2020-06-14 15:19:14 -07:00
switch (origin) {
case MZ_SEEK_SET:
if ((buffered->readbuf_len > 0) && (offset < buffered->position) &&
(offset >= buffered->position - buffered->readbuf_len)) {
buffered->readbuf_pos = (int32_t)(offset - (buffered->position - buffered->readbuf_len));
return MZ_OK;
}
2020-06-14 15:19:14 -07:00
if (buffered->writebuf_len > 0) {
if ((offset >= buffered->position) && (offset <= buffered->position + buffered->writebuf_len)) {
buffered->writebuf_pos = (int32_t)(offset - buffered->position);
2017-10-03 21:56:07 -07:00
return MZ_OK;
2012-05-07 00:39:30 -07:00
}
2020-06-14 15:19:14 -07:00
}
2017-09-29 21:02:09 -07:00
2020-06-14 15:19:14 -07:00
err = mz_stream_buffered_flush(stream, &bytes_flushed);
if (err != MZ_OK)
return err;
2017-09-29 21:02:09 -07:00
2020-06-14 15:19:14 -07:00
buffered->position = offset;
break;
2012-06-28 02:02:57 -07:00
2020-06-14 15:19:14 -07:00
case MZ_SEEK_CUR:
2012-06-28 02:02:57 -07:00
2020-06-14 15:19:14 -07:00
if (buffered->readbuf_len > 0) {
if (offset <= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos)) {
buffered->readbuf_pos += (uint32_t)offset;
return MZ_OK;
2012-06-28 02:02:57 -07:00
}
2020-06-14 15:19:14 -07:00
offset -= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos);
buffered->position += offset;
}
if (buffered->writebuf_len > 0) {
if (offset <= ((int64_t)buffered->writebuf_len - buffered->writebuf_pos)) {
buffered->writebuf_pos += (uint32_t)offset;
return MZ_OK;
2012-06-28 02:02:57 -07:00
}
2020-06-14 15:19:14 -07:00
/* offset -= (buffered->writebuf_len - buffered->writebuf_pos); */
}
2012-06-28 02:02:57 -07:00
2020-06-14 15:19:14 -07:00
err = mz_stream_buffered_flush(stream, &bytes_flushed);
if (err != MZ_OK)
return err;
2012-06-28 02:02:57 -07:00
2020-06-14 15:19:14 -07:00
break;
2012-06-28 02:02:57 -07:00
2020-06-14 15:19:14 -07:00
case MZ_SEEK_END:
2012-06-28 02:02:57 -07:00
2020-06-14 15:19:14 -07:00
if (buffered->writebuf_len > 0) {
buffered->writebuf_pos = buffered->writebuf_len;
return MZ_OK;
}
break;
2012-05-06 23:52:22 -07:00
}
2012-06-28 02:02:57 -07:00
2017-09-29 21:02:09 -07:00
buffered->readbuf_len = 0;
buffered->readbuf_pos = 0;
buffered->writebuf_len = 0;
buffered->writebuf_pos = 0;
2012-06-28 02:02:57 -07:00
2017-10-01 21:43:24 -07:00
return mz_stream_seek(buffered->stream.base, offset, origin);
2012-05-06 13:04:17 -07:00
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_buffered_close(void *stream) {
2017-10-01 21:43:24 -07:00
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int32_t bytes_flushed = 0;
2017-10-01 21:43:24 -07:00
mz_stream_buffered_flush(stream, &bytes_flushed);
mz_stream_buffered_print("Buffered - Close (flushed %" PRId32 ")\n", bytes_flushed);
2020-06-14 15:19:14 -07:00
if (buffered->readbuf_hits + buffered->readbuf_misses > 0) {
mz_stream_buffered_print("Buffered - Read efficiency %.02f%%\n",
(buffered->readbuf_hits / ((float)buffered->readbuf_hits + buffered->readbuf_misses)) * 100);
}
2020-06-14 15:19:14 -07:00
if (buffered->writebuf_hits + buffered->writebuf_misses > 0) {
mz_stream_buffered_print("Buffered - Write efficiency %.02f%%\n",
(buffered->writebuf_hits / ((float)buffered->writebuf_hits + buffered->writebuf_misses)) * 100);
}
mz_stream_buffered_reset(buffered);
return mz_stream_close(buffered->stream.base);
2012-05-06 13:04:17 -07:00
}
2020-06-14 15:19:14 -07:00
int32_t mz_stream_buffered_error(void *stream) {
2017-10-01 21:43:24 -07:00
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
return mz_stream_error(buffered->stream.base);
2012-05-06 13:04:17 -07:00
}
2020-06-14 15:19:14 -07:00
void *mz_stream_buffered_create(void **stream) {
2017-10-01 21:43:24 -07:00
mz_stream_buffered *buffered = NULL;
2012-05-06 13:04:17 -07:00
buffered = (mz_stream_buffered *)malloc(sizeof(mz_stream_buffered));
2023-02-19 11:17:54 -08:00
if (buffered) {
2017-10-01 21:43:24 -07:00
memset(buffered, 0, sizeof(mz_stream_buffered));
buffered->stream.vtbl = &mz_stream_buffered_vtbl;
2017-10-01 21:43:24 -07:00
}
2023-02-19 11:17:54 -08:00
if (stream)
2017-10-01 21:43:24 -07:00
*stream = buffered;
2017-09-29 21:02:09 -07:00
return buffered;
2012-05-06 13:04:17 -07:00
}
2020-06-14 15:19:14 -07:00
void mz_stream_buffered_delete(void **stream) {
2017-10-01 21:43:24 -07:00
mz_stream_buffered *buffered = NULL;
2023-02-19 11:17:54 -08:00
if (!stream)
2017-10-01 21:43:24 -07:00
return;
buffered = (mz_stream_buffered *)*stream;
2023-02-19 11:17:54 -08:00
if (buffered)
free(buffered);
*stream = NULL;
2012-05-06 13:04:17 -07:00
}
2020-06-14 15:19:14 -07:00
void *mz_stream_buffered_get_interface(void) {
return (void *)&mz_stream_buffered_vtbl;
}