Skip to content

Commit c3f4e45

Browse files
committed
Update minizip library to version 1.1
1 parent 1e89bea commit c3f4e45

File tree

5 files changed

+1748
-871
lines changed

5 files changed

+1748
-871
lines changed

src/drivers/minizip/crypt.h

+13-20
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,20 @@
3232
/***********************************************************************
3333
* Return the next byte in the pseudo-random sequence
3434
*/
35-
static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab)
36-
{
35+
static int decrypt_byte(unsigned long* pkeys, const z_crc_t* pcrc_32_tab) {
3736
unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
3837
* unpredictable manner on 16-bit systems; not a problem
3938
* with any known compiler so far, though */
4039

40+
(void)pcrc_32_tab;
4141
temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2;
4242
return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
4343
}
4444

4545
/***********************************************************************
4646
* Update the encryption keys with the next byte of plain text
4747
*/
48-
static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int c)
49-
{
48+
static int update_keys(unsigned long* pkeys, const z_crc_t* pcrc_32_tab, int c) {
5049
(*(pkeys+0)) = CRC32((*(pkeys+0)), c);
5150
(*(pkeys+1)) += (*(pkeys+0)) & 0xff;
5251
(*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
@@ -62,8 +61,7 @@ static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int
6261
* Initialize the encryption keys and the random header according to
6362
* the given password.
6463
*/
65-
static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned long* pcrc_32_tab)
66-
{
64+
static void init_keys(const char* passwd, unsigned long* pkeys, const z_crc_t* pcrc_32_tab) {
6765
*(pkeys+0) = 305419896L;
6866
*(pkeys+1) = 591751049L;
6967
*(pkeys+2) = 878082192L;
@@ -76,26 +74,21 @@ static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned lon
7674
#define zdecode(pkeys,pcrc_32_tab,c) \
7775
(update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab)))
7876

79-
#define zencode(pkeys,pcrc_32_tab,c,t) \
80-
(t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c))
77+
#define zencode(pkeys, pcrc_32_tab, c, t) \
78+
(t = decrypt_byte(pkeys, pcrc_32_tab), update_keys(pkeys, pcrc_32_tab, c), (Byte)t ^ (c))
8179

8280
#ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED
8381

8482
#define RAND_HEAD_LEN 12
8583
/* "last resort" source for second part of crypt seed pattern */
8684
# ifndef ZCR_SEED2
87-
# define ZCR_SEED2 3141592654UL /* use PI as default pattern */
85+
#define ZCR_SEED2 3141592654UL /* use PI as default pattern */
8886
# endif
8987

90-
static int crypthead(passwd, buf, bufSize, pkeys, pcrc_32_tab, crcForCrypting)
91-
const char *passwd; /* password string */
92-
unsigned char *buf; /* where to write header */
93-
int bufSize;
94-
unsigned long* pkeys;
95-
const unsigned long* pcrc_32_tab;
96-
unsigned long crcForCrypting;
97-
{
98-
int n; /* index in random header */
88+
static unsigned crypthead(const char* passwd, /* password string */
89+
unsigned char* buf, /* where to write header */
90+
int bufSize, unsigned long* pkeys, const z_crc_t* pcrc_32_tab, unsigned long crcForCrypting) {
91+
unsigned n; /* index in random header */
9992
int t; /* temporary */
10093
int c; /* random byte */
10194
unsigned char header[RAND_HEAD_LEN-2]; /* random header */
@@ -124,8 +117,8 @@ static int crypthead(passwd, buf, bufSize, pkeys, pcrc_32_tab, crcForCrypting)
124117
{
125118
buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t);
126119
}
127-
buf[n++] = zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t);
128-
buf[n++] = zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t);
120+
buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t);
121+
buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t);
129122
return n;
130123
}
131124

src/drivers/minizip/ioapi.c

