2 // ZIP file support (mostly ripped from MAME--thx MAME team!)
4 // Added by James L. Hammons
5 // (C) 2010 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- -------------------------------------------------------------
11 // JLH 01/16/2010 Created this log ;-)
24 int gUnzipQuiet = 0; /* flag controls error messages */
27 #define ERROR_CORRUPT "The zipfile seems to be corrupt, please check it"
28 #define ERROR_FILESYSTEM "Your filesystem seems to be corrupt, please check it"
29 #define ERROR_UNSUPPORTED "The format of this zipfile is not supported, please recompress it"
31 #define INFLATE_INPUT_BUFFER_MAX 16384
33 #define MIN(x,y) ((x)<(y)?(x):(y))
36 /* Print a error message */
37 void errormsg(const char * extmsg, const char * usermsg, const char * zipname)
39 /* Output to the user with no internal detail */
41 printf("Error in zipfile %s\n%s\n", zipname, usermsg);
42 /* Output to log file with all informations */
43 WriteLog("Error in zipfile %s: %s\n", zipname, extmsg);
46 /* -------------------------------------------------------------------------
48 ------------------------------------------------------------------------- */
50 /* Use these to avoid structure padding and byte-ordering problems */
51 static uint16_t read_word (char *buf) {
52 unsigned char *ubuf = (unsigned char *) buf;
54 return ((uint16_t)ubuf[1] << 8) | (uint16_t)ubuf[0];
57 /* Use these to avoid structure padding and byte-ordering problems */
58 static uint32_t read_dword (char *buf) {
59 unsigned char *ubuf = (unsigned char *) buf;
61 return ((uint32_t)ubuf[3] << 24) | ((uint32_t)ubuf[2] << 16) | ((uint32_t)ubuf[1] << 8) | (uint32_t)ubuf[0];
64 /* Locate end-of-central-dir sig in buffer and return offset
66 *offset offset of cent dir start in buffer
69 !=0 found, *offset valid
71 static int ecd_find_sig (char *buffer, int buflen, int *offset)
73 static char ecdsig[] = { 'P', 'K', 0x05, 0x06 };
75 for (i=buflen-22; i>=0; i--) {
76 if (memcmp(buffer+i, ecdsig, 4) == 0) {
84 /* Read ecd data in zip structure
86 zip->fp, zip->length zip file
88 zip->ecd, zip->ecd_length ecd data
90 static int ecd_read(ZIP * zip)
93 int buf_length = 1024; /* initial buffer length */
99 if (buf_length > zip->length)
100 buf_length = zip->length;
102 if (fseek(zip->fp, zip->length - buf_length, SEEK_SET) != 0)
107 /* allocate buffer */
108 buf = (char *)malloc(buf_length);
114 if (fread(buf, 1, buf_length, zip->fp) != buf_length)
120 if (ecd_find_sig(buf, buf_length, &offset)) {
121 zip->ecd_length = buf_length - offset;
123 zip->ecd = (char*)malloc( zip->ecd_length );
129 memcpy(zip->ecd, buf + offset, zip->ecd_length);
137 if (buf_length < zip->length) {
139 buf_length = 2*buf_length;
141 WriteLog("Retry reading of zip ecd for %d bytes\n",buf_length);
149 /* offsets in end of central directory structure */
154 #define ZIPECENN 0x0a
156 #define ZIPEOFST 0x10
157 #define ZIPECOML 0x14
160 /* offsets in central directory entry structure */
161 #define ZIPCENSIG 0x0
182 /* offsets in local file header structure */
183 #define ZIPLOCSIG 0x00
185 #define ZIPGENFLG 0x06
191 #define ZIPUNCMP 0x16
193 #define ZIPXTRALN 0x1c
197 // Opens a zip stream for reading
199 // !=0 success, zip stream
202 ZIP * openzip(int pathtype, int pathindex, const char * zipfile)
205 ZIP * zip = (ZIP *)malloc(sizeof(ZIP));
212 zip->fp = fopen(zipfile, "rb");
215 errormsg("Opening for reading", ERROR_FILESYSTEM, zipfile);
221 if (fseek(zip->fp, 0L, SEEK_END) != 0)
223 errormsg ("Seeking to end", ERROR_FILESYSTEM, zipfile);
230 zip->length = ftell(zip->fp);
233 errormsg("Get file size", ERROR_FILESYSTEM, zipfile);
238 if (zip->length == 0)
240 errormsg ("Empty file", ERROR_CORRUPT, zipfile);
247 if (ecd_read(zip) != 0)
249 errormsg("Reading ECD (end of central directory)", ERROR_CORRUPT, zipfile);
255 /* compile ecd info */
256 zip->end_of_cent_dir_sig = read_dword (zip->ecd+ZIPESIG);
257 zip->number_of_this_disk = read_word (zip->ecd+ZIPEDSK);
258 zip->number_of_disk_start_cent_dir = read_word (zip->ecd+ZIPECEN);
259 zip->total_entries_cent_dir_this_disk = read_word (zip->ecd+ZIPENUM);
260 zip->total_entries_cent_dir = read_word (zip->ecd+ZIPECENN);
261 zip->size_of_cent_dir = read_dword (zip->ecd+ZIPECSZ);
262 zip->offset_to_start_of_cent_dir = read_dword (zip->ecd+ZIPEOFST);
263 zip->zipfile_comment_length = read_word (zip->ecd+ZIPECOML);
264 zip->zipfile_comment = zip->ecd+ZIPECOM;
266 /* verify that we can work with this zipfile (no disk spanning allowed) */
267 if ((zip->number_of_this_disk != zip->number_of_disk_start_cent_dir) ||
268 (zip->total_entries_cent_dir_this_disk != zip->total_entries_cent_dir) ||
269 (zip->total_entries_cent_dir < 1))
271 errormsg("Cannot span disks", ERROR_UNSUPPORTED, zipfile);
278 if (fseek(zip->fp, zip->offset_to_start_of_cent_dir, SEEK_SET) != 0)
280 errormsg("Seeking to central directory", ERROR_CORRUPT, zipfile);
287 /* read from start of central directory */
288 zip->cd = (char *)malloc(zip->size_of_cent_dir);
297 if (fread(zip->cd, 1, zip->size_of_cent_dir, zip->fp) != zip->size_of_cent_dir)
299 errormsg("Reading central directory", ERROR_CORRUPT, zipfile);
314 zip->zip = (char*)malloc(strlen(zipfile)+1);
323 strcpy(zip->zip, zipfile);
324 zip->pathtype = pathtype;
325 zip->pathindex = pathindex;
330 /* Reads the current entry from a zip stream
337 struct zipent* readzip(ZIP* zip) {
339 /* end of directory */
340 if (zip->cd_pos >= zip->size_of_cent_dir)
343 /* compile zipent info */
344 zip->ent.cent_file_header_sig = read_dword (zip->cd+zip->cd_pos+ZIPCENSIG);
345 zip->ent.version_made_by = *(zip->cd+zip->cd_pos+ZIPCVER);
346 zip->ent.host_os = *(zip->cd+zip->cd_pos+ZIPCOS);
347 zip->ent.version_needed_to_extract = *(zip->cd+zip->cd_pos+ZIPCVXT);
348 zip->ent.os_needed_to_extract = *(zip->cd+zip->cd_pos+ZIPCEXOS);
349 zip->ent.general_purpose_bit_flag = read_word (zip->cd+zip->cd_pos+ZIPCFLG);
350 zip->ent.compression_method = read_word (zip->cd+zip->cd_pos+ZIPCMTHD);
351 zip->ent.last_mod_file_time = read_word (zip->cd+zip->cd_pos+ZIPCTIM);
352 zip->ent.last_mod_file_date = read_word (zip->cd+zip->cd_pos+ZIPCDAT);
353 zip->ent.crc32 = read_dword (zip->cd+zip->cd_pos+ZIPCCRC);
354 zip->ent.compressed_size = read_dword (zip->cd+zip->cd_pos+ZIPCSIZ);
355 zip->ent.uncompressed_size = read_dword (zip->cd+zip->cd_pos+ZIPCUNC);
356 zip->ent.filename_length = read_word (zip->cd+zip->cd_pos+ZIPCFNL);
357 zip->ent.extra_field_length = read_word (zip->cd+zip->cd_pos+ZIPCXTL);
358 zip->ent.file_comment_length = read_word (zip->cd+zip->cd_pos+ZIPCCML);
359 zip->ent.disk_number_start = read_word (zip->cd+zip->cd_pos+ZIPDSK);
360 zip->ent.internal_file_attrib = read_word (zip->cd+zip->cd_pos+ZIPINT);
361 zip->ent.external_file_attrib = read_dword (zip->cd+zip->cd_pos+ZIPEXT);
362 zip->ent.offset_lcl_hdr_frm_frst_disk = read_dword (zip->cd+zip->cd_pos+ZIPOFST);
364 /* check to see if filename length is illegally long (past the size of this directory
366 if (zip->cd_pos + ZIPCFN + zip->ent.filename_length > zip->size_of_cent_dir)
368 errormsg("Invalid filename length in directory", ERROR_CORRUPT,zip->zip);
374 zip->ent.name = (char*)malloc(zip->ent.filename_length + 1);
375 memcpy(zip->ent.name, zip->cd+zip->cd_pos+ZIPCFN, zip->ent.filename_length);
376 zip->ent.name[zip->ent.filename_length] = 0;
378 /* skip to next entry in central dir */
379 zip->cd_pos += ZIPCFN + zip->ent.filename_length + zip->ent.extra_field_length + zip->ent.file_comment_length;
384 /* Closes a zip stream */
385 void closezip(ZIP * zip)
391 /* only if not suspended */
398 /* Suspend access to a zip file (release file handler)
402 A suspended zip is automatically reopened at first call of
403 readuncompressd() or readcompressed() functions
405 void suspendzip(ZIP * zip)
414 /* Revive a suspended zip file (reopen file handler)
419 ==0 error (zip must be closed with closezip)
421 static ZIP * revivezip(ZIP * zip)
425 zip->fp = fopen(zip->zip, "rb");
433 /* Reset a zip stream to the first entry
437 ZIP file must be opened and not suspended
439 void rewindzip(ZIP * zip)
445 // Seek zip->fp to compressed data
450 int seekcompresszip(ZIP * zip, struct zipent * ent)
461 if (fseek(zip->fp, ent->offset_lcl_hdr_frm_frst_disk, SEEK_SET) != 0)
463 errormsg("Seeking to header", ERROR_CORRUPT, zip->zip);
467 if (fread(buf, 1, ZIPNAME, zip->fp) != ZIPNAME)
469 errormsg("Reading header", ERROR_CORRUPT, zip->zip);
474 uint16_t filename_length = read_word(buf+ZIPFNLN);
475 uint16_t extra_field_length = read_word(buf+ZIPXTRALN);
477 // calculate offset to data and fseek() there
478 offset = ent->offset_lcl_hdr_frm_frst_disk + ZIPNAME + filename_length + extra_field_length;
480 if (fseek(zip->fp, offset, SEEK_SET) != 0)
482 errormsg("Seeking to compressed data", ERROR_CORRUPT, zip->zip);
494 // in_file stream to inflate
495 // in_size size of the compressed data to read
497 // out_size size of decompressed data
498 // out_data buffer for decompressed data
502 // 990525 rewritten for use with zlib MLR
504 static int inflate_file(FILE * in_file, unsigned in_size, unsigned char * out_data, unsigned out_size)
507 unsigned char * in_buffer;
508 z_stream d_stream; // decompression stream
514 d_stream.next_in = 0;
515 d_stream.avail_in = 0;
516 d_stream.next_out = out_data;
517 d_stream.avail_out = out_size;
519 err = inflateInit2(&d_stream, -MAX_WBITS);
520 /* windowBits is passed < 0 to tell that there is no zlib header.
521 * Note that in this case inflate *requires* an extra "dummy" byte
522 * after the compressed stream in order to complete decompression and
523 * return Z_STREAM_END.
527 WriteLog("inflateInit error: %d\n", err);
531 in_buffer = (unsigned char*)malloc(INFLATE_INPUT_BUFFER_MAX+1);
539 WriteLog("inflate error: compressed size too small\n");
543 d_stream.next_in = in_buffer;
544 d_stream.avail_in = fread(in_buffer, 1, MIN(in_size, INFLATE_INPUT_BUFFER_MAX), in_file);
545 in_size -= d_stream.avail_in;
547 d_stream.avail_in++; /* add dummy byte at end of compressed data */
549 err = inflate(&d_stream, Z_NO_FLUSH);
550 if (err == Z_STREAM_END)
554 WriteLog("inflate error: %d\n", err);
560 err = inflateEnd(&d_stream);
563 WriteLog("inflateEnd error: %d\n", err);
570 if ((d_stream.avail_out > 0) || (in_size > 0))
572 WriteLog("zip size mismatch. %i\n", in_size);
580 // Read compressed data
582 // data compressed data read
587 int readcompresszip(ZIP * zip, struct zipent * ent, char * data)
589 int err = seekcompresszip(zip, ent);
593 if (fread(data, 1, ent->compressed_size, zip->fp) != ent->compressed_size)
595 errormsg("Reading compressed data", ERROR_CORRUPT, zip->zip);
603 // Read UNcompressed data
605 // data UNcompressed data
610 int readuncompresszip(ZIP * zip, struct zipent * ent, char * data)
612 if (ent->compression_method == 0x0000)
614 /* file is not compressed, simply stored */
616 /* check if size are equal */
617 if (ent->compressed_size != ent->uncompressed_size)
619 errormsg("Wrong uncompressed size in store compression", ERROR_CORRUPT,zip->zip);
623 return readcompresszip(zip, ent, data);
625 else if (ent->compression_method == 0x0008)
627 /* file is compressed using "Deflate" method */
628 if (ent->version_needed_to_extract > 0x14)
630 errormsg("Version too new", ERROR_UNSUPPORTED, zip->zip);
634 if (ent->os_needed_to_extract != 0x00)
636 errormsg("OS not supported", ERROR_UNSUPPORTED, zip->zip);
640 if (ent->disk_number_start != zip->number_of_this_disk)
642 errormsg("Cannot span disks", ERROR_UNSUPPORTED, zip->zip);
646 /* read compressed data */
647 if (seekcompresszip(zip, ent) != 0)
652 /* configure inflate */
653 if (inflate_file(zip->fp, ent->compressed_size, (unsigned char *)data, ent->uncompressed_size))
655 errormsg("Inflating compressed data", ERROR_CORRUPT, zip->zip);
663 errormsg("Compression method unsupported", ERROR_UNSUPPORTED, zip->zip);
668 /* -------------------------------------------------------------------------
670 ------------------------------------------------------------------------- */
672 /* Use the zip cache */
678 /* ZIP cache entries */
679 #define ZIP_CACHE_MAX 5
681 /* ZIP cache buffer LRU ( Last Recently Used )
682 zip_cache_map[0] is the newer
683 zip_cache_map[ZIP_CACHE_MAX-1] is the older
685 static ZIP* zip_cache_map[ZIP_CACHE_MAX];
687 static ZIP* cache_openzip(int pathtype, int pathindex, const char* zipfile) {
691 /* search in the cache buffer */
692 for(i=0;i<ZIP_CACHE_MAX;++i) {
693 if (zip_cache_map[i] && zip_cache_map[i]->pathtype == pathtype && zip_cache_map[i]->pathindex == pathindex && strcmp(zip_cache_map[i]->zip,zipfile)==0) {
698 WriteLog("Zip cache HIT for %s\n", zipfile);
701 /* reset the zip directory */
702 rewindzip( zip_cache_map[i] );
705 zip = zip_cache_map[i];
709 zip_cache_map[j] = zip_cache_map[j-1];
711 /* set the first entry */
712 zip_cache_map[0] = zip;
714 return zip_cache_map[0];
720 WriteLog("Zip cache FAIL for %s\n", zipfile);
724 zip = openzip( pathtype, pathindex, zipfile );
728 /* close the oldest entry */
729 if (zip_cache_map[ZIP_CACHE_MAX-1]) {
731 closezip(zip_cache_map[ZIP_CACHE_MAX-1]);
732 /* reset the entry */
733 zip_cache_map[ZIP_CACHE_MAX-1] = 0;
737 for(i=ZIP_CACHE_MAX-1;i>0;--i)
738 zip_cache_map[i] = zip_cache_map[i-1];
740 /* set the first entry */
741 zip_cache_map[0] = zip;
743 return zip_cache_map[0];
746 static void cache_closezip(ZIP* zip) {
749 /* search in the cache buffer */
750 for(i=0;i<ZIP_CACHE_MAX;++i) {
751 if (zip_cache_map[i]==zip) {
755 /* reset cache entry */
756 zip_cache_map[i] = 0;
767 /* CK980415 added to allow osd code to clear zip cache for auditing--each time
768 the user opens up an audit for a game we should reread the zip */
769 void unzip_cache_clear()
773 /* search in the cache buffer for any zip info and clear it */
774 for(i=0;i<ZIP_CACHE_MAX;++i) {
775 if (zip_cache_map[i] != NULL) {
777 closezip(zip_cache_map[i]);
779 /* reset cache entry */
780 zip_cache_map[i] = 0;
787 #define cache_suspendzip(a) suspendzip(a)
791 #define cache_openzip(a,b,c) openzip(a,b,c)
792 #define cache_closezip(a) closezip(a)
793 #define cache_suspendzip(a) closezip(a)
795 #define unzip_cache_clear()
799 /* -------------------------------------------------------------------------
800 Backward MAME compatibility
801 ------------------------------------------------------------------------- */
804 // Compare two filenames
805 // NOTE: Don't check directory in zip and ignore case
807 static int equal_filename(const char * zipfile, const char * file)
809 // const char * s1 = file;
810 // start comparison after last / also...!
811 const char * s1 = strrchr(file, '/');
816 // start comparison after last /
817 const char * s2 = strrchr(zipfile, '/');
822 //WriteLog("--> Comparing filenames: [%s] <-> [%s]\n", s1, s2);
824 // This assumes that s1 is longer than s2... Might not be! !!! FIX !!!
825 while (*s1 && toupper(*s1) == toupper(*s2))
835 // Pass the path to the zipfile and the name of the file within the zipfile.
836 // buf will be set to point to the uncompressed image of that zipped file.
837 // length will be set to the length of the uncompressed data.
839 int load_zipped_file(int pathtype, int pathindex, const char * zipfile, const char * filename, unsigned char ** buf, uint32_t * length)
844 zip = cache_openzip(pathtype, pathindex, zipfile);
850 /* NS981003: support for "load by CRC" */
854 sprintf(crc, "%08x", (unsigned int)ent->crc32);
856 if (filename == NULL || equal_filename(ent->name, filename)
857 || (ent->crc32 && !strcmp(crc, filename)))
859 *length = ent->uncompressed_size;
861 if (readuncompresszip(zip, ent, (char *)*buf) != 0)
867 cache_suspendzip(zip);
872 cache_suspendzip(zip);