File: unzip-mem.c

package info (click to toggle)
zziplib 0.13.56-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,760 kB
  • ctags: 2,135
  • sloc: sh: 12,147; ansic: 7,392; perl: 2,826; python: 2,190; makefile: 968; sed: 44
file content (420 lines) | stat: -rw-r--r-- 11,776 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 *	Copyright (c) 2003 Guido Draheim <guidod@gmx.de>
 *      Use freely under the restrictions of the ZLIB license.
 *
 *      This file is used as an example to clarify zzipmmap api usage.
 */

#include <zlib.h> /* crc32 */

#include <zzip/memdisk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef ZZIP_HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef ZZIP_HAVE_IO_H
#include <io.h>
#endif

#include <time.h>

#ifdef ZZIP_HAVE_FNMATCH_H
#include <fnmatch.h>
#else
#define fnmatch(x,y,z) strcmp(x,y)
#endif

#ifndef O_BINARY
#define O_BINARY 0
#endif

#define ___ {
#define ____ }

#define EXIT_SUCCESS 0
#define EXIT_WARNINGS 1
#define EXIT_ERRORS 2
#define EXIT_EFORMAT 3
#define EXIT_ENOMEM 4
#define EXIT_ENOTTY 5
#define EXIT_EINFLATE 6
#define EXIT_ENOARCH 9
#define EXIT_EOPTION 10
#define EXIT_ENOFILE 11
#define EXIT_EDISKFULL 50
#define EXIT_EPREMATURE 51
#define EXIT_USERABORT 80
#define EXIT_BADCOMPRESSION 81
#define EXIT_BADPASSWORD 82

static int status = EXIT_SUCCESS;
static int option_list = 0;        /* "-l" */
static int option_pipe = 0;        /* "-p" */
static int option_verbose = 0;     /* "-v" */
static int option_testcrc = 0;     /* "-t" */
static int option_binary = 0;      /* "-b" */
static int option_nocase = 0;      /* "-C" */
static int option_junkpaths = 0;   /* "-j" */
static int option_dosfiles = 0;    /* "-L" */
static int option_keepold = 0;     /* "-n" */
static int option_overwrite = 0;   /* "-o" */
static int option_quiet = 0;       /* "-q" */
static int option_permbits = 0;    /* "-X" */

static const char usage[] = 
{
    "unzzip-mem <zip> [names].. \n"
    "  - unzzip a zip archive.\n"
    "options:\n"
    "  -l list archive files (name, usize, mtime, comments)\n" /* +totals */
    "  -p extract archive files to pipe, i.e. stdout (binary mode)\n"
    "  -t test archive files (check the crc values)\n"
    "  -v verbose list of archive files\n"
    "  -b accept and ignore (force binary extract)\n"
    "  -C match filenames case-insensitively\n"
    "  -j junk paths (do not recreate directory structure)\n"
    "  -L convert dos filenames to lowercase upon extract\n"
    "  -n never overwrite existing files\n"
    "  -o always overwrite existing files\n"
    "  -q quite operation\n"
    "  -X restore user/owner attributes of files\n"
};

static void zzip_mem_entry_pipe(ZZIP_MEM_DISK* disk, 
				ZZIP_MEM_ENTRY* entry, FILE* out)
{
    ZZIP_DISK_FILE* file = zzip_mem_entry_fopen (disk, entry);
    if (file) 
    {
	char buffer[1024]; int len;
	while ((len = zzip_mem_disk_fread (buffer, 1024, 1, file)))
	    fwrite (buffer, len, 1, out);
	
	zzip_mem_disk_fclose (file);
    }
}

static void zzip_mem_entry_make(ZZIP_MEM_DISK* disk, 
				ZZIP_MEM_ENTRY* entry)
{
    FILE* file = fopen (entry->zz_name, "w");
    if (file) { zzip_mem_entry_pipe (disk, entry, file); fclose (file); }
    perror (entry->zz_name);
    if (status < EXIT_WARNINGS) status = EXIT_WARNINGS;
}

/* ------------------------- test ------------------------------------ */

static char* archive = 0;
static int test_errors = 0;

static void zzip_mem_entry_test(ZZIP_MEM_DISK* disk, 
				ZZIP_MEM_ENTRY* entry)
{
    ZZIP_DISK_FILE* file = zzip_mem_entry_fopen (disk, entry);
    printf ("    testing: %s ", entry->zz_name);
    if (strlen (entry->zz_name) < 24) {
	printf ("%.*s", 24 - (int) strlen (entry->zz_name),
		"                        ");
    }
    if (file) 
    {
	unsigned long crc = crc32 (0L, NULL, 0);
	unsigned char buffer[1024]; int len; 
	while ((len = zzip_mem_disk_fread (buffer, 1024, 1, file))) {
	    crc = crc32 (crc, buffer, len);
	}
	
	zzip_mem_disk_fclose (file);
	if (crc == (unsigned long) entry->zz_crc32) {
	    printf ("OK\n");
	} else {
	    printf ("BAD %lx (should be %lx)\n", crc, entry->zz_crc32);
	    test_errors ++;
	} 
    } else {
	printf ("ERROR (no such file)\n");
	test_errors ++;
    }
}

