11 #include "zlib/zlib.h"
\r
14 /* public globals */
\r
15 //int gUnzipQuiet = 0; /* flag controls error messages */
\r
17 #define ERROR_CORRUPT "The zipfile seems to be corrupt, please check it"
\r
18 #define ERROR_FILESYSTEM "Your filesystem seems to be corrupt, please check it"
\r
19 #define ERROR_UNSUPPORTED "The format of this zipfile is not supported, please recompress it"
\r
21 #define INFLATE_INPUT_BUFFER_MAX 16384
\r
23 #define MIN(x,y) ((x)<(y)?(x):(y))
\r
28 #if 1 //def __DEBUG_PRINT
\r
29 #define logerror printf
\r
30 #define errormsg(str1,def,fname) printf("%s: " #def ": " str1 "\n", fname);
\r
32 #define logerror(x...)
\r
33 #define errormsg(x...)
\r
36 /* Print a error message */
\r
37 //void errormsg(const char* extmsg, const char* usermsg, const char* zipname) {
\r
38 /* Output to the user with no internal detail */
\r
39 // if (!gUnzipQuiet)
\r
40 // printf("Error in zipfile %s\n%s\n", zipname, usermsg);
\r
41 /* Output to log file with all informations */
\r
42 // logerror("Error in zipfile %s: %s\n", zipname, extmsg);
\r
43 // printf("Error in zipfile %s: %s\n", zipname, extmsg);
\r
46 /* -------------------------------------------------------------------------
\r
48 ------------------------------------------------------------------------- */
\r
50 /* Use these to avoid structure padding and byte-ordering problems */
\r
51 static UINT16 read_word (char *buf) {
\r
52 unsigned char *ubuf = (unsigned char *) buf;
\r
54 return ((UINT16)ubuf[1] << 8) | (UINT16)ubuf[0];
\r
57 /* Use these to avoid structure padding and byte-ordering problems */
\r
58 static UINT32 read_dword (char *buf) {
\r
59 unsigned char *ubuf = (unsigned char *) buf;
\r
61 return ((UINT32)ubuf[3] << 24) | ((UINT32)ubuf[2] << 16) | ((UINT32)ubuf[1] << 8) | (UINT32)ubuf[0];
\r
64 /* Locate end-of-central-dir sig in buffer and return offset
\r
66 *offset offset of cent dir start in buffer
\r
69 !=0 found, *offset valid
\r
71 static int ecd_find_sig (char *buffer, int buflen, int *offset)
\r
73 static char ecdsig[] = { 'P', 'K', 0x05, 0x06 };
\r
75 for (i=buflen-22; i>=0; i--) {
\r
76 if (memcmp(buffer+i, ecdsig, 4) == 0) {
\r
84 /* Read ecd data in zip structure
\r
86 zip->fp, zip->length zip file
\r
88 zip->ecd, zip->ecd_length ecd data
\r
90 static int ecd_read(ZIP* zip) {
\r
92 int buf_length = 1024; /* initial buffer length */
\r
97 if (buf_length > zip->length)
\r
98 buf_length = zip->length;
\r
100 if (fseek(zip->fp, zip->length - buf_length, SEEK_SET) != 0) {
\r
104 /* allocate buffer */
\r
105 buf = (char*)malloc( buf_length );
\r
110 if (fread( buf, buf_length, 1, zip->fp ) != 1) {
\r
115 if (ecd_find_sig(buf, buf_length, &offset)) {
\r
116 zip->ecd_length = buf_length - offset;
\r
118 zip->ecd = (char*)malloc( zip->ecd_length );
\r
124 memcpy(zip->ecd, buf + offset, zip->ecd_length);
\r
132 if (buf_length < zip->length) {
\r
133 /* double buffer */
\r
134 buf_length = 2*buf_length;
\r
136 logerror("Retry reading of zip ecd for %d bytes\n",buf_length);
\r
144 /* offsets in end of central directory structure */
\r
145 #define ZIPESIG 0x00
\r
146 #define ZIPEDSK 0x04
\r
147 #define ZIPECEN 0x06
\r
148 #define ZIPENUM 0x08
\r
149 #define ZIPECENN 0x0a
\r
150 #define ZIPECSZ 0x0c
\r
151 #define ZIPEOFST 0x10
\r
152 #define ZIPECOML 0x14
\r
153 #define ZIPECOM 0x16
\r
155 /* offsets in central directory entry structure */
\r
156 #define ZIPCENSIG 0x0
\r
157 #define ZIPCVER 0x4
\r
159 #define ZIPCVXT 0x6
\r
160 #define ZIPCEXOS 0x7
\r
161 #define ZIPCFLG 0x8
\r
162 #define ZIPCMTHD 0xa
\r
163 #define ZIPCTIM 0xc
\r
164 #define ZIPCDAT 0xe
\r
165 #define ZIPCCRC 0x10
\r
166 #define ZIPCSIZ 0x14
\r
167 #define ZIPCUNC 0x18
\r
168 #define ZIPCFNL 0x1c
\r
169 #define ZIPCXTL 0x1e
\r
170 #define ZIPCCML 0x20
\r
171 #define ZIPDSK 0x22
\r
172 #define ZIPINT 0x24
\r
173 #define ZIPEXT 0x26
\r
174 #define ZIPOFST 0x2a
\r
175 #define ZIPCFN 0x2e
\r
177 /* offsets in local file header structure */
\r
178 #define ZIPLOCSIG 0x00
\r
179 #define ZIPVER 0x04
\r
180 #define ZIPGENFLG 0x06
\r
181 #define ZIPMTHD 0x08
\r
182 #define ZIPTIME 0x0a
\r
183 #define ZIPDATE 0x0c
\r
184 #define ZIPCRC 0x0e
\r
185 #define ZIPSIZE 0x12
\r
186 #define ZIPUNCMP 0x16
\r
187 #define ZIPFNLN 0x1a
\r
188 #define ZIPXTRALN 0x1c
\r
189 #define ZIPNAME 0x1e
\r
191 /* Opens a zip stream for reading
\r
193 !=0 success, zip stream
\r
196 ZIP* openzip(const char* zipfile) {
\r
198 ZIP* zip = (ZIP*)malloc( sizeof(ZIP) );
\r
204 zip->fp = fopen(zipfile, "rb");
\r
206 errormsg ("Opening for reading", ERROR_FILESYSTEM, zipfile);
\r
212 if (fseek(zip->fp, 0L, SEEK_END) != 0) {
\r
213 errormsg ("Seeking to end", ERROR_FILESYSTEM, zipfile);
\r
220 zip->length = ftell(zip->fp);
\r
221 if (zip->length < 0) {
\r
222 errormsg ("Get file size", ERROR_FILESYSTEM, zipfile);
\r
227 if (zip->length == 0) {
\r
228 errormsg ("Empty file", ERROR_CORRUPT, zipfile);
\r
234 /* read ecd data */
\r
235 if (ecd_read(zip)!=0) {
\r
236 errormsg ("Reading ECD (end of central directory)", ERROR_CORRUPT, zipfile);
\r
242 /* compile ecd info */
\r
243 zip->end_of_cent_dir_sig = read_dword (zip->ecd+ZIPESIG);
\r
244 zip->number_of_this_disk = read_word (zip->ecd+ZIPEDSK);
\r
245 zip->number_of_disk_start_cent_dir = read_word (zip->ecd+ZIPECEN);
\r
246 zip->total_entries_cent_dir_this_disk = read_word (zip->ecd+ZIPENUM);
\r
247 zip->total_entries_cent_dir = read_word (zip->ecd+ZIPECENN);
\r
248 zip->size_of_cent_dir = read_dword (zip->ecd+ZIPECSZ);
\r
249 zip->offset_to_start_of_cent_dir = read_dword (zip->ecd+ZIPEOFST);
\r
250 zip->zipfile_comment_length = read_word (zip->ecd+ZIPECOML);
\r
251 zip->zipfile_comment = zip->ecd+ZIPECOM;
\r
253 /* verify that we can work with this zipfile (no disk spanning allowed) */
\r
254 if ((zip->number_of_this_disk != zip->number_of_disk_start_cent_dir) ||
\r
255 (zip->total_entries_cent_dir_this_disk != zip->total_entries_cent_dir) ||
\r
256 (zip->total_entries_cent_dir < 1)) {
\r
257 errormsg("Cannot span disks", ERROR_UNSUPPORTED, zipfile);
\r
264 if (fseek(zip->fp, zip->offset_to_start_of_cent_dir, SEEK_SET)!=0) {
\r
265 errormsg ("Seeking to central directory", ERROR_CORRUPT, zipfile);
\r
272 /* read from start of central directory */
\r
273 zip->cd = (char*)malloc( zip->size_of_cent_dir );
\r
281 if (fread(zip->cd, zip->size_of_cent_dir, 1, zip->fp)!=1) {
\r
282 errormsg ("Reading central directory", ERROR_CORRUPT, zipfile);
\r
297 zip->zip = (char*)malloc(strlen(zipfile)+1);
\r
305 strcpy(zip->zip, zipfile);
\r
310 /* Reads the current entry from a zip stream
\r
317 struct zipent* readzip(ZIP* zip) {
\r
319 /* end of directory */
\r
320 if (zip->cd_pos >= zip->size_of_cent_dir)
\r
323 /* compile zipent info */
\r
324 zip->ent.cent_file_header_sig = read_dword (zip->cd+zip->cd_pos+ZIPCENSIG);
\r
325 zip->ent.version_made_by = *(zip->cd+zip->cd_pos+ZIPCVER);
\r
326 zip->ent.host_os = *(zip->cd+zip->cd_pos+ZIPCOS);
\r
327 zip->ent.version_needed_to_extract = *(zip->cd+zip->cd_pos+ZIPCVXT);
\r
328 zip->ent.os_needed_to_extract = *(zip->cd+zip->cd_pos+ZIPCEXOS);
\r
329 zip->ent.general_purpose_bit_flag = read_word (zip->cd+zip->cd_pos+ZIPCFLG);
\r
330 zip->ent.compression_method = read_word (zip->cd+zip->cd_pos+ZIPCMTHD);
\r
331 zip->ent.last_mod_file_time = read_word (zip->cd+zip->cd_pos+ZIPCTIM);
\r
332 zip->ent.last_mod_file_date = read_word (zip->cd+zip->cd_pos+ZIPCDAT);
\r
333 zip->ent.crc32 = read_dword (zip->cd+zip->cd_pos+ZIPCCRC);
\r
334 zip->ent.compressed_size = read_dword (zip->cd+zip->cd_pos+ZIPCSIZ);
\r
335 zip->ent.uncompressed_size = read_dword (zip->cd+zip->cd_pos+ZIPCUNC);
\r
336 zip->ent.filename_length = read_word (zip->cd+zip->cd_pos+ZIPCFNL);
\r
337 zip->ent.extra_field_length = read_word (zip->cd+zip->cd_pos+ZIPCXTL);
\r
338 zip->ent.file_comment_length = read_word (zip->cd+zip->cd_pos+ZIPCCML);
\r
339 zip->ent.disk_number_start = read_word (zip->cd+zip->cd_pos+ZIPDSK);
\r
340 zip->ent.internal_file_attrib = read_word (zip->cd+zip->cd_pos+ZIPINT);
\r
341 zip->ent.external_file_attrib = read_dword (zip->cd+zip->cd_pos+ZIPEXT);
\r
342 zip->ent.offset_lcl_hdr_frm_frst_disk = read_dword (zip->cd+zip->cd_pos+ZIPOFST);
\r
344 /* check to see if filename length is illegally long (past the size of this directory
\r
346 if (zip->cd_pos + ZIPCFN + zip->ent.filename_length > zip->size_of_cent_dir)
\r
348 errormsg("Invalid filename length in directory", ERROR_CORRUPT,zip->zip);
\r
352 /* copy filename */
\r
353 free(zip->ent.name);
\r
354 zip->ent.name = (char*)malloc(zip->ent.filename_length + 1);
\r
355 memcpy(zip->ent.name, zip->cd+zip->cd_pos+ZIPCFN, zip->ent.filename_length);
\r
356 zip->ent.name[zip->ent.filename_length] = 0;
\r
358 /* skip to next entry in central dir */
\r
359 zip->cd_pos += ZIPCFN + zip->ent.filename_length + zip->ent.extra_field_length + zip->ent.file_comment_length;
\r
364 /* Closes a zip stream */
\r
365 void closezip(ZIP* zip) {
\r
367 free(zip->ent.name);
\r
370 /* only if not suspended */
\r
377 /* Suspend access to a zip file (release file handler)
\r
381 A suspended zip is automatically reopened at first call of
\r
382 readuncompressd() or readcompressed() functions
\r
384 void suspendzip(ZIP* zip) {
\r
391 /* Revive a suspended zip file (reopen file handler)
\r
396 ==0 error (zip must be closed with closezip)
\r
398 static ZIP* revivezip(ZIP* zip) {
\r
400 zip->fp = fopen(zip->zip, "rb");
\r
409 /* Reset a zip stream to the first entry
\r
413 ZIP file must be opened and not suspended
\r
415 void rewindzip(ZIP* zip) {
\r
419 /* Seek zip->fp to compressed data
\r
424 int seekcompresszip(ZIP* zip, struct zipent* ent) {
\r
429 if (!revivezip(zip))
\r
433 if (fseek(zip->fp, ent->offset_lcl_hdr_frm_frst_disk, SEEK_SET)!=0) {
\r
434 errormsg ("Seeking to header", ERROR_CORRUPT, zip->zip);
\r
438 if (fread(buf, ZIPNAME, 1, zip->fp)!=1) {
\r
439 errormsg ("Reading header", ERROR_CORRUPT, zip->zip);
\r
444 UINT16 filename_length = read_word (buf+ZIPFNLN);
\r
445 UINT16 extra_field_length = read_word (buf+ZIPXTRALN);
\r
447 /* calculate offset to data and fseek() there */
\r
448 offset = ent->offset_lcl_hdr_frm_frst_disk + ZIPNAME + filename_length + extra_field_length;
\r
450 if (fseek(zip->fp, offset, SEEK_SET) != 0) {
\r
451 errormsg ("Seeking to compressed data", ERROR_CORRUPT, zip->zip);
\r
462 in_file stream to inflate
\r
463 in_size size of the compressed data to read
\r
464 out_size size of decompressed data
\r
466 out_data buffer for decompressed data
\r
470 990525 rewritten for use with zlib MLR
\r
472 static int inflate_file(FILE* in_file, unsigned in_size, unsigned char* out_data, unsigned out_size)
\r
475 unsigned char* in_buffer;
\r
476 z_stream d_stream; /* decompression stream */
\r
478 d_stream.zalloc = 0;
\r
479 d_stream.zfree = 0;
\r
480 d_stream.opaque = 0;
\r
482 d_stream.next_in = 0;
\r
483 d_stream.avail_in = 0;
\r
484 d_stream.next_out = out_data;
\r
485 d_stream.avail_out = out_size;
\r
487 err = inflateInit2(&d_stream, -MAX_WBITS);
\r
488 /* windowBits is passed < 0 to tell that there is no zlib header.
\r
489 * Note that in this case inflate *requires* an extra "dummy" byte
\r
490 * after the compressed stream in order to complete decompression and
\r
491 * return Z_STREAM_END.
\r
495 logerror("inflateInit error: %d\n", err);
\r
499 in_buffer = (unsigned char*)malloc(INFLATE_INPUT_BUFFER_MAX+1);
\r
507 logerror("inflate error: compressed size too small\n");
\r
511 d_stream.next_in = in_buffer;
\r
512 d_stream.avail_in = fread (in_buffer, 1, MIN(in_size, INFLATE_INPUT_BUFFER_MAX), in_file);
\r
513 in_size -= d_stream.avail_in;
\r
515 d_stream.avail_in++; /* add dummy byte at end of compressed data */
\r
517 err = inflate(&d_stream, Z_NO_FLUSH);
\r
518 if (err == Z_STREAM_END)
\r
522 logerror("inflate error: %d\n", err);
\r
528 err = inflateEnd(&d_stream);
\r
531 logerror("inflateEnd error: %d\n", err);
\r
538 if ((d_stream.avail_out > 0) || (in_size > 0))
\r
540 logerror("zip size mismatch. %i\n", in_size);
\r
547 /* Read compressed data
\r
549 data compressed data read
\r
554 int readcompresszip(ZIP* zip, struct zipent* ent, char* data) {
\r
555 int err = seekcompresszip(zip,ent);
\r
559 if (fread(data, ent->compressed_size, 1, zip->fp)!=1) {
\r
560 errormsg ("Reading compressed data", ERROR_CORRUPT, zip->zip);
\r
567 /* Read UNcompressed data
\r
569 data UNcompressed data
\r
574 int readuncompresszip(ZIP* zip, struct zipent* ent, char* data) {
\r
575 if (ent->compression_method == 0x0000) {
\r
576 /* file is not compressed, simply stored */
\r
578 /* check if size are equal */
\r
579 if (ent->compressed_size != ent->uncompressed_size) {
\r
580 errormsg("Wrong uncompressed size in store compression", ERROR_CORRUPT,zip->zip);
\r
584 return readcompresszip(zip,ent,data);
\r
585 } else if (ent->compression_method == 0x0008) {
\r
586 /* file is compressed using "Deflate" method */
\r
587 if (ent->version_needed_to_extract > 0x14) {
\r
588 errormsg("Version too new", ERROR_UNSUPPORTED,zip->zip);
\r
592 if (ent->os_needed_to_extract != 0x00) {
\r
593 errormsg("OS not supported", ERROR_UNSUPPORTED,zip->zip);
\r
597 if (ent->disk_number_start != zip->number_of_this_disk) {
\r
598 errormsg("Cannot span disks", ERROR_UNSUPPORTED,zip->zip);
\r
602 /* read compressed data */
\r
603 if (seekcompresszip(zip,ent)!=0) {
\r
607 /* configure inflate */
\r
608 if (inflate_file( zip->fp, ent->compressed_size, (unsigned char*)data, ent->uncompressed_size))
\r
610 errormsg("Inflating compressed data", ERROR_CORRUPT, zip->zip);
\r
616 errormsg("Compression method unsupported", ERROR_UNSUPPORTED, zip->zip);
\r