Debian Codegraph @

libs/zziplib/zziplib-src/bins/unzip-mem.c

raw
1 /*
2 * Copyright (c) 2003 Guido Draheim <[email protected]>
3 * Use freely under the restrictions of the ZLIB license.
4 *
5 * This file is used as an example to clarify zzipmmap api usage.
6 */
7
8 #include <zlib.h> /* crc32 */
9
10 #include <zzip/memdisk.h>
11 #include <zzip/__fnmatch.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #ifdef ZZIP_HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #ifdef ZZIP_HAVE_IO_H
20 #include <io.h>
21 #endif
22
23 #include <time.h>
24
25 #define ___ {
26 #define ____ }
27
28 #define EXIT_SUCCESS 0
29 #define EXIT_WARNINGS 1
30 #define EXIT_ERRORS 2
31 #define EXIT_EFORMAT 3
32 #define EXIT_ENOMEM 4
33 #define EXIT_ENOTTY 5
34 #define EXIT_EINFLATE 6
35 #define EXIT_ENOARCH 9
36 #define EXIT_EOPTION 10
37 #define EXIT_ENOFILE 11
38 #define EXIT_EDISKFULL 50
39 #define EXIT_EPREMATURE 51
40 #define EXIT_USERABORT 80
41 #define EXIT_BADCOMPRESSION 81
42 #define EXIT_BADPASSWORD 82
43
44 static int status = EXIT_SUCCESS;
45 static int option_list = 0; /* "-l" */
46 static int option_pipe = 0; /* "-p" */
47 static int option_verbose = 0; /* "-v" */
48 static int option_testcrc = 0; /* "-t" */
49 static int option_binary = 0; /* "-b" */
50 static int option_nocase = 0; /* "-C" */
51 static int option_junkpaths = 0; /* "-j" */
52 static int option_dosfiles = 0; /* "-L" */
53 static int option_keepold = 0; /* "-n" */
54 static int option_overwrite = 0; /* "-o" */
55 static int option_quiet = 0; /* "-q" */
56 static int option_permbits = 0; /* "-X" */
57
58 static const char usage[] =
59 {
60 "unzzip-mem <zip> [names].. \n"
61 " - unzzip a zip archive.\n"
62 "options:\n"
63 " -l list archive files (name, usize, mtime, comments)\n" /* +totals */
64 " -p extract archive files to pipe, i.e. stdout (binary mode)\n"
65 " -t test archive files (check the crc values)\n"
66 " -v verbose list of archive files\n"
67 " -b accept and ignore (force binary extract)\n"
68 " -C match filenames case-insensitively\n"
69 " -j junk paths (do not recreate directory structure)\n"
70 " -L convert dos filenames to lowercase upon extract\n"
71 " -n never overwrite existing files\n"
72 " -o always overwrite existing files\n"
73 " -q quite operation\n"
74 " -X restore user/owner attributes of files\n"
75 };
76
77 static void zzip_mem_entry_pipe(ZZIP_MEM_DISK* disk,
78 ZZIP_MEM_ENTRY* entry, FILE* out)
79 {
80 ZZIP_DISK_FILE* file = zzip_mem_entry_fopen (disk, entry);
81 if (file)
82 {
83 char buffer[1024]; int len;
84 while (0 < (len = zzip_mem_disk_fread (buffer, 1024, 1, file)))
85 fwrite (buffer, len, 1, out);
86
87 zzip_mem_disk_fclose (file);
88 }
89 }
90
91 static void zzip_mem_entry_make(ZZIP_MEM_DISK* disk,
92 ZZIP_MEM_ENTRY* entry)
93 {
94 FILE* file = fopen (entry->zz_name, "wb");
95 if (file) { zzip_mem_entry_pipe (disk, entry, file); fclose (file); }
96 perror (entry->zz_name);
97 if (status < EXIT_WARNINGS) status = EXIT_WARNINGS;
98 }
99
100 /* ------------------------- test ------------------------------------ */
101
102 static char* archive = 0;
103 static int test_errors = 0;
104
105 static void zzip_mem_entry_test(ZZIP_MEM_DISK* disk,
106 ZZIP_MEM_ENTRY* entry)
107 {
108 ZZIP_DISK_FILE* file = zzip_mem_entry_fopen (disk, entry);
109 printf (" testing: %s ", entry->zz_name);
110 if (strlen (entry->zz_name) < 24) {
111 printf ("%.*s", 24 - (int) strlen (entry->zz_name),
112 " ");
113 }
114 if (file)
115 {
116 unsigned long crc = crc32 (0L, NULL, 0);
117 unsigned char buffer[1024]; int len;
118 while (0 < (len = zzip_mem_disk_fread (buffer, 1024, 1, file))) {
119 crc = crc32 (crc, buffer, len);
120 }
121
122 zzip_mem_disk_fclose (file);
123 if (crc == (unsigned long) entry->zz_crc32) {
124 printf ("OK\n");
125 } else {
126 printf ("BAD %lx (should be %lx)\n", crc, entry->zz_crc32);
127 test_errors ++;
128 }
129 } else {
130 printf ("ERROR (no such file)\n");
131 test_errors ++;
132 }
133 }
134
135 static void zzip_mem_entry_test_start(void)
136 {
137 test_errors = 0;
138 printf ("Archive: %s\n", archive);
139 }
140
141 static void zzip_mem_entry_test_done(void)
142 {
143 if (test_errors == 0) {
144 printf ("No errors detected in compressed data of %s\n", archive);
145 } else {
146 printf ("%i errors detected in compressed data of %s\n",
147 test_errors, archive);
148 }
149 }
150
151 /* ------------------------- list ------------------------------------ */
152
153 static char _zzip_time_[30];
154 static char* _zzip_ctime (const time_t* timeval)
155 {
156 struct tm* date = localtime (timeval);
157 sprintf (_zzip_time_, "%02i-%02i-%02i %02i:%02i",
158 date->tm_mon, date->tm_mday, date->tm_year%100,
159 date->tm_hour, date->tm_min);
160 return _zzip_time_;
161 }
162
163 static const char* comprlevel[] = {
164 "stored", "shrunk", "redu:1", "redu:2", "redu:3", "redu:4",
165 "impl:N", "toknze", "defl:N", "defl:B", "impl:B" };
166
167 zzip_off_t sum_usize = 0;
168 zzip_off_t sum_csize = 0;
169 zzip_off_t sum_files = 0;
170 #define L (long)
171
172 static void zzip_mem_entry_direntry_start (void)
173 {
174 sum_usize = 0;
175 sum_csize = 0;
176 sum_files = 0;
177 if (option_verbose) goto verbose;
178 printf(" Length Date & Time Name\n");
179 printf(" -------- ---- ---- ----\n");
180 return;
181 verbose:
182 printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
183 printf("-------- ------ ------- ----- ---- ---- ------ ----\n");
184 }
185
186 static void zzip_mem_entry_direntry_done (void)
187 {
188 char exp = ' ';
189 if (sum_usize / 1024 > 1024*1024*1024) { exp = 'G';
190 sum_usize /= 1024*1024*1024; sum_usize /= 1024*1024*1024; }
191 if (sum_usize > 1024*1024*1024) { exp = 'M';
192 sum_usize /= 1024*1024; sum_csize /= 1024*1024; }
193 if (sum_usize > 1024*1024) { exp = 'K';
194 sum_usize /= 1024; sum_csize /= 1024; }
195 if (option_verbose) goto verbose;
196 printf(" -------- ----\n");
197 printf(" %8li%c %8li %s\n", L sum_usize, exp, L sum_files,
198 sum_files == 1 ? "file" : "files");
199 return;
200 verbose:
201 printf("-------- ------ ------- ----- ----\n");
202 printf("%8li%c %8li%c %3li%% %8li %s\n",
203 L sum_usize, exp, L sum_csize, exp,
204 L (100 - (sum_csize*100/sum_usize)), L sum_files,
205 sum_files == 1 ? "file" : "files");
206 }
207
208 static void zzip_mem_entry_direntry(ZZIP_MEM_ENTRY* entry)
209 {
210 char* name = zzip_mem_entry_to_name (entry);
211 zzip_off_t usize = zzip_mem_entry_usize (entry);
212 zzip_off_t csize = zzip_mem_entry_csize (entry);
213 int compr = zzip_mem_entry_data_comprlevel (entry);
214 time_t mtime = entry->zz_mktime;
215 long crc32 = entry->zz_crc32;
216 const char* comment = zzip_mem_entry_to_comment (entry);
217 char exp = ' ';
218
219 sum_usize += usize;
220 sum_csize += csize;
221 sum_files += 1;
222
223 if (usize / 1024 > 1024*1024*1024) { exp = 'G';
224 usize /= 1024*1024*1024; usize /= 1024*1024*1024; }
225 if (usize > 1024*1024*1024) { exp = 'M';
226 usize /= 1024*1024; csize /= 1024*1024; }
227 if (usize > 1024*1024) { exp = 'K';
228 usize /= 1024; csize /= 1024; }
229
230 if (! comment) comment = "";
231 if (*name == '\n') name++;
232
233 if (option_verbose) {
234 long percentage;
235
236 percentage = usize ? (L (100 - (csize*100/usize))) : 0; /* 0% if file size is 0 */
237 printf("%8li%c %s %8li%c%3li%% %s %8lx %s %s\n",
238 L usize, exp, comprlevel[compr], L csize, exp,
239 percentage,
240 _zzip_ctime(&mtime), crc32, name, comment);
241 } else {
242 printf(" %8li%c %s %s %s\n",
243 L usize, exp, _zzip_ctime(&mtime), name, comment);
244 }
245 }
246
247 static void zzip_mem_entry_listfiles(ZZIP_MEM_DISK* disk, char* filespec)
248 {
249 zzip_mem_entry_direntry_start ();
250 ___ ZZIP_MEM_ENTRY* entry = 0;
251 while ((entry = zzip_mem_disk_findmatch(disk, filespec, entry, 0, 0)))
252 zzip_mem_entry_direntry (entry); ____;
253 zzip_mem_entry_direntry_done ();
254 }
255
256 static void zzip_mem_entry_listall(ZZIP_MEM_DISK* disk)
257 {
258 zzip_mem_entry_direntry_start ();
259 ___ ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
260 for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
261 zzip_mem_entry_direntry (entry); ____;
262 zzip_mem_entry_direntry_done ();
263 }
264
265 static void zzip_mem_entry_testfiles(ZZIP_MEM_DISK* disk, char* filespec)
266 {
267 zzip_mem_entry_test_start ();
268 ___ ZZIP_MEM_ENTRY* entry = 0;
269 while ((entry = zzip_mem_disk_findmatch(disk, filespec, entry, 0, 0)))
270 zzip_mem_entry_test (disk, entry); ____;
271 zzip_mem_entry_test_done ();
272 }
273
274 static void zzip_mem_entry_testall(ZZIP_MEM_DISK* disk)
275 {
276 zzip_mem_entry_test_start ();
277 ___ ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
278 for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
279 zzip_mem_entry_test (disk, entry); ____;
280 zzip_mem_entry_test_done ();
281 }
282
283 static void zzip_mem_entry_pipefiles(ZZIP_MEM_DISK* disk, char* filespec)
284 {
285 ZZIP_MEM_ENTRY* entry = 0;
286 while ((entry = zzip_mem_disk_findmatch(disk, filespec, entry, 0, 0)))
287 zzip_mem_entry_pipe (disk, entry, stdout);
288 }
289
290 static void zzip_mem_entry_pipeall(ZZIP_MEM_DISK* disk)
291 {
292 ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
293 for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
294 zzip_mem_entry_pipe (disk, entry, stdout);
295 }
296
297 static void zzip_mem_entry_makefiles(ZZIP_MEM_DISK* disk, char* filespec)
298 {
299 ZZIP_MEM_ENTRY* entry = 0;
300 while ((entry = zzip_mem_disk_findmatch(disk, filespec, entry, 0, 0)))
301 zzip_mem_entry_make (disk, entry);
302 }
303
304 static void zzip_mem_entry_makeall(ZZIP_MEM_DISK* disk)
305 {
306 ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
307 for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
308 zzip_mem_entry_make (disk, entry);
309 }
310
311 int
312 main (int argc, char ** argv)
313 {
314 int argn; int archname = 0; int filespec = 0;
315 ZZIP_MEM_DISK* disk;
316 if (argc <= 1 || ! strcmp (argv[1], "--help"))
317 {
318 printf (usage);
319 return 0;
320 }
321 if (! strcmp (argv[1], "--version"))
322 {
323 printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n");
324 return 0;
325 }
326
327 for (argn=1; argn < argc; argn++) {
328 if ((argv[argn])[0] == '-') {
329 int x = 1;
330 while ((argv[argn])[x]) {
331 switch (argv[argn][x]) {
332 case 'l': option_list++; break;
333 case 'p': option_pipe++; break;
334 case 't': option_testcrc++; break;
335 case 'v': option_verbose++; break;
336 case 'b': option_binary++; break;
337 case 'C': option_nocase++; break;
338 case 'j': option_junkpaths++; break;
339 case 'L': option_dosfiles++; break;
340 case 'n': option_keepold++; break;
341 case 'o': option_overwrite++; break;
342 case 'q': option_quiet++; break;
343 case 'X': option_permbits++; break;
344 }
345 x++;
346 }
347 (argv[argn])[0] = 0;
348 } else {
349 if (! archname) { archname = argn; continue; }
350 if (! filespec) { filespec = argn; continue; }
351 }
352 }
353
354 if (! archname) {
355 printf (usage);
356 return 0;
357 } else archive = argv[archname];
358
359 disk = zzip_mem_disk_open (argv[archname]);
360 if (! disk) {
361 perror(argv[archname]);
362 return -1;
363 }
364
365 if (option_list || option_verbose) {
366 if (! filespec) {
367 zzip_mem_entry_listall (disk);
368 } else {
369 for (argn=filespec; argn < argc; argn++) {
370 if (argv[argn][0]) {
371 zzip_mem_entry_listfiles (disk, argv[argn]);
372 }
373 }
374 }
375 } else if (option_pipe) {
376 if (! filespec) {
377 zzip_mem_entry_pipeall (disk);
378 } else {
379 for (argn=filespec; argn < argc; argn++) {
380 if (argv[argn][0]) {
381 zzip_mem_entry_pipefiles (disk, argv[argn]);
382 }
383 }
384 }
385 } else if (option_testcrc) {
386 if (! filespec) {
387 zzip_mem_entry_testall (disk);
388 } else {
389 for (argn=filespec; argn < argc; argn++) {
390 if (argv[argn][0]) {
391 zzip_mem_entry_testfiles (disk, argv[argn]);
392 }
393 }
394 }
395 } else {
396 if (! filespec) {
397 zzip_mem_entry_makeall (disk);
398 } else {
399 for (argn=filespec; argn < argc; argn++) {
400 if (argv[argn][0]) {
401 zzip_mem_entry_makefiles (disk, argv[argn]);
402 }
403 }
404 }
405 }
406
407 return status;
408 }
409
410 /*
411 * Local variables:
412 * c-file-style: "stroustrup"
413 * End:
414 */