8 #include "zlib/zlib.h"
\r
10 /* public globals */
\r
11 //int gUnzipQuiet = 0; /* flag controls error messages */
\r
13 #define ERROR_CORRUPT "The zipfile seems to be corrupt, please check it"
\r
14 #define ERROR_FILESYSTEM "Your filesystem seems to be corrupt, please check it"
\r
15 #define ERROR_UNSUPPORTED "The format of this zipfile is not supported, please recompress it"
\r
17 #define INFLATE_INPUT_BUFFER_MAX 16384
\r
19 #define MIN(x,y) ((x)<(y)?(x):(y))
\r
24 #if 1 //def __DEBUG_PRINT
\r
25 #define logerror printf
\r
26 #define errormsg(str1,def,fname) printf("%s: " #def ": " str1 "\n", fname);
\r
28 #define logerror(x...)
\r
29 #define errormsg(x...)
\r
32 /* Print a error message */
\r
33 //void errormsg(const char* extmsg, const char* usermsg, const char* zipname) {
\r
34 /* Output to the user with no internal detail */
\r
35 // if (!gUnzipQuiet)
\r
36 // printf("Error in zipfile %s\n%s\n", zipname, usermsg);
\r
37 /* Output to log file with all informations */
\r
38 // logerror("Error in zipfile %s: %s\n", zipname, extmsg);
\r
39 // printf("Error in zipfile %s: %s\n", zipname, extmsg);
\r
42 /* -------------------------------------------------------------------------
\r
44 ------------------------------------------------------------------------- */
\r
46 /* Use these to avoid structure padding and byte-ordering problems */
\r
47 static UINT16 read_word (char *buf) {
\r
48 unsigned char *ubuf = (unsigned char *) buf;
\r
50 return ((UINT16)ubuf[1] << 8) | (UINT16)ubuf[0];
\r
53 /* Use these to avoid structure padding and byte-ordering problems */
\r
54 static UINT32 read_dword (char *buf) {
\r
55 unsigned char *ubuf = (unsigned char *) buf;
\r
57 return ((UINT32)ubuf[3] << 24) | ((UINT32)ubuf[2] << 16) | ((UINT32)ubuf[1] << 8) | (UINT32)ubuf[0];
\r
60 /* Locate end-of-central-dir sig in buffer and return offset
\r
62 *offset offset of cent dir start in buffer
\r
65 !=0 found, *offset valid
\r
67 static int ecd_find_sig (char *buffer, int buflen, int *offset)
\r
69 static char ecdsig[] = { 'P', 'K', 0x05, 0x06 };
\r
71 for (i=buflen-22; i>=0; i--) {
\r
72 if (memcmp(buffer+i, ecdsig, 4) == 0) {
\r
80 /* Read ecd data in zip structure
\r
82 zip->fp, zip->length zip file
\r
84 zip->ecd, zip->ecd_length ecd data
\r
86 static int ecd_read(ZIP* zip) {
\r
88 int buf_length = 1024; /* initial buffer length */
\r
93 if (buf_length > zip->length)
\r
94 buf_length = zip->length;
\r
96 if (fseek(zip->fp, zip->length - buf_length, SEEK_SET) != 0) {
\r
100 /* allocate buffer */
\r
101 buf = (char*)malloc( buf_length );
\r
106 if (fread( buf, buf_length, 1, zip->fp ) != 1) {
\r
111 if (ecd_find_sig(buf, buf_length, &offset)) {
\r
112 zip->ecd_length = buf_length - offset;
\r
114 zip->ecd = (char*)malloc( zip->ecd_length );
\r
120 memcpy(zip->ecd, buf + offset, zip->ecd_length);
\r
128 if (buf_length < zip->length) {
\r
129 /* double buffer */
\r
130 buf_length = 2*buf_length;
\r
132 logerror("Retry reading of zip ecd for %d bytes\n",buf_length);
\r
140 /* offsets in end of central directory structure */
\r
141 #define ZIPESIG 0x00
\r
142 #define ZIPEDSK 0x04
\r
143 #define ZIPECEN 0x06
\r
144 #define ZIPENUM 0x08
\r
145 #define ZIPECENN 0x0a
\r
146 #define ZIPECSZ 0x0c
\r
147 #define ZIPEOFST 0x10
\r
148 #define ZIPECOML 0x14
\r
149 #define ZIPECOM 0x16
\r
151 /* offsets in central directory entry structure */
\r
152 #define ZIPCENSIG 0x0
\r
153 #define ZIPCVER 0x4
\r
155 #define ZIPCVXT 0x6
\r
156 #define ZIPCEXOS 0x7
\r
157 #define ZIPCFLG 0x8
\r
158 #define ZIPCMTHD 0xa
\r
159 #define ZIPCTIM 0xc
\r
160 #define ZIPCDAT 0xe
\r
161 #define ZIPCCRC 0x10
\r
162 #define ZIPCSIZ 0x14
\r
163 #define ZIPCUNC 0x18
\r
164 #define ZIPCFNL 0x1c
\r
165 #define ZIPCXTL 0x1e
\r
166 #define ZIPCCML 0x20
\r
167 #define ZIPDSK 0x22
\r
168 #define ZIPINT 0x24
\r
169 #define ZIPEXT 0x26
\r
170 #define ZIPOFST 0x2a
\r
171 #define ZIPCFN 0x2e
\r
173 /* offsets in local file header structure */
\r
174 #define ZIPLOCSIG 0x00
\r
175 #define ZIPVER 0x04
\r
176 #define ZIPGENFLG 0x06
\r
177 #define ZIPMTHD 0x08
\r
178 #define ZIPTIME 0x0a
\r
179 #define ZIPDATE 0x0c
\r
180 #define ZIPCRC 0x0e
\r
181 #define ZIPSIZE 0x12
\r
182 #define ZIPUNCMP 0x16
\r
183 #define ZIPFNLN 0x1a
\r
184 #define ZIPXTRALN 0x1c
\r
185 #define ZIPNAME 0x1e
\r
187 /* Opens a zip stream for reading
\r
189 !=0 success, zip stream
\r
192 ZIP* openzip(const char* zipfile) {
\r
194 ZIP* zip = (ZIP*)malloc( sizeof(ZIP) );
\r
200 zip->fp = fopen(zipfile, "rb");
\r
202 errormsg ("Opening for reading", ERROR_FILESYSTEM, zipfile);
\r
208 if (fseek(zip->fp, 0L, SEEK_END) != 0) {
\r
209 errormsg ("Seeking to end", ERROR_FILESYSTEM, zipfile);
\r
216 zip->length = ftell(zip->fp);
\r
217 if (zip->length < 0) {
\r
218 errormsg ("Get file size", ERROR_FILESYSTEM, zipfile);
\r
223 if (zip->length == 0) {
\r
224 errormsg ("Empty file", ERROR_CORRUPT, zipfile);
\r
230 /* read ecd data */
\r
231 if (ecd_read(zip)!=0) {
\r
232 errormsg ("Reading ECD (end of central directory)", ERROR_CORRUPT, zipfile);
\r
238 /* compile ecd info */
\r
239 zip->end_of_cent_dir_sig = read_dword (zip->ecd+ZIPESIG);
\r
240 zip->number_of_this_disk = read_word (zip->ecd+ZIPEDSK);
\r
241 zip->number_of_disk_start_cent_dir = read_word (zip->ecd+ZIPECEN);
\r
242 zip->total_entries_cent_dir_this_disk = read_word (zip->ecd+ZIPENUM);
\r
243 zip->total_entries_cent_dir = read_word (zip->ecd+ZIPECENN);
\r
244 zip->size_of_cent_dir = read_dword (zip->ecd+ZIPECSZ);
\r
245 zip->offset_to_start_of_cent_dir = read_dword (zip->ecd+ZIPEOFST);
\r
246 zip->zipfile_comment_length = read_word (zip->ecd+ZIPECOML);
\r
247 zip->zipfile_comment = zip->ecd+ZIPECOM;
\r
249 /* verify that we can work with this zipfile (no disk spanning allowed) */
\r
250 if ((zip->number_of_this_disk != zip->number_of_disk_start_cent_dir) ||
\r
251 (zip->total_entries_cent_dir_this_disk != zip->total_entries_cent_dir) ||
\r
252 (zip->total_entries_cent_dir < 1)) {
\r
253 errormsg("Cannot span disks", ERROR_UNSUPPORTED, zipfile);
\r
260 if (fseek(zip->fp, zip->offset_to_start_of_cent_dir, SEEK_SET)!=0) {
\r
261 errormsg ("Seeking to central directory", ERROR_CORRUPT, zipfile);
\r
268 /* read from start of central directory */
\r
269 zip->cd = (char*)malloc( zip->size_of_cent_dir );
\r
277 if (fread(zip->cd, zip->size_of_cent_dir, 1, zip->fp)!=1) {
\r
278 errormsg ("Reading central directory", ERROR_CORRUPT, zipfile);
\r
293 zip->zip = (char*)malloc(strlen(zipfile)+1);
\r
301 strcpy(zip->zip, zipfile);
\r
306 /* Reads the current entry from a zip stream
\r
313 struct zipent* readzip(ZIP* zip) {
\r
315 /* end of directory */
\r
316 if (zip->cd_pos >= zip->size_of_cent_dir)
\r
319 /* compile zipent info */
\r
320 zip->ent.cent_file_header_sig = read_dword (zip->cd+zip->cd_pos+ZIPCENSIG);
\r
321 zip->ent.version_made_by = *(zip->cd+zip->cd_pos+ZIPCVER);
\r
322 zip->ent.host_os = *(zip->cd+zip->cd_pos+ZIPCOS);
\r
323 zip->ent.version_needed_to_extract = *(zip->cd+zip->cd_pos+ZIPCVXT);
\r
324 zip->ent.os_needed_to_extract = *(zip->cd+zip->cd_pos+ZIPCEXOS);
\r
325 zip->ent.general_purpose_bit_flag = read_word (zip->cd+zip->cd_pos+ZIPCFLG);
\r
326 zip->ent.compression_method = read_word (zip->cd+zip->cd_pos+ZIPCMTHD);
\r
327 zip->ent.last_mod_file_time = read_word (zip->cd+zip->cd_pos+ZIPCTIM);
\r
328 zip->ent.last_mod_file_date = read_word (zip->cd+zip->cd_pos+ZIPCDAT);
\r
329 zip->ent.crc32 = read_dword (zip->cd+zip->cd_pos+ZIPCCRC);
\r
330 zip->ent.compressed_size = read_dword (zip->cd+zip->cd_pos+ZIPCSIZ);
\r
331 zip->ent.uncompressed_size = read_dword (zip->cd+zip->cd_pos+ZIPCUNC);
\r
332 zip->ent.filename_length = read_word (zip->cd+zip->cd_pos+ZIPCFNL);
\r
333 zip->ent.extra_field_length = read_word (zip->cd+zip->cd_pos+ZIPCXTL);
\r
334 zip->ent.file_comment_length = read_word (zip->cd+zip->cd_pos+ZIPCCML);
\r
335 zip->ent.disk_number_start = read_word (zip->cd+zip->cd_pos+ZIPDSK);
\r
336 zip->ent.internal_file_attrib = read_word (zip->cd+zip->cd_pos+ZIPINT);
\r
337 zip->ent.external_file_attrib = read_dword (zip->cd+zip->cd_pos+ZIPEXT);
\r
338 zip->ent.offset_lcl_hdr_frm_frst_disk = read_dword (zip->cd+zip->cd_pos+ZIPOFST);
\r
340 /* check to see if filename length is illegally long (past the size of this directory
\r
342 if (zip->cd_pos + ZIPCFN + zip->ent.filename_length > zip->size_of_cent_dir)
\r
344 errormsg("Invalid filename length in directory", ERROR_CORRUPT,zip->zip);
\r
348 /* copy filename */
\r
349 free(zip->ent.name);
\r
350 zip->ent.name = (char*)malloc(zip->ent.filename_length + 1);
\r
351 memcpy(zip->ent.name, zip->cd+zip->cd_pos+ZIPCFN, zip->ent.filename_length);
\r
352 zip->ent.name[zip->ent.filename_length] = 0;
\r
354 /* skip to next entry in central dir */
\r
355 zip->cd_pos += ZIPCFN + zip->ent.filename_length + zip->ent.extra_field_length + zip->ent.file_comment_length;
\r
360 /* Closes a zip stream */
\r
361 void closezip(ZIP* zip) {
\r
363 free(zip->ent.name);
\r
366 /* only if not suspended */
\r
373 /* Suspend access to a zip file (release file handler)
\r
377 A suspended zip is automatically reopened at first call of
\r
378 readuncompressd() or readcompressed() functions
\r
380 void suspendzip(ZIP* zip) {
\r
387 /* Revive a suspended zip file (reopen file handler)
\r
392 ==0 error (zip must be closed with closezip)
\r
394 static ZIP* revivezip(ZIP* zip) {
\r
396 zip->fp = fopen(zip->zip, "rb");
\r
405 /* Reset a zip stream to the first entry
\r
409 ZIP file must be opened and not suspended
\r
411 void rewindzip(ZIP* zip) {
\r
415 /* Seek zip->fp to compressed data
\r
420 int seekcompresszip(ZIP* zip, struct zipent* ent) {
\r
425 if (!revivezip(zip))
\r
429 if (fseek(zip->fp, ent->offset_lcl_hdr_frm_frst_disk, SEEK_SET)!=0) {
\r
430 errormsg ("Seeking to header", ERROR_CORRUPT, zip->zip);
\r
434 if (fread(buf, ZIPNAME, 1, zip->fp)!=1) {
\r
435 errormsg ("Reading header", ERROR_CORRUPT, zip->zip);
\r
440 UINT16 filename_length = read_word (buf+ZIPFNLN);
\r
441 UINT16 extra_field_length = read_word (buf+ZIPXTRALN);
\r
443 /* calculate offset to data and fseek() there */
\r
444 offset = ent->offset_lcl_hdr_frm_frst_disk + ZIPNAME + filename_length + extra_field_length;
\r
446 if (fseek(zip->fp, offset, SEEK_SET) != 0) {
\r
447 errormsg ("Seeking to compressed data", ERROR_CORRUPT, zip->zip);
\r
458 in_file stream to inflate
\r
459 in_size size of the compressed data to read
\r
460 out_size size of decompressed data
\r
462 out_data buffer for decompressed data
\r
466 990525 rewritten for use with zlib MLR
\r
468 static int inflate_file(FILE* in_file, unsigned in_size, unsigned char* out_data, unsigned out_size)
\r
471 unsigned char* in_buffer;
\r
472 z_stream d_stream; /* decompression stream */
\r
474 d_stream.zalloc = 0;
\r
475 d_stream.zfree = 0;
\r
476 d_stream.opaque = 0;
\r
478 d_stream.next_in = 0;
\r
479 d_stream.avail_in = 0;
\r
480 d_stream.next_out = out_data;
\r
481 d_stream.avail_out = out_size;
\r
483 err = inflateInit2(&d_stream, -MAX_WBITS);
\r
484 /* windowBits is passed < 0 to tell that there is no zlib header.
\r
485 * Note that in this case inflate *requires* an extra "dummy" byte
\r
486 * after the compressed stream in order to complete decompression and
\r
487 * return Z_STREAM_END.
\r
491 logerror("inflateInit error: %d\n", err);
\r
495 in_buffer = (unsigned char*)malloc(INFLATE_INPUT_BUFFER_MAX+1);
\r
503 logerror("inflate error: compressed size too small\n");
\r
507 d_stream.next_in = in_buffer;
\r
508 d_stream.avail_in = fread (in_buffer, 1, MIN(in_size, INFLATE_INPUT_BUFFER_MAX), in_file);
\r
509 in_size -= d_stream.avail_in;
\r
511 d_stream.avail_in++; /* add dummy byte at end of compressed data */
\r
513 err = inflate(&d_stream, Z_NO_FLUSH);
\r
514 if (err == Z_STREAM_END)
\r
518 logerror("inflate error: %d\n", err);
\r
524 err = inflateEnd(&d_stream);
\r
527 logerror("inflateEnd error: %d\n", err);
\r
534 if ((d_stream.avail_out > 0) || (in_size > 0))
\r
536 logerror("zip size mismatch. %i\n", in_size);
\r
543 /* Read compressed data
\r
545 data compressed data read
\r
550 int readcompresszip(ZIP* zip, struct zipent* ent, char* data) {
\r
551 int err = seekcompresszip(zip,ent);
\r
555 if (fread(data, ent->compressed_size, 1, zip->fp)!=1) {
\r
556 errormsg ("Reading compressed data", ERROR_CORRUPT, zip->zip);
\r
563 /* Read UNcompressed data
\r
565 data UNcompressed data
\r
570 int readuncompresszip(ZIP* zip, struct zipent* ent, char* data) {
\r
571 if (ent->compression_method == 0x0000) {
\r
572 /* file is not compressed, simply stored */
\r
574 /* check if size are equal */
\r
575 if (ent->compressed_size != ent->uncompressed_size) {
\r
576 errormsg("Wrong uncompressed size in store compression", ERROR_CORRUPT,zip->zip);
\r
580 return readcompresszip(zip,ent,data);
\r
581 } else if (ent->compression_method == 0x0008) {
\r
582 /* file is compressed using "Deflate" method */
\r
583 if (ent->version_needed_to_extract > 0x14) {
\r
584 errormsg("Version too new", ERROR_UNSUPPORTED,zip->zip);
\r
588 if (ent->os_needed_to_extract != 0x00) {
\r
589 errormsg("OS not supported", ERROR_UNSUPPORTED,zip->zip);
\r
593 if (ent->disk_number_start != zip->number_of_this_disk) {
\r
594 errormsg("Cannot span disks", ERROR_UNSUPPORTED,zip->zip);
\r
598 /* read compressed data */
\r
599 if (seekcompresszip(zip,ent)!=0) {
\r
603 /* configure inflate */
\r
604 if (inflate_file( zip->fp, ent->compressed_size, (unsigned char*)data, ent->uncompressed_size))
\r
606 errormsg("Inflating compressed data", ERROR_CORRUPT, zip->zip);
\r
612 errormsg("Compression method unsupported", ERROR_UNSUPPORTED, zip->zip);
\r