Debian Codegraph @

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

raw
1
2 /*
3 * NOTE: this is part of libzzipfseeko (i.e. it is not libzzip).
4 * ==================
5 *
6 * These routines are fully independent from the traditional zzip
7 * implementation. They assume a readonly seekable stdio handle
8 * representing a complete zip file. The functions show how to
9 * parse the structure, find files and return a decoded bytestream.
10 *
11 * These routines are a bit simple and really here for documenting
12 * the way to access a zip file. The complexity of zip access comes
13 * from staggered reading of bytes and reposition of a filepointer in
14 * a big archive with lots of files and long compressed datastreams.
15 * Plus varaints of drop-in stdio replacements, obfuscation routines,
16 * auto fileextensions, drop-in dirent replacements, and so on...
17 *
18 * btw, we can _not_ use fgetpos/fsetpos since an fpos_t has no asserted
19 * relation to a linear seek value as specified in zip info headers. In
20 * general it is not a problem if your system has no fseeko/ftello pair
21 * since we can fallback to fseek/ftell which limits the zip disk size
22 * to 2MiBs but the zip-storable seek values are 32bit limited anyway.
23 *
24 * Author:
25 * Guido Draheim <[email protected]>
26 *
27 * Copyright (c) Guido Draheim, use under copyleft (LGPL,MPL)
28 */
29
30 #define _LARGEFILE_SOURCE 1
31 #define _ZZIP_ENTRY_STRUCT 1
32
33 #include <zzip/fseeko.h>
34
35 #include <zzip/fetch.h>
36 #include <zzip/__mmap.h>
37 #include <zzip/__fnmatch.h>
38 #include <zzip/__errno.h>
39
40 #include <stdlib.h>
41 #include <sys/stat.h>
42
43 #if defined ZZIP_HAVE_STRING_H
44 #include <string.h>
45 #elif defined ZZIP_HAVE_STRINGS_H
46 #include <strings.h>
47 #endif
48
49 #if defined ZZIP_HAVE_STDINT_H
50 #include <stdint.h>
51 #endif
52
53 #if __STDC_VERSION__+0 > 199900L
54 #define ___
55 #define ____
56 #else
57 #define ___ {
58 #define ____ }
59 #endif
60
61 #ifndef ZZIP_HAVE_FSEEKO
62 #define fseeko fseek
63 #define ftello ftell
64 #endif
65
66 /* note that the struct zzip_entry inherits the zzip_disk_entry values
67 * and usually carries a copy of its values (in disk format!). To make the
68 * following code more readable, we use a shorthand notation for the
69 * upcast needed in C (not needed in C++) as "disk_(entry)".
70 */
71 #ifdef __zzip_entry_extends_zzip_disk_entry
72 #define disk_(_entry_) _entry_
73 #else
74 #define disk_(_entry_) (& (_entry_)->head)
75 #endif
76
77 /* we try to round all seeks to the pagesize - since we do not use
78 * the sys/mmap interface we have to guess a good value here: */
79 #ifndef PAGESIZE
80 # define PAGESIZE 8192
81 #endif
82
83 #ifdef DEBUG
84 #define debug1(msg) do { fprintf(stderr, "DEBUG: %s : " msg "\n", __func__); } while(0)
85 #define debug2(msg, arg1) do { fprintf(stderr, "DEBUG: %s : " msg "\n", __func__, arg1); } while(0)
86 #define debug3(msg, arg1, arg2) do { fprintf(stderr, "DEBUG: %s : " msg "\n", __func__, arg1, arg2); } while(0)
87 #define debug4(msg, arg1, arg2, arg3) do { fprintf(stderr, "DEBUG: %s : " msg "\n", __func__, arg1, arg2, arg3); } while(0)
88 #else
89 #define debug1(msg)
90 #define debug2(msg, arg1)
91 #define debug3(msg, arg1, arg2)
92 #define debug4(msg, arg1, arg2, arg3)
93 #endif
94
95 /* ====================================================================== */
96
97 /* helper functions */
98
99 /** => zzip_entry_data_offset
100 * This functions read the correspoding struct zzip_file_header from
101 * the zip disk of the given "entry". The returned off_t points to the
102 * end of the file_header where the current fseek pointer has stopped.
103 * This is used to immediately parse out any filename/extras block following
104 * the file_header.
105 *
106 * Returns zero on error. (errno = EINVAL|EBADMSG|EBADF|EIO)
107 */
108 static zzip_off_t
109 zzip_entry_fread_file_header(ZZIP_ENTRY * entry,
110 struct zzip_file_header *file_header)
111 {
112 if (! entry || ! file_header)
113 {
114 errno = EINVAL;
115 return 0;
116 }
117 ___ zzip_off_t offset = zzip_disk_entry_fileoffset(disk_(entry));
118 if (0 > offset || offset >= entry->disksize)
119 {
120 errno = EBADMSG;
121 return 0;
122 }
123
124 if (fseeko(entry->diskfile, offset, SEEK_SET) == -1)
125 {
126 debug2("fseeko failed: %s", strerror(errno));
127 return 0; /* EBADF */
128 }
129
130 ___ zzip_size_t n = fread(file_header, 1, sizeof(*file_header), entry->diskfile);
131 if (n < sizeof(*file_header))
132 {
133 debug4("fread failed: at offset %llu got %i instead of %i",
134 (long long) offset, n, sizeof(*file_header));
135 errno = ferror(entry->diskfile) ? EBADF : EIO;
136 return 0;
137 }
138 ____;
139 return offset + sizeof(*file_header);
140 ____;
141 }
142
143 /** helper functions for (fseeko) zip access api
144 *
145 * This functions returns the seekval offset of the data portion of the
146 * file referenced by the given zzip_entry. It requires an intermediate
147 * check of the file_header structure (i.e. it reads it from disk). After
148 * this call, the contained diskfile readposition is already set to the
149 * data_offset returned here.
150 *
151 * Returns -1 on error. (errno = EINVAL|EBADMSG)
152 */
153 zzip_off_t
154 zzip_entry_data_offset(ZZIP_ENTRY * entry)
155 {
156 struct zzip_file_header file_header;
157 if (! entry)
158 {
159 errno = EINVAL;
160 return -1;
161 }
162 ___ zzip_off_t offset = zzip_entry_fread_file_header(entry, &file_header);
163 if (! offset)
164 {
165 return -1; /* EBADMSG */
166 }
167 offset += zzip_file_header_sizeof_tails(&file_header);
168 if (fseeko(entry->diskfile, offset, SEEK_SET) == -1)
169 {
170 return -1; /* EBADF */
171 }
172 return offset;
173 ____;
174 }
175
176 /** => zzip_entry_data_offset
177 * This function is a big helper despite its little name: in a zip file the
178 * encoded filenames are usually NOT zero-terminated but for common usage
179 * with libc we need it that way. Secondly, the filename SHOULD be present
180 * in the zip central directory but if not then we fallback to the filename
181 * given in the file_header of each compressed data portion.
182 *
183 * returns: new string buffer, null on error (errno = EINVAL|ENOMEM|EBADMSG)
184 */
185 zzip__new__ char *
186 zzip_entry_strdup_name(ZZIP_ENTRY * entry)
187 {
188 if (! entry)
189 {
190 errno = EINVAL;
191 return 0;
192 }
193
194 ___ zzip_size_t len;
195 if ((len = zzip_disk_entry_namlen(disk_(entry))))
196 {
197 char *name = malloc(len + 1);
198 if (! name)
199 {
200 return 0; /* ENOMEM */
201 }
202 memcpy(name, entry->tail, len);
203 name[len] = '\0';
204 return name;
205 }
206 ___ auto struct zzip_file_header header;
207 if (zzip_entry_fread_file_header(entry, &header)
208 && (len = zzip_file_header_namlen(&header)))
209 {
210 char *name = malloc(len + 1);
211 if (! name) {
212 return 0; /* ENOMEM */
213 }
214 ___ zzip_size_t n = fread(name, 1, len, entry->diskfile);
215 if (n < len)
216 {
217 errno = ferror(entry->diskfile) ? EBADF : EIO;
218 free (name);
219 return 0;
220 }
221 name[n] = '\0';
222 return name;
223 ____;
224 }
225 errno = EBADMSG;
226 return 0;
227 ____;
228 ____;
229 }
230
231 static int
232 prescan_entry(ZZIP_ENTRY * entry)
233 {
234 if (! entry)
235 return EINVAL;
236 ___ zzip_off_t tailsize = zzip_disk_entry_sizeof_tails(disk_(entry));
237 if (tailsize + 1 > entry->tailalloc)
238 {
239 char *newtail = realloc(entry->tail, tailsize + 1);
240 if (! newtail)
241 return ENOMEM;
242 entry->tail = newtail;
243 entry->tailalloc = tailsize + 1;
244 }
245 # ifdef SIZE_MAX /* from stdint.h */
246 if ((unsigned long long)(tailsize) > (unsigned long long)(SIZE_MAX))
247 {
248 debug3("tailsize %llu bigger than sizemax %llu", (long long)(tailsize), (long long)(SIZE_MAX));
249 return EFBIG;
250 }
251 # endif
252 ___ zzip_size_t readsize = fread(entry->tail, 1, tailsize, entry->diskfile);
253 /* name + comment + extras */
254 if ((zzip_off_t)readsize != tailsize) {
255 debug1("read to data");
256 return errno;
257 } else {
258 return 0;
259 } ____; ____;
260 }
261
262 static void
263 prescan_clear(ZZIP_ENTRY * entry)
264 {
265 if (! entry)
266 return;
267 if (entry->tail)
268 free(entry->tail);
269 entry->tail = 0;
270 entry->tailalloc = 0;
271 }
272
273 /* ====================================================================== */
274
275 /** => zzip_entry_findfile
276 *
277 * This function is the first call of all the zip access functions here.
278 * It contains the code to find the first entry of the zip central directory.
279 * Here we require the stdio handle to represent a real zip file where the
280 * disk_trailer is _last_ in the file area, so that its position would be at
281 * a fixed offset from the end of the file area if not for the comment field
282 * allowed to be of variable length (which needs us to do a little search
283 * for the disk_tailer). However, in this simple implementation we disregard
284 * any disk_trailer info telling about multidisk archives, so we just return
285 * a pointer to the first entry in the zip central directory of that file.
286 *
287 * For an actual means, we are going to search backwards from the end
288 * of the mmaped block looking for the PK-magic signature of a
289 * disk_trailer. If we see one then we check the rootseek value to
290 * find the first disk_entry of the root central directory. If we find
291 * the correct PK-magic signature of a disk_entry over there then we
292 * assume we are done and we are going to return a pointer to that label.
293 *
294 * The return value is a pointer to the first zzip_disk_entry being checked
295 * to be within the bounds of the file area specified by the arguments. If
296 * no disk_trailer was found then null is returned, and likewise we only
297 * accept a disk_trailer with a seekvalue that points to a disk_entry and
298 * both parts have valid PK-magic parts. Beyond some sanity check we try to
299 * catch a common brokeness with zip archives that still allows us to find
300 * the start of the zip central directory.
301 *
302 * Returns null on error (errno = EINVAL|ENOMEM|EBADMSG|EBADF|ENOENT)
303 */
304 zzip__new__ ZZIP_ENTRY *
305 zzip_entry_findfirst(FILE * disk)
306 {
307 if (! disk)
308 {
309 errno = EINVAL;
310 return 0;
311 }
312 if (fseeko(disk, 0, SEEK_END) == -1)
313 {
314 printf("fseeko failed: %s", strerror(errno));
315 return 0; /* EBADF */
316 }
317 ___ zzip_off_t disksize = ftello(disk);
318 if (disksize < (zzip_off_t) sizeof(struct zzip_disk_trailer))
319 {
320 errno = EBADMSG;
321 return 0;
322 }
323 /* we read out chunks of 8 KiB in the hope to match disk granularity */
324 ___ zzip_off_t pagesize = PAGESIZE; /* getpagesize() */
325 ___ ZZIP_ENTRY *entry = calloc(1, sizeof(*entry));
326 if (! entry)
327 goto error0; /* ENOMEM */
328 ___ unsigned char *buffer = malloc(pagesize);
329 if (! buffer)
330 goto error1; /* ENOMEM */
331
332 if (pagesize / 2 <= (zzip_off_t) sizeof(struct zzip_disk_trailer))
333 {
334 errno = EBADMSG;
335 goto error2;
336 }
337 /* at each step, we will fread a pagesize block which overlaps with the
338 * previous read by means of pagesize/2 step at the end of the while(1) */
339 ___ zzip_off_t mapoffs = disksize & ~(pagesize - 1);
340 ___ zzip_off_t mapsize = disksize - mapoffs;
341 if (mapoffs && mapsize < pagesize / 2)
342 {
343 mapoffs -= pagesize / 2;
344 mapsize += pagesize / 2;
345 }
346 if (mapsize >= 3*8192)
347 {
348 errno = EBADMSG;
349 goto error2;
350 }
351 while (1)
352 {
353 if (fseeko(disk, mapoffs, SEEK_SET) == -1)
354 goto error2; /* EBADF */
355 if (fread(buffer, 1, mapsize, disk) < (zzip_size_t)mapsize)
356 {
357 errno = ferror(disk) ? EBADF : EIO;
358 debug1("in fread");
359 goto error2;
360 }
361 ___ unsigned char *p =
362 buffer + mapsize - sizeof(struct zzip_disk_trailer);
363 for (; p >= buffer; p--)
364 {
365 zzip_off_t root; /* (struct zzip_disk_entry*) */
366 if (zzip_disk_trailer_check_magic(p))
367 {
368 root = zzip_disk_trailer_rootseek((struct zzip_disk_trailer *)
369 p);
370 if (root > disksize - (long) sizeof(struct zzip_disk_trailer))
371 {
372 /* first disk_entry is after the disk_trailer? can't be! */
373 struct zzip_disk_trailer *trailer =
374 (struct zzip_disk_trailer *) p;
375 zzip_off_t rootsize = zzip_disk_trailer_rootsize(trailer);
376 if (rootsize > mapoffs)
377 continue;
378 /* a common brokeness that can be fixed: we just assume the
379 * central directory was written directly before : */
380 root = mapoffs - rootsize;
381 }
382 if (buffer + sizeof(struct zzip_disk64_locator) <= p) {
383 p -= sizeof(struct zzip_disk64_locator);
384 }
385 if (zzip_disk64_locator_check_magic(p))
386 {
387 struct zzip_disk64_locator *locator =
388 (struct zzip_disk64_locator *) p;
389 debug1("found zip64 disk locator (not supported)");
390 /* seek = zzip_disk64_locator_rootseek(locator); */
391 }
392 } else if (zzip_disk64_trailer_check_magic(p))
393 {
394 struct zzip_disk64_trailer *trailer =
395 (struct zzip_disk64_trailer *) p;
396 if (sizeof(zzip_off_t) < 8)
397 {
398 debug1("disk64 trailer on non-large compile");
399 errno = EFBIG;
400 goto error2;
401 }
402 if ((p + sizeof(*trailer)) > (buffer + mapsize))
403 {
404 debug1("disk64 trailer is not complete");
405 errno = EBADMSG;
406 goto error2;
407 }
408 root = zzip_disk64_trailer_rootseek(trailer);
409 } else
410 continue;
411
412 if (!(0 <= root && root < disksize))
413 {
414 debug3("bogus rootseek value %lli (disksize %lli)", (long long)root, (long long)disksize);
415 errno = EBADMSG;
416 goto error2;
417 }
418 if (fseeko(disk, root, SEEK_SET) == -1)
419 {
420 debug2("next seeko %s", strerror(errno));
421 goto error2; /* EBADF */
422 }
423 if (fread(disk_(entry), 1, sizeof(*disk_(entry)), disk)
424 != sizeof(*disk_(entry)))
425 {
426 debug2("next freed %s", strerror(errno));
427 errno = ferror(disk) ? EBADF : EIO;
428 goto error2;
429 }
430 if (zzip_disk_entry_check_magic(entry))
431 {
432 free(buffer);
433 buffer = NULL;
434 entry->headseek = root;
435 entry->diskfile = disk;
436 entry->disksize = disksize;
437 ___ int err = prescan_entry(entry);
438 if (err)
439 {
440 debug2("prescan %s", strerror(err));
441 errno = err;
442 goto error2;
443 }
444 return entry;
445 ____;
446 }
447 }
448 ____;
449 if (! mapoffs)
450 break;
451 if (mapsize < pagesize / 2)
452 {
453 debug1("bad mapsize should not be possible");
454 errno = EBADMSG;
455 goto error2;
456 }
457 mapoffs -= pagesize / 2; /* mapsize += pagesize/2; */
458 mapsize = pagesize; /* if (mapsize > pagesize) ... */
459 if (disksize - mapoffs > 64 * 1024)
460 break;
461 }
462 errno = ENOENT; /* not found */
463 error2:
464 if (buffer != NULL)
465 free(buffer);
466 error1:
467 free(entry);
468 ____;
469 ____;
470 ____;
471 ____;
472 ____;
473 ____;
474 error0:
475 debug1("error..");
476 return 0;
477 }
478
479 /** => zzip_entry_findfile
480 *
481 * This function takes an existing "entry" in the central root directory
482 * (e.g. from zzip_entry_findfirst) and moves it to point to the next entry.
483 * On error it returns 0, otherwise the old entry. If no further match is
484 * found then null is returned and the entry already free()d. If you want
485 * to stop searching for matches before that case then please call
486 * => zzip_entry_free on the cursor struct ZZIP_ENTRY.
487 */
488 zzip__new__ ZZIP_ENTRY *
489 zzip_entry_findnext(ZZIP_ENTRY * _zzip_restrict entry)
490 {
491 if (! entry)
492 {
493 errno = EINVAL;
494 return entry;
495 }
496 if (! zzip_disk_entry_check_magic(entry))
497 goto error_EBADMSG;
498 ___ zzip_off_t seek =
499 entry->headseek + zzip_disk_entry_sizeto_end(disk_(entry));
500 if (seek + (zzip_off_t) sizeof(*disk_(entry)) > entry->disksize)
501 goto error_EBADMSG;
502
503 if (fseeko(entry->diskfile, seek, SEEK_SET) == -1)
504 goto error; /* EBADF */
505 if (fread(disk_(entry), 1, sizeof(*disk_(entry)), entry->diskfile)
506 < sizeof(*disk_(entry)))
507 {
508 errno = ferror(entry->diskfile) ? EBADF : EIO;
509 goto error;
510 }
511 entry->headseek = seek;
512 if (! zzip_disk_entry_check_magic(entry))
513 goto error_EBADMSG;
514 ___ int err = prescan_entry(entry);
515 if (err)
516 {
517 errno = err;
518 goto error;
519 }
520 return entry;
521 ____;
522 error_EBADMSG:
523 errno = EBADMSG;
524 error:
525 zzip_entry_free(entry);
526 return 0;
527 ____;
528 }
529
530 /** => zzip_entry_findfile
531 * this function releases the malloc()ed areas needed for zzip_entry, the
532 * pointer is invalid afterwards. This function has #define synonyms of
533 * zzip_entry_findlast(), zzip_entry_findlastfile(), zzip_entry_findlastmatch()
534 */
535 int
536 zzip_entry_free(ZZIP_ENTRY * entry)
537 {
538 if (! entry)
539 {
540 errno = EINVAL;
541 return 0;
542 }
543 prescan_clear(entry);
544 free(entry);
545 return 1;
546 }
547
548 /** search for files in the (fseeko) zip central directory
549 *
550 * This function is given a filename as an additional argument, to find the
551 * disk_entry matching a given filename. The compare-function is usually
552 * strcmp or strcasecmp or perhaps strcoll, if null then strcmp is used.
553 * - use null as argument for "old"-entry when searching the first
554 * matching entry, otherwise the last returned value if you look for other
555 * entries with a special "compare" function (if null then a doubled search
556 * is rather useless with this variant of _findfile). If no further entry is
557 * found then null is returned and any "old"-entry gets already free()d.
558 */
559 zzip__new__ ZZIP_ENTRY *
560 zzip_entry_findfile(FILE * disk, char *filename,
561 ZZIP_ENTRY * _zzip_restrict entry, zzip_strcmp_fn_t compare)
562 {
563 if (! filename || ! disk)
564 {
565 errno = EINVAL;
566 return 0;
567 }
568 if (! entry)
569 entry = zzip_entry_findfirst(disk);
570 else
571 entry = zzip_entry_findnext(entry);
572
573 if (! compare)
574 compare = (zzip_strcmp_fn_t) (strcmp);
575
576 for (; entry; entry = zzip_entry_findnext(entry))
577 {
578 /* filenames within zip files are often not null-terminated! */
579 char *realname = zzip_entry_strdup_name(entry);
580 if (! realname)
581 {
582 return 0; /* ENOMEM|EBADMSG */
583 }
584 if (! compare(filename, realname))
585 {
586 free(realname);
587 return entry;
588 } else
589 {
590 free(realname);
591 continue;
592 }
593 }
594 errno = ENOENT;
595 return 0;
596 }
597
598 /** => zzip_entry_findfile
599 *
600 * This function uses a compare-function with an additional argument
601 * and it is called just like fnmatch(3) from POSIX.2 AD:1993), i.e.
602 * the argument filespec first and the ziplocal filename second with
603 * the integer-flags put in as third to the indirect call. If the
604 * platform has fnmatch available then null-compare will use that one
605 * and otherwise we fall back to mere strcmp, so if you need fnmatch
606 * searching then please provide an implementation somewhere else.
607 * - use null as argument for "after"-entry when searching the first
608 * matching entry, or the last disk_entry return-value to find the
609 * next entry matching the given filespec. If no further entry is
610 * found then null is returned and any "old"-entry gets already free()d.
611 */
612 zzip__new__ ZZIP_ENTRY *
613 zzip_entry_findmatch(FILE * disk, char *filespec,
614 ZZIP_ENTRY * _zzip_restrict entry,
615 zzip_fnmatch_fn_t compare, int flags)
616 {
617 if (! filespec || ! disk)
618 {
619 errno = EINVAL;
620 return 0;
621 }
622 if (! entry)
623 entry = zzip_entry_findfirst(disk);
624 else
625 entry = zzip_entry_findnext(entry);
626
627 if (! compare)
628 compare = (zzip_fnmatch_fn_t) _zzip_fnmatch;
629
630 for (; entry; entry = zzip_entry_findnext(entry))
631 {
632 /* filenames within zip files are often not null-terminated! */
633 char *realname = zzip_entry_strdup_name(entry);
634 if (! realname)
635 {
636 return 0; /* ENOMEM|EBADMSG */
637 }
638 if (! compare(filespec, realname, flags))
639 {
640 debug3("compare '%s' equal with zip '%s'", filespec, realname);
641 free(realname);
642 return entry;
643 } else
644 {
645 debug3("compare '%s' not like zip '%s'", filespec, realname);
646 free(realname);
647 continue;
648 }
649 }
650 errno = ENOENT;
651 return 0;
652 }
653
654 /* ====================================================================== */
655
656 /**
657 * typedef struct zzip_disk_file ZZIP_ENTRY_FILE;
658 */
659 struct zzip_entry_file /* : zzip_file_header */
660 {
661 struct zzip_file_header header; /* fopen detected header */
662 ZZIP_ENTRY *entry; /* fopen entry */
663 zzip_off_t data; /* for stored blocks */
664 zzip_size_t avail; /* memorized for checks on EOF */
665 zzip_size_t compressed; /* compressed flag and datasize */
666 zzip_size_t dataoff; /* offset from data start */
667 z_stream zlib; /* for inflated blocks */
668 unsigned char buffer[PAGESIZE]; /* work buffer for inflate algorithm */
669 };
670
671 /** open a file within a zip disk for reading
672 *
673 * This function does take an "entry" argument and copies it (or just takes
674 * it over as owner) to a new ZZIP_ENTRY_FILE handle structure. That
675 * structure contains also a zlib buffer for decoding. This function does
676 * seek to the file_header of the given "entry" and validates it for the
677 * data buffer following it. We do also prefetch some data from the data
678 * buffer thereby trying to match the disk pagesize for faster access later.
679 * The => zzip_entry_fread will then read in chunks of pagesizes which is
680 * the size of the internal readahead buffer. If an error occurs then null
681 * is returned.
682 */
683 zzip__new__ ZZIP_ENTRY_FILE *
684 zzip_entry_fopen(ZZIP_ENTRY * entry, int takeover)
685 {
686 if (! entry)
687 {
688 errno = EINVAL;
689 return 0;
690 }
691 if (! takeover)
692 {
693 ZZIP_ENTRY *found = malloc(sizeof(*entry));
694 if (! found)
695 {
696 return 0; /* ENOMEM */
697 }
698 memcpy(found, entry, sizeof(*entry)); /* prescan_copy */
699 found->tail = malloc(found->tailalloc);
700 if (! found->tail)
701 {
702 free (found);
703 return 0; /* ENOMEM */
704 }
705 memcpy(found->tail, entry->tail, entry->tailalloc);
706 entry = found;
707 }
708 ___ ZZIP_ENTRY_FILE *file = malloc(sizeof(*file));
709 if (! file)
710 goto error1; /* ENOMEM */
711 file->entry = entry;
712 if (! zzip_entry_fread_file_header(entry, &file->header))
713 {
714 debug2("file header: %s", strerror(errno));
715 goto error2; /* EIO|EBADMSG */
716 }
717 file->avail = zzip_file_header_usize(&file->header);
718 file->data = zzip_entry_data_offset(entry);
719 file->dataoff = 0;
720
721 if (! file->avail || zzip_file_header_data_stored(&file->header))
722 { file->compressed = 0; return file; }
723
724 file->compressed = zzip_file_header_csize(&file->header);
725 file->zlib.opaque = 0;
726 file->zlib.zalloc = Z_NULL;
727 file->zlib.zfree = Z_NULL;
728
729 ___ zzip_size_t size = file->avail;
730 if (size > sizeof(file->buffer))
731 size = sizeof(file->buffer);
732 if (fseeko(file->entry->diskfile, file->data + file->dataoff, SEEK_SET) == -1)
733 {
734 debug2("file seek failed: %s", strerror(errno));
735 goto error2; /* EBADF */
736 }
737 file->zlib.next_in = file->buffer;
738 file->zlib.avail_in = fread(file->buffer, 1, size,
739 file->entry->diskfile);
740 file->dataoff += file->zlib.avail_in;
741 ____;
742
743 if (! zzip_file_header_data_deflated(&file->header)
744 || inflateInit2(&file->zlib, -MAX_WBITS) != Z_OK)
745 {
746 debug1("decompress failed");
747 errno = EBADMSG;
748 goto error2;
749 }
750
751 return file;
752 error2:
753 free(file);
754 error1:
755 zzip_entry_free(entry);
756 return 0;
757 ____;
758 }
759
760 /** => zzip_entry_fopen
761 *
762 * This function opens a file found by name, so it does a search into
763 * the zip central directory with => zzip_entry_findfile and whatever
764 * is found first is given to => zzip_entry_fopen
765 */
766 zzip__new__ ZZIP_ENTRY_FILE *
767 zzip_entry_ffile(FILE * disk, char *filename)
768 {
769 ZZIP_ENTRY *entry = zzip_entry_findfile(disk, filename, 0, 0);
770 if (! entry)
771 {
772 return 0; /* EBADMSG */
773 }
774 return zzip_entry_fopen(entry, 1);
775 }
776
777
778 /** => zzip_entry_fopen
779 *
780 * This function reads more bytes into the output buffer specified as
781 * arguments. The return value is null on eof or error, the stdio-like
782 * interface can not distinguish between these so you need to check
783 * with => zzip_entry_feof for the difference.
784 */
785 zzip_size_t
786 zzip_entry_fread(void *ptr, zzip_size_t sized, zzip_size_t nmemb,
787 ZZIP_ENTRY_FILE * file)
788 {
789 if (! file)
790 {
791 errno = EINVAL;
792 return 0;
793 }
794 ___ zzip_size_t size = sized * nmemb;
795 if (! file->compressed)
796 {
797 if (size > file->avail)
798 size = file->avail;
799 if (fread(ptr, 1, size, file->entry->diskfile) != size)
800 {
801 errno = ferror(file->entry->diskfile) ? EBADF : EIO;
802 debug2("uncompressed fread incomplete: %s", strerror(errno));
803 return 0;
804 }
805 file->dataoff += size;
806 file->avail -= size;
807 return size;
808 }
809
810 file->zlib.avail_out = size;
811 file->zlib.next_out = ptr;
812 ___ zzip_size_t total_old = file->zlib.total_out;
813 while (1)
814 {
815 if (! file->zlib.avail_in)
816 {
817 size = file->compressed - file->dataoff;
818 debug2("remaining compressed bytes: %lli", (long long)size);
819 if (size > sizeof(file->buffer))
820 size = sizeof(file->buffer);
821 /* fseek (file->data + file->dataoff, file->entry->diskfile); */
822 file->zlib.avail_in = fread(file->buffer, 1, size,
823 file->entry->diskfile);
824 file->zlib.next_in = file->buffer;
825 file->dataoff += file->zlib.avail_in;
826 debug2("remaining compressed fread %lli", (long long) file->zlib.avail_in);
827 }
828 if (! file->zlib.avail_in)
829 {
830 errno = EIO;
831 return 0;
832 }
833
834 ___ int err = inflate(&file->zlib, Z_NO_FLUSH);
835 if (err == Z_STREAM_END)
836 file->avail = 0;
837 else if (err == Z_OK)
838 file->avail -= file->zlib.total_out - total_old;
839 else
840 {
841 debug1("zlib decode incomplete");
842 errno = EBADMSG;
843 return 0;
844 }
845 ____;
846 if (file->zlib.avail_out && ! file->zlib.avail_in)
847 continue;
848 return file->zlib.total_out - total_old;
849 }
850 ____;
851 ____;
852 }
853
854 /** => zzip_entry_fopen
855 * This function releases any zlib decoder info needed for decompression
856 * and dumps the ZZIP_ENTRY_FILE struct then.
857 */
858 int
859 zzip_entry_fclose(ZZIP_ENTRY_FILE * file)
860 {
861 if (! file)
862 {
863 errno = EINVAL;
864 return EOF;
865 }
866 if (file->compressed)
867 inflateEnd(&file->zlib);
868 zzip_entry_free(file->entry);
869 free(file);
870 return 0;
871 }
872
873 /** => zzip_entry_fopen
874 *
875 * This function allows to distinguish an error from an eof condition.
876 * Actually, if we found an error but we did already reach eof then we
877 * just keep on saying that it was an eof, so the app can just continue.
878 */
879 int
880 zzip_entry_feof(ZZIP_ENTRY_FILE * file)
881 {
882 return ! file || ! file->avail;
883 }