-
Notifications
You must be signed in to change notification settings - Fork 0
/
unzip.c
764 lines (639 loc) · 18.9 KB
/
unzip.c
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/***************************************************************************
unzip.c
Support for retrieving files from zipfiles
***************************************************************************/
#include "unzip.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* public globals */
int gUnzipQuiet = 0; /* flag controls error messages */
/* private globals */
static int gZipfileCorrupt = 0; /* flag is set when corruption is detected during inflate */
/*----------------------------------
inflate.c support
----------------------------------*/
extern int mame_inflate (void);
unsigned char *slide; /* 32K sliding window for inflate.c */
unsigned char *g_nextbyte; /* pointer to next byte of input */
unsigned char *g_inputlimit; /* pointer to first byte beyond input buffer */
unsigned char *g_outbuf; /* pointer to next byte in output buffer */
unsigned char *g_outputlimit; /* pointer to first byte beyond output buffer */
/* flush data to output buffer -- used by mame_inflate() */
void inflate_FLUSH (unsigned char *buffer, unsigned long n)
{
if ( (g_outbuf + n) <= g_outputlimit)
{
memcpy (g_outbuf, buffer, n);
g_outbuf += n;
}
else
{
/* inflate attempted write beyond end of output buffer */
gZipfileCorrupt = 1;
}
}
/* read next byte of input -- used by mame_inflate() */
int mame_nextbyte (void)
{
if (g_nextbyte < g_inputlimit)
return *g_nextbyte++;
/* inflate attempted read beyond end of input buffer */
gZipfileCorrupt = 1;
return 256; /* signal EOF */
}
/*----------------------------------
end of inflate.c support
----------------------------------*/
/* -------------------------------------------------------------------------
Mame support
------------------------------------------------------------------------- */
#define ERROR_CORRUPT "The zipfile seems to be corrupt, please check it"
#define ERROR_FILESYSTEM "Your filesystem seems to be corrupt, please check it"
#define ERROR_UNSUPPORTED "The format of this zipfile is not supported, please recompress it"
/* Print a error message */
void errormsg(const char* extmsg, const char* usermsg, const char* zipname) {
/* Output to the user with no internal detail */
if (!gUnzipQuiet)
printf("Error in zipfile %s\n%s\n", zipname, usermsg);
/* Output to log file with all informations */
}
/* -------------------------------------------------------------------------
Unzip support
------------------------------------------------------------------------- */
/* Use these to avoid structure padding and byte-ordering problems */
static word read_word (char *buf) {
unsigned char *ubuf = (unsigned char *) buf;
return ((word)ubuf[1] << 8) | (word)ubuf[0];
}
/* Use these to avoid structure padding and byte-ordering problems */
static dword read_dword (char *buf) {
unsigned char *ubuf = (unsigned char *) buf;
return ((dword)ubuf[3] << 24) | ((dword)ubuf[2] << 16) | ((dword)ubuf[1] << 8) | (dword)ubuf[0];
}
/* Locate end-of-central-dir sig in buffer and return offset
out:
*offset offset of cent dir start in buffer
return:
==0 not found
!=0 found, *offset valid
*/
static int ecd_find_sig (char *buffer, int buflen, int *offset)
{
static char ecdsig[] = { 'P', 'K', 0x05, 0x06 };
int i;
for (i=buflen-22; i>=0; i--) {
if (memcmp(buffer+i, ecdsig, 4) == 0) {
*offset = i;
return 1;
}
}
return 0;
}
/* Read ecd data in zip structure
in:
zip->fp, zip->length zip file
out:
zip->ecd, zip->ecd_length ecd data
*/
static int ecd_read(ZIP* zip) {
char* buf;
int buf_length = 1024; /* initial buffer length */
while (1) {
int offset;
if (buf_length > zip->length)
buf_length = zip->length;
if (fseek(zip->fp, zip->length - buf_length, SEEK_SET) != 0) {
return -1;
}
/* allocate buffer */
buf = (char*)malloc( buf_length );
if (!buf) {
return -1;
}
if (fread( buf, buf_length, 1, zip->fp ) != 1) {
free(buf);
return -1;
}
if (ecd_find_sig(buf, buf_length, &offset)) {
zip->ecd_length = buf_length - offset;
zip->ecd = (char*)malloc( zip->ecd_length );
if (!zip->ecd) {
free(buf);
return -1;
}
memcpy(zip->ecd, buf + offset, zip->ecd_length);
free(buf);
return 0;
}
free(buf);
if (buf_length < zip->length) {
/* double buffer */
buf_length = 2*buf_length;
} else {
return -1;
}
}
}
/* offsets in end of central directory structure */
#define ZIPESIG 0x00
#define ZIPEDSK 0x04
#define ZIPECEN 0x06
#define ZIPENUM 0x08
#define ZIPECENN 0x0a
#define ZIPECSZ 0x0c
#define ZIPEOFST 0x10
#define ZIPECOML 0x14
#define ZIPECOM 0x16
/* offsets in central directory entry structure */
#define ZIPCENSIG 0x0
#define ZIPCVER 0x4
#define ZIPCOS 0x5
#define ZIPCVXT 0x6
#define ZIPCEXOS 0x7
#define ZIPCFLG 0x8
#define ZIPCMTHD 0xa
#define ZIPCTIM 0xc
#define ZIPCDAT 0xe
#define ZIPCCRC 0x10
#define ZIPCSIZ 0x14
#define ZIPCUNC 0x18
#define ZIPCFNL 0x1c
#define ZIPCXTL 0x1e
#define ZIPCCML 0x20
#define ZIPDSK 0x22
#define ZIPINT 0x24
#define ZIPEXT 0x26
#define ZIPOFST 0x2a
#define ZIPCFN 0x2e
/* offsets in local file header structure */
#define ZIPLOCSIG 0x00
#define ZIPVER 0x04
#define ZIPGENFLG 0x06
#define ZIPMTHD 0x08
#define ZIPTIME 0x0a
#define ZIPDATE 0x0c
#define ZIPCRC 0x0e
#define ZIPSIZE 0x12
#define ZIPUNCMP 0x16
#define ZIPFNLN 0x1a
#define ZIPXTRALN 0x1c
#define ZIPNAME 0x1e
/* Opens a zip stream for reading
return:
!=0 success, zip stream
==0 error
*/
ZIP* openzip(const char* zipfile) {
/* allocate */
ZIP* zip = (ZIP*)malloc( sizeof(ZIP) );
if (!zip) {
return 0;
}
/* open */
zip->fp = fopen(zipfile, "rb");
if (!zip->fp) {
errormsg ("Opening for reading", ERROR_FILESYSTEM, zipfile);
free(zip);
return 0;
}
/* go to end */
if (fseek(zip->fp, 0L, SEEK_END) != 0) {
errormsg ("Seeking to end", ERROR_FILESYSTEM, zipfile);
fclose(zip->fp);
free(zip);
return 0;
}
/* get length */
zip->length = ftell(zip->fp);
if (zip->length < 0) {
errormsg ("Get file size", ERROR_FILESYSTEM, zipfile);
fclose(zip->fp);
free(zip);
return 0;
}
if (zip->length == 0) {
errormsg ("Empty file", ERROR_CORRUPT, zipfile);
fclose(zip->fp);
free(zip);
return 0;
}
/* read ecd data */
if (ecd_read(zip)!=0) {
errormsg ("Reading ECD (end of central directory)", ERROR_CORRUPT, zipfile);
fclose(zip->fp);
free(zip);
return 0;
}
/* compile ecd info */
zip->end_of_cent_dir_sig = read_dword (zip->ecd+ZIPESIG);
zip->number_of_this_disk = read_word (zip->ecd+ZIPEDSK);
zip->number_of_disk_start_cent_dir = read_word (zip->ecd+ZIPECEN);
zip->total_entries_cent_dir_this_disk = read_word (zip->ecd+ZIPENUM);
zip->total_entries_cent_dir = read_word (zip->ecd+ZIPECENN);
zip->size_of_cent_dir = read_dword (zip->ecd+ZIPECSZ);
zip->offset_to_start_of_cent_dir = read_dword (zip->ecd+ZIPEOFST);
zip->zipfile_comment_length = read_word (zip->ecd+ZIPECOML);
zip->zipfile_comment = zip->ecd+ZIPECOM;
/* verify that we can work with this zipfile (no disk spanning allowed) */
if ((zip->number_of_this_disk != zip->number_of_disk_start_cent_dir) ||
(zip->total_entries_cent_dir_this_disk != zip->total_entries_cent_dir) ||
(zip->total_entries_cent_dir < 1)) {
errormsg("Cannot span disks", ERROR_UNSUPPORTED, zip->zip);
free(zip->ecd);
fclose(zip->fp);
free(zip);
return 0;
}
if (fseek(zip->fp, zip->offset_to_start_of_cent_dir, SEEK_SET)!=0) {
errormsg ("Seeking to central directory", ERROR_CORRUPT, zipfile);
free(zip->ecd);
fclose(zip->fp);
free(zip);
return 0;
}
/* read from start of central directory */
zip->cd = (char*)malloc( zip->size_of_cent_dir );
if (!zip->cd) {
free(zip->ecd);
fclose(zip->fp);
free(zip);
return 0;
}
if (fread(zip->cd, zip->size_of_cent_dir, 1, zip->fp)!=1) {
errormsg ("Reading central directory", ERROR_CORRUPT, zipfile);
free(zip->cd);
free(zip->ecd);
fclose(zip->fp);
free(zip);
return 0;
}
/* reset ent */
zip->ent.name = 0;
/* rewind */
zip->cd_pos = 0;
/* file name */
zip->zip = (char*)malloc(strlen(zipfile)+1);
if (!zip->zip) {
free(zip->cd);
free(zip->ecd);
fclose(zip->fp);
free(zip);
return 0;
}
strcpy(zip->zip, zipfile);
return zip;
}
/* Reads the current entry from a zip stream
in:
zip opened zip
return:
!=0 success
==0 error
*/
struct zipent* readzip(ZIP* zip) {
/* end of directory */
if (zip->cd_pos >= zip->size_of_cent_dir)
return 0;
/* compile zipent info */
zip->ent.cent_file_header_sig = read_dword (zip->cd+zip->cd_pos+ZIPCENSIG);
zip->ent.version_made_by = *(zip->cd+zip->cd_pos+ZIPCVER);
zip->ent.host_os = *(zip->cd+zip->cd_pos+ZIPCOS);
zip->ent.version_needed_to_extract = *(zip->cd+zip->cd_pos+ZIPCVXT);
zip->ent.os_needed_to_extract = *(zip->cd+zip->cd_pos+ZIPCEXOS);
zip->ent.general_purpose_bit_flag = read_word (zip->cd+zip->cd_pos+ZIPCFLG);
zip->ent.compression_method = read_word (zip->cd+zip->cd_pos+ZIPCMTHD);
zip->ent.last_mod_file_time = read_word (zip->cd+zip->cd_pos+ZIPCTIM);
zip->ent.last_mod_file_date = read_word (zip->cd+zip->cd_pos+ZIPCDAT);
zip->ent.crc32 = read_dword (zip->cd+zip->cd_pos+ZIPCCRC);
zip->ent.compressed_size = read_dword (zip->cd+zip->cd_pos+ZIPCSIZ);
zip->ent.uncompressed_size = read_dword (zip->cd+zip->cd_pos+ZIPCUNC);
zip->ent.filename_length = read_word (zip->cd+zip->cd_pos+ZIPCFNL);
zip->ent.extra_field_length = read_word (zip->cd+zip->cd_pos+ZIPCXTL);
zip->ent.file_comment_length = read_word (zip->cd+zip->cd_pos+ZIPCCML);
zip->ent.disk_number_start = read_word (zip->cd+zip->cd_pos+ZIPDSK);
zip->ent.internal_file_attrib = read_word (zip->cd+zip->cd_pos+ZIPINT);
zip->ent.external_file_attrib = read_dword (zip->cd+zip->cd_pos+ZIPEXT);
zip->ent.offset_lcl_hdr_frm_frst_disk = read_dword (zip->cd+zip->cd_pos+ZIPOFST);
/* copy filename */
free(zip->ent.name);
zip->ent.name = (char*)malloc(zip->ent.filename_length + 1);
memcpy(zip->ent.name, zip->cd+zip->cd_pos+ZIPCFN, zip->ent.filename_length);
zip->ent.name[zip->ent.filename_length] = 0;
/* skip to next entry in central dir */
zip->cd_pos += ZIPCFN + zip->ent.filename_length + zip->ent.extra_field_length + zip->ent.file_comment_length;
return &zip->ent;
}
/* Closes a zip stream */
void closezip(ZIP* zip) {
/* release all */
free(zip->ent.name);
free(zip->cd);
free(zip->ecd);
/* only if not suspended */
if (zip->fp)
fclose(zip->fp);
free(zip->zip);
free(zip);
}
/* Suspend access to a zip file (release file handler)
in:
zip opened zip
note:
A suspended zip is automatically reopened at first call of
readuncompressd() or readcompressed() functions
*/
void suspendzip(ZIP* zip) {
if (zip->fp) {
fclose(zip->fp);
zip->fp = 0;
}
}
/* Revive a suspended zip file (reopen file handler)
in:
zip suspended zip
return:
zip success
==0 error (zip must be closed with closezip)
*/
static ZIP* revivezip(ZIP* zip) {
if (!zip->fp) {
zip->fp = fopen(zip->zip, "rb");
if (!zip->fp) {
return 0;
}
}
return zip;
}
/* Reset a zip stream to the first entry
in:
zip opened zip
note:
ZIP file must be opened and not suspended
*/
void rewindzip(ZIP* zip) {
zip->cd_pos = 0;
}
/* Read compressed data
out:
data compressed data read
return:
==0 success
<0 error
*/
int readcompresszip(ZIP* zip, struct zipent* ent, char* data) {
char buf[ZIPNAME];
long offset;
if (!zip->fp) {
if (!revivezip(zip))
return -1;
}
if (fseek(zip->fp, ent->offset_lcl_hdr_frm_frst_disk, SEEK_SET)!=0) {
errormsg ("Seeking to header", ERROR_CORRUPT, zip->zip);
return -1;
}
if (fread(buf, ZIPNAME, 1, zip->fp)!=1) {
errormsg ("Reading header", ERROR_CORRUPT, zip->zip);
return -1;
}
{
word filename_length = read_word (buf+ZIPFNLN);
word extra_field_length = read_word (buf+ZIPXTRALN);
/* calculate offset to data and fseek() there */
offset = ent->offset_lcl_hdr_frm_frst_disk + ZIPNAME + filename_length + extra_field_length;
if (fseek(zip->fp, offset, SEEK_SET) != 0) {
errormsg ("Seeking to compressed data", ERROR_CORRUPT, zip->zip);
return -1;
}
if (fread(data, ent->compressed_size, 1, zip->fp)!=1) {
errormsg ("Reading compressed data", ERROR_CORRUPT, zip->zip);
return -1;
}
}
return 0;
}
/* Read UNcompressed data
out:
data UNcompressed data
return:
==0 success
<0 error
*/
int readuncompresszip(ZIP* zip, struct zipent* ent, char* data) {
if (ent->compression_method == 0x0000) {
/* file is not compressed, simply stored */
/* check if size are equal */
if (ent->compressed_size != ent->uncompressed_size) {
errormsg("Wrong uncompressed size in store compression", ERROR_CORRUPT,zip->zip);
return -3;
}
return readcompresszip(zip,ent,data);
} else if (ent->compression_method == 0x0008) {
/* file is compressed using "Deflate" method */
char* compdata;
if (ent->version_needed_to_extract > 0x14) {
errormsg("Version too new", ERROR_UNSUPPORTED,zip->zip);
return -2;
}
if (ent->os_needed_to_extract != 0x00) {
errormsg("OS not supported", ERROR_UNSUPPORTED,zip->zip);
return -2;
}
if (ent->disk_number_start != zip->number_of_this_disk) {
errormsg("Cannot span disks", ERROR_UNSUPPORTED,zip->zip);
return -2;
}
compdata = (char*)malloc( ent->compressed_size );
if (!compdata) {
return -1;
}
/* read compressed data */
if (readcompresszip(zip,ent,compdata)!=0) {
free(compdata);
return -1;
}
/* configure inflate input */
g_nextbyte = (unsigned char*)compdata;
g_inputlimit = g_nextbyte + ent->compressed_size;
/* configure inflate output */
g_outbuf = (unsigned char*)data;
g_outputlimit = g_outbuf + ent->uncompressed_size;
slide = (unsigned char*)malloc(0x8000);
if (!slide) {
free(compdata);
return -1;
}
/* Reset Corrupt flag */
gZipfileCorrupt = 0;
/* inflate the compressed file (now in memory) */
if (mame_inflate()!=0 || gZipfileCorrupt) {
errormsg("Inflating compressed data", ERROR_CORRUPT,zip->zip);
free(slide);
free(compdata);
return -3;
}
free(slide);
free(compdata);
return 0;
} else {
errormsg("Compression method unsupported", ERROR_UNSUPPORTED, zip->zip);
return -2;
}
}
/* -------------------------------------------------------------------------
Zip cache support
------------------------------------------------------------------------- */
/* Use the zip cache */
#define ZIP_CACHE
#ifdef ZIP_CACHE
/* ZIP cache entries */
#define ZIP_CACHE_MAX 2
/* ZIP cache buffer LRU ( Last Recently Used )
zip_cache_map[0] is the newer
zip_cache_map[ZIP_CACHE_MAX-1] is the older
*/
static ZIP* zip_cache_map[ZIP_CACHE_MAX];
static ZIP* cache_openzip(const char* zipfile) {
ZIP* zip;
unsigned i;
/* search in the cache buffer */
for(i=0;i<ZIP_CACHE_MAX;++i) {
if (zip_cache_map[i] && strcmp(zip_cache_map[i]->zip,zipfile)==0) {
/* found */
unsigned j;
/* reset the zip directory */
rewindzip( zip_cache_map[i] );
/* store */
zip = zip_cache_map[i];
/* shift */
for(j=i;j>0;--j)
zip_cache_map[j] = zip_cache_map[j-1];
/* set the first entry */
zip_cache_map[0] = zip;
return zip_cache_map[0];
}
}
/* open the zip */
zip = openzip( zipfile );
if (!zip)
return 0;
/* close the oldest entry */
if (zip_cache_map[ZIP_CACHE_MAX-1]) {
/* close last zip */
closezip(zip_cache_map[ZIP_CACHE_MAX-1]);
/* reset the entry */
zip_cache_map[ZIP_CACHE_MAX-1] = 0;
}
/* shift */
for(i=ZIP_CACHE_MAX-1;i>0;--i)
zip_cache_map[i] = zip_cache_map[i-1];
/* set the first entry */
zip_cache_map[0] = zip;
return zip_cache_map[0];
}
static void cache_closezip(ZIP* zip) {
unsigned i;
/* search in the cache buffer */
for(i=0;i<ZIP_CACHE_MAX;++i) {
if (zip_cache_map[i]==zip) {
/* close zip */
closezip(zip);
/* reset cache entry */
zip_cache_map[i] = 0;
return;
}
}
/* not found */
/* close zip */
closezip(zip);
}
#define cache_suspendzip(a) suspendzip(a)
#else
#define cache_openzip(a) openzip(a)
#define cache_closezip(a) closezip(a)
#define cache_suspendzip(a) closezip(a)
#endif
/* -------------------------------------------------------------------------
Backward MAME compatibility
------------------------------------------------------------------------- */
/* Compare two filename
note:
don't check directory in zip and ignore case
*/
static int equal_filename(const char* zipfile, const char* file) {
const char* s1 = file;
/* start comparison after last / */
const char* s2 = strrchr(zipfile,'/');
if (s2)
++s2;
else
s2 = zipfile;
while (*s1 && toupper(*s1)==toupper(*s2)) {
++s1;
++s2;
}
return !*s1 && !*s2;
}
/* Pass the path to the zipfile and the name of the file within the zipfile.
buf will be set to point to the uncompressed image of that zipped file.
length will be set to the length of the uncompressed data. */
int /* error */ load_zipped_file (const char* zipfile, const char* filename, unsigned char** buf, unsigned int* length) {
ZIP* zip;
struct zipent* ent;
zip = cache_openzip(zipfile);
if (!zip)
return -1;
ent = readzip(zip);
while (ent) {
/* NS981003: support for "load by CRC" */
char crc[9];
sprintf(crc,"%08x",ent->crc32);
if (equal_filename(ent->name, filename) ||
!strcmp(crc, filename))
{
*length = ent->uncompressed_size;
*buf = (unsigned char*)malloc( *length );
if (!*buf) {
cache_closezip(zip);
return -1;
}
if (readuncompresszip(zip, ent, (char*)*buf)!=0) {
free(*buf);
cache_closezip(zip);
return -1;
}
cache_suspendzip(zip);
return 0;
}
/* next entry */
ent = readzip(zip);
}
cache_suspendzip(zip);
return -1;
}
/* Pass the path to the zipfile and the name of the file within the zipfile.
sum will be set to the CRC-32 of that zipped file. */
/* The caller can preset sum to the expected checksum to enable "load by CRC" */
int /* error */ checksum_zipped_file (const char *zipfile, const char *filename, unsigned int *length, unsigned int *sum) {
ZIP* zip;
struct zipent* ent;
zip = cache_openzip(zipfile);
if (!zip)
return -1;
ent = readzip(zip);
while (ent) {
/* NS981003: support for "load by CRC" */
if (equal_filename(ent->name, filename) ||
(*sum && ent->crc32 == *sum))
{
*length = ent->uncompressed_size;
*sum = ent->crc32;
cache_suspendzip(zip);
return 0;
}
/* next entry */
ent = readzip(zip);
}
cache_suspendzip(zip);
return -1;
}