git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@104 77e5149b-7576-45b1-b177-96237e5ba77b
Tomasz Kojm authored on 2003/11/07 06:53:29... | ... |
@@ -1,3 +1,7 @@ |
1 |
+Thu Nov 6 22:40:58 CET 2003 (tk) |
|
2 |
+---------------------------------- |
|
3 |
+ * libclamav: zziplib: reverted to 0.12.83 |
|
4 |
+ |
|
1 | 5 |
Thu Nov 6 10:36:57 IST 2003 (njh) |
2 | 6 |
---------------------------------- |
3 | 7 |
* libclamav: Not all applications (those not sent as legal attachments) |
... | ... |
@@ -243,7 +243,7 @@ int cl_statinidir(const char *dirname, struct cl_stat *dbstat) |
243 | 243 |
|
244 | 244 |
while((dent = readdir(dd))) { |
245 | 245 |
if(dent->d_ino) { |
246 |
- if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") && (cli_strbcasestr(dent->d_name, ".db") || cli_strbcasestr(dent->d_name, ".db2"))) { |
|
246 |
+ if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") && (cli_strbcasestr(dent->d_name, ".db") || cli_strbcasestr(dent->d_name, ".db2") || cli_strbcasestr(dent->d_name, ".cvd"))) { |
|
247 | 247 |
|
248 | 248 |
dbstat->no++; |
249 | 249 |
dbstat->stattab = (struct stat *) realloc(dbstat->stattab, dbstat->no * sizeof(struct stat)); |
... | ... |
@@ -282,7 +282,7 @@ int cl_statchkdir(const struct cl_stat *dbstat) |
282 | 282 |
|
283 | 283 |
while((dent = readdir(dd))) { |
284 | 284 |
if(dent->d_ino) { |
285 |
- if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") && (cli_strbcasestr(dent->d_name, ".db") || cli_strbcasestr(dent->d_name, ".db2"))) { |
|
285 |
+ if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") && (cli_strbcasestr(dent->d_name, ".db") || cli_strbcasestr(dent->d_name, ".db2") || cli_strbcasestr(dent->d_name, ".cvd"))) { |
|
286 | 286 |
|
287 | 287 |
fname = cli_calloc(strlen(dbstat->dir) + strlen(dent->d_name) + 2, sizeof(char)); |
288 | 288 |
sprintf(fname, "%s/%s", dbstat->dir, dent->d_name); |
... | ... |
@@ -2,7 +2,7 @@ |
2 | 2 |
* Author: |
3 | 3 |
* Guido Draheim <guidod@gmx.de> |
4 | 4 |
* |
5 |
- * Copyright (c) 2001 Guido Draheim |
|
5 |
+ * Copyright (c) 2001,2002,2003 Guido Draheim |
|
6 | 6 |
* All rights reserved, |
7 | 7 |
* use under the restrictions of the |
8 | 8 |
* Lesser GNU General Public License |
... | ... |
@@ -53,15 +53,34 @@ |
53 | 53 |
#define _zzip_inline inline |
54 | 54 |
#endif |
55 | 55 |
#endif |
56 |
+#ifndef _zzip_size_t |
|
57 |
+#ifdef ZZIP_size_t |
|
58 |
+#define _zzip_size_t ZZIP_size_t |
|
59 |
+#else |
|
60 |
+#define _zzip_size_t size_t |
|
61 |
+#endif |
|
62 |
+#endif |
|
63 |
+#ifndef _zzip_ssize_t |
|
64 |
+#ifdef ZZIP_ssize_t |
|
65 |
+#define _zzip_ssize_t ZZIP_ssize_t |
|
66 |
+#else |
|
67 |
+#define _zzip_ssize_t ssize_t |
|
68 |
+#endif |
|
69 |
+#endif |
|
56 | 70 |
|
57 | 71 |
/* whether this library shall use a 64bit off_t largefile variant in 64on32: */ |
58 | 72 |
/* (some exported names must be renamed to avoid bad calls after linking) */ |
59 |
-#if defined ZZIP_LARGEFILE_SENSITIVE && _FILE_OFFSET_BITS+0 == 64 |
|
60 |
-#define ZZIP_LARGEFILE_RENAME |
|
61 |
-#elif defined _LARGE_FILES /* on AIX */ |
|
62 |
-#define ZZIP_LARGEFILE_RENAME |
|
73 |
+#if defined ZZIP_LARGEFILE_SENSITIVE |
|
74 |
+# if _FILE_OFFSET_BITS+0 == 64 |
|
75 |
+# define ZZIP_LARGEFILE_RENAME |
|
76 |
+# elif defined _LARGE_FILES /* used on older AIX to get at 64bit off_t */ |
|
77 |
+# define ZZIP_LARGEFILE_RENAME |
|
78 |
+# elif defined _ZZIP_LARGEFILE /* or simply use this one for zzip64 runs */ |
|
79 |
+# define ZZIP_LARGEFILE_RENAME |
|
80 |
+# endif |
|
63 | 81 |
#endif |
64 |
-/* if some were forgotten but required to have 64bit off_t largefile.. */ |
|
82 |
+ |
|
83 |
+/* if the environment did not setup these for 64bit off_t largefile... */ |
|
65 | 84 |
#ifdef ZZIP_LARGEFILE_RENAME |
66 | 85 |
# ifndef _FILE_OFFSET_BITS |
67 | 86 |
# ifdef ZZIP__FILE_OFFSET_BITS /* == 64 */ |
... | ... |
@@ -70,7 +89,12 @@ |
70 | 70 |
# endif |
71 | 71 |
# ifndef _LARGE_FILES |
72 | 72 |
# ifdef ZZIP__LARGE_FILES /* == 1 */ |
73 |
-# define _LARGE_FILES 1 |
|
73 |
+# define _LARGE_FILES ZZIP__LARGE_FILES |
|
74 |
+# endif |
|
75 |
+# endif |
|
76 |
+# ifndef _LARGEFILE_SOURCE |
|
77 |
+# ifdef ZZIP__LARGEFILE_SOURCE /* == 1 */ |
|
78 |
+# define _LARGEFILE_SOURCE ZZIP__LARGEFILE_SOURCE |
|
74 | 79 |
# endif |
75 | 80 |
# endif |
76 | 81 |
#endif |
... | ... |
@@ -141,6 +165,11 @@ |
141 | 141 |
#define __attribute__(X) |
142 | 142 |
#endif |
143 | 143 |
|
144 |
+#if defined ZZIP_EXPORTS || defined ZZIPLIB_EXPORTS |
|
145 |
+# undef ZZIP_DLL |
|
146 |
+#define ZZIP_DLL 1 |
|
147 |
+#endif |
|
148 |
+ |
|
144 | 149 |
/* based on zconf.h : */ |
145 | 150 |
/* compile with -DZZIP_DLL for Windows DLL support */ |
146 | 151 |
#if defined ZZIP_DLL |
... | ... |
@@ -1,4 +1,3 @@ |
1 |
-#define USE_DIRENT 0 |
|
2 | 1 |
/* |
3 | 2 |
* Author: |
4 | 3 |
* Guido Draheim <guidod@gmx.de> |
... | ... |
@@ -24,6 +23,8 @@ |
24 | 24 |
#include <stdio.h> |
25 | 25 |
#endif |
26 | 26 |
|
27 |
+//#include "__dirent.h" |
|
28 |
+ |
|
27 | 29 |
#ifndef offsetof |
28 | 30 |
#pragma warning had to DEFINE offsetof as it was not in stddef.h |
29 | 31 |
#define offsetof(T,M) ((unsigned)(& ((T*)0)->M)) |
... | ... |
@@ -50,11 +51,13 @@ zzip_rewinddir(ZZIP_DIR * dir) |
50 | 50 |
{ |
51 | 51 |
if (! dir) return; |
52 | 52 |
|
53 |
+ /* |
|
53 | 54 |
if (USE_DIRENT && dir->realdir) |
54 | 55 |
{ |
55 | 56 |
_zzip_rewinddir(dir->realdir); |
56 | 57 |
return; |
57 | 58 |
} |
59 |
+ */ |
|
58 | 60 |
|
59 | 61 |
if (dir->hdr0) |
60 | 62 |
dir->hdr = dir->hdr0; |
... | ... |
@@ -118,11 +121,12 @@ zzip_readdir(ZZIP_DIR * dir) |
118 | 118 |
{ |
119 | 119 |
if (! dir) { errno=EBADF; return 0; } |
120 | 120 |
|
121 |
+ /* |
|
121 | 122 |
if (USE_DIRENT && dir->realdir) |
122 | 123 |
{ |
123 | 124 |
if (! real_readdir(dir)) |
124 | 125 |
return 0; |
125 |
- }else |
|
126 |
+ }else */ |
|
126 | 127 |
{ |
127 | 128 |
if (! dir->hdr) return 0; |
128 | 129 |
|
... | ... |
@@ -147,10 +151,10 @@ zzip_telldir(ZZIP_DIR* dir) |
147 | 147 |
{ |
148 | 148 |
if (! dir) { errno=EBADF; return -1; } |
149 | 149 |
|
150 |
- if (USE_DIRENT && dir->realdir) |
|
150 |
+ /* if (USE_DIRENT && dir->realdir) |
|
151 | 151 |
{ |
152 | 152 |
return _zzip_telldir(dir->realdir); |
153 |
- }else |
|
153 |
+ }else*/ |
|
154 | 154 |
{ |
155 | 155 |
return ((zzip_off_t) ((char*) dir->hdr - (char*) dir->hdr0)); |
156 | 156 |
} |
... | ... |
@@ -164,10 +168,10 @@ zzip_seekdir(ZZIP_DIR* dir, zzip_off_t offset) |
164 | 164 |
{ |
165 | 165 |
if (! dir) return; |
166 | 166 |
|
167 |
- if (USE_DIRENT && dir->realdir) |
|
167 |
+ /*if (USE_DIRENT && dir->realdir) |
|
168 | 168 |
{ |
169 | 169 |
_zzip_seekdir(dir->realdir, offset); |
170 |
- }else |
|
170 |
+ }else*/ |
|
171 | 171 |
{ |
172 | 172 |
dir->hdr = (struct zzip_dir_hdr*) |
173 | 173 |
(dir->hdr0 ? (char*) dir->hdr0 + (size_t) offset : 0); |
... | ... |
@@ -229,6 +233,23 @@ zzip_opendir_ext_io(zzip_char_t* filename, int o_modes, |
229 | 229 |
# ifdef ZZIP_HAVE_SYS_STAT_H |
230 | 230 |
if (stat(filename, &st) >= 0 && S_ISDIR(st.st_mode) |
231 | 231 |
){ |
232 |
+ /* if (USE_DIRENT) |
|
233 |
+ { |
|
234 |
+ _zzip_DIR* realdir = _zzip_opendir(filename); |
|
235 |
+ if (realdir) |
|
236 |
+ { |
|
237 |
+ if (! (dir = (ZZIP_DIR *)calloc(1, sizeof (*dir)))) |
|
238 |
+ { |
|
239 |
+ _zzip_closedir(realdir); |
|
240 |
+ return 0; |
|
241 |
+ }else |
|
242 |
+ { |
|
243 |
+ dir->realdir = realdir; |
|
244 |
+ dir->realname = strdup(filename); |
|
245 |
+ return dir; |
|
246 |
+ } |
|
247 |
+ } |
|
248 |
+ } */ |
|
232 | 249 |
return 0; |
233 | 250 |
} |
234 | 251 |
# endif /* HAVE_SYS_STAT_H */ |
... | ... |
@@ -253,13 +274,13 @@ zzip_closedir(ZZIP_DIR* dir) |
253 | 253 |
{ |
254 | 254 |
if (! dir) { errno = EBADF; return -1; } |
255 | 255 |
|
256 |
- if (USE_DIRENT && dir->realdir) |
|
256 |
+ /*if (USE_DIRENT && dir->realdir) |
|
257 | 257 |
{ |
258 | 258 |
_zzip_closedir(dir->realdir); |
259 | 259 |
free(dir->realname); |
260 | 260 |
free(dir); |
261 | 261 |
return 0; |
262 |
- }else |
|
262 |
+ }else*/ |
|
263 | 263 |
{ |
264 | 264 |
zzip_dir_close(dir); |
265 | 265 |
return 0; |
... | ... |
@@ -16,6 +16,7 @@ |
16 | 16 |
|
17 | 17 |
#include "strc.h" |
18 | 18 |
|
19 |
+#include <sys/types.h> /* njh@bandsman.co.uk: for icc7.0 */ |
|
19 | 20 |
#include <string.h> |
20 | 21 |
#include <sys/stat.h> |
21 | 22 |
#include <errno.h> |
... | ... |
@@ -23,6 +24,7 @@ |
23 | 23 |
#include <ctype.h> |
24 | 24 |
|
25 | 25 |
#include <zzipformat.h> |
26 |
+//#include "__debug.h" |
|
26 | 27 |
|
27 | 28 |
#if 0 |
28 | 29 |
# if defined ZZIP_HAVE_IO_H |
... | ... |
@@ -168,6 +170,9 @@ zzip_file_open(ZZIP_DIR * dir, zzip_char_t* name, int o_mode) |
168 | 168 |
if (n) hdr_name = n + 1; |
169 | 169 |
} |
170 | 170 |
|
171 |
+ //HINT4("name='%s', compr=%d, size=%d\n", |
|
172 |
+ // hdr->d_name, hdr->d_compr, hdr->d_usize); |
|
173 |
+ |
|
171 | 174 |
if (!cmp(hdr_name, name)) |
172 | 175 |
{ |
173 | 176 |
switch (hdr->d_compr) |
... | ... |
@@ -218,11 +223,11 @@ zzip_file_open(ZZIP_DIR * dir, zzip_char_t* name, int o_mode) |
218 | 218 |
|
219 | 219 |
{ /* skip local header - should test tons of other info, |
220 | 220 |
* but trust that those are correct */ |
221 |
- int dataoff; |
|
221 |
+ zzip_ssize_t dataoff; |
|
222 | 222 |
struct zzip_file_header * p = (void*) fp->buf32k; |
223 | 223 |
|
224 | 224 |
dataoff = dir->io->read(dir->fd, (void*)p, sizeof(*p)); |
225 |
- if (dataoff < (int) sizeof(*p)) |
|
225 |
+ if (dataoff < (zzip_ssize_t)sizeof(*p)) |
|
226 | 226 |
{ err = ZZIP_DIR_READ; goto error; } |
227 | 227 |
if (! ZZIP_FILE_HEADER_CHECKMAGIC(p)) /* PK\3\4 */ |
228 | 228 |
{ err = ZZIP_CORRUPTED; goto error; } |
... | ... |
@@ -319,12 +324,12 @@ zzip_close(ZZIP_FILE* fp) |
319 | 319 |
* required just that but the latest zlib would work just fine with |
320 | 320 |
* a smaller buffer. |
321 | 321 |
*/ |
322 |
-int |
|
323 |
-zzip_file_read(ZZIP_FILE * fp, char * buf, int len) |
|
322 |
+zzip_ssize_t |
|
323 |
+zzip_file_read(ZZIP_FILE * fp, char * buf, zzip_size_t len) |
|
324 | 324 |
{ |
325 | 325 |
ZZIP_DIR * dir; |
326 |
- int l; |
|
327 |
- int rv; |
|
326 |
+ zzip_size_t l; |
|
327 |
+ zzip_ssize_t rv; |
|
328 | 328 |
|
329 | 329 |
if (! fp || ! fp->dir) return 0; |
330 | 330 |
|
... | ... |
@@ -354,14 +359,15 @@ zzip_file_read(ZZIP_FILE * fp, char * buf, int len) |
354 | 354 |
|
355 | 355 |
do { |
356 | 356 |
int err; |
357 |
- int startlen; |
|
357 |
+ zzip_size_t startlen; |
|
358 | 358 |
|
359 | 359 |
if (fp->crestlen > 0 && fp->d_stream.avail_in == 0) |
360 | 360 |
{ |
361 |
- int cl = fp->crestlen > ZZIP_32K ? ZZIP_32K : fp->crestlen; |
|
362 |
- /* int cl = fp->crestlen > 128? 128: fp->crestlen; */ |
|
361 |
+ zzip_size_t cl = ( fp->crestlen < ZZIP_32K ? |
|
362 |
+ fp->crestlen : ZZIP_32K ); |
|
363 |
+ /* zzip_size_t cl = fp->crestlen > 128 ? 128 : fp->crestlen; */ |
|
363 | 364 |
|
364 |
- int i = fp->io->read(dir->fd, fp->buf32k, cl); |
|
365 |
+ zzip_ssize_t i = fp->io->read(dir->fd, fp->buf32k, cl); |
|
365 | 366 |
if (i <= 0) |
366 | 367 |
{ |
367 | 368 |
dir->errcode = ZZIP_DIR_READ; /* or ZZIP_DIR_READ_EOF ? */ |
... | ... |
@@ -408,14 +414,14 @@ zzip_file_read(ZZIP_FILE * fp, char * buf, int len) |
408 | 408 |
* perform a normal => read(2)-call, otherwise => zzip_file_read is called |
409 | 409 |
* to decompress the data stream and any error is mapped to => errno(3). |
410 | 410 |
*/ |
411 |
-int |
|
412 |
-zzip_read(ZZIP_FILE * fp, char * buf, int len) |
|
411 |
+zzip_ssize_t |
|
412 |
+zzip_read(ZZIP_FILE * fp, char * buf, zzip_size_t len) |
|
413 | 413 |
{ |
414 | 414 |
if (! fp) return 0; |
415 | 415 |
if (! fp->dir) |
416 | 416 |
{ return fp->io->read(fp->fd, buf, len); } /* stat fd */ |
417 | 417 |
else |
418 |
- { register int v; |
|
418 |
+ { register zzip_ssize_t v; |
|
419 | 419 |
v = zzip_file_read(fp, buf, len); |
420 | 420 |
if (v == -1) { errno = zzip_errno(fp->dir->errcode); } |
421 | 421 |
return v; |
... | ... |
@@ -424,8 +430,8 @@ zzip_read(ZZIP_FILE * fp, char * buf, int len) |
424 | 424 |
|
425 | 425 |
/** => zzip_read |
426 | 426 |
*/ |
427 |
-int |
|
428 |
-zzip_fread(void *ptr, int size, int nmemb, ZZIP_FILE *file) |
|
427 |
+zzip_size_t |
|
428 |
+zzip_fread(void *ptr, zzip_size_t size, zzip_size_t nmemb, ZZIP_FILE *file) |
|
429 | 429 |
{ |
430 | 430 |
if (! size) size=1; |
431 | 431 |
return zzip_read (file, ptr, size*nmemb)/size; |
... | ... |
@@ -710,7 +716,7 @@ zzip_open_shared_io (ZZIP_FILE* stream, |
710 | 710 |
/* see if we can share the same zip directory */ |
711 | 711 |
if (stream && stream->dir && stream->dir->realname) |
712 | 712 |
{ |
713 |
- int len = strlen (stream->dir->realname); |
|
713 |
+ zzip_size_t len = strlen (stream->dir->realname); |
|
714 | 714 |
if (! memcmp (filename, stream->dir->realname, len) && |
715 | 715 |
filename[len] == '/' && filename[len+1]) |
716 | 716 |
{ |
... | ... |
@@ -860,10 +866,10 @@ zzip_rewind(ZZIP_FILE *fp) |
860 | 860 |
* how gzio implements it, so I'm not sure there is a better way |
861 | 861 |
* without using the internals of the algorithm. |
862 | 862 |
*/ |
863 |
-int |
|
864 |
-zzip_seek(ZZIP_FILE * fp, int offset, int whence) |
|
863 |
+zzip_off_t |
|
864 |
+zzip_seek(ZZIP_FILE * fp, zzip_off_t offset, int whence) |
|
865 | 865 |
{ |
866 |
- int cur_pos, rel_ofs, read_size, ofs; |
|
866 |
+ zzip_off_t cur_pos, rel_ofs, read_size, ofs; |
|
867 | 867 |
ZZIP_DIR *dir; |
868 | 868 |
|
869 | 869 |
if (! fp) |
... | ... |
@@ -871,7 +877,7 @@ zzip_seek(ZZIP_FILE * fp, int offset, int whence) |
871 | 871 |
|
872 | 872 |
if (! fp->dir) |
873 | 873 |
{ /* stat fd */ |
874 |
- return fp->io->seeks(fp->fd,offset,whence); |
|
874 |
+ return fp->io->seeks(fp->fd, offset, whence); |
|
875 | 875 |
} |
876 | 876 |
|
877 | 877 |
cur_pos = zzip_tell(fp); |
... | ... |
@@ -910,7 +916,7 @@ zzip_seek(ZZIP_FILE * fp, int offset, int whence) |
910 | 910 |
if (read_size < 0) /* bad offset, before beginning of file */ |
911 | 911 |
return -1; |
912 | 912 |
|
913 |
- if (read_size + cur_pos > fp->usize) /* bad offset, past EOF */ |
|
913 |
+ if (read_size + cur_pos > (zzip_off_t)fp->usize) /* bad offset, past EOF */ |
|
914 | 914 |
return -1; |
915 | 915 |
|
916 | 916 |
if (read_size == 0) /* nothing to read */ |
... | ... |
@@ -949,10 +955,10 @@ zzip_seek(ZZIP_FILE * fp, int offset, int whence) |
949 | 949 |
|
950 | 950 |
while (read_size > 0) |
951 | 951 |
{ |
952 |
- int size = ZZIP_32K; |
|
953 |
- if (read_size < ZZIP_32K) size = (int)read_size; |
|
952 |
+ zzip_off_t size = ZZIP_32K; |
|
953 |
+ if (read_size < size/*32K*/) size = read_size; |
|
954 | 954 |
|
955 |
- size = zzip_file_read(fp, buf, size); |
|
955 |
+ size = zzip_file_read(fp, buf, (zzip_size_t)size); |
|
956 | 956 |
if (size <= 0) { free(buf); return -1; } |
957 | 957 |
|
958 | 958 |
read_size -= size; |
... | ... |
@@ -975,7 +981,7 @@ zzip_seek(ZZIP_FILE * fp, int offset, int whence) |
975 | 975 |
* calculated from the amount of data left and the total uncompressed |
976 | 976 |
* size; |
977 | 977 |
*/ |
978 |
-int |
|
978 |
+zzip_off_t |
|
979 | 979 |
zzip_tell(ZZIP_FILE * fp) |
980 | 980 |
{ |
981 | 981 |
if (! fp) |
... | ... |
@@ -65,10 +65,10 @@ struct zzip_file |
65 | 65 |
struct zzip_dir* dir; |
66 | 66 |
int fd; |
67 | 67 |
int method; |
68 |
- int restlen; |
|
69 |
- int crestlen; |
|
70 |
- int usize; |
|
71 |
- int csize; |
|
68 |
+ zzip_size_t restlen; |
|
69 |
+ zzip_size_t crestlen; |
|
70 |
+ zzip_size_t usize; |
|
71 |
+ zzip_size_t csize; |
|
72 | 72 |
/* added dataoffset member - data offset from start of zipfile*/ |
73 | 73 |
zzip_off_t dataoffset; |
74 | 74 |
char* buf32k; |
... | ... |
@@ -26,9 +26,11 @@ |
26 | 26 |
|
27 | 27 |
//#include "__mmap.h" |
28 | 28 |
//#include "__debug.h" |
29 |
-// |
|
30 | 29 |
|
31 |
-#include "others.h" |
|
30 |
+#define __sizeof(X) ((zzip_ssize_t)(sizeof(X))) |
|
31 |
+ |
|
32 |
+/* per default, we use a little hack to correct bad z_rootseek parts */ |
|
33 |
+#define ZZIP_CORRECT_ROOTSEEK 1 |
|
32 | 34 |
|
33 | 35 |
/* ------------------------- fetch helpers --------------------------------- */ |
34 | 36 |
|
... | ... |
@@ -42,7 +44,7 @@ uint32_t __zzip_get32(unsigned char * s) |
42 | 42 |
| ((uint32_t)s[1] << 8) | (uint32_t)s[0]; |
43 | 43 |
} |
44 | 44 |
|
45 |
-/** => __zzip_get16 |
|
45 |
+/** => __zzip_get32 |
|
46 | 46 |
* This function does the same for a 16 bit value. |
47 | 47 |
*/ |
48 | 48 |
uint16_t __zzip_get16(unsigned char * s) |
... | ... |
@@ -50,6 +52,88 @@ uint16_t __zzip_get16(unsigned char * s) |
50 | 50 |
return ((uint16_t)s[1] << 8) | (uint16_t)s[0]; |
51 | 51 |
} |
52 | 52 |
|
53 |
+/* --------------------------- internals -------------------------------- */ |
|
54 |
+/* internal functions of zziplib, avoid at all cost, changes w/o warning. |
|
55 |
+ * we do export them for debugging purpose and special external tools |
|
56 |
+ * which know what they do and which can adapt from version to version |
|
57 |
+ */ |
|
58 |
+ |
|
59 |
+int __zzip_find_disk_trailer( int fd, zzip_off_t filesize, |
|
60 |
+ struct zzip_disk_trailer * trailer, |
|
61 |
+ zzip_plugin_io_t io); |
|
62 |
+int __zzip_parse_root_directory( int fd, |
|
63 |
+ struct zzip_disk_trailer * trailer, |
|
64 |
+ struct zzip_dir_hdr ** hdr_return, |
|
65 |
+ zzip_plugin_io_t io); |
|
66 |
+ |
|
67 |
+_zzip_inline char* __zzip_aligned4(char* p); |
|
68 |
+ |
|
69 |
+/* ------------------------ harden routines ------------------------------ */ |
|
70 |
+ |
|
71 |
+#ifdef ZZIP_HARDEN |
|
72 |
+/* |
|
73 |
+ * check for inconsistent values in trailer and prefer lower seek value |
|
74 |
+ * - we fix values assuming the root directory was written at the end |
|
75 |
+ * and it is just before the zip trailer. Therefore, ... |
|
76 |
+ */ |
|
77 |
+_zzip_inline static void __fixup_rootseek( |
|
78 |
+ zzip_off_t offset_of_trailer, |
|
79 |
+ struct zzip_disk_trailer* trailer) |
|
80 |
+{ |
|
81 |
+ if ( (zzip_off_t) ZZIP_GET32(trailer->z_rootseek) > |
|
82 |
+ offset_of_trailer - (zzip_off_t) ZZIP_GET32(trailer->z_rootsize) && |
|
83 |
+ offset_of_trailer > (zzip_off_t) ZZIP_GET32(trailer->z_rootsize)) |
|
84 |
+ { |
|
85 |
+ register zzip_off_t offset; |
|
86 |
+ offset = offset_of_trailer - ZZIP_GET32(trailer->z_rootsize); |
|
87 |
+ trailer->z_rootseek[0] = offset & 0xff; |
|
88 |
+ trailer->z_rootseek[1] = offset >> 8 & 0xff; |
|
89 |
+ trailer->z_rootseek[2] = offset >> 16 & 0xff; |
|
90 |
+ trailer->z_rootseek[3] = offset >> 24 & 0xff; |
|
91 |
+ //HINT2("new rootseek=%li", |
|
92 |
+ // (long) ZZIP_GET32(trailer->z_rootseek)); |
|
93 |
+ } |
|
94 |
+} |
|
95 |
+#define __correct_rootseek(A,B,C) |
|
96 |
+ |
|
97 |
+#elif defined ZZIP_CORRECT_ROOTSEEK |
|
98 |
+/* store the seekvalue of the trailer into the "z_magic" field and with |
|
99 |
+ * a 64bit off_t we overwrite z_disk/z_finaldisk as well. If you change |
|
100 |
+ * anything in zziplib or dump the trailer structure then watch out that |
|
101 |
+ * these are still unused, so that this code may still (ab)use those. */ |
|
102 |
+#define __fixup_rootseek(_offset_of_trailer, _trailer) \ |
|
103 |
+ *(zzip_off_t*)_trailer = _offset_of_trailer; |
|
104 |
+#define __correct_rootseek( _u_rootseek, _u_rootsize, _trailer) \ |
|
105 |
+ if (_u_rootseek > *(zzip_off_t*)_trailer - _u_rootsize) \ |
|
106 |
+ _u_rootseek = *(zzip_off_t*)_trailer - _u_rootsize; |
|
107 |
+#else |
|
108 |
+#define __fixup_rootseek(A,B) |
|
109 |
+#define __correct_rootseek(A,B,C) |
|
110 |
+#endif |
|
111 |
+ |
|
112 |
+ |
|
113 |
+#ifdef DEBUG |
|
114 |
+_zzip_inline static void __debug_dir_hdr (struct zzip_dir_hdr* hdr) |
|
115 |
+{ |
|
116 |
+ if (sizeof(struct zzip_dir_hdr) > sizeof(struct zzip_root_dirent)) |
|
117 |
+ { WARN1("internal sizeof-mismatch may break wreakage"); } |
|
118 |
+ /* the internal directory structure is never bigger than the |
|
119 |
+ * external zip central directory space had been beforehand |
|
120 |
+ * (as long as the following assertion holds...) |
|
121 |
+ */ |
|
122 |
+ |
|
123 |
+ //if (((unsigned)hdr)&3) |
|
124 |
+ //{ NOTE1("this machine's malloc(3) returns sth. not u32-aligned"); } |
|
125 |
+ /* we assume that if this machine's malloc has returned a non-aligned |
|
126 |
+ * memory block, then it is actually safe to access misaligned data, and |
|
127 |
+ * since it does only affect the first hdr it should not even bring about |
|
128 |
+ * too much of that cpu's speed penalty |
|
129 |
+ */ |
|
130 |
+} |
|
131 |
+#else |
|
132 |
+#define __debug_dir_hdr(X) |
|
133 |
+#endif |
|
134 |
+ |
|
53 | 135 |
/* -------------------------- low-level interface -------------------------- */ |
54 | 136 |
|
55 | 137 |
#if defined BUFSIZ |
... | ... |
@@ -73,11 +157,13 @@ __zzip_find_disk_trailer(int fd, zzip_off_t filesize, |
73 | 73 |
struct zzip_disk_trailer * trailer, |
74 | 74 |
zzip_plugin_io_t io) |
75 | 75 |
{ |
76 |
+/* |
|
76 | 77 |
#ifdef DEBUG |
77 |
-#define return(val) { e=val; goto cleanup; } |
|
78 |
+#define return(val) { e=val; HINT2("%s", zzip_strerror(e)); goto cleanup; } |
|
78 | 79 |
#else |
80 |
+*/ |
|
79 | 81 |
#define return(val) { e=val; goto cleanup; } |
80 |
-#endif |
|
82 |
+//#endif |
|
81 | 83 |
register int e; |
82 | 84 |
|
83 | 85 |
#ifndef _LOWSTK |
... | ... |
@@ -87,13 +173,13 @@ __zzip_find_disk_trailer(int fd, zzip_off_t filesize, |
87 | 87 |
char* buf = malloc(2*ZZIP_BUFSIZ); |
88 | 88 |
#endif |
89 | 89 |
zzip_off_t offset = 0; |
90 |
- size_t maplen = 0; |
|
90 |
+ zzip_off_t maplen = 0; /* mmap(),read(),getpagesize() use size_t !! */ |
|
91 | 91 |
char* fd_map = 0; |
92 | 92 |
|
93 | 93 |
if (!trailer) |
94 | 94 |
{ return(EINVAL); } |
95 | 95 |
|
96 |
- if (filesize < sizeof(struct zzip_disk_trailer)) |
|
96 |
+ if (filesize < __sizeof(struct zzip_disk_trailer)) |
|
97 | 97 |
{ return(ZZIP_DIR_TOO_SHORT); } |
98 | 98 |
|
99 | 99 |
if (!buf) |
... | ... |
@@ -102,8 +188,7 @@ __zzip_find_disk_trailer(int fd, zzip_off_t filesize, |
102 | 102 |
offset = filesize; /* a.k.a. old offset */ |
103 | 103 |
while(1) /* outer loop */ |
104 | 104 |
{ |
105 |
- register unsigned char* p; |
|
106 |
- register unsigned char* s; |
|
105 |
+ register unsigned char* mapped; |
|
107 | 106 |
|
108 | 107 |
if (offset <= 0) { return(ZZIP_DIR_EDH_MISSING); } |
109 | 108 |
|
... | ... |
@@ -111,6 +196,34 @@ __zzip_find_disk_trailer(int fd, zzip_off_t filesize, |
111 | 111 |
if (filesize-offset > 64*1024) |
112 | 112 |
{ return(ZZIP_DIR_EDH_MISSING); } |
113 | 113 |
|
114 |
+ /* the new offset shall overlap with the area after the old offset! */ |
|
115 |
+ /*if (USE_MMAP && io->use_mmap) |
|
116 |
+ { |
|
117 |
+ zzip_off_t mapoff = offset; |
|
118 |
+ { |
|
119 |
+ zzip_off_t pagesize = _zzip_getpagesize (io->use_mmap); |
|
120 |
+ if (pagesize < ZZIP_BUFSIZ) goto non_mmap; |
|
121 |
+ if (mapoff == filesize && filesize > pagesize) |
|
122 |
+ mapoff -= pagesize; |
|
123 |
+ if (mapoff < pagesize) { |
|
124 |
+ maplen = mapoff + pagesize; mapoff = 0; |
|
125 |
+ } else { |
|
126 |
+ mapoff -= pagesize; maplen = 2*pagesize; |
|
127 |
+ if (mapoff & (pagesize-1)) { |
|
128 |
+ pagesize -= mapoff & (pagesize-1); |
|
129 |
+ mapoff += pagesize; |
|
130 |
+ maplen -= pagesize; |
|
131 |
+ } |
|
132 |
+ } |
|
133 |
+ if (mapoff + maplen > filesize) maplen = filesize - mapoff; |
|
134 |
+ } |
|
135 |
+ |
|
136 |
+ fd_map = _zzip_mmap(io->use_mmap, fd, mapoff, (zzip_size_t)maplen); |
|
137 |
+ if (fd_map == MAP_FAILED) goto non_mmap; |
|
138 |
+ mapped = fd_map; offset = mapoff; |
|
139 |
+ HINT3("mapped *%p len=%li", fd_map, (long) maplen); |
|
140 |
+ } else */ { |
|
141 |
+ non_mmap: |
|
114 | 142 |
fd_map = 0; /* have no mmap */ |
115 | 143 |
{ |
116 | 144 |
zzip_off_t pagesize = ZZIP_BUFSIZ; |
... | ... |
@@ -131,50 +244,52 @@ __zzip_find_disk_trailer(int fd, zzip_off_t filesize, |
131 | 131 |
|
132 | 132 |
if (io->seeks(fd, offset, SEEK_SET) < 0) |
133 | 133 |
{ return(ZZIP_DIR_SEEK); } |
134 |
- if (io->read(fd, buf, maplen) < (long)maplen) |
|
134 |
+ if (io->read(fd, buf, (zzip_size_t)maplen) < (zzip_ssize_t)maplen) |
|
135 | 135 |
{ return(ZZIP_DIR_READ); } |
136 |
- p = buf; /* success */ |
|
137 |
- |
|
136 |
+ mapped = buf; /* success */ |
|
137 |
+ //HINT5("offs=$%lx len=%li filesize=%li pagesize=%i", |
|
138 |
+ //(long)offset, (long)maplen, (long)filesize, ZZIP_BUFSIZ); |
|
139 |
+ } |
|
138 | 140 |
|
139 |
- /* now, check for the trailer-magic, hopefully near the end of file */ |
|
140 |
- for (s = p + maplen-1; (s >= p); s--) |
|
141 |
- { |
|
142 |
- if (*s == 'P' |
|
143 |
- && p+maplen-1-s > sizeof(*trailer)-2 |
|
144 |
- && ZZIP_DISK_TRAILER_CHECKMAGIC(s)) |
|
145 |
- { |
|
146 |
- /* if the file-comment is not present, it happens |
|
147 |
- that the z_comment field often isn't either */ |
|
148 |
- if (p+maplen-1-s > sizeof(*trailer)) |
|
149 |
- { memcpy (trailer, s, sizeof(*trailer)); } |
|
150 |
- else |
|
151 |
- { |
|
152 |
- memcpy (trailer, s, sizeof(*trailer)-2); |
|
153 |
- trailer->z_comment[0] = 0; trailer->z_comment[1] = 0; |
|
154 |
- } |
|
155 |
- |
|
156 |
- /* check for inconsistent values in trailer |
|
157 |
- (prefer lower seek value) */ |
|
158 |
- if ( ZZIP_GET32(trailer->z_rootseek) > |
|
159 |
- offset+s-p - ZZIP_GET32(trailer->z_rootsize) |
|
160 |
- && offset+s-p > ZZIP_GET32(trailer->z_rootsize)) |
|
161 |
- { |
|
162 |
- cli_errmsg("zziplib: rootseek or length information incorrect\n"); |
|
163 |
- offset = offset+s-p - ZZIP_GET32(trailer->z_rootsize); |
|
164 |
- trailer->z_rootseek[0] = offset & 0xff; |
|
165 |
- trailer->z_rootseek[1] = offset >> 8 & 0xff; |
|
166 |
- trailer->z_rootseek[2] = offset >> 16 & 0xff; |
|
167 |
- trailer->z_rootseek[3] = offset >> 24 & 0xff; |
|
168 |
- /* HINT2("new rootseek=%li", |
|
169 |
- (long) ZZIP_GET32(trailer->z_rootseek)); */ |
|
170 |
- } |
|
171 |
- return(0); |
|
172 |
- } |
|
141 |
+ {/* now, check for the trailer-magic, hopefully near the end of file */ |
|
142 |
+ register unsigned char* end = mapped + maplen; |
|
143 |
+ register unsigned char* tail; |
|
144 |
+ for (tail = end-1; (tail >= mapped); tail--) |
|
145 |
+ { |
|
146 |
+ if ((*tail == 'P') && /* quick pre-check for trailer magic */ |
|
147 |
+ end-tail >= __sizeof(*trailer)-2 && |
|
148 |
+ ZZIP_DISK_TRAILER_CHECKMAGIC(tail)) |
|
149 |
+ { |
|
150 |
+ /* if the file-comment is not present, it happens |
|
151 |
+ that the z_comment field often isn't either */ |
|
152 |
+ if (end-tail >= __sizeof(*trailer)) |
|
153 |
+ { |
|
154 |
+ memcpy (trailer, tail, sizeof(*trailer)); |
|
155 |
+ }else{ |
|
156 |
+ memcpy (trailer, tail, sizeof(*trailer)-2); |
|
157 |
+ trailer->z_comment[0] = 0; |
|
158 |
+ trailer->z_comment[1] = 0; |
|
159 |
+ } |
|
160 |
+ |
|
161 |
+ __fixup_rootseek (offset + tail-mapped, trailer); |
|
162 |
+ { return(0); } |
|
163 |
+ } |
|
164 |
+ } |
|
173 | 165 |
} |
174 | 166 |
|
167 |
+ /*if (USE_MMAP && fd_map) |
|
168 |
+ { |
|
169 |
+ HINT3("unmap *%p len=%li", fd_map, (long) maplen); |
|
170 |
+ _zzip_munmap(io->use_mmap, fd_map, (zzip_size_t)maplen); fd_map = 0; |
|
171 |
+ }*/ |
|
175 | 172 |
} /*outer loop*/ |
176 | 173 |
|
177 | 174 |
cleanup: |
175 |
+ /*if (USE_MMAP && fd_map) |
|
176 |
+ { |
|
177 |
+ HINT3("unmap *%p len=%li", fd_map, (long) maplen); |
|
178 |
+ _zzip_munmap(io->use_mmap, fd_map, (zzip_size_t)maplen); |
|
179 |
+ }*/ |
|
178 | 180 |
# ifdef _LOWSTK |
179 | 181 |
free(buf); |
180 | 182 |
# endif |
... | ... |
@@ -192,8 +307,8 @@ __zzip_find_disk_trailer(int fd, zzip_off_t filesize, |
192 | 192 |
_zzip_inline char* __zzip_aligned4(char* p) |
193 | 193 |
{ |
194 | 194 |
#define aligned4 __zzip_aligned4 |
195 |
- p += ((long)p)&1; |
|
196 |
- p += ((long)p)&2; |
|
195 |
+ p += ((long)p)&1; /* warnings about truncation of a "pointer" */ |
|
196 |
+ p += ((long)p)&2; /* to a "long int" may be safely ignored :) */ |
|
197 | 197 |
return p; |
198 | 198 |
} |
199 | 199 |
|
... | ... |
@@ -214,34 +329,33 @@ __zzip_parse_root_directory(int fd, |
214 | 214 |
struct zzip_dir_hdr * hdr0; |
215 | 215 |
uint16_t * p_reclen = 0; |
216 | 216 |
short entries; |
217 |
- long offset, fallback_offset; |
|
218 |
- char* fd_map = 0; |
|
217 |
+ long offset; /* offset from start of root directory */ |
|
218 |
+ char* fd_map = 0; |
|
219 | 219 |
int32_t fd_gap = 0; |
220 | 220 |
uint16_t u_entries = ZZIP_GET16(trailer->z_entries); |
221 | 221 |
uint32_t u_rootsize = ZZIP_GET32(trailer->z_rootsize); |
222 | 222 |
uint32_t u_rootseek = ZZIP_GET32(trailer->z_rootseek); |
223 |
+ __correct_rootseek (u_rootseek, u_rootsize, trailer); |
|
223 | 224 |
|
224 | 225 |
hdr0 = (struct zzip_dir_hdr*) malloc(u_rootsize); |
225 | 226 |
if (!hdr0) |
226 | 227 |
return ZZIP_DIRSIZE; |
227 |
- hdr = hdr0; |
|
228 |
+ hdr = hdr0; __debug_dir_hdr (hdr); |
|
228 | 229 |
|
229 |
-# ifdef DEBUG |
|
230 |
- if (sizeof(struct zzip_dir_hdr) > sizeof(struct zzip_root_dirent)) |
|
231 |
- { WARN1("internal sizeof-mismatch may break wreakage"); } |
|
232 |
- /* the internal directory structure is never bigger than the |
|
233 |
- * external zip central directory space had been beforehand |
|
234 |
- * (as long as the following assertion holds...) |
|
235 |
- */ |
|
236 |
- |
|
237 |
- if (((unsigned)hdr0)&3) |
|
238 |
- { NOTE1("this machine's malloc(3) returns sth. not u32-aligned"); } |
|
239 |
- /* we assume that if this machine's malloc has returned a non-aligned |
|
240 |
- * memory block, then it is actually safe to access misaligned data, and |
|
241 |
- * since it does only affect the first hdr it should not even bring about |
|
242 |
- * too much of that cpu's speed penalty |
|
243 |
- */ |
|
244 |
-# endif |
|
230 |
+ /*if (USE_MMAP && io->use_mmap) |
|
231 |
+ { |
|
232 |
+ fd_gap = u_rootseek & (_zzip_getpagesize(io->use_mmap)-1) ; |
|
233 |
+ HINT4(" mapseek=0x%x, maplen=%d, fd_gap=%d", |
|
234 |
+ u_rootseek-fd_gap, u_rootsize+fd_gap, fd_gap); |
|
235 |
+ fd_map = _zzip_mmap(io->use_mmap, |
|
236 |
+ fd, u_rootseek-fd_gap, u_rootsize+fd_gap); |
|
237 |
+ if (fd_map == MAP_FAILED) { |
|
238 |
+ NOTE2("map failed: %s",strerror(errno)); |
|
239 |
+ fd_map=0; |
|
240 |
+ }else{ |
|
241 |
+ HINT3("mapped *%p len=%i", fd_map, u_rootsize+fd_gap); |
|
242 |
+ } |
|
243 |
+ }*/ |
|
245 | 244 |
|
246 | 245 |
for (entries=u_entries, offset=0; entries > 0; entries--) |
247 | 246 |
{ |
... | ... |
@@ -254,55 +368,14 @@ __zzip_parse_root_directory(int fd, |
254 | 254 |
{ |
255 | 255 |
if (io->seeks(fd, u_rootseek+offset, SEEK_SET) < 0) |
256 | 256 |
return ZZIP_DIR_SEEK; |
257 |
- if (io->read(fd, &dirent, sizeof(dirent)) < sizeof(dirent)) |
|
257 |
+ if (io->read(fd, &dirent, sizeof(dirent)) < __sizeof(dirent)) |
|
258 | 258 |
return ZZIP_DIR_READ; |
259 | 259 |
d = &dirent; |
260 | 260 |
} |
261 | 261 |
|
262 |
- /* another check for corrupted files */ |
|
263 |
- if (! ZZIP_ROOT_DIRENT_CHECKMAGIC(d)) |
|
264 |
- { |
|
265 |
- cli_errmsg("zziplib: directory entry incorrect: %lx - search %lx...\n", |
|
266 |
- offset+u_rootseek, fallback_offset+u_rootseek); |
|
267 |
- offset = fallback_offset; |
|
268 |
- while (offset < u_rootsize-sizeof(dirent)) |
|
269 |
- { |
|
270 |
- offset++; |
|
271 |
- if (fd_map) |
|
272 |
- { d = (void*)(fd_map+fd_gap+offset); } |
|
273 |
- else |
|
274 |
- { |
|
275 |
- if (io->seeks(fd, u_rootseek+offset, SEEK_SET) < 0) |
|
276 |
- return ZZIP_DIR_SEEK; |
|
277 |
- if (io->read(fd, &dirent, sizeof(dirent.z_magic)) |
|
278 |
- < sizeof(dirent.z_magic)) |
|
279 |
- return ZZIP_DIR_READ; |
|
280 |
- d = &dirent; |
|
281 |
- } |
|
282 |
- if (ZZIP_ROOT_DIRENT_CHECKMAGIC(d)) |
|
283 |
- { |
|
284 |
- /* HINT2("found directory entry magic at %lx", u_rootseek+offset); */ |
|
285 |
- break; |
|
286 |
- } |
|
287 |
- |
|
288 |
- } |
|
289 |
- if (ZZIP_ROOT_DIRENT_CHECKMAGIC(d)) |
|
290 |
- { |
|
291 |
- entries++; |
|
292 |
- continue; |
|
293 |
- } |
|
294 |
- |
|
295 |
- else |
|
296 |
- { |
|
297 |
- cli_errmsg("zziplib: directory entry not found\n", |
|
298 |
- offset+u_rootseek); |
|
299 |
- return ZZIP_CORRUPTED; |
|
300 |
- } |
|
301 |
- } |
|
302 |
- |
|
303 |
- |
|
304 |
- |
|
305 |
- |
|
262 |
+ if (offset+sizeof(*d) > u_rootsize) |
|
263 |
+ { /*FAIL2("%i's entry stretches beyond root directory", entries);*/ break;} |
|
264 |
+ |
|
306 | 265 |
# if 0 && defined DEBUG |
307 | 266 |
zzip_debug_xbuf ((unsigned char*) d, sizeof(*d) + 8); |
308 | 267 |
# endif |
... | ... |
@@ -310,8 +383,9 @@ __zzip_parse_root_directory(int fd, |
310 | 310 |
u_extras = ZZIP_GET16(d->z_extras); |
311 | 311 |
u_comment = ZZIP_GET16(d->z_comment); |
312 | 312 |
u_namlen = ZZIP_GET16(d->z_namlen); |
313 |
- |
|
314 |
- |
|
313 |
+ //HINT5("offset=0x%lx, size %ld, dirent *%p, hdr %p\n", |
|
314 |
+ // offset+u_rootseek, (long)u_rootsize, d, hdr); |
|
315 |
+ |
|
315 | 316 |
/* writes over the read buffer, Since the structure where data is |
316 | 317 |
copied is smaller than the data in buffer this can be done. |
317 | 318 |
It is important that the order of setting the fields is considered |
... | ... |
@@ -326,38 +400,51 @@ __zzip_parse_root_directory(int fd, |
326 | 326 |
hdr->d_compr = (uint8_t)ZZIP_GET16(d->z_compr); |
327 | 327 |
if (hdr->d_compr > 255) hdr->d_compr = 255; |
328 | 328 |
|
329 |
- if (fd_map) |
|
330 |
- { memcpy(hdr->d_name, fd_map+fd_gap+offset+sizeof(*d), u_namlen); } |
|
331 |
- else |
|
332 |
- { io->read(fd, hdr->d_name, u_namlen); } |
|
329 |
+ if (offset+sizeof(*d) + u_namlen > u_rootsize) |
|
330 |
+ { /*FAIL2("%i's name stretches beyond root directory", entries);*/ break;} |
|
331 |
+ |
|
332 |
+ if (fd_map) |
|
333 |
+ { memcpy(hdr->d_name, fd_map+fd_gap+offset+sizeof(*d), u_namlen); } |
|
334 |
+ else { io->read(fd, hdr->d_name, u_namlen); } |
|
333 | 335 |
hdr->d_name[u_namlen] = '\0'; |
334 | 336 |
hdr->d_namlen = u_namlen; |
335 | 337 |
|
336 | 338 |
/* update offset by the total length of this entry -> next entry */ |
337 |
- fallback_offset = offset + sizeof(*d) - 1; |
|
338 | 339 |
offset += sizeof(*d) + u_namlen + u_extras + u_comment; |
339 | 340 |
|
340 | 341 |
if (offset > (long)u_rootsize) |
341 |
- break; |
|
342 |
- |
|
342 |
+ { /*FAIL2("%i's end beyond root directory", entries);*/ entries--; break;} |
|
343 |
+ |
|
344 |
+ //HINT5("file %d { compr=%d crc32=$%x offset=%d", |
|
345 |
+ // entries, hdr->d_compr, hdr->d_crc32, hdr->d_off); |
|
346 |
+ //HINT5("csize=%d usize=%d namlen=%d extras=%d", |
|
347 |
+ // hdr->d_csize, hdr->d_usize, u_namlen, u_extras); |
|
348 |
+ //HINT5("comment=%d name='%s' %s <sizeof %d> } ", |
|
349 |
+ // u_comment, hdr->d_name, "",(int) sizeof(*d)); |
|
350 |
+ |
|
343 | 351 |
p_reclen = &hdr->d_reclen; |
344 | 352 |
|
345 | 353 |
{ register char* p = (char*) hdr; |
346 | 354 |
register char* q = aligned4 (p + sizeof(*hdr) + u_namlen + 1); |
347 |
- *p_reclen = q - p; |
|
355 |
+ *p_reclen = (uint16_t)(q - p); |
|
348 | 356 |
hdr = (struct zzip_dir_hdr*) q; |
349 | 357 |
} |
350 | 358 |
}/*for*/ |
351 | 359 |
|
352 |
- if (!p_reclen) |
|
353 |
- return 0; /* 0 (sane) entries in zip directory... */ |
|
354 |
- |
|
355 |
- *p_reclen = 0; /* mark end of list */ |
|
360 |
+ /*if (USE_MMAP && fd_map) |
|
361 |
+ { |
|
362 |
+ HINT3("unmap *%p len=%i", fd_map, u_rootsize+fd_gap); |
|
363 |
+ _zzip_munmap(io->use_mmap, fd_map, u_rootsize+fd_gap); |
|
364 |
+ }*/ |
|
356 | 365 |
|
357 |
- if (hdr_return) |
|
358 |
- *hdr_return = hdr0; |
|
366 |
+ if (p_reclen) |
|
367 |
+ { |
|
368 |
+ *p_reclen = 0; /* mark end of list */ |
|
359 | 369 |
|
360 |
- return 0; |
|
370 |
+ if (hdr_return) |
|
371 |
+ *hdr_return = hdr0; |
|
372 |
+ } /* else zero (sane) entries */ |
|
373 |
+ return (entries ? ZZIP_CORRUPTED : 0); |
|
361 | 374 |
} |
362 | 375 |
|
363 | 376 |
/* ------------------------- high-level interface ------------------------- */ |
... | ... |
@@ -507,13 +594,18 @@ __zzip_dir_parse (ZZIP_DIR* dir) |
507 | 507 |
* { rv = EINVAL; goto error; } |
508 | 508 |
*/ |
509 | 509 |
|
510 |
+ //HINT2("------------------ fd=%i", (int) dir->fd); |
|
510 | 511 |
if ((filesize = dir->io->filesize(dir->fd)) < 0) |
511 | 512 |
{ rv = ZZIP_DIR_STAT; goto error; } |
512 | 513 |
|
514 |
+ //HINT2("------------------ filesize=%ld", (long) filesize); |
|
513 | 515 |
if ((rv = __zzip_find_disk_trailer(dir->fd, filesize, &trailer, |
514 | 516 |
dir->io)) != 0) |
515 | 517 |
{ goto error; } |
516 | 518 |
|
519 |
+ //HINT5("directory = { entries= %d/%d, size= %d, seek= %d } ", |
|
520 |
+ // ZZIP_GET16(trailer.z_entries), ZZIP_GET16(trailer.z_finalentries), |
|
521 |
+ // ZZIP_GET32(trailer.z_rootsize), ZZIP_GET32(trailer.z_rootseek)); |
|
517 | 522 |
|
518 | 523 |
if ( (rv = __zzip_parse_root_directory(dir->fd, &trailer, &dir->hdr0, |
519 | 524 |
dir->io)) != 0) |
... | ... |
@@ -533,7 +625,7 @@ __zzip_try_open(zzip_char_t* filename, int filemode, |
533 | 533 |
{ |
534 | 534 |
auto char file[PATH_MAX]; |
535 | 535 |
int fd; |
536 |
- int len = strlen (filename); |
|
536 |
+ zzip_size_t len = strlen (filename); |
|
537 | 537 |
|
538 | 538 |
if (len+4 >= PATH_MAX) return -1; |
539 | 539 |
memcpy(file, filename, len+1); |
... | ... |
@@ -75,6 +75,9 @@ ZZIP_DIR* |
75 | 75 |
zzip_dir_fdopen_ext_io(int fd, zzip_error_t * errorcode_p, |
76 | 76 |
zzip_strings_t* ext, const zzip_plugin_io_t io); |
77 | 77 |
|
78 |
+ZZIP_DIR* /*depracated*/ |
|
79 |
+zzip_dir_alloc_ext_io (zzip_strings_t* ext, const zzip_plugin_io_t io); |
|
80 |
+ |
|
78 | 81 |
/* get 16/32 bits from little-endian zip-file to host byteorder */ |
79 | 82 |
uint32_t __zzip_get32(unsigned char * s); |
80 | 83 |
uint16_t __zzip_get16(unsigned char * s); |
... | ... |
@@ -3,12 +3,17 @@ |
3 | 3 |
* Guido Draheim <guidod@gmx.de> |
4 | 4 |
* Tomi Ollila <Tomi.Ollila@iki.fi> |
5 | 5 |
* |
6 |
- * Copyright (c) 1999,2000,2001,2002 Guido Draheim |
|
6 |
+ * Copyright (c) 1999,2000,2001,2002,2003 Guido Draheim |
|
7 | 7 |
* All rights reserved, |
8 | 8 |
* usage allowed under the restrictions of the |
9 | 9 |
* Lesser GNU General Public License |
10 | 10 |
* note the additional license information |
11 | 11 |
* that can be found in COPYING.ZZIP |
12 |
+ * |
|
13 |
+ * if you see "unknown symbol" errors, check first that `-I ..` is part of |
|
14 |
+ * your compiler options - a special hint to VC/IDE users who tend to make up |
|
15 |
+ * their own workspace files. All includes look like #include <zzip|*.h>, so |
|
16 |
+ * you need to add an include path to the dir containing (!!) the ./zzip/ dir |
|
12 | 17 |
*/ |
13 | 18 |
|
14 | 19 |
#ifndef _ZZIP_ZZIP_H /* zziplib.h */ |
... | ... |
@@ -17,6 +22,8 @@ |
17 | 17 |
#include <zzip-conf.h> |
18 | 18 |
|
19 | 19 |
#include <fcntl.h> |
20 |
+#include <stddef.h> /* size_t and friends */ |
|
21 |
+/* msvc6 has neither ssize_t (we assume "int") nor off_t (assume "long") */ |
|
20 | 22 |
|
21 | 23 |
#ifdef __cplusplus |
22 | 24 |
extern "C" { |
... | ... |
@@ -72,6 +79,8 @@ typedef enum |
72 | 72 |
typedef char _zzip_const * _zzip_const zzip_strings_t; |
73 | 73 |
typedef char _zzip_const zzip_char_t; |
74 | 74 |
typedef _zzip_off_t zzip_off_t; |
75 |
+typedef _zzip_size_t zzip_size_t; |
|
76 |
+typedef _zzip_ssize_t zzip_ssize_t; |
|
75 | 77 |
typedef struct zzip_dir ZZIP_DIR; |
76 | 78 |
typedef struct zzip_file ZZIP_FILE; |
77 | 79 |
typedef struct zzip_dirent ZZIP_DIRENT; |
... | ... |
@@ -87,7 +96,7 @@ struct zzip_dirent |
87 | 87 |
|
88 | 88 |
/* |
89 | 89 |
* Getting error strings |
90 |
- * zzip-err.c |
|
90 |
+ * zzip/err.c |
|
91 | 91 |
*/ |
92 | 92 |
_zzip_export /* error in _opendir : */ |
93 | 93 |
zzip_char_t* zzip_strerror(int errcode); |
... | ... |
@@ -100,7 +109,7 @@ int zzip_errno(int errcode); |
100 | 100 |
/* |
101 | 101 |
* Functions to grab information from ZZIP_DIR/ZZIP_FILE structure |
102 | 102 |
* (if ever needed) |
103 |
- * zzip-info.c |
|
103 |
+ * zzip/info.c |
|
104 | 104 |
*/ |
105 | 105 |
_zzip_export |
106 | 106 |
int zzip_error(ZZIP_DIR * dir); |
... | ... |
@@ -124,7 +133,7 @@ int zzip_realfd(ZZIP_FILE * fp); |
124 | 124 |
|
125 | 125 |
/* |
126 | 126 |
* zip handle management |
127 |
- * zzip-zip.c |
|
127 |
+ * zzip/zip.c |
|
128 | 128 |
*/ |
129 | 129 |
_zzip_export |
130 | 130 |
ZZIP_DIR * zzip_dir_alloc(zzip_strings_t* fileext); |
... | ... |
@@ -147,8 +156,8 @@ int zzip_dir_read(ZZIP_DIR * dir, ZZIP_DIRENT * dirent); |
147 | 147 |
|
148 | 148 |
/* |
149 | 149 |
* Scanning files in zip archive |
150 |
- * zzip-dir.c |
|
151 |
- * zzip-zip.c |
|
150 |
+ * zzip/dir.c |
|
151 |
+ * zzip/zip.c |
|
152 | 152 |
*/ |
153 | 153 |
_zzip_export |
154 | 154 |
ZZIP_DIR * zzip_opendir(zzip_char_t* filename); |
... | ... |
@@ -165,30 +174,33 @@ void zzip_seekdir(ZZIP_DIR * dir, zzip_off_t offset); |
165 | 165 |
|
166 | 166 |
/* |
167 | 167 |
* 'opening', 'closing' and reading invidual files in zip archive. |
168 |
- * zzip-file.c |
|
168 |
+ * zzip/file.c |
|
169 | 169 |
*/ |
170 |
- |
|
171 | 170 |
_zzip_export |
172 | 171 |
ZZIP_FILE * zzip_file_open(ZZIP_DIR * dir, zzip_char_t* name, int modes); |
173 | 172 |
_zzip_export |
174 | 173 |
int zzip_file_close(ZZIP_FILE * fp); |
175 | 174 |
_zzip_export |
176 |
-int zzip_file_read(ZZIP_FILE * fp, char* buf, int len); |
|
175 |
+zzip_ssize_t zzip_file_read(ZZIP_FILE * fp, char* buf, zzip_size_t len); |
|
177 | 176 |
|
178 | 177 |
_zzip_export |
179 | 178 |
ZZIP_FILE * zzip_open(zzip_char_t* name, int flags); |
180 | 179 |
_zzip_export |
181 | 180 |
int zzip_close(ZZIP_FILE * fp); |
182 | 181 |
_zzip_export |
183 |
-int zzip_read(ZZIP_FILE * fp, char * buf, int len); |
|
184 |
- |
|
182 |
+zzip_ssize_t zzip_read(ZZIP_FILE * fp, char * buf, zzip_size_t len); |
|
185 | 183 |
|
184 |
+/* |
|
185 |
+ * the stdc variant to open/read/close files. - Take note of the freopen() |
|
186 |
+ * call as it may reuse an existing preparsed copy of a zip central directory |
|
187 |
+ */ |
|
186 | 188 |
_zzip_export |
187 | 189 |
ZZIP_FILE* zzip_freopen(zzip_char_t* name, zzip_char_t* mode, ZZIP_FILE*); |
188 | 190 |
_zzip_export |
189 | 191 |
ZZIP_FILE* zzip_fopen(zzip_char_t* name, zzip_char_t* mode); |
190 | 192 |
_zzip_export |
191 |
-int zzip_fread(void *ptr, int size, int nmemb, ZZIP_FILE *file); |
|
193 |
+zzip_size_t zzip_fread(void *ptr, zzip_size_t size, zzip_size_t nmemb, |
|
194 |
+ ZZIP_FILE * file); |
|
192 | 195 |
_zzip_export |
193 | 196 |
int zzip_fclose(ZZIP_FILE * fp); |
194 | 197 |
|
... | ... |
@@ -198,21 +210,18 @@ int zzip_fclose(ZZIP_FILE * fp); |
198 | 198 |
_zzip_export |
199 | 199 |
int zzip_rewind(ZZIP_FILE *fp); |
200 | 200 |
_zzip_export |
201 |
-int zzip_seek(ZZIP_FILE * fp, int offset, int |
|
202 |
- whence); |
|
201 |
+zzip_off_t zzip_seek(ZZIP_FILE * fp, zzip_off_t offset, int whence); |
|
203 | 202 |
_zzip_export |
204 |
-int zzip_tell(ZZIP_FILE * fp); |
|
205 |
- |
|
203 |
+zzip_off_t zzip_tell(ZZIP_FILE * fp); |
|
206 | 204 |
|
207 | 205 |
/* |
208 | 206 |
* reading info of a single file |
209 |
- * zzip-stat.c |
|
207 |
+ * zzip/stat.c |
|
210 | 208 |
*/ |
211 | 209 |
_zzip_export |
212 | 210 |
int zzip_dir_stat(ZZIP_DIR * dir, zzip_char_t* name, |
213 | 211 |
ZZIP_STAT * zs, int flags); |
214 | 212 |
|
215 |
- |
|
216 | 213 |
#ifdef ZZIP_LARGEFILE_RENAME |
217 | 214 |
#define zzip_open_shared_io zzip_open_shared_io64 |
218 | 215 |
#define zzip_open_ext_io zzip_open_ext_io64 |
... | ... |
@@ -246,13 +255,73 @@ ZZIP_FILE * zzip_file_open_ext_io(ZZIP_DIR * dir, |
246 | 246 |
zzip_strings_t* ext, zzip_plugin_io_t io); |
247 | 247 |
|
248 | 248 |
_zzip_export |
249 |
-ZZIP_DIR * zzip_dir_open_ext_io(zzip_char_t* filename, |
|
249 |
+ZZIP_DIR * zzip_dir_open_ext_io(zzip_char_t* filename, |
|
250 | 250 |
zzip_error_t* errcode_p, |
251 | 251 |
zzip_strings_t* ext, zzip_plugin_io_t io); |
252 | 252 |
|
253 |
+#if defined _ZZIP_WRITE_SOURCE |
|
254 |
+/* ........................................................................ |
|
255 |
+ * write support is not yet implemented |
|
256 |
+ * zzip/write.c |
|
257 |
+ */ |
|
258 |
+#define ZZIP_NO_CREAT 1 |
|
259 |
+ |
|
260 |
+ZZIP_DIR* zzip_dir_creat_ext_io(zzip_char_t* name, int o_mode, |
|
261 |
+ zzip_strings_t* ext, zzip_plugin_io_t io); |
|
262 |
+ZZIP_DIR* zzip_dir_creat(zzip_char_t* name, int o_mode); |
|
263 |
+int zzip_file_mkdir(ZZIP_DIR* dir, zzip_char_t* name, int o_mode); |
|
264 |
+ZZIP_FILE* zzip_file_creat(ZZIP_DIR* dir, zzip_char_t* name, int o_mode); |
|
265 |
+zzip_ssize_t zzip_file_write(ZZIP_FILE* file, |
|
266 |
+ const void* ptr, zzip_size_t len); |
|
267 |
+ |
|
268 |
+ZZIP_DIR* zzip_createdir(zzip_char_t* name, int o_mode); |
|
269 |
+zzip_ssize_t zzip_write(ZZIP_FILE* file, const void* ptr, zzip_size_t len); |
|
270 |
+zzip_size_t zzip_fwrite(const void* ptr, zzip_size_t len, |
|
271 |
+ zzip_size_t multiply, ZZIP_FILE* file); |
|
272 |
+#ifndef zzip_savefile |
|
273 |
+#define zzip_savefile 0 |
|
274 |
+#define zzip_savefile_is_null |
|
275 |
+#endif |
|
276 |
+ |
|
277 |
+#ifdef _ZZIP_NO_INLINE |
|
278 |
+#define zzip_mkdir(_name_,_mode_) \ |
|
279 |
+ zzip_file_mkdir((zzip_savefile),(_name_),(_mode_)) |
|
280 |
+#define zzip_creat(_name_,_mode_) \ |
|
281 |
+ zzip_file_creat((zzip_savefile),(_name_),(_mode_)) |
|
282 |
+#define zzip_sync() \ |
|
283 |
+ { zzip_closedir((zzip_savefile)); (zzip_savefile) = 0; } |
|
284 |
+#define zzip_start(_name_,_mode_,_ext_) \ |
|
285 |
+ { if ((zzip_savefile)) zzip_closedir((zzip_savefile)); |
|
286 |
+ zzip_savefile = zzip_dir_creat(_name_, _mode_,_ext_); } |
|
287 |
+ |
|
288 |
+#else |
|
289 |
+ |
|
290 |
+_zzip_inline static int zzip_mkdir(zzip_char_t* name, int o_mode) |
|
291 |
+{ return zzip_file_mkdir(zzip_savefile, name, o_mode); } |
|
292 |
+_zzip_inline static ZZIP_FILE* zzip_creat(zzip_char_t* name, int o_mode) |
|
293 |
+{ return zzip_file_creat(zzip_savefile, name, o_mode); } |
|
294 |
+ |
|
295 |
+#ifndef zzip_savefile_is_null |
|
296 |
+_zzip_inline static void zzip_sync(void) |
|
297 |
+{ zzip_closedir(zzip_savefile); zzip_savefile = 0; } |
|
298 |
+_zzip_inline static void zzip_mkfifo(zzip_char_t* name, int o_mode) |
|
299 |
+{ if ((zzip_savefile)) zzip_closedir (zzip_savefile); |
|
300 |
+ zzip_savefile = zzip_createdir(_name_,_mode_); } |
|
301 |
+#else |
|
302 |
+_zzip_inline static void zzip_sync(void) {} |
|
303 |
+_zzip_inline static void zzip_mkfifo(zzip_char_t* name, int o_mode) {} |
|
304 |
+#endif |
|
305 |
+#endif /* _ZZIP_NO_INLINE */ |
|
306 |
+#endif /* _ZZIP_WRITE_SOURCE */ |
|
307 |
+ |
|
253 | 308 |
#ifdef __cplusplus |
254 | 309 |
}; |
255 | 310 |
#endif |
256 | 311 |
|
257 | 312 |
#endif /* _ZZIPLIB_H */ |
258 | 313 |
|
314 |
+/* |
|
315 |
+ * Local variables: |
|
316 |
+ * c-file-style: "stroustrup" |
|
317 |
+ * End: |
|
318 |
+ */ |