Debian Codegraph @

libs/zziplib/zziplib-src/zzip/zip.c

raw
1 /*
2 * Author:
3 * Guido Draheim <[email protected]>
4 * Tomi Ollila <[email protected]>
5 *
6 * Copyright (c) Guido Draheim, use under copyleft (LGPL,MPL)
7 */
8
9 #include <zzip/lib.h> /* archive handling */
10 #include <zzip/file.h>
11 #include <zzip/format.h>
12 #include <zzip/fetch.h>
13
14 #include <ctype.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <fcntl.h>
19 #ifdef ZZIP_HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22
23 #include <zzip/__mmap.h>
24 #include <zzip/__debug.h>
25
26 #define __sizeof(X) ((zzip_ssize_t)(sizeof(X)))
27
28 #ifndef ZZIP_EASY
29 /* per default, we use a little hack to correct bad z_rootseek parts */
30 #define ZZIP_CORRECT_ROOTSEEK 1
31 #endif
32
33 #if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || (__GNUC__ >= 4)
34 # ifdef DEBUG
35 # warning suppress a warning where the compiler should have optimized instead.
36 # endif
37 #define _255 254
38 #else
39 #define _255 255
40 #endif
41
42 #define ZZIP_DISK64_TRAILER 1
43
44 #ifdef ZZIP_DISK64_TRAILER
45 struct _disk_trailer
46 {
47 void *zz_tail;
48 void *zz_for_correct_rootseek; // ZZIP_CORRECT_ROOTSEEK
49 zzip_off64_t zz_entries;
50 zzip_off64_t zz_finalentries;
51 zzip_off64_t zz_rootseek;
52 zzip_off64_t zz_rootsize;
53 };
54 #define _disk_trailer_entries(__p) ((__p)->zz_entries)
55 #define _disk_trailer_localentries(__p) ((__p)->zz_entries)
56 #define _disk_trailer_finalentries(__p) ((__p)->zz_entries)
57 #define _disk_trailer_rootseek(__p) ((__p)->zz_rootseek)
58 #define _disk_trailer_rootsize(__p) ((__p)->zz_rootsize)
59 #define _disk_trailer_set_rootseek(__p,__v) ((__p)->rootseek = (__v))
60
61 #else
62 #define _disk_trailer zzip_disk_trailer
63 #define _disk_trailer_entries zzip_disk_trailer_entries
64 #define _disk_trailer_localentries zzip_disk_trailer_localentries
65 #define _disk_trailer_finalentries zzip_disk_trailer_finalentries
66 #define _disk_trailer_rootseek zzip_disk_trailer_rootseek
67 #define _disk_trailer_rootsize zzip_disk_trailer_rootsize
68 #define _disk_trailer_set_rootseek zzip_disk_trailer_set_rootseek
69 #define __zzip_fetch_disk_trailer __zzip_find_disk_trailer
70 #endif
71
72 /* --------------------------- internals -------------------------------- */
73
74 /* internal functions of zziplib, avoid at all cost, changes w/o warning.
75 * we do export them for debugging purpose and special external tools
76 * which know what they do and which can adapt from version to version
77 */
78
79 static int __zzip_fetch_disk_trailer(int fd, zzip_off_t filesize,
80 struct _disk_trailer *_zzip_restrict trailer,
81 zzip_plugin_io_t io);
82 static int __zzip_parse_root_directory(int fd,
83 struct _disk_trailer *trailer,
84 struct zzip_dir_hdr **hdr_return,
85 zzip_plugin_io_t io,
86 zzip_off_t filesize);
87
88 _zzip_inline static char *__zzip_aligned4(char *p);
89
90 /* ------------------------ harden routines ------------------------------ */
91
92 #ifdef ZZIP_HARDEN
93
94 /* internal.
95 * check for inconsistent values in trailer and prefer lower seek value
96 * - we fix values assuming the root directory was written at the end
97 * and it is just before the zip trailer. Therefore, ...
98 */
99 _zzip_inline static void
100 __fixup_rootseek(zzip_off_t offset_of_trailer, struct _disk_trailer *trailer)
101 {
102 if (_disk_trailer_rootseek(trailer) >
103 offset_of_trailer - _disk_trailer_rootsize(trailer) &&
104 offset_of_trailer > _disk_trailer_rootsize(trailer))
105 {
106 register zzip_off_t offset;
107 offset = offset_of_trailer - _disk_trailer_rootsize(trailer);
108 _disk_trailer_set_rootseek(trailer, offset);
109 HINT2("new rootseek=%li", (long) _disk_trailer_rootseek(trailer));
110 }
111 }
112
113 #define __correct_rootseek(A,B,C)
114
115 #elif defined ZZIP_CORRECT_ROOTSEEK
116
117 /* store the seekvalue of the trailer into the "z_magic" field and with
118 * a 64bit off_t we overwrite z_disk/z_finaldisk as well. If you change
119 * anything in zziplib or dump the trailer structure then watch out that
120 * these are still unused, so that this code may still (ab)use those. */
121 #define __fixup_rootseek(_offset_of_trailer, _trailer) \
122 *(zzip_off_t*)_trailer = _offset_of_trailer;
123 #define __correct_rootseek( _u_rootseek, _u_rootsize, _trailer) \
124 if (_u_rootseek > *(zzip_off_t*)_trailer - _u_rootsize) \
125 _u_rootseek = *(zzip_off_t*)_trailer - _u_rootsize;
126 #else
127 #define __fixup_rootseek(A,B)
128 #define __correct_rootseek(A,B,C)
129 #endif
130
131
132 #ifdef DEBUG
133 _zzip_inline static void
134 __debug_dir_hdr(struct zzip_dir_hdr *hdr)
135 {
136 if (sizeof(struct zzip_dir_hdr) > sizeof(struct zzip_disk_entry))
137 { WARN1("internal sizeof-mismatch may break wreakage"); }
138 /* the internal directory structure is never bigger than the
139 * external zip central directory space had been beforehand
140 * (as long as the following assertion holds...)
141 */
142
143 if (((zzip_off_t) hdr) & 3)
144 {
145 NOTE1("this machine's malloc(3) returns sth. not u32-aligned");
146 }
147 /* we assume that if this machine's malloc has returned a non-aligned
148 * memory block, then it is actually safe to access misaligned data, and
149 * since it does only affect the first hdr it should not even bring about
150 * too much of that cpu's speed penalty
151 */
152 }
153 #else
154 #define __debug_dir_hdr(X)
155 #endif
156
157 /* -------------------------- low-level interface -------------------------- */
158
159 #if defined BUFSIZ
160 #if BUFSIZ == 1024 || BUFSIZ == 512 || BUFSIZ == 256
161 #define ZZIP_BUFSIZ BUFSIZ
162 #endif
163 #endif
164
165 #ifndef ZZIP_BUFSIZ
166 #define ZZIP_BUFSIZ 512
167
168 /* #define ZZIP_BUFSIZ 64 / * for testing */
169 #endif
170
171 /** internal.
172 * This function is used by => zzip_file_open. It tries to find
173 * the zip's central directory info that is usually a few
174 * bytes off the end of the file.
175 */
176 static int
177 __zzip_fetch_disk_trailer(int fd, zzip_off_t filesize,
178 struct _disk_trailer *_zzip_restrict trailer,
179 zzip_plugin_io_t io)
180 {
181 #ifdef DEBUG
182 #define return(val) { e=val; HINT2("%s", zzip_strerror(e)); goto cleanup; }
183 #else
184 #define return(val) { e=val; goto cleanup; }
185 #endif
186 register int e;
187
188 #ifndef _LOWSTK
189 auto char buffer[2 * ZZIP_BUFSIZ];
190 char *buf = buffer;
191 #else
192 char *buf = malloc(2 * ZZIP_BUFSIZ);
193 #endif
194 zzip_off_t offset = 0;
195 zzip_ssize_t maplen = 0; /* mmap(),read(),getpagesize() use size_t !! */
196 char *fd_map = 0;
197
198 if (! trailer)
199 { return(EINVAL); }
200
201 if (filesize < __sizeof(struct zzip_disk_trailer))
202 { return(ZZIP_DIR_TOO_SHORT); }
203
204 if (! buf)
205 { return(ZZIP_OUTOFMEM); }
206
207 offset = filesize; /* a.k.a. old offset */
208 while (1) /* outer loop */
209 {
210 register unsigned char *mapped;
211
212 if (offset <= 0)
213 { return(ZZIP_DIR_EDH_MISSING); }
214
215 /* trailer cannot be farther away than 64K from fileend */
216 if (filesize - offset > 64 * 1024)
217 { return(ZZIP_DIR_EDH_MISSING); }
218
219 /* the new offset shall overlap with the area after the old offset! */
220 if (USE_MMAP && io->fd.sys)
221 {
222 zzip_off_t mapoff = offset;
223 {
224 zzip_ssize_t pagesize = _zzip_getpagesize(io->fd.sys);
225 if (pagesize < ZZIP_BUFSIZ)
226 goto non_mmap; /* an error? */
227 if (mapoff == filesize && filesize > pagesize)
228 mapoff -= pagesize;
229 if (mapoff < pagesize)
230 {
231 maplen = (zzip_ssize_t) mapoff + pagesize;
232 mapoff = 0;
233 } else
234 {
235 mapoff -= pagesize;
236 maplen = 2 * pagesize;
237 if ((zzip_ssize_t) mapoff & (pagesize - 1))
238 { /*only 1. run */
239 pagesize -= (zzip_ssize_t) mapoff & (pagesize - 1);
240 mapoff += pagesize;
241 maplen -= pagesize;
242 }
243 }
244 if (mapoff + maplen > filesize)
245 maplen = filesize - mapoff;
246 }
247
248 fd_map = _zzip_mmap(io->fd.sys, fd, mapoff, (zzip_size_t) maplen);
249 if (fd_map == MAP_FAILED)
250 goto non_mmap;
251 mapped = (unsigned char *) fd_map;
252 offset = mapoff; /* success */
253 HINT3("mapped *%p len=%li", fd_map, (long) maplen);
254 } else
255 {
256 non_mmap:
257 fd_map = 0; /* have no mmap */
258 {
259 zzip_off_t pagesize = ZZIP_BUFSIZ;
260 if (offset == filesize && filesize > pagesize)
261 offset -= pagesize;
262 if (offset < pagesize)
263 {
264 maplen = (zzip_ssize_t) offset + pagesize;
265 offset = 0;
266 } else
267 {
268 offset -= pagesize;
269 maplen = 2 * pagesize;
270 if ((zzip_ssize_t) offset & (pagesize - 1))
271 { /*on 1st run */
272 pagesize -= (zzip_ssize_t) offset & (pagesize - 1);
273 offset += pagesize;
274 maplen -= pagesize;
275 }
276 }
277 if (offset + maplen > filesize)
278 maplen = filesize - offset;
279 }
280
281 if (io->fd.seeks(fd, offset, SEEK_SET) < 0)
282 { return(ZZIP_DIR_SEEK); }
283 if (io->fd.read(fd, buf, (zzip_size_t) maplen) < maplen)
284 { return(ZZIP_DIR_READ); }
285 mapped = (unsigned char *) buf; /* success */
286 HINT5("offs=$%lx len=%li filesize=%li pagesize=%i",
287 (long) offset, (long) maplen, (long) filesize, ZZIP_BUFSIZ);
288 }
289
290 { /* now, check for the trailer-magic, hopefully near the end of file */
291 register unsigned char *end = mapped + maplen;
292 register unsigned char *tail;
293 for (tail = end - 1; (tail >= mapped); tail--)
294 {
295 if ((*tail == 'P') && /* quick pre-check for trailer magic */
296 end - tail >= __sizeof(struct zzip_disk_trailer) - 2 &&
297 zzip_disk_trailer_check_magic(tail))
298 {
299 # ifndef ZZIP_DISK64_TRAILER
300 /* if the file-comment is not present, it happens
301 that the z_comment field often isn't either */
302 if (end - tail >= __sizeof(*trailer))
303 {
304 memcpy(trailer, tail, sizeof(*trailer));
305 } else
306 {
307 memcpy(trailer, tail, sizeof(*trailer) - 2);
308 trailer->z_comment[0] = 0;
309 trailer->z_comment[1] = 0;
310 }
311 # else
312 struct zzip_disk_trailer *orig =
313 (struct zzip_disk_trailer *) tail;
314 trailer->zz_tail = tail;
315 trailer->zz_entries = zzip_disk_trailer_localentries(orig);
316 trailer->zz_finalentries =
317 zzip_disk_trailer_finalentries(orig);
318 trailer->zz_rootseek = zzip_disk_trailer_rootseek(orig);
319 trailer->zz_rootsize = zzip_disk_trailer_rootsize(orig);
320 # endif
321 if (trailer->zz_rootseek < 0 || trailer->zz_rootsize < 0)
322 return(ZZIP_CORRUPTED); // forged value
323
324 __fixup_rootseek(offset + tail - mapped, trailer);
325 /*
326 * "extract data from files archived in a single zip file."
327 * So the file offsets must be within the current ZIP archive!
328 */
329 if (trailer->zz_rootseek >= filesize || (trailer->zz_rootseek + trailer->zz_rootsize) >= filesize)
330 return(ZZIP_CORRUPTED);
331 { return(0); }
332 } else if ((*tail == 'P') &&
333 end - tail >=
334 __sizeof(struct zzip_disk64_trailer) - 2
335 && zzip_disk64_trailer_check_magic(tail))
336 {
337 # ifndef ZZIP_DISK64_TRAILER
338 return (ZZIP_DIR_LARGEFILE);
339 # else
340 struct zzip_disk64_trailer *orig =
341 (struct zzip_disk64_trailer *) tail;
342 trailer->zz_tail = tail;
343 trailer->zz_entries =
344 zzip_disk64_trailer_localentries(orig);
345 trailer->zz_finalentries =
346 zzip_disk64_trailer_finalentries(orig);
347 trailer->zz_rootseek = zzip_disk64_trailer_rootseek(orig);
348 trailer->zz_rootsize = zzip_disk64_trailer_rootsize(orig);
349 if (trailer->zz_rootseek < 0 || trailer->zz_rootsize < 0)
350 return(ZZIP_CORRUPTED); // forged value
351 /*
352 * "extract data from files archived in a single zip file."
353 * So the file offsets must be within the current ZIP archive!
354 */
355 if (trailer->zz_rootseek >= filesize || (trailer->zz_rootseek + trailer->zz_rootsize) >= filesize)
356 return(ZZIP_CORRUPTED);
357 { return(0); }
358 # endif
359 }
360 }
361 }
362
363 if (USE_MMAP && fd_map)
364 {
365 HINT3("unmap *%p len=%li", fd_map, (long) maplen);
366 _zzip_munmap(io->fd.sys, fd_map, (zzip_size_t) maplen);
367 fd_map = 0;
368 }
369 } /*outer loop */
370
371 cleanup:
372 if (USE_MMAP && fd_map)
373 {
374 HINT3("unmap *%p len=%li", fd_map, (long) maplen);
375 _zzip_munmap(io->fd.sys, fd_map, (zzip_size_t) maplen);
376 }
377 # ifdef _LOWSTK
378 free(buf);
379 # endif
380 # undef return
381 return e;
382 }
383
384 /*
385 * making pointer alignments to values that can be handled as structures
386 * is tricky. We assume here that an align(4) is sufficient even for
387 * 64 bit machines. Note that binary operations are not usually allowed
388 * to pointer types but we do need only the lower bits in this implementation,
389 * so we can just cast the value to a long value.
390 */
391 _zzip_inline static char *
392 __zzip_aligned4(char *p)
393 {
394 #define aligned4 __zzip_aligned4
395 p += ((intptr_t) p) & 1;
396 p += ((intptr_t) p) & 2;
397 return p;
398 }
399
400 /** internal.
401 * This function is used by => zzip_file_open, it is usually called after
402 * => __zzip_find_disk_trailer. It will parse the zip's central directory
403 * information and create a zziplib private directory table in
404 * memory.
405 */
406 static int
407 __zzip_parse_root_directory(int fd,
408 struct _disk_trailer *trailer,
409 struct zzip_dir_hdr **hdr_return,
410 zzip_plugin_io_t io,
411 zzip_off_t filesize)
412 {
413 auto struct zzip_disk_entry dirent;
414 struct zzip_dir_hdr *hdr;
415 struct zzip_dir_hdr *hdr0;
416 uint16_t *p_reclen = 0;
417 zzip_off64_t entries;
418 zzip_off64_t zz_offset; /* offset from start of root directory */
419 char *fd_map = 0;
420 zzip_off64_t zz_fd_gap = 0;
421 zzip_off64_t zz_entries = _disk_trailer_localentries(trailer);
422 zzip_off64_t zz_rootsize = _disk_trailer_rootsize(trailer);
423 zzip_off64_t zz_rootseek = _disk_trailer_rootseek(trailer);
424 __correct_rootseek(zz_rootseek, zz_rootsize, trailer);
425
426 if (zz_entries <= 0 || zz_rootsize < 0 ||
427 zz_rootseek < 0 || zz_rootseek >= filesize)
428 return ZZIP_CORRUPTED;
429
430 hdr0 = (struct zzip_dir_hdr *) malloc(zz_rootsize);
431 if (! hdr0)
432 return ZZIP_DIRSIZE;
433 hdr = hdr0;
434 __debug_dir_hdr(hdr);
435
436 if (USE_MMAP && io->fd.sys)
437 {
438 zz_fd_gap = zz_rootseek & (_zzip_getpagesize(io->fd.sys) - 1);
439 HINT4(" fd_gap=%ld, mapseek=0x%lx, maplen=%ld", (long) (zz_fd_gap),
440 (long) (zz_rootseek - zz_fd_gap),
441 (long) (zz_rootsize + zz_fd_gap));
442 fd_map =
443 _zzip_mmap(io->fd.sys, fd, zz_rootseek - zz_fd_gap,
444 zz_rootsize + zz_fd_gap);
445 /* if mmap failed we will fallback to seek/read mode */
446 if (fd_map == MAP_FAILED)
447 {
448 NOTE2("map failed: %s", strerror(errno));
449 fd_map = 0;
450 } else
451 {
452 HINT3("mapped *%p len=%li", fd_map,
453 (long) (zz_rootsize + zz_fd_gap));
454 }
455 }
456
457 for (entries=0, zz_offset=0; ; entries++)
458 {
459 register struct zzip_disk_entry *d;
460 uint16_t u_extras, u_comment, u_namlen;
461
462 # ifndef ZZIP_ALLOW_MODULO_ENTRIES
463 if (entries >= zz_entries) {
464 if (zz_offset + 256 < zz_rootsize) {
465 FAIL4("%li's entry is long before the end of directory - enable modulo_entries? (O:%li R:%li)",
466 (long) entries, (long) (zz_offset), (long) zz_rootsize);
467 }
468 break;
469 }
470 # endif
471
472 if (fd_map)
473 {
474 d = (void*)(fd_map+zz_fd_gap+zz_offset); /* fd_map+fd_gap==u_rootseek */
475 } else
476 {
477 if (io->fd.seeks(fd, zz_rootseek + zz_offset, SEEK_SET) < 0)
478 {
479 free(hdr0);
480 return ZZIP_DIR_SEEK;
481 }
482 if (io->fd.read(fd, &dirent, sizeof(dirent)) < __sizeof(dirent))
483 {
484 free(hdr0);
485 return ZZIP_DIR_READ;
486 }
487 d = &dirent;
488 }
489
490 if ((zzip_off64_t) (zz_offset + sizeof(*d)) > zz_rootsize ||
491 (zzip_off64_t) (zz_offset + sizeof(*d)) < 0)
492 {
493 FAIL4("%li's entry stretches beyond root directory (O:%li R:%li)",
494 (long) entries, (long) (zz_offset), (long) zz_rootsize);
495 break;
496 }
497
498 if (! zzip_disk_entry_check_magic(d)) {
499 # ifndef ZZIP_ALLOW_MODULO_ENTRIES
500 FAIL4("%li's entry has no disk_entry magic indicator (O:%li R:%li)",
501 (long) entries, (long) (zz_offset), (long) zz_rootsize);
502 # endif
503 break;
504 }
505
506 # if 0 && defined DEBUG
507 zzip_debug_xbuf((unsigned char *) d, sizeof(*d) + 8);
508 # endif
509
510 u_extras = zzip_disk_entry_get_extras(d);
511 u_comment = zzip_disk_entry_get_comment(d);
512 u_namlen = zzip_disk_entry_get_namlen(d);
513 HINT5("offset=0x%lx, size %ld, dirent *%p, hdr %p\n",
514 (long) (zz_offset + zz_rootseek), (long) zz_rootsize, d, hdr);
515
516 /* writes over the read buffer, Since the structure where data is
517 copied is smaller than the data in buffer this can be done.
518 It is important that the order of setting the fields is considered
519 when filling the structure, so that some data is not trashed in
520 first structure read.
521 at the end the whole copied list of structures is copied into
522 newly allocated buffer */
523 hdr->d_crc32 = zzip_disk_entry_get_crc32(d);
524 hdr->d_csize = zzip_disk_entry_get_csize(d);
525 hdr->d_usize = zzip_disk_entry_get_usize(d);
526 hdr->d_off = zzip_disk_entry_get_offset(d);
527 hdr->d_compr = zzip_disk_entry_get_compr(d);
528 if (hdr->d_compr > _255)
529 hdr->d_compr = 255;
530
531 if ((zzip_off64_t) (zz_offset + sizeof(*d) + u_namlen) > zz_rootsize ||
532 (zzip_off64_t) (zz_offset + sizeof(*d) + u_namlen) < 0)
533 {
534 FAIL4("%li's name stretches beyond root directory (O:%li N:%li)",
535 (long) entries, (long) (zz_offset), (long) (u_namlen));
536 break;
537 }
538
539 if (fd_map)
540 { memcpy(hdr->d_name, fd_map+zz_fd_gap + zz_offset+sizeof(*d), u_namlen); }
541 else
542 { io->fd.read(fd, hdr->d_name, u_namlen); }
543 hdr->d_name[u_namlen] = '\0';
544 hdr->d_namlen = u_namlen;
545
546 /* update offset by the total length of this entry -> next entry */
547 zz_offset += sizeof(*d) + u_namlen + u_extras + u_comment;
548
549 if (zz_offset > zz_rootsize)
550 {
551 FAIL3("%li's entry stretches beyond root directory (O:%li)",
552 (long) entries, (long) (zz_offset));
553 entries ++;
554 break;
555 }
556
557 HINT5("file %ld { compr=%d crc32=$%x offset=%d",
558 (long) entries, hdr->d_compr, hdr->d_crc32, hdr->d_off);
559 HINT5("csize=%d usize=%d namlen=%d extras=%d",
560 hdr->d_csize, hdr->d_usize, u_namlen, u_extras);
561 HINT5("comment=%d name='%s' %s <sizeof %d> } ",
562 u_comment, hdr->d_name, "", (int) sizeof(*d));
563
564 p_reclen = &hdr->d_reclen;
565
566 {
567 register char *p = (char *) hdr;
568 register char *q = aligned4(p + sizeof(*hdr) + u_namlen + 1);
569 *p_reclen = (uint16_t) (q - p);
570 hdr = (struct zzip_dir_hdr *) q;
571 }
572 } /*for */
573
574 if (USE_MMAP && fd_map)
575 {
576 HINT3("unmap *%p len=%li", fd_map, (long) (zz_rootsize + zz_fd_gap));
577 _zzip_munmap(io->fd.sys, fd_map, zz_rootsize + zz_fd_gap);
578 }
579
580 if (p_reclen)
581 {
582 *p_reclen = 0; /* mark end of list */
583
584 if (hdr_return)
585 *hdr_return = hdr0;
586 else
587 {
588 /* If it is not assigned to *hdr_return, it will never be free()'d */
589 free(hdr0);
590 }
591 } /* else zero (sane) entries */
592 else
593 free(hdr0);
594 # ifndef ZZIP_ALLOW_MODULO_ENTRIES
595 return (entries != zz_entries) ? ZZIP_CORRUPTED : 0;
596 # else
597 return ((entries & (unsigned)0xFFFF) != zz_entries) ? ZZIP_CORRUPTED : 0;
598 # endif
599 }
600
601 /* ------------------------- high-level interface ------------------------- */
602
603 #ifndef O_BINARY
604 #define O_BINARY 0
605 #endif
606
607 static zzip_strings_t *
608 zzip_get_default_ext(void)
609 {
610 static zzip_strings_t ext[] = {
611 /* *INDENT-OFF* */
612 ".zip", ".ZIP", /* common extension */
613 # ifdef ZZIP_USE_ZIPLIKES
614 ".pk3", ".PK3", /* ID Software's Quake3 zipfiles */
615 ".jar", ".JAR", /* Java zipfiles */
616 # endif
617 /* *INDENT-ON* */
618 0
619 };
620
621 return ext;
622 }
623
624 /** start usage. also: zzip_dir_free
625 * This function allocates a new ZZIP_DIR handle and do basic
626 * initializations before usage by => zzip_dir_fdopen
627 * => zzip_dir_open => zzip_file_open or through
628 * => zzip_open
629 * (ext==null flags uses { ".zip" , ".ZIP" } )
630 * (io ==null flags use of posix io defaults)
631 */
632 ZZIP_DIR *
633 zzip_dir_alloc_ext_io(zzip_strings_t * ext, const zzip_plugin_io_t io)
634 {
635 ZZIP_DIR *dir;
636 if ((dir = (ZZIP_DIR *) calloc(1, sizeof(*dir))) == NULL)
637 return 0;
638
639 /* dir->fileext is currently unused - so what, still initialize it */
640 dir->fileext = ext ? ext : zzip_get_default_ext();
641 dir->io = io ? io : zzip_get_default_io();
642 return dir;
643 }
644
645 /** => zzip_dir_alloc_ext_io
646 * This function is obsolete - it was generally used for implementation
647 * and exported to let other code build on it. It is now advised to
648 * use => zzip_dir_alloc_ext_io now on explicitly, just set that second
649 * argument to zero to achieve the same functionality as the old style.
650 */
651 ZZIP_DIR *
652 zzip_dir_alloc(zzip_strings_t * fileext)
653 {
654 return zzip_dir_alloc_ext_io(fileext, 0);
655 }
656
657 /** also: zzip_dir_alloc_ext_io
658 * This function will free the zzip_dir handle unless there are still
659 * zzip_files attached (that may use its cache buffer).
660 * This is the inverse of => zzip_dir_alloc , and both
661 * are helper functions used implicitly in other zzipcalls
662 * e.g. => zzip_dir_close = zzip_close
663 *
664 * returns zero on success
665 * returns the refcount when files are attached.
666 */
667 int
668 zzip_dir_free(ZZIP_DIR * dir)
669 {
670 if (dir->refcount)
671 return (dir->refcount); /* still open files attached */
672
673 if (dir->fd >= 0)
674 dir->io->fd.close(dir->fd);
675 if (dir->hdr0)
676 free(dir->hdr0);
677 if (dir->cache.fp)
678 free(dir->cache.fp);
679 if (dir->cache.buf32k)
680 free(dir->cache.buf32k);
681 if (dir->realname)
682 free(dir->realname);
683 free(dir);
684 return 0;
685 }
686
687 /** => zzip_dir_free
688 * It will also => free(2) the => ZZIP_DIR-handle given.
689 * the counterpart for => zzip_dir_open
690 * see also => zzip_dir_free
691 */
692 int
693 zzip_dir_close(ZZIP_DIR * dir)
694 {
695 dir->refcount &= ~0x10000000; /* explicit dir close */
696 return zzip_dir_free(dir);
697 }
698
699 /** fd reopen.
700 * This function is used by the => zzip_dir_open and zzip_opendir(2) call.
701 * Opens the zip-archive as specified with the fd which points to an
702 * already openend file. This function then search and parse
703 * the zip's central directory.
704 *
705 * NOTE: refcount is zero, so an _open/_close pair will also delete
706 * this _dirhandle
707 */
708 ZZIP_DIR *
709 zzip_dir_fdopen(int fd, zzip_error_t * errcode_p)
710 {
711 return zzip_dir_fdopen_ext_io(fd, errcode_p, 0, 0);
712 }
713
714 static zzip_error_t __zzip_dir_parse(ZZIP_DIR * dir); /* forward */
715
716 /** => zzip_dir_fdopen
717 * this function uses explicit ext and io instead of the internal
718 * defaults, setting these to zero is equivalent to => zzip_dir_fdopen
719 */
720 ZZIP_DIR *
721 zzip_dir_fdopen_ext_io(int fd, zzip_error_t * errcode_p,
722 zzip_strings_t * ext, const zzip_plugin_io_t io)
723 {
724 zzip_error_t rv;
725 ZZIP_DIR *dir;
726
727 if ((dir = zzip_dir_alloc_ext_io(ext, io)) == NULL)
728 { rv = ZZIP_OUTOFMEM; goto error; }
729
730 dir->fd = fd;
731 if ((rv = __zzip_dir_parse(dir)))
732 goto error;
733
734 dir->hdr = dir->hdr0;
735 dir->refcount |= 0x10000000;
736
737 if (errcode_p)
738 *errcode_p = rv;
739 return dir;
740 error:
741 if (dir)
742 zzip_dir_free(dir);
743 if (errcode_p)
744 *errcode_p = rv;
745 return NULL;
746 }
747
748 static zzip_error_t
749 __zzip_dir_parse(ZZIP_DIR * dir)
750 {
751 zzip_error_t rv;
752 zzip_off_t filesize;
753 struct _disk_trailer trailer;
754 /* if (! dir || dir->fd < 0)
755 * { rv = EINVAL; goto error; }
756 */
757
758 HINT2("------------------ fd=%i", (int) dir->fd);
759 if ((filesize = dir->io->fd.filesize(dir->fd)) < 0)
760 { rv = ZZIP_DIR_STAT; goto error; }
761
762 HINT2("------------------ filesize=%ld", (long) filesize);
763 if ((rv = __zzip_fetch_disk_trailer(dir->fd, filesize, &trailer,
764 dir->io)) != 0)
765 { goto error; }
766
767 HINT5("directory = { entries= %ld/%ld, size= %ld, seek= %ld } ",
768 (long) _disk_trailer_localentries(&trailer),
769 (long) _disk_trailer_finalentries(&trailer),
770 (long) _disk_trailer_rootsize(&trailer),
771 (long) _disk_trailer_rootseek(&trailer));
772
773 if ((rv = __zzip_parse_root_directory(dir->fd, &trailer, &dir->hdr0,
774 dir->io, filesize)) != 0)
775 { goto error; }
776 error:
777 return rv;
778 }
779
780 /** internal.
781 * This function will attach any of the .zip extensions then
782 * trying to open it the with => open(2). This is a helper
783 * function for => zzip_dir_open, => zzip_opendir and => zzip_open.
784 *
785 * This function returns a new system file handle or -1 on error.
786 * On error this function leaves the errno(3) of the underlying
787 * open(2) call on the last file.
788 */
789 int
790 __zzip_try_open(zzip_char_t * filename, int filemode,
791 zzip_strings_t * ext, zzip_plugin_io_t io)
792 {
793 auto char file[PATH_MAX];
794 int fd;
795 zzip_size_t len = strlen(filename);
796
797 if (len + 4 >= PATH_MAX)
798 { errno = ENAMETOOLONG; return -1; }
799 memcpy(file, filename, len + 1);
800
801 if (! io)
802 io = zzip_get_default_io();
803 if (! ext)
804 ext = zzip_get_default_ext();
805
806 for (; *ext; ++ext)
807 {
808 strcpy(file + len, *ext);
809 fd = (io->fd.open)(file, filemode);
810 if (fd != -1)
811 return fd;
812 }
813 return -1;
814 }
815
816 /** open zip-archive.
817 * This function opens the given zip-archive (if available).
818 * The two ext_io arguments will default to use posix io and
819 * a set of default fileext that can atleast add .zip ext itself.
820 */
821 ZZIP_DIR *
822 zzip_dir_open(zzip_char_t * filename, zzip_error_t * e)
823 {
824 return zzip_dir_open_ext_io(filename, e, 0, 0);
825 }
826
827 /** => zzip_dir_open
828 * this function uses explicit ext and io instead of the internal
829 * defaults. Setting these to zero is equivalent to => zzip_dir_open
830 * Note that the referenced ext_io plugin handlers structure must be
831 * static as it is not copied to the returned ZZIP_DIR structure.
832 */
833 ZZIP_DIR *
834 zzip_dir_open_ext_io(zzip_char_t * filename, zzip_error_t * e,
835 zzip_strings_t * ext, zzip_plugin_io_t io)
836 {
837 int fd;
838
839 if (! io)
840 io = zzip_get_default_io();
841 if (! ext)
842 ext = zzip_get_default_ext();
843
844 fd = (io->fd.open)(filename, O_RDONLY | O_BINARY);
845 if (fd != -1)
846 {
847 return zzip_dir_fdopen_ext_io(fd, e, ext, io);
848 } else
849 {
850 fd = __zzip_try_open(filename, O_RDONLY | O_BINARY, ext, io);
851 if (fd != -1)
852 {
853 return zzip_dir_fdopen_ext_io(fd, e, ext, io);
854 } else
855 {
856 if (e)
857 { *e = ZZIP_DIR_OPEN; }
858 return 0;
859 }
860 }
861 }
862
863 /** => zzip_dir_open
864 * fills the dirent-argument with the values and
865 * increments the read-pointer of the dir-argument.
866 *
867 * returns 0 if there no entry (anymore).
868 */
869 int
870 zzip_dir_read(ZZIP_DIR * dir, ZZIP_DIRENT * d)
871 {
872 if (! dir || ! dir->hdr || ! d)
873 return 0;
874
875 d->d_compr = dir->hdr->d_compr;
876 d->d_csize = dir->hdr->d_csize;
877 d->st_size = dir->hdr->d_usize;
878 d->d_name = dir->hdr->d_name;
879
880 if (! dir->hdr->d_reclen)
881 {
882 dir->hdr = 0;
883 } else
884 {
885 dir->hdr = (struct zzip_dir_hdr *)
886 ((char *) dir->hdr + dir->hdr->d_reclen);
887 }
888
889 return 1;
890 }
891
892 /*
893 * Local variables:
894 * c-file-style: "stroustrup"
895 * End:
896 */