Added bzip2 compression back in.

Added test stream functions.
Refactored zip library.
This commit is contained in:
Nathan Moinvaziri 2017-10-02 22:11:03 -07:00
parent 3624400b08
commit 0a592082d0
27 changed files with 1615 additions and 1855 deletions

View File

@ -599,7 +599,7 @@ void sendMTFValues ( EState* s )
/*---------------------------------------------------*/
void BZ2_compressBlock ( EState* s, Bool is_last_block )
extern void BZ2_compressBlock ( EState* s, Bool is_last_block )
{
if (s->nblock > 0) {

View File

@ -1,31 +0,0 @@
/* Spew out a long sequence of the byte 251. When fed to bzip2
versions 1.0.0 or 1.0.1, causes it to die with internal error
1007 in blocksort.c. This assertion misses an extremely rare
case, which is fixed in this version (1.0.2) and above.
*/
/* ------------------------------------------------------------------
This file is part of bzip2/libbzip2, a program and library for
lossless, block-sorting data compression.
bzip2/libbzip2 version 1.0.6 of 6 September 2010
Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
Please read the WARNING, DISCLAIMER and PATENTS sections in the
README file.
This program is released under the terms of the license contained
in the file LICENSE.
------------------------------------------------------------------ */
#include <stdio.h>
int main ()
{
int i;
for (i = 0; i < 48500000 ; i++)
putchar(251);
return 0;
}

View File

@ -1,141 +0,0 @@
/* A test program written to test robustness to decompression of
corrupted data. Usage is
unzcrash filename
and the program will read the specified file, compress it (in memory),
and then repeatedly decompress it, each time with a different bit of
the compressed data inverted, so as to test all possible one-bit errors.
This should not cause any invalid memory accesses. If it does,
I want to know about it!
PS. As you can see from the above description, the process is
incredibly slow. A file of size eg 5KB will cause it to run for
many hours.
*/
/* ------------------------------------------------------------------
This file is part of bzip2/libbzip2, a program and library for
lossless, block-sorting data compression.
bzip2/libbzip2 version 1.0.6 of 6 September 2010
Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
Please read the WARNING, DISCLAIMER and PATENTS sections in the
README file.
This program is released under the terms of the license contained
in the file LICENSE.
------------------------------------------------------------------ */
#include <stdio.h>
#include <assert.h>
#include "bzlib.h"
#define M_BLOCK 1000000
typedef unsigned char uchar;
#define M_BLOCK_OUT (M_BLOCK + 1000000)
uchar inbuf[M_BLOCK];
uchar outbuf[M_BLOCK_OUT];
uchar zbuf[M_BLOCK + 600 + (M_BLOCK / 100)];
int nIn, nOut, nZ;
static char *bzerrorstrings[] = {
"OK"
,"SEQUENCE_ERROR"
,"PARAM_ERROR"
,"MEM_ERROR"
,"DATA_ERROR"
,"DATA_ERROR_MAGIC"
,"IO_ERROR"
,"UNEXPECTED_EOF"
,"OUTBUFF_FULL"
,"???" /* for future */
,"???" /* for future */
,"???" /* for future */
,"???" /* for future */
,"???" /* for future */
,"???" /* for future */
};
void flip_bit ( int bit )
{
int byteno = bit / 8;
int bitno = bit % 8;
uchar mask = 1 << bitno;
//fprintf ( stderr, "(byte %d bit %d mask %d)",
// byteno, bitno, (int)mask );
zbuf[byteno] ^= mask;
}
int main ( int argc, char** argv )
{
FILE* f;
int r;
int bit;
int i;
if (argc != 2) {
fprintf ( stderr, "usage: unzcrash filename\n" );
return 1;
}
f = fopen ( argv[1], "r" );
if (!f) {
fprintf ( stderr, "unzcrash: can't open %s\n", argv[1] );
return 1;
}
nIn = fread ( inbuf, 1, M_BLOCK, f );
fprintf ( stderr, "%d bytes read\n", nIn );
nZ = M_BLOCK;
r = BZ2_bzBuffToBuffCompress (
zbuf, &nZ, inbuf, nIn, 9, 0, 30 );
assert (r == BZ_OK);
fprintf ( stderr, "%d after compression\n", nZ );
for (bit = 0; bit < nZ*8; bit++) {
fprintf ( stderr, "bit %d ", bit );
flip_bit ( bit );
nOut = M_BLOCK_OUT;
r = BZ2_bzBuffToBuffDecompress (
outbuf, &nOut, zbuf, nZ, 0, 0 );
fprintf ( stderr, " %d %s ", r, bzerrorstrings[-r] );
if (r != BZ_OK) {
fprintf ( stderr, "\n" );
} else {
if (nOut != nIn) {
fprintf(stderr, "nIn/nOut mismatch %d %d\n", nIn, nOut );
return 1;
} else {
for (i = 0; i < nOut; i++)
if (inbuf[i] != outbuf[i]) {
fprintf(stderr, "mismatch at %d\n", i );
return 1;
}
if (i == nOut) fprintf(stderr, "really ok!\n" );
}
}
flip_bit ( bit );
}
#if 0
assert (nOut == nIn);
for (i = 0; i < nOut; i++) {
if (inbuf[i] != outbuf[i]) {
fprintf ( stderr, "difference at %d !\n", i );
return 1;
}
}
#endif
fprintf ( stderr, "all ok\n" );
return 0;
}

View File

@ -42,4 +42,4 @@ void display_zpos64(uint64_t n, int size_char);
}
#endif
#endif /* _MINISHARED_H */
#endif

289
minizip.c
View File

@ -35,7 +35,9 @@
#include "zip.h"
#include "mz_compat.h"
#include "mzstrm.h"
#include "test.h"
#include "minishared.h"
@ -56,12 +58,11 @@ void minizip_help()
" -j exclude path. store only the file name.\n\n");
}
int minizip_addfile(zipFile zf, const char *path, const char *filenameinzip, int level, const char *password)
int minizip_addfile(void *zf, const char *path, const char *filenameinzip, int level, const char *password)
{
zip_fileinfo zi = { 0 };
voidpf stream_entry = NULL;
void *stream_entry = NULL;
int size_read = 0;
int zip64 = 0;
int err = ZIP_OK;
char buf[UINT16_MAX];
@ -69,14 +70,12 @@ int minizip_addfile(zipFile zf, const char *path, const char *filenameinzip, int
/* Get information about the file on disk so we can store it in zip */
get_file_date(path, &zi.dos_date);
zip64 = mz_os_file_is_large(path);
#define DEF_MEM_LEVEL 8
/* Add to zip file */
err = zipOpenNewFileInZip3_64(zf, filenameinzip, &zi,
NULL, 0, NULL, 0, NULL /* comment*/,
(level != 0) ? Z_DEFLATED : 0, level, 0,
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
password, 0, zip64);
password, 0, 1);
mz_stream_os_create(&stream_entry);
@ -134,289 +133,25 @@ int minizip_addfile(zipFile zf, const char *path, const char *filenameinzip, int
return err;
}
#include "mzstrm_zlib.h"
#include "mzstrm_aes.h"
void test_aes()
{
char buf[UINT16_MAX];
int16_t read = 0;
int16_t written = 0;
voidpf out_stream = NULL;
voidpf in_stream = NULL;
voidpf aes_out_stream = NULL;
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, "LICENSE", MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
read = mz_stream_os_read(in_stream, buf, UINT16_MAX);
mz_stream_os_close(in_stream);
}
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
if (mz_stream_os_open(out_stream, "LICENSE.aes-encrypt", MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
mz_stream_aes_create(&aes_out_stream);
mz_stream_aes_set_password(aes_out_stream, "hello");
mz_stream_set_base(aes_out_stream, out_stream);
if (mz_stream_open(aes_out_stream, NULL, MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
written = mz_stream_write(aes_out_stream, buf, read);
mz_stream_close(aes_out_stream);
}
mz_stream_aes_delete(&aes_out_stream);
mz_stream_os_close(out_stream);
printf("LICENSE aes encrypted %d\n", written);
}
mz_stream_os_delete(&out_stream);
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, "LICENSE.aes-encrypt", MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
mz_stream_aes_create(&aes_out_stream);
mz_stream_aes_set_password(aes_out_stream, "hello");
mz_stream_set_base(aes_out_stream, in_stream);
if (mz_stream_open(aes_out_stream, NULL, MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
read = mz_stream_read(aes_out_stream, buf, read);
mz_stream_close(aes_out_stream);
}
mz_stream_aes_delete(&aes_out_stream);
mz_stream_os_close(in_stream);
printf("LICENSE aes decrypted %d\n", read);
}
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
if (mz_stream_os_open(out_stream, "LICENSE.aes-decrypt", MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
mz_stream_os_write(out_stream, buf, read);
mz_stream_os_close(out_stream);
}
mz_stream_os_delete(&out_stream);
}
void test_crypt()
{
char buf[UINT16_MAX];
int16_t read = 0;
int16_t written = 0;
voidpf out_stream = NULL;
voidpf in_stream = NULL;
voidpf crypt_out_stream = NULL;
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, "LICENSE", MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
read = mz_stream_os_read(in_stream, buf, UINT16_MAX);
mz_stream_os_close(in_stream);
}
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
if (mz_stream_os_open(out_stream, "LICENSE.encrypt", MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
mz_stream_crypt_create(&crypt_out_stream);
mz_stream_crypt_set_password(crypt_out_stream, "hello");
mz_stream_set_base(crypt_out_stream, out_stream);
if (mz_stream_open(crypt_out_stream, NULL, MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
written = mz_stream_write(crypt_out_stream, buf, read);
mz_stream_close(crypt_out_stream);
}
mz_stream_crypt_delete(&crypt_out_stream);
mz_stream_os_close(out_stream);
printf("LICENSE encrypted %d\n", written);
}
mz_stream_os_delete(&out_stream);
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, "LICENSE.encrypt", MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
mz_stream_crypt_create(&crypt_out_stream);
mz_stream_crypt_set_password(crypt_out_stream, "hello");
mz_stream_set_base(crypt_out_stream, in_stream);
if (mz_stream_open(crypt_out_stream, NULL, MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
read = mz_stream_read(crypt_out_stream, buf, read);
mz_stream_close(crypt_out_stream);
}
mz_stream_crypt_delete(&crypt_out_stream);
mz_stream_os_close(in_stream);
printf("LICENSE decrypted %d\n", read);
}
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
if (mz_stream_os_open(out_stream, "LICENSE.decrypt", MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
mz_stream_os_write(out_stream, buf, read);
mz_stream_os_close(out_stream);
}
mz_stream_os_delete(&out_stream);
}
void test_inflate()
{
char buf[UINT16_MAX];
int16_t read = 0;
voidpf out_stream = NULL;
voidpf in_stream = NULL;
voidpf inflate_stream = NULL;
voidpf crc_in_stream = NULL;
uint32_t crc32 = 0;
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, "LICENSE.deflate", MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
uint64_t total_in = 0;
uint64_t total_out = 0;
mz_stream_zlib_create(&inflate_stream);
mz_stream_set_base(inflate_stream, in_stream);
mz_stream_zlib_open(inflate_stream, NULL, MZ_STREAM_MODE_READ);
read = mz_stream_read(inflate_stream, buf, UINT16_MAX);
mz_stream_close(inflate_stream);
total_in = mz_stream_zlib_get_total_in(inflate_stream);
total_out = mz_stream_zlib_get_total_out(inflate_stream);
mz_stream_zlib_delete(&inflate_stream);
mz_stream_os_close(in_stream);
printf("LICENSE uncompressed from %d to %d\n", (uint32_t)total_in, (uint32_t)total_out);
}
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
if (mz_stream_os_open(out_stream, "LICENSE.inflate", MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
mz_stream_crc32_create(&crc_in_stream);
mz_stream_crc32_open(crc_in_stream, NULL, MZ_STREAM_MODE_WRITE);
mz_stream_set_base(crc_in_stream, in_stream);
if (mz_stream_write(crc_in_stream, buf, read) != read)
printf("Failed to write LICENSE\n");
crc32 = mz_stream_crc32_get_value(crc_in_stream);
mz_stream_close(crc_in_stream);
mz_stream_crc32_delete(&crc_in_stream);
mz_stream_os_close(out_stream);
printf("LICENSE crc 0x%08x\n", crc32);
}
mz_stream_os_delete(&out_stream);
}
void test_deflate()
{
char buf[UINT16_MAX];
int16_t read = 0;
voidpf crc_in_stream = NULL;
voidpf in_stream = NULL;
voidpf out_stream = NULL;
voidpf deflate_stream = NULL;
uint32_t crc32 = 0;
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, "LICENSE", MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
mz_stream_crc32_create(&crc_in_stream);
mz_stream_set_base(crc_in_stream, in_stream);
mz_stream_crc32_open(crc_in_stream, NULL, MZ_STREAM_MODE_READ);
read = mz_stream_read(crc_in_stream, buf, UINT16_MAX);
crc32 = mz_stream_crc32_get_value(crc_in_stream);
mz_stream_close(crc_in_stream);
mz_stream_crc32_delete(&crc_in_stream);
mz_stream_os_close(in_stream);
}
mz_stream_os_delete(&in_stream);
if (read == MZ_STREAM_ERR)
{
printf("Failed to read LICENSE\n");
return;
}
printf("LICENSE crc 0x%08x\n", crc32);
mz_stream_os_create(&out_stream);
if (mz_stream_os_open(out_stream, "LICENSE.deflate", MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
uint64_t total_in = 0;
uint64_t total_out = 0;
mz_stream_zlib_create(&deflate_stream);
mz_stream_set_base(deflate_stream, out_stream);
mz_stream_zlib_open(deflate_stream, NULL, MZ_STREAM_MODE_WRITE);
mz_stream_zlib_write(deflate_stream, buf, read);
mz_stream_zlib_close(deflate_stream);
total_in = mz_stream_zlib_get_total_in(deflate_stream);
total_out = mz_stream_zlib_get_total_out(deflate_stream);
mz_stream_zlib_delete(&deflate_stream);
printf("LICENSE compressed from %d to %d\n", (uint32_t)total_in, (uint32_t)total_out);
mz_stream_os_close(out_stream);
}
mz_stream_os_delete(&out_stream);
}
#ifndef NOMAIN
int main(int argc, char *argv[])
{
zipFile zf = NULL;
voidpf stream = NULL;
void *zf = NULL;
void *stream = NULL;
char *zipfilename = NULL;
const char *password = NULL;
int zipfilenamearg = 0;
int errclose = 0;
int err = 0;
int i = 0;
int opt_overwrite = APPEND_STATUS_CREATE;
int opt_overwrite = MZ_APPEND_STATUS_CREATE;
int opt_compress_level = Z_DEFAULT_COMPRESSION;
int opt_exclude_path = 0;
//test_crypt();
//test_aes();
//test_deflate();
//test_inflate();
//test_zlib();
//test_bzip();
minizip_banner();
if (argc == 1)
{
@ -435,9 +170,9 @@ int main(int argc, char *argv[])
{
char c = *(p++);;
if ((c == 'o') || (c == 'O'))
opt_overwrite = APPEND_STATUS_CREATEAFTER;
opt_overwrite = MZ_APPEND_STATUS_CREATEAFTER;
if ((c == 'a') || (c == 'A'))
opt_overwrite = APPEND_STATUS_ADDINZIP;
opt_overwrite = MZ_APPEND_STATUS_ADDINZIP;
if ((c >= '0') && (c <= '9'))
opt_compress_level = (c - '0');
if ((c == 'j') || (c == 'J'))

112
mzstrm.c
View File

@ -130,11 +130,81 @@ int32_t mz_stream_write(void *stream, const void *buf, uint32_t size)
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->write == NULL)
return MZ_STREAM_ERR;
if (size == 0)
return size;
if (strm->is_open != NULL && strm->is_open(strm) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
return strm->write(strm, buf, size);
}
static int32_t mz_stream_write_value(void *stream, uint64_t value, uint32_t len)
{
uint8_t buf[8];
uint32_t n = 0;
for (n = 0; n < len; n++)
{
buf[n] = (uint8_t)(value & 0xff);
value >>= 8;
}
if (value != 0)
{
// Data overflow - hack for ZIP64 (X Roche)
for (n = 0; n < len; n++)
buf[n] = 0xff;
}
if (mz_stream_write(stream, buf, len) != len)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
}
int32_t mz_stream_write_uint8(void *stream, uint8_t value)
{
return mz_stream_write_value(stream, value, sizeof(uint8_t));
}
int32_t mz_stream_write_uint16(void *stream, uint16_t value)
{
return mz_stream_write_value(stream, value, sizeof(uint16_t));
}
int32_t mz_stream_write_uint32(void *stream, uint32_t value)
{
return mz_stream_write_value(stream, value, sizeof(uint32_t));
}
int32_t mz_stream_write_uint64(void *stream, uint64_t value)
{
return mz_stream_write_value(stream, value, sizeof(uint64_t));
}
int32_t mz_stream_copy(void *target, void *source, int32_t len)
{
uint8_t buf[UINT16_MAX];
int32_t bytes_to_copy = 0;
int32_t read = 0;
int32_t written = 0;
while (len > 0)
{
bytes_to_copy = len;
if (bytes_to_copy > UINT16_MAX)
bytes_to_copy = UINT16_MAX;
read = mz_stream_read(source, buf, bytes_to_copy);
if (read == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
written = mz_stream_write(target, buf, read);
if (written != read)
return MZ_STREAM_ERR;
len -= read;
}
return MZ_STREAM_OK;
}
int64_t mz_stream_tell(void *stream)
{
mz_stream *strm = (mz_stream *)stream;
@ -178,6 +248,22 @@ int32_t mz_stream_set_base(void *stream, void *base)
return MZ_STREAM_OK;
}
int64_t mz_stream_get_total_in(void *stream)
{
mz_stream *strm = (mz_stream *)stream;
if (strm->get_total_in == NULL)
return MZ_STREAM_ERR;
return strm->get_total_in(stream);
}
int64_t mz_stream_get_total_out(void *stream)
{
mz_stream *strm = (mz_stream *)stream;
if (strm->get_total_out == NULL)
return MZ_STREAM_ERR;
return strm->get_total_out(stream);
}
void *mz_stream_create(void **stream)
{
mz_stream *strm = NULL;
@ -198,6 +284,8 @@ void mz_stream_delete(void **stream)
typedef struct mz_stream_passthru_s {
mz_stream stream;
int64_t total_in;
int64_t total_out;
} mz_stream_passthru;
int32_t mz_stream_passthru_open(void *stream, const char *path, int mode)
@ -215,13 +303,19 @@ int32_t mz_stream_passthru_is_open(void *stream)
int32_t mz_stream_passthru_read(void *stream, void *buf, uint32_t size)
{
mz_stream_passthru *passthru = (mz_stream_passthru *)stream;
return mz_stream_read(passthru->stream.base, buf, size);
int32_t read = mz_stream_read(passthru->stream.base, buf, size);
if (read > 0)
passthru->total_in += read;
return read;
}
int32_t mz_stream_passthru_write(void *stream, const void *buf, uint32_t size)
{
mz_stream_passthru *passthru = (mz_stream_passthru *)stream;
return mz_stream_write(passthru->stream.base, buf, size);
int32_t written = mz_stream_write(passthru->stream.base, buf, size);
if (written > 0)
passthru->total_out += written;
return written;
}
int64_t mz_stream_passthru_tell(void *stream)
@ -248,6 +342,18 @@ int32_t mz_stream_passthru_error(void *stream)
return mz_stream_error(passthru->stream.error);
}
int64_t mz_stream_passthru_get_total_in(void *stream)
{
mz_stream_passthru *passthru = (mz_stream_passthru *)stream;
return passthru->total_in;
}
int64_t mz_stream_passthru_get_total_out(void *stream)
{
mz_stream_passthru *passthru = (mz_stream_passthru *)stream;
return passthru->total_out;
}
void *mz_stream_passthru_create(void **stream)
{
mz_stream_passthru *passthru = NULL;
@ -267,6 +373,8 @@ void *mz_stream_passthru_create(void **stream)
passthru->stream.error = mz_stream_passthru_error;
passthru->stream.create = mz_stream_passthru_create;
passthru->stream.delete = mz_stream_passthru_delete;
passthru->stream.get_total_in = mz_stream_passthru_get_total_in;
passthru->stream.get_total_out = mz_stream_passthru_get_total_out;
}
if (stream != NULL)
*stream = passthru;

View File

@ -22,6 +22,8 @@
extern "C" {
#endif
/***************************************************************************/
#define MZ_STREAM_SEEK_CUR (1)
#define MZ_STREAM_SEEK_END (2)
#define MZ_STREAM_SEEK_SET (0)
@ -32,8 +34,10 @@ extern "C" {
#define MZ_STREAM_MODE_EXISTING (4)
#define MZ_STREAM_MODE_CREATE (8)
#define MZ_STREAM_ERR (-1)
#define MZ_STREAM_OK (0)
#define MZ_STREAM_ERR (-1)
/***************************************************************************/
typedef int32_t (*mz_stream_open_cb) (void *stream, const char *path, int mode);
typedef int32_t (*mz_stream_is_open_cb) (void *stream);
@ -45,9 +49,14 @@ typedef int32_t (*mz_stream_close_cb) (void *stream);
typedef int32_t (*mz_stream_error_cb) (void *stream);
typedef void* (*mz_stream_create_cb) (void **stream);
typedef void (*mz_stream_delete_cb) (void **stream);
typedef int64_t (*mz_stream_get_total_in_cb) (void *stream);
typedef int64_t (*mz_stream_get_total_out_cb) (void *stream);
/***************************************************************************/
typedef struct mz_stream_s
{
struct mz_stream_s *base;
mz_stream_open_cb open;
mz_stream_is_open_cb is_open;
mz_stream_read_cb read;
@ -58,9 +67,12 @@ typedef struct mz_stream_s
mz_stream_error_cb error;
mz_stream_create_cb create;
mz_stream_delete_cb delete;
struct mz_stream_s *base;
mz_stream_get_total_in_cb get_total_in;
mz_stream_get_total_out_cb get_total_out;
} mz_stream;
/***************************************************************************/
int32_t mz_stream_open(void *stream, const char *path, int mode);
int32_t mz_stream_is_open(void *stream);
int32_t mz_stream_read(void *stream, void* buf, uint32_t size);
@ -69,12 +81,19 @@ int32_t mz_stream_read_uint16(void *stream, uint16_t *value);
int32_t mz_stream_read_uint32(void *stream, uint32_t *value);
int32_t mz_stream_read_uint64(void *stream, uint64_t *value);
int32_t mz_stream_write(void *stream, const void *buf, uint32_t size);
int32_t mz_stream_write_uint8(void *stream, uint8_t value);
int32_t mz_stream_write_uint16(void *stream, uint16_t value);
int32_t mz_stream_write_uint32(void *stream, uint32_t value);
int32_t mz_stream_write_uint64(void *stream, uint64_t value);
int32_t mz_stream_copy(void *target, void *source, int32_t len);
int64_t mz_stream_tell(void *stream);
int32_t mz_stream_seek(void *stream, uint64_t offset, int origin);
int32_t mz_stream_close(void *stream);
int32_t mz_stream_error(void *stream);
int32_t mz_stream_set_base(void *stream, void *base);
int64_t mz_stream_get_total_in(void *stream);
int64_t mz_stream_get_total_out(void *stream);
void* mz_stream_create(void **stream);
void mz_stream_delete(void **stream);
@ -82,6 +101,8 @@ void mz_stream_delete(void **stream);
void* mz_stream_passthru_create(void **stream);
void mz_stream_passthru_delete(void **stream);
/***************************************************************************/
#if !defined(_WIN32) && !defined(USEWIN32IOAPI)
#include "mzstrm_posix.h"
@ -119,6 +140,8 @@ void mz_stream_passthru_delete(void **stream);
int32_t mz_os_file_exists(const char *path);
int32_t mz_os_file_is_large(const char *path);
/***************************************************************************/
#ifdef __cplusplus
}
#endif

View File

@ -19,11 +19,9 @@
#include "aes/fileenc.h"
#include "aes/prng.h"
#define AES_PWVERIFYSIZE (2)
#define AES_AUTHCODESIZE (10)
#define AES_MAXSALTLENGTH (16)
#define AES_VERSION (0x0001)
#define AES_ENCRYPTIONMODE (0x03)
#define MZ_AES_PWVERIFYSIZE (2)
#define MZ_AES_AUTHCODESIZE (10)
#define MZ_AES_MAXSALTLENGTH (16)
typedef struct mz_stream_aes_s {
mz_stream stream;
@ -33,18 +31,19 @@ typedef struct mz_stream_aes_s {
int16_t error;
int16_t encryption_mode;
const char *password;
uint64_t total_in;
uint64_t total_out;
int64_t total_in;
int64_t total_out;
} mz_stream_aes;
int32_t mz_stream_aes_open(void *stream, const char *path, int mode)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
uint16_t salt_length = 0;
uint8_t verify[AES_PWVERIFYSIZE];
uint8_t verify_expected[AES_PWVERIFYSIZE];
uint8_t salt_value[AES_MAXSALTLENGTH];
uint8_t verify[MZ_AES_PWVERIFYSIZE];
uint8_t verify_expected[MZ_AES_PWVERIFYSIZE];
uint8_t salt_value[MZ_AES_MAXSALTLENGTH];
prng_ctx rng_ctx[1];
const char *password = path;
aes->total_in = 0;
aes->total_out = 0;
@ -52,7 +51,9 @@ int32_t mz_stream_aes_open(void *stream, const char *path, int mode)
if (mz_stream_is_open(aes->stream.base) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (aes->password == NULL)
if (password == NULL)
password = aes->password;
if (password == NULL)
return MZ_STREAM_ERR;
salt_length = SALT_LENGTH(aes->encryption_mode);
@ -63,8 +64,8 @@ int32_t mz_stream_aes_open(void *stream, const char *path, int mode)
prng_rand(salt_value, salt_length, rng_ctx);
prng_end(rng_ctx);
if (fcrypt_init(aes->encryption_mode, (uint8_t *)aes->password,
(uint32_t)strlen(aes->password), salt_value, verify, &aes->crypt_ctx) != 0)
if (fcrypt_init(aes->encryption_mode, (uint8_t *)password,
(uint32_t)strlen(password), salt_value, verify, &aes->crypt_ctx) != 0)
return MZ_STREAM_ERR;
if (mz_stream_write(aes->stream.base, salt_value, salt_length) != salt_length)
@ -72,10 +73,10 @@ int32_t mz_stream_aes_open(void *stream, const char *path, int mode)
aes->total_out += salt_length;
if (mz_stream_write(aes->stream.base, verify, AES_PWVERIFYSIZE) != AES_PWVERIFYSIZE)
if (mz_stream_write(aes->stream.base, verify, MZ_AES_PWVERIFYSIZE) != MZ_AES_PWVERIFYSIZE)
return MZ_STREAM_ERR;
aes->total_out += AES_PWVERIFYSIZE;
aes->total_out += MZ_AES_PWVERIFYSIZE;
}
else if (mode & MZ_STREAM_MODE_READ)
{
@ -84,16 +85,16 @@ int32_t mz_stream_aes_open(void *stream, const char *path, int mode)
aes->total_in += salt_length;
if (mz_stream_read(aes->stream.base, verify_expected, AES_PWVERIFYSIZE) != AES_PWVERIFYSIZE)
if (mz_stream_read(aes->stream.base, verify_expected, MZ_AES_PWVERIFYSIZE) != MZ_AES_PWVERIFYSIZE)
return MZ_STREAM_ERR;
aes->total_in += AES_PWVERIFYSIZE;
aes->total_in += MZ_AES_PWVERIFYSIZE;
if (fcrypt_init(aes->encryption_mode, (uint8_t *)aes->password,
(uint32_t)strlen(aes->password), salt_value, verify, &aes->crypt_ctx) != 0)
if (fcrypt_init(aes->encryption_mode, (uint8_t *)password,
(uint32_t)strlen(password), salt_value, verify, &aes->crypt_ctx) != 0)
return MZ_STREAM_ERR;
if (memcmp(verify_expected, verify, AES_PWVERIFYSIZE) != 0)
if (memcmp(verify_expected, verify, MZ_AES_PWVERIFYSIZE) != 0)
return MZ_STREAM_ERR;
}
@ -114,7 +115,7 @@ int32_t mz_stream_aes_is_open(void *stream)
int32_t mz_stream_aes_read(void *stream, void *buf, uint32_t size)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
uint32_t read = 0;
int32_t read = 0;
read = mz_stream_read(aes->stream.base, buf, size);
if (read > 0)
fcrypt_decrypt((uint8_t *)buf, read, &aes->crypt_ctx);
@ -125,7 +126,7 @@ int32_t mz_stream_aes_read(void *stream, void *buf, uint32_t size)
int32_t mz_stream_aes_write(void *stream, const void *buf, uint32_t size)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
uint32_t written = 0;
int32_t written = 0;
fcrypt_encrypt((uint8_t *)buf, size, &aes->crypt_ctx);
written = mz_stream_write(aes->stream.base, buf, size);
if (written > 0)
@ -148,28 +149,28 @@ int32_t mz_stream_aes_seek(void *stream, uint64_t offset, int origin)
int32_t mz_stream_aes_close(void *stream)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
unsigned char authcode[AES_AUTHCODESIZE];
unsigned char rauthcode[AES_AUTHCODESIZE];
unsigned char authcode[MZ_AES_AUTHCODESIZE];
unsigned char rauthcode[MZ_AES_AUTHCODESIZE];
if (aes->mode & MZ_STREAM_MODE_WRITE)
{
fcrypt_end(authcode, &aes->crypt_ctx);
if (mz_stream_write(aes->stream.base, authcode, AES_AUTHCODESIZE) != AES_AUTHCODESIZE)
if (mz_stream_write(aes->stream.base, authcode, MZ_AES_AUTHCODESIZE) != MZ_AES_AUTHCODESIZE)
return MZ_STREAM_ERR;
aes->total_out += AES_AUTHCODESIZE;
aes->total_out += MZ_AES_AUTHCODESIZE;
}
else if (aes->mode & MZ_STREAM_MODE_READ)
{
if (mz_stream_read(aes->stream.base, authcode, AES_AUTHCODESIZE) != AES_AUTHCODESIZE)
if (mz_stream_read(aes->stream.base, authcode, MZ_AES_AUTHCODESIZE) != MZ_AES_AUTHCODESIZE)
return MZ_STREAM_ERR;
aes->total_in += AES_AUTHCODESIZE;
aes->total_in += MZ_AES_AUTHCODESIZE;
if (fcrypt_end(rauthcode, &aes->crypt_ctx) != AES_AUTHCODESIZE)
if (fcrypt_end(rauthcode, &aes->crypt_ctx) != MZ_AES_AUTHCODESIZE)
return MZ_STREAM_ERR;
if (memcmp(authcode, rauthcode, AES_AUTHCODESIZE) != 0)
if (memcmp(authcode, rauthcode, MZ_AES_AUTHCODESIZE) != 0)
return MZ_STREAM_ERR;
}
@ -195,6 +196,18 @@ void mz_stream_aes_set_encryption_mode(void *stream, int16_t encryption_mode)
aes->encryption_mode = encryption_mode;
}
int64_t mz_stream_aes_get_total_in(void *stream)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
return aes->total_in;
}
int64_t mz_stream_aes_get_total_out(void *stream)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
return aes->total_out;
}
void *mz_stream_aes_create(void **stream)
{
mz_stream_aes *aes = NULL;
@ -214,7 +227,9 @@ void *mz_stream_aes_create(void **stream)
aes->stream.error = mz_stream_aes_error;
aes->stream.create = mz_stream_aes_create;
aes->stream.delete = mz_stream_aes_delete;
aes->encryption_mode = AES_ENCRYPTIONMODE;
aes->stream.get_total_in = mz_stream_aes_get_total_in;
aes->stream.get_total_out = mz_stream_aes_get_total_out;
aes->encryption_mode = MZ_AES_ENCRYPTIONMODE;
}
if (stream != NULL)
*stream = aes;

View File

@ -16,6 +16,14 @@
extern "C" {
#endif
/***************************************************************************/
#define MZ_AES_METHOD (99)
#define MZ_AES_VERSION (0x0001)
#define MZ_AES_ENCRYPTIONMODE (0x03)
/***************************************************************************/
int32_t mz_stream_aes_open(void *stream, const char* filename, int mode);
int32_t mz_stream_aes_read(void *stream, void* buf, uint32_t size);
int32_t mz_stream_aes_write(void *stream, const void* buf, uint32_t size);
@ -26,10 +34,14 @@ int32_t mz_stream_aes_error(void *stream);
void mz_stream_aes_set_password(void *stream, const char *password);
void mz_stream_aes_set_encryption_mode(void *stream, int16_t encryption_mode);
int64_t mz_stream_aes_get_total_in(void *stream);
int64_t mz_stream_aes_get_total_out(void *stream);
void* mz_stream_aes_create(void **stream);
void mz_stream_aes_delete(void **stream);
/***************************************************************************/
#ifdef __cplusplus
}
#endif

View File

@ -19,9 +19,7 @@
#include "mzstrm.h"
#include "mzstrm_buf.h"
#ifndef IOBUF_BUFFERSIZE
# define IOBUF_BUFFERSIZE (UINT16_MAX)
#endif
#define MZ_BUF_BUFFERSIZE (UINT16_MAX)
#if defined(_WIN32)
# include <conio.h>
@ -54,12 +52,12 @@ _x < _y ? _x : _y; })
typedef struct mz_stream_buffered_s {
mz_stream stream;
char readbuf[IOBUF_BUFFERSIZE];
char readbuf[MZ_BUF_BUFFERSIZE];
uint32_t readbuf_len;
uint32_t readbuf_pos;
uint32_t readbuf_hits;
uint32_t readbuf_misses;
char writebuf[IOBUF_BUFFERSIZE];
char writebuf[MZ_BUF_BUFFERSIZE];
uint32_t writebuf_len;
uint32_t writebuf_pos;
uint32_t writebuf_hits;
@ -109,7 +107,8 @@ int32_t mz_stream_buffered_flush(void *stream, uint32_t *written)
buffered->writebuf_misses += 1;
mz_stream_buffered_print(opaque, stream, "write flush [%d:%d len %d]\n", bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
mz_stream_buffered_print(opaque, stream, "write flush [%d:%d len %d]\n",
bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
total_bytes_written += bytes_written;
bytes_left_to_write -= bytes_written;
@ -141,13 +140,13 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, uint32_t size)
{
if ((buffered->readbuf_len == 0) || (buffered->readbuf_pos == buffered->readbuf_len))
{
if (buffered->readbuf_len == IOBUF_BUFFERSIZE)
if (buffered->readbuf_len == MZ_BUF_BUFFERSIZE)
{
buffered->readbuf_pos = 0;
buffered->readbuf_len = 0;
}
bytes_to_read = IOBUF_BUFFERSIZE - (buffered->readbuf_len - buffered->readbuf_pos);
bytes_to_read = MZ_BUF_BUFFERSIZE - (buffered->readbuf_len - buffered->readbuf_pos);
if (mz_stream_read(buffered->stream.base, buffered->readbuf + buffered->readbuf_pos, bytes_to_read) != bytes_to_read)
return 0;
@ -156,7 +155,8 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, uint32_t size)
buffered->readbuf_len += bytes_read;
buffered->position += bytes_read;
mz_stream_buffered_print(opaque, stream, "filled [read %d/%d buf %d:%d pos %lld]\n", bytes_read, bytes_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
mz_stream_buffered_print(opaque, stream, "filled [read %d/%d buf %d:%d pos %lld]\n",
bytes_read, bytes_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
if (bytes_read == 0)
break;
@ -173,7 +173,8 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, uint32_t size)
buffered->readbuf_hits += 1;
buffered->readbuf_pos += bytes_to_copy;
mz_stream_buffered_print(opaque, stream, "emptied [copied %d remaining %d buf %d:%d pos %lld]\n", bytes_to_copy, bytes_left_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
mz_stream_buffered_print(opaque, stream, "emptied [copied %d remaining %d buf %d:%d pos %lld]\n",
bytes_to_copy, bytes_left_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
}
}
@ -208,7 +209,7 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, uint32_t size)
while (bytes_left_to_write > 0)
{
bytes_to_copy = min(bytes_left_to_write, (uint32_t)(IOBUF_BUFFERSIZE - min(buffered->writebuf_len, buffered->writebuf_pos)));
bytes_to_copy = min(bytes_left_to_write, (uint32_t)(MZ_BUF_BUFFERSIZE - min(buffered->writebuf_len, buffered->writebuf_pos)));
if (bytes_to_copy == 0)
{
@ -222,7 +223,8 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, uint32_t size)
memcpy(buffered->writebuf + buffered->writebuf_pos, (char *)buf + (bytes_to_write - bytes_left_to_write), bytes_to_copy);
mz_stream_buffered_print(opaque, stream, "write copy [remaining %d write %d:%d len %d]\n", bytes_to_copy, bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
mz_stream_buffered_print(opaque, stream, "write copy [remaining %d write %d:%d len %d]\n",
bytes_to_copy, bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
bytes_left_to_write -= bytes_to_copy;
@ -239,7 +241,10 @@ int64_t mz_stream_buffered_tellinternal(void *stream, uint64_t position)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
buffered->position = position;
mz_stream_buffered_print(opaque, stream, "tell [pos %llu readpos %d writepos %d err %d]\n", buffered->position, buffered->readbuf_pos, buffered->writebuf_pos, errno);
mz_stream_buffered_print(opaque, stream, "tell [pos %llu readpos %d writepos %d err %d]\n",
buffered->position, buffered->readbuf_pos, buffered->writebuf_pos, errno);
if (buffered->readbuf_len > 0)
position -= (buffered->readbuf_len - buffered->readbuf_pos);
if (buffered->writebuf_len > 0)
@ -341,12 +346,18 @@ int32_t mz_stream_buffered_close(void *stream)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
uint32_t bytes_flushed = 0;
mz_stream_buffered_flush(stream, &bytes_flushed);
mz_stream_buffered_print(opaque, stream, "close\n");
if (buffered->readbuf_hits + buffered->readbuf_misses > 0)
mz_stream_buffered_print(opaque, stream, "read efficency %.02f%%\n", (buffered->readbuf_hits / ((float)buffered->readbuf_hits + buffered->readbuf_misses)) * 100);
mz_stream_buffered_print(opaque, stream, "read efficency %.02f%%\n",
(buffered->readbuf_hits / ((float)buffered->readbuf_hits + buffered->readbuf_misses)) * 100);
if (buffered->writebuf_hits + buffered->writebuf_misses > 0)
mz_stream_buffered_print(opaque, stream, "write efficency %.02f%%\n", (buffered->writebuf_hits / ((float)buffered->writebuf_hits + buffered->writebuf_misses)) * 100);
mz_stream_buffered_print(opaque, stream, "write efficency %.02f%%\n",
(buffered->writebuf_hits / ((float)buffered->writebuf_hits + buffered->writebuf_misses)) * 100);
return mz_stream_close(&buffered->stream.base);
}

View File

@ -19,6 +19,8 @@
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_buffered_open(void *stream, const char *path, int mode);
int32_t mz_stream_buffered_read(void *stream, void* buf, uint32_t size);
int32_t mz_stream_buffered_write(void *stream, const void *buf, uint32_t size);
@ -30,6 +32,8 @@ int32_t mz_stream_buffered_error(void *stream);
void* mz_stream_buffered_create(void **stream);
void mz_stream_buffered_delete(void **stream);
/***************************************************************************/
#ifdef __cplusplus
}
#endif

318
mzstrm_bzip.c Normal file
View File

@ -0,0 +1,318 @@
/* mzstrm_bzip.c -- Stream for bzip inflate/deflate
part of MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as bzip.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "bzip2\bzlib.h"
#include "mzstrm.h"
#include "mzstrm_bzip.h"
typedef struct mz_stream_bzip_s {
mz_stream stream;
bz_stream bzstream;
uint8_t buffer[UINT16_MAX];
int32_t buffer_len;
int64_t total_in;
int64_t total_out;
int8_t initialized;
int16_t level;
int16_t mode;
int16_t error;
} mz_stream_bzip;
extern void bz_internal_error(int errcode)
{
}
int32_t mz_stream_bzip_open(void *stream, const char *path, int mode)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
bzip->bzstream.bzalloc = 0;
bzip->bzstream.bzfree = 0;
bzip->bzstream.opaque = 0;
bzip->bzstream.total_in_lo32 = 0;
bzip->bzstream.total_in_hi32 = 0;
bzip->bzstream.total_out_lo32 = 0;
bzip->bzstream.total_out_hi32 = 0;
bzip->total_in = 0;
bzip->total_out = 0;
if (mode & MZ_STREAM_MODE_READ)
{
bzip->bzstream.next_in = bzip->buffer;
bzip->bzstream.avail_in = 0;
bzip->error = BZ2_bzDecompressInit(&bzip->bzstream, 0, 0);
}
else if (mode & MZ_STREAM_MODE_WRITE)
{
bzip->bzstream.next_out = bzip->buffer;
bzip->bzstream.avail_out = UINT16_MAX;
bzip->error = BZ2_bzCompressInit(&bzip->bzstream, bzip->level, 0, 35);
}
if (bzip->error != BZ_OK)
return MZ_STREAM_ERR;
bzip->initialized = 1;
bzip->mode = mode;
return MZ_STREAM_OK;
}
int32_t mz_stream_bzip_is_open(void *stream)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
if (bzip->initialized != 1)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
}
int32_t mz_stream_bzip_read(void *stream, void *buf, uint32_t size)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
uint64_t total_out_before = 0;
uint64_t total_out_after = 0;
uint32_t out_bytes = 0;
uint32_t total_out = 0;
int32_t read = 0;
int16_t err = BZ_OK;
bzip->bzstream.next_out = (uint8_t*)buf;
bzip->bzstream.avail_out = (uint16_t)size;
do
{
if (bzip->bzstream.avail_in == 0)
{
read = mz_stream_read(bzip->stream.base, bzip->buffer, UINT16_MAX);
if (mz_stream_error(bzip->stream.base))
{
bzip->error = BZ_IO_ERROR;
break;
}
if (read == 0)
break;
bzip->total_in += read;
bzip->bzstream.next_in = bzip->buffer;
bzip->bzstream.avail_in = read;
}
total_out_before = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
err = BZ2_bzDecompress(&bzip->bzstream);
total_out_after = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
out_bytes = (uint32_t)(total_out_after - total_out_before);
total_out += out_bytes;
if (err == BZ_STREAM_END)
break;
if (err != BZ_RUN_OK)
{
bzip->error = err;
break;
}
}
while (bzip->bzstream.avail_out > 0);
bzip->total_out += total_out;
return total_out;
}
int32_t mz_stream_bzip_flush(void *stream)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
if (mz_stream_write(bzip->stream.base, bzip->buffer, bzip->buffer_len) != bzip->buffer_len)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
}
uint32_t mz_stream_bzip_compress(void *stream, int flush)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
uint64_t total_out_before = 0;
uint64_t total_out_after = 0;
uint32_t out_bytes = 0;
int16_t err = BZ_OK;
total_out_before = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
err = BZ2_bzCompress(&bzip->bzstream, flush);
total_out_after = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
out_bytes = (uint32_t)(total_out_after - total_out_before);
if (err != BZ_OK && err != BZ_STREAM_END)
{
bzip->error = err;
return MZ_STREAM_ERR;
}
return out_bytes;
}
int32_t mz_stream_bzip_write(void *stream, const void *buf, uint32_t size)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
uint32_t out_bytes = 0;
uint32_t total_out = 0;
bzip->bzstream.next_in = (uint8_t*)buf;
bzip->bzstream.avail_in = size;
while ((bzip->error == BZ_OK) && (bzip->bzstream.avail_in > 0))
{
if (bzip->bzstream.avail_out == 0)
{
if (mz_stream_bzip_flush(bzip) == MZ_STREAM_ERR)
{
bzip->error = BZ_DATA_ERROR;
return 0;
}
bzip->bzstream.avail_out = UINT32_MAX;
bzip->bzstream.next_out = bzip->buffer;
bzip->buffer_len = 0;
}
out_bytes = mz_stream_bzip_compress(stream, BZ_RUN);
total_out += out_bytes;
bzip->buffer_len += out_bytes;
}
bzip->total_in += size;
bzip->total_out += total_out;
return size;
}
int64_t mz_stream_bzip_tell(void *stream)
{
mz_stream_bzip *mem = (mz_stream_bzip *)stream;
return MZ_STREAM_ERR;
}
int32_t mz_stream_bzip_seek(void *stream, uint64_t offset, int origin)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
return MZ_STREAM_ERR;
}
int32_t mz_stream_bzip_close(void *stream)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
uint32_t out_bytes = 0;
if (bzip->mode & MZ_STREAM_MODE_READ)
{
BZ2_bzDecompressEnd(&bzip->bzstream);
}
else if (bzip->mode & MZ_STREAM_MODE_WRITE)
{
out_bytes = mz_stream_bzip_compress(stream, BZ_FINISH);
bzip->buffer_len += out_bytes;
bzip->total_out += out_bytes;
mz_stream_bzip_flush(stream);
bzip->error = BZ2_bzCompressEnd(&bzip->bzstream);
}
bzip->initialized = 0;
if (bzip->error != BZ_OK)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
}
int32_t mz_stream_bzip_error(void *stream)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
return bzip->error;
}
void mz_stream_bzip_set_level(void *stream, int16_t level)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
bzip->level = level;
}
int64_t mz_stream_bzip_get_total_in(void *stream)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
return bzip->total_in;
}
int64_t mz_stream_bzip_get_total_out(void *stream)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
return bzip->total_out;
}
void *mz_stream_bzip_create(void **stream)
{
mz_stream_bzip *bzip = NULL;
bzip = (mz_stream_bzip *)malloc(sizeof(mz_stream_bzip));
if (bzip != NULL)
{
memset(bzip, 0, sizeof(mz_stream_bzip));
bzip->stream.open = mz_stream_bzip_open;
bzip->stream.is_open = mz_stream_bzip_is_open;
bzip->stream.read = mz_stream_bzip_read;
bzip->stream.write = mz_stream_bzip_write;
bzip->stream.tell = mz_stream_bzip_tell;
bzip->stream.seek = mz_stream_bzip_seek;
bzip->stream.close = mz_stream_bzip_close;
bzip->stream.error = mz_stream_bzip_error;
bzip->stream.create = mz_stream_bzip_create;
bzip->stream.delete = mz_stream_bzip_delete;
bzip->stream.get_total_in = mz_stream_bzip_get_total_in;
bzip->stream.get_total_out = mz_stream_bzip_get_total_out;
bzip->level = 6;
}
if (stream != NULL)
*stream = bzip;
return bzip;
}
void mz_stream_bzip_delete(void **stream)
{
mz_stream_bzip *bzip = NULL;
if (stream == NULL)
return;
bzip = (mz_stream_bzip *)*stream;
if (bzip != NULL)
free(bzip);
}

43
mzstrm_bzip.h Normal file
View File

@ -0,0 +1,43 @@
/* mzstrm_zlib.h -- Stream for zlib inflate/deflate
part of MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
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.
*/
#ifndef _MZ_STREAM_BZIP_H
#define _MZ_STREAM_BZIP_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_bzip_open(void *stream, const char* filename, int mode);
int32_t mz_stream_bzip_read(void *stream, void* buf, uint32_t size);
int32_t mz_stream_bzip_write(void *stream, const void* buf, uint32_t size);
int64_t mz_stream_bzip_tell(void *stream);
int32_t mz_stream_bzip_seek(void *stream, uint64_t offset, int origin);
int32_t mz_stream_bzip_close(void *stream);
int32_t mz_stream_bzip_error(void *stream);
void mz_stream_bzip_set_level(void *stream, int16_t level);
int64_t mz_stream_bzip_get_total_in(void *stream);
int64_t mz_stream_bzip_get_total_out(void *stream);
void* mz_stream_bzip_create(void **stream);
void mz_stream_bzip_delete(void **stream);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif

View File

@ -38,15 +38,15 @@
typedef struct mz_stream_crypt_s {
mz_stream stream;
uint32_t keys[3]; /* keys defining the pseudo-random sequence */
uint32_t keys[3]; // keys defining the pseudo-random sequence
const z_crc_t *crc_32_tab;
int16_t initialized;
int16_t error;
uint8_t verify1;
uint8_t verify2;
const char *password;
uint64_t total_in;
uint64_t total_out;
int64_t total_in;
int64_t total_out;
} mz_stream_crypt;
#define zdecode(keys,crc_32_tab,c) \
@ -84,6 +84,7 @@ void mz_stream_crypt_init_keys(const char *password, uint32_t *keys, const z_crc
*(keys+0) = 305419896L;
*(keys+1) = 591751049L;
*(keys+2) = 878082192L;
while (*password != 0)
{
mz_stream_crypt_update_keys(keys, crc_32_tab, *password);
@ -91,7 +92,7 @@ void mz_stream_crypt_init_keys(const char *password, uint32_t *keys, const z_crc
}
}
int32_t mz_stream_crypt_open(voidpf stream, const char *path, int mode)
int32_t mz_stream_crypt_open(void *stream, const char *path, int mode)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
uint16_t t = 0;
@ -99,6 +100,7 @@ int32_t mz_stream_crypt_open(voidpf stream, const char *path, int mode)
uint8_t header[RAND_HEAD_LEN];
uint8_t verify1 = 0;
uint8_t verify2 = 0;
const char *password = path;
crypt->total_in = 0;
crypt->total_out = 0;
@ -106,14 +108,17 @@ int32_t mz_stream_crypt_open(voidpf stream, const char *path, int mode)
if (mz_stream_is_open(crypt->stream.base) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (crypt->password == NULL)
if (password == NULL)
password = crypt->password;
if (password == NULL)
return MZ_STREAM_ERR;
crypt->crc_32_tab = get_crc_table();
if (crypt->crc_32_tab == NULL)
return MZ_STREAM_ERR;
mz_stream_crypt_init_keys(crypt->password, crypt->keys, crypt->crc_32_tab);
mz_stream_crypt_init_keys(password, crypt->keys, crypt->crc_32_tab);
if (mode & MZ_STREAM_MODE_WRITE)
{
@ -150,7 +155,7 @@ int32_t mz_stream_crypt_open(voidpf stream, const char *path, int mode)
return MZ_STREAM_OK;
}
int32_t mz_stream_crypt_is_open(voidpf stream)
int32_t mz_stream_crypt_is_open(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
if (crypt->initialized == 0)
@ -158,7 +163,7 @@ int32_t mz_stream_crypt_is_open(voidpf stream)
return MZ_STREAM_OK;
}
int32_t mz_stream_crypt_read(voidpf stream, void *buf, uint32_t size)
int32_t mz_stream_crypt_read(void *stream, void *buf, uint32_t size)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
uint8_t *buf_ptr = (uint8_t *)buf;
@ -172,7 +177,7 @@ int32_t mz_stream_crypt_read(voidpf stream, void *buf, uint32_t size)
return read;
}
int32_t mz_stream_crypt_write(voidpf stream, const void *buf, uint32_t size)
int32_t mz_stream_crypt_write(void *stream, const void *buf, uint32_t size)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
uint8_t *buf_ptr = (uint8_t *)buf;
@ -187,52 +192,64 @@ int32_t mz_stream_crypt_write(voidpf stream, const void *buf, uint32_t size)
return written;
}
int64_t mz_stream_crypt_tell(voidpf stream)
int64_t mz_stream_crypt_tell(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
return mz_stream_tell(crypt->stream.base);
}
int32_t mz_stream_crypt_seek(voidpf stream, uint64_t offset, int origin)
int32_t mz_stream_crypt_seek(void *stream, uint64_t offset, int origin)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
return mz_stream_seek(crypt->stream.base, offset, origin);
}
int32_t mz_stream_crypt_close(voidpf stream)
int32_t mz_stream_crypt_close(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
crypt->initialized = 0;
return MZ_STREAM_OK;
}
int32_t mz_stream_crypt_error(voidpf stream)
int32_t mz_stream_crypt_error(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
return crypt->error;
}
void mz_stream_crypt_set_password(voidpf stream, const char *password)
void mz_stream_crypt_set_password(void *stream, const char *password)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
crypt->password = password;
}
void mz_stream_crypt_set_verify(voidpf stream, uint8_t verify1, uint8_t verify2)
void mz_stream_crypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
crypt->verify1 = verify1;
crypt->verify2 = verify2;
}
void mz_stream_crypt_get_verify(voidpf stream, uint8_t *verify1, uint8_t *verify2)
void mz_stream_crypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
*verify1 = crypt->verify1;
*verify2 = crypt->verify2;
}
voidpf mz_stream_crypt_create(voidpf *stream)
int64_t mz_stream_crypt_get_total_in(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
return crypt->total_in;
}
int64_t mz_stream_crypt_get_total_out(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
return crypt->total_out;
}
void *mz_stream_crypt_create(void **stream)
{
mz_stream_crypt *crypt = NULL;
@ -251,14 +268,16 @@ voidpf mz_stream_crypt_create(voidpf *stream)
crypt->stream.error = mz_stream_crypt_error;
crypt->stream.create = mz_stream_crypt_create;
crypt->stream.delete = mz_stream_crypt_delete;
crypt->stream.get_total_in = mz_stream_crypt_get_total_in;
crypt->stream.get_total_out = mz_stream_crypt_get_total_out;
}
if (stream != NULL)
*stream = crypt;
return (voidpf)crypt;
return crypt;
}
void mz_stream_crypt_delete(voidpf *stream)
void mz_stream_crypt_delete(void **stream)
{
mz_stream_crypt *crypt = NULL;
if (stream == NULL)

View File

@ -36,6 +36,8 @@ int32_t mz_stream_crypt_error(void *stream);
void mz_stream_crypt_set_password(void *stream, const char *password);
void mz_stream_crypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2);
void mz_stream_crypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2);
int64_t mz_stream_crypt_get_total_in(void *stream);
int64_t mz_stream_crypt_get_total_out(void *stream);
void* mz_stream_crypt_create(void **stream);
void mz_stream_crypt_delete(void **stream);

View File

@ -29,7 +29,8 @@ typedef struct mz_stream_mem_s {
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
int8_t grow; // Memory buffer can grow
uint32_t grow_size; // Size to grow when full
} mz_stream_mem;
int32_t mz_stream_mem_open(void *stream, const char *path, int mode)
@ -38,9 +39,9 @@ int32_t mz_stream_mem_open(void *stream, const char *path, int mode)
if (mode & MZ_STREAM_MODE_CREATE)
{
if (mem->growable)
if (mem->grow)
{
mem->size = UINT16_MAX;
mem->size = mem->grow_size;
mem->buffer = (char *)malloc(mem->size);
}
@ -84,13 +85,16 @@ int32_t mz_stream_mem_write(void *stream, const void *buf, uint32_t size)
uint32_t new_size = 0;
char *new_buf = NULL;
if (size == 0)
return size;
if (size > mem->size - mem->position)
{
if (mem->growable)
if (mem->grow)
{
new_size = mem->size;
if (size < UINT16_MAX)
new_size += UINT16_MAX;
if (size < mem->grow_size)
new_size += mem->grow_size;
else
new_size += size;
@ -169,10 +173,16 @@ void mz_stream_mem_set_buffer(void *stream, void *buf, uint32_t size)
mem->size = size;
}
void mz_stream_mem_set_growable(void *stream, int growable)
void mz_stream_mem_set_grow(void *stream, int8_t grow)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
mem->growable = growable;
mem->grow = grow;
}
void mz_stream_mem_set_grow_size(void *stream, uint32_t grow_size)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
mem->grow_size = grow_size;
}
void *mz_stream_mem_create(void **stream)
@ -194,8 +204,9 @@ void *mz_stream_mem_create(void **stream)
mem->stream.error = mz_stream_mem_error;
mem->stream.create = mz_stream_mem_create;
mem->stream.delete = mz_stream_mem_delete;
mem->grow_size = UINT16_MAX;
}
if (stream == NULL)
if (stream != NULL)
*stream = mem;
return mem;
@ -209,7 +220,7 @@ void mz_stream_mem_delete(void **stream)
mem = (mz_stream_mem *)*stream;
if (mem != NULL)
{
if (mem->growable && mem->buffer != NULL)
if (mem->grow && mem->buffer != NULL)
free(mem->buffer);
free(mem);
}

View File

@ -19,6 +19,8 @@
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_mem_open(void *stream, const char* filename, int mode);
int32_t mz_stream_mem_read(void *stream, void* buf, uint32_t size);
int32_t mz_stream_mem_write(void *stream, const void* buf, uint32_t size);
@ -28,11 +30,14 @@ int32_t mz_stream_mem_close(void *stream);
int32_t mz_stream_mem_error(void *stream);
void mz_stream_mem_set_buffer(void *stream, void *buf, uint32_t size);
void mz_stream_mem_set_growable(void *stream, int growable);
void mz_stream_mem_set_grow(void *stream, int8_t grow);
void mz_stream_mem_set_grow_size(void *stream, uint32_t grow_size);
void* mz_stream_mem_create(void **stream);
void mz_stream_mem_delete(void **stream);
/***************************************************************************/
#ifdef __cplusplus
}
#endif

View File

@ -22,6 +22,8 @@
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_win32_open(void *stream, const char *path, int mode);
int32_t mz_stream_win32_is_open(void *stream);
int32_t mz_stream_win32_read(void *stream, void* buf, uint32_t size);
@ -36,6 +38,8 @@ void mz_stream_win32_delete(void **stream);
int32_t mz_win32_rand(uint8_t *buf, uint32_t size);
/***************************************************************************/
#ifdef __cplusplus
}
#endif

