diff options
Diffstat (limited to 'Build/source/libs/zziplib/zziplib-src/zzip/mmapped.c')
-rw-r--r-- | Build/source/libs/zziplib/zziplib-src/zzip/mmapped.c | 121 |
1 files changed, 41 insertions, 80 deletions
diff --git a/Build/source/libs/zziplib/zziplib-src/zzip/mmapped.c b/Build/source/libs/zziplib/zziplib-src/zzip/mmapped.c index 67f293fb0d1..c61fc5656b6 100644 --- a/Build/source/libs/zziplib/zziplib-src/zzip/mmapped.c +++ b/Build/source/libs/zziplib/zziplib-src/zzip/mmapped.c @@ -30,8 +30,11 @@ #include <zzip/mmapped.h> #include <zzip/format.h> #include <zzip/fetch.h> +#include <zzip/__debug.h> #include <zzip/__mmap.h> +#include <zzip/__string.h> #include <zzip/__fnmatch.h> +#include <zzip/__errno.h> #include <stdlib.h> #include <sys/stat.h> @@ -57,18 +60,6 @@ #define ____ } #endif -#ifdef DEBUG -#define debug1(msg) do { fprintf(stderr, "%s : " msg "\n", __func__); } while(0) -#define debug2(msg, arg1) do { fprintf(stderr, "%s : " msg "\n", __func__, arg1); } while(0) -#define debug3(msg, arg1, arg2) do { fprintf(stderr, "%s : " msg "\n", __func__, arg1, arg2); } while(0) -#define debug4(msg, arg1, arg2, arg3) do { fprintf(stderr, "%s : " msg "\n", __func__, arg1, arg2, arg3); } while(0) -#else -#define debug1(msg) -#define debug2(msg, arg1) -#define debug3(msg, arg1, arg2) -#define debug4(msg, arg1, arg2, arg3) -#endif - /** => zzip_disk_mmap * This function does primary initialization of a disk-buffer struct. * @@ -151,7 +142,7 @@ zzip_disk_munmap(ZZIP_DISK * disk) * to => zzip_disk_mmap for bringing it to main memory. If it can not * be => mmap(2)'ed then we slurp the whole file into a newly => malloc(2)'ed * memory block. Only if that fails too then we return null. Since handling - * of disk->buffer is ambigous it should not be snatched away please. + * of disk->buffer is ambiguous it should not be snatched away please. * * This function may return null on errors (errno). */ @@ -225,7 +216,7 @@ zzip_disk_buffer(void *buffer, size_t buflen) { * zip archive, including any malloc()ed blocks, sharedmem mappings * and it dumps the handle struct as well. * - * This function returns 0 on sucess (or whatever => munmap says). + * This function returns 0 on success (or whatever => munmap says). */ int zzip_disk_close(ZZIP_DISK * disk) @@ -241,55 +232,8 @@ zzip_disk_close(ZZIP_DISK * disk) } /* ====================================================================== */ - /* helper functions */ -#ifdef ZZIP_HAVE_STRNDUP -#define _zzip_strndup strndup -#else - -/* if your system does not have strndup: */ -zzip__new__ static char * -_zzip_strndup(char *p, size_t maxlen) -{ - if (! p) - return 0; - ___ zzip_byte_t *r = malloc(maxlen + 1); - if (! r) - return r; - strncpy(r, p, maxlen); - r[maxlen] = '\0'; - return r; - ____; -} -#endif - -#if defined ZZIP_HAVE_STRCASECMP || defined strcasecmp -#define _zzip_strcasecmp strcasecmp -#else - -/* if your system does not have strcasecmp: */ -static int -_zzip_strcasecmp(char *__zzip_restrict a, char *_zzip_restrict b) -{ - if (! a) - return (b) ? 1 : 0; - if (! b) - return -1; - while (1) - { - int v = tolower(*a) - tolower(*b); - if (v) - return v; - if (! *a) - return 1; - if (! *b) - return -1; - a++; - b++; - } -} -#endif /** helper functions for (mmapped) zip access api * @@ -453,16 +397,16 @@ zzip_disk_entry_strdup_comment(ZZIP_DISK * disk, struct zzip_disk_entry *entry) struct zzip_disk_entry * zzip_disk_findfirst(ZZIP_DISK * disk) { - debug1("findfirst"); + DBG1("findfirst"); if (! disk) { - debug1("non arg"); + DBG1("non arg"); errno = EINVAL; return 0; } if (disk->buffer > disk->endbuf - sizeof(struct zzip_disk_trailer)) { - debug1("not enough data for a disk trailer"); + DBG1("not enough data for a disk trailer"); errno = EBADMSG; return 0; } @@ -475,12 +419,12 @@ zzip_disk_findfirst(ZZIP_DISK * disk) struct zzip_disk_trailer *trailer = (struct zzip_disk_trailer *) p; zzip_size_t rootseek = zzip_disk_trailer_get_rootseek(trailer); root = disk->buffer + rootseek; - debug2("disk rootseek at %lli", (long long)rootseek); + DBG2("disk rootseek at %lli", (long long)rootseek); if (root > p) { /* the first disk_entry is after the disk_trailer? can't be! */ zzip_size_t rootsize = zzip_disk_trailer_get_rootsize(trailer); - debug2("have rootsize at %lli", (long long)rootsize); + DBG2("have rootsize at %lli", (long long)rootsize); if (disk->buffer + rootsize > p) continue; /* a common brokeness that can be fixed: we just assume the @@ -493,12 +437,12 @@ zzip_disk_findfirst(ZZIP_DISK * disk) (struct zzip_disk64_trailer *) p; if (sizeof(void *) < 8) { - debug1("disk64 trailer in non-largefile part"); + DBG1("disk64 trailer in non-largefile part"); errno = EFBIG; return 0; } zzip_size_t rootseek = zzip_disk64_trailer_get_rootseek(trailer); - debug2("disk64 rootseek at %lli", (long long)rootseek); + DBG2("disk64 rootseek at %lli", (long long)rootseek); root = disk->buffer + rootseek; if (root > p) continue; @@ -507,16 +451,16 @@ zzip_disk_findfirst(ZZIP_DISK * disk) continue; } - debug4("buffer %p root %p endbuf %p", disk->buffer, root, disk->endbuf); + DBG4("buffer %p root %p endbuf %p", disk->buffer, root, disk->endbuf); if (root < disk->buffer) { - debug1("root before buffer should be impossible"); + DBG1("root before buffer should be impossible"); errno = EBADMSG; return 0; } if (zzip_disk_entry_check_magic(root)) { - debug1("found the disk root"); + DBG2("found the disk root %p", root); return (struct zzip_disk_entry *) root; } } ____; @@ -630,7 +574,7 @@ zzip_disk_findmatch(ZZIP_DISK * disk, char *filespec, { compare = (zzip_fnmatch_fn_t) _zzip_fnmatch; if (disk->flags & ZZIP_DISK_FLAGS_MATCH_NOCASE) - flags |= _zzip_fnmatch_CASEFOLD; + flags |= _zzip_FNM_CASEFOLD; } for (; entry; entry = zzip_disk_findnext(disk, entry)) { @@ -682,7 +626,13 @@ zzip_disk_entry_fopen(ZZIP_DISK * disk, ZZIP_DISK_ENTRY * entry) file->avail = zzip_file_header_usize(header); if (! file->avail || zzip_file_header_data_stored(header)) - { file->stored = zzip_file_header_to_data (header); return file; } + { + file->stored = zzip_file_header_to_data (header); + DBG2("stored size %i", (int) file->avail); + if (file->stored + file->avail >= disk->endbuf) + goto error; + return file; + } file->stored = 0; file->zlib.opaque = 0; @@ -691,15 +641,20 @@ zzip_disk_entry_fopen(ZZIP_DISK * disk, ZZIP_DISK_ENTRY * entry) file->zlib.avail_in = zzip_file_header_csize(header); file->zlib.next_in = zzip_file_header_to_data(header); - if (! zzip_file_header_data_deflated(header) || - inflateInit2(&file->zlib, -MAX_WBITS) != Z_OK) - { - free (file); - errno = EBADMSG; - return 0; - } + DBG2("compressed size %i", (int) file->zlib.avail_in); + if (file->zlib.next_in + file->zlib.avail_in >= disk->endbuf) + goto error; + + if (! zzip_file_header_data_deflated(header)) + goto error; + if (inflateInit2(&file->zlib, -MAX_WBITS) != Z_OK) + goto error; return file; +error: + free (file); + errno = EBADMSG; + return 0; ____; } @@ -738,6 +693,12 @@ zzip_disk_fread(void *ptr, zzip_size_t sized, zzip_size_t nmemb, size = file->avail; if (file->stored) { + if (file->stored + size >= file->endbuf) + { + DBG1("try to read beyond end of file"); + return 0; /* ESPIPE */ + } + DBG3("copy stored %p %i", file->stored, (int)size); memcpy(ptr, file->stored, size); file->stored += size; file->avail -= size; |