Refactored unzip functionality.

This commit is contained in:
Nathan Moinvaziri 2017-10-03 21:56:07 -07:00
parent b1badee350
commit 4138ca5144
26 changed files with 2044 additions and 2354 deletions

161
crypt.c
View File

@ -1,161 +0,0 @@
/* crypt.c -- base code for traditional PKWARE encryption
Version 1.2.0, September 16th, 2017
Copyright (C) 2012-2017 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2005 Gilles Vollant
Modifications for Info-ZIP crypting
http://www.winimage.com/zLibDll/minizip.html
Copyright (C) 2003 Terry Thorsen
This code is a modified version of crypting code in Info-ZIP distribution
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
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.
This encryption code is a direct transcription of the algorithm from
Roger Schlafly, described by Phil Katz in the file appnote.txt. This
file (appnote.txt) is distributed with the PKZIP program (even in the
version without encryption capabilities).
If you don't need crypting in your application, just define symbols
NOCRYPT and NOUNCRYPT.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#ifdef _WIN32
# include <windows.h>
# include <wincrypt.h>
#else
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
#endif
#include "zlib.h"
#include "crypt.h"
/***************************************************************************/
#define CRC32(c, b) ((*(pcrc_32_tab+(((uint32_t)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
#ifndef ZCR_SEED2
# define ZCR_SEED2 3141592654UL /* use PI as default pattern */
#endif
/***************************************************************************/
uint8_t decrypt_byte(uint32_t *pkeys)
{
unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
* unpredictable manner on 16-bit systems; not a problem
* with any known compiler so far, though */
temp = ((uint32_t)(*(pkeys+2)) & 0xffff) | 2;
return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
}
uint8_t update_keys(uint32_t *pkeys, const z_crc_t *pcrc_32_tab, int32_t c)
{
(*(pkeys+0)) = (uint32_t)CRC32((*(pkeys+0)), c);
(*(pkeys+1)) += (*(pkeys+0)) & 0xff;
(*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
{
register int32_t keyshift = (int32_t)((*(pkeys + 1)) >> 24);
(*(pkeys+2)) = (uint32_t)CRC32((*(pkeys+2)), keyshift);
}
return c;
}
void init_keys(const char *passwd, uint32_t *pkeys, const z_crc_t *pcrc_32_tab)
{
*(pkeys+0) = 305419896L;
*(pkeys+1) = 591751049L;
*(pkeys+2) = 878082192L;
while (*passwd != 0)
{
update_keys(pkeys, pcrc_32_tab, *passwd);
passwd += 1;
}
}
/***************************************************************************/
int cryptrand(unsigned char *buf, unsigned int len)
{
static unsigned calls = 0;
int rlen = 0;
#ifdef _WIN32
HCRYPTPROV provider;
unsigned __int64 pentium_tsc[1];
int result = 0;
if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
result = CryptGenRandom(provider, len, buf);
CryptReleaseContext(provider, 0);
if (result)
return len;
}
for (rlen = 0; rlen < (int)len; ++rlen)
{
if (rlen % 8 == 0)
QueryPerformanceCounter((LARGE_INTEGER *)pentium_tsc);
buf[rlen] = ((unsigned char*)pentium_tsc)[rlen % 8];
}
#else
int frand = open("/dev/urandom", O_RDONLY);
if (frand != -1)
{
rlen = (int)read(frand, buf, len);
close(frand);
}
#endif
if (rlen < (int)len)
{
/* Ensure different random header each time */
if (++calls == 1)
srand((unsigned)(time(NULL) ^ ZCR_SEED2));
while (rlen < (int)len)
buf[rlen++] = (rand() >> 7) & 0xff;
}
return rlen;
}
int crypthead(const char *passwd, uint8_t *buf, int buf_size, uint32_t *pkeys,
const z_crc_t *pcrc_32_tab, uint8_t verify1, uint8_t verify2)
{
uint8_t n = 0; /* index in random header */
uint8_t header[RAND_HEAD_LEN-2]; /* random header */
uint16_t t = 0; /* temporary */
if (buf_size < RAND_HEAD_LEN)
return 0;
init_keys(passwd, pkeys, pcrc_32_tab);
/* First generate RAND_HEAD_LEN-2 random bytes. */
cryptrand(header, RAND_HEAD_LEN-2);
/* Encrypt random header (last two bytes is high word of crc) */
init_keys(passwd, pkeys, pcrc_32_tab);
for (n = 0; n < RAND_HEAD_LEN-2; n++)
buf[n] = (uint8_t)zencode(pkeys, pcrc_32_tab, header[n], t);
buf[n++] = (uint8_t)zencode(pkeys, pcrc_32_tab, verify1, t);
buf[n++] = (uint8_t)zencode(pkeys, pcrc_32_tab, verify2, t);
return n;
}
/***************************************************************************/

64
crypt.h
View File

@ -1,64 +0,0 @@
/* crypt.h -- base code for traditional PKWARE encryption
Version 1.2.0, September 16th, 2017
Copyright (C) 2012-2017 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2005 Gilles Vollant
Modifications for Info-ZIP crypting
http://www.winimage.com/zLibDll/minizip.html
Copyright (C) 2003 Terry Thorsen
This code is a modified version of crypting code in Info-ZIP distribution
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
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 _MINICRYPT_H
#define _MINICRYPT_H
#if ZLIB_VERNUM < 0x1270
typedef unsigned long z_crc_t;
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define RAND_HEAD_LEN 12
/***************************************************************************/
#define zdecode(pkeys,pcrc_32_tab,c) \
(update_keys(pkeys,pcrc_32_tab, c ^= decrypt_byte(pkeys)))
#define zencode(pkeys,pcrc_32_tab,c,t) \
(t = decrypt_byte(pkeys), update_keys(pkeys,pcrc_32_tab,c), t^(c))
/***************************************************************************/
/* Return the next byte in the pseudo-random sequence */
uint8_t decrypt_byte(uint32_t *pkeys);
/* Update the encryption keys with the next byte of plain text */
uint8_t update_keys(uint32_t *pkeys, const z_crc_t *pcrc_32_tab, int32_t c);
/* Initialize the encryption keys and the random header according to the given password. */
void init_keys(const char *passwd, uint32_t *pkeys, const z_crc_t *pcrc_32_tab);
/* Generate cryptographically secure random numbers */
int cryptrand(unsigned char *buf, unsigned int len);
/* Create encryption header */
int crypthead(const char *passwd, uint8_t *buf, int buf_size, uint32_t *pkeys,
const z_crc_t *pcrc_32_tab, uint8_t verify1, uint8_t verify2);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif

View File