View File

@ -32,8 +32,8 @@ typedef struct mz_stream_zlib_s {
z_stream zstream;
uint8_t buffer[UINT16_MAX];
int32_t buffer_len;
uint64_t total_in;
uint64_t total_out;
int64_t total_in;
int64_t total_out;
int8_t initialized;
int16_t level;
int16_t window_bits;
@ -221,7 +221,7 @@ int32_t mz_stream_zlib_write(void *stream, const void *buf, uint32_t size)
zlib->total_in += size;
zlib->total_out += total_out;
return total_out;
return size;
}
int64_t mz_stream_zlib_tell(void *stream)
@ -294,13 +294,13 @@ void mz_stream_zlib_set_strategy(void *stream, int16_t strategy)
zlib->strategy = strategy;
}
uint64_t mz_stream_zlib_get_total_in(void *stream)
int64_t mz_stream_zlib_get_total_in(void *stream)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
return zlib->total_in;
}
uint64_t mz_stream_zlib_get_total_out(void *stream)
int64_t mz_stream_zlib_get_total_out(void *stream)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
return zlib->total_out;
@ -325,6 +325,8 @@ void *mz_stream_zlib_create(void **stream)
zlib->stream.error = mz_stream_zlib_error;
zlib->stream.create = mz_stream_zlib_create;
zlib->stream.delete = mz_stream_zlib_delete;
zlib->stream.get_total_in = mz_stream_zlib_get_total_in;
zlib->stream.get_total_out = mz_stream_zlib_get_total_out;
zlib->level = Z_DEFAULT_COMPRESSION;
zlib->window_bits = -MAX_WBITS;
zlib->mem_level = DEF_MEM_LEVEL;
@ -333,7 +335,7 @@ void *mz_stream_zlib_create(void **stream)
if (stream != NULL)
*stream = zlib;
return (voidpf)zlib;
return zlib;
}
void mz_stream_zlib_delete(void **stream)
@ -349,9 +351,9 @@ void mz_stream_zlib_delete(void **stream)
typedef struct mz_stream_crc32_s {
mz_stream stream;
int8_t initialized;
uint32_t value;
uint64_t total_in;
uint64_t total_out;
int32_t value;
int64_t total_in;
int64_t total_out;
} mz_stream_crc32;
int32_t mz_stream_crc32_open(void *stream, const char *path, int mode)
@ -415,19 +417,19 @@ int32_t mz_stream_crc32_error(void *stream)
return mz_stream_error(crc32->stream.base);
}
uint32_t mz_stream_crc32_get_value(void *stream)
int32_t mz_stream_crc32_get_value(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return crc32->value;
}
uint64_t mz_stream_crc32_get_total_in(void *stream)
int64_t mz_stream_crc32_get_total_in(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return crc32->total_in;
}
uint64_t mz_stream_crc32_get_total_out(void *stream)
int64_t mz_stream_crc32_get_total_out(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return crc32->total_out;
@ -452,11 +454,13 @@ void *mz_stream_crc32_create(void **stream)
crc32->stream.error = mz_stream_crc32_error;
crc32->stream.create = mz_stream_crc32_create;
crc32->stream.delete = mz_stream_crc32_delete;
crc32->stream.get_total_in = mz_stream_crc32_get_total_in;
crc32->stream.get_total_out = mz_stream_crc32_get_total_out;
}
if (stream != NULL)
*stream = crc32;
return (voidpf)crc32;
return crc32;
}
void mz_stream_crc32_delete(void **stream)