+144-95
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,101 @@
1-
/* ioapi.c -- IO base function header for compress/uncompress .zip
2-
files using zlib + zip or unzip API
1+
/* ioapi.h -- IO base function header for compress/uncompress .zip
2+
part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
33
4-
Version 1.01e, February 12th, 2005
4+
Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
5+
6+
Modifications for Zip64 support
7+
Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
8+
9+
For more info read MiniZip_info.txt
510
6-
Copyright (C) 1998-2005 Gilles Vollant
711
*/
812

9-
#include <stdio.h>
10-
#include <stdlib.h>
11-
#include <string.h>
13+
#if defined(_WIN32) && (!(defined(_CRT_SECURE_NO_WARNINGS)))
14+
#define _CRT_SECURE_NO_WARNINGS
15+
#endif
16+
17+
#if defined(__APPLE__) || defined(IOAPI_NO_64) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64)
18+
// In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
19+
#define FOPEN_FUNC(filename, mode) fopen(filename, mode)
20+
#define FTELLO_FUNC(stream) ftello(stream)
21+
#define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
22+
#else
23+
#define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
24+
#define FTELLO_FUNC(stream) ftello64(stream)
25+
#define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
26+
#endif
27+
1228

13-
#include "zlib.h"
1429
#include "ioapi.h"
1530

31+
voidpf call_zopen64(const zlib_filefunc64_32_def* pfilefunc, const void* filename, int mode) {
32+
if (pfilefunc->zfile_func64.zopen64_file != NULL)
33+
return (*(pfilefunc->zfile_func64.zopen64_file))(pfilefunc->zfile_func64.opaque, filename, mode);
34+
else {
35+
return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque, (const char*)filename, mode);
36+
}
37+
}
1638

39+
long call_zseek64(const zlib_filefunc64_32_def* pfilefunc, voidpf filestream, ZPOS64_T offset, int origin) {
40+
if (pfilefunc->zfile_func64.zseek64_file != NULL)
41+
return (*(pfilefunc->zfile_func64.zseek64_file))(pfilefunc->zfile_func64.opaque, filestream, offset, origin);
42+
else {
43+
uLong offsetTruncated = (uLong)offset;
44+
if (offsetTruncated != offset)
45+
return -1;
46+
else
47+
return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque, filestream, offsetTruncated, origin);
48+
}
49+
}
1750