static void zzip_mem_entry_test_start(void)
{
    test_errors = 0;
    printf ("Archive: %s\n", archive);
}

static void zzip_mem_entry_test_done(void)
{
    if (test_errors == 0) {
	printf ("No errors detected in compressed data of %s\n", archive);
    } else {
	printf ("%i errors detected in compressed data of %s\n", 
		test_errors, archive);
    }
}

/* ------------------------- list ------------------------------------ */

static char _zzip_time_[30];
static char* _zzip_ctime (const time_t* timeval) 
{
    struct tm* date = localtime (timeval);
    sprintf (_zzip_time_, "%02i-%02i-%02i %02i:%02i",
	     date->tm_mon, date->tm_mday, date->tm_year%100,
	     date->tm_hour, date->tm_min);
    return _zzip_time_;
}

static const char* comprlevel[] = {
    "stored",   "shrunk",   "redu:1",   "redu:2",   "redu:3",   "redu:4",
    "impl:N",   "toknze",   "defl:N",   "defl:B",   "impl:B" };

zzip_off_t sum_usize = 0;
zzip_off_t sum_csize = 0;
zzip_off_t sum_files = 0;
#define L (long)

static void zzip_mem_entry_direntry_start (void)
{
    sum_usize = 0;
    sum_csize = 0;
    sum_files = 0;
    if (option_verbose) goto verbose;
    printf("  Length    Date & Time     Name\n");
    printf(" --------    ----   ----    ----\n");
    return;
 verbose:
    printf(" Length   Method    Size  Ratio   Date   Time   CRC-32    Name\n");
    printf("--------  ------  ------- -----   ----   ----   ------    ----\n");
}

static void zzip_mem_entry_direntry_done (void)
{
    char exp = ' ';
    if (sum_usize / 1024 > 1024*1024*1024) { exp = 'G';
	sum_usize /= 1024*1024*1024; sum_usize /= 1024*1024*1024; }
    if (sum_usize > 1024*1024*1024) { exp = 'M';
	sum_usize /= 1024*1024; sum_csize /= 1024*1024; }
    if (sum_usize > 1024*1024) { exp = 'K';
	sum_usize /= 1024; sum_csize /= 1024; }
    if (option_verbose) goto verbose;
    printf(" --------                   ----\n");
    printf(" %8li%c           %8li %s\n", L sum_usize, exp, L sum_files,
	   sum_files == 1 ? "file" : "files");
    return;
 verbose:
    printf("--------  ------  ------- -----                           ----\n");
    printf("%8li%c       %8li%c %3li%%                     %8li %s\n",
	   L sum_usize, exp, L sum_csize, exp, 
	   L (100 - (sum_csize*100/sum_usize)), L sum_files, 
	   sum_files == 1 ? "file" : "files");
}

static void zzip_mem_entry_direntry(ZZIP_MEM_ENTRY* entry)
{
    char* name = zzip_mem_entry_to_name (entry);
    zzip_off_t usize = zzip_mem_entry_usize (entry);
    zzip_off_t csize = zzip_mem_entry_csize (entry);
    int compr = zzip_mem_entry_data_comprlevel (entry);
    time_t mtime = entry->zz_mktime;
    long crc32 = entry->zz_crc32;
    const char* comment = zzip_mem_entry_to_comment (entry);
    char exp = ' ';

    sum_usize += usize;
    sum_csize += csize;
    sum_files += 1;

    if (usize / 1024 > 1024*1024*1024) { exp = 'G';
	usize /= 1024*1024*1024; usize /= 1024*1024*1024; }
    if (usize > 1024*1024*1024) { exp = 'M';
	usize /= 1024*1024; csize /= 1024*1024; }
    if (usize > 1024*1024) { exp = 'K';
	usize /= 1024; csize /= 1024; }
   
    if (! comment) comment = "";
    if (*name == '\n') name++;

    if (option_verbose) {
	printf("%8li%c %s %8li%c%3li%%  %s  %8lx  %s %s\n", 
	       L usize, exp, comprlevel[compr], L csize, exp, 
	       L (100 - (csize*100/usize)),
	       _zzip_ctime(&mtime), crc32, name, comment);
    } else {
	printf(" %8li%c %s   %s %s\n", 
	       L usize, exp, _zzip_ctime(&mtime), name, comment);
    }    
}

static void zzip_mem_entry_listfiles(ZZIP_MEM_DISK* disk, char* filespec)
{
    zzip_mem_entry_direntry_start ();
    ___ ZZIP_MEM_ENTRY* entry = 0;
    while ((entry = zzip_mem_disk_findmatch(disk, filespec, entry, 0, 0)))
	zzip_mem_entry_direntry (entry); ____;
    zzip_mem_entry_direntry_done ();
}

