mirror of
https://github.com/zlib-ng/minizip-ng
synced 2025-03-28 21:13:18 +00:00
Added traditional encryption stream.
Broke out random encryption code into platform specific stream code. Cleanup other streams.
This commit is contained in:
parent
a4f2ea02ed
commit
74f8c7a6b1
36
ioapi.c
36
ioapi.c
@ -15,10 +15,13 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#if defined unix || defined __APPLE__
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#include "ioapi.h"
|
||||
@ -45,6 +48,10 @@
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifndef ZCR_SEED2
|
||||
# define ZCR_SEED2 3141592654UL // use PI as default pattern
|
||||
#endif
|
||||
|
||||
int32_t mzstream_open(voidpf stream, const char *filename, int mode)
|
||||
{
|
||||
mzstream *strm = (mzstream *)stream;
|
||||
@ -243,4 +250,33 @@ void mzstream_posix_free(voidpf stream)
|
||||
mzstream_posix *posix = (mzstream_posix *)stream;
|
||||
if (posix != NULL)
|
||||
free(posix);
|
||||
}
|
||||
|
||||
int32_t mzstream_posix_rand(uint8_t *buf, uint16_t size)
|
||||
{
|
||||
static unsigned calls = 0;
|
||||
voidpf rand_stream = NULL;
|
||||
int32_t len = 0;
|
||||
|
||||
|
||||
rand_stream = mzstream_posix_alloc();
|
||||
|
||||
if (mzstream_posix_open(rand_stream, "/dev/urandom", MZSTREAM_MODE_READ) == MZSTREAM_OK)
|
||||
{
|
||||
len = mzstream_posix_read(rand_stream, buf, size);
|
||||
mzstream_posix_close(rand_stream);
|
||||
}
|
||||
|
||||
mzstream_posix_free(rand_stream);
|
||||
|
||||
if (len < (int)size)
|
||||
{
|
||||
// Ensure different random header each time
|
||||
if (++calls == 1)
|
||||
srand((unsigned)(time(NULL) ^ ZCR_SEED2));
|
||||
|
||||
while (len < (int)size)
|
||||
buf[len++] = (rand() >> 7) & 0xff;
|
||||
}
|
||||
return len;
|
||||
}
|
14
ioapi.h
14
ioapi.h
@ -13,8 +13,8 @@
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _ZLIBIOAPI64_H
|
||||
#define _ZLIBIOAPI64_H
|
||||
#ifndef _MZSTREAM_H
|
||||
#define _MZSTREAM_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -71,7 +71,7 @@ typedef struct mzstream_s
|
||||
mzstream_error_cb error;
|
||||
mzstream_alloc_cb alloc;
|
||||
mzstream_free_cb free;
|
||||
struct mzstream *base;
|
||||
struct mzstream_s *base;
|
||||
} mzstream;
|
||||
|
||||
int32_t mzstream_open(voidpf stream, const char *filename, int mode);
|
||||
@ -97,6 +97,8 @@ int32_t ZCALLBACK mzstream_posix_error(voidpf stream);
|
||||
voidpf mzstream_posix_alloc(void);
|
||||
void mzstream_posix_free(voidpf stream);
|
||||
|
||||
int32_t mzstream_posix_rand(uint8_t *buf, uint16_t size);
|
||||
|
||||
#ifndef _WIN32
|
||||
#define mzstream_os_open mzstream_posix_open
|
||||
#define mzstream_os_is_open mzstream_posix_is_open
|
||||
@ -109,7 +111,11 @@ void mzstream_posix_free(voidpf stream);
|
||||
|
||||
#define mzstream_os_alloc mzstream_posix_alloc
|
||||
#define mzstream_os_free mzstream_posix_free
|
||||
|
||||
#define mzstream_os_rand mzstream_posix_rand
|
||||
#else
|
||||
#include "iowin32.h"
|
||||
|
||||
#define mzstream_os_open mzstream_win32_open
|
||||
#define mzstream_os_is_open mzstream_win32_is_open
|
||||
#define mzstream_os_read mzstream_win32_read
|
||||
@ -121,6 +127,8 @@ void mzstream_posix_free(voidpf stream);
|
||||
|
||||
#define mzstream_os_alloc mzstream_win32_alloc
|
||||
#define mzstream_os_free mzstream_win32_free
|
||||
|
||||
#define mzstream_os_rand mzstream_win32_rand
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -10,8 +10,8 @@
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _IOAPI_BUFFERED_H
|
||||
#define _IOAPI_BUFFERED_H
|
||||
#ifndef _MZSTREAM_BUFFERED_H
|
||||
#define _MZSTREAM_BUFFERED_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -32,8 +32,8 @@ int32_t ZCALLBACK mzstream_buffered_seek(voidpf stream, uint64_t offset, int ori
|
||||
int32_t ZCALLBACK mzstream_buffered_close(voidpf stream);
|
||||
int32_t ZCALLBACK mzstream_buffered_error(voidpf stream);
|
||||
|
||||
voidpf mzstream_buffered_alloc(void);
|
||||
void mzstream_buffered_free(voidpf stream);
|
||||
voidpf mzstream_buffered_alloc(void);
|
||||
void mzstream_buffered_free(voidpf stream);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
263
ioapi_crypt.c
Normal file
263
ioapi_crypt.c
Normal file
@ -0,0 +1,263 @@
|
||||
/* ioapi_crypt.c -- IO base function header for compress/uncompress .zip
|
||||
files using zlib + zip or unzip API
|
||||
|
||||
This version of ioapi is designed to access memory rather than files.
|
||||
We do use a region of memory to put data in to and take it out of. We do
|
||||
not have auto-extending buffers and do not inform anyone else that the
|
||||
data has been written. It is really intended for accessing a zip archive
|
||||
embedded in an application such that I can write an installer with no
|
||||
external files. Creation of archives has not been attempted, although
|
||||
parts of the framework are present.
|
||||
|
||||
Based on Unzip ioapi.c version 0.22, May 19th, 2003
|
||||
|
||||
Copyright (C) 2012-2017 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 2003 Justin Fletcher
|
||||
Copyright (C) 1998-2003 Gilles Vollant
|
||||
http://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This file is under the same license as the Unzip tool it is distributed
|
||||
with.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "zlib.h"
|
||||
#include "ioapi.h"
|
||||
|
||||
#include "ioapi_crypt.h"
|
||||
|
||||
#define RAND_HEAD_LEN 12
|
||||
|
||||
typedef struct mzstream_crypt_s {
|
||||
mzstream stream;
|
||||
uint32_t keys[3]; /* keys defining the pseudo-random sequence */
|
||||
const z_crc_t *crc_32_tab;
|
||||
int16_t initialized;
|
||||
int16_t error;
|
||||
uint8_t verify1;
|
||||
uint8_t verify2;
|
||||
char *password;
|
||||
uint64_t total_in;
|
||||
uint64_t total_out;
|
||||
} mzstream_crypt;
|
||||
|
||||
#define zdecode(keys,crc_32_tab,c) \
|
||||
(mzstream_crypt_update_keys(keys,crc_32_tab, c ^= mzstream_crypt_decrypt_byte(keys)))
|
||||
|
||||
#define zencode(keys,crc_32_tab,c,t) \
|
||||
(t = mzstream_crypt_decrypt_byte(keys), mzstream_crypt_update_keys(keys,crc_32_tab,c), t^(c))
|
||||
|
||||
uint8_t mzstream_crypt_decrypt_byte(uint32_t *keys)
|
||||
{
|
||||
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)(*(keys+2)) & 0xffff) | 2;
|
||||
return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
|
||||
}
|
||||
|
||||
uint8_t mzstream_crypt_update_keys(uint32_t *keys, const z_crc_t *crc_32_tab, int32_t c)
|
||||
{
|
||||
#define CRC32(c, b) ((*(crc_32_tab+(((uint32_t)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
|
||||
|
||||
(*(keys+0)) = (uint32_t)CRC32((*(keys+0)), c);
|
||||
(*(keys+1)) += (*(keys+0)) & 0xff;
|
||||
(*(keys+1)) = (*(keys+1)) * 134775813L + 1;
|
||||
{
|
||||
register int32_t keyshift = (int32_t)((*(keys + 1)) >> 24);
|
||||
(*(keys+2)) = (uint32_t)CRC32((*(keys+2)), keyshift);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
void mzstream_crypt_init_keys(const char *password, uint32_t *keys, const z_crc_t *crc_32_tab)
|
||||
{
|
||||
*(keys+0) = 305419896L;
|
||||
*(keys+1) = 591751049L;
|
||||
*(keys+2) = 878082192L;
|
||||
while (*password != 0)
|
||||
{
|
||||
mzstream_crypt_update_keys(keys, crc_32_tab, *password);
|
||||
password += 1;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t ZCALLBACK mzstream_crypt_open(voidpf stream, const char *filename, int mode)
|
||||
{
|
||||
mzstream_crypt *crypt = (mzstream_crypt *)stream;
|
||||
uint16_t t = 0;
|
||||
int16_t i = 0;
|
||||
uint8_t header[RAND_HEAD_LEN];
|
||||
uint8_t verify1 = 0;
|
||||
uint8_t verify2 = 0;
|
||||
|
||||
crypt->total_in = 0;
|
||||
crypt->total_out = 0;
|
||||
crypt->initialized = 0;
|
||||
|
||||
if (mzstream_is_open(crypt->stream.base) == MZSTREAM_ERR)
|
||||
return MZSTREAM_ERR;
|
||||
if (crypt->password == NULL)
|
||||
return MZSTREAM_ERR;
|
||||
|
||||
crypt->crc_32_tab = get_crc_table();
|
||||
if (crypt->crc_32_tab == NULL)
|
||||
return MZSTREAM_ERR;
|
||||
|
||||
mzstream_crypt_init_keys(crypt->password, crypt->keys, crypt->crc_32_tab);
|
||||
|
||||
if (mode & MZSTREAM_MODE_WRITE)
|
||||
{
|
||||
// First generate RAND_HEAD_LEN - 2 random bytes.
|
||||
mzstream_os_rand(header, RAND_HEAD_LEN - 2);
|
||||
|
||||
// Encrypt random header (last two bytes is high word of crc)
|
||||
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
|
||||
header[i] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, header[i], t);
|
||||
|
||||
header[i++] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, crypt->verify1, t);
|
||||
header[i++] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, crypt->verify2, t);
|
||||
|
||||
if (mzstream_write(crypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
|
||||
return MZSTREAM_ERR;
|
||||
|
||||
crypt->total_out += RAND_HEAD_LEN;
|
||||
}
|
||||
else if (mode & MZSTREAM_MODE_READ)
|
||||
{
|
||||
if (mzstream_read(crypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
|
||||
return MZSTREAM_ERR;
|
||||
|
||||
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
|
||||
header[i] = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i]);
|
||||
|
||||
crypt->verify1 = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i++]);
|
||||
crypt->verify2 = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i++]);
|
||||
|
||||
crypt->total_in += RAND_HEAD_LEN;
|
||||
}
|
||||
|
||||
crypt->initialized = 1;
|
||||
return MZSTREAM_OK;
|
||||
}
|
||||
|
||||
int32_t ZCALLBACK mzstream_crypt_is_open(voidpf stream)
|
||||
{
|
||||
mzstream_crypt *crypt = (mzstream_crypt *)stream;
|
||||
if (crypt->initialized == 0)
|
||||
return MZSTREAM_ERR;
|
||||
return MZSTREAM_OK;
|
||||
}
|
||||
|
||||
int32_t ZCALLBACK mzstream_crypt_read(voidpf stream, void *buf, uint32_t size)
|
||||
{
|
||||
mzstream_crypt *crypt = (mzstream_crypt *)stream;
|
||||
uint8_t *buf_ptr = (uint8_t *)buf;
|
||||
uint32_t read = 0;
|
||||
uint32_t i = 0;
|
||||
|
||||
read = mzstream_read(crypt->stream.base, buf, size);
|
||||
for (i = 0; i < read; i++)
|
||||
buf_ptr[i] = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, buf_ptr[i]);
|
||||
crypt->total_in += read;
|
||||
return read;
|
||||
}
|
||||
|
||||
int32_t ZCALLBACK mzstream_crypt_write(voidpf stream, const void *buf, uint32_t size)
|
||||
{
|
||||
mzstream_crypt *crypt = (mzstream_crypt *)stream;
|
||||
uint8_t *buf_ptr = (uint8_t *)buf;
|
||||
uint32_t written = 0;
|
||||
uint32_t i = 0;
|
||||
uint16_t t = 0;
|
||||
for (i = 0; i < size; i++)
|
||||
buf_ptr[i] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, buf_ptr[i], t);
|
||||
written = mzstream_write(crypt->stream.base, buf, size);
|
||||
if (written > 0)
|
||||
crypt->total_out += written;
|
||||
if (written != size)
|
||||
return MZSTREAM_ERR;
|
||||
return written;
|
||||
}
|
||||
|
||||
int64_t ZCALLBACK mzstream_crypt_tell(voidpf stream)
|
||||
{
|
||||
mzstream_crypt *crypt = (mzstream_crypt *)stream;
|
||||
return mzstream_tell(crypt->stream.base);
|
||||
}
|
||||
|
||||
int32_t ZCALLBACK mzstream_crypt_seek(voidpf stream, uint64_t offset, int origin)
|
||||
{
|
||||
mzstream_crypt *crypt = (mzstream_crypt *)stream;
|
||||
return mzstream_seek(crypt->stream.base, offset, origin);
|
||||
}
|
||||
|
||||
int32_t ZCALLBACK mzstream_crypt_close(voidpf stream)
|
||||
{
|
||||
mzstream_crypt *crypt = (mzstream_crypt *)stream;
|
||||
crypt->initialized = 0;
|
||||
return MZSTREAM_OK;
|
||||
}
|
||||
|
||||
int32_t ZCALLBACK mzstream_crypt_error(voidpf stream)
|
||||
{
|
||||
mzstream_crypt *crypt = (mzstream_crypt *)stream;
|
||||
return crypt->error;
|
||||
}
|
||||
|
||||
void mzstream_crypt_set_password(voidpf stream, char *password)
|
||||
{
|
||||
mzstream_crypt *crypt = (mzstream_crypt *)stream;
|
||||
crypt->password = password;
|
||||
}
|
||||
|
||||
void mzstream_crypt_set_verify(voidpf stream, uint8_t verify1, uint8_t verify2)
|
||||
{
|
||||
mzstream_crypt *crypt = (mzstream_crypt *)stream;
|
||||
crypt->verify1 = verify1;
|
||||
crypt->verify2 = verify2;
|
||||
}
|
||||
|
||||
void mzstream_crypt_get_verify(voidpf stream, uint8_t *verify1, uint8_t *verify2)
|
||||
{
|
||||
mzstream_crypt *crypt = (mzstream_crypt *)stream;
|
||||
*verify1 = crypt->verify1;
|
||||
*verify2 = crypt->verify2;
|
||||
}
|
||||
|
||||
voidpf mzstream_crypt_alloc(void)
|
||||
{
|
||||
mzstream_crypt *crypt = NULL;
|
||||
|
||||
crypt = (mzstream_crypt *)malloc(sizeof(mzstream_crypt));
|
||||
if (crypt == NULL)
|
||||
return NULL;
|
||||
|
||||
memset(crypt, 0, sizeof(mzstream_crypt));
|
||||
|
||||
crypt->stream.open = mzstream_crypt_open;
|
||||
crypt->stream.is_open = mzstream_crypt_is_open;
|
||||
crypt->stream.read = mzstream_crypt_read;
|
||||
crypt->stream.write = mzstream_crypt_write;
|
||||
crypt->stream.tell = mzstream_crypt_tell;
|
||||
crypt->stream.seek = mzstream_crypt_seek;
|
||||
crypt->stream.close = mzstream_crypt_close;
|
||||
crypt->stream.error = mzstream_crypt_error;
|
||||
crypt->stream.alloc = mzstream_crypt_alloc;
|
||||
crypt->stream.free = mzstream_crypt_free;
|
||||
|
||||
return (voidpf)crypt;
|
||||
}
|
||||
|
||||
void mzstream_crypt_free(voidpf stream)
|
||||
{
|
||||
mzstream_crypt *crypt = (mzstream_crypt *)stream;
|
||||
if (crypt != NULL)
|
||||
free(crypt);
|
||||
}
|
52
ioapi_crypt.h
Normal file
52
ioapi_crypt.h
Normal file
@ -0,0 +1,52 @@
|
||||
/* 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 _MZSTREAM_CRYPT_H
|
||||
#define _MZSTREAM_CRYPT_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "zlib.h"
|
||||
#include "ioapi.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int32_t ZCALLBACK mzstream_crypt_open(voidpf stream, const char* filename, int mode);
|
||||
int32_t ZCALLBACK mzstream_crypt_read(voidpf stream, void* buf, uint32_t size);
|
||||
int32_t ZCALLBACK mzstream_crypt_write(voidpf stream, const void* buf, uint32_t size);
|
||||
int64_t ZCALLBACK mzstream_crypt_tell(voidpf stream);
|
||||
int32_t ZCALLBACK mzstream_crypt_seek(voidpf stream, uint64_t offset, int origin);
|
||||
int32_t ZCALLBACK mzstream_crypt_close(voidpf stream);
|
||||
int32_t ZCALLBACK mzstream_crypt_error(voidpf stream);
|
||||
|
||||
void mzstream_crypt_set_password(voidpf stream, char *password);
|
||||
void mzstream_crypt_set_verify(voidpf stream, uint8_t verify1, uint8_t verify2);
|
||||
void mzstream_crypt_get_verify(voidpf stream, uint8_t *verify1, uint8_t *verify2);
|
||||
|
||||
voidpf mzstream_crypt_alloc(void);
|
||||
void mzstream_crypt_free(voidpf stream);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -37,7 +37,7 @@ typedef struct mzstream_mem_s {
|
||||
uint32_t size; // Size of the memory buffer
|
||||
uint32_t limit; // Furthest we've written
|
||||
uint32_t position; // Current positoin in the memory
|
||||
int growable; // Growable memory buffer
|
||||
int16_t growable; // Growable memory buffer
|
||||
} mzstream_mem;
|
||||
|
||||
int32_t ZCALLBACK mzstream_mem_open(voidpf stream, const char *filename, int mode)
|
||||
|
11
ioapi_mem.h
11
ioapi_mem.h
@ -4,16 +4,17 @@
|
||||
This version of ioapi is designed to access memory rather than files.
|
||||
We do use a region of memory to put data in to and take it out of.
|
||||
|
||||
Copyright (C) 2012-2017 Nathan Moinvaziri (https://github.com/nmoinvaz/minizip)
|
||||
(C) 2003 Justin Fletcher
|
||||
(C) 1998-2003 Gilles Vollant
|
||||
Copyright (C) 2012-2017 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 2003 Justin Fletcher
|
||||
Copyright (C) 1998-2003 Gilles Vollant
|
||||
|
||||
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 _IOAPI_MEM_H
|
||||
#define _IOAPI_MEM_H
|
||||
#ifndef _MZSTREAM_MEM_H
|
||||
#define _MZSTREAM_MEM_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
10
ioapi_zlib.c
10
ioapi_zlib.c
@ -71,14 +71,14 @@ int32_t ZCALLBACK mzstream_zlib_open(voidpf stream, const char *filename, int mo
|
||||
zlib->total_in = 0;
|
||||
zlib->total_out = 0;
|
||||
|
||||
if (mode == MZSTREAM_MODE_READ)
|
||||
if (mode & MZSTREAM_MODE_READ)
|
||||
{
|
||||
zlib->zstream.next_in = zlib->buffer;
|
||||
zlib->zstream.avail_in = 0;
|
||||
|
||||
zlib->error = inflateInit2(&zlib->zstream, -MAX_WBITS);
|
||||
}
|
||||
else if (mode == MZSTREAM_MODE_WRITE)
|
||||
else if (mode & MZSTREAM_MODE_WRITE)
|
||||
{
|
||||
window_bits = zlib->window_bits;
|
||||
if (window_bits > 0)
|
||||
@ -237,11 +237,11 @@ int32_t ZCALLBACK mzstream_zlib_close(voidpf stream)
|
||||
mzstream_zlib *zlib = (mzstream_zlib *)stream;
|
||||
uint32_t out_bytes = 0;
|
||||
|
||||
if (zlib->mode == MZSTREAM_MODE_READ)
|
||||
if (zlib->mode & MZSTREAM_MODE_READ)
|
||||
{
|
||||
inflateEnd(&zlib->zstream);
|
||||
}
|
||||
else if (zlib->mode == MZSTREAM_MODE_WRITE)
|
||||
else if (zlib->mode & MZSTREAM_MODE_WRITE)
|
||||
{
|
||||
out_bytes = mzstream_zlib_deflate(stream, Z_FINISH);
|
||||
|
||||
@ -383,7 +383,7 @@ int64_t ZCALLBACK mzstream_crc32_tell(voidpf stream)
|
||||
int32_t ZCALLBACK mzstream_crc32_seek(voidpf stream, uint64_t offset, int origin)
|
||||
{
|
||||
mzstream_crc32 *crc32 = (mzstream_crc32 *)stream;
|
||||
return MZSTREAM_ERR;
|
||||
return mzstream_seek(crc32->stream.base, offset, origin);
|
||||
}
|
||||
|
||||
int32_t ZCALLBACK mzstream_crc32_close(voidpf stream)
|
||||
|
10
ioapi_zlib.h
10
ioapi_zlib.h
@ -4,16 +4,15 @@
|
||||
This version of ioapi is designed to access memory rather than files.
|
||||
We do use a region of memory to put data in to and take it out of.
|
||||
|
||||
Copyright (C) 2012-2017 Nathan Moinvaziri (https://github.com/nmoinvaz/minizip)
|
||||
(C) 2003 Justin Fletcher
|
||||
(C) 1998-2003 Gilles Vollant
|
||||
Copyright (C) 2012-2017 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _IOAPI_ZLIB_H
|
||||
#define _IOAPI_ZLIB_H
|
||||
#ifndef _MZSTREAM_ZLIB_H
|
||||
#define _MZSTREAM_ZLIB_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -56,6 +55,7 @@ uint32_t mzstream_crc32_get_value(voidpf stream);
|
||||
|
||||
voidpf mzstream_crc32_alloc(void);
|
||||
void mzstream_crc32_free(voidpf stream);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
39
iowin32.c
39
iowin32.c
@ -15,7 +15,9 @@
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <tchar.h>
|
||||
|
||||
#include <windows.h>
|
||||
#include <wincrypt.h>
|
||||
|
||||
#include "zlib.h"
|
||||
#include "ioapi.h"
|
||||
@ -49,7 +51,7 @@ int32_t ZCALLBACK mzstream_win32_open(voidpf stream, const char *filename, int m
|
||||
mzstream_win32 *win32 = (mzstream_win32 *)stream;
|
||||
uint32_t desired_access = 0;
|
||||
uint32_t creation_disposition = 0;
|
||||
uint32_t share_mode = 0;
|
||||
uint32_t share_mode = FILE_SHARE_READ;
|
||||
uint32_t flags_attribs = FILE_ATTRIBUTE_NORMAL;
|
||||
wchar_t *filename_wide = NULL;
|
||||
uint32_t filename_wide_size = 0;
|
||||
@ -62,7 +64,7 @@ int32_t ZCALLBACK mzstream_win32_open(voidpf stream, const char *filename, int m
|
||||
{
|
||||
desired_access = GENERIC_READ;
|
||||
creation_disposition = OPEN_EXISTING;
|
||||
share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
|
||||
share_mode &= FILE_SHARE_WRITE;
|
||||
}
|
||||
else if (mode & MZSTREAM_MODE_EXISTING)
|
||||
{
|
||||
@ -94,7 +96,10 @@ int32_t ZCALLBACK mzstream_win32_open(voidpf stream, const char *filename, int m
|
||||
free(filename_wide);
|
||||
|
||||
if (mzstream_win32_is_open(stream) == MZSTREAM_ERR)
|
||||
{
|
||||
win32->error = GetLastError();
|
||||
return MZSTREAM_ERR;
|
||||
}
|
||||
|
||||
win32->filename_size = strlen(filename) + 1;
|
||||
win32->filename = (char *)malloc(win32->filename_size);
|
||||
@ -281,4 +286,30 @@ void mzstream_win32_free(voidpf stream)
|
||||
mzstream_win32 *win32 = (mzstream_win32 *)stream;
|
||||
if (win32 != NULL)
|
||||
free(win32);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t mzstream_win32_rand(uint8_t *buf, uint16_t size)
|
||||
{
|
||||
HCRYPTPROV provider;
|
||||
unsigned __int64 pentium_tsc[1];
|
||||
int32_t len = 0;
|
||||
int32_t result = 0;
|
||||
|
||||
|
||||
if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
|
||||
{
|
||||
result = CryptGenRandom(provider, size, buf);
|
||||
CryptReleaseContext(provider, 0);
|
||||
if (result)
|
||||
return size;
|
||||
}
|
||||
|
||||
for (len = 0; len < (int)size; ++len)
|
||||
{
|
||||
if (len % 8 == 0)
|
||||
QueryPerformanceCounter((LARGE_INTEGER *)pentium_tsc);
|
||||
buf[len] = ((unsigned char*)pentium_tsc)[len % 8];
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
10
iowin32.h
10
iowin32.h
@ -14,8 +14,8 @@
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _IOWIN32_H
|
||||
#define _IOWIN32_H
|
||||
#ifndef _MZSTREAM_WIN32_H
|
||||
#define _MZSTREAM_WIN32_H
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
@ -32,8 +32,10 @@ int32_t ZCALLBACK mzstream_win32_seek(voidpf stream, uint64_t offset, int origin
|
||||
int32_t ZCALLBACK mzstream_win32_close(voidpf stream);
|
||||
int32_t ZCALLBACK mzstream_win32_error(voidpf stream);
|
||||
|
||||
voidpf mzstream_win32_alloc(void);
|
||||
void mzstream_win32_free(voidpf stream);
|
||||
voidpf mzstream_win32_alloc(void);
|
||||
void mzstream_win32_free(voidpf stream);
|
||||
|
||||
int32_t mzstream_win32_rand(uint8_t *buf, uint16_t size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
153
minizip.c
153
minizip.c
@ -138,6 +138,153 @@ int minizip_addfile(zipFile zf, const char *path, const char *filenameinzip, int
|
||||
return err;
|
||||
}
|
||||
#include "ioapi_zlib.h"
|
||||
#include "ioapi_aes.h"
|
||||
void test_aes()
|
||||
{
|
||||
char buf[UINT16_MAX];
|
||||
int16_t read = 0;
|
||||
int16_t written = 0;
|
||||
|
||||
voidpf in_stream = mzstream_os_alloc();
|
||||
|
||||
if (mzstream_os_open(in_stream, "LICENSE", MZSTREAM_MODE_READ) == MZSTREAM_OK)
|
||||
{
|
||||
read = mzstream_os_read(in_stream, buf, UINT16_MAX);
|
||||
mzstream_os_close(in_stream);
|
||||
}
|
||||
|
||||
mzstream_os_free(in_stream);
|
||||
|
||||
voidpf out_stream = mzstream_os_alloc();
|
||||
|
||||
if (mzstream_os_open(out_stream, "LICENSE.aes-encrypt", MZSTREAM_MODE_CREATE | MZSTREAM_MODE_WRITE) == MZSTREAM_OK)
|
||||
{
|
||||
voidpf aes_out_stream = mzstream_aes_alloc();
|
||||
|
||||
mzstream_aes_set_password(aes_out_stream, "hello");
|
||||
mzstream_set_base(aes_out_stream, out_stream);
|
||||
|
||||
if (mzstream_open(aes_out_stream, NULL, MZSTREAM_MODE_WRITE) == MZSTREAM_OK)
|
||||
{
|
||||
written = mzstream_write(aes_out_stream, buf, read);
|
||||
mzstream_close(aes_out_stream);
|
||||
}
|
||||
|
||||
mzstream_aes_free(aes_out_stream);
|
||||
|
||||
mzstream_os_close(out_stream);
|
||||
|
||||
printf("LICENSE aes encrypted %d\n", written);
|
||||
}
|
||||
|
||||
mzstream_os_free(out_stream);
|
||||
|
||||
in_stream = mzstream_os_alloc();
|
||||
|
||||
if (mzstream_os_open(in_stream, "LICENSE.aes-encrypt", MZSTREAM_MODE_READ) == MZSTREAM_OK)
|
||||
{
|
||||
voidpf aes_out_stream = mzstream_aes_alloc();
|
||||
|
||||
mzstream_aes_set_password(aes_out_stream, "hello");
|
||||
mzstream_set_base(aes_out_stream, in_stream);
|
||||
|
||||
if (mzstream_open(aes_out_stream, NULL, MZSTREAM_MODE_READ) == MZSTREAM_OK)
|
||||
{
|
||||
read = mzstream_read(aes_out_stream, buf, read);
|
||||
mzstream_close(aes_out_stream);
|
||||
}
|
||||
|
||||
mzstream_aes_free(aes_out_stream);
|
||||
|
||||
mzstream_os_close(in_stream);
|
||||
|
||||
printf("LICENSE aes decrypted %d\n", read);
|
||||
}
|
||||
|
||||
mzstream_os_free(in_stream);
|
||||
out_stream = mzstream_os_alloc();
|
||||
|
||||
if (mzstream_os_open(out_stream, "LICENSE.aes-decrypt", MZSTREAM_MODE_CREATE | MZSTREAM_MODE_WRITE) == MZSTREAM_OK)
|
||||
{
|
||||
mzstream_os_write(out_stream, buf, read);
|
||||
mzstream_os_close(out_stream);
|
||||
}
|
||||
|
||||
mzstream_os_free(out_stream);
|
||||
}
|
||||
void test_crypt()
|
||||
{
|
||||
char buf[UINT16_MAX];
|
||||
int16_t read = 0;
|
||||
int16_t written = 0;
|
||||
|
||||
voidpf in_stream = mzstream_os_alloc();
|
||||
|
||||
if (mzstream_os_open(in_stream, "LICENSE", MZSTREAM_MODE_READ) == MZSTREAM_OK)
|
||||
{
|
||||
read = mzstream_os_read(in_stream, buf, UINT16_MAX);
|
||||
mzstream_os_close(in_stream);
|
||||
}
|
||||
|
||||
mzstream_os_free(in_stream);
|
||||
|
||||
voidpf out_stream = mzstream_os_alloc();
|
||||
|
||||
if (mzstream_os_open(out_stream, "LICENSE.encrypt", MZSTREAM_MODE_CREATE | MZSTREAM_MODE_WRITE) == MZSTREAM_OK)
|
||||
{
|
||||
voidpf crypt_out_stream = mzstream_crypt_alloc();
|
||||
|
||||
mzstream_crypt_set_password(crypt_out_stream, "hello");
|
||||
mzstream_set_base(crypt_out_stream, out_stream);
|
||||
|
||||
if (mzstream_open(crypt_out_stream, NULL, MZSTREAM_MODE_WRITE) == MZSTREAM_OK)
|
||||
{
|
||||
written = mzstream_write(crypt_out_stream, buf, read);
|
||||
mzstream_close(crypt_out_stream);
|
||||
}
|
||||
|
||||
mzstream_crypt_free(crypt_out_stream);
|
||||
|
||||
mzstream_os_close(out_stream);
|
||||
|
||||
printf("LICENSE encrypted %d\n", written);
|
||||
}
|
||||
|
||||
mzstream_os_free(out_stream);
|
||||
|
||||
in_stream = mzstream_os_alloc();
|
||||
|
||||
if (mzstream_os_open(in_stream, "LICENSE.encrypt", MZSTREAM_MODE_READ) == MZSTREAM_OK)
|
||||
{
|
||||
voidpf crypt_out_stream = mzstream_crypt_alloc();
|
||||
|
||||
mzstream_crypt_set_password(crypt_out_stream, "hello");
|
||||
mzstream_set_base(crypt_out_stream, in_stream);
|
||||
|
||||
if (mzstream_open(crypt_out_stream, NULL, MZSTREAM_MODE_READ) == MZSTREAM_OK)
|
||||
{
|
||||
read = mzstream_read(crypt_out_stream, buf, read);
|
||||
mzstream_close(crypt_out_stream);
|
||||
}
|
||||
|
||||
mzstream_crypt_free(crypt_out_stream);
|
||||
|
||||
mzstream_os_close(in_stream);
|
||||
|
||||
printf("LICENSE decrypted %d\n", read);
|
||||
}
|
||||
|
||||
mzstream_os_free(in_stream);
|
||||
out_stream = mzstream_os_alloc();
|
||||
|
||||
if (mzstream_os_open(out_stream, "LICENSE.decrypt", MZSTREAM_MODE_CREATE | MZSTREAM_MODE_WRITE) == MZSTREAM_OK)
|
||||
{
|
||||
mzstream_os_write(out_stream, buf, read);
|
||||
mzstream_os_close(out_stream);
|
||||
}
|
||||
|
||||
mzstream_os_free(out_stream);
|
||||
}
|
||||
void test_inflate()
|
||||
{
|
||||
char buf[UINT16_MAX];
|
||||
@ -153,8 +300,8 @@ void test_inflate()
|
||||
|
||||
mzstream_set_base(inflate_stream, in_stream);
|
||||
|
||||
read = mzstream_zlib_read(inflate_stream, buf, UINT16_MAX);
|
||||
mzstream_zlib_close(inflate_stream);
|
||||
read = mzstream_read(inflate_stream, buf, UINT16_MAX);
|
||||
mzstream_close(inflate_stream);
|
||||
|
||||
total_in = mzstream_zlib_get_total_in(inflate_stream);
|
||||
total_out = mzstream_zlib_get_total_out(inflate_stream);
|
||||
@ -249,6 +396,8 @@ int main(int argc, char *argv[])
|
||||
int opt_overwrite = APPEND_STATUS_CREATE;
|
||||
int opt_compress_level = Z_DEFAULT_COMPRESSION;
|
||||
int opt_exclude_path = 0;
|
||||
test_crypt();
|
||||
test_aes();
|
||||
test_deflate();
|
||||
test_inflate();
|
||||
minizip_banner();
|
||||
|
Loading…
x
Reference in New Issue
Block a user