18-
/* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
51+
ZPOS64_T call_ztell64(const zlib_filefunc64_32_def* pfilefunc, voidpf filestream) {
52+
if (pfilefunc->zfile_func64.zseek64_file != NULL)
53+
return (*(pfilefunc->zfile_func64.ztell64_file))(pfilefunc->zfile_func64.opaque, filestream);
54+
else {
55+
uLong tell_uLong = (uLong)(*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque, filestream);
56+
if ((tell_uLong) == MAXU32)
57+
return (ZPOS64_T)-1;
58+
else
59+
return tell_uLong;
60+
}
61+
}
1962

20-
#ifndef SEEK_CUR
21-
#define SEEK_CUR 1
22-
#endif
63+
void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,
64+
const zlib_filefunc_def* p_filefunc32) {
65+
p_filefunc64_32->zfile_func64.zopen64_file = NULL;
66+
p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
67+
p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
68+
p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
69+
p_filefunc64_32->zfile_func64.ztell64_file = NULL;
70+
p_filefunc64_32->zfile_func64.zseek64_file = NULL;
71+
p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
72+
p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
73+
p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
74+
p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
75+
p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
76+
}
2377

24-
#ifndef SEEK_END
25-
#define SEEK_END 2
26-
#endif
2778

28-
#ifndef SEEK_SET
29-
#define SEEK_SET 0
30-
#endif
79+
static voidpf ZCALLBACK fopen_file_func(voidpf opaque, const char* filename, int mode) {
80+
FILE* file = NULL;
81+
const char* mode_fopen = NULL;
82+
(void)opaque;
83+
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
84+
mode_fopen = "rb";
85+
else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
86+
mode_fopen = "r+b";
87+
else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
88+
mode_fopen = "wb";
3189

32-
voidpf ZCALLBACK fopen_file_func OF((
33-
voidpf opaque,
34-
const char* filename,
35-
int mode));
36-
37-
uLong ZCALLBACK fread_file_func OF((
38-
voidpf opaque,
39-
voidpf stream,
40-
void* buf,
41-
uLong size));
42-
43-
uLong ZCALLBACK fwrite_file_func OF((
44-
voidpf opaque,
45-
voidpf stream,
46-
const void* buf,
47-
uLong size));
48-
49-
long ZCALLBACK ftell_file_func OF((
50-
voidpf opaque,
51-
voidpf stream));
52-
53-
long ZCALLBACK fseek_file_func OF((
54-
voidpf opaque,
55-
voidpf stream,
56-
uLong offset,
57-
int origin));
58-
59-
int ZCALLBACK fclose_file_func OF((
60-
voidpf opaque,
61-
voidpf stream));
62-
63-
int ZCALLBACK ferror_file_func OF((
64-
voidpf opaque,
65-
voidpf stream));
66-
67-
68-
voidpf ZCALLBACK fopen_file_func (opaque, filename, mode)
69-
voidpf opaque;
70-
const char* filename;
71-
int mode;
72-
{
90+
if ((filename != NULL) && (mode_fopen != NULL))
91+
file = fopen(filename, mode_fopen);
92+
return file;
93+
}
94+
95+
static voidpf ZCALLBACK fopen64_file_func(voidpf opaque, const void* filename, int mode) {
7396
FILE* file = NULL;
7497
const char* mode_fopen = NULL;
98+
(void)opaque;
7599
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
76100
mode_fopen = "rb";
77101
else
@@ -82,51 +106,67 @@ voidpf ZCALLBACK fopen_file_func (opaque, filename, mode)
82106
mode_fopen = "wb";
83107

84108
if ((filename!=NULL) && (mode_fopen != NULL))
85-
file = fopen(filename, mode_fopen);
109+
file = FOPEN_FUNC((const char*)filename, mode_fopen);
86110
return file;
87111
}
88112

89113

90-
uLong ZCALLBACK fread_file_func (opaque, stream, buf, size)
91-
voidpf opaque;
92-
voidpf stream;
93-
void* buf;
94-
uLong size;
95-
{
114+
static uLong ZCALLBACK fread_file_func(voidpf opaque, voidpf stream, void* buf, uLong size) {
96115
uLong ret;
116+
(void)opaque;
97117
ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
98118
return ret;
99119
}
100120

101-
102-
uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size)
103-
voidpf opaque;
104-
voidpf stream;
105-
const void* buf;
106-
uLong size;
107-
{
121+
static uLong ZCALLBACK fwrite_file_func(voidpf opaque, voidpf stream, const void* buf, uLong size) {
108122
uLong ret;
123+
(void)opaque;
109124
ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
110125
return ret;
111126
}
112127

113-
long ZCALLBACK ftell_file_func (opaque, stream)
114-
voidpf opaque;
115-
voidpf stream;
116-
{
128+
static long ZCALLBACK ftell_file_func(voidpf opaque, voidpf stream) {
117129
long ret;
130+
(void)opaque;
118131
ret = ftell((FILE *)stream);
119132
return ret;
120133
}
121134

122-
long ZCALLBACK fseek_file_func (opaque, stream, offset, origin)
123-
voidpf opaque;
124-
voidpf stream;
125-
uLong offset;
126-
int origin;
127-
{
135+
136+
static ZPOS64_T ZCALLBACK ftell64_file_func(voidpf opaque, voidpf stream) {
137+
ZPOS64_T ret;
138+
(void)opaque;
139+
ret = (ZPOS64_T)FTELLO_FUNC((FILE*)stream);
140+
return ret;
141+
}
142+
143+
static long ZCALLBACK fseek_file_func(voidpf opaque, voidpf stream, uLong offset, int origin) {
144+
int fseek_origin = 0;
145+
long ret;
146+
(void)opaque;
147+
switch (origin) {
148+
case ZLIB_FILEFUNC_SEEK_CUR:
149+
fseek_origin = SEEK_CUR;
150+
break;
151+
case ZLIB_FILEFUNC_SEEK_END:
152+
fseek_origin = SEEK_END;
153+
break;
154+
case ZLIB_FILEFUNC_SEEK_SET:
155+
fseek_origin = SEEK_SET;
156+
break;
157+
default:
158+
return -1;
159+
}
160+
ret = 0;
161+
if (fseek((FILE*)stream, (long)offset, fseek_origin) != 0)
162+
ret = -1;
163+
return ret;
164+
}
165+
166+
static long ZCALLBACK fseek64_file_func(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin) {
128167
int fseek_origin=0;
129168
long ret;
169+
(void)opaque;
130170
switch (origin)
131171
{
132172
case ZLIB_FILEFUNC_SEEK_CUR :
@@ -141,31 +181,29 @@ long ZCALLBACK fseek_file_func (opaque, stream, offset, origin)
141181
default: return -1;
142182
}
143183
ret = 0;
144-
fseek((FILE *)stream, offset, fseek_origin);
184+
185+
if (FSEEKO_FUNC((FILE*)stream, (z_off64_t)offset, fseek_origin) != 0)
186+
ret = -1;
187+
145188
return ret;
146189
}
147190

148-
int ZCALLBACK fclose_file_func (opaque, stream)
149-
voidpf opaque;
150-
voidpf stream;
151-
{
191+
192+
static int ZCALLBACK fclose_file_func(voidpf opaque, voidpf stream) {
152193
int ret;
194+
(void)opaque;
153195
ret = fclose((FILE *)stream);
154196
return ret;
155197
}
156198

157-
int ZCALLBACK ferror_file_func (opaque, stream)
158-
voidpf opaque;
159-
voidpf stream;
160-
{
199+
static int ZCALLBACK ferror_file_func(voidpf opaque, voidpf stream) {
161200
int ret;
201+
(void)opaque;
162202
ret = ferror((FILE *)stream);
163203
return ret;
164204
}
165205

166-
void fill_fopen_filefunc (pzlib_filefunc_def)
167-
zlib_filefunc_def* pzlib_filefunc_def;
168-
{
206+
void fill_fopen_filefunc(zlib_filefunc_def* pzlib_filefunc_def) {
169207
pzlib_filefunc_def->zopen_file = fopen_file_func;
170208
pzlib_filefunc_def->zread_file = fread_file_func;
171209
pzlib_filefunc_def->zwrite_file = fwrite_file_func;
@@ -175,3 +213,14 @@ void fill_fopen_filefunc (pzlib_filefunc_def)
175213
pzlib_filefunc_def->zerror_file = ferror_file_func;
176214
pzlib_filefunc_def->opaque = NULL;
177215
}
216+
217+
void fill_fopen64_filefunc(zlib_filefunc64_def* pzlib_filefunc_def) {
218+
pzlib_filefunc_def->zopen64_file = fopen64_file_func;
219+
pzlib_filefunc_def->zread_file = fread_file_func;
220+
pzlib_filefunc_def->zwrite_file = fwrite_file_func;
221+
pzlib_filefunc_def->ztell64_file = ftell64_file_func;
222+
pzlib_filefunc_def->zseek64_file = fseek64_file_func;
223+
pzlib_filefunc_def->zclose_file = fclose_file_func;
224+
pzlib_filefunc_def->zerror_file = ferror_file_func;
225+
pzlib_filefunc_def->opaque = NULL;
226+
}

0 commit comments

Comments
 (0)