2017-10-05 23:32:57 -07:00
|
|
|
/* mz_strm_zlib.c -- Stream for zlib inflate/deflate
|
2017-10-05 07:34:59 -07:00
|
|
|
Version 2.0.0, October 4th, 2017
|
|
|
|
part of the MiniZip project
|
2017-09-29 21:02:09 -07:00
|
|
|
|
|
|
|
Copyright (C) 2012-2017 Nathan Moinvaziri
|
2017-10-01 22:42:35 -07:00
|
|
|
https://github.com/nmoinvaz/minizip
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-01 22:42:35 -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.
|
2017-09-29 21:02:09 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-10-01 22:42:35 -07:00
|
|
|
#include <stdint.h>
|
2017-09-29 21:02:09 -07:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "zlib.h"
|
|
|
|
|
2017-10-05 23:32:57 -07:00
|
|
|
#include "mz_error.h"
|
2017-10-04 22:10:11 -07:00
|
|
|
#include "mz_strm.h"
|
|
|
|
#include "mz_strm_zlib.h"
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-03 21:56:07 -07:00
|
|
|
/***************************************************************************/
|
|
|
|
|
2017-09-29 21:02:09 -07:00
|
|
|
#ifndef DEF_MEM_LEVEL
|
|
|
|
# if MAX_MEM_LEVEL >= 8
|
|
|
|
# define DEF_MEM_LEVEL 8
|
|
|
|
# else
|
|
|
|
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2017-10-03 21:56:07 -07:00
|
|
|
/***************************************************************************/
|
|
|
|
|
2017-10-08 21:48:28 -07:00
|
|
|
mz_stream_vtbl mz_stream_zlib_vtbl = {
|
|
|
|
mz_stream_zlib_open,
|
|
|
|
mz_stream_zlib_is_open,
|
|
|
|
mz_stream_zlib_read,
|
|
|
|
mz_stream_zlib_write,
|
|
|
|
mz_stream_zlib_tell,
|
|
|
|
mz_stream_zlib_seek,
|
|
|
|
mz_stream_zlib_close,
|
|
|
|
mz_stream_zlib_error,
|
|
|
|
mz_stream_zlib_create,
|
|
|
|
mz_stream_zlib_delete,
|
2017-10-09 23:36:30 -07:00
|
|
|
mz_stream_zlib_get_prop_int64,
|
|
|
|
mz_stream_zlib_set_prop_int64
|
2017-10-08 21:48:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
|
2017-10-01 21:43:24 -07:00
|
|
|
typedef struct mz_stream_zlib_s {
|
2017-10-02 00:44:51 -07:00
|
|
|
mz_stream stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
z_stream zstream;
|
|
|
|
uint8_t buffer[UINT16_MAX];
|
|
|
|
int32_t buffer_len;
|
2017-10-02 22:11:03 -07:00
|
|
|
int64_t total_in;
|
|
|
|
int64_t total_out;
|
2017-10-03 21:56:07 -07:00
|
|
|
int64_t max_total_in;
|
2017-09-29 21:02:09 -07:00
|
|
|
int8_t initialized;
|
|
|
|
int16_t level;
|
|
|
|
int16_t window_bits;
|
|
|
|
int16_t mem_level;
|
|
|
|
int16_t strategy;
|
|
|
|
int16_t mode;
|
|
|
|
int16_t error;
|
2017-10-01 21:43:24 -07:00
|
|
|
} mz_stream_zlib;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-03 21:56:07 -07:00
|
|
|
/***************************************************************************/
|
|
|
|
|
2017-10-05 21:26:34 -07:00
|
|
|
int32_t mz_stream_zlib_open(void *stream, const char *path, int32_t mode)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
int16_t window_bits = 0;
|
|
|
|
|
|
|
|
|
|
|
|
zlib->zstream.data_type = Z_BINARY;
|
|
|
|
zlib->zstream.zalloc = Z_NULL;
|
|
|
|
zlib->zstream.zfree = Z_NULL;
|
|
|
|
zlib->zstream.opaque = Z_NULL;
|
|
|
|
zlib->zstream.total_in = 0;
|
|
|
|
zlib->zstream.total_out = 0;
|
|
|
|
|
|
|
|
zlib->total_in = 0;
|
|
|
|
zlib->total_out = 0;
|
|
|
|
|
2017-10-04 20:06:25 -07:00
|
|
|
window_bits = zlib->window_bits;
|
|
|
|
if (window_bits > 0)
|
|
|
|
window_bits = -window_bits;
|
|
|
|
|
2017-10-01 21:43:24 -07:00
|
|
|
if (mode & MZ_STREAM_MODE_READ)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
|
|
|
zlib->zstream.next_in = zlib->buffer;
|
|
|
|
zlib->zstream.avail_in = 0;
|
|
|
|
|
2017-10-04 20:06:25 -07:00
|
|
|
zlib->error = inflateInit2(&zlib->zstream, window_bits);
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
2017-10-01 21:43:24 -07:00
|
|
|
else if (mode & MZ_STREAM_MODE_WRITE)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
|
|
|
zlib->zstream.next_out = zlib->buffer;
|
2017-10-04 20:06:25 -07:00
|
|
|
zlib->zstream.avail_out = sizeof(zlib->buffer);
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-04 20:06:25 -07:00
|
|
|
zlib->error = deflateInit2(&zlib->zstream, (int8_t)zlib->level, Z_DEFLATED, window_bits, zlib->mem_level, zlib->strategy);
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (zlib->error != Z_OK)
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_STREAM_ERROR;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
|
|
|
zlib->initialized = 1;
|
|
|
|
zlib->mode = mode;
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_OK;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_zlib_is_open(void *stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (zlib->initialized != 1)
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_STREAM_ERROR;
|
|
|
|
return MZ_OK;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-05 21:26:34 -07:00
|
|
|
int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
2017-10-05 00:02:07 -07:00
|
|
|
uint64_t total_out_before = 0;
|
|
|
|
uint64_t total_out_after = 0;
|
2017-09-29 21:02:09 -07:00
|
|
|
uint32_t out_bytes = 0;
|
|
|
|
uint32_t total_out = 0;
|
2017-10-03 21:56:07 -07:00
|
|
|
int32_t bytes_to_read = 0;
|
2017-10-02 00:44:51 -07:00
|
|
|
int32_t read = 0;
|
|
|
|
int16_t err = Z_OK;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-03 21:56:07 -07:00
|
|
|
|
2017-09-29 21:02:09 -07:00
|
|
|
zlib->zstream.next_out = (uint8_t*)buf;
|
|
|
|
zlib->zstream.avail_out = (uint16_t)size;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (zlib->zstream.avail_in == 0)
|
|
|
|
{
|
2017-10-03 23:19:37 -07:00
|
|
|
bytes_to_read = sizeof(zlib->buffer);
|
2017-10-03 21:56:07 -07:00
|
|
|
if (zlib->max_total_in > 0)
|
|
|
|
{
|
2017-10-03 23:19:37 -07:00
|
|
|
if ((zlib->max_total_in - zlib->total_in) < sizeof(zlib->buffer))
|
2017-10-03 21:56:07 -07:00
|
|
|
bytes_to_read = (int32_t)(zlib->max_total_in - zlib->total_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
read = mz_stream_read(zlib->stream.base, zlib->buffer, bytes_to_read);
|
|
|
|
|
|
|
|
if (read < 0)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
|
|
|
zlib->error = Z_STREAM_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2017-10-02 00:44:51 -07:00
|
|
|
if (read == 0)
|
2017-09-29 21:02:09 -07:00
|
|
|
break;
|
|
|
|
|
2017-10-02 00:44:51 -07:00
|
|
|
zlib->total_in += read;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
|
|
|
zlib->zstream.next_in = zlib->buffer;
|
2017-10-02 00:44:51 -07:00
|
|
|
zlib->zstream.avail_in = read;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
total_out_before = zlib->zstream.total_out;
|
|
|
|
|
2017-10-02 00:44:51 -07:00
|
|
|
err = inflate(&zlib->zstream, Z_SYNC_FLUSH);
|
|
|
|
if ((err >= Z_OK) && (zlib->zstream.msg != NULL))
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
|
|
|
zlib->error = Z_DATA_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_out_after = zlib->zstream.total_out;
|
|
|
|
|
2017-10-05 00:02:07 -07:00
|
|
|
out_bytes = (uint32_t)(total_out_after - total_out_before);
|
2017-09-29 21:02:09 -07:00
|
|
|
total_out += out_bytes;
|
2017-10-02 00:44:51 -07:00
|
|
|
|
|
|
|
if (err == Z_STREAM_END)
|
2017-10-03 21:56:07 -07:00
|
|
|
{
|
2017-10-02 00:44:51 -07:00
|
|
|
break;
|
2017-10-03 21:56:07 -07:00
|
|
|
}
|
2017-10-02 00:44:51 -07:00
|
|
|
if (err != Z_OK)
|
|
|
|
{
|
|
|
|
zlib->error = err;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
while (zlib->zstream.avail_out > 0);
|
|
|
|
|
|
|
|
zlib->total_out += total_out;
|
|
|
|
|
|
|
|
return total_out;
|
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_zlib_flush(void *stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
|
|
|
if (mz_stream_write(zlib->stream.base, zlib->buffer, zlib->buffer_len) != zlib->buffer_len)
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_STREAM_ERROR;
|
|
|
|
return MZ_OK;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-09 23:36:30 -07:00
|
|
|
int32_t mz_stream_zlib_deflate(void *stream, int flush)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
2017-10-05 00:02:07 -07:00
|
|
|
uint64_t total_out_before = 0;
|
|
|
|
uint64_t total_out_after = 0;
|
2017-10-09 23:36:30 -07:00
|
|
|
int32_t out_bytes = 0;
|
2017-10-02 00:44:51 -07:00
|
|
|
int16_t err = Z_OK;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-09 23:36:30 -07:00
|
|
|
|
|
|
|
if (zlib->zstream.avail_out == 0)
|
|
|
|
{
|
|
|
|
if (mz_stream_zlib_flush(zlib) != MZ_OK)
|
|
|
|
{
|
|
|
|
zlib->error = Z_STREAM_ERROR;
|
|
|
|
return MZ_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
zlib->zstream.avail_out = sizeof(zlib->buffer);
|
|
|
|
zlib->zstream.next_out = zlib->buffer;
|
|
|
|
|
|
|
|
zlib->buffer_len = 0;
|
|
|
|
}
|
|
|
|
|
2017-09-29 21:02:09 -07:00
|
|
|
total_out_before = zlib->zstream.total_out;
|
2017-10-02 00:44:51 -07:00
|
|
|
err = deflate(&zlib->zstream, flush);
|
2017-09-29 21:02:09 -07:00
|
|
|
total_out_after = zlib->zstream.total_out;
|
|
|
|
|
|
|
|
out_bytes = (uint32_t)(total_out_after - total_out_before);
|
|
|
|
|
2017-10-02 00:44:51 -07:00
|
|
|
if (err != Z_OK && err != Z_STREAM_END)
|
|
|
|
{
|
|
|
|
zlib->error = err;
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_STREAM_ERROR;
|
2017-10-02 00:44:51 -07:00
|
|
|
}
|
|
|
|
|
2017-10-09 23:36:30 -07:00
|
|
|
zlib->buffer_len += out_bytes;
|
|
|
|
zlib->total_out += out_bytes;
|
|
|
|
|
|
|
|
return MZ_OK;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-05 21:26:34 -07:00
|
|
|
int32_t mz_stream_zlib_write(void *stream, const void *buf, int32_t size)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
|
|
|
|
|
|
|
zlib->zstream.next_in = (uint8_t*)buf;
|
|
|
|
zlib->zstream.avail_in = size;
|
|
|
|
|
|
|
|
while ((zlib->error == Z_OK) && (zlib->zstream.avail_in > 0))
|
2017-10-09 23:36:30 -07:00
|
|
|
mz_stream_zlib_deflate(stream, Z_NO_FLUSH);
|
2017-09-29 21:02:09 -07:00
|
|
|
|
|
|
|
zlib->total_in += size;
|
|
|
|
|
2017-10-02 22:11:03 -07:00
|
|
|
return size;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int64_t mz_stream_zlib_tell(void *stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_STREAM_ERROR;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-05 21:26:34 -07:00
|
|
|
int32_t mz_stream_zlib_seek(void *stream, int64_t offset, int32_t origin)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_STREAM_ERROR;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_zlib_close(void *stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
2017-10-09 23:36:30 -07:00
|
|
|
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-01 21:43:24 -07:00
|
|
|
if (zlib->mode & MZ_STREAM_MODE_READ)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
|
|
|
inflateEnd(&zlib->zstream);
|
|
|
|
}
|
2017-10-01 21:43:24 -07:00
|
|
|
else if (zlib->mode & MZ_STREAM_MODE_WRITE)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-09 23:36:30 -07:00
|
|
|
mz_stream_zlib_deflate(stream, Z_FINISH);
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_zlib_flush(stream);
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-09 23:36:30 -07:00
|
|
|
deflateEnd(&zlib->zstream);
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
zlib->initialized = 0;
|
2017-10-02 00:44:51 -07:00
|
|
|
|
2017-09-29 21:02:09 -07:00
|
|
|
if (zlib->error != Z_OK)
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_STREAM_ERROR;
|
|
|
|
return MZ_OK;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_zlib_error(void *stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
return zlib->error;
|
|
|
|
}
|
|
|
|
|
2017-10-09 23:36:30 -07:00
|
|
|
int32_t mz_stream_zlib_get_prop_int64(void *stream, int32_t prop, int64_t *value)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
2017-10-09 23:36:30 -07:00
|
|
|
switch (prop)
|
|
|
|
{
|
|
|
|
case MZ_STREAM_PROP_TOTAL_IN:
|
|
|
|
*value = zlib->total_in;
|
|
|
|
return MZ_OK;
|
|
|
|
case MZ_STREAM_PROP_TOTAL_OUT:
|
|
|
|
*value = zlib->total_out;
|
|
|
|
return MZ_OK;
|
|
|
|
}
|
|
|
|
return MZ_EXIST_ERROR;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-09 23:36:30 -07:00
|
|
|
int32_t mz_stream_zlib_set_prop_int64(void *stream, int32_t prop, int64_t value)
|
2017-10-03 21:56:07 -07:00
|
|
|
{
|
|
|
|
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
2017-10-09 23:36:30 -07:00
|
|
|
switch (prop)
|
|
|
|
{
|
|
|
|
case MZ_STREAM_PROP_COMPRESS_LEVEL:
|
|
|
|
zlib->level = (int16_t)value;
|
|
|
|
return MZ_OK;
|
|
|
|
case MZ_STREAM_PROP_COMPRESS_WINDOW_BITS:
|
|
|
|
if (value == 0)
|
|
|
|
value = -MAX_WBITS;
|
|
|
|
zlib->window_bits = (int16_t)value;
|
|
|
|
return MZ_OK;
|
|
|
|
case MZ_STREAM_PROP_COMPRESS_MEM_LEVEL:
|
|
|
|
if (value == 0)
|
|
|
|
value = DEF_MEM_LEVEL;
|
|
|
|
zlib->mem_level = (int16_t)value;
|
|
|
|
return MZ_OK;
|
|
|
|
case MZ_STREAM_PROP_COMPRESS_STRATEGY:
|
|
|
|
zlib->strategy = (int16_t)value;
|
|
|
|
return MZ_OK;
|
|
|
|
case MZ_STREAM_PROP_TOTAL_IN_MAX:
|
|
|
|
zlib->max_total_in = value;
|
|
|
|
return MZ_OK;
|
|
|
|
}
|
|
|
|
return MZ_EXIST_ERROR;
|
2017-10-03 21:56:07 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
void *mz_stream_zlib_create(void **stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_zlib *zlib = NULL;
|
|
|
|
|
|
|
|
zlib = (mz_stream_zlib *)malloc(sizeof(mz_stream_zlib));
|
|
|
|
if (zlib != NULL)
|
|
|
|
{
|
|
|
|
memset(zlib, 0, sizeof(mz_stream_zlib));
|
2017-10-08 21:48:28 -07:00
|
|
|
zlib->stream.vtbl = &mz_stream_zlib_vtbl;
|
2017-10-01 21:43:24 -07:00
|
|
|
zlib->level = Z_DEFAULT_COMPRESSION;
|
|
|
|
zlib->window_bits = -MAX_WBITS;
|
|
|
|
zlib->mem_level = DEF_MEM_LEVEL;
|
|
|
|
zlib->strategy = Z_DEFAULT_STRATEGY;
|
|
|
|
}
|
|
|
|
if (stream != NULL)
|
|
|
|
*stream = zlib;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-02 22:11:03 -07:00
|
|
|
return zlib;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
void mz_stream_zlib_delete(void **stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_zlib *zlib = NULL;
|
|
|
|
if (stream == NULL)
|
|
|
|
return;
|
|
|
|
zlib = (mz_stream_zlib *)*stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (zlib != NULL)
|
|
|
|
free(zlib);
|
2017-10-05 18:45:23 -07:00
|
|
|
*stream = NULL;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-08 21:48:28 -07:00
|
|
|
void *mz_stream_zlib_get_interface(void)
|
|
|
|
{
|
|
|
|
return (void *)&mz_stream_zlib_vtbl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
mz_stream_vtbl mz_stream_crc32_vtbl = {
|
|
|
|
mz_stream_crc32_open,
|
|
|
|
mz_stream_crc32_is_open,
|
|
|
|
mz_stream_crc32_read,
|
|
|
|
mz_stream_crc32_write,
|
|
|
|
mz_stream_crc32_tell,
|
|
|
|
mz_stream_crc32_seek,
|
|
|
|
mz_stream_crc32_close,
|
|
|
|
mz_stream_crc32_error,
|
|
|
|
mz_stream_crc32_create,
|
|
|
|
mz_stream_crc32_delete,
|
2017-10-09 23:36:30 -07:00
|
|
|
mz_stream_crc32_get_prop_int64
|
2017-10-08 21:48:28 -07:00
|
|
|
};
|
|
|
|
|
2017-10-04 20:06:25 -07:00
|
|
|
/***************************************************************************/
|
|
|
|
|
2017-10-01 21:43:24 -07:00
|
|
|
typedef struct mz_stream_crc32_s {
|
2017-10-02 22:11:03 -07:00
|
|
|
mz_stream stream;
|
|
|
|
int8_t initialized;
|
|
|
|
int32_t value;
|
|
|
|
int64_t total_in;
|
|
|
|
int64_t total_out;
|
2017-10-01 21:43:24 -07:00
|
|
|
} mz_stream_crc32;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-04 20:06:25 -07:00
|
|
|
/***************************************************************************/
|
|
|
|
|
2017-10-05 21:26:34 -07:00
|
|
|
int32_t mz_stream_crc32_open(void *stream, const char *path, int32_t mode)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
crc32->initialized = 1;
|
|
|
|
crc32->value = 0;
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_OK;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_crc32_is_open(void *stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (crc32->initialized != 1)
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_STREAM_ERROR;
|
|
|
|
return MZ_OK;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-05 21:26:34 -07:00
|
|
|
int32_t mz_stream_crc32_read(void *stream, void *buf, int32_t size)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_crc32 *crc32x = (mz_stream_crc32 *)stream;
|
2017-10-08 21:48:28 -07:00
|
|
|
int32_t read = 0;
|
|
|
|
read = mz_stream_read(crc32x->stream.base, buf, size);
|
2017-09-29 21:02:09 -07:00
|
|
|
if (read > 0)
|
2017-10-05 00:02:07 -07:00
|
|
|
crc32x->value = (int32_t)crc32(crc32x->value, buf, read);
|
2017-10-02 00:44:51 -07:00
|
|
|
crc32x->total_in += read;
|
2017-09-29 21:02:09 -07:00
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
2017-10-05 21:26:34 -07:00
|
|
|
int32_t mz_stream_crc32_write(void *stream, const void *buf, int32_t size)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_crc32 *crc32x = (mz_stream_crc32 *)stream;
|
2017-10-02 00:44:51 -07:00
|
|
|
int32_t written = 0;
|
2017-10-05 00:02:07 -07:00
|
|
|
crc32x->value = (int32_t)crc32(crc32x->value, buf, size);
|
2017-10-02 00:44:51 -07:00
|
|
|
written = mz_stream_write(crc32x->stream.base, buf, size);
|
|
|
|
crc32x->total_out += written;
|
|
|
|
return written;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int64_t mz_stream_crc32_tell(void *stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
|
|
|
|
return mz_stream_tell(crc32->stream.base);
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-05 21:26:34 -07:00
|
|
|
int32_t mz_stream_crc32_seek(void *stream, int64_t offset, int32_t origin)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
|
|
|
|
return mz_stream_seek(crc32->stream.base, offset, origin);
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_crc32_close(void *stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
crc32->initialized = 0;
|
2017-10-03 21:56:07 -07:00
|
|
|
return MZ_OK;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_crc32_error(void *stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
|
|
|
|
return mz_stream_error(crc32->stream.base);
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-02 22:11:03 -07:00
|
|
|
int32_t mz_stream_crc32_get_value(void *stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
return crc32->value;
|
|
|
|
}
|
|
|
|
|
2017-10-09 23:36:30 -07:00
|
|
|
int32_t mz_stream_crc32_get_prop_int64(void *stream, int32_t prop, int64_t *value)
|
2017-10-02 00:44:51 -07:00
|
|
|
{
|
|
|
|
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
|
2017-10-09 23:36:30 -07:00
|
|
|
switch (prop)
|
|
|
|
{
|
|
|
|
case MZ_STREAM_PROP_TOTAL_IN:
|
|
|
|
*value = crc32->total_in;
|
|
|
|
return MZ_OK;
|
|
|
|
case MZ_STREAM_PROP_TOTAL_OUT:
|
|
|
|
*value = crc32->total_out;
|
|
|
|
return MZ_OK;
|
|
|
|
}
|
|
|
|
return MZ_EXIST_ERROR;
|
2017-10-02 00:44:51 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
void *mz_stream_crc32_create(void **stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_crc32 *crc32 = NULL;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-01 21:43:24 -07:00
|
|
|
crc32 = (mz_stream_crc32 *)malloc(sizeof(mz_stream_crc32));
|
|
|
|
if (crc32 != NULL)
|
|
|
|
{
|
|
|
|
memset(crc32, 0, sizeof(mz_stream_crc32));
|
2017-10-08 21:48:28 -07:00
|
|
|
crc32->stream.vtbl = &mz_stream_crc32_vtbl;
|
2017-10-01 21:43:24 -07:00
|
|
|
}
|
|
|
|
if (stream != NULL)
|
|
|
|
*stream = crc32;
|
2017-09-29 21:02:09 -07:00
|
|
|
|
2017-10-02 22:11:03 -07:00
|
|
|
return crc32;
|
2017-09-29 21:02:09 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
void mz_stream_crc32_delete(void **stream)
|
2017-09-29 21:02:09 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream_crc32 *crc32 = NULL;
|
|
|
|
if (stream == NULL)
|
|
|
|
return;
|
|
|
|
crc32 = (mz_stream_crc32 *)*stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (crc32 != NULL)
|
|
|
|
free(crc32);
|
2017-10-05 18:45:23 -07:00
|
|
|
*stream = NULL;
|
2017-10-05 00:02:07 -07:00
|
|
|
}
|
2017-10-08 21:48:28 -07:00
|
|
|
|
|
|
|
void *mz_stream_crc32_get_interface(void)
|
|
|
|
{
|
|
|
|
return (void *)&mz_stream_crc32_vtbl;
|
2017-10-10 11:59:09 -07:00
|
|
|
}
|