View File

@ -17,6 +17,8 @@
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_zlib_open(void *stream, const char* filename, int mode);
int32_t mz_stream_zlib_read(void *stream, void* buf, uint32_t size);
int32_t mz_stream_zlib_write(void *stream, const void* buf, uint32_t size);
@ -29,12 +31,14 @@ void mz_stream_zlib_set_level(void *stream, int16_t level);
void mz_stream_zlib_set_window_bits(void *stream, int16_t window_bits);
void mz_stream_zlib_set_mem_level(void *stream, int16_t mem_level);
void mz_stream_zlib_set_strategy(void *stream, int16_t strategy);
uint64_t mz_stream_zlib_get_total_in(void *stream);
uint64_t mz_stream_zlib_get_total_out(void *stream);
int64_t mz_stream_zlib_get_total_in(void *stream);
int64_t mz_stream_zlib_get_total_out(void *stream);
void* mz_stream_zlib_create(void **stream);
void mz_stream_zlib_delete(void **stream);
/***************************************************************************/
int32_t mz_stream_crc32_open(void *stream, const char* filename, int mode);
int32_t mz_stream_crc32_read(void *stream, void* buf, uint32_t size);
int32_t mz_stream_crc32_write(void *stream, const void* buf, uint32_t size);
@ -43,13 +47,15 @@ int32_t mz_stream_crc32_seek(void *stream, uint64_t offset, int origin);
int32_t mz_stream_crc32_close(void *stream);
int32_t mz_stream_crc32_error(void *stream);
uint32_t mz_stream_crc32_get_value(void *stream);
uint64_t mz_stream_crc32_get_total_in(void *stream);
uint64_t mz_stream_crc32_get_total_out(void *stream);
int32_t mz_stream_crc32_get_value(void *stream);
int64_t mz_stream_crc32_get_total_in(void *stream);
int64_t mz_stream_crc32_get_total_out(void *stream);
void* mz_stream_crc32_create(void **stream);
void mz_stream_crc32_delete(void **stream);
/***************************************************************************/
#ifdef __cplusplus
}
#endif