@ -34,6 +34,7 @@
#include "unzip.h"
#include "mz_compat.h"
#include "mzstrm.h"
#include "minishared.h"
@ -141,6 +142,7 @@ int miniunz_extract_currentfile(unzFile uf, int opt_extract_without_path, int *p
unz_file_info64 file_info = {0};
void* buf = NULL;
uint16_t size_buf = 8192;
int32_t read = 0;
int err = UNZ_OK;
int errclose = UNZ_OK;
int skip = 0;
@ -220,7 +222,7 @@ int miniunz_extract_currentfile(unzFile uf, int opt_extract_without_path, int *p
if ((skip == 0) && (err == UNZ_OK))
{
/* Some zips don't contain directory alone before file */
if ((mz_stream_os_open(stream_entry, write_filename, MZ_STREAM_MODE_WRITE) == MZ_STREAM_ERR) &&
if ((mz_stream_os_open(stream_entry, write_filename, MZ_STREAM_MODE_CREATE) != MZ_OK) &&
(opt_extract_without_path == 0) &&
(filename_withoutpath != (char*)filename_inzip))
{
@ -229,33 +231,33 @@ int miniunz_extract_currentfile(unzFile uf, int opt_extract_without_path, int *p
makedir(write_filename);
*(filename_withoutpath-1) = c;
mz_stream_os_open(stream_entry, write_filename, MZ_STREAM_MODE_WRITE);
mz_stream_os_open(stream_entry, write_filename, MZ_STREAM_MODE_CREATE);
}
}
/* Read from the zip, unzip to buffer, and write to disk */
if (mz_stream_os_is_open(stream_entry))
if (mz_stream_os_is_open(stream_entry) == MZ_OK)
{
printf(" extracting: %s\n", write_filename);
do
while (1)
{
err = unzReadCurrentFile(uf, buf, size_buf);
if (err < 0)
read = unzReadCurrentFile(uf, buf, size_buf);
if (read < 0)
{
err = read;
printf("error %d with zipfile in unzReadCurrentFile\n", err);
break;
}
if (err == 0)
if (read == 0)
break;
if (mz_stream_os_write(stream_entry, buf, 1) != 1)
if (mz_stream_os_write(stream_entry, buf, read) != read)
{
printf("error %d in writing extracted file\n", errno);
err = UNZ_ERRNO;
printf("error %d in writing extracted file\n", err);
break;
}
}
while (err > 0);
mz_stream_os_close(stream_entry);

View File

@ -85,7 +85,7 @@ int minizip_addfile(void *zf, const char *path, const char *filenameinzip, int l
}
else
{
if (mz_stream_os_open(stream_entry, path, MZ_STREAM_MODE_READ) == MZ_STREAM_ERR)
if (mz_stream_os_open(stream_entry, path, MZ_STREAM_MODE_READ) != MZ_OK)
{
err = ZIP_ERRNO;
printf("error in opening %s for reading\n", path);

366
mz_compat.c Normal file
View File

@ -0,0 +1,366 @@
/* mzstrm.c -- Stream interface
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Modifications for Zip64 support
Copyright (C) 2009-2010 Mathias Svensson
http://result42.com
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
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.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "zip.h"
#include "unzip.h"
#include "mz_compat.h"
#ifndef VERSIONMADEBY
# define VERSIONMADEBY (0x0) // platform dependent
#endif
extern zipFile ZEXPORT zipOpen(const char *path, int append, voidpf stream)
{
return zipOpen2(path, append, NULL, stream);
}
extern zipFile ZEXPORT zipOpen2(const char *path, int append, const char **globalcomment, voidpf stream)
{
return zipOpen3(path, append, 0, globalcomment, stream);
}
extern zipFile ZEXPORT zipOpen3(const char *path, int append, uint64_t disk_size, const char **globalcomment, voidpf stream)
{
zipFile file = mz_zip_open(path, append, disk_size, stream);
if (file != NULL && globalcomment != NULL)
mz_zip_get_global_comment(file, globalcomment);
return file;
}
extern int ZEXPORT zipOpenNewFileInZip5(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 compression_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)
{
mz_zip_file file_info;
if (zipfi != NULL)
{
file_info.dos_date = zipfi->dos_date;
file_info.external_fa = zipfi->external_fa;
file_info.internal_fa = zipfi->internal_fa;
}
file_info.filename = filename;
file_info.extrafield_local = extrafield_local;
file_info.extrafield_local_size = size_extrafield_local;
file_info.extrafield_global = extrafield_global;
file_info.extrafield_global_size = size_extrafield_global;
file_info.version_madeby = version_madeby;
file_info.comment = comment;
file_info.flag = flag_base;
file_info.zip64 = zip64;
mz_zip_compress compress_info;
compress_info.level = level;
compress_info.window_bits = windowBits;
compress_info.mem_level = memLevel;
compress_info.strategy = strategy;
compress_info.method = compression_method;
mz_zip_crypt crypt_info;
#ifdef HAVE_AES
crypt_info.aes = 1;
#endif
crypt_info.password = password;
return mz_zip_entry_open(file, &file_info, &compress_info, &crypt_info);
}
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)
{
return zipOpenNewFileInZip5(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global, size_extrafield_global,
comment, method, level, raw, windowBits, memLevel, strategy, password, crc_for_crypting, version_madeby, flag_base, zip64);
}
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)
{
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, method, level, raw, windowBits, memLevel,
strategy, password, crc_for_crypting, version_madeby, flag_base, 0);
}
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)
{
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, method, level, raw, windowBits, memLevel,
strategy, password, crc_for_crypting, VERSIONMADEBY, 0, 0);
}
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)
{
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, method, level, raw, windowBits, memLevel, strategy,
password, crc_for_crypting, VERSIONMADEBY, 0, zip64);
}
extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len)
{
return mz_zip_entry_write(file, buf, len);
}
extern int ZEXPORT zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32)
{
return mz_zip_entry_close_raw(file, uncompressed_size, crc32);
}
extern int ZEXPORT zipCloseFileInZip(zipFile file)
{
return zipCloseFileInZipRaw(file, 0, 0);
}
extern int ZEXPORT zipCloseFileInZip64(zipFile file)
{
return zipCloseFileInZipRaw(file, 0, 0);
}
extern int ZEXPORT zipClose(zipFile file, const char *global_comment)
{
return zipClose_64(file, global_comment);
}
extern int ZEXPORT zipClose_64(zipFile file, const char *global_comment)
{
return zipClose2_64(file, global_comment, VERSIONMADEBY);
}
extern int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby)
{
return mz_zip_close(file, global_comment, version_madeby);
}
extern unzFile ZEXPORT unzOpen(const char *path, void *stream)
{
return mz_unzip_open(path, stream);
}
extern int ZEXPORT unzClose(unzFile file)
{
return mz_unzip_close(file);
}
extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password)
{
if (method != NULL)
*method = 0;
if (level != NULL)
*level = 0;
return mz_unzip_entry_open(file, raw, password);
}
extern int ZEXPORT unzOpenCurrentFile(void *file)
{
return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
}
extern int ZEXPORT unzOpenCurrentFilePassword(void *file, const char *password)
{
return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
}
extern int ZEXPORT unzOpenCurrentFile2(void *file, int *method, int *level, int raw)
{
return unzOpenCurrentFile3(file, method, level, raw, NULL);
}
extern int ZEXPORT unzGoToFirstFile(void *file)
{
return mz_unzip_goto_first_entry(file);
}
extern int ZEXPORT unzGoToNextFile(void *file)
{
return mz_unzip_goto_next_entry(file);
}
/* Read bytes from the current file.
buf contain buffer where data must be copied
len the size of buf.
return the number of byte copied if some bytes are copied
return 0 if the end of file was reached
return <0 with error code if there is an error (UNZ_ERRNO for IO error, or zLib error for uncompress error) */
extern int ZEXPORT unzReadCurrentFile(voidpf file, voidp buf, uint32_t len)
{
return mz_unzip_entry_read(file, buf, len);
}
extern int ZEXPORT unzGetCurrentFileInfo(voidpf file, unz_file_info *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
{
mz_unzip_file *file_info;
int16_t bytes_to_copy = 0;
int err = MZ_OK;
err = mz_unzip_entry_get_info(file, &file_info);
if ((err == MZ_OK) && (pfile_info != NULL))
{
pfile_info->version = file_info->version;
pfile_info->version_needed = file_info->version_needed;
pfile_info->flag = file_info->flag;
pfile_info->compression_method = file_info->compression_method;
pfile_info->dos_date = file_info->dos_date;
pfile_info->crc = file_info->crc;
pfile_info->size_filename = file_info->filename_size;
pfile_info->size_file_extra = file_info->extrafield_size;
pfile_info->size_file_comment = file_info->comment_size;
pfile_info->disk_num_start = (uint16_t)file_info->disk_num_start;
pfile_info->internal_fa = file_info->internal_fa;
pfile_info->external_fa = file_info->external_fa;
pfile_info->compressed_size = (uint32_t)file_info->compressed_size;
pfile_info->uncompressed_size = (uint32_t)file_info->uncompressed_size;
if (filename_size > 0 && filename != NULL)
{
bytes_to_copy = filename_size;
if (bytes_to_copy > file_info->filename_size)
bytes_to_copy = file_info->filename_size;
memcpy(filename, file_info->filename, bytes_to_copy);
}
if (extrafield_size > 0 && extrafield != NULL)
{
bytes_to_copy = extrafield_size;
if (bytes_to_copy > file_info->extrafield_size)
bytes_to_copy = file_info->extrafield_size;
memcpy(extrafield, file_info->extrafield, bytes_to_copy);
}
if (comment_size > 0 && comment != NULL)
{
bytes_to_copy = comment_size;
if (bytes_to_copy > file_info->comment_size)
bytes_to_copy = file_info->comment_size;
memcpy(comment, file_info->comment, bytes_to_copy);
}
}
return err;
}
extern int ZEXPORT unzGetCurrentFileInfo64(voidpf file, unz_file_info64 * pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
{
mz_unzip_file *file_info;
int16_t bytes_to_copy = 0;
int err = MZ_OK;
err = mz_unzip_entry_get_info(file, &file_info);
if ((err == MZ_OK) && (pfile_info != NULL))
{
pfile_info->version = file_info->version;
pfile_info->version_needed = file_info->version_needed;
pfile_info->flag = file_info->flag;
pfile_info->compression_method = file_info->compression_method;
pfile_info->dos_date = file_info->dos_date;
pfile_info->crc = file_info->crc;
pfile_info->size_filename = file_info->filename_size;
pfile_info->size_file_extra = file_info->extrafield_size;
pfile_info->size_file_comment = file_info->comment_size;
pfile_info->disk_num_start = file_info->disk_num_start;
pfile_info->internal_fa = file_info->internal_fa;
pfile_info->external_fa = file_info->external_fa;
pfile_info->compressed_size = file_info->compressed_size;
pfile_info->uncompressed_size = file_info->uncompressed_size;
if (filename_size > 0 && filename != NULL)
{
bytes_to_copy = filename_size;
if (bytes_to_copy > file_info->filename_size)
bytes_to_copy = file_info->filename_size;
memcpy(filename, file_info->filename, bytes_to_copy);
}
if (extrafield_size > 0 && extrafield != NULL)
{
bytes_to_copy = extrafield_size;
if (bytes_to_copy > file_info->extrafield_size)
bytes_to_copy = file_info->extrafield_size;
memcpy(extrafield, file_info->extrafield, bytes_to_copy);
}
if (comment_size > 0 && comment != NULL)
{
bytes_to_copy = comment_size;
if (bytes_to_copy > file_info->comment_size)
bytes_to_copy = file_info->comment_size;
memcpy(comment, file_info->comment, bytes_to_copy);
}
}
return err;
}
extern int ZEXPORT unzCloseCurrentFile(unzFile file)
{
return mz_unzip_entry_close(file);
}
extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func)
{
return mz_unzip_locate_entry(file, filename, filename_compare_func);
}
extern int ZEXPORT unzGetGlobalInfo(voidpf file, unz_global_info* pglobal_info32)
{
unz_global_info64 global_info64;
int err = unzGetGlobalInfo64(file, &global_info64);
if (err != UNZ_OK)
{
pglobal_info32->number_entry = (uint32_t)global_info64.number_entry;
pglobal_info32->size_comment = global_info64.size_comment;
pglobal_info32->number_disk_with_CD = global_info64.number_disk_with_CD;
}
return MZ_OK;
}
extern int ZEXPORT unzGetGlobalInfo64(voidpf file, unz_global_info64 *pglobal_info)
{
mz_unzip_global global_info;
int err = mz_unzip_get_global_info(file, &global_info);
if (err == MZ_OK)
{
pglobal_info->size_comment = global_info.comment_size;
pglobal_info->number_entry = global_info.number_entry;
pglobal_info->number_disk_with_CD = global_info.number_disk_with_CD;
}
return err;
}
extern int ZEXPORT unzGetGlobalComment(voidpf file, char *comment, uint16_t comment_size)
{
return mz_unzip_get_global_comment(file, comment, comment_size);
}

391
mz_compat.h Normal file
View File

@ -0,0 +1,391 @@
/* mz_compat.h -- Backwards compatible interface for older versions of MiniZip
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
http://result42.com
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
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_COMPAT_H
#define _MZ_COMPAT_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __GNUC__
# define ZIP_UNUSED __attribute__((__unused__))
#else
# define ZIP_UNUSED
#endif
#ifndef DEF_MEM_LEVEL
# if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
# else
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
# endif
#endif
#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
typedef struct
{
uint32_t dos_date;
uint16_t internal_fa; /* internal file attributes 2 bytes */
uint32_t external_fa; /* external file attributes 4 bytes */
} zip_fileinfo;
#define ZIP_OK (0)
#define ZIP_EOF (0)
#define ZIP_ERRNO (Z_ERRNO)
#define ZIP_PARAMERROR (-102)
#define ZIP_BADZIPFILE (-103)
#define ZIP_INTERNALERROR (-104)
#define APPEND_STATUS_CREATE (MZ_APPEND_STATUS_CREATE)
#define APPEND_STATUS_CREATEAFTER (MZ_APPEND_STATUS_CREATEAFTER)
#define APPEND_STATUS_ADDINZIP (MZ_APPEND_STATUS_ADDINZIP)
extern zipFile ZEXPORT zipOpen(const char *path, int append, voidpf stream);
/* Create a zipfile.
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
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)
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. */
extern zipFile ZEXPORT zipOpen2(const char *path, int append, const char **globalcomment,
voidpf stream);
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 */
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);
/* 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 */
#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
/* like the STRICT of WIN32, we define a pointer that cannot be converted
from (void*) without cast */
typedef struct TagunzFile__ { int unused; } unz_file__;
typedef unz_file__ *unzFile;
#else
typedef voidp unzFile;
#endif
#define ZIP_OK (0)
#define ZIP_EOF (0)
#define ZIP_ERRNO (Z_ERRNO)
#define ZIP_PARAMERROR (-102)
#define ZIP_BADZIPFILE (-103)
#define ZIP_INTERNALERROR (-104)
#define UNZ_OK (0)
#define UNZ_END_OF_LIST_OF_FILE (-100)
#define UNZ_ERRNO (Z_ERRNO)
#define UNZ_EOF (0)
#define UNZ_PARAMERROR (-102)
#define UNZ_BADZIPFILE (-103)
#define UNZ_INTERNALERROR (-104)
#define UNZ_CRCERROR (-105)
#define UNZ_BADPASSWORD (-106)
/* unz_global_info structure contain global data about the ZIPfile
These data comes from the end of central dir */
typedef struct unz_global_info64_s
{
uint64_t number_entry; /* total number of entries in the central dir on this disk */
uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP*/
uint16_t size_comment; /* size of the global comment of the zipfile */
} unz_global_info64;
typedef struct unz_global_info_s
{
uint32_t number_entry; /* total number of entries in the central dir on this disk */
uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP*/
uint16_t size_comment; /* size of the global comment of the zipfile */
} unz_global_info;
/* unz_file_info contain information about a file in the zipfile */
typedef struct unz_file_info64_s
{
uint16_t version; /* version made by 2 bytes */
uint16_t version_needed; /* version needed to extract 2 bytes */
uint16_t flag; /* general purpose bit flag 2 bytes */
uint16_t compression_method; /* compression method 2 bytes */
uint32_t dos_date; /* last mod file date in Dos fmt 4 bytes */
uint32_t crc; /* crc-32 4 bytes */
uint64_t compressed_size; /* compressed size 8 bytes */
uint64_t uncompressed_size; /* uncompressed size 8 bytes */
uint16_t size_filename; /* filename length 2 bytes */
uint16_t size_file_extra; /* extra field length 2 bytes */
uint16_t size_file_comment; /* file comment length 2 bytes */
uint32_t disk_num_start; /* disk number start 4 bytes */
uint16_t internal_fa; /* internal file attributes 2 bytes */
uint32_t external_fa; /* external file attributes 4 bytes */
uint64_t disk_offset;
uint16_t size_file_extra_internal;
} unz_file_info64;
typedef struct unz_file_info_s
{
uint16_t version; /* version made by 2 bytes */
uint16_t version_needed; /* version needed to extract 2 bytes */
uint16_t flag; /* general purpose bit flag 2 bytes */
uint16_t compression_method; /* compression method 2 bytes */
uint32_t dos_date; /* last mod file date in Dos fmt 4 bytes */
uint32_t crc; /* crc-32 4 bytes */
uint32_t compressed_size; /* compressed size 4 bytes */
uint32_t uncompressed_size; /* uncompressed size 4 bytes */
uint16_t size_filename; /* filename length 2 bytes */
uint16_t size_file_extra; /* extra field length 2 bytes */
uint16_t size_file_comment; /* file comment length 2 bytes */
uint16_t disk_num_start; /* disk number start 2 bytes */
uint16_t internal_fa; /* internal file attributes 2 bytes */
uint32_t external_fa; /* external file attributes 4 bytes */
uint64_t disk_offset;
} unz_file_info;
extern unzFile ZEXPORT unzOpen(const char *path, void *stream);
/* Open a Zip file.
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 or doesn't exist
return unzFile handle if no error
NOTE: The "64" function take a const void *pointer, because the path is just the value passed to the
open64_file_func callback. Under Windows, if UNICODE is defined, using fill_fopen64_filefunc, the path
is a pointer to a wide unicode string (LPCTSTR is LPCWSTR), so const char *does not describe the reality */
extern int ZEXPORT unzClose(unzFile file);
/* Close a ZipFile opened with unzOpen. If there is files inside the .Zip opened with unzOpenCurrentFile,
these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
return UNZ_OK if there is no error */
extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info *pglobal_info);
extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info);
/* Write info about the ZipFile in the *pglobal_info structure.
return UNZ_OK if no error */
extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size);
/* Get the global comment string of the ZipFile, in the comment buffer.
uSizeBuf is the size of the szComment buffer.
return the number of byte copied or an error code <0 */
/***************************************************************************/
/* Reading the content of the current zipfile, you can open it, read data from it, and close it
(you can close it before reading all the file) */
extern int ZEXPORT unzOpenCurrentFile(unzFile file);
/* Open for reading data the current file in the zipfile.
return UNZ_OK if no error */
extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password);
/* Open for reading data the current file in the zipfile.
password is a crypting password
return UNZ_OK if no error */
extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
/* Same as unzOpenCurrentFile, but open for read raw the file (not uncompress)
if raw==1 *method will receive method of compression, *level will receive level of compression
NOTE: you can set level parameter as NULL (if you did not want known level,
but you CANNOT set method parameter as NULL */
extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
/* Same as unzOpenCurrentFile, but takes extra parameter password for encrypted files */
extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len);
/* Read bytes from the current file (opened by unzOpenCurrentFile)
buf contain buffer where data must be copied
len the size of buf.
return the number of byte copied if somes bytes are copied
return 0 if the end of file was reached
return <0 with error code if there is an error (UNZ_ERRNO for IO error, or zLib error for uncompress error) */
extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
/* Get Info about the current file
pfile_info if != NULL, the *pfile_info structure will contain somes info about the current file
filename if != NULL, the file name string will be copied in filename
filename_size is the size of the filename buffer
extrafield if != NULL, the extra field information from the central header will be copied in to
extrafield_size is the size of the extraField buffer
comment if != NULL, the comment string of the file will be copied in to
comment_size is the size of the comment buffer */
extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, uint32_t len);
/* Read extra field from the current file (opened by unzOpenCurrentFile)
This is the local-header version of the extra field (sometimes, there is
more info in the local-header version than in the central-header)
if buf == NULL, it return the size of the local extra field
if buf != NULL, len is the size of the buffer, the extra header is copied in buf.
return number of bytes copied in buf, or (if <0) the error code */
extern int ZEXPORT unzCloseCurrentFile(unzFile file);
/* Close the file in zip opened with unzOpenCurrentFile
return UNZ_CRCERROR if all the file was read but the CRC is not good */
/***************************************************************************/
/* Browse the directory of the zipfile */
typedef int (*unzFileNameComparer)(unzFile file, const char *filename1, const char *filename2);
typedef int (*unzIteratorFunction)(unzFile file);
typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
extern int ZEXPORT unzGoToFirstFile(unzFile file);
/* Set the current file of the zipfile to the first file.
return UNZ_OK if no error */
extern int ZEXPORT unzGoToFirstFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
/* Set the current file of the zipfile to the first file and retrieves the current info on success.
Not as seek intensive as unzGoToFirstFile + unzGetCurrentFileInfo.
return UNZ_OK if no error */
extern int ZEXPORT unzGoToNextFile(unzFile file);
/* Set the current file of the zipfile to the next file.
return UNZ_OK if no error
return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest */
extern int ZEXPORT unzGoToNextFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
/* Set the current file of the zipfile to the next file and retrieves the current
info on success. Does less seeking around than unzGotoNextFile + unzGetCurrentFileInfo.
return UNZ_OK if no error
return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest */
extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func);
/* Try locate the file szFileName in the zipfile. For custom filename comparison pass in comparison function.
return UNZ_OK if the file is found (it becomes the current file)
return UNZ_END_OF_LIST_OF_FILE if the file is not found */
#ifdef __cplusplus
}
#endif
#endif

