From a735e41377bab4fb9f90fc1b00128e835f43c64a Mon Sep 17 00:00:00 2001 From: Jakub Martisko Date: Fri, 2 Aug 2024 12:40:27 +0200 Subject: [PATCH] Refresh the tests from the previous commit Related: RHEL-47131 --- tests/gating/build/__fnmatch.h | 46 +++++++--- tests/gating/build/__param.h | 18 ++++ tests/gating/build/unzzip.c | 115 ++++++++++++++++++++++++- tests/gating/build/unzzipcat-zip.c | 133 +++-------------------------- tests/gating/build/unzzipdir-mix.c | 13 +-- zziplib.spec | 5 +- 6 files changed, 186 insertions(+), 144 deletions(-) create mode 100644 tests/gating/build/__param.h diff --git a/tests/gating/build/__fnmatch.h b/tests/gating/build/__fnmatch.h index 90d36dc..8d35b80 100644 --- a/tests/gating/build/__fnmatch.h +++ b/tests/gating/build/__fnmatch.h @@ -8,25 +8,43 @@ #ifdef ZZIP_HAVE_FNMATCH_H #include -#endif - -#ifdef ZZIP_HAVE_FNMATCH_H #define _zzip_fnmatch fnmatch -# ifdef FNM_CASEFOLD -# define _zzip_FNM_CASEFOLD FNM_CASEFOLD -# else -# define _zzip_FNM_CASEFOLD 0 -# endif +#ifdef FNM_CASEFOLD +#define _zzip_FNM_CASEFOLD FNM_CASEFOLD +#else +#define _zzip_FNM_CASEFOLD 0 +#endif +#ifdef FNM_NOESCAPE +#define _zzip_FNM_NOESCAPE FNM_NOESCAPE +#else +#define _zzip_FNM_NOESCAPE 0 +#endif +#ifdef FNM_PATHNAME +#define _zzip_FNM_PATHNAME FNM_PATHNAME +#else +#define _zzip_FNM_PATHNAME 0 +#endif +#ifdef FNM_PERIOD +#define _zzip_FNM_PERIOD FNM_PERIOD +#else +#define _zzip_FNM_PERIOD 0 +#endif + #else -# define _zzip_FNM_CASEFOLD 0 /* if your system does not have fnmatch, we fall back to strcmp: */ -static int _zzip_fnmatch(char* pattern, char* string, int flags) -{ -# ifdef DBG2 +static int +_zzip_fnmatch(char* pattern, char* string, int flags) +{ +#ifdef DBG2 DBG1(""); -# endif - return strcmp (pattern, string); +#endif + return strcmp(pattern, string); } + +#define _zzip_FNM_CASEFOLD 0 +#define _zzip_FNM_NOESCAPE 0 +#define _zzip_FNM_PATHNAME 0 +#define _zzip_FNM_PERIOD 0 #endif #endif diff --git a/tests/gating/build/__param.h b/tests/gating/build/__param.h new file mode 100644 index 0000000..c9cde9d --- /dev/null +++ b/tests/gating/build/__param.h @@ -0,0 +1,18 @@ +#ifndef __ZZIP_INTERNAL_PARAM_H +#define __ZZIP_INTERNAL_PARAM_H + +#include + +#ifdef ZZIP_HAVE_SYS_PARAM_H +#include /* PATH_MAX */ +#endif + +#ifndef PATH_MAX +#ifdef MAX_PATH /* windows */ +#define PATH_MAX MAX_PATH +#else +#define PATH_MAX 512 +#endif +#endif + +#endif diff --git a/tests/gating/build/unzzip.c b/tests/gating/build/unzzip.c index f91c5eb..5eafe06 100644 --- a/tests/gating/build/unzzip.c +++ b/tests/gating/build/unzzip.c @@ -5,8 +5,14 @@ * This file is used as an example to clarify zzip api usage. */ +#include #include +#include "__string.h" +#include "__mkdir.h" +#include "__debug.h" +#include "__param.h" #include +#include #include #include "unzzipcat-zip.h" #include "unzzipdir-zip.h" @@ -22,7 +28,7 @@ static const char usage[] = static int unzzip_version(void) { - printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n"); + printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n"); return 0; } @@ -32,6 +38,113 @@ static int unzzip_help(void) return 0; } +/* Functions used by unzzipcat-*.c: */ +int exitcode(int e) +{ + switch (e) + { + case ZZIP_NO_ERROR: + return EXIT_OK; + case ZZIP_OUTOFMEM: /* out of memory */ + return EXIT_ENOMEM; + case ZZIP_DIR_OPEN: /* failed to open zipfile, see errno for details */ + return EXIT_ZIP_NOT_FOUND; + case ZZIP_DIR_STAT: /* failed to fstat zipfile, see errno for details */ + case ZZIP_DIR_SEEK: /* failed to lseek zipfile, see errno for details */ + case ZZIP_DIR_READ: /* failed to read zipfile, see errno for details */ + case ZZIP_DIR_TOO_SHORT: + case ZZIP_DIR_EDH_MISSING: + return EXIT_FILEFORMAT; + case ZZIP_DIRSIZE: + return EXIT_EARLY_END_OF_FILE; + case ZZIP_ENOENT: + return EXIT_FILE_NOT_FOUND; + case ZZIP_UNSUPP_COMPR: + return EXIT_UNSUPPORTED_COMPRESSION; + case ZZIP_CORRUPTED: + case ZZIP_UNDEF: + case ZZIP_DIR_LARGEFILE: + return EXIT_FILEFORMAT; + } + return EXIT_ERRORS; +} + +/* + * NAME: remove_dotdotslash + * PURPOSE: To remove any "../" components from the given pathname + * ARGUMENTS: path: path name with maybe "../" components + * RETURNS: Nothing, "path" is modified in-place + * NOTE: removing "../" from the path ALWAYS shortens the path, never adds to it! + * Also, "path" is not used after creating it. + * So modifying "path" in-place is safe to do. + */ +static inline void +remove_dotdotslash(char *path) +{ + /* Note: removing "../" from the path ALWAYS shortens the path, never adds to it! */ + char *dotdotslash; + int warned = 0; + + dotdotslash = path; + while ((dotdotslash = strstr(dotdotslash, "../")) != NULL) + { + /* + * Remove only if at the beginning of the pathname ("../path/name") + * or when preceded by a slash ("path/../name"), + * otherwise not ("path../name..")! + */ + if (dotdotslash == path || dotdotslash[-1] == '/') + { + char *src, *dst; + if (!warned) + { + /* Note: the first time through the pathname is still intact */ + fprintf(stderr, "Removing \"../\" path component(s) in %s\n", path); + warned = 1; + } + /* We cannot use strcpy(), as there "The strings may not overlap" */ + for (src = dotdotslash+3, dst=dotdotslash; (*dst = *src) != '\0'; src++, dst++) + ; + } + else + dotdotslash +=3; /* skip this instance to prevent infinite loop */ + } +} + +static void makedirs(const char* name) +{ + char* p = strrchr(name, '/'); + if (p) { + char* dir_name = _zzip_strndup(name, p-name); + makedirs(dir_name); + free (dir_name); + } + if (_zzip_mkdir(name, 0775) == -1 && errno != EEXIST) + { + DBG3("while mkdir %s : %s", name, strerror(errno)); + } + errno = 0; +} + +FILE* create_fopen(char* name, char* mode, int subdirs) +{ + char name_stripped[PATH_MAX]; + + strncpy(name_stripped, name, PATH_MAX); + remove_dotdotslash(name_stripped); + + if (subdirs) + { + char* p = strrchr(name_stripped, '/'); + if (p) { + char* dir_name = _zzip_strndup(name_stripped, p-name_stripped); + makedirs(dir_name); + free (dir_name); + } + } + return fopen(name_stripped, mode); +} + int main (int argc, char ** argv) { diff --git a/tests/gating/build/unzzipcat-zip.c b/tests/gating/build/unzzipcat-zip.c index 0f0b1d5..cdcfb34 100644 --- a/tests/gating/build/unzzipcat-zip.c +++ b/tests/gating/build/unzzipcat-zip.c @@ -10,9 +10,9 @@ #include #include #include +#include "__fnmatch.h" #include "__mkdir.h" #include "__string.h" -#include "__fnmatch.h" #include "__debug.h" #include "unzzipcat-zip.h" #include "unzzip-states.h" @@ -24,35 +24,9 @@ #include #endif -static int exitcode(int e) -{ - switch (e) - { - case ZZIP_NO_ERROR: - return EXIT_OK; - case ZZIP_OUTOFMEM: /* out of memory */ - return EXIT_ENOMEM; - case ZZIP_DIR_OPEN: /* failed to open zipfile, see errno for details */ - return EXIT_ZIP_NOT_FOUND; - case ZZIP_DIR_STAT: /* failed to fstat zipfile, see errno for details */ - case ZZIP_DIR_SEEK: /* failed to lseek zipfile, see errno for details */ - case ZZIP_DIR_READ: /* failed to read zipfile, see errno for details */ - case ZZIP_DIR_TOO_SHORT: - case ZZIP_DIR_EDH_MISSING: - return EXIT_FILEFORMAT; - case ZZIP_DIRSIZE: - return EXIT_EARLY_END_OF_FILE; - case ZZIP_ENOENT: - return EXIT_FILE_NOT_FOUND; - case ZZIP_UNSUPP_COMPR: - return EXIT_UNSUPPORTED_COMPRESSION; - case ZZIP_CORRUPTED: - case ZZIP_UNDEF: - case ZZIP_DIR_LARGEFILE: - return EXIT_FILEFORMAT; - } - return EXIT_ERRORS; -} +/* Functions in unzzip.c: */ +extern int exitcode(int); +extern FILE* create_fopen(char*, char*, int); static void unzzip_cat_file(ZZIP_DIR* disk, char* name, FILE* out) { @@ -60,99 +34,17 @@ static void unzzip_cat_file(ZZIP_DIR* disk, char* name, FILE* out) if (file) { char buffer[1024]; int len; - while ((len = zzip_file_read (file, buffer, 1024))) + while (0 < (len = zzip_file_read (file, buffer, 1024))) { fwrite (buffer, 1, len, out); } zzip_file_close (file); + } else { + DBG3("'%s' file not found [%s]", name, disk->realname); } } -/* - * NAME: remove_dotdotslash - * PURPOSE: To remove any "../" components from the given pathname - * ARGUMENTS: path: path name with maybe "../" components - * RETURNS: Nothing, "path" is modified in-place - * NOTE: removing "../" from the path ALWAYS shortens the path, never adds to it! - * Also, "path" is not used after creating it. - * So modifying "path" in-place is safe to do. - */ -static inline void -remove_dotdotslash(char *path) -{ - /* Note: removing "../" from the path ALWAYS shortens the path, never adds to it! */ - char *dotdotslash; - int warned = 0; - - dotdotslash = path; - while ((dotdotslash = strstr(dotdotslash, "../")) != NULL) - { - /* - * Remove only if at the beginning of the pathname ("../path/name") - * or when preceded by a slash ("path/../name"), - * otherwise not ("path../name..")! - */ - if (dotdotslash == path || dotdotslash[-1] == '/') - { - char *src, *dst; - if (!warned) - { - /* Note: the first time through the pathname is still intact */ - fprintf(stderr, "Removing \"../\" path component(s) in %s\n", path); - warned = 1; - } - /* We cannot use strcpy(), as there "The strings may not overlap" */ - for (src = dotdotslash+3, dst=dotdotslash; (*dst = *src) != '\0'; src++, dst++) - ; - } - else - dotdotslash +=3; /* skip this instance to prevent infinite loop */ - } -} - -static void makedirs(const char* name) -{ - char* p = strrchr(name, '/'); - if (p) { - char* dir_name = _zzip_strndup(name, p-name); - makedirs(dir_name); - free (dir_name); - } - if (_zzip_mkdir(name, 0775) == -1 && errno != EEXIST) - { - DBG3("while mkdir %s : %s", name, strerror(errno)); - } - errno = 0; -} - -static FILE* create_fopen(char* name, char* mode, int subdirs) -{ - char *name_stripped; - FILE *fp; - int mustfree = 0; - - if ((name_stripped = strdup(name)) != NULL) - { - remove_dotdotslash(name_stripped); - name = name_stripped; - mustfree = 1; - } - if (subdirs) - { - char* p = strrchr(name, '/'); - if (p) { - char* dir_name = _zzip_strndup(name, p-name); - makedirs(dir_name); - free (dir_name); - } - } - fp = fopen(name, mode); - if (mustfree) - free(name_stripped); - return fp; -} - static int unzzip_cat (int argc, char ** argv, int extract) { int done = 0; @@ -162,7 +54,7 @@ static int unzzip_cat (int argc, char ** argv, int extract) if (argc == 1) { - printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n"); + printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n"); return EXIT_OK; /* better provide an archive argument */ } @@ -179,7 +71,10 @@ static int unzzip_cat (int argc, char ** argv, int extract) { char* name = entry.d_name; FILE* out = stdout; - if (extract) out = create_fopen(name, "w", 1); + if (extract) { + out = create_fopen(name, "wb", 1); + DBG3("extract '%s' to new file %p", name, out); + } if (! out) { DBG3("fopen' %s : %s", name, strerror(errno)); if (errno != EISDIR) done = EXIT_ERRORS; @@ -198,10 +93,10 @@ static int unzzip_cat (int argc, char ** argv, int extract) for (argn=1; argn < argc; argn++) { if (! _zzip_fnmatch (argv[argn], name, - FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD)) + _zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD)) { FILE* out = stdout; - if (extract) out = create_fopen(name, "w", 1); + if (extract) out = create_fopen(name, "wb", 1); if (! out) { DBG3("fopen. %s : %s", name, strerror(errno)); if (errno != EISDIR) done = EXIT_ERRORS; diff --git a/tests/gating/build/unzzipdir-mix.c b/tests/gating/build/unzzipdir-mix.c index 140410e..18a5d0d 100644 --- a/tests/gating/build/unzzipdir-mix.c +++ b/tests/gating/build/unzzipdir-mix.c @@ -7,6 +7,7 @@ #include #include "__debug.h" +#include "__fnmatch.h" #include #include #include @@ -20,12 +21,6 @@ #include #endif -#ifdef ZZIP_HAVE_FNMATCH_H -#include -#else -#define fnmatch(x,y,z) strcmp(x,y) -#endif - static const char* comprlevel[] = { "stored", "shrunk", "redu:1", "redu:2", "redu:3", "redu:4", "impl:N", "toknze", "defl:N", "defl:B", "impl:B" }; @@ -68,7 +63,7 @@ unzzip_list (int argc, char ** argv, int verbose) if (argc == 1) { - printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n"); + printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n"); return EXIT_OK; /* better provide an archive argument */ } @@ -107,8 +102,8 @@ unzzip_list (int argc, char ** argv, int verbose) char* name = entry->d_name; for (argn=1; argn < argc; argn++) { - if (! fnmatch (argv[argn], name, - FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD)) + if (! _zzip_fnmatch (argv[argn], name, + _zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD)) { long long usize = entry->st_size; if (!verbose) diff --git a/zziplib.spec b/zziplib.spec index cc51a98..45cedef 100644 --- a/zziplib.spec +++ b/zziplib.spec @@ -5,7 +5,7 @@ Summary: Lightweight library to easily extract data from zip files Name: zziplib Version: 0.13.74 -Release: 2%{?dist} +Release: 3%{?dist} License: LGPL-2.0-or-later OR MPL-1.1 URL: http://zziplib.sourceforge.net/ Source: https://github.com/gdraheim/zziplib/archive/v%{version}.tar.gz @@ -90,6 +90,9 @@ zziplib library. %{_mandir}/man3/* %changelog +* Wed Jul 03 2024 Jakub Martisko - 0.13.74-3 +- Refresh the gating tests + bump the release number + * Wed Jul 03 2024 Jakub Martisko - 0.13.74-2 - Add gating tests + bump the release number