219
test.c Normal file
View File

@ -0,0 +1,219 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include "mzstrm.h"
#include "mzstrm_bzip.h"
#include "mzstrm_crypt.h"
#include "mzstrm_aes.h"
#include "mzstrm_zlib.h"
void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password)
{
char buf[UINT16_MAX];
int16_t read = 0;
int16_t written = 0;
void *out_stream = NULL;
void *in_stream = NULL;
void *crypt_out_stream = NULL;
char filename[120];
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, "LICENSE", MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
read = mz_stream_os_read(in_stream, buf, UINT16_MAX);
mz_stream_os_close(in_stream);
}
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
snprintf(filename, sizeof(filename), "LICENSE.encrypt.%s", method);
if (mz_stream_os_open(out_stream, filename, MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
crypt_create(&crypt_out_stream);
mz_stream_set_base(crypt_out_stream, out_stream);
if (mz_stream_open(crypt_out_stream, password, MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
written = mz_stream_write(crypt_out_stream, buf, read);
mz_stream_close(crypt_out_stream);
}
mz_stream_delete(&crypt_out_stream);
mz_stream_os_close(out_stream);
printf("%s encrypted %d\n", filename, written);
}
mz_stream_os_delete(&out_stream);
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, filename, MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
crypt_create(&crypt_out_stream);
mz_stream_set_base(crypt_out_stream, in_stream);
if (mz_stream_open(crypt_out_stream, password, MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
read = mz_stream_read(crypt_out_stream, buf, read);
mz_stream_close(crypt_out_stream);
}
mz_stream_delete(&crypt_out_stream);
mz_stream_os_close(in_stream);
printf("%s decrypted %d\n", filename, read);
}
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
snprintf(filename, sizeof(filename), "LICENSE.decrypt.%s", method);
if (mz_stream_os_open(out_stream, filename, MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
mz_stream_os_write(out_stream, buf, read);
mz_stream_os_close(out_stream);
}
mz_stream_os_delete(&out_stream);
}
void test_compress(char *method, mz_stream_create_cb create_compress)
{
char buf[UINT16_MAX];
int16_t read = 0;
uint64_t total_in = 0;
uint64_t total_out = 0;
void *crc_in_stream = NULL;
void *in_stream = NULL;
void *out_stream = NULL;
void *deflate_stream = NULL;
void *inflate_stream = NULL;
uint32_t crc32 = 0;
char filename[120];
printf("Testing compress %s\n", method);
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, "LICENSE", MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
mz_stream_crc32_create(&crc_in_stream);
mz_stream_set_base(crc_in_stream, in_stream);
mz_stream_crc32_open(crc_in_stream, NULL, MZ_STREAM_MODE_READ);
read = mz_stream_read(crc_in_stream, buf, UINT16_MAX);
crc32 = mz_stream_crc32_get_value(crc_in_stream);
mz_stream_close(crc_in_stream);
mz_stream_crc32_delete(&crc_in_stream);
mz_stream_os_close(in_stream);
}
mz_stream_os_delete(&in_stream);
if (read == MZ_STREAM_ERR)
{
printf("Failed to read LICENSE\n");
return;
}
printf("LICENSE crc 0x%08x\n", crc32);
mz_stream_os_create(&out_stream);
snprintf(filename, sizeof(filename), "LICENSE.deflate.%s", method);
if (mz_stream_os_open(out_stream, filename, MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
create_compress(&deflate_stream);
mz_stream_set_base(deflate_stream, out_stream);
mz_stream_open(deflate_stream, NULL, MZ_STREAM_MODE_WRITE);
mz_stream_write(deflate_stream, buf, read);
mz_stream_close(deflate_stream);
total_in = mz_stream_get_total_in(deflate_stream);
total_out = mz_stream_get_total_out(deflate_stream);
mz_stream_delete(&deflate_stream);
printf("%s compressed from %d to %d\n", filename, (uint32_t)total_in, (uint32_t)total_out);
mz_stream_os_close(out_stream);
}
mz_stream_os_delete(&out_stream);
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, filename, MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
{
create_compress(&inflate_stream);
mz_stream_set_base(inflate_stream, in_stream);
mz_stream_open(inflate_stream, NULL, MZ_STREAM_MODE_READ);
read = mz_stream_read(inflate_stream, buf, UINT16_MAX);
mz_stream_close(inflate_stream);
total_in = mz_stream_get_total_in(inflate_stream);
total_out = mz_stream_get_total_out(inflate_stream);
mz_stream_delete(&inflate_stream);
mz_stream_os_close(in_stream);
printf("%s uncompressed from %d to %d\n", filename, (uint32_t)total_in, (uint32_t)total_out);
}
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
snprintf(filename, sizeof(filename), "LICENSE.inflate.%s", method);
if (mz_stream_os_open(out_stream, filename, MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_STREAM_OK)
{
mz_stream_crc32_create(&crc_in_stream);
mz_stream_crc32_open(crc_in_stream, NULL, MZ_STREAM_MODE_WRITE);
mz_stream_set_base(crc_in_stream, in_stream);
if (mz_stream_write(crc_in_stream, buf, read) != read)
printf("Failed to write %s\n", filename);
crc32 = mz_stream_crc32_get_value(crc_in_stream);
mz_stream_close(crc_in_stream);
mz_stream_delete(&crc_in_stream);
mz_stream_os_close(out_stream);
printf("%s crc 0x%08x\n", filename, crc32);
}
mz_stream_os_delete(&out_stream);
}
void test_aes()
{
test_encrypt("aes", mz_stream_aes_create, "hello");
}
void test_crypt()
{
test_encrypt("crypt", mz_stream_crypt_create, "hello");
}
void test_zlib()
{
test_compress("zlib", mz_stream_zlib_create);
}
void test_bzip()
{
test_compress("bzip", mz_stream_bzip_create);
}

18
test.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef _MZ_TEST_H
#define _MZ_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
void test_aes();
void test_crypt();
void test_inflate();
void test_deflate();
void test_bzip();
#ifdef __cplusplus
}
#endif
#endif

46
unzip.c
View File

@ -27,10 +27,10 @@
#include "unzip.h"
#ifdef HAVE_AES
# define AES_METHOD (99)
# define AES_PWVERIFYSIZE (2)
# define AES_MAXSALTLENGTH (16)
# define AES_AUTHCODESIZE (10)
# define MZ_AES_METHOD (99)
# define MZ_AES_PWVERIFYSIZE (2)
# define MZ_AES_MAXSALTLENGTH (16)
# define MZ_AES_AUTHCODESIZE (10)
# define AES_HEADERSIZE (11)
# define AES_KEYSIZE(mode) (64 + (mode * 64))
@ -847,7 +847,7 @@ static int unzCheckCurrentFileCoherencyHeader(unz64_internal *s, uint32_t *psize
compression_method = s->cur_file_info.compression_method;
#ifdef HAVE_AES
if (compression_method == AES_METHOD)
if (compression_method == MZ_AES_METHOD)
compression_method = s->cur_file_info_internal.aes_compression_method;
#endif
@ -923,7 +923,7 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in
compression_method = s->cur_file_info.compression_method;
#ifdef HAVE_AES
if (compression_method == AES_METHOD)
if (compression_method == MZ_AES_METHOD)
{
compression_method = s->cur_file_info_internal.aes_compression_method;
if (password == NULL)
@ -1064,11 +1064,11 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in
MZ_STREAM_SEEK_SET) != 0)
return UNZ_INTERNALERROR;
#ifdef HAVE_AES
if (s->cur_file_info.compression_method == AES_METHOD)
if (s->cur_file_info.compression_method == MZ_AES_METHOD)
{
unsigned char passverify_archive[AES_PWVERIFYSIZE];
unsigned char passverify_password[AES_PWVERIFYSIZE];
unsigned char salt_value[AES_MAXSALTLENGTH];
unsigned char passverify_archive[MZ_AES_PWVERIFYSIZE];
unsigned char passverify_password[MZ_AES_PWVERIFYSIZE];
unsigned char salt_value[MZ_AES_MAXSALTLENGTH];
uint32_t salt_length = 0;
if ((s->cur_file_info_internal.aes_encryption_mode < 1) ||
@ -1079,19 +1079,19 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in
if (mz_stream_read(s->stream, salt_value, salt_length) != salt_length)
return UNZ_INTERNALERROR;
else if (mz_stream_read(s->stream, passverify_archive, AES_PWVERIFYSIZE) != AES_PWVERIFYSIZE)
else if (mz_stream_read(s->stream, passverify_archive, MZ_AES_PWVERIFYSIZE) != MZ_AES_PWVERIFYSIZE)
return UNZ_INTERNALERROR;
fcrypt_init(s->cur_file_info_internal.aes_encryption_mode, (uint8_t *)password,
(uint32_t)strlen(password), salt_value, passverify_password, &s->pfile_in_zip_read->aes_ctx);
if (memcmp(passverify_archive, passverify_password, AES_PWVERIFYSIZE) != 0)
if (memcmp(passverify_archive, passverify_password, MZ_AES_PWVERIFYSIZE) != 0)
return UNZ_BADPASSWORD;
s->pfile_in_zip_read->rest_read_compressed -= salt_length + AES_PWVERIFYSIZE;
s->pfile_in_zip_read->rest_read_compressed -= AES_AUTHCODESIZE;
s->pfile_in_zip_read->rest_read_compressed -= salt_length + MZ_AES_PWVERIFYSIZE;
s->pfile_in_zip_read->rest_read_compressed -= MZ_AES_AUTHCODESIZE;
s->pfile_in_zip_read->pos_in_zipfile += salt_length + AES_PWVERIFYSIZE;
s->pfile_in_zip_read->pos_in_zipfile += salt_length + MZ_AES_PWVERIFYSIZE;
}
else
#endif
@ -1221,7 +1221,7 @@ extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len)
if ((s->cur_file_info.flag & 1) != 0)
{
#ifdef HAVE_AES
if (s->cur_file_info.compression_method == AES_METHOD)
if (s->cur_file_info.compression_method == MZ_AES_METHOD)
{
fcrypt_decrypt(s->pfile_in_zip_read->read_buffer, bytes_to_read, &s->pfile_in_zip_read->aes_ctx);
}
@ -1467,21 +1467,21 @@ extern int ZEXPORT unzCloseCurrentFile(unzFile file)
return UNZ_PARAMERROR;
#ifdef HAVE_AES
if (s->cur_file_info.compression_method == AES_METHOD)
if (s->cur_file_info.compression_method == MZ_AES_METHOD)
{
unsigned char authcode[AES_AUTHCODESIZE];
unsigned char rauthcode[AES_AUTHCODESIZE];
unsigned char authcode[MZ_AES_AUTHCODESIZE];
unsigned char rauthcode[MZ_AES_AUTHCODESIZE];
if (mz_stream_read(s->stream, authcode, AES_AUTHCODESIZE) != AES_AUTHCODESIZE)
if (mz_stream_read(s->stream, authcode, MZ_AES_AUTHCODESIZE) != MZ_AES_AUTHCODESIZE)
return UNZ_ERRNO;
if (fcrypt_end(rauthcode, &s->pfile_in_zip_read->aes_ctx) != AES_AUTHCODESIZE)
if (fcrypt_end(rauthcode, &s->pfile_in_zip_read->aes_ctx) != MZ_AES_AUTHCODESIZE)
err = UNZ_CRCERROR;
if (memcmp(authcode, rauthcode, AES_AUTHCODESIZE) != 0)
if (memcmp(authcode, rauthcode, MZ_AES_AUTHCODESIZE) != 0)
err = UNZ_CRCERROR;
}
/* AES zip version AE-1 will expect a valid crc as well */
if ((s->cur_file_info.compression_method != AES_METHOD) ||
if ((s->cur_file_info.compression_method != MZ_AES_METHOD) ||
(s->cur_file_info_internal.aes_version == 0x0001))
#endif
{

View File

@ -131,9 +131,11 @@
<ClCompile Include="..\..\..\zutil.c" />
<ClCompile Include="..\crypt.c" />
<ClCompile Include="..\lib\bzip2\bzlib.c" />
<ClCompile Include="..\lib\bzip2\crctable.c" />
<ClCompile Include="..\mzstrm.c" />
<ClCompile Include="..\mzstrm_aes.c" />
<ClCompile Include="..\mzstrm_buf.c" />
<ClCompile Include="..\mzstrm_bzip.c" />
<ClCompile Include="..\mzstrm_crypt.c" />
<ClCompile Include="..\mzstrm_mem.c" />
<ClCompile Include="..\mzstrm_posix.c" />
@ -150,12 +152,11 @@
<ClCompile Include="..\lib\aes\sha1.c" />
<ClCompile Include="..\lib\bzip2\blocksort.c" />
<ClCompile Include="..\lib\bzip2\compress.c" />
<ClCompile Include="..\lib\bzip2\crctable.c" />
<ClCompile Include="..\lib\bzip2\decompress.c" />
<ClCompile Include="..\lib\bzip2\huffman.c" />
<ClCompile Include="..\lib\bzip2\mk251.c" />
<ClCompile Include="..\lib\bzip2\randtable.c" />
<ClCompile Include="..\lib\bzip2\unzcrash.c" />
<ClCompile Include="..\mz_compat.c" />
<ClCompile Include="..\test.c" />
<ClCompile Include="..\unzip.c" />
<ClCompile Include="..\zip.c" />
</ItemGroup>
@ -171,9 +172,11 @@
<ClInclude Include="..\..\..\zlib.h" />
<ClInclude Include="..\..\..\zutil.h" />
<ClInclude Include="..\crypt.h" />
<ClInclude Include="..\lib\bzip2\bzlib_private.h" />
<ClInclude Include="..\mzstrm.h" />
<ClInclude Include="..\mzstrm_aes.h" />
<ClInclude Include="..\mzstrm_buf.h" />
<ClInclude Include="..\mzstrm_bzip.h" />
<ClInclude Include="..\mzstrm_crypt.h" />
<ClInclude Include="..\mzstrm_mem.h" />
<ClInclude Include="..\mzstrm_posix.h" />
@ -191,6 +194,8 @@
<ClInclude Include="..\lib\aes\pwd2key.h" />
<ClInclude Include="..\lib\aes\sha1.h" />
<ClInclude Include="..\lib\bzip2\bzlib.h" />
<ClInclude Include="..\mz_compat.h" />
<ClInclude Include="..\test.h" />
<ClInclude Include="..\unzip.h" />
<ClInclude Include="..\zip.h" />
</ItemGroup>

View File

@ -15,7 +15,7 @@
<Filter Include="AES">
<UniqueIdentifier>{a41d606c-337c-4c8f-aa15-65aafd88063d}</UniqueIdentifier>
</Filter>
<Filter Include="BZip2">
<Filter Include="BZip">
<UniqueIdentifier>{fa8c9130-e0d5-4563-a4dc-bb3b0668d94c}</UniqueIdentifier>
</Filter>
</ItemGroup>
@ -90,28 +90,19 @@
<Filter>AES</Filter>
</ClCompile>
<ClCompile Include="..\lib\bzip2\blocksort.c">
<Filter>BZip2</Filter>
<Filter>BZip</Filter>
</ClCompile>
<ClCompile Include="..\lib\bzip2\compress.c">
<Filter>BZip2</Filter>
</ClCompile>
<ClCompile Include="..\lib\bzip2\crctable.c">
<Filter>BZip2</Filter>
<Filter>BZip</Filter>
</ClCompile>
<ClCompile Include="..\lib\bzip2\decompress.c">
<Filter>BZip2</Filter>
<Filter>BZip</Filter>
</ClCompile>
<ClCompile Include="..\lib\bzip2\huffman.c">
<Filter>BZip2</Filter>
</ClCompile>
<ClCompile Include="..\lib\bzip2\mk251.c">
<Filter>BZip2</Filter>
<Filter>BZip</Filter>
</ClCompile>
<ClCompile Include="..\lib\bzip2\randtable.c">
<Filter>BZip2</Filter>
</ClCompile>
<ClCompile Include="..\lib\bzip2\unzcrash.c">
<Filter>BZip2</Filter>
<Filter>BZip</Filter>
</ClCompile>
<ClCompile Include="..\mzstrm.c">
<Filter>Source Files</Filter>
@ -138,7 +129,19 @@
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\bzip2\bzlib.c">
<Filter>BZip2</Filter>
<Filter>BZip</Filter>
</ClCompile>
<ClCompile Include="..\mz_compat.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\test.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\mzstrm_bzip.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\bzip2\crctable.c">
<Filter>BZip</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
@ -215,7 +218,7 @@
<Filter>AES</Filter>
</ClInclude>
<ClInclude Include="..\lib\bzip2\bzlib.h">
<Filter>BZip2</Filter>
<Filter>BZip</Filter>
</ClInclude>
<ClInclude Include="..\mzstrm.h">
<Filter>Header Files</Filter>
@ -241,5 +244,17 @@
<ClInclude Include="..\mzstrm_win32.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\mz_compat.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\test.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\mzstrm_bzip.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\lib\bzip2\bzlib_private.h">
<Filter>BZip</Filter>
</ClInclude>
</ItemGroup>
</Project>

1630
zip.c

File diff suppressed because it is too large Load Diff

173
zip.h
View File

@ -14,8 +14,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _ZIP_H
#define _ZIP_H
#ifndef _MZ_ZIP_H
#define _MZ_ZIP_H
#ifdef __cplusplus
extern "C" {
@ -35,14 +35,7 @@ extern "C" {
#define Z_BZIP2ED 12
#if defined(STRICTZIP) || defined(STRICTZIPUNZIP)
/* like the STRICT of WIN32, we define a pointer that cannot be converted
from (void*) without cast */
typedef struct TagzipFile__ { int unused; } zip_file__;
typedef zip_file__ *zipFile;
#else
typedef voidp zipFile;
#endif
/***************************************************************************/
#define ZIP_OK (0)
#define ZIP_EOF (0)
@ -51,128 +44,70 @@ typedef voidp zipFile;
#define ZIP_BADZIPFILE (-103)
#define ZIP_INTERNALERROR (-104)
#ifdef __GNUC__
# define ZIP_UNUSED __attribute__((__unused__))
#else
# define ZIP_UNUSED
#endif
/***************************************************************************/
typedef struct
typedef struct mz_zip_file_s
{
uint32_t dos_date;
uint16_t internal_fa; /* internal file attributes 2 bytes */
uint32_t external_fa; /* external file attributes 4 bytes */
} zip_fileinfo;
uint32_t dos_date; // ms-dos date and time
uint16_t internal_fa; // internal file attributes
uint32_t external_fa; // external file attributes
const uint8_t *extrafield_local; // extra fields in local header
uint16_t extrafield_local_size; // size of additional extra fields in local header
const uint8_t *extrafield_global; // extra fields in global header
uint16_t extrafield_global_size; // size of extra fields in global header
uint16_t version_madeby; // version made by
const char *comment; // file comment
const char *filename; // filename
uint8_t zip64; // enable zip64 extensions if 1
uint16_t flag; // base flag value
} mz_zip_file;
#define APPEND_STATUS_CREATE (0)
#define APPEND_STATUS_CREATEAFTER (1)
#define APPEND_STATUS_ADDINZIP (2)
typedef struct mz_zip_compress_s
{
uint16_t method; // compression method ie Z_DEFLATE
int level; // compression level
uint8_t raw; // no compression method if 1
int window_bits; // deflate window bits
int mem_level; // deflate memory level
int strategy; // deflate strategy
} mz_zip_compress;
typedef struct mz_zip_crypt_s
{
#if defined(HAVE_AES)
uint8_t aes; // enable winzip aes encryption if 1
#endif
uint32_t crc_for_crypting; // crc to use for traditional encryption
const char *password; // encryption password
} mz_zip_crypt;
/***************************************************************************/
/* Writing a zip file */
extern zipFile ZEXPORT zipOpen(const char *path, int append, voidpf stream);
/* Create a zipfile.
#define MZ_APPEND_STATUS_CREATE (0) // create new zip
#define MZ_APPEND_STATUS_CREATEAFTER (1) // create zip after file
#define MZ_APPEND_STATUS_ADDINZIP (2) // add existing files to zip
path should contain the full path (by example, on a Windows XP computer
"c:\\zlib\\zlib113.zip" or on an Unix computer "zlib/zlib113.zip".
/***************************************************************************/
return NULL if zipfile cannot be opened
return zipFile handle if no error
// Create a zipfile
extern void* ZEXPORT mz_zip_open(const char *path, int append, uint64_t disk_size,
const char **globalcomment, void *stream);
If the file path exist and append == APPEND_STATUS_CREATEAFTER, the zip
will be created at the end of the file. (useful if the file contain a self extractor code)
If the file path exist and append == APPEND_STATUS_ADDINZIP, we will add files in existing
zip (be sure you don't add file that doesn't exist)
// Open a file in the ZIP for writing
extern int ZEXPORT mz_zip_entry_open(void *handle, const mz_zip_file *file_info,
const mz_zip_compress *compress_info, const mz_zip_crypt *crypt_info);
NOTE: There is no delete function into a zipfile. If you want delete file into a zipfile,
you must open a zipfile, and create another. Of course, you can use RAW reading and writing to copy
the file you did not want delete. */
// Write data in the zipfile
extern int ZEXPORT mz_zip_entry_write(void *handle, const void *buf, uint32_t len);
extern zipFile ZEXPORT zipOpen2(const char *path, int append, const char **globalcomment,
voidpf stream);
// Close the current file in the zipfile
extern int ZEXPORT mz_zip_entry_close(void *handle);
extern zipFile ZEXPORT zipOpen3(const char *path, int append, uint64_t disk_size,
const char **globalcomment, voidpf stream);
/* Same as zipOpen2 but allows specification of spanned zip size */
// Close the current file in the zipfile where raw is compressed data
extern int ZEXPORT mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32);
extern int ZEXPORT zipOpenNewFileInZip(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t method, int level);
/* Open a file in the ZIP for writing.
filename : the filename in zip (if NULL, '-' without quote will be used
*zipfi contain supplemental information
extrafield_local buffer to store the local header extra field data, can be NULL
size_extrafield_local size of extrafield_local buffer
extrafield_global buffer to store the global header extra field data, can be NULL
size_extrafield_global size of extrafield_local buffer
comment buffer for comment string
method contain the compression method (0 for store, Z_DEFLATED for deflate)
level contain the level of compression (can be Z_DEFAULT_COMPRESSION)
zip64 is set to 1 if a zip64 extended information block should be added to the local file header.
this MUST be '1' if the uncompressed size is >= 0xffffffff. */
extern int ZEXPORT zipOpenNewFileInZip64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int zip64);
/* Same as zipOpenNewFileInZip with zip64 support */
extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int raw);
/* Same as zipOpenNewFileInZip, except if raw=1, we write raw file */
extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int raw, int zip64);
/* Same as zipOpenNewFileInZip3 with zip64 support */
extern int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int raw, int windowBits, int memLevel,
int strategy, const char *password, ZIP_UNUSED uint32_t crc_for_crypting);
/* Same as zipOpenNewFileInZip2, except
windowBits, memLevel, strategy : see parameter strategy in deflateInit2
password : crypting password (NULL for no crypting)
crc_for_crypting : crc of file to compress (needed for crypting) */
extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int raw, int windowBits, int memLevel,
int strategy, const char *password, ZIP_UNUSED uint32_t crc_for_crypting, int zip64);
/* Same as zipOpenNewFileInZip3 with zip64 support */
extern int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int raw, int windowBits, int memLevel,
int strategy, const char *password, ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base);
/* Same as zipOpenNewFileInZip3 except versionMadeBy & flag fields */
extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int raw, int windowBits, int memLevel,
int strategy, const char *password, ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
/* Same as zipOpenNewFileInZip4 with zip64 support */
extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len);
/* Write data in the zipfile */
extern int ZEXPORT zipCloseFileInZip(zipFile file);
/* Close the current file in the zipfile */
extern int ZEXPORT zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32);
extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, uint32_t crc32);
/* Close the current file in the zipfile, for file opened with parameter raw=1 in zipOpenNewFileInZip2
where raw is compressed data. Parameters uncompressed_size and crc32 are value for the uncompressed data. */
extern int ZEXPORT zipClose(zipFile file, const char *global_comment);
/* Close the zipfile */
extern int ZEXPORT zipClose_64(zipFile file, const char *global_comment);
extern int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby);
/* Same as zipClose_64 except version_madeby field */
// Close the zipfile
extern int ZEXPORT mz_zip_close(void *handle, const char *global_comment, uint16_t version_madeby);
/***************************************************************************/