122
mzstrm.c
View File

@ -23,7 +23,7 @@ int32_t mz_stream_open(void *stream, const char *path, int mode)
{
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->open == NULL)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
return strm->open(strm, path, mode);
}
@ -31,7 +31,7 @@ int32_t mz_stream_is_open(void *stream)
{
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->is_open == NULL)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
return strm->is_open(strm);
}
@ -39,9 +39,9 @@ int32_t mz_stream_read(void *stream, void* buf, uint32_t size)
{
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->read == NULL)
return MZ_STREAM_ERR;
if (strm->is_open != NULL && strm->is_open(strm) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
if (strm->is_open != NULL && strm->is_open(strm) != MZ_OK)
return MZ_STREAM_ERROR;
return strm->read(strm, buf, size);
}
@ -52,9 +52,9 @@ int32_t mz_stream_read_uint8(void *stream, uint8_t *value)
if (mz_stream_read(stream, &c, 1) == 1)
*value = (uint8_t)c;
else if (mz_stream_error(stream))
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_read_uint16(void *stream, uint16_t *value)
@ -62,14 +62,14 @@ int32_t mz_stream_read_uint16(void *stream, uint16_t *value)
uint8_t c = 0;
*value = 0;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value = (uint16_t)c;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value += ((uint16_t)c) << 8;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_read_uint32(void *stream, uint32_t *value)
@ -77,63 +77,63 @@ int32_t mz_stream_read_uint32(void *stream, uint32_t *value)
uint8_t c = 0;
*value = 0;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value = (uint32_t)c;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value += ((uint32_t)c) << 8;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value += ((uint32_t)c) << 16;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value += ((uint32_t)c) << 24;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_read_uint64(void *stream, uint64_t *value)
{
uint8_t c = 0;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value = (uint64_t)c;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value += ((uint64_t)c) << 8;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value += ((uint64_t)c) << 16;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value += ((uint64_t)c) << 24;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value += ((uint64_t)c) << 32;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value += ((uint64_t)c) << 40;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value += ((uint64_t)c) << 48;
if (mz_stream_read_uint8(stream, &c) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_read_uint8(stream, &c) != MZ_OK)
return MZ_STREAM_ERROR;
*value += ((uint64_t)c) << 56;
return MZ_STREAM_OK;
return MZ_OK;
}
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;
return MZ_STREAM_ERROR;
if (size == 0)
return size;
if (strm->is_open != NULL && strm->is_open(strm) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (strm->is_open != NULL && strm->is_open(strm) != MZ_OK)
return MZ_STREAM_ERROR;
return strm->write(strm, buf, size);
}
@ -156,9 +156,9 @@ static int32_t mz_stream_write_value(void *stream, uint64_t value, uint32_t len)
}
if (mz_stream_write(stream, buf, len) != len)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_write_uint8(void *stream, uint8_t value)
@ -194,24 +194,24 @@ int32_t mz_stream_copy(void *target, void *source, int32_t 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;
if (read < 0)
return MZ_STREAM_ERROR;
written = mz_stream_write(target, buf, read);
if (written != read)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
len -= read;
}
return MZ_STREAM_OK;
return MZ_OK;
}
int64_t mz_stream_tell(void *stream)
{
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->tell == NULL)
return MZ_STREAM_ERR;
if (strm->is_open != NULL && strm->is_open(strm) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
if (strm->is_open != NULL && strm->is_open(strm) != MZ_OK)
return MZ_STREAM_ERROR;
return strm->tell(strm);
}
@ -219,9 +219,9 @@ int32_t mz_stream_seek(void *stream, uint64_t offset, int origin)
{
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->seek == NULL)
return MZ_STREAM_ERR;
if (strm->is_open != NULL && strm->is_open(strm) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
if (strm->is_open != NULL && strm->is_open(strm) != MZ_OK)
return MZ_STREAM_ERROR;
return strm->seek(strm, offset, origin);
}
@ -229,7 +229,7 @@ int32_t mz_stream_close(void *stream)
{
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->close == NULL)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
return strm->close(strm);
}
@ -237,7 +237,7 @@ int32_t mz_stream_error(void *stream)
{
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->error == NULL)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
return strm->error(strm);
}
@ -245,14 +245,14 @@ int32_t mz_stream_set_base(void *stream, void *base)
{
mz_stream *strm = (mz_stream *)stream;
strm->base = (mz_stream *)base;
return MZ_STREAM_OK;
return MZ_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 MZ_STREAM_ERROR;
return strm->get_total_in(stream);
}
@ -260,7 +260,7 @@ 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 MZ_STREAM_ERROR;
return strm->get_total_out(stream);
}
@ -339,7 +339,7 @@ int32_t mz_stream_passthru_close(void *stream)
int32_t mz_stream_passthru_error(void *stream)
{
mz_stream_passthru *passthru = (mz_stream_passthru *)stream;
return mz_stream_error(passthru->stream.error);
return mz_stream_error(passthru->stream.base);
}
int64_t mz_stream_passthru_get_total_in(void *stream)
@ -399,7 +399,7 @@ int32_t mz_os_file_exists(const char *path)
mz_stream_os_create(&stream);
if (mz_stream_os_open(stream, path, MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
if (mz_stream_os_open(stream, path, MZ_STREAM_MODE_READ) == MZ_OK)
{
mz_stream_os_close(stream);
opened = 1;
@ -417,7 +417,7 @@ int32_t mz_os_file_is_large(const char *path)
mz_stream_os_create(&stream);
if (mz_stream_os_open(stream, path, MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
if (mz_stream_os_open(stream, path, MZ_STREAM_MODE_READ) == MZ_OK)
{
mz_stream_os_seek(stream, 0, MZ_STREAM_SEEK_END);
size = mz_stream_os_tell(stream);

View File

@ -34,8 +34,17 @@ extern "C" {
#define MZ_STREAM_MODE_EXISTING (4)
#define MZ_STREAM_MODE_CREATE (8)
#define MZ_STREAM_OK (0)
#define MZ_STREAM_ERR (-1)
#ifndef MZ_RETURN
# define MZ_OK (0)
# define MZ_EOF (MZ_OK)
# define MZ_STREAM_ERROR (-1)
# define MZ_END_OF_LIST (-100)
# define MZ_PARAM_ERROR (-102)
# define MZ_FORMAT_ERROR (-103)
# define MZ_INTERNAL_ERROR (-104)
# define MZ_CRC_ERROR (-105)
# define MZ_CRYPT_ERROR (-106)
#endif
/***************************************************************************/

View File

@ -19,10 +19,14 @@
#include "aes/fileenc.h"
#include "aes/prng.h"
/***************************************************************************/
#define MZ_AES_PWVERIFYSIZE (2)
#define MZ_AES_AUTHCODESIZE (10)
#define MZ_AES_MAXSALTLENGTH (16)
/***************************************************************************/
typedef struct mz_stream_aes_s {
mz_stream stream;
fcrypt_ctx crypt_ctx;
@ -35,6 +39,8 @@ typedef struct mz_stream_aes_s {
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;
@ -49,12 +55,12 @@ int32_t mz_stream_aes_open(void *stream, const char *path, int mode)
aes->total_out = 0;
aes->initialized = 0;
if (mz_stream_is_open(aes->stream.base) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_is_open(aes->stream.base) != MZ_OK)
return MZ_STREAM_ERROR;
if (password == NULL)
password = aes->password;
if (password == NULL)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
salt_length = SALT_LENGTH(aes->encryption_mode);
@ -66,50 +72,50 @@ int32_t mz_stream_aes_open(void *stream, const char *path, int mode)
if (fcrypt_init(aes->encryption_mode, (uint8_t *)password,
(uint32_t)strlen(password), salt_value, verify, &aes->crypt_ctx) != 0)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
if (mz_stream_write(aes->stream.base, salt_value, salt_length) != salt_length)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
aes->total_out += salt_length;
if (mz_stream_write(aes->stream.base, verify, MZ_AES_PWVERIFYSIZE) != MZ_AES_PWVERIFYSIZE)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
aes->total_out += MZ_AES_PWVERIFYSIZE;
}
else if (mode & MZ_STREAM_MODE_READ)
{
if (mz_stream_read(aes->stream.base, salt_value, salt_length) != salt_length)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
aes->total_in += salt_length;
if (mz_stream_read(aes->stream.base, verify_expected, MZ_AES_PWVERIFYSIZE) != MZ_AES_PWVERIFYSIZE)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
aes->total_in += MZ_AES_PWVERIFYSIZE;
if (fcrypt_init(aes->encryption_mode, (uint8_t *)password,
(uint32_t)strlen(password), salt_value, verify, &aes->crypt_ctx) != 0)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
if (memcmp(verify_expected, verify, MZ_AES_PWVERIFYSIZE) != 0)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
}
aes->mode = mode;
aes->initialized = 1;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_aes_is_open(void *stream)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
if (aes->initialized == 0)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_aes_read(void *stream, void *buf, uint32_t size)
@ -157,25 +163,25 @@ int32_t mz_stream_aes_close(void *stream)
fcrypt_end(authcode, &aes->crypt_ctx);
if (mz_stream_write(aes->stream.base, authcode, MZ_AES_AUTHCODESIZE) != MZ_AES_AUTHCODESIZE)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
aes->total_out += MZ_AES_AUTHCODESIZE;
}
else if (aes->mode & MZ_STREAM_MODE_READ)
{
if (mz_stream_read(aes->stream.base, authcode, MZ_AES_AUTHCODESIZE) != MZ_AES_AUTHCODESIZE)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
aes->total_in += MZ_AES_AUTHCODESIZE;
if (fcrypt_end(rauthcode, &aes->crypt_ctx) != MZ_AES_AUTHCODESIZE)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
if (memcmp(authcode, rauthcode, MZ_AES_AUTHCODESIZE) != 0)
return MZ_STREAM_ERR;
return MZ_CRC_ERROR;
}
aes->initialized = 0;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_aes_error(void *stream)
@ -208,6 +214,11 @@ int64_t mz_stream_aes_get_total_out(void *stream)
return aes->total_out;
}
int32_t mz_stream_aes_get_footer_size(void *stream)
{
return MZ_AES_AUTHCODESIZE;
}
void *mz_stream_aes_create(void **stream)
{
mz_stream_aes *aes = NULL;

View File

@ -36,6 +36,7 @@ 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);
int32_t mz_stream_aes_get_footer_size(void *stream);
void* mz_stream_aes_create(void **stream);
void mz_stream_aes_delete(void **stream);

View File

@ -19,18 +19,18 @@
#include "mzstrm.h"
#include "mzstrm_buf.h"
#define MZ_BUF_BUFFERSIZE (UINT16_MAX)
//#define MZ_BUF_VERBOSE
/***************************************************************************/
#if defined(_WIN32)
# include <conio.h>
# define PRINTF _cprintf
# define VPRINTF _vcprintf
#define MZ_BUF_BUFFERSIZE (UINT16_MAX)
#if 0
# define mz_stream_buffered_print(o,s,f,...) printf(o,s,f,__VA_ARGS__);
#else
# define PRINTF printf
# define VPRINTF vprintf
# define mz_stream_buffered_print(o,s,f,...)
#endif
/***************************************************************************/
typedef struct mz_stream_buffered_s {
mz_stream stream;
char readbuf[MZ_BUF_BUFFERSIZE];
@ -47,21 +47,7 @@ typedef struct mz_stream_buffered_s {
int32_t error;
} mz_stream_buffered;
#ifdef MZ_BUF_VERBOSE
# define mz_stream_buffered_print(o,s,f,...) mz_stream_buffered_print_internal(o,s,f,__VA_ARGS__);
#else
# define mz_stream_buffered_print(o,s,f,...)
#endif
void mz_stream_buffered_printinternal(void *stream, char *format, ...)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
va_list arglist;
PRINTF("Buf stream %p - ", buffered);
va_start(arglist, format);
VPRINTF(format, arglist);
va_end(arglist);
}
/***************************************************************************/
int32_t mz_stream_buffered_open(void *stream, const char *path, int mode)
{
@ -84,7 +70,7 @@ int32_t mz_stream_buffered_flush(void *stream, uint32_t *written)
{
bytes_written = mz_stream_write(buffered->stream.base, buffered->writebuf + (bytes_to_write - bytes_left_to_write), bytes_left_to_write);
if (bytes_written != bytes_left_to_write)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
buffered->writebuf_misses += 1;
@ -100,7 +86,7 @@ int32_t mz_stream_buffered_flush(void *stream, uint32_t *written)
buffered->writebuf_pos = 0;
*written = total_bytes_written;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_buffered_read(void *stream, void *buf, uint32_t size)
@ -188,8 +174,8 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, uint32_t size)
mz_stream_buffered_print(opaque, stream, "switch from read to write [%lld]\n", buffered->position);
if (mz_stream_seek(buffered->stream.base, buffered->position, MZ_STREAM_SEEK_SET) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_seek(buffered->stream.base, buffered->position, MZ_STREAM_SEEK_SET) != MZ_OK)
return MZ_STREAM_ERROR;
}
while (bytes_left_to_write > 0)
@ -203,8 +189,8 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, uint32_t size)
if (bytes_to_copy == 0)
{
if (mz_stream_buffered_flush(stream, &bytes_flushed) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_buffered_flush(stream, &bytes_flushed) != MZ_OK)
return MZ_STREAM_ERROR;
if (bytes_flushed == 0)
return 0;
@ -265,17 +251,17 @@ int mz_stream_buffered_seekinternal(void *stream, uint64_t offset, int origin)
if ((offset >= buffered->position) && (offset <= buffered->position + buffered->writebuf_len))
{
buffered->writebuf_pos = (uint32_t)(offset - buffered->position);
return MZ_STREAM_OK;
return MZ_OK;
}
}
if ((buffered->readbuf_len > 0) && (offset < buffered->position) && (offset >= buffered->position - buffered->readbuf_len))
{
buffered->readbuf_pos = (uint32_t)(offset - (buffered->position - buffered->readbuf_len));
return MZ_STREAM_OK;
return MZ_OK;
}
if (mz_stream_buffered_flush(stream, &bytes_flushed) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_buffered_flush(stream, &bytes_flushed) != MZ_OK)
return MZ_STREAM_ERROR;
buffered->position = offset;
break;
@ -287,7 +273,7 @@ int mz_stream_buffered_seekinternal(void *stream, uint64_t offset, int origin)
if (offset <= (buffered->readbuf_len - buffered->readbuf_pos))
{
buffered->readbuf_pos += (uint32_t)offset;
return MZ_STREAM_OK;
return MZ_OK;
}
offset -= (buffered->readbuf_len - buffered->readbuf_pos);
buffered->position += offset;
@ -297,13 +283,13 @@ int mz_stream_buffered_seekinternal(void *stream, uint64_t offset, int origin)
if (offset <= (buffered->writebuf_len - buffered->writebuf_pos))
{
buffered->writebuf_pos += (uint32_t)offset;
return MZ_STREAM_OK;
return MZ_OK;
}
//offset -= (buffered->writebuf_len - buffered->writebuf_pos);
}
if (mz_stream_buffered_flush(stream, &bytes_flushed) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_buffered_flush(stream, &bytes_flushed) != MZ_OK)
return MZ_STREAM_ERROR;
break;
@ -312,7 +298,7 @@ int mz_stream_buffered_seekinternal(void *stream, uint64_t offset, int origin)
if (buffered->writebuf_len > 0)
{
buffered->writebuf_pos = buffered->writebuf_len;
return MZ_STREAM_OK;
return MZ_OK;
}
break;
}
@ -321,14 +307,14 @@ int mz_stream_buffered_seekinternal(void *stream, uint64_t offset, int origin)
buffered->readbuf_pos = 0;
buffered->writebuf_len = 0;
buffered->writebuf_pos = 0;
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
}
int32_t mz_stream_buffered_seek(void *stream, uint64_t offset, int origin)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
if (mz_stream_buffered_seekinternal(stream, offset, origin) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_buffered_seekinternal(stream, offset, origin) != MZ_OK)
return MZ_STREAM_ERROR;
return mz_stream_seek(buffered->stream.base, offset, origin);
}

View File

@ -19,6 +19,8 @@
#include "mzstrm.h"
#include "mzstrm_bzip.h"
/***************************************************************************/
typedef struct mz_stream_bzip_s {
mz_stream stream;
bz_stream bzstream;
@ -26,15 +28,14 @@ typedef struct mz_stream_bzip_s {
int32_t buffer_len;
int64_t total_in;
int64_t total_out;
int64_t max_total_in;
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)
{
@ -68,19 +69,19 @@ int32_t mz_stream_bzip_open(void *stream, const char *path, int mode)
}
if (bzip->error != BZ_OK)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
bzip->initialized = 1;
bzip->mode = mode;
return MZ_STREAM_OK;
return MZ_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;
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_bzip_read(void *stream, void *buf, uint32_t size)
@ -90,6 +91,7 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, uint32_t size)
uint64_t total_out_after = 0;
uint32_t out_bytes = 0;
uint32_t total_out = 0;
int32_t bytes_to_read = 0;
int32_t read = 0;
int16_t err = BZ_OK;
@ -100,7 +102,14 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, uint32_t size)
{
if (bzip->bzstream.avail_in == 0)
{
read = mz_stream_read(bzip->stream.base, bzip->buffer, UINT16_MAX);
bytes_to_read = UINT16_MAX;
if (bzip->max_total_in > 0)
{
if ((bzip->max_total_in - bzip->total_in) < UINT16_MAX)
bytes_to_read = (int32_t)(bzip->max_total_in - bzip->total_in);
}
read = mz_stream_read(bzip->stream.base, bzip->buffer, bytes_to_read);
if (mz_stream_error(bzip->stream.base))
{
bzip->error = BZ_IO_ERROR;
@ -145,8 +154,8 @@ 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;
return MZ_STREAM_ERROR;
return MZ_OK;
}
uint32_t mz_stream_bzip_compress(void *stream, int flush)
@ -170,7 +179,7 @@ uint32_t mz_stream_bzip_compress(void *stream, int flush)
if (err != BZ_OK && err != BZ_STREAM_END)
{
bzip->error = err;
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
}
return out_bytes;
@ -190,7 +199,7 @@ int32_t mz_stream_bzip_write(void *stream, const void *buf, uint32_t size)
{
if (bzip->bzstream.avail_out == 0)
{
if (mz_stream_bzip_flush(bzip) == MZ_STREAM_ERR)
if (mz_stream_bzip_flush(bzip) != MZ_OK)
{
bzip->error = BZ_DATA_ERROR;
return 0;
@ -217,13 +226,13 @@ int32_t mz_stream_bzip_write(void *stream, const void *buf, uint32_t size)
int64_t mz_stream_bzip_tell(void *stream)
{
mz_stream_bzip *mem = (mz_stream_bzip *)stream;
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
}
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;
return MZ_STREAM_ERROR;
}
int32_t mz_stream_bzip_close(void *stream)
@ -250,8 +259,8 @@ int32_t mz_stream_bzip_close(void *stream)
bzip->initialized = 0;
if (bzip->error != BZ_OK)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_bzip_error(void *stream)
@ -278,6 +287,12 @@ int64_t mz_stream_bzip_get_total_out(void *stream)
return bzip->total_out;
}
void mz_stream_bzip_set_max_total_in(void *stream, int64_t max_total_in)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
bzip->max_total_in = max_total_in;
}
void *mz_stream_bzip_create(void **stream)
{
mz_stream_bzip *bzip = NULL;
@ -316,3 +331,7 @@ void mz_stream_bzip_delete(void **stream)
if (bzip != NULL)
free(bzip);
}
extern void bz_internal_error(int errcode)
{
}

View File

@ -30,6 +30,7 @@ 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_set_max_total_in(void *stream, int64_t max_total_in);
void* mz_stream_bzip_create(void **stream);
void mz_stream_bzip_delete(void **stream);

View File

@ -34,8 +34,12 @@
#include "mzstrm.h"
#include "mzstrm_crypt.h"
/***************************************************************************/
#define RAND_HEAD_LEN 12
/***************************************************************************/
typedef struct mz_stream_crypt_s {
mz_stream stream;
uint32_t keys[3]; // keys defining the pseudo-random sequence
@ -49,12 +53,16 @@ typedef struct mz_stream_crypt_s {
int64_t total_out;
} mz_stream_crypt;
/***************************************************************************/
#define zdecode(keys,crc_32_tab,c) \
(mz_stream_crypt_update_keys(keys,crc_32_tab, c ^= mz_stream_crypt_decrypt_byte(keys)))
#define zencode(keys,crc_32_tab,c,t) \
(t = mz_stream_crypt_decrypt_byte(keys), mz_stream_crypt_update_keys(keys,crc_32_tab,c), t^(c))
/***************************************************************************/
uint8_t mz_stream_crypt_decrypt_byte(uint32_t *keys)
{
unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
@ -92,6 +100,8 @@ void mz_stream_crypt_init_keys(const char *password, uint32_t *keys, const z_crc
}
}
/***************************************************************************/
int32_t mz_stream_crypt_open(void *stream, const char *path, int mode)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
@ -106,17 +116,17 @@ int32_t mz_stream_crypt_open(void *stream, const char *path, int mode)
crypt->total_out = 0;
crypt->initialized = 0;
if (mz_stream_is_open(crypt->stream.base) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_is_open(crypt->stream.base) != MZ_OK)
return MZ_STREAM_ERROR;
if (password == NULL)
password = crypt->password;
if (password == NULL)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
crypt->crc_32_tab = get_crc_table();
if (crypt->crc_32_tab == NULL)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
mz_stream_crypt_init_keys(password, crypt->keys, crypt->crc_32_tab);
@ -133,14 +143,14 @@ int32_t mz_stream_crypt_open(void *stream, const char *path, int mode)
header[i++] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, crypt->verify2, t);
if (mz_stream_write(crypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
crypt->total_out += RAND_HEAD_LEN;
}
else if (mode & MZ_STREAM_MODE_READ)
{
if (mz_stream_read(crypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
header[i] = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i]);
@ -152,15 +162,15 @@ int32_t mz_stream_crypt_open(void *stream, const char *path, int mode)
}
crypt->initialized = 1;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_crypt_is_open(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
if (crypt->initialized == 0)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_crypt_read(void *stream, void *buf, uint32_t size)
@ -208,7 +218,7 @@ int32_t mz_stream_crypt_close(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
crypt->initialized = 0;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_crypt_error(void *stream)

View File

@ -23,6 +23,8 @@
#include "mzstrm.h"
#include "mzstrm_mem.h"
/***************************************************************************/
typedef struct mz_stream_mem_s {
mz_stream stream;
char *buffer; // Memory buffer pointer
@ -33,6 +35,8 @@ typedef struct mz_stream_mem_s {
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)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
@ -55,15 +59,15 @@ int32_t mz_stream_mem_open(void *stream, const char *path, int mode)
mem->position = 0;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_mem_is_open(void *stream)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
if (mem->buffer == NULL)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_mem_read(void *stream, void *buf, uint32_t size)
@ -144,26 +148,26 @@ int32_t mz_stream_mem_seek(void *stream, uint64_t offset, int origin)
new_pos = offset;
break;
default:
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
}
if (new_pos > mem->size)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
mem->position = (uint32_t)new_pos;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_mem_close(void *stream)
{
// We never return errors
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_mem_error(void *stream)
{
// We never return errors
return MZ_STREAM_OK;
return MZ_OK;
}
void mz_stream_mem_set_buffer(void *stream, void *buf, uint32_t size)
@ -173,6 +177,20 @@ void mz_stream_mem_set_buffer(void *stream, void *buf, uint32_t size)
mem->size = size;
}
int8_t mz_stream_mem_get_buffer(void *stream, void **buf)
{
return mz_stream_mem_get_buffer_at(stream, 0, buf);
}
int8_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, void **buf)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
if (buf == NULL || position < 0 || mem->size < position || mem->buffer == NULL)
return MZ_STREAM_ERROR;
*buf = mem->buffer + position;
return MZ_OK;
}
void mz_stream_mem_set_grow(void *stream, int8_t grow)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
@ -204,7 +222,7 @@ 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;
mem->grow_size = 16384;
}
if (stream != NULL)
*stream = mem;

View File

@ -30,6 +30,8 @@ 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);
int8_t mz_stream_mem_get_buffer(void *stream, void **buf);
int8_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, void **buf);
void mz_stream_mem_set_grow(void *stream, int8_t grow);
void mz_stream_mem_set_grow_size(void *stream, uint32_t grow_size);

