]> granicus.if.org Git - zziplib/blob - bins/unzzipdir-zip.c
CVE-2017-5981 testcase
[zziplib] / bins / unzzipdir-zip.c
1 /*
2  *      Copyright (c) 2003 Guido Draheim <guidod@gmx.de>
3  *      Use freely under the restrictions of the ZLIB license.
4  *
5  *      This file is used as an example to clarify zzip api usage.
6  */
7
8 #include <zzip/lib.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "unzzipdir-zip.h"
13
14 #ifdef ZZIP_HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #ifdef ZZIP_HAVE_IO_H
18 #include <io.h>
19 #endif
20
21 #ifdef ZZIP_HAVE_FNMATCH_H
22 #include <fnmatch.h>
23 #else
24 #define fnmatch(x,y,z) strcmp(x,y)
25 #endif
26
27 static const char* comprlevel[] = {
28     "stored",   "shrunk",   "redu:1",   "redu:2",   "redu:3",   "redu:4",
29     "impl:N",   "toknze",   "defl:N",   "defl:B",   "impl:B" };
30
31
32 static int 
33 unzzip_list (int argc, char ** argv, int verbose)
34 {
35     int argn;
36     ZZIP_DIR* disk;
37     zzip_error_t error;
38     
39     if (argc == 1)
40     {
41         printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
42         return -1; /* better provide an archive argument */
43     }
44     
45     disk = zzip_dir_open (argv[1], &error);
46     if (! disk) {
47         perror(argv[1]);
48         return -1;
49     }
50
51     if (argc == 2)
52     {  /* list all */
53         ZZIP_DIRENT entry;
54         while(zzip_dir_read(disk, &entry))
55         {
56             char* name = entry.d_name;
57             long long usize = entry.st_size;
58             if (!verbose)
59             {
60                 printf ("%22lli %s\n", usize, name);
61             } else
62             {
63                 long long csize = entry.d_csize;
64                 unsigned compr = entry.d_compr;
65                 const char* defl = (compr < sizeof(comprlevel)) ? comprlevel[compr] : "(redu)";
66                 printf ("%lli/%lli %s %s\n", usize, csize, defl, name);
67             }
68         }
69     }
70     else
71     {   /* list only the matching entries - in order of zip directory */
72         ZZIP_DIRENT entry;
73         while(zzip_dir_read(disk, &entry))
74         {
75             char* name = entry.d_name;
76             for (argn=1; argn < argc; argn++)
77             {
78                 if (! fnmatch (argv[argn], name, 
79                                FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD))
80                 {
81                     long long usize = entry.st_size;
82                     if (!verbose)
83                     {
84                         printf ("%22lli %s\n", usize, name);
85                     } else
86                     {
87                         long long csize = entry.d_csize;
88                         unsigned compr = entry.d_compr;
89                         const char* defl = (compr < sizeof(comprlevel)) ? comprlevel[compr] : "(redu)";
90                         printf ("%lli/%lli %s %s\n", usize, csize, defl, name);
91                     }
92                     break; /* match loop */
93                 }
94             }
95         }
96     }
97     zzip_dir_close(disk);
98     return 0;
99
100
101 int 
102 unzzip_long_list (int argc, char ** argv)
103 {
104     return unzzip_list(argc, argv, 1);
105 }
106
107 int 
108 unzzip_show_list (int argc, char ** argv)
109 {
110     return unzzip_list(argc, argv, 0);
111 }
112
113 /* 
114  * Local variables:
115  * c-file-style: "stroustrup"
116  * End:
117  */