From ebf257fb1fabfdc6a24bc4e79f46af8dd25ee2aa Mon Sep 17 00:00:00 2001 From: Guido Draheim Date: Sun, 18 Mar 2007 22:50:50 +0000 Subject: [PATCH] indentation (.) --- configure | 2 +- zzip/Makefile.am | 26 +- zzip/Makefile.in | 26 +- zzip/dir.c | 178 +++++++----- zzip/err.c | 71 ++--- zzip/fetch.c | 33 ++- zzip/file.c | 732 ++++++++++++++++++++++++++--------------------- zzip/info.c | 54 ++-- zzip/memdisk.c | 283 ++++++++++-------- zzip/mmapped.c | 15 +- zzip/plugin.c | 26 +- zzip/stat.c | 6 +- 12 files changed, 810 insertions(+), 642 deletions(-) diff --git a/configure b/configure index a40d017..4f39d93 100755 --- a/configure +++ b/configure @@ -1,5 +1,5 @@ #! /bin/sh -# From configure.ac Revision: 1.8 . +# From configure.ac Revision: 1.9 . # Guess values for system-dependent variables and create Makefiles. # Generated by GNU Autoconf 2.60. # diff --git a/zzip/Makefile.am b/zzip/Makefile.am index 6e1fe89..b0f8674 100644 --- a/zzip/Makefile.am +++ b/zzip/Makefile.am @@ -99,7 +99,6 @@ uninstall-local : INDENT = indent INDENTFLAGS = \ ---blank-lines-after-declarations \ --blank-lines-after-procedures \ --blank-lines-before-block-comments \ --brace-indent0 \ @@ -126,18 +125,23 @@ INDENTFLAGS = \ --space-special-semicolon DIFF_U0 = diff -U0 +# __indent1 = "s|^\\([^{};]*if[^{};]*\\)\\(return[^{};]*;\\)|\\1 { \\2 }|" +__indent1 = ":begin" +__indent2 = "s|^\\([^{};]*\\)\\({[^{}]*}\\)\\([^{};]*\\)$$|\\1(' \\2 ');\\3|" +__indent3 = "s|___+[;].*|/*&*/|" +__indent4 = ":end" +__indent6 = "s|!\\([^ !=)]\\)|! \\1|g" +__indent7 = "s|(' \\({[^{}]*}\\) ');|\\1|" +__indent8 = "s|/[*]\\(___+[;].*\\)[*]/$$|\\1|" +__indent9 = ":end" indent-check: for file in $(srcdir)/*.c ; do f=`basename "$$file"` \ - ; sed \ - -e "s|\\(if[^{};]*\\)\\(return[^{};];\\)|\\1 { \\2 }|" \ - -e "s|{\\([^{}]\\)*}|(' & ');|" \ - -e "s|___;.*|//&|" $$file > $$f.tmp.2 \ - ; $(INDENT) $(INDENTFLAGS) $(INDENTOPTS) -st "$$f.tmp.2" \ - | sed \ - -e "s|!\\([^ !=)]\\)|! \\1|g" $(INDENTSED) \ - -e "s|(' \\({[^{}]*}\\) ');|\\1|" \ - -e "s|//\\(___;.*\\)|\\1|" $$f.tmp.2 > $$f.tmp \ - ; $(DIFF_U0) -s "$$file" "$$f.tmp" ; rm $$f.tmp.* ; done + ; sed -e $(__indent1) -e $(__indent2) -e $(__indent3) -e $(__indent4) \ + $(INDENTBEFORE) $$file \ + | $(INDENT) $(INDENTFLAGS) $(INDENTOPTS) -st - \ + | sed -e $(__indent6) -e $(__indent7) -e $(__indent8) -e $(__indent9) \ + $(INDENTAFTER) > $$f.tmp \ + ; $(DIFF_U0) -s "$$file" "$$f.tmp" ; done i: @ f=`basename $(file) .tmp` \ diff --git a/zzip/Makefile.in b/zzip/Makefile.in index 959b4b8..08a6e73 100644 --- a/zzip/Makefile.in +++ b/zzip/Makefile.in @@ -309,7 +309,6 @@ MAINTAINERCLEANFILES = _msvc.h CLEANFILES = $(WCC10_CLEAN) *.pc *.omf *~ *.tmp *.tmp.* INDENT = indent INDENTFLAGS = \ ---blank-lines-after-declarations \ --blank-lines-after-procedures \ --blank-lines-before-block-comments \ --brace-indent0 \ @@ -336,6 +335,15 @@ INDENTFLAGS = \ --space-special-semicolon DIFF_U0 = diff -U0 +# __indent1 = "s|^\\([^{};]*if[^{};]*\\)\\(return[^{};]*;\\)|\\1 { \\2 }|" +__indent1 = ":begin" +__indent2 = "s|^\\([^{};]*\\)\\({[^{}]*}\\)\\([^{};]*\\)$$|\\1(' \\2 ');\\3|" +__indent3 = "s|___+[;].*|/*&*/|" +__indent4 = ":end" +__indent6 = "s|!\\([^ !=)]\\)|! \\1|g" +__indent7 = "s|(' \\({[^{}]*}\\) ');|\\1|" +__indent8 = "s|/[*]\\(___+[;].*\\)[*]/$$|\\1|" +__indent9 = ":end" pkgconfig_HEADERS = zzip-zlib-config.pc zziplib.pc \ zzipmmapped.pc zzipfseeko.pc @@ -702,16 +710,12 @@ uninstall-local : (cd $(DESTDIR)$(includedir) && rm zziplib.h zzip.h zzip-io.h) indent-check: for file in $(srcdir)/*.c ; do f=`basename "$$file"` \ - ; sed \ - -e "s|\\(if[^{};]*\\)\\(return[^{};];\\)|\\1 { \\2 }|" \ - -e "s|{\\([^{}]\\)*}|(' & ');|" \ - -e "s|___;.*|//&|" $$file > $$f.tmp.2 \ - ; $(INDENT) $(INDENTFLAGS) $(INDENTOPTS) -st "$$f.tmp.2" \ - | sed \ - -e "s|!\\([^ !=)]\\)|! \\1|g" $(INDENTSED) \ - -e "s|(' \\({[^{}]*}\\) ');|\\1|" \ - -e "s|//\\(___;.*\\)|\\1|" $$f.tmp.2 > $$f.tmp \ - ; $(DIFF_U0) -s "$$file" "$$f.tmp" ; rm $$f.tmp.* ; done + ; sed -e $(__indent1) -e $(__indent2) -e $(__indent3) -e $(__indent4) \ + $(INDENTBEFORE) $$file \ + | $(INDENT) $(INDENTFLAGS) $(INDENTOPTS) -st - \ + | sed -e $(__indent6) -e $(__indent7) -e $(__indent8) -e $(__indent9) \ + $(INDENTAFTER) > $$f.tmp \ + ; $(DIFF_U0) -s "$$file" "$$f.tmp" ; done i: @ f=`basename $(file) .tmp` \ diff --git a/zzip/dir.c b/zzip/dir.c index 9d6e0ef..a5105fa 100644 --- a/zzip/dir.c +++ b/zzip/dir.c @@ -1,3 +1,4 @@ + /* * Author: * Guido Draheim @@ -10,9 +11,9 @@ * of the Mozilla Public License 1.1 */ -#include /* exported... */ +#include /* exported... */ #include -#include /*offsetof*/ +#include /*offsetof */ #include #include #include @@ -31,6 +32,7 @@ #endif #ifdef ZZIP_HAVE_SYS_STAT_H + /* MSVC does have IFbitmask but not the corresponding IStests */ # if ! defined S_ISDIR && defined S_IFDIR # define S_ISDIR(_X_) ((_X_) & S_IFDIR) @@ -46,12 +48,13 @@ * => zzip_opendir has a flag saying realdir or zipfile. As for a zipfile, * the filenames will include the filesubpath, so take care. */ -void +void zzip_rewinddir(ZZIP_DIR * dir) { - if (! dir) return; + if (! dir) + return; - if (USE_DIRENT && dir->realdir) + if (USE_DIRENT && dir->realdir) { _zzip_rewinddir(dir->realdir); return; @@ -67,12 +70,14 @@ zzip_rewinddir(ZZIP_DIR * dir) #define real_readdir(_X_) 1 #else static int -real_readdir(ZZIP_DIR* dir) +real_readdir(ZZIP_DIR * dir) { struct stat st = { 0 }; char filename[PATH_MAX]; - struct dirent* dirent = _zzip_readdir(dir->realdir); - if (! dirent) return 0; + struct dirent *dirent = _zzip_readdir(dir->realdir); + + if (! dirent) + return 0; dir->dirent.d_name = dirent->d_name; strcpy(filename, dir->realname); @@ -89,16 +94,16 @@ real_readdir(ZZIP_DIR* dir) if (! S_ISREG(st.st_mode)) { dir->dirent.d_compr = st.st_mode; - dir->dirent.d_compr |= 0x80000000; - /* makes it effectively negative, - * but can still be fed to S_ISXXX(x) */ - }else + dir->dirent.d_compr |= 0x80000000; + /* makes it effectively negative, + * but can still be fed to S_ISXXX(x) */ + } else { - dir->dirent.d_compr = 0; /* stored */ + dir->dirent.d_compr = 0; /* stored */ } - }else + } else { - dir->dirent.d_compr = 0; /* stored */ + dir->dirent.d_compr = 0; /* stored */ } return 1; @@ -114,28 +119,32 @@ real_readdir(ZZIP_DIR* dir) * a ZZIP_DIRENT are d_name (the file name), d_compr (compression), d_csize * (compressed size), st_size (uncompressed size). */ -ZZIP_DIRENT* +ZZIP_DIRENT * zzip_readdir(ZZIP_DIR * dir) { - if (! dir) { errno=EBADF; return 0; } + if (! dir) + { errno=EBADF; return 0; } if (USE_DIRENT && dir->realdir) { if (! real_readdir(dir)) return 0; - }else + } else { - if (! dir->hdr) return 0; + if (! dir->hdr) + return 0; - dir->dirent.d_name = dir->hdr->d_name; + dir->dirent.d_name = dir->hdr->d_name; dir->dirent.d_compr = dir->hdr->d_compr; dir->dirent.d_csize = dir->hdr->d_csize; dir->dirent.st_size = dir->hdr->d_usize; - if (! dir->hdr->d_reclen) dir->hdr = 0; - else dir->hdr = (struct zzip_dir_hdr *) - ((char *)dir->hdr + dir->hdr->d_reclen); + if (! dir->hdr->d_reclen) + dir->hdr = 0; + else + dir->hdr = (struct zzip_dir_hdr *) + ((char *) dir->hdr + dir->hdr->d_reclen); } return &dir->dirent; } @@ -143,17 +152,18 @@ zzip_readdir(ZZIP_DIR * dir) /** => zzip_rewinddir * This function is the equivalent of => telldir(2) for a realdir or zipfile. */ -zzip_off_t -zzip_telldir(ZZIP_DIR* dir) +zzip_off_t +zzip_telldir(ZZIP_DIR * dir) { - if (! dir) { errno=EBADF; return -1; } + if (! dir) + { errno=EBADF; return -1; } if (USE_DIRENT && dir->realdir) { return _zzip_telldir(dir->realdir); - }else + } else { - return ((zzip_off_t) ((char*) dir->hdr - (char*) dir->hdr0)); + return ((zzip_off_t) ((char *) dir->hdr - (char *) dir->hdr0)); } } @@ -161,40 +171,45 @@ zzip_telldir(ZZIP_DIR* dir) * This function is the equivalent of => seekdir(2) for a realdir or zipfile. */ void -zzip_seekdir(ZZIP_DIR* dir, zzip_off_t offset) +zzip_seekdir(ZZIP_DIR * dir, zzip_off_t offset) { - if (! dir) return; - + if (! dir) + return; + if (USE_DIRENT && dir->realdir) { _zzip_seekdir(dir->realdir, offset); - }else + } else { - dir->hdr = (struct zzip_dir_hdr*) - (dir->hdr0 ? (char*) dir->hdr0 + (size_t) offset : 0); + dir->hdr = (struct zzip_dir_hdr *) + (dir->hdr0 ? (char *) dir->hdr0 + (size_t) offset : 0); } } #if defined ZZIP_LARGEFILE_RENAME && defined EOVERFLOW && defined PIC -#undef zzip_seekdir /* zzip_seekdir64 */ -#undef zzip_telldir /* zzip_telldir64 */ +#undef zzip_seekdir /* zzip_seekdir64 */ +#undef zzip_telldir /* zzip_telldir64 */ -long zzip_telldir(ZZIP_DIR* dir); -void zzip_seekdir(ZZIP_DIR* dir, long offset); +long zzip_telldir(ZZIP_DIR * dir); +void zzip_seekdir(ZZIP_DIR * dir, long offset); /* DLL compatibility layer - so that 32bit code can link with this lib too */ -long zzip_telldir(ZZIP_DIR* dir) -{ - off_t off = zzip_telldir64 (dir); +long +zzip_telldir(ZZIP_DIR * dir) +{ + off_t off = zzip_telldir64(dir); long offs = off; - if (offs != off) { errno = EOVERFLOW; return -1; } + + if (offs != off) + { errno = EOVERFLOW; return -1; } return offs; } -void zzip_seekdir(ZZIP_DIR* dir, long offset) -{ - zzip_seekdir64 (dir, offset); +void +zzip_seekdir(ZZIP_DIR * dir, long offset) +{ + zzip_seekdir64(dir, offset); } #endif @@ -208,58 +223,62 @@ void zzip_seekdir(ZZIP_DIR* dir, long offset) * * the error-code is mapped to => errno(3). */ -ZZIP_DIR* -zzip_opendir(zzip_char_t* filename) +ZZIP_DIR * +zzip_opendir(zzip_char_t * filename) { - return zzip_opendir_ext_io (filename, 0, 0, 0); + return zzip_opendir_ext_io(filename, 0, 0, 0); } /** => zzip_opendir * This function uses explicit ext and io instead of the internal * defaults, setting them to zero is equivalent to => zzip_opendir */ -ZZIP_DIR* -zzip_opendir_ext_io(zzip_char_t* filename, int o_modes, - zzip_strings_t* ext, zzip_plugin_io_t io) +ZZIP_DIR * +zzip_opendir_ext_io(zzip_char_t * filename, int o_modes, + zzip_strings_t * ext, zzip_plugin_io_t io) { zzip_error_t e; - ZZIP_DIR* dir; + ZZIP_DIR *dir; # ifdef ZZIP_HAVE_SYS_STAT_H struct stat st; # endif - if (o_modes & (ZZIP_PREFERZIP|ZZIP_ONLYZIP)) goto try_zzip; - try_real: + if (o_modes & (ZZIP_PREFERZIP | ZZIP_ONLYZIP)) + goto try_zzip; + try_real: # ifdef ZZIP_HAVE_SYS_STAT_H - if (stat(filename, &st) >= 0 && S_ISDIR(st.st_mode) - ){ - if (USE_DIRENT) - { - _zzip_DIR* realdir = _zzip_opendir(filename); - if (realdir) - { - if (! (dir = (ZZIP_DIR *)calloc(1, sizeof (*dir)))) - { - _zzip_closedir(realdir); - return 0; - }else - { - dir->realdir = realdir; - dir->realname = strdup(filename); - return dir; - } - } + if (stat(filename, &st) >= 0 && S_ISDIR(st.st_mode)) + { + if (USE_DIRENT) + { + _zzip_DIR *realdir = _zzip_opendir(filename); + + if (realdir) + { + if (! (dir = (ZZIP_DIR *) calloc(1, sizeof(*dir)))) + { + _zzip_closedir(realdir); + return 0; + } else + { + dir->realdir = realdir; + dir->realname = strdup(filename); + return dir; + } + } } return 0; } # endif /* HAVE_SYS_STAT_H */ - try_zzip: - dir = zzip_dir_open_ext_io (filename, &e, ext, io); - if (! dir && (o_modes & ZZIP_PREFERZIP)) goto try_real; - if (e) errno = zzip_errno(e); + try_zzip: + dir = zzip_dir_open_ext_io(filename, &e, ext, io); + if (! dir && (o_modes & ZZIP_PREFERZIP)) + goto try_real; + if (e) + errno = zzip_errno(e); return dir; } @@ -272,9 +291,10 @@ zzip_opendir_ext_io(zzip_char_t* filename, int o_modes, * to => zzip_dir_close which will free the ZZIP_DIR structure. */ int -zzip_closedir(ZZIP_DIR* dir) +zzip_closedir(ZZIP_DIR * dir) { - if (! dir) { errno = EBADF; return -1; } + if (! dir) + { errno = EBADF; return -1; } if (USE_DIRENT && dir->realdir) { @@ -282,7 +302,7 @@ zzip_closedir(ZZIP_DIR* dir) free(dir->realname); free(dir); return 0; - }else + } else { zzip_dir_close(dir); return 0; diff --git a/zzip/err.c b/zzip/err.c index 5d0ec26..1ba869b 100644 --- a/zzip/err.c +++ b/zzip/err.c @@ -1,3 +1,4 @@ + /* * Author: * Guido Draheim @@ -11,7 +12,7 @@ * of the Mozilla Public License 1.1 */ -#include /* exported... */ +#include /* exported... */ #include #include @@ -53,29 +54,30 @@ errlist[] = * or it can be an error code from => libzzip, which is an * negative value lower than => ZZIP_ERROR */ -zzip_char_t* +zzip_char_t * zzip_strerror(int errcode) { - if (errcode < ZZIP_ERROR && errcode > ZZIP_ERROR-32) - { - struct errlistentry* err = errlist; - for (; err->mesg ; err++) - { - if (err->code == errcode) - return err->mesg; - } - errcode = EINVAL; - } - - if (errcode < 0) - { - if (errcode == -1) - return strerror(errcode); - else - return zError(errcode); - } - - return strerror (errcode); + if (errcode < ZZIP_ERROR && errcode > ZZIP_ERROR - 32) + { + struct errlistentry *err = errlist; + + for (; err->mesg; err++) + { + if (err->code == errcode) + return err->mesg; + } + errcode = EINVAL; + } + + if (errcode < 0) + { + if (errcode == -1) + return strerror(errcode); + else + return zError(errcode); + } + + return strerror(errcode); } /** => zzip_strerror @@ -83,10 +85,11 @@ zzip_strerror(int errcode) * runs it through => zzip_strerror to obtain the static string * describing the error. */ -zzip_char_t* -zzip_strerror_of(ZZIP_DIR* dir) +zzip_char_t * +zzip_strerror_of(ZZIP_DIR * dir) { - if (! dir) return strerror (errno); + if (! dir) + return strerror(errno); return zzip_strerror(dir->errcode); } @@ -142,16 +145,20 @@ errnolist[] = int zzip_errno(int errcode) { - if (errcode >= -1) return errno; - - { struct errnolistentry* err = errnolist; - for (; err->code ; err++) + if (errcode >= -1) + { + return errno; + } else + { + struct errnolistentry *err = errnolist; + + for (; err->code; err++) { - if (err->code == errcode) - return err->e_no; + if (err->code == errcode) + return err->e_no; } + return EINVAL; } - return EINVAL; } /* diff --git a/zzip/fetch.c b/zzip/fetch.c index 4ae24cc..b1d9808 100644 --- a/zzip/fetch.c +++ b/zzip/fetch.c @@ -1,3 +1,4 @@ + /* * These routines are helpers - note that this file gets linked into all the * zip-access variants that refer to . On the x86 platform the @@ -33,32 +34,35 @@ * Make 32 bit value in host byteorder from little-endian mapped octet-data * (works also on machines which SIGBUS on misaligned data access (eg. 68000)) */ -uint32_t __zzip_get32(unsigned char * s) +uint32_t +__zzip_get32(unsigned char *s) { #if defined __ZZIP_GET32 return __ZZIP_GET32(s); #else - return ((uint32_t)s[3] << 24) | ((uint32_t)s[2] << 16) - | ((uint32_t)s[1] << 8) | ((uint32_t)s[0]); + return ((uint32_t) s[3] << 24) | ((uint32_t) s[2] << 16) + | ((uint32_t) s[1] << 8) | ((uint32_t) s[0]); #endif } /** => __zzip_get32 * This function does the same for a 16 bit value. */ -uint16_t __zzip_get16(unsigned char * s) +uint16_t +__zzip_get16(unsigned char *s) { #if defined __ZZIP_GET16 return __ZZIP_GET16(s); #else - return ((uint16_t)s[1] << 8) | ((uint16_t)s[0]); + return ((uint16_t) s[1] << 8) | ((uint16_t) s[0]); #endif } /** => __zzip_get32 * This function does the same for an off64_t value. */ -uint64_t __zzip_get64(unsigned char * s) +uint64_t +__zzip_get64(unsigned char *s) { #ifdef __GNUC__ /* *INDENT-OFF* */ @@ -73,17 +77,18 @@ uint64_t __zzip_get64(unsigned char * s) v |= s[0]; return v; /* *INDENT-ON* */ #else - return ((uint64_t)s[7] << 56) | ((uint64_t)s[6] << 48) - | ((uint64_t)s[5] << 40) | ((uint64_t)s[4] << 32) - | ((uint64_t)s[3] << 24) | ((uint64_t)s[2] << 16) - | ((uint64_t)s[1] << 8) | ((uint64_t)s[0]); + return ((uint64_t) s[7] << 56) | ((uint64_t) s[6] << 48) + | ((uint64_t) s[5] << 40) | ((uint64_t) s[4] << 32) + | ((uint64_t) s[3] << 24) | ((uint64_t) s[2] << 16) + | ((uint64_t) s[1] << 8) | ((uint64_t) s[0]); #endif } /** => __zzip_get32 * This function pushes a 32bit value at the specified address */ -void __zzip_set32(unsigned char * s, uint32_t v) +void +__zzip_set32(unsigned char *s, uint32_t v) { #if defined __ZZIP_SET32 return __ZZIP_SET32(s, v); @@ -100,7 +105,8 @@ void __zzip_set32(unsigned char * s, uint32_t v) /** => __zzip_get32 * This function does the same for a 16 bit value. */ -void __zzip_set16(unsigned char * s, uint16_t v) +void +__zzip_set16(unsigned char *s, uint16_t v) { #if defined __ZZIP_SET16 return __ZZIP_SET16(s, v); @@ -115,7 +121,8 @@ void __zzip_set16(unsigned char * s, uint16_t v) /** => __zzip_get32 * This function pushes a off64_t value at the specified address */ -void __zzip_set64(unsigned char * s, uint64_t v) +void +__zzip_set64(unsigned char *s, uint64_t v) { /* *INDENT-OFF* */ s[0] = (unsigned char) (v); v >>= 8; diff --git a/zzip/file.c b/zzip/file.c index acdd8c3..4e7abcb 100644 --- a/zzip/file.c +++ b/zzip/file.c @@ -1,3 +1,4 @@ + /* * Author: * Guido Draheim @@ -11,7 +12,7 @@ * of the Mozilla Public License 1.1 */ -#include /* exported...*/ +#include /* exported... */ #include #include @@ -26,7 +27,7 @@ #if 0 # if defined ZZIP_HAVE_IO_H -# include /* tell */ +# include /* tell */ # else # define tell(fd) lseek(fd,0,SEEK_CUR) # endif @@ -43,50 +44,56 @@ * * the .refcount is decreased and if zero the fp->dir is closed just as well. */ -int +int zzip_file_close(ZZIP_FILE * fp) { auto int self; - ZZIP_DIR * dir = fp->dir; - + ZZIP_DIR *dir = fp->dir; + if (fp->method) - inflateEnd(&fp->d_stream); /* inflateEnd() can be called many times */ + inflateEnd(&fp->d_stream); /* inflateEnd() can be called many times */ - if (dir->cache.locked == NULL) - dir->cache.locked = &self; + if (dir->cache.locked == NULL) + dir->cache.locked = &self; if (fp->buf32k) { - if (dir->cache.locked == &self && - dir->cache.buf32k == NULL) dir->cache.buf32k = fp->buf32k; - else free(fp->buf32k); + if (dir->cache.locked == &self && dir->cache.buf32k == NULL) + dir->cache.buf32k = fp->buf32k; + else + free(fp->buf32k); } if (dir->currentfp == fp) dir->currentfp = NULL; - + dir->refcount--; /* ease to notice possible dangling reference errors */ - memset(fp, 0, sizeof(*fp)); + memset(fp, 0, sizeof(*fp)); + + if (dir->cache.locked == &self && dir->cache.fp == NULL) + dir->cache.fp = fp; + else + free(fp); - if (dir->cache.locked == &self && - dir->cache.fp == NULL) dir->cache.fp = fp; - else free(fp); + if (dir->cache.locked == &self) + dir->cache.locked = NULL; - if (dir->cache.locked == &self) - dir->cache.locked = NULL; - - if (! dir->refcount) return zzip_dir_close(dir); else return 0; + if (! dir->refcount) + return zzip_dir_close(dir); + else + return 0; } - -static int + +static int zzip_file_saveoffset(ZZIP_FILE * fp) { if (fp) { int fd = fp->dir->fd; zzip_off_t off = fp->io->fd.seeks(fd, 0, SEEK_CUR); + if (off < 0) return -1; @@ -95,8 +102,8 @@ zzip_file_saveoffset(ZZIP_FILE * fp) return 0; } -# ifndef ZZIP_CHECK_BACKSLASH_DIRSEPARATOR /* NOTE: also default */ -# define ZZIP_CHECK_BACKSLASH_DIRSEPARATOR 0 /* to "NO" on win32 ! */ +# ifndef ZZIP_CHECK_BACKSLASH_DIRSEPARATOR /* NOTE: also default */ +# define ZZIP_CHECK_BACKSLASH_DIRSEPARATOR 0 /* to "NO" on win32 ! */ # endif # if ! defined strcasecmp && ! defined ZZIP_HAVE_STRCASECMP @@ -109,32 +116,38 @@ zzip_file_saveoffset(ZZIP_FILE * fp) #else #define dirsep_strrchr(N,C) _dirsep_strrchr(N) #define dirsep_casecmp _dirsep_casecmp -static zzip_char_t* -_dirsep_strrchr (zzip_char_t* name) +static zzip_char_t * +_dirsep_strrchr(zzip_char_t * name) { - char* n = strrchr (name, '/'); - char* m = strrchr (name, '\\'); - if (m && n && m > n) n = m; + char *n = strrchr(name, '/'); + char *m = strrchr(name, '\\'); + + if (m && n && m > n) + n = m; return n; } static int -_dirsep_casecmp (zzip_char_t* s1, zzip_char_t* s2) +_dirsep_casecmp(zzip_char_t * s1, zzip_char_t * s2) { /* ASCII tolower - including mapping of backslash in normal slash */ static const char mapping[] = "@abcdefghijklmnopqrstuvwxyz[/]^_"; int c1, c2; + while (*s1 && *s2) { - c1 = (int)(unsigned char) *s1; - c2 = (int)(unsigned char) *s2; - if ((c1&0xE0) == 0x40) c1 = mapping[c1&0x1f]; - if ((c2&0xE0) == 0x40) c2 = mapping[c2&0x1f]; - if (c1 != c2) - return (c1 - c2); - s1++; s2++; + c1 = (int) (unsigned char) *s1; + c2 = (int) (unsigned char) *s2; + if ((c1 & 0xE0) == 0x40) + c1 = mapping[c1 & 0x1f]; + if ((c2 & 0xE0) == 0x40) + c2 = mapping[c2 & 0x1f]; + if (c1 != c2) + return (c1 - c2); + s1++; + s2++; } - return (((int)(unsigned char) *s1) - ((int)(unsigned char) *s2)); + return (((int) (unsigned char) *s1) - ((int) (unsigned char) *s2)); } #endif @@ -150,84 +163,93 @@ static int zzip_inflate_init(ZZIP_FILE *, struct zzip_dir_hdr *); * * On error it returns null and sets errcode in the ZZIP_DIR. */ -ZZIP_FILE * -zzip_file_open(ZZIP_DIR * dir, zzip_char_t* name, int o_mode) +ZZIP_FILE * +zzip_file_open(ZZIP_DIR * dir, zzip_char_t * name, int o_mode) { auto int self; zzip_error_t err = 0; - struct zzip_file * fp = 0; - struct zzip_dir_hdr * hdr = dir->hdr0; - int (*cmp)(zzip_char_t*, zzip_char_t*); - - cmp = (o_mode & ZZIP_CASELESS)? dirsep_casecmp: strcmp; + struct zzip_file *fp = 0; + struct zzip_dir_hdr *hdr = dir->hdr0; + int (*cmp) (zzip_char_t *, zzip_char_t *); + + cmp = (o_mode & ZZIP_CASELESS) ? dirsep_casecmp : strcmp; - if (! dir) return NULL; - if (! dir->fd || dir->fd == -1) { dir->errcode = EBADF; return NULL; } - if (! hdr) { dir->errcode = ENOENT; return NULL; } + if (! dir) + return NULL; + if (! dir->fd || dir->fd == -1) + { dir->errcode = EBADF; return NULL; } + if (! hdr) + { dir->errcode = ENOENT; return NULL; } if (o_mode & ZZIP_NOPATHS) { - register zzip_char_t* n = dirsep_strrchr(name, '/'); - if (n) name = n + 1; + register zzip_char_t *n = dirsep_strrchr(name, '/'); + + if (n) + name = n + 1; } while (1) { - register zzip_char_t* hdr_name = hdr->d_name; + register zzip_char_t *hdr_name = hdr->d_name; + if (o_mode & ZZIP_NOPATHS) { - register zzip_char_t* n = dirsep_strrchr(hdr_name, '/'); - if (n) hdr_name = n + 1; + register zzip_char_t *n = dirsep_strrchr(hdr_name, '/'); + + if (n) + hdr_name = n + 1; } - HINT4("name='%s', compr=%d, size=%d\n", - hdr->d_name, hdr->d_compr, hdr->d_usize); + HINT4("name='%s', compr=%d, size=%d\n", + hdr->d_name, hdr->d_compr, hdr->d_usize); if (! cmp(hdr_name, name)) { switch (hdr->d_compr) { - case 0: /* store */ - case 8: /* inflate */ + case 0: /* store */ + case 8: /* inflate */ break; default: { err = ZZIP_UNSUPP_COMPR; goto error; } } - if (dir->cache.locked == NULL) - dir->cache.locked = &self; + if (dir->cache.locked == NULL) + dir->cache.locked = &self; - if (dir->cache.locked == &self && - dir->cache.fp) + if (dir->cache.locked == &self && dir->cache.fp) { - fp = dir->cache.fp; dir->cache.fp = NULL; + fp = dir->cache.fp; + dir->cache.fp = NULL; /* memset(zfp, 0, sizeof *fp); cleared in zzip_file_close() */ - }else + } else { - if (! (fp = (ZZIP_FILE *)calloc(1, sizeof(*fp)))) + if (! (fp = (ZZIP_FILE *) calloc(1, sizeof(*fp)))) { err = ZZIP_OUTOFMEM; goto error; } } fp->dir = dir; fp->io = dir->io; dir->refcount++; - - if (dir->cache.locked == &self && - dir->cache.buf32k) - { fp->buf32k = dir->cache.buf32k; dir->cache.buf32k = NULL; } - else + + if (dir->cache.locked == &self && dir->cache.buf32k) + { + fp->buf32k = dir->cache.buf32k; + dir->cache.buf32k = NULL; + } else { - if (! (fp->buf32k = (char *)malloc(ZZIP_32K))) + if (! (fp->buf32k = (char *) malloc(ZZIP_32K))) { err = ZZIP_OUTOFMEM; goto error; } } - if (dir->cache.locked == &self) - dir->cache.locked = NULL; + if (dir->cache.locked == &self) + dir->cache.locked = NULL; /* * In order to support simultaneous open files in one zip archive * we'll fix the fd offset when opening new file/changing which * file to read... - */ + */ if (zzip_file_saveoffset(dir->currentfp) < 0) { err = ZZIP_DIR_SEEK; goto error; } @@ -238,42 +260,44 @@ zzip_file_open(ZZIP_DIR * dir, zzip_char_t* name, int o_mode) if (dir->io->fd.seeks(dir->fd, hdr->d_off, SEEK_SET) < 0) { err = ZZIP_DIR_SEEK; goto error; } - { /* skip local header - should test tons of other info, - * but trust that those are correct */ + { /* skip local header - should test tons of other info, + * but trust that those are correct */ zzip_ssize_t dataoff; - struct zzip_file_header * p = (void*) fp->buf32k; + struct zzip_file_header *p = (void *) fp->buf32k; - dataoff = dir->io->fd.read(dir->fd, (void*)p, sizeof(*p)); - if (dataoff < (zzip_ssize_t)sizeof(*p)) - { err = ZZIP_DIR_READ; goto error; } - if (! zzip_file_header_check_magic(p)) /* PK\3\4 */ - { err = ZZIP_CORRUPTED; goto error; } + dataoff = dir->io->fd.read(dir->fd, (void *) p, sizeof(*p)); + if (dataoff < (zzip_ssize_t) sizeof(*p)) + { err = ZZIP_DIR_READ; goto error; } + if (! zzip_file_header_check_magic(p)) /* PK\3\4 */ + { err = ZZIP_CORRUPTED; goto error; } dataoff = zzip_file_header_sizeof_tail(p); - + if (dir->io->fd.seeks(dir->fd, dataoff, SEEK_CUR) < 0) - { err = ZZIP_DIR_SEEK; goto error; } + { err = ZZIP_DIR_SEEK; goto error; } fp->dataoffset = dir->io->fd.tells(dir->fd); fp->usize = hdr->d_usize; fp->csize = hdr->d_csize; } - err = zzip_inflate_init (fp, hdr); - if (err) { goto error; } - + err = zzip_inflate_init(fp, hdr); + if (err) + goto error; + return fp; - }else + } else { if (hdr->d_reclen == 0) break; - hdr = (struct zzip_dir_hdr *)((char *)hdr + hdr->d_reclen); - }/*cmp name*/ - }/*forever*/ + hdr = (struct zzip_dir_hdr *) ((char *) hdr + hdr->d_reclen); + } /*cmp name */ + } /*forever */ dir->errcode = ZZIP_ENOENT; return NULL; -error: - if (fp) zzip_file_close(fp); + error: + if (fp) + zzip_file_close(fp); dir->errcode = err; return NULL; } @@ -282,25 +306,28 @@ error: * call => inflateInit and setup fp's iterator variables, * used by lowlevel => _open functions. */ -static int -zzip_inflate_init(ZZIP_FILE * fp, struct zzip_dir_hdr* hdr) +static int +zzip_inflate_init(ZZIP_FILE * fp, struct zzip_dir_hdr *hdr) { int err; + fp->method = hdr->d_compr; fp->restlen = hdr->d_usize; - + if (fp->method) { memset(&fp->d_stream, 0, sizeof(fp->d_stream)); - + err = inflateInit2(&fp->d_stream, -MAX_WBITS); - if (err != Z_OK) { goto error; } + if (err != Z_OK) + goto error; fp->crestlen = hdr->d_csize; } return 0; -error: - if (fp) zzip_file_close(fp); + error: + if (fp) + zzip_file_close(fp); return err; } @@ -310,21 +337,23 @@ error: * If the ZZIP_FILE wraps a normal stat'fd then it is just that int'fd * that is being closed and the otherwise empty ZZIP_FILE gets freed. */ -int +int zzip_fclose(ZZIP_FILE * fp) { - if (! fp) return 0; - if (! fp->dir) - { int r = fp->io->fd.close(fp->fd); free(fp); return r; } /* stat fd */ - else return zzip_file_close(fp); + if (! fp) + return 0; + if (! fp->dir) + { int r = fp->io->fd.close(fp->fd); free(fp); return r; } /* stat fd */ + else + return zzip_file_close(fp); } /** => zzip_fclose */ -int -zzip_close(ZZIP_FILE* fp) +int +zzip_close(ZZIP_FILE * fp) { - return zzip_fclose (fp); + return zzip_fclose(fp); } /** @@ -341,14 +370,15 @@ zzip_close(ZZIP_FILE* fp) * required just that but the latest zlib would work just fine with * a smaller buffer. */ -zzip_ssize_t -zzip_file_read(ZZIP_FILE * fp, void * buf, zzip_size_t len) +zzip_ssize_t +zzip_file_read(ZZIP_FILE * fp, void *buf, zzip_size_t len) { - ZZIP_DIR * dir; + ZZIP_DIR *dir; zzip_size_t l; zzip_ssize_t rv; - - if (! fp || ! fp->dir) return 0; + + if (! fp || ! fp->dir) + return 0; dir = fp->dir; l = fp->restlen > len ? len : fp->restlen; @@ -361,64 +391,65 @@ zzip_file_read(ZZIP_FILE * fp, void * buf, zzip_size_t len) */ if (dir->currentfp != fp) { - if (zzip_file_saveoffset(dir->currentfp) < 0 - || fp->io->fd.seeks(dir->fd, fp->offset, SEEK_SET) < 0) - { dir->errcode = ZZIP_DIR_SEEK; return -1; } + if (zzip_file_saveoffset(dir->currentfp) < 0 + || fp->io->fd.seeks(dir->fd, fp->offset, SEEK_SET) < 0) + { dir->errcode = ZZIP_DIR_SEEK; return -1; } else - { dir->currentfp = fp; } + { dir->currentfp = fp; } } - + /* if more methods is to be supported, change this to `switch ()' */ - if (fp->method) /* method != 0 == 8, inflate */ + if (fp->method) /* method != 0 == 8, inflate */ { fp->d_stream.avail_out = l; - fp->d_stream.next_out = (unsigned char *)buf; + fp->d_stream.next_out = (unsigned char *) buf; - do { + do + { int err; zzip_size_t startlen; if (fp->crestlen > 0 && fp->d_stream.avail_in == 0) { - zzip_size_t cl = ( fp->crestlen < ZZIP_32K ? - fp->crestlen : ZZIP_32K ); - /* zzip_size_t cl = fp->crestlen > 128 ? 128 : fp->crestlen; */ + zzip_size_t cl = (fp->crestlen < ZZIP_32K ? + fp->crestlen : ZZIP_32K); + /* zzip_size_t cl = fp->crestlen > 128 ? 128 : fp->crestlen; */ zzip_ssize_t i = fp->io->fd.read(dir->fd, fp->buf32k, cl); + if (i <= 0) { - dir->errcode = ZZIP_DIR_READ; /* or ZZIP_DIR_READ_EOF ? */ + dir->errcode = ZZIP_DIR_READ; /* or ZZIP_DIR_READ_EOF ? */ return -1; } fp->crestlen -= i; fp->d_stream.avail_in = i; - fp->d_stream.next_in = (unsigned char *)fp->buf32k; + fp->d_stream.next_in = (unsigned char *) fp->buf32k; } startlen = fp->d_stream.total_out; err = inflate(&fp->d_stream, Z_NO_FLUSH); - if (err == Z_STREAM_END) - { fp->restlen = 0; } - else - if (err == Z_OK) - { fp->restlen -= (fp->d_stream.total_out - startlen); } + if (err == Z_STREAM_END) + { fp->restlen = 0; } + else if (err == Z_OK) + { fp->restlen -= (fp->d_stream.total_out - startlen); } else - { dir->errcode = err; return -1; } - } while (fp->restlen && fp->d_stream.avail_out); + { dir->errcode = err; return -1; } + } + while (fp->restlen && fp->d_stream.avail_out); return l - fp->d_stream.avail_out; - }else - { /* method == 0 -- unstore */ + } else + { /* method == 0 -- unstore */ rv = fp->io->fd.read(dir->fd, buf, l); if (rv > 0) { fp->restlen-= rv; } - else - if (rv < 0) + else if (rv < 0) { dir->errcode = ZZIP_DIR_READ; } return rv; } -} +} /** * This function will read(2) data from a real/zipped file. @@ -432,15 +463,19 @@ zzip_file_read(ZZIP_FILE * fp, void * buf, zzip_size_t len) * to decompress the data stream and any error is mapped to => errno(3). */ zzip_ssize_t -zzip_read(ZZIP_FILE * fp, void * buf, zzip_size_t len) +zzip_read(ZZIP_FILE * fp, void *buf, zzip_size_t len) { - if (! fp) return 0; - if (! fp->dir) - { return fp->io->fd.read(fp->fd, buf, len); } /* stat fd */ + if (! fp) + return 0; + if (! fp->dir) + { return fp->io->fd.read(fp->fd, buf, len); } /* stat fd */ else - { register zzip_ssize_t v; + { + register zzip_ssize_t v; + v = zzip_file_read(fp, buf, len); - if (v == -1) { errno = zzip_errno(fp->dir->errcode); } + if (v == -1) + { errno = zzip_errno(fp->dir->errcode); } return v; } } @@ -448,10 +483,11 @@ zzip_read(ZZIP_FILE * fp, void * buf, zzip_size_t len) /** => zzip_read */ zzip_size_t -zzip_fread(void *ptr, zzip_size_t size, zzip_size_t nmemb, ZZIP_FILE *file) +zzip_fread(void *ptr, zzip_size_t size, zzip_size_t nmemb, ZZIP_FILE * file) { - if (! size) size=1; - return zzip_read (file, ptr, size*nmemb)/size; + if (! size) + size = 1; + return zzip_read(file, ptr, size * nmemb) / size; } @@ -460,7 +496,7 @@ zzip_fread(void *ptr, zzip_size_t size, zzip_size_t nmemb, ZZIP_FILE *file) #if defined O_SYNC #define ZZIP_SYNC O_SYNC -#else +#else #define ZZIP_SYNC 0 #endif @@ -468,7 +504,7 @@ zzip_fread(void *ptr, zzip_size_t size, zzip_size_t nmemb, ZZIP_FILE *file) #define ZZIP_NONBLOCK O_NONBLOCK #elif defined O_NDELAY #define ZZIP_NOCTTY O_NDELAY -#else +#else #define ZZIP_NOCTTY 0 #endif @@ -521,10 +557,10 @@ zzip_fread(void *ptr, zzip_size_t size, zzip_size_t nmemb, ZZIP_FILE *file) * This function returns a new zzip-handle (use => zzip_close to return * it). On error this function will return null setting => errno(3). */ -ZZIP_FILE* -zzip_fopen(zzip_char_t* filename, zzip_char_t* mode) +ZZIP_FILE * +zzip_fopen(zzip_char_t * filename, zzip_char_t * mode) { - return zzip_freopen (filename, mode, 0); + return zzip_freopen(filename, mode, 0); } /** => zzip_fopen @@ -548,13 +584,15 @@ zzip_fopen(zzip_char_t* filename, zzip_char_t* mode) * * This function returns a new zzip-handle (use => zzip_close to return * it). On error this function will return null setting => errno(3). - */ -ZZIP_FILE* -zzip_freopen(zzip_char_t* filename, zzip_char_t* mode, ZZIP_FILE* stream) + */ +ZZIP_FILE * +zzip_freopen(zzip_char_t * filename, zzip_char_t * mode, ZZIP_FILE * stream) { int o_flags = 0; int o_modes = 0664; - if (! mode) mode = "rb"; + + if (! mode) + mode = "rb"; # ifndef O_BINARY # define O_BINARY 0 @@ -569,10 +607,11 @@ zzip_freopen(zzip_char_t* filename, zzip_char_t* mode, ZZIP_FILE* stream) # define O_NONBLOCK 0 # endif - for(; *mode; mode++) + for (; *mode; mode++) { switch (*mode) { + /* *INDENT-OFF* */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': continue; /* ignore if not attached to other info */ @@ -587,25 +626,26 @@ zzip_freopen(zzip_char_t* filename, zzip_char_t* mode, ZZIP_FILE* stream) case 's': o_flags |= O_SYNC; break; case 'n': o_flags |= O_NONBLOCK; break; case 'o': o_modes &=~ 07; - o_modes |= ((mode[1] - '0'))&07; continue; + o_modes |= ((mode[1] - '0')) & 07; continue; case 'g': o_modes &=~ 070; - o_modes |= ((mode[1] - '0')<<3)&070; continue; + o_modes |= ((mode[1] - '0') << 3) & 070; continue; case 'u': o_modes &=~ 0700; - o_modes |= ((mode[1] - '0')<<6)&0700; continue; + o_modes |= ((mode[1] - '0') << 6) & 0700; continue; case 'q': o_modes |= ZZIP_FACTORY; break; case 'z': /* compression level */ continue; /* currently ignored, just for write mode */ + /* *INDENT-ON* */ } } { - ZZIP_FILE* fp = - zzip_open_shared_io (stream, filename, o_flags, o_modes, 0, 0); + ZZIP_FILE *fp = + zzip_open_shared_io(stream, filename, o_flags, o_modes, 0, 0); - if (! o_modes&ZZIP_FACTORY && stream) - zzip_file_close (stream); + if (! (o_modes & ZZIP_FACTORY) && stream) + zzip_file_close(stream); - return fp; + return fp; } } @@ -636,15 +676,16 @@ zzip_freopen(zzip_char_t* filename, zzip_char_t* mode, ZZIP_FILE* stream) * * compare with => open(2) and => zzip_fopen */ -ZZIP_FILE* -zzip_open(zzip_char_t* filename, int o_flags) +ZZIP_FILE * +zzip_open(zzip_char_t * filename, int o_flags) { /* backward compatibility */ int o_modes = 0664; - if (o_flags & ZZIP_CASEINSENSITIVE) - { o_flags ^= ZZIP_CASEINSENSITIVE; o_modes |= ZZIP_CASELESS; } - if (o_flags & ZZIP_IGNOREPATH) - { o_flags ^= ZZIP_IGNOREPATH; o_modes |= ZZIP_NOPATHS; } + + if (o_flags & ZZIP_CASEINSENSITIVE) + { o_flags ^= ZZIP_CASEINSENSITIVE; o_modes |= ZZIP_CASELESS; } + if (o_flags & ZZIP_IGNOREPATH) + { o_flags ^= ZZIP_IGNOREPATH; o_modes |= ZZIP_NOPATHS; } return zzip_open_ext_io(filename, o_flags, o_modes, 0, 0); } @@ -667,11 +708,11 @@ zzip_open(zzip_char_t* filename, int o_flags) * This function returns a new zzip-handle (use => zzip_close to return * it). On error this function will return null setting => errno(3). */ -ZZIP_FILE* -zzip_open_ext_io(zzip_char_t* filename, int o_flags, int o_modes, - zzip_strings_t* ext, zzip_plugin_io_t io) +ZZIP_FILE * +zzip_open_ext_io(zzip_char_t * filename, int o_flags, int o_modes, + zzip_strings_t * ext, zzip_plugin_io_t io) { - return zzip_open_shared_io (0, filename, o_flags, o_modes, ext, io); + return zzip_open_shared_io(0, filename, o_flags, o_modes, ext, io); } /** => zzip_open @@ -695,144 +736,174 @@ zzip_open_ext_io(zzip_char_t* filename, int o_flags, int o_modes, * This function returns a new zzip-handle (use => zzip_close to return * it). On error this function will return null setting => errno(3). */ -ZZIP_FILE* -zzip_open_shared_io (ZZIP_FILE* stream, - zzip_char_t* filename, int o_flags, int o_modes, - zzip_strings_t* ext, zzip_plugin_io_t io) +ZZIP_FILE * +zzip_open_shared_io(ZZIP_FILE * stream, + zzip_char_t * filename, int o_flags, int o_modes, + zzip_strings_t * ext, zzip_plugin_io_t io) { if (stream && stream->dir) { - if (! ext) ext = stream->dir->fileext; - if (! io) io = stream->dir->io; + if (! ext) + ext = stream->dir->fileext; + if (! io) + io = stream->dir->io; } - if (! io) io = zzip_get_default_io (); + if (! io) + io = zzip_get_default_io(); - if (o_modes & (ZZIP_PREFERZIP|ZZIP_ONLYZIP)) goto try_zzip; - try_real: + if (o_modes & (ZZIP_PREFERZIP | ZZIP_ONLYZIP)) + goto try_zzip; + try_real: /* prefer an existing real file */ - { - zzip_plugin_io_t os = (o_modes & ZZIP_ALLOWREAL) - ? zzip_get_default_io () : io; - int fd = os->fd.open(filename, o_flags); /* io->fd.open */ + { + zzip_plugin_io_t os = (o_modes & ZZIP_ALLOWREAL) + ? zzip_get_default_io() : io; + int fd = os->fd.open(filename, o_flags); /* io->fd.open */ + if (fd != -1) { - ZZIP_FILE* fp = calloc (1, sizeof(ZZIP_FILE)); - if (! fp) { os->fd.close(fd); return 0; } /* io->fd.close */ + ZZIP_FILE *fp = calloc(1, sizeof(ZZIP_FILE)); + + if (! fp) + { os->fd.close(fd); return 0; } /* io->fd.close */ - fp->fd = fd; + fp->fd = fd; fp->io = os; return fp; } - if (o_modes & ZZIP_PREFERZIP) return 0; + if (o_modes & ZZIP_PREFERZIP) + return 0; } - try_zzip: + try_zzip: /* if the user had it in place of a normal xopen, then * we better defend this lib against illegal usage */ - if (o_flags & (O_CREAT|O_WRONLY)) { errno = EINVAL; return 0; } - if (o_flags & (O_RDWR)) { o_flags ^= O_RDWR; o_flags |= O_RDONLY; } + if (o_flags & (O_CREAT | O_WRONLY)) + { errno = EINVAL; return 0; } + if (o_flags & (O_RDWR)) + { o_flags ^= O_RDWR; o_flags |= O_RDONLY; } /* this is just for backward compatibility -and strictly needed to * prepare ourselves for more options and more options later on... */ /*# if (o_modes & ZZIP_CASELESS) { o_flags |= ZZIP_CASEINSENSITIVE; } */ /*# if (o_modes & ZZIP_NOPATHS) { o_flags |= ZZIP_IGNOREPATH; } */ - + /* see if we can open a file that is a zip file */ - { char basename[PATH_MAX]; - char* p; - int filename_len = strlen (filename); - if (filename_len >= PATH_MAX) { errno = ENAMETOOLONG; return 0; } - memcpy (basename, filename, filename_len+1); - - /* see if we can share the same zip directory */ - if (stream && stream->dir && stream->dir->realname) - { - zzip_size_t len = strlen (stream->dir->realname); - if (! memcmp (filename, stream->dir->realname, len) && - filename[len] == '/' && filename[len+1]) - { - ZZIP_FILE* fp = - zzip_file_open (stream->dir, filename+len+1, o_modes); - if (! fp) { errno = zzip_errno (stream->dir->errcode); } - return fp; - } - } - - /* per each slash in filename, check if it there is a zzip around */ - while ((p = strrchr (basename, '/'))) - { - zzip_error_t e = 0; - ZZIP_DIR* dir; - ZZIP_FILE* fp; - int fd; - - *p = '\0'; /* cut at path separator == possible zipfile basename */ - fd = __zzip_try_open (basename, o_flags|O_RDONLY|O_BINARY, ext, io); - if (fd == -1) { continue; } -/* found: */ - /* found zip-file, now try to parse it */ - dir = zzip_dir_fdopen_ext_io(fd, &e, ext, io); - if (e) { errno = zzip_errno(e); io->fd.close(fd); return 0; } + { + char basename[PATH_MAX]; + char *p; + int filename_len = strlen(filename); + + if (filename_len >= PATH_MAX) + { errno = ENAMETOOLONG; return 0; } + memcpy(basename, filename, filename_len + 1); - /* (p - basename) is the lenghtof zzip_dir part of the filename */ - fp = zzip_file_open(dir, filename + (p - basename) +1, o_modes); - if (! fp) { errno = zzip_errno(dir->errcode); } - else { if (! dir->realname) dir->realname = strdup (basename); } + /* see if we can share the same zip directory */ + if (stream && stream->dir && stream->dir->realname) + { + zzip_size_t len = strlen(stream->dir->realname); + + if (! memcmp(filename, stream->dir->realname, len) && + filename[len] == '/' && filename[len + 1]) + { + ZZIP_FILE *fp = + zzip_file_open(stream->dir, filename + len + 1, o_modes); + if (! fp) + { errno = zzip_errno (stream->dir->errcode); } + return fp; + } + } + + /* per each slash in filename, check if it there is a zzip around */ + while ((p = strrchr(basename, '/'))) + { + zzip_error_t e = 0; + ZZIP_DIR *dir; + ZZIP_FILE *fp; + int fd; + + *p = '\0'; /* cut at path separator == possible zipfile basename */ + fd = __zzip_try_open(basename, o_flags | O_RDONLY | O_BINARY, ext, + io); + if (fd == -1) + { continue; } + +/* found: */ + /* found zip-file, now try to parse it */ + dir = zzip_dir_fdopen_ext_io(fd, &e, ext, io); + if (e) + { errno = zzip_errno(e); io->fd.close(fd); return 0; } + + /* (p - basename) is the lenghtof zzip_dir part of the filename */ + fp = zzip_file_open(dir, filename + (p - basename) + 1, o_modes); + if (! fp) + { errno = zzip_errno(dir->errcode); } + else + { if (! dir->realname) dir->realname = strdup (basename); } - zzip_dir_close(dir); - /* note: since (fp) is attached that (dir) will survive */ - /* but (dir) is implicitly closed on next zzip_close(fp) */ + zzip_dir_close(dir); + /* note: since (fp) is attached that (dir) will survive */ + /* but (dir) is implicitly closed on next zzip_close(fp) */ - return fp; - } /*again*/ + return fp; + } /*again */ - if (o_modes & ZZIP_PREFERZIP) goto try_real; - errno = ENOENT; return 0; + if (o_modes & ZZIP_PREFERZIP) + goto try_real; + else + { errno = ENOENT; return 0; } } } #if defined ZZIP_LARGEFILE_RENAME && defined EOVERFLOW && defined PIC -#undef zzip_open_shared_io /* zzip_open_shared_io64 */ -#undef zzip_open_ext_io /* zzip_open_ext_io64 */ -#undef zzip_opendir_ext_io /* zzip_opendir_ext_io64 */ +#undef zzip_open_shared_io /* zzip_open_shared_io64 */ +#undef zzip_open_ext_io /* zzip_open_ext_io64 */ +#undef zzip_opendir_ext_io /* zzip_opendir_ext_io64 */ _zzip_export -ZZIP_FILE * zzip_open_shared_io(ZZIP_FILE* stream, - zzip_char_t* name, int o_flags, int o_modes, - zzip_strings_t* ext, zzip_plugin_io_t io); -_zzip_export -ZZIP_FILE * zzip_open_ext_io(zzip_char_t* name, int o_flags, int o_modes, - zzip_strings_t* ext, zzip_plugin_io_t io); -_zzip_export -ZZIP_DIR * zzip_opendir_ext_io(zzip_char_t* name, int o_modes, - zzip_strings_t* ext, zzip_plugin_io_t io); + ZZIP_FILE * zzip_open_shared_io(ZZIP_FILE * stream, + zzip_char_t * name, int o_flags, + int o_modes, zzip_strings_t * ext, + zzip_plugin_io_t io); +_zzip_export ZZIP_FILE *zzip_open_ext_io(zzip_char_t * name, int o_flags, + int o_modes, zzip_strings_t * ext, + zzip_plugin_io_t io); +_zzip_export ZZIP_DIR *zzip_opendir_ext_io(zzip_char_t * name, int o_modes, + zzip_strings_t * ext, + zzip_plugin_io_t io); /* DLL compatibility layer - so that 32bit code can link with this lib too */ _zzip_export -ZZIP_FILE * zzip_open_shared_io(ZZIP_FILE* stream, - zzip_char_t* name, int o_flags, int o_modes, - zzip_strings_t* ext, zzip_plugin_io_t io) + ZZIP_FILE * zzip_open_shared_io(ZZIP_FILE * stream, + zzip_char_t * name, int o_flags, + int o_modes, zzip_strings_t * ext, + zzip_plugin_io_t io) { - if (! io) return zzip_open_shared_io64 (stream, name, o_flags, o_modes, - ext, io); - errno = EOVERFLOW; return NULL; + if (! io) + return zzip_open_shared_io64(stream, name, o_flags, o_modes, ext, io); + errno = EOVERFLOW; + return NULL; } _zzip_export -ZZIP_FILE * zzip_open_ext_io(zzip_char_t* name, int o_flags, int o_modes, - zzip_strings_t* ext, zzip_plugin_io_t io) + ZZIP_FILE * zzip_open_ext_io(zzip_char_t * name, int o_flags, int o_modes, + zzip_strings_t * ext, zzip_plugin_io_t io) { - if (! io) return zzip_open_ext_io64 (name, o_flags, o_modes, ext, io); - errno = EOVERFLOW; return NULL; + if (! io) + return zzip_open_ext_io64(name, o_flags, o_modes, ext, io); + errno = EOVERFLOW; + return NULL; } _zzip_export -ZZIP_DIR * zzip_opendir_ext_io(zzip_char_t* name, int o_modes, - zzip_strings_t* ext, zzip_plugin_io_t io) + ZZIP_DIR * zzip_opendir_ext_io(zzip_char_t * name, int o_modes, + zzip_strings_t * ext, zzip_plugin_io_t io) { - if (! io) return zzip_opendir_ext_io64 (name, o_modes, ext, io); - errno = EOVERFLOW; return NULL; + if (! io) + return zzip_opendir_ext_io64(name, o_modes, ext, io); + else + { errno = EOVERFLOW; return NULL; } } #endif /* ZZIP_LARGEFILE_RENAME && EOVERFLOW && PIC */ @@ -846,7 +917,7 @@ ZZIP_DIR * zzip_opendir_ext_io(zzip_char_t* name, int o_modes, * or the beginning of the file for a stat'fd. */ int -zzip_rewind(ZZIP_FILE *fp) +zzip_rewind(ZZIP_FILE * fp) { ZZIP_DIR *dir; int err; @@ -854,46 +925,48 @@ zzip_rewind(ZZIP_FILE *fp) if (! fp) return -1; - if (! fp->dir) - { /* stat fd */ - fp->io->fd.seeks(fp->fd,0,SEEK_SET); + if (! fp->dir) + { /* stat fd */ + fp->io->fd.seeks(fp->fd, 0, SEEK_SET); return 0; } - + dir = fp->dir; /* * If this is other handle than previous, save current seek pointer */ - if (dir->currentfp != fp) + if (dir->currentfp != fp) { if (zzip_file_saveoffset(dir->currentfp) < 0) - { dir->errcode = ZZIP_DIR_SEEK; return -1; } + { dir->errcode = ZZIP_DIR_SEEK; return -1; } else - { dir->currentfp = fp; } + { dir->currentfp = fp; } } - + /* seek to beginning of this file */ if (fp->io->fd.seeks(dir->fd, fp->dataoffset, SEEK_SET) < 0) return -1; - + /* reset the inflate init stuff */ fp->restlen = fp->usize; fp->offset = fp->dataoffset; - - if (fp->method) - { /* method == 8, deflate */ + + if (fp->method) + { /* method == 8, deflate */ err = inflateReset(&fp->d_stream); - if (err != Z_OK) { goto error; } + if (err != Z_OK) + goto error; - /* start over at next inflate with a fresh read() */ - fp->d_stream.avail_in = 0; + /* start over at next inflate with a fresh read() */ + fp->d_stream.avail_in = 0; fp->crestlen = fp->csize; } return 0; - - error: - if (fp) zzip_file_close(fp); + + error: + if (fp) + zzip_file_close(fp); return err; } @@ -916,100 +989,105 @@ zzip_seek(ZZIP_FILE * fp, zzip_off_t offset, int whence) { zzip_off_t cur_pos, rel_ofs, read_size, ofs; ZZIP_DIR *dir; - + if (! fp) return -1; - if (! fp->dir) - { /* stat fd */ + if (! fp->dir) + { /* stat fd */ return fp->io->fd.seeks(fp->fd, offset, whence); } cur_pos = zzip_tell(fp); /* calculate relative offset */ - switch (whence) + switch (whence) { - case SEEK_SET: /* from beginning */ + case SEEK_SET: /* from beginning */ rel_ofs = offset - cur_pos; break; - case SEEK_CUR: /* from current */ + case SEEK_CUR: /* from current */ rel_ofs = offset; break; - case SEEK_END: /* from end */ + case SEEK_END: /* from end */ rel_ofs = fp->usize + offset - cur_pos; break; - default: /* something wrong */ + default: /* something wrong */ return -1; } - if (rel_ofs == 0) - return cur_pos; /* don't have to move */ + if (rel_ofs == 0) + return cur_pos; /* don't have to move */ - if (rel_ofs < 0) - { /* convert backward into forward */ - if (zzip_rewind(fp) == -1) + if (rel_ofs < 0) + { /* convert backward into forward */ + if (zzip_rewind(fp) == -1) return -1; read_size = cur_pos + rel_ofs; cur_pos = 0; - }else - { /* amount to read is positive relative offset */ + } else + { /* amount to read is positive relative offset */ read_size = rel_ofs; } - if (read_size < 0) /* bad offset, before beginning of file */ + if (read_size < 0) /* bad offset, before beginning of file */ return -1; - if (read_size + cur_pos > (zzip_off_t)fp->usize) /* bad offset, past EOF */ + if (read_size + cur_pos > (zzip_off_t) fp->usize) /* bad offset, past EOF */ return -1; - if (read_size == 0) /* nothing to read */ + if (read_size == 0) /* nothing to read */ return cur_pos; - + dir = fp->dir; /* * If this is other handle than previous, save current seek pointer * and read the file position of `this' handle. */ - if (dir->currentfp != fp) + if (dir->currentfp != fp) { - if (zzip_file_saveoffset(dir->currentfp) < 0 + if (zzip_file_saveoffset(dir->currentfp) < 0 || dir->currentfp->io->fd.seeks(dir->fd, fp->offset, SEEK_SET) < 0) - { dir->errcode = ZZIP_DIR_SEEK; return -1; } + { dir->errcode = ZZIP_DIR_SEEK; return -1; } else - { dir->currentfp = fp; } + { dir->currentfp = fp; } } - - if (fp->method == 0) - { /* unstore, just lseek relatively */ + + if (fp->method == 0) + { /* unstore, just lseek relatively */ ofs = fp->io->fd.tells(dir->fd); - ofs = fp->io->fd.seeks(dir->fd,read_size,SEEK_CUR); - if (ofs > 0) - { /* readjust from beginning of file */ + ofs = fp->io->fd.seeks(dir->fd, read_size, SEEK_CUR); + if (ofs > 0) + { /* readjust from beginning of file */ ofs -= fp->dataoffset; fp->restlen = fp->usize - ofs; } return ofs; - }else - { /* method == 8, inflate */ + } else + { /* method == 8, inflate */ char *buf; + /*FIXME: use a static buffer! */ - buf = (char *)malloc(ZZIP_32K); - if (! buf) return -1; - - while (read_size > 0) + buf = (char *) malloc(ZZIP_32K); + if (! buf) + return -1; + + while (read_size > 0) { zzip_off_t size = ZZIP_32K; - if (read_size < size/*32K*/) size = read_size; - size = zzip_file_read(fp, buf, (zzip_size_t)size); - if (size <= 0) { free(buf); return -1; } - + if (read_size < size /*32K */ ) + size = read_size; + + size = zzip_file_read(fp, buf, (zzip_size_t) size); + if (size <= 0) + { free(buf); return -1; } + read_size -= size; } - free (buf); + free(buf); } return zzip_tell(fp); @@ -1032,7 +1110,7 @@ zzip_tell(ZZIP_FILE * fp) if (! fp) return -1; - if (! fp->dir) /* stat fd */ + if (! fp->dir) /* stat fd */ return fp->io->fd.tells(fp->fd); /* current uncompressed offset is uncompressed size - data left */ diff --git a/zzip/info.c b/zzip/info.c index 144b163..a1324d6 100644 --- a/zzip/info.c +++ b/zzip/info.c @@ -1,3 +1,4 @@ + /* * Author: * Guido Draheim @@ -10,7 +11,7 @@ * of the Mozilla Public License 1.1 */ -#include /* exported... */ +#include /* exported... */ #include #include @@ -25,18 +26,17 @@ * just returns dir->errcode of the ZZIP_DIR handle * see: => zzip_dir_open, => zzip_diropen, => zzip_readdir, => zzip_dir_read */ -int -zzip_error(ZZIP_DIR * dir) -{ - return dir->errcode; +int +zzip_error(ZZIP_DIR * dir) +{ + return dir->errcode; } /** => zzip_error * This function just does dir->errcode = errcode */ -void -zzip_seterror(ZZIP_DIR * dir, int errcode) -{ dir->errcode = errcode; } +void +zzip_seterror(ZZIP_DIR * dir, int errcode) { dir->errcode = errcode; } /** * This function will just return fp->dir @@ -44,10 +44,10 @@ zzip_seterror(ZZIP_DIR * dir, int errcode) * If a ZZIP_FILE is contained within a zip-file that one will be a valid * pointer, otherwise a NULL is returned and the ZZIP_FILE wraps a real file. */ -ZZIP_DIR * -zzip_dirhandle(ZZIP_FILE * fp) -{ - return fp->dir; +ZZIP_DIR * +zzip_dirhandle(ZZIP_FILE * fp) +{ + return fp->dir; } /** => zzip_dirhandle @@ -57,21 +57,22 @@ zzip_dirhandle(ZZIP_FILE * fp) * zipfile is returned, otherwise a NULL is returned and the ZZIP_DIR wraps * a real directory DIR (if you have dirent on your system). */ -int -zzip_dirfd(ZZIP_DIR* dir) -{ - return dir->fd; +int +zzip_dirfd(ZZIP_DIR * dir) +{ + return dir->fd; } /** * return static const string of the known compression methods, * otherwise just "zipped" is returned */ -zzip_char_t* +zzip_char_t * zzip_compr_str(int compr) { - switch(compr) + switch (compr) { + /* *INDENT-OFF* */ case ZZIP_IS_STORED: return "stored"; case ZZIP_IS_SHRUNK: return "shrunk"; case ZZIP_IS_REDUCEDx1: @@ -107,16 +108,17 @@ zzip_compr_str(int compr) # endif return "special"; } - }/*switch*/ + /* *INDENT-ON* */ + } /*switch */ } /** => zzip_file_real * This function checks if the ZZIP_DIR-handle is wrapping * a real directory or a zip-archive. * Returns 1 for a stat'able directory, and 0 for a handle to zip-archive. - */ + */ int -zzip_dir_real(ZZIP_DIR* dir) +zzip_dir_real(ZZIP_DIR * dir) { return dir->realdir != 0; } @@ -127,9 +129,9 @@ zzip_dir_real(ZZIP_DIR* dir) * Returns 1 for a stat'able file, and 0 for a file inside a zip-archive. */ int -zzip_file_real(ZZIP_FILE* fp) +zzip_file_real(ZZIP_FILE * fp) { - return fp->dir == 0; /* ie. not dependent on a zip-arch-dir */ + return fp->dir == 0; /* ie. not dependent on a zip-arch-dir */ } /** => zzip_file_real @@ -137,8 +139,8 @@ zzip_file_real(ZZIP_FILE* fp) * Check before with => zzip_dir_real if the * the ZZIP_DIR points to a real directory. */ -void* -zzip_realdir(ZZIP_DIR* dir) +void * +zzip_realdir(ZZIP_DIR * dir) { return dir->realdir; } @@ -149,7 +151,7 @@ zzip_realdir(ZZIP_DIR* dir) * the ZZIP_FILE points to a real file. */ int -zzip_realfd(ZZIP_FILE* fp) +zzip_realfd(ZZIP_FILE * fp) { return fp->fd; } diff --git a/zzip/memdisk.c b/zzip/memdisk.c index 6f07a88..3e309f6 100644 --- a/zzip/memdisk.c +++ b/zzip/memdisk.c @@ -1,3 +1,4 @@ + /* * NOTE: this is part of libzzipmmapped (i.e. it is not libzzip). * ================== @@ -41,7 +42,7 @@ #define ___ { #define ____ } -static const char* error[] = { +static const char *error[] = { "Ok", # define _zzip_mem_disk_open_fail 1 "zzip_mem_disk_open: zzip_disk_open did fail", @@ -49,54 +50,56 @@ static const char* error[] = { "zzip_mem_disk_fdopen: zzip_disk_mmap did fail" }; -#define R _zzip_restrict /* a.k.a. allocated */ - - #define ZZIP_EXTRA_zip64 0x0001 -typedef struct _zzip_extra_zip64 { /* ZIP64 extended information extra field */ - zzip_byte_t z_datatype[2]; /* Tag for this "extra" block type */ - zzip_byte_t z_datasize[2]; /* Size of this "extra" block */ - zzip_byte_t z_usize[8]; /* Original uncompressed file size */ - zzip_byte_t z_csize[8]; /* Size of compressed data */ - zzip_byte_t z_offset[8]; /* Offset of local header record */ - zzip_byte_t z_diskstart[4]; /* Number of the disk for file start*/ +typedef struct _zzip_extra_zip64 +{ /* ZIP64 extended information extra field */ + zzip_byte_t z_datatype[2]; /* Tag for this "extra" block type */ + zzip_byte_t z_datasize[2]; /* Size of this "extra" block */ + zzip_byte_t z_usize[8]; /* Original uncompressed file size */ + zzip_byte_t z_csize[8]; /* Size of compressed data */ + zzip_byte_t z_offset[8]; /* Offset of local header record */ + zzip_byte_t z_diskstart[4]; /* Number of the disk for file start */ } zzip_extra_zip64; /*forward*/ -zzip__new__ static ZZIP_MEM_ENTRY* -zzip_mem_entry_new(ZZIP_DISK* disk, ZZIP_DISK_ENTRY* entry); -zzip__new__ static void -zzip_mem_entry_free(ZZIP_MEM_ENTRY* _zzip_restrict item); +zzip__new__ static ZZIP_MEM_ENTRY *zzip_mem_entry_new(ZZIP_DISK * disk, + ZZIP_DISK_ENTRY * entry); +zzip__new__ static void zzip_mem_entry_free(ZZIP_MEM_ENTRY * + _zzip_restrict item); -zzip__new__ ZZIP_MEM_DISK* +zzip__new__ ZZIP_MEM_DISK * zzip_mem_disk_new(void) { - return calloc(1, sizeof(ZZIP_MEM_DISK)); + return calloc(1, sizeof(ZZIP_MEM_DISK)); } /** create new diskdir handle. * wraps underlying zzip_disk_open. */ -zzip__new__ ZZIP_MEM_DISK* -zzip_mem_disk_open(char* filename) +zzip__new__ ZZIP_MEM_DISK * +zzip_mem_disk_open(char *filename) { - ZZIP_DISK* disk = zzip_disk_open(filename); - if (! disk) { perror(error[_zzip_mem_disk_open_fail]); return 0; } - ___ ZZIP_MEM_DISK* dir = zzip_mem_disk_new(); + ZZIP_DISK *disk = zzip_disk_open(filename); + if (! disk) + { perror(error[_zzip_mem_disk_open_fail]); return 0; } + ___ ZZIP_MEM_DISK *dir = zzip_mem_disk_new(); zzip_mem_disk_load(dir, disk); - return dir; ____; + return dir; + ____; } /** create new diskdir handle. * wraps underlying zzip_disk_open. */ -zzip__new__ ZZIP_MEM_DISK* +zzip__new__ ZZIP_MEM_DISK * zzip_mem_disk_fdopen(int fd) { - ZZIP_DISK* disk = zzip_disk_mmap(fd); - if (! disk) { perror(error[_zzip_mem_disk_fdopen_fail]); return 0; } - ___ ZZIP_MEM_DISK* dir = zzip_mem_disk_new(); + ZZIP_DISK *disk = zzip_disk_mmap(fd); + if (! disk) + { perror(error[_zzip_mem_disk_fdopen_fail]); return 0; } + ___ ZZIP_MEM_DISK *dir = zzip_mem_disk_new(); zzip_mem_disk_load(dir, disk); - return dir; ____; + return dir; + ____; } /** parse central dir. @@ -104,23 +107,35 @@ zzip_mem_disk_fdopen(int fd) * returns: number of entries, or -1 on error (setting errno) */ long -zzip_mem_disk_load(ZZIP_MEM_DISK* dir, ZZIP_DISK* disk) +zzip_mem_disk_load(ZZIP_MEM_DISK * dir, ZZIP_DISK * disk) { - if (! dir || ! disk) { errno=EINVAL; return -1; } - if (dir->list) zzip_mem_disk_unload(dir); + if (! dir || ! disk) + { errno=EINVAL; return -1; } + if (dir->list) + zzip_mem_disk_unload(dir); ___ long count = 0; - ___ struct zzip_disk_entry* entry = zzip_disk_findfirst(disk); - for (; entry ; entry = zzip_disk_findnext(disk, entry)) { - ZZIP_MEM_ENTRY* item = zzip_mem_entry_new(disk, entry); - if (! item) goto error; - if (dir->last) { dir->last->zz_next = item; } /* chain last */ - else { dir->list = item; }; dir->last = item; /* to earlier */ - count++; - } ____; + ___ struct zzip_disk_entry *entry = zzip_disk_findfirst(disk); + for (; entry; entry = zzip_disk_findnext(disk, entry)) + { + ZZIP_MEM_ENTRY *item = zzip_mem_entry_new(disk, entry); + if (! item) + goto error; + if (dir->last) + { + dir->last->zz_next = item; /* chain last */ + } else + { + dir->list = item; + } + dir->last = item; /* to earlier */ + count++; + } + ____; dir->disk = disk; - return count; ____; - error: - zzip_mem_disk_unload (dir); + return count; + ____; + error: + zzip_mem_disk_unload(dir); return -1; } @@ -131,129 +146,152 @@ zzip_mem_disk_load(ZZIP_MEM_DISK* dir, ZZIP_DISK* disk) * right into the diskdir_entry for later usage in higher layers. * returns: new item, or null on error (setting errno) */ -zzip__new__ ZZIP_MEM_ENTRY* -zzip_mem_entry_new(ZZIP_DISK* disk, ZZIP_DISK_ENTRY* entry) +zzip__new__ ZZIP_MEM_ENTRY * +zzip_mem_entry_new(ZZIP_DISK * disk, ZZIP_DISK_ENTRY * entry) { - if (! disk || ! entry) { errno=EINVAL; return 0; } - ___ ZZIP_MEM_ENTRY* item = calloc(1, sizeof(*item)); - if (! item) return 0; /* errno=ENOMEM; */ - ___ struct zzip_file_header* header = - zzip_disk_entry_to_file_header(disk, entry); + if (! disk || ! entry) + { errno=EINVAL; return 0; } + ___ ZZIP_MEM_ENTRY *item = calloc(1, sizeof(*item)); + if (! item) + return 0; /* errno=ENOMEM; */ + ___ struct zzip_file_header *header = + zzip_disk_entry_to_file_header(disk, entry); /* there is a number of duplicated information in the file header * or the disk entry block. Theoretically some part may be missing * that exists in the other, ... but we will prefer the disk entry. */ - item->zz_comment = zzip_disk_entry_strdup_comment(disk, entry); - item->zz_name = zzip_disk_entry_strdup_name(disk, entry); - item->zz_data = zzip_file_header_to_data(header); - item->zz_flags = zzip_disk_entry_get_flags(entry); - item->zz_compr = zzip_disk_entry_get_compr(entry); - item->zz_mktime = zzip_disk_entry_get_mktime(entry); - item->zz_crc32 = zzip_disk_entry_get_crc32(entry); - item->zz_csize = zzip_disk_entry_get_csize(entry); - item->zz_usize = zzip_disk_entry_get_usize(entry); + item->zz_comment = zzip_disk_entry_strdup_comment(disk, entry); + item->zz_name = zzip_disk_entry_strdup_name(disk, entry); + item->zz_data = zzip_file_header_to_data(header); + item->zz_flags = zzip_disk_entry_get_flags(entry); + item->zz_compr = zzip_disk_entry_get_compr(entry); + item->zz_mktime = zzip_disk_entry_get_mktime(entry); + item->zz_crc32 = zzip_disk_entry_get_crc32(entry); + item->zz_csize = zzip_disk_entry_get_csize(entry); + item->zz_usize = zzip_disk_entry_get_usize(entry); item->zz_diskstart = zzip_disk_entry_get_diskstart(entry); - item->zz_filetype = zzip_disk_entry_get_filetype(entry); - - { /* copy the extra blocks to memory as well */ - int ext1 = zzip_disk_entry_get_extras(entry); - char* R ptr1 = zzip_disk_entry_to_extras(entry); - int ext2 = zzip_file_header_get_extras(header); - char* R ptr2 = zzip_file_header_to_extras(header); - - if (ext1) { - void* mem = malloc (ext1+2); - item->zz_ext[1] = mem; - memcpy (mem, ptr1, ext1); - ((char*)(mem))[ext1+0] = 0; - ((char*)(mem))[ext1+1] = 0; - } - if (ext2) { - void* mem = malloc (ext2+2); - item->zz_ext[2] = mem; - memcpy (mem, ptr2, ext2); - ((char*)(mem))[ext2+0] = 0; - ((char*)(mem))[ext2+1] = 0; - } - } - { /* override sizes/offsets with zip64 values for largefile support */ - zzip_extra_zip64* block = (zzip_extra_zip64*) - zzip_mem_entry_extra_block (item, ZZIP_EXTRA_zip64); - if (block) { - item->zz_usize = __zzip_get64(block->z_usize); - item->zz_csize = __zzip_get64(block->z_csize); - item->zz_offset = __zzip_get64(block->z_offset); - item->zz_diskstart = __zzip_get32(block->z_diskstart); - } + item->zz_filetype = zzip_disk_entry_get_filetype(entry); + + { /* copy the extra blocks to memory as well */ + int /* */ ext1 = zzip_disk_entry_get_extras(entry); + char *_zzip_restrict ptr1 = zzip_disk_entry_to_extras(entry); + int /* */ ext2 = zzip_file_header_get_extras(header); + char *_zzip_restrict ptr2 = zzip_file_header_to_extras(header); + + if (ext1) + { + void *mem = malloc(ext1 + 2); + item->zz_ext[1] = mem; + memcpy(mem, ptr1, ext1); + ((char *) (mem))[ext1 + 0] = 0; + ((char *) (mem))[ext1 + 1] = 0; + } + if (ext2) + { + void *mem = malloc(ext2 + 2); + item->zz_ext[2] = mem; + memcpy(mem, ptr2, ext2); + ((char *) (mem))[ext2 + 0] = 0; + ((char *) (mem))[ext2 + 1] = 0; + } + } + { /* override sizes/offsets with zip64 values for largefile support */ + zzip_extra_zip64 *block = (zzip_extra_zip64 *) + zzip_mem_entry_extra_block(item, ZZIP_EXTRA_zip64); + if (block) + { + item->zz_usize = __zzip_get64(block->z_usize); + item->zz_csize = __zzip_get64(block->z_csize); + item->zz_offset = __zzip_get64(block->z_offset); + item->zz_diskstart = __zzip_get32(block->z_diskstart); + } } /* NOTE: * All information from the central directory entry is now in memory. * Effectivly that allows us to modify it and write it back to disk. */ - return item; ____;____; + return item; + ____; + ____; } /* find an extra block for the given datatype code. * We assume that the central directory has been preparsed to memory. */ -ZZIP_EXTRA_BLOCK* -zzip_mem_entry_extra_block (ZZIP_MEM_ENTRY* entry, short datatype) +ZZIP_EXTRA_BLOCK * +zzip_mem_entry_extra_block(ZZIP_MEM_ENTRY * entry, short datatype) { int i = 2; - while (1) { - ZZIP_EXTRA_BLOCK* ext = entry->zz_ext[i]; - if (ext) { - while (*(short*)(ext->z_datatype)) { - if (datatype == zzip_extra_block_get_datatype (ext)) { - return ext; - } - ___ char* e = (char*) ext; - e += zzip_extra_block_headerlength; - e += zzip_extra_block_get_datasize (ext); - ext = (void*) e; ____; - } - } - if (! i) return 0; - i--; + while (1) + { + ZZIP_EXTRA_BLOCK *ext = entry->zz_ext[i]; + if (ext) + { + while (*(short *) (ext->z_datatype)) + { + if (datatype == zzip_extra_block_get_datatype(ext)) + { + return ext; + } + ___ char *e = (char *) ext; + e += zzip_extra_block_headerlength; + e += zzip_extra_block_get_datasize(ext); + ext = (void *) e; + ____; + } + } + if (! i) + return 0; + i--; } } void -zzip_mem_entry_free(ZZIP_MEM_ENTRY* _zzip_restrict item) +zzip_mem_entry_free(ZZIP_MEM_ENTRY * _zzip_restrict item) { - if (item) { + if (item) + { + /* *INDENT-OFF* */ if (item->zz_ext[0]) free (item->zz_ext[0]); if (item->zz_ext[1]) free (item->zz_ext[1]); if (item->zz_ext[2]) free (item->zz_ext[2]); if (item->zz_comment) free (item->zz_comment); if (item->zz_name) free (item->zz_name); free (item); + /* *INDENT-ON* */ } } void -zzip_mem_disk_unload(ZZIP_MEM_DISK* dir) +zzip_mem_disk_unload(ZZIP_MEM_DISK * dir) { - ZZIP_MEM_ENTRY* item = dir->list; - while (item) { - ZZIP_MEM_ENTRY* next = item->zz_next; - zzip_mem_entry_free(item); item = next; + ZZIP_MEM_ENTRY *item = dir->list; + while (item) + { + ZZIP_MEM_ENTRY *next = item->zz_next; + zzip_mem_entry_free(item); + item = next; } - dir->list = dir->last = 0; dir->disk = 0; + dir->list = dir->last = 0; + dir->disk = 0; } void -zzip_mem_disk_close(ZZIP_MEM_DISK* _zzip_restrict dir) +zzip_mem_disk_close(ZZIP_MEM_DISK * _zzip_restrict dir) { - if (dir) { - zzip_mem_disk_unload (dir); - zzip_disk_close(dir->disk); - free (dir); + if (dir) + { + zzip_mem_disk_unload(dir); + zzip_disk_close(dir->disk); + free(dir); } } #if 0 -static void foo (short zz_datatype) { +static void +foo(short zz_datatype) +{ + /* *INDENT-OFF* */ switch (zz_datatype) { case 0x0001: /* ZIP64 extended information extra field */ case 0x0007: /* AV Info */ @@ -293,6 +331,7 @@ static void foo (short zz_datatype) { case 0x756e: /* ASi Unix */ case 0x7855: /* Info-ZIP Unix (new) */ case 0xfd4a: /* SMS/QDOS */ + /* *INDENT-OFF* */ } } #endif diff --git a/zzip/mmapped.c b/zzip/mmapped.c index daaf433..8712afa 100644 --- a/zzip/mmapped.c +++ b/zzip/mmapped.c @@ -326,23 +326,24 @@ zzip_disk_findfirst(ZZIP_DISK* disk) { zzip_byte_t* root; /* (struct zzip_disk_entry*) */ if (zzip_disk_trailer_check_magic(p)) { - root = disk->buffer + zzip_disk_trailer_get_rootseek ( - (struct zzip_disk_trailer*)p); + struct zzip_disk_trailer* trailer = (struct zzip_disk_trailer*)p; + root = disk->buffer + zzip_disk_trailer_get_rootseek (trailer); if (root > p) { /* the first disk_entry is after the disk_trailer? can't be! */ - zzip_size_t rootsize = zzip_disk_trailer_get_rootsize ( - (struct zzip_disk_trailer*)p); + zzip_size_t rootsize = zzip_disk_trailer_get_rootsize (trailer); if (disk->buffer+rootsize > p) continue; /* a common brokeness that can be fixed: we just assume the * central directory was written directly before the trailer:*/ root = p - rootsize; } } else if (zzip_disk64_trailer_check_magic(p)) { + struct zzip_disk64_trailer* trailer = (struct zzip_disk64_trailer*)p; if (sizeof(void*) < 8) return 0; /* EOVERFLOW */ - root = disk->buffer + zzip_disk64_trailer_get_rootseek ( - (struct zzip_disk64_trailer*)p); + root = disk->buffer + zzip_disk64_trailer_get_rootseek (trailer); if (root > p) continue; - } else continue; + } else { + continue; + } if (root < disk->buffer) continue; if (zzip_disk_entry_check_magic(root)) diff --git a/zzip/plugin.c b/zzip/plugin.c index cb057b7..4bb224b 100644 --- a/zzip/plugin.c +++ b/zzip/plugin.c @@ -1,3 +1,4 @@ + /* * Author: * Guido Draheim @@ -28,22 +29,21 @@ zzip_off_t zzip_filesize(int fd) { - struct stat st; + struct stat st; - if (fstat(fd, &st) < 0) - return -1; + if (fstat(fd, &st) < 0) + return -1; # if defined DEBUG && ! defined _WIN32 - if (! st.st_size && st.st_blocks > 1) /* seen on some darwin 10.1 machines */ - fprintf(stderr, "broken fstat(2) ?? st_size=%ld st_blocks=%ld\n", - (long) st.st_size, (long) st.st_blocks); + if (! st.st_size && st.st_blocks > 1) /* seen on some darwin 10.1 machines */ + fprintf(stderr, "broken fstat(2) ?? st_size=%ld st_blocks=%ld\n", + (long) st.st_size, (long) st.st_blocks); # endif - return st.st_size; + return st.st_size; } -static const struct zzip_plugin_io default_io = -{ +static const struct zzip_plugin_io default_io = { &open, &close, &_zzip_read, @@ -60,16 +60,18 @@ static const struct zzip_plugin_io default_io = zzip_plugin_io_t zzip_get_default_io() { - return (zzip_plugin_io_t) &default_io; + return (zzip_plugin_io_t) & default_io; } /** * This function initializes the users handler struct to default values * being the posix io functions in default configured environments. */ -int zzip_init_io(zzip_plugin_io_handlers_t io, int flags) +int +zzip_init_io(zzip_plugin_io_handlers_t io, int flags) { - if (! io) { + if (! io) + { return ZZIP_ERROR; } memcpy(io, &default_io, sizeof(default_io)); diff --git a/zzip/stat.c b/zzip/stat.c index 0395c67..8fd6d85 100644 --- a/zzip/stat.c +++ b/zzip/stat.c @@ -38,13 +38,17 @@ zzip_dir_stat(ZZIP_DIR * dir, zzip_char_t* name, ZZIP_STAT * zs, int flags) cmp = (flags & ZZIP_CASEINSENSITIVE) ? strcasecmp : strcmp; + if (! hdr) { + dir->errcode = ZZIP_ENOENT; + return -1; + } + if (flags & ZZIP_IGNOREPATH) { char* n = strrchr(name, '/'); if (n) name = n + 1; } - if (hdr) while (1) { register char* hdr_name = hdr->d_name; -- 2.40.0