View File

@ -21,6 +21,8 @@
#include "mzstrm.h"
#include "mzstrm_win32.h"
/***************************************************************************/
#ifndef INVALID_HANDLE_VALUE
# define INVALID_HANDLE_VALUE (0xFFFFFFFF)
#endif
@ -35,6 +37,8 @@
# endif
#endif
/***************************************************************************/
typedef struct mz_stream_win32_s
{
mz_stream stream;
@ -44,6 +48,8 @@ typedef struct mz_stream_win32_s
int path_size;
} mz_stream_win32;
/***************************************************************************/
int32_t mz_stream_win32_open(void *stream, const char *path, int mode)
{
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
@ -56,7 +62,7 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int mode)
HANDLE handle = NULL;
if (path == NULL)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
if ((mode & MZ_STREAM_MODE_READWRITEFILTER) == MZ_STREAM_MODE_READ)
{
@ -76,7 +82,7 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int mode)
}
else
{
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
}
path_wide_size = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
@ -86,17 +92,17 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int mode)
MultiByteToWideChar(CP_UTF8, 0, path, -1, path_wide, path_wide_size);
#ifdef IOWIN32_USING_WINRT_API
win32->handle = CreateFile2W(filename_wide, desired_access, share_mode, creation_disposition, NULL);
win32->handle = CreateFile2W(path_wide, desired_access, share_mode, creation_disposition, NULL);
#else
win32->handle = CreateFileW(path_wide, desired_access, share_mode, NULL, creation_disposition, flags_attribs, NULL);
#endif
free(path_wide);
if (mz_stream_win32_is_open(stream) == MZ_STREAM_ERR)
if (mz_stream_win32_is_open(stream) != MZ_OK)
{
win32->error = GetLastError();
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
}
win32->path_size = strlen(path) + 1;
@ -104,15 +110,15 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int mode)
strncpy(win32->path, path, win32->path_size);
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_win32_is_open(void *stream)
{
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
if (win32->handle == NULL || win32->handle == INVALID_HANDLE_VALUE)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_win32_read(void *stream, void* buf, uint32_t size)
@ -121,8 +127,8 @@ int32_t mz_stream_win32_read(void *stream, void* buf, uint32_t size)
uint32_t read = 0;
HANDLE handle = NULL;
if (mz_stream_win32_is_open(stream) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_win32_is_open(stream) != MZ_OK)
return MZ_STREAM_ERROR;
if (!ReadFile(win32->handle, buf, size, &read, NULL))
{
@ -141,8 +147,8 @@ int32_t mz_stream_win32_write(void *stream, const void *buf, uint32_t size)
uint32_t error = 0;
HANDLE handle = NULL;
if (mz_stream_win32_is_open(stream) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_win32_is_open(stream) != MZ_OK)
return MZ_STREAM_ERROR;
if (!WriteFile(win32->handle, buf, size, &written, NULL))
{
@ -163,7 +169,7 @@ static int32_t mz_stream_win32_seekinternal(HANDLE handle, LARGE_INTEGER large_p
uint32_t pos = SetFilePointer(handle, large_pos.LowPart, &high_part, move_method);
if ((pos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
if (new_pos != NULL)
{
@ -171,7 +177,7 @@ static int32_t mz_stream_win32_seekinternal(HANDLE handle, LARGE_INTEGER large_p
new_pos->HighPart = high_part;
}
return MZ_STREAM_OK;
return MZ_OK;
#endif
}
@ -183,12 +189,12 @@ int64_t mz_stream_win32_tell(void *stream)
HANDLE handle = NULL;
LARGE_INTEGER large_pos;
if (mz_stream_win32_is_open(stream) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_win32_is_open(stream) != MZ_OK)
return MZ_STREAM_ERROR;
large_pos.QuadPart = 0;
if (mz_stream_win32_seekinternal(win32->handle, large_pos, &large_pos, FILE_CURRENT) == MZ_STREAM_ERR)
if (mz_stream_win32_seekinternal(win32->handle, large_pos, &large_pos, FILE_CURRENT) != MZ_OK)
{
error = GetLastError();
win32->error = error;
@ -207,8 +213,8 @@ int32_t mz_stream_win32_seek(void *stream, uint64_t offset, int origin)
LARGE_INTEGER large_pos;
if (mz_stream_win32_is_open(stream) == MZ_STREAM_ERR)
return MZ_STREAM_ERR;
if (mz_stream_win32_is_open(stream) == MZ_STREAM_ERROR)
return MZ_STREAM_ERROR;
switch (origin)
{
@ -222,19 +228,19 @@ int32_t mz_stream_win32_seek(void *stream, uint64_t offset, int origin)
move_method = FILE_BEGIN;
break;
default:
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
}
large_pos.QuadPart = offset;
if (mz_stream_win32_seekinternal(win32->handle, large_pos, NULL, move_method) == MZ_STREAM_ERR)
if (mz_stream_win32_seekinternal(win32->handle, large_pos, NULL, move_method) != MZ_OK)
{
error = GetLastError();
win32->error = error;
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
}
return MZ_STREAM_OK;
return MZ_OK;
}
int mz_stream_win32_close(void *stream)
@ -246,7 +252,7 @@ int mz_stream_win32_close(void *stream)
if (win32->handle != NULL)
CloseHandle(win32->handle);
win32->handle = NULL;
return MZ_STREAM_OK;
return MZ_OK;
}
int mz_stream_win32_error(void *stream)

View File

@ -19,6 +19,8 @@
#include "mzstrm.h"
#include "mzstrm_zlib.h"
/***************************************************************************/
#ifndef DEF_MEM_LEVEL
# if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
@ -27,6 +29,8 @@
# endif
#endif
/***************************************************************************/
typedef struct mz_stream_zlib_s {
mz_stream stream;
z_stream zstream;
@ -34,6 +38,7 @@ typedef struct mz_stream_zlib_s {
int32_t buffer_len;
int64_t total_in;
int64_t total_out;
int64_t max_total_in;
int8_t initialized;
int16_t level;
int16_t window_bits;
@ -43,6 +48,8 @@ typedef struct mz_stream_zlib_s {
int16_t error;
} mz_stream_zlib;
/***************************************************************************/
int32_t mz_stream_zlib_open(void *stream, const char *path, int mode)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
@ -79,19 +86,19 @@ int32_t mz_stream_zlib_open(void *stream, const char *path, int mode)
}
if (zlib->error != Z_OK)
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
zlib->initialized = 1;
zlib->mode = mode;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_zlib_is_open(void *stream)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
if (zlib->initialized != 1)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_zlib_read(void *stream, void *buf, uint32_t size)
@ -101,9 +108,11 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, uint32_t size)
uint32_t total_out_after = 0;
uint32_t out_bytes = 0;
uint32_t total_out = 0;
int32_t bytes_to_read = 0;
int32_t read = 0;
int16_t err = Z_OK;
zlib->zstream.next_out = (uint8_t*)buf;
zlib->zstream.avail_out = (uint16_t)size;
@ -111,8 +120,16 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, uint32_t size)
{
if (zlib->zstream.avail_in == 0)
{
read = mz_stream_read(zlib->stream.base, zlib->buffer, UINT16_MAX);
if (mz_stream_error(zlib->stream.base))
bytes_to_read = UINT16_MAX;
if (zlib->max_total_in > 0)
{
if ((zlib->max_total_in - zlib->total_in) < UINT16_MAX)
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)
{
zlib->error = Z_STREAM_ERROR;
break;
@ -141,7 +158,9 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, uint32_t size)
total_out += out_bytes;
if (err == Z_STREAM_END)
{
break;
}
if (err != Z_OK)
{
zlib->error = err;
@ -159,8 +178,8 @@ int32_t mz_stream_zlib_flush(void *stream)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
if (mz_stream_write(zlib->stream.base, zlib->buffer, zlib->buffer_len) != zlib->buffer_len)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
return MZ_STREAM_ERROR;
return MZ_OK;
}
uint32_t mz_stream_zlib_deflate(void *stream, int flush)
@ -180,7 +199,7 @@ uint32_t mz_stream_zlib_deflate(void *stream, int flush)
if (err != Z_OK && err != Z_STREAM_END)
{
zlib->error = err;
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
}
return out_bytes;
@ -200,7 +219,7 @@ int32_t mz_stream_zlib_write(void *stream, const void *buf, uint32_t size)
{
if (zlib->zstream.avail_out == 0)
{
if (mz_stream_zlib_flush(zlib) == MZ_STREAM_ERR)
if (mz_stream_zlib_flush(zlib) != MZ_OK)
{
zlib->error = Z_STREAM_ERROR;
return 0;
@ -227,13 +246,13 @@ int32_t mz_stream_zlib_write(void *stream, const void *buf, uint32_t size)
int64_t mz_stream_zlib_tell(void *stream)
{
mz_stream_zlib *mem = (mz_stream_zlib *)stream;
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
}
int32_t mz_stream_zlib_seek(void *stream, uint64_t offset, int origin)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
return MZ_STREAM_ERR;
return MZ_STREAM_ERROR;
}
int32_t mz_stream_zlib_close(void *stream)
@ -260,8 +279,8 @@ int32_t mz_stream_zlib_close(void *stream)
zlib->initialized = 0;
if (zlib->error != Z_OK)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_zlib_error(void *stream)
@ -306,6 +325,12 @@ int64_t mz_stream_zlib_get_total_out(void *stream)
return zlib->total_out;
}
void mz_stream_zlib_set_max_total_in(void *stream, int64_t max_total_in)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
zlib->max_total_in = max_total_in;
}
void *mz_stream_zlib_create(void **stream)
{
mz_stream_zlib *zlib = NULL;
@ -361,15 +386,15 @@ int32_t mz_stream_crc32_open(void *stream, const char *path, int mode)
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
crc32->initialized = 1;
crc32->value = 0;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_crc32_is_open(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
if (crc32->initialized != 1)
return MZ_STREAM_ERR;
return MZ_STREAM_OK;
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_crc32_read(void *stream, void *buf, uint32_t size)
@ -408,7 +433,7 @@ int32_t mz_stream_crc32_close(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
crc32->initialized = 0;
return MZ_STREAM_OK;
return MZ_OK;
}
int32_t mz_stream_crc32_error(void *stream)

View File

@ -33,6 +33,7 @@ void mz_stream_zlib_set_mem_level(void *stream, int16_t mem_level);
void mz_stream_zlib_set_strategy(void *stream, int16_t strategy);
int64_t mz_stream_zlib_get_total_in(void *stream);
int64_t mz_stream_zlib_get_total_out(void *stream);
void mz_stream_zlib_set_max_total_in(void *stream, int64_t max_total_in);
void* mz_stream_zlib_create(void **stream);
void mz_stream_zlib_delete(void **stream);

22
test.c
View File

@ -23,7 +23,7 @@ void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, "LICENSE", MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
if (mz_stream_os_open(in_stream, "LICENSE", MZ_STREAM_MODE_READ) == MZ_OK)
{
read = mz_stream_os_read(in_stream, buf, UINT16_MAX);
mz_stream_os_close(in_stream);
@ -33,13 +33,13 @@ void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password
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)
if (mz_stream_os_open(out_stream, filename, MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_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)
if (mz_stream_open(crypt_out_stream, password, MZ_STREAM_MODE_WRITE) == MZ_OK)
{
written = mz_stream_write(crypt_out_stream, buf, read);
mz_stream_close(crypt_out_stream);
@ -55,13 +55,13 @@ void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password
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)
if (mz_stream_os_open(in_stream, filename, MZ_STREAM_MODE_READ) == MZ_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)
if (mz_stream_open(crypt_out_stream, password, MZ_STREAM_MODE_READ) == MZ_OK)
{
read = mz_stream_read(crypt_out_stream, buf, read);
mz_stream_close(crypt_out_stream);
@ -78,7 +78,7 @@ void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password
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)
if (mz_stream_os_open(out_stream, filename, MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_OK)
{
mz_stream_os_write(out_stream, buf, read);
mz_stream_os_close(out_stream);
@ -105,7 +105,7 @@ void test_compress(char *method, mz_stream_create_cb create_compress)
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, "LICENSE", MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
if (mz_stream_os_open(in_stream, "LICENSE", MZ_STREAM_MODE_READ) == MZ_OK)
{
mz_stream_crc32_create(&crc_in_stream);
mz_stream_set_base(crc_in_stream, in_stream);
@ -120,7 +120,7 @@ void test_compress(char *method, mz_stream_create_cb create_compress)
mz_stream_os_delete(&in_stream);
if (read == MZ_STREAM_ERR)
if (read < 0)
{
printf("Failed to read LICENSE\n");
return;
@ -131,7 +131,7 @@ void test_compress(char *method, mz_stream_create_cb create_compress)
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)
if (mz_stream_os_open(out_stream, filename, MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_OK)
{
create_compress(&deflate_stream);
mz_stream_set_base(deflate_stream, out_stream);
@ -153,7 +153,7 @@ void test_compress(char *method, mz_stream_create_cb create_compress)
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)
if (mz_stream_os_open(in_stream, filename, MZ_STREAM_MODE_READ) == MZ_OK)
{
create_compress(&inflate_stream);
mz_stream_set_base(inflate_stream, in_stream);
@ -176,7 +176,7 @@ void test_compress(char *method, mz_stream_create_cb create_compress)
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)
if (mz_stream_os_open(out_stream, filename, MZ_STREAM_MODE_CREATE | MZ_STREAM_MODE_WRITE) == MZ_OK)
{
mz_stream_crc32_create(&crc_in_stream);
mz_stream_crc32_open(crc_in_stream, NULL, MZ_STREAM_MODE_WRITE);

2168
unzip.c

File diff suppressed because it is too large Load Diff

316
unzip.h
View File

@ -16,8 +16,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _UNZ_H
#define _UNZ_H
#ifndef _MZ_UNZIP_H
#define _MZ_UNZIP_H
#ifdef __cplusplus
extern "C" {
@ -31,267 +31,117 @@ extern "C" {
#include "mzstrm.h"
#endif
#ifdef HAVE_BZIP2
#include "bzlib.h"
#endif
#define Z_BZIP2ED 12
#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
/* like the STRICT of WIN32, we define a pointer that cannot be converted
from (void*) without cast */
typedef struct TagunzFile__ { int unused; } unz_file__;
typedef unz_file__ *unzFile;
#else
typedef voidp unzFile;
/***************************************************************************/
#ifndef MZ_RETURN
# define MZ_OK (0)
# define MZ_EOF (MZ_OK)
# define MZ_STREAM_ERROR (-1)
# define MZ_END_OF_LIST (-100)
# define MZ_PARAM_ERROR (-102)
# define MZ_FORMAT_ERROR (-103)
# define MZ_INTERNAL_ERROR (-104)
# define MZ_CRC_ERROR (-105)
# define MZ_CRYPT_ERROR (-106)
#endif
#define UNZ_OK (0)
#define UNZ_END_OF_LIST_OF_FILE (-100)
#define UNZ_ERRNO (Z_ERRNO)
#define UNZ_EOF (0)
#define UNZ_PARAMERROR (-102)
#define UNZ_BADZIPFILE (-103)
#define UNZ_INTERNALERROR (-104)
#define UNZ_CRCERROR (-105)
#define UNZ_BADPASSWORD (-106)
/***************************************************************************/
/* unz_global_info structure contain global data about the ZIPfile
These data comes from the end of central dir */
typedef struct unz_global_info64_s
// Global data about the zip file that come from the end of central dir
typedef struct mz_unzip_global_s
{
uint64_t number_entry; /* total number of entries in the central dir on this disk */
uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP*/
uint16_t size_comment; /* size of the global comment of the zipfile */
} unz_global_info64;
uint64_t number_entry; // total number of entries in the central dir on this disk
uint32_t number_disk_with_CD; // number the the disk with central dir, used for spanning ZIP
uint16_t comment_size; // size of the global comment of the zipfile
} mz_unzip_global;
typedef struct unz_global_info_s
// Info about a file in the zip file at the central directory
typedef struct mz_unzip_file_s
{
uint32_t number_entry; /* total number of entries in the central dir on this disk */
uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP*/
uint16_t size_comment; /* size of the global comment of the zipfile */
} unz_global_info;
uint16_t version; // version made by 2 bytes
uint16_t version_needed; // version needed to extract 2 bytes
uint16_t flag; // general purpose bit flag 2 bytes
uint16_t compression_method; // compression method 2 bytes
uint32_t dos_date; // last mod file date in Dos fmt 4 bytes
uint32_t crc; // crc-32 4 bytes
uint64_t compressed_size; // compressed size 8 bytes
uint64_t uncompressed_size; // uncompressed size 8 bytes
uint16_t filename_size; // filename length 2 bytes
uint16_t extrafield_size; // extra field length 2 bytes
uint16_t comment_size; // file comment length 2 bytes
/* unz_file_info contain information about a file in the zipfile */
typedef struct unz_file_info64_s
{
uint16_t version; /* version made by 2 bytes */
uint16_t version_needed; /* version needed to extract 2 bytes */
uint16_t flag; /* general purpose bit flag 2 bytes */
uint16_t compression_method; /* compression method 2 bytes */
uint32_t dos_date; /* last mod file date in Dos fmt 4 bytes */
uint32_t crc; /* crc-32 4 bytes */
uint64_t compressed_size; /* compressed size 8 bytes */
uint64_t uncompressed_size; /* uncompressed size 8 bytes */
uint16_t size_filename; /* filename length 2 bytes */
uint16_t size_file_extra; /* extra field length 2 bytes */
uint16_t size_file_comment; /* file comment length 2 bytes */
uint32_t disk_num_start; // disk number start 4 bytes
uint16_t internal_fa; // internal file attributes 2 bytes
uint32_t external_fa; // external file attributes 4 bytes
uint32_t disk_num_start; /* disk number start 4 bytes */
uint16_t internal_fa; /* internal file attributes 2 bytes */
uint32_t external_fa; /* external file attributes 4 bytes */
uint64_t disk_offset; // relative offset of local header 8 bytes
uint64_t disk_offset;
uint16_t size_file_extra_internal;
} unz_file_info64;
typedef struct unz_file_info_s
{
uint16_t version; /* version made by 2 bytes */
uint16_t version_needed; /* version needed to extract 2 bytes */
uint16_t flag; /* general purpose bit flag 2 bytes */
uint16_t compression_method; /* compression method 2 bytes */
uint32_t dos_date; /* last mod file date in Dos fmt 4 bytes */
uint32_t crc; /* crc-32 4 bytes */
uint32_t compressed_size; /* compressed size 4 bytes */
uint32_t uncompressed_size; /* uncompressed size 4 bytes */
uint16_t size_filename; /* filename length 2 bytes */
uint16_t size_file_extra; /* extra field length 2 bytes */
uint16_t size_file_comment; /* file comment length 2 bytes */
uint16_t disk_num_start; /* disk number start 2 bytes */
uint16_t internal_fa; /* internal file attributes 2 bytes */
uint32_t external_fa; /* external file attributes 4 bytes */
uint64_t disk_offset;
} unz_file_info;
char *filename; // filename string
uint8_t *extrafield; // extrafield data
char *comment; // comment string
} mz_unzip_file;
/***************************************************************************/
/* Opening and close a zip file */
// Opening and close a zip file
extern unzFile ZEXPORT unzOpen(const char *path, voidpf stream);
/* Open a Zip file.
// Open a zip file
extern void* ZEXPORT mz_unzip_open(const char *path, void *stream);
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 or doesn't exist
return unzFile handle if no error
// Close a zip file
extern int ZEXPORT mz_unzip_close(void *handle);
NOTE: The "64" function take a const void *pointer, because the path is just the value passed to the
open64_file_func callback. Under Windows, if UNICODE is defined, using fill_fopen64_filefunc, the path
is a pointer to a wide unicode string (LPCTSTR is LPCWSTR), so const char *does not describe the reality */
// Get global info about the zip file
extern int ZEXPORT mz_unzip_get_global_info(void *handle, mz_unzip_global *global_info);
extern int ZEXPORT unzClose(unzFile file);
/* Close a ZipFile opened with unzOpen. If there is files inside the .Zip opened with unzOpenCurrentFile,
these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
return UNZ_OK if there is no error */
extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info *pglobal_info);
extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info);
/* Write info about the ZipFile in the *pglobal_info structure.
return UNZ_OK if no error */
extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size);
/* Get the global comment string of the ZipFile, in the comment buffer.
uSizeBuf is the size of the szComment buffer.
return the number of byte copied or an error code <0 */
// Get the global comment string of the zip file, in the comment buffer
extern int ZEXPORT mz_unzip_get_global_comment(void *handle, char *comment, uint16_t comment_size);
/***************************************************************************/
/* Reading the content of the current zipfile, you can open it, read data from it, and close it
(you can close it before reading all the file) */
// Reading the content of the current zip file, you can open it, read it, and close it
extern int ZEXPORT unzOpenCurrentFile(unzFile file);
/* Open for reading data the current file in the zipfile.
extern int ZEXPORT mz_unzip_entry_open(void *handle, int raw, const char *password);
// Open for reading data the current file in the zip file
return UNZ_OK if no error */
extern int ZEXPORT mz_unzip_entry_read(void *handle, void *buf, uint32_t len);
// Read bytes from the current file
extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password);
/* Open for reading data the current file in the zipfile.
password is a crypting password
extern int ZEXPORT mz_unzip_entry_get_info(void *handle, mz_unzip_file **file_info);
// Get info about the current file
return UNZ_OK if no error */
extern int ZEXPORT mz_unzip_entry_get_extrafield_local(void *handle, void *buf, uint32_t len);
// Read extra field from the current file
//
// This is the local-header version of the extra field (sometimes, there is
// more info in the local-header version than in the central-header)
//
// if buf == NULL, it return the size of the local extra field
// if buf != NULL, len is the size of the buffer, the extra header is copied in buf.
//
// return number of bytes copied in buf, or (if <0) the error code
extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
/* Same as unzOpenCurrentFile, but open for read raw the file (not uncompress)
if raw==1 *method will receive method of compression, *level will receive level of compression
NOTE: you can set level parameter as NULL (if you did not want known level,
but you CANNOT set method parameter as NULL */
extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
/* Same as unzOpenCurrentFile, but takes extra parameter password for encrypted files */
extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len);
/* Read bytes from the current file (opened by unzOpenCurrentFile)
buf contain buffer where data must be copied
len the size of buf.
return the number of byte copied if somes bytes are copied
return 0 if the end of file was reached
return <0 with error code if there is an error (UNZ_ERRNO for IO error, or zLib error for uncompress error) */
extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
/* Get Info about the current file
pfile_info if != NULL, the *pfile_info structure will contain somes info about the current file
filename if != NULL, the file name string will be copied in filename
filename_size is the size of the filename buffer
extrafield if != NULL, the extra field information from the central header will be copied in to
extrafield_size is the size of the extraField buffer
comment if != NULL, the comment string of the file will be copied in to
comment_size is the size of the comment buffer */
extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, uint32_t len);
/* Read extra field from the current file (opened by unzOpenCurrentFile)
This is the local-header version of the extra field (sometimes, there is
more info in the local-header version than in the central-header)
if buf == NULL, it return the size of the local extra field
if buf != NULL, len is the size of the buffer, the extra header is copied in buf.
return number of bytes copied in buf, or (if <0) the error code */
extern int ZEXPORT unzCloseCurrentFile(unzFile file);
/* Close the file in zip opened with unzOpenCurrentFile
return UNZ_CRCERROR if all the file was read but the CRC is not good */
extern int ZEXPORT mz_unzip_entry_close(void *handle);
// Close the file in zip
/***************************************************************************/
/* Browse the directory of the zipfile */
// Navigate the directory of the zip file
typedef int (*unzFileNameComparer)(unzFile file, const char *filename1, const char *filename2);
typedef int (*unzIteratorFunction)(unzFile file);
typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
typedef int (*mz_filename_compare_cb)(void *handle, const char *filename1, const char *filename2);
extern int ZEXPORT unzGoToFirstFile(unzFile file);
/* Set the current file of the zipfile to the first file.
extern int ZEXPORT mz_unzip_locate_entry(void *file, const char *filename, mz_filename_compare_cb filename_compare_cb);
// Locate the file with the specified name in the zip file
//
// if filename_compare_cb == NULL, it uses strcmp
//
// return MZ_OK if the file is found (it becomes the current file)
// return MZ_END_OF_LIST if the file is not found
return UNZ_OK if no error */
extern int ZEXPORT mz_unzip_goto_first_entry(void *handle);
// Go to the first entry in the zip file
extern int ZEXPORT unzGoToFirstFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
/* Set the current file of the zipfile to the first file and retrieves the current info on success.
Not as seek intensive as unzGoToFirstFile + unzGetCurrentFileInfo.
return UNZ_OK if no error */
extern int ZEXPORT unzGoToNextFile(unzFile file);
/* Set the current file of the zipfile to the next file.
return UNZ_OK if no error
return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest */
extern int ZEXPORT unzGoToNextFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
/* Set the current file of the zipfile to the next file and retrieves the current
info on success. Does less seeking around than unzGotoNextFile + unzGetCurrentFileInfo.
return UNZ_OK if no error
return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest */
extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func);
/* Try locate the file szFileName in the zipfile. For custom filename comparison pass in comparison function.
return UNZ_OK if the file is found (it becomes the current file)
return UNZ_END_OF_LIST_OF_FILE if the file is not found */
/***************************************************************************/
/* Raw access to zip file */
typedef struct unz_file_pos_s
{
uint32_t pos_in_zip_directory; /* offset in zip file directory */
uint32_t num_of_file; /* # of file */
} unz_file_pos;
extern int ZEXPORT unzGetFilePos(unzFile file, unz_file_pos *file_pos);
extern int ZEXPORT unzGoToFilePos(unzFile file, unz_file_pos *file_pos);
typedef struct unz64_file_pos_s
{
uint64_t pos_in_zip_directory; /* offset in zip file directory */
uint64_t num_of_file; /* # of file */
} unz64_file_pos;
extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos *file_pos);
extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos);
extern int32_t ZEXPORT unzGetOffset(unzFile file);
extern int64_t ZEXPORT unzGetOffset64(unzFile file);
/* Get the current file offset */
extern int ZEXPORT unzSetOffset(unzFile file, uint32_t pos);
extern int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos);
/* Set the current file offset */
extern int32_t ZEXPORT unzTell(unzFile file);
extern int64_t ZEXPORT unzTell64(unzFile file);
/* return current position in uncompressed data */
extern int ZEXPORT unzSeek(unzFile file, uint32_t offset, int origin);
extern int ZEXPORT unzSeek64(unzFile file, uint64_t offset, int origin);
/* Seek within the uncompressed data if compression method is storage */
extern int ZEXPORT unzEndOfFile(unzFile file);
/* return 1 if the end of file was reached, 0 elsewhere */
extern int ZEXPORT mz_unzip_goto_next_entry(void *handle);
// Go to the next entry in the zip file or MZ_END_OF_LIST if reaching the end
/***************************************************************************/

View File

@ -129,7 +129,6 @@
<ClCompile Include="..\..\..\trees.c" />
<ClCompile Include="..\..\..\uncompr.c" />
<ClCompile Include="..\..\..\zutil.c" />
<ClCompile Include="..\crypt.c" />
<ClCompile Include="..\lib\bzip2\bzlib.c" />
<ClCompile Include="..\lib\bzip2\crctable.c" />
<ClCompile Include="..\mzstrm.c" />
@ -171,7 +170,6 @@
<ClInclude Include="..\..\..\zconf.h" />
<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" />

View File

@ -20,9 +20,6 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\crypt.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\unzip.c">
<Filter>Source Files</Filter>
</ClCompile>
@ -148,9 +145,6 @@
<ClInclude Include="..\..\inffixed.h">
<Filter>Zlib</Filter>
</ClInclude>
<ClInclude Include="..\crypt.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\unzip.h">
<Filter>Header Files</Filter>
</ClInclude>

343
zip.c
View File

@ -22,17 +22,21 @@
#include <errno.h>
#include "zlib.h"
#include "zip.h"
#include "mzstrm.h"
#include "mzstrm_mem.h"
#include "mzstrm_zlib.h"
#ifdef HAVE_AES
# include "mzstrm_aes.h"
#endif
#ifdef HAVE_BZIP2
# include "mzstrm_bzip.h"
#endif
#ifndef NOCRYPT
# include "mzstrm_crypt.h"
#endif
#include "mzstrm_mem.h"
#include "mzstrm_zlib.h"
#include "zip.h"
/***************************************************************************/
@ -94,7 +98,7 @@ static uint64_t mz_zip_search_cd(void *stream)
uint64_t read_pos = 0;
uint32_t i = 0;
if (mz_stream_seek(stream, 0, MZ_STREAM_SEEK_END) == MZ_STREAM_ERR)
if (mz_stream_seek(stream, 0, MZ_STREAM_SEEK_END) != MZ_OK)
return 0;
file_size = mz_stream_tell(stream);
@ -113,7 +117,7 @@ static uint64_t mz_zip_search_cd(void *stream)
read_size = ((BUFREADCOMMENT + 4) < (file_size - read_pos)) ?
(BUFREADCOMMENT + 4) : (uint32_t)(file_size - read_pos);
if (mz_stream_seek(stream, read_pos, MZ_STREAM_SEEK_SET) == MZ_STREAM_ERR)
if (mz_stream_seek(stream, read_pos, MZ_STREAM_SEEK_SET) != MZ_OK)
break;
if (mz_stream_read(stream, buf, read_size) != read_size)
break;
@ -144,28 +148,28 @@ static uint64_t mz_zip_search_zip64_cd(void *stream, const uint64_t endcentralof
uint32_t value32 = 0;
// Zip64 end of central directory locator
if (mz_stream_seek(stream, endcentraloffset - SIZECENTRALHEADERLOCATOR, MZ_STREAM_SEEK_SET) == MZ_STREAM_ERR)
if (mz_stream_seek(stream, endcentraloffset - SIZECENTRALHEADERLOCATOR, MZ_STREAM_SEEK_SET) != MZ_OK)
return 0;
// Read locator signature
if (mz_stream_read_uint32(stream, &value32) != ZIP_OK)
if (mz_stream_read_uint32(stream, &value32) != MZ_OK)
return 0;
if (value32 != ZIP64ENDLOCHEADERMAGIC)
return 0;
// Number of the disk with the start of the zip64 end of central directory
if (mz_stream_read_uint32(stream, &value32) != ZIP_OK)
if (mz_stream_read_uint32(stream, &value32) != MZ_OK)
return 0;
// Relative offset of the zip64 end of central directory record
if (mz_stream_read_uint64(stream, &offset) != ZIP_OK)
if (mz_stream_read_uint64(stream, &offset) != MZ_OK)
return 0;
// Total number of disks
if (mz_stream_read_uint32(stream, &value32) != ZIP_OK)
if (mz_stream_read_uint32(stream, &value32) != MZ_OK)
return 0;
// Goto end of central directory record
if (mz_stream_seek(stream, offset, MZ_STREAM_SEEK_SET) != 0)
return 0;
// The signature
if (mz_stream_read_uint32(stream, &value32) != ZIP_OK)
if (mz_stream_read_uint32(stream, &value32) != MZ_OK)
return 0;
if (value32 != ZIP64ENDHEADERMAGIC)
return 0;
@ -173,7 +177,7 @@ static uint64_t mz_zip_search_zip64_cd(void *stream, const uint64_t endcentralof
return offset;
}
extern void* ZEXPORT mz_zip_open(const char *path, int append, uint64_t disk_size, const char **globalcomment, void *stream)
extern void* ZEXPORT mz_zip_open(const char *path, int append, uint64_t disk_size, void *stream)
{
mz_zip *zip = NULL;
#ifndef NO_ADDFILEINEXISTINGZIP
@ -188,7 +192,7 @@ extern void* ZEXPORT mz_zip_open(const char *path, int append, uint64_t disk_siz
uint64_t value64 = 0;
uint16_t comment_size = 0;
#endif
int16_t err = ZIP_OK;
int16_t err = MZ_OK;
int16_t mode = 0;
if (append == MZ_APPEND_STATUS_CREATE)
@ -196,7 +200,7 @@ extern void* ZEXPORT mz_zip_open(const char *path, int append, uint64_t disk_siz
else
mode = (MZ_STREAM_MODE_READ | MZ_STREAM_MODE_WRITE | MZ_STREAM_MODE_EXISTING);
if (mz_stream_open(stream, path, mode) == MZ_STREAM_ERR)
if (mz_stream_open(stream, path, mode) != MZ_OK)
return NULL;
if (append == MZ_APPEND_STATUS_CREATEAFTER)
@ -204,7 +208,7 @@ extern void* ZEXPORT mz_zip_open(const char *path, int append, uint64_t disk_siz
// Don't support spanning ZIP with APPEND_STATUS_CREATEAFTER
if (disk_size > 0)
return NULL;
if (mz_stream_seek(stream, 0, SEEK_END) == MZ_STREAM_ERR)
if (mz_stream_seek(stream, 0, MZ_STREAM_SEEK_END) != MZ_OK)
return NULL;
}
@ -234,56 +238,56 @@ extern void* ZEXPORT mz_zip_open(const char *path, int append, uint64_t disk_siz
// Disable to allow appending to empty ZIP archive (must be standard zip, not zip64)
// if (central_pos == 0)
// err = ZIP_ERRNO;
// err = MZ_FORMAT_ERROR;
if (err == ZIP_OK)
if (err == MZ_OK)
{
// Read end of central directory info
if (mz_stream_seek(zip->stream, central_pos, MZ_STREAM_SEEK_SET) != 0)
err = ZIP_ERRNO;
if (mz_stream_seek(zip->stream, central_pos, MZ_STREAM_SEEK_SET) != MZ_OK)
err = MZ_STREAM_ERROR;
// The signature, already checked
if (mz_stream_read_uint32(zip->stream, &value32) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint32(zip->stream, &value32) != MZ_OK)
err = MZ_STREAM_ERROR;
// Number of this disk
if (mz_stream_read_uint16(zip->stream, &value16) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint16(zip->stream, &value16) != MZ_OK)
err = MZ_STREAM_ERROR;
zip->number_disk = value16;
// Number of the disk with the start of the central directory
if (mz_stream_read_uint16(zip->stream, &value16) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint16(zip->stream, &value16) != MZ_OK)
err = MZ_STREAM_ERROR;
zip->number_disk_with_CD = value16;
// Total number of entries in the central dir on this disk
number_entry = 0;
if (mz_stream_read_uint16(zip->stream, &value16) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint16(zip->stream, &value16) != MZ_OK)
err = MZ_STREAM_ERROR;
else
number_entry = value16;
// Total number of entries in the central dir
number_entry_CD = 0;
if (mz_stream_read_uint16(zip->stream, &value16) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint16(zip->stream, &value16) != MZ_OK)
err = MZ_STREAM_ERROR;
else
number_entry_CD = value16;
if (number_entry_CD != number_entry)
err = ZIP_BADZIPFILE;
err = MZ_FORMAT_ERROR;
// Size of the central directory
size_central_dir = 0;
if (mz_stream_read_uint32(zip->stream, &value32) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint32(zip->stream, &value32) != MZ_OK)
err = MZ_STREAM_ERROR;
else
size_central_dir = value32;
// Offset of start of central directory with respect to the starting disk number
offset_central_dir = 0;
if (mz_stream_read_uint32(zip->stream, &value32) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint32(zip->stream, &value32) != MZ_OK)
err = MZ_STREAM_ERROR;
else
offset_central_dir = value32;
// Zipfile global comment length
if (mz_stream_read_uint16(zip->stream, &comment_size) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint16(zip->stream, &comment_size) != MZ_OK)
err = MZ_STREAM_ERROR;
if ((err == ZIP_OK) && ((number_entry_CD == UINT16_MAX) || (offset_central_dir == UINT32_MAX)))
if ((err == MZ_OK) && ((number_entry_CD == UINT16_MAX) || (offset_central_dir == UINT32_MAX)))
{
// Format should be Zip64, as the central directory or file size is too large
central_pos = mz_zip_search_zip64_cd(zip->stream, central_pos);
@ -291,83 +295,85 @@ extern void* ZEXPORT mz_zip_open(const char *path, int append, uint64_t disk_siz
if (central_pos)
{
if (mz_stream_seek(zip->stream, central_pos, MZ_STREAM_SEEK_SET) != 0)
err = ZIP_ERRNO;
err = MZ_STREAM_ERROR;
// The signature, already checked
if (mz_stream_read_uint32(zip->stream, &value32) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint32(zip->stream, &value32) != MZ_OK)
err = MZ_STREAM_ERROR;
// Size of zip64 end of central directory record
if (mz_stream_read_uint64(zip->stream, &value64) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint64(zip->stream, &value64) != MZ_OK)
err = MZ_STREAM_ERROR;
// Version made by
if (mz_stream_read_uint16(zip->stream, &value16) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint16(zip->stream, &value16) != MZ_OK)
err = MZ_STREAM_ERROR;
// Version needed to extract
if (mz_stream_read_uint16(zip->stream, &value16) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint16(zip->stream, &value16) != MZ_OK)
err = MZ_STREAM_ERROR;
// Number of this disk
if (mz_stream_read_uint32(zip->stream, &zip->number_disk) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint32(zip->stream, &zip->number_disk) != MZ_OK)
err = MZ_STREAM_ERROR;
// Number of the disk with the start of the central directory
if (mz_stream_read_uint32(zip->stream, &zip->number_disk_with_CD) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint32(zip->stream, &zip->number_disk_with_CD) != MZ_OK)
err = MZ_STREAM_ERROR;
// Total number of entries in the central directory on this disk
if (mz_stream_read_uint64(zip->stream, &number_entry) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint64(zip->stream, &number_entry) != MZ_OK)
err = MZ_STREAM_ERROR;
// Total number of entries in the central directory
if (mz_stream_read_uint64(zip->stream, &number_entry_CD) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint64(zip->stream, &number_entry_CD) != MZ_OK)
err = MZ_STREAM_ERROR;
if (number_entry_CD != number_entry)
err = ZIP_BADZIPFILE;
err = MZ_FORMAT_ERROR;
// Size of the central directory
if (mz_stream_read_uint64(zip->stream, &size_central_dir) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint64(zip->stream, &size_central_dir) != MZ_OK)
err = MZ_STREAM_ERROR;
// Offset of start of central directory with respect to the starting disk number
if (mz_stream_read_uint64(zip->stream, &offset_central_dir) != ZIP_OK)
err = ZIP_ERRNO;
if (mz_stream_read_uint64(zip->stream, &offset_central_dir) != MZ_OK)
err = MZ_STREAM_ERROR;
}
else
err = ZIP_BADZIPFILE;
{
err = MZ_FORMAT_ERROR;
}
}
}
if ((err == ZIP_OK) && (central_pos < offset_central_dir + size_central_dir))
err = ZIP_BADZIPFILE;
if (err == MZ_OK)
{
if (central_pos < offset_central_dir + size_central_dir)
err = MZ_FORMAT_ERROR;
}
if ((err == ZIP_OK) && (comment_size > 0))
if ((err == MZ_OK) && (comment_size > 0))
{
zip->comment = (char *)malloc(comment_size + 1);
if (zip->comment)
{
if (mz_stream_read(zip->stream, zip->comment, comment_size) != comment_size)
err = ZIP_ERRNO;
err = MZ_STREAM_ERROR;
zip->comment[comment_size] = 0;
}
}
if (err == ZIP_OK)
if (err == MZ_OK)
{
byte_before_the_zipfile = central_pos - (offset_central_dir + size_central_dir);
zip->add_position_when_writting_offset = byte_before_the_zipfile;
// Store central directory in memory
if (mz_stream_seek(zip->stream, offset_central_dir + byte_before_the_zipfile, MZ_STREAM_SEEK_SET) == MZ_STREAM_ERR)
err = ZIP_ERRNO;
if (mz_stream_copy(zip->cd_stream, zip->stream, (uint32_t)size_central_dir) == MZ_STREAM_ERR)
err = ZIP_ERRNO;
if (mz_stream_seek(zip->stream, offset_central_dir + byte_before_the_zipfile, MZ_STREAM_SEEK_SET) != MZ_OK)
err = MZ_STREAM_ERROR;
if (mz_stream_copy(zip->cd_stream, zip->stream, (uint32_t)size_central_dir) != MZ_OK)
err = MZ_STREAM_ERROR;
zip->number_entry = number_entry_CD;
if (mz_stream_seek(zip->stream, offset_central_dir + byte_before_the_zipfile, MZ_STREAM_SEEK_SET) == MZ_STREAM_ERR)
err = ZIP_ERRNO;
if (mz_stream_seek(zip->stream, offset_central_dir + byte_before_the_zipfile, MZ_STREAM_SEEK_SET) != MZ_OK)
err = MZ_STREAM_ERROR;
}
}
if (globalcomment)
*globalcomment = zip->comment;
#endif
if (err != ZIP_OK)
if (err != MZ_OK)
{
mz_stream_close(zip->cd_stream);
mz_stream_delete(&zip->cd_stream);
@ -384,6 +390,17 @@ extern void* ZEXPORT mz_zip_open(const char *path, int append, uint64_t disk_siz
return zip;
}
extern int ZEXPORT mz_zip_get_global_comment(void *handle, const char **global_comment)
{
mz_zip *zip = NULL;
if (handle == NULL || global_comment == NULL)
return MZ_PARAM_ERROR;
zip = (mz_zip*)handle;
*global_comment = zip->comment;
return MZ_OK;
}
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)
{
@ -391,29 +408,31 @@ extern int ZEXPORT mz_zip_entry_open(void *handle, const mz_zip_file *file_info,
uint16_t filename_size = 0;
uint16_t version_needed = 0;
uint16_t i = 0;
int16_t err = ZIP_OK;
int16_t err = MZ_OK;
#ifdef NOCRYPT
if (password != NULL)
return ZIP_PARAMERROR;
return MZ_PARAM_ERROR;
#endif
if (handle == NULL)
return ZIP_PARAMERROR;
return MZ_PARAM_ERROR;
if ((compress_info->method != 0) &&
if ((compress_info->method != 0) && (compress_info->method != Z_DEFLATED))
{
#ifdef HAVE_BZIP2
(compress_info->method != Z_BZIP2ED) &&
if (compress_info->method != Z_BZIP2ED)
return MZ_PARAM_ERROR;
#else
return MZ_PARAM_ERROR;
#endif
(compress_info->method != Z_DEFLATED))
return ZIP_PARAMERROR;
}
zip = (mz_zip*)handle;
if (zip->entry_opened == 1)
{
err = mz_zip_entry_close(handle);
if (err != ZIP_OK)
if (err != MZ_OK)
return err;
}
@ -446,7 +465,7 @@ extern int ZEXPORT mz_zip_entry_open(void *handle, const mz_zip_file *file_info,
zip->number_disk = zip->number_disk;
// Write the local header
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, (uint32_t)LOCALHEADERMAGIC);
version_needed = 20;
@ -457,11 +476,11 @@ extern int ZEXPORT mz_zip_entry_open(void *handle, const mz_zip_file *file_info,
version_needed = 51;
#endif
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint16(zip->stream, version_needed);
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint16(zip->stream, zip->file_info.flag);
if (err == ZIP_OK)
if (err == MZ_OK)
{
#ifdef HAVE_AES
if ((zip->file_info.flag & 1) && (zip->crypt_info.aes))
@ -470,19 +489,19 @@ extern int ZEXPORT mz_zip_entry_open(void *handle, const mz_zip_file *file_info,
#endif
err = mz_stream_write_uint16(zip->stream, zip->compress_info.method);
}
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, zip->file_info.dos_date);
// CRC & compressed size & uncompressed size is in data descriptor
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, 0); // crc 32
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, 0); // compressed size
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, 0); // uncompressed size
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint16(zip->stream, filename_size);
if (err == ZIP_OK)
if (err == MZ_OK)
{
uint16_t extrafield_size = zip->file_info.extrafield_local_size;
#ifdef HAVE_AES
@ -491,34 +510,34 @@ extern int ZEXPORT mz_zip_entry_open(void *handle, const mz_zip_file *file_info,
#endif
err = mz_stream_write_uint16(zip->stream, extrafield_size);
}
if (err == ZIP_OK)
if (err == MZ_OK)
{
if (mz_stream_write(zip->stream, zip->file_info.filename, filename_size) != filename_size)
err = ZIP_ERRNO;
err = MZ_STREAM_ERROR;
}
if (err == ZIP_OK)
if (err == MZ_OK)
{
if (mz_stream_write(zip->stream, zip->file_info.extrafield_local,
zip->file_info.extrafield_local_size) != zip->file_info.extrafield_local_size)
err = ZIP_ERRNO;
zip->file_info.extrafield_local_size) != zip->file_info.extrafield_local_size)
err = MZ_STREAM_ERROR;
}
#ifdef HAVE_AES
// Write the AES extended info
if ((err == ZIP_OK) && (zip->file_info.flag & 1) && (zip->crypt_info.aes))
if ((err == MZ_OK) && (zip->file_info.flag & 1) && (zip->crypt_info.aes))
{
err = mz_stream_write_uint16(zip->stream, 0x9901);
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint16(zip->stream, 7);
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint16(zip->stream, MZ_AES_VERSION);
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint8(zip->stream, 'A');
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint8(zip->stream, 'E');
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint8(zip->stream, MZ_AES_ENCRYPTIONMODE);
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint16(zip->stream, zip->compress_info.method);
}
#endif
@ -539,8 +558,8 @@ extern int ZEXPORT mz_zip_entry_open(void *handle, const mz_zip_file *file_info,
mz_stream_set_base(zip->crypt_stream, zip->stream);
if (mz_stream_open(zip->crypt_stream, NULL, MZ_STREAM_MODE_WRITE) == MZ_STREAM_ERR)
err = ZIP_ERRNO;
if (mz_stream_open(zip->crypt_stream, NULL, MZ_STREAM_MODE_WRITE) != MZ_OK)
err = MZ_INTERNAL_ERROR;
}
else
#endif
@ -560,15 +579,15 @@ extern int ZEXPORT mz_zip_entry_open(void *handle, const mz_zip_file *file_info,
mz_stream_set_base(zip->crypt_stream, zip->stream);
if (mz_stream_open(zip->crypt_stream, NULL, MZ_STREAM_MODE_WRITE) == MZ_STREAM_ERR)
err = ZIP_ERRNO;
if (mz_stream_open(zip->crypt_stream, NULL, MZ_STREAM_MODE_WRITE) != MZ_OK)
err = MZ_INTERNAL_ERROR;
}
}
#endif
if (err == ZIP_OK)
if (err == MZ_OK)
{
if (zip->compress_info.raw)
if (zip->compress_info.method == 0) // raw
{
mz_stream_passthru_create(&zip->compress_stream);
mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
@ -583,8 +602,8 @@ extern int ZEXPORT mz_zip_entry_open(void *handle, const mz_zip_file *file_info,
mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
if (mz_stream_open(zip->compress_stream, NULL, MZ_STREAM_MODE_WRITE) == MZ_STREAM_ERR)
err = ZIP_ERRNO;
if (mz_stream_open(zip->compress_stream, NULL, MZ_STREAM_MODE_WRITE) != MZ_OK)
err = MZ_INTERNAL_ERROR;
}
else if (zip->compress_info.method == Z_BZIP2ED)
{
@ -594,8 +613,8 @@ extern int ZEXPORT mz_zip_entry_open(void *handle, const mz_zip_file *file_info,
mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
if (mz_stream_open(zip->compress_stream, NULL, MZ_STREAM_MODE_WRITE) == MZ_STREAM_ERR)
err = ZIP_ERRNO;
if (mz_stream_open(zip->compress_stream, NULL, MZ_STREAM_MODE_WRITE) != MZ_OK)
err = MZ_INTERNAL_ERROR;
#endif
}
}
@ -605,8 +624,8 @@ extern int ZEXPORT mz_zip_entry_open(void *handle, const mz_zip_file *file_info,
mz_stream_crc32_create(&zip->crc32_stream);
mz_stream_set_base(zip->crc32_stream, zip->compress_stream);
if (mz_stream_open(zip->crc32_stream, NULL, MZ_STREAM_MODE_WRITE) == MZ_STREAM_ERR)
err = ZIP_ERRNO;
if (mz_stream_open(zip->crc32_stream, NULL, MZ_STREAM_MODE_WRITE) != MZ_OK)
err = MZ_INTERNAL_ERROR;
}
if (err == Z_OK)
@ -620,16 +639,16 @@ extern int ZEXPORT mz_zip_entry_write(void *handle, const void *buf, uint32_t le
mz_zip *zip = NULL;
if (handle == NULL)
return ZIP_PARAMERROR;
return MZ_PARAM_ERROR;
zip = (mz_zip*)handle;
if (zip->entry_opened == 0)
return ZIP_PARAMERROR;
if (mz_stream_write(zip->crc32_stream, buf, len) == MZ_STREAM_ERR)
return ZIP_ERRNO;
return MZ_PARAM_ERROR;
if (mz_stream_write(zip->crc32_stream, buf, len) == MZ_STREAM_ERROR)
return MZ_STREAM_ERROR;
return ZIP_OK;
return MZ_OK;
}
extern int ZEXPORT mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32)
@ -642,19 +661,19 @@ extern int ZEXPORT mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_si
uint16_t comment_size = 0;
uint16_t version_needed = 0;
uint32_t i = 0;
int16_t err = ZIP_OK;
int16_t err = MZ_OK;
if (handle == NULL)
return ZIP_PARAMERROR;
return MZ_PARAM_ERROR;
zip = (mz_zip*)handle;
if (zip->entry_opened == 0)
return ZIP_PARAMERROR;
return MZ_PARAM_ERROR;
mz_stream_close(zip->compress_stream);
if ((!zip->compress_info.raw) || (uncompressed_size == 0))
if ((zip->compress_info.method != 0) || (uncompressed_size == 0))
{
crc32 = mz_stream_crc32_get_value(zip->crc32_stream);
uncompressed_size = mz_stream_get_total_out(zip->crc32_stream);
@ -665,10 +684,9 @@ extern int ZEXPORT mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_si
{
mz_stream_set_base(zip->crypt_stream, zip->stream);
if (mz_stream_close(zip->crypt_stream) == MZ_STREAM_ERR)
err = ZIP_ERRNO;
err = mz_stream_close(zip->crypt_stream);
if ((!zip->compress_info.raw) || (uncompressed_size == 0))
if ((zip->compress_info.method != 0) || (uncompressed_size == 0))
compressed_size = mz_stream_get_total_out(zip->crypt_stream);
mz_stream_delete(&zip->crypt_stream);
@ -678,18 +696,18 @@ extern int ZEXPORT mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_si
mz_stream_crc32_delete(&zip->crc32_stream);
// Write data descriptor
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, (uint32_t)DATADESCRIPTORMAGIC);
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, crc32);
if (err == ZIP_OK)
if (err == MZ_OK)
{
if (zip->file_info.zip64)
err = mz_stream_write_uint64(zip->stream, compressed_size);
else
err = mz_stream_write_uint32(zip->stream, (uint32_t)compressed_size);
}
if (err == ZIP_OK)
if (err == MZ_OK)
{
if (zip->file_info.zip64)
err = mz_stream_write_uint64(zip->stream, uncompressed_size);
@ -817,10 +835,10 @@ extern int ZEXPORT mz_zip_close(void *handle, const char *global_comment, uint16
uint64_t pos = 0;
uint64_t cd_pos = 0;
uint32_t write = 0;
int16_t err = ZIP_OK;
int16_t err = MZ_OK;
if (handle == NULL)
return ZIP_PARAMERROR;
return MZ_PARAM_ERROR;
zip = (mz_zip*)handle;
if (zip->entry_opened == 1)
@ -837,8 +855,7 @@ extern int ZEXPORT mz_zip_close(void *handle, const char *global_comment, uint16
size_centraldir = (uint32_t)mz_stream_tell(zip->cd_stream);
mz_stream_seek(zip->cd_stream, 0, MZ_STREAM_SEEK_SET);
if (mz_stream_copy(zip->stream, zip->cd_stream, size_centraldir) == MZ_STREAM_ERR)
err = ZIP_ERRNO;
err = mz_stream_copy(zip->stream, zip->cd_stream, size_centraldir);
mz_stream_close(zip->cd_stream);
mz_stream_delete(&zip->cd_stream);
@ -853,66 +870,66 @@ extern int ZEXPORT mz_zip_close(void *handle, const char *global_comment, uint16
err = mz_stream_write_uint32(zip->stream, (uint32_t)ZIP64ENDHEADERMAGIC);
// Size of this 'zip64 end of central directory'
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
// Version made by
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint16(zip->stream, version_madeby);
// Version needed
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
// Number of this disk
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, zip->number_disk_with_CD);
// Number of the disk with the start of the central directory
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, zip->number_disk_with_CD);
// Total number of entries in the central dir on this disk
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint64(zip->stream, zip->number_entry);
// Total number of entries in the central dir
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint64(zip->stream, zip->number_entry);
// Size of the central directory
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint64(zip->stream, (uint64_t)size_centraldir);
if (err == ZIP_OK)
if (err == MZ_OK)
{
// Offset of start of central directory with respect to the starting disk number
cd_pos = centraldir_pos_inzip - zip->add_position_when_writting_offset;
err = mz_stream_write_uint64(zip->stream, cd_pos);
}
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, (uint32_t)ZIP64ENDLOCHEADERMAGIC);
// Number of the disk with the start of the central directory
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, zip->number_disk_with_CD);
// Relative offset to the end of zip64 central directory
if (err == ZIP_OK)
if (err == MZ_OK)
{
cd_pos = zip64_eocd_pos_inzip - zip->add_position_when_writting_offset;
err = mz_stream_write_uint64(zip->stream, cd_pos);
}
// Number of the disk with the start of the central directory
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, zip->number_disk_with_CD + 1);
}
// Write the central directory header
// Signature
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, (uint32_t)ENDHEADERMAGIC);
// Number of this disk
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_disk_with_CD);
// Number of the disk with the start of the central directory
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_disk_with_CD);
// Total number of entries in the central dir on this disk
if (err == ZIP_OK)
if (err == MZ_OK)
{
if (zip->number_entry >= UINT16_MAX)
err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
@ -920,7 +937,7 @@ extern int ZEXPORT mz_zip_close(void *handle, const char *global_comment, uint16
err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
}
// Total number of entries in the central dir
if (err == ZIP_OK)
if (err == MZ_OK)
{
if (zip->number_entry >= UINT16_MAX)
err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
@ -928,10 +945,10 @@ extern int ZEXPORT mz_zip_close(void *handle, const char *global_comment, uint16
err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
}
// Size of the central directory
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, size_centraldir);
// Offset of start of central directory with respect to the starting disk number
if (err == ZIP_OK)
if (err == MZ_OK)
{
cd_pos = centraldir_pos_inzip - zip->add_position_when_writting_offset;
if (pos >= UINT32_MAX)
@ -943,16 +960,16 @@ extern int ZEXPORT mz_zip_close(void *handle, const char *global_comment, uint16
// Write global comment
if (global_comment != NULL)
comment_size = (uint16_t)strlen(global_comment);
if (err == ZIP_OK)
if (err == MZ_OK)
err = mz_stream_write_uint16(zip->stream, comment_size);
if (err == ZIP_OK)
if (err == MZ_OK)
{
if (mz_stream_write(zip->stream, global_comment, comment_size) != comment_size)
err = ZIP_ERRNO;
err = MZ_STREAM_ERROR;
}
if (mz_stream_close(zip->stream) == MZ_STREAM_ERR)
err = ZIP_ERRNO;
if (mz_stream_close(zip->stream) == MZ_STREAM_ERROR)
err = MZ_STREAM_ERROR;
#ifndef NO_ADDFILEINEXISTINGZIP
if (zip->comment)

46
zip.h
View File

@ -33,13 +33,18 @@ extern "C" {
/***************************************************************************/
#define ZIP_OK (0)
#define ZIP_EOF (0)
#define ZIP_ERRNO (Z_ERRNO)
#define ZIP_PARAMERROR (-102)
#define ZIP_BADZIPFILE (-103)
#define ZIP_INTERNALERROR (-104)
#ifndef MZ_RETURN
# define MZ_OK (0)
# define MZ_EOF (MZ_OK)
# define MZ_STREAM_ERROR (-1)
# define MZ_END_OF_LIST (-100)
# define MZ_PARAM_ERROR (-102)
# define MZ_FORMAT_ERROR (-103)
# define MZ_INTERNAL_ERROR (-104)
# define MZ_CRC_ERROR (-105)
# define MZ_CRYPT_ERROR (-106)
#endif
/***************************************************************************/
typedef struct mz_zip_file_s
@ -60,9 +65,8 @@ typedef struct mz_zip_file_s
typedef struct mz_zip_compress_s
{
uint16_t method; // compression method ie Z_DEFLATE
uint16_t method; // compression method ie Z_DEFLATE, 0 for raw
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
@ -70,11 +74,11 @@ typedef struct mz_zip_compress_s
typedef struct mz_zip_crypt_s
{
const char *password; // encryption password
uint32_t crc_for_crypting; // crc to use for traditional encryption
#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;
/***************************************************************************/
@ -85,25 +89,31 @@ typedef struct mz_zip_crypt_s
/***************************************************************************/
extern void* ZEXPORT mz_zip_open(const char *path, int append, uint64_t disk_size, void *stream);
// Create a zipfile
extern void* ZEXPORT mz_zip_open(const char *path, int append, uint64_t disk_size,
const char **globalcomment, void *stream);
//
// NOTE: There is no delete function into a zipfile. If you want delete file in a zip file,
// you must open a zip file, and create another. You can use RAW reading and writing to copy
// the file you did not want delete.
extern int ZEXPORT mz_zip_get_global_comment(void *handle, const char **global_comment);
// Gets the global comments if opening an existing zip
// 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);
// Open a file in the ZIP for writing
// Write data in the zipfile
extern int ZEXPORT mz_zip_entry_write(void *handle, const void *buf, uint32_t len);
// Write data in the zipfile
// Close the current file in the zipfile
extern int ZEXPORT mz_zip_entry_close(void *handle);
// Close the current file in the zipfile
// 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);
// Close the current file in the zipfile where raw is compressed data
// Close the zipfile
extern int ZEXPORT mz_zip_close(void *handle, const char *global_comment, uint16_t version_madeby);
// Close the zipfile
/***************************************************************************/