static void zzip_mem_entry_listall(ZZIP_MEM_DISK* disk)
{
    zzip_mem_entry_direntry_start ();
    ___ ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
    for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
	zzip_mem_entry_direntry (entry); ____;
    zzip_mem_entry_direntry_done ();
}

static void zzip_mem_entry_testfiles(ZZIP_MEM_DISK* disk, char* filespec)
{
    zzip_mem_entry_test_start ();
    ___ ZZIP_MEM_ENTRY* entry = 0;
    while ((entry = zzip_mem_disk_findmatch(disk, filespec, entry, 0, 0)))
	zzip_mem_entry_test (disk, entry); ____;
    zzip_mem_entry_test_done ();
}

static void zzip_mem_entry_testall(ZZIP_MEM_DISK* disk)
{
    zzip_mem_entry_test_start ();
    ___ ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
    for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
	zzip_mem_entry_test (disk, entry); ____;
    zzip_mem_entry_test_done ();
}

static void zzip_mem_entry_pipefiles(ZZIP_MEM_DISK* disk, char* filespec)
{
    ZZIP_MEM_ENTRY* entry = 0;
    while ((entry = zzip_mem_disk_findmatch(disk, filespec, entry, 0, 0)))
	zzip_mem_entry_pipe (disk, entry, stdout);
}

static void zzip_mem_entry_pipeall(ZZIP_MEM_DISK* disk)
{
    ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
    for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
	zzip_mem_entry_pipe (disk, entry, stdout);
}

static void zzip_mem_entry_makefiles(ZZIP_MEM_DISK* disk, char* filespec)
{
    ZZIP_MEM_ENTRY* entry = 0;
    while ((entry = zzip_mem_disk_findmatch(disk, filespec, entry, 0, 0)))
	zzip_mem_entry_make (disk, entry);
}

static void zzip_mem_entry_makeall(ZZIP_MEM_DISK* disk)
{
    ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
    for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
	zzip_mem_entry_make (disk, entry);
}

int 
main (int argc, char ** argv)
{
    int argn; int archname = 0; int filespec = 0;
    ZZIP_MEM_DISK* disk;
    if (argc <= 1 || ! strcmp (argv[1], "--help"))
    {
	printf (usage);
	return 0;
    }
    if (! strcmp (argv[1], "--version"))
    {
	printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
	return 0;
    }

    for (argn=1; argn < argc; argn++) {
	if ((argv[argn])[0] == '-') {
	    int x = 1;
	    while ((argv[argn])[x]) {
		switch (argv[argn][x]) {
		case 'l': option_list++; break;
		case 'p': option_pipe++; break;
		case 't': option_testcrc++; break;
		case 'v': option_verbose++; break;
		case 'b': option_binary++; break;
		case 'C': option_nocase++; break;
		case 'j': option_junkpaths++; break;
		case 'L': option_dosfiles++; break;
		case 'n': option_keepold++; break;
		case 'o': option_overwrite++; break;
		case 'q': option_quiet++; break;
		case 'X': option_permbits++; break;
		}
		x++;
	    }
	    (argv[argn])[0] = 0;
	} else {
	    if (! archname) { archname = argn; continue; }
	    if (! filespec) { filespec = argn; continue; }
	}
    }

    if (! archname) {
	printf (usage);
	return 0;
    } else archive = argv[archname];

    disk = zzip_mem_disk_open (argv[archname]);
    if (! disk) {
	perror(argv[archname]);
	return -1;
    }

    if (option_list || option_verbose) {
	if (! filespec) {
	    zzip_mem_entry_listall (disk);
	} else {
	    for (argn=filespec; argn < argc; argn++) {
		if (argv[argn][0]) {
		    zzip_mem_entry_listfiles (disk, argv[argn]);
		}
	    }
	}
    } else if (option_pipe) {
	if (! filespec) {
	    zzip_mem_entry_pipeall (disk);
	} else {
	    for (argn=filespec; argn < argc; argn++) {
		if (argv[argn][0]) {
		    zzip_mem_entry_pipefiles (disk, argv[argn]);
		}
	    }
	}
    } else if (option_testcrc) {
	if (! filespec) {
	    zzip_mem_entry_testall (disk);
	} else {
	    for (argn=filespec; argn < argc; argn++) {
		if (argv[argn][0]) {
		    zzip_mem_entry_testfiles (disk, argv[argn]);
		}
	    }
	}
    } else {
	if (! filespec) {
	    zzip_mem_entry_makeall (disk);
	} else {
	    for (argn=filespec; argn < argc; argn++) {
		if (argv[argn][0]) {
		    zzip_mem_entry_makefiles (disk, argv[argn]);
		}
	    }
	}
    }

    return status;
} 

/* 
 * Local variables:
 * c-file-style: "stroustrup"
 * End:
 */