diff options
author | Nils Durner <durner@gnunet.org> | 2008-11-16 22:00:21 +0000 |
---|---|---|
committer | Nils Durner <durner@gnunet.org> | 2008-11-16 22:00:21 +0000 |
commit | 64c63da758619d3a1896c4dcaafd4bf689ab830f (patch) | |
tree | 0c81a5754d6d0aad96f2c8abc2ca060b59d9f7d8 /src | |
parent | e54cd0ec18b824ad63a85412edb49fb5bc2fdd41 (diff) | |
download | libextractor-64c63da758619d3a1896c4dcaafd4bf689ab830f.tar.gz libextractor-64c63da758619d3a1896c4dcaafd4bf689ab830f.zip |
move unzip code to libextractor_common
Diffstat (limited to 'src')
-rw-r--r-- | src/common/ChangeLogUnzip (renamed from src/plugins/oo/ChangeLogUnzip) | 0 | ||||
-rw-r--r-- | src/common/Makefile.am | 4 | ||||
-rw-r--r-- | src/common/unzip.c | 1324 | ||||
-rw-r--r-- | src/common/unzip.h | 126 | ||||
-rw-r--r-- | src/plugins/oo/Makefile.am | 1 | ||||
-rw-r--r-- | src/plugins/oo/ooextractor.c | 1437 |
6 files changed, 1489 insertions, 1403 deletions
diff --git a/src/plugins/oo/ChangeLogUnzip b/src/common/ChangeLogUnzip index 06c70d9..06c70d9 100644 --- a/src/plugins/oo/ChangeLogUnzip +++ b/src/common/ChangeLogUnzip | |||
diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 4ffd5bd..6d2ff77 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am | |||
@@ -19,9 +19,11 @@ libextractor_common_la_LDFLAGS = \ | |||
19 | $(exportflag) $(winexportflag) -version-info @LIB_VERSION_CURRENT@:@LIB_VERSION_REVISION@:@LIB_VERSION_AGE@ $(LIBICONV) | 19 | $(exportflag) $(winexportflag) -version-info @LIB_VERSION_CURRENT@:@LIB_VERSION_REVISION@:@LIB_VERSION_AGE@ $(LIBICONV) |
20 | 20 | ||
21 | libextractor_common_la_LIBADD = \ | 21 | libextractor_common_la_LIBADD = \ |
22 | $(LIBLTDL) $(dlflag) | 22 | $(LIBLTDL) $(dlflag) -lz |
23 | 23 | ||
24 | libextractor_common_la_SOURCES = \ | 24 | libextractor_common_la_SOURCES = \ |
25 | unzip.c \ | ||
26 | unzip.h \ | ||
25 | pack.c \ | 27 | pack.c \ |
26 | pack.h \ | 28 | pack.h \ |
27 | convert.c \ | 29 | convert.c \ |
diff --git a/src/common/unzip.c b/src/common/unzip.c new file mode 100644 index 0000000..60c97b1 --- /dev/null +++ b/src/common/unzip.c | |||
@@ -0,0 +1,1324 @@ | |||
1 | /* | ||
2 | This file is part of libextractor. | ||
3 | (C) 2004, 2008 Vidyut Samanta and Christian Grothoff | ||
4 | |||
5 | libextractor is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | libextractor is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with libextractor; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | #include "platform.h" | ||
22 | #include <ctype.h> | ||
23 | #include "extractor.h" | ||
24 | #include "unzip.h" | ||
25 | |||
26 | #define CASESENSITIVITY (0) | ||
27 | #define MAXFILENAME (256) | ||
28 | |||
29 | /* *********************** IOAPI ***************** */ | ||
30 | |||
31 | #define ZLIB_FILEFUNC_SEEK_CUR (1) | ||
32 | #define ZLIB_FILEFUNC_SEEK_END (2) | ||
33 | #define ZLIB_FILEFUNC_SEEK_SET (0) | ||
34 | |||
35 | #define ZLIB_FILEFUNC_MODE_READ (1) | ||
36 | #define ZLIB_FILEFUNC_MODE_WRITE (2) | ||
37 | #define ZLIB_FILEFUNC_MODE_READWRITEFILTER (3) | ||
38 | #define ZLIB_FILEFUNC_MODE_EXISTING (4) | ||
39 | #define ZLIB_FILEFUNC_MODE_CREATE (8) | ||
40 | |||
41 | |||
42 | #define ZREAD(filefunc,filestream,buf,size) ((*((filefunc).zread_file))((filefunc).opaque,filestream,buf,size)) | ||
43 | #define ZWRITE(filefunc,filestream,buf,size) ((*((filefunc).zwrite_file))((filefunc).opaque,filestream,buf,size)) | ||
44 | #define ZTELL(filefunc,filestream) ((*((filefunc).ztell_file))((filefunc).opaque,filestream)) | ||
45 | #define ZSEEK(filefunc,filestream,pos,mode) ((*((filefunc).zseek_file))((filefunc).opaque,filestream,pos,mode)) | ||
46 | #define ZCLOSE(filefunc,filestream) ((*((filefunc).zclose_file))((filefunc).opaque,filestream)) | ||
47 | #define ZERROR(filefunc,filestream) ((*((filefunc).zerror_file))((filefunc).opaque,filestream)) | ||
48 | |||
49 | |||
50 | /* ******************* former crypt.h ********************* */ | ||
51 | |||
52 | /* unz_global_info structure contain global data about the ZIPfile | ||
53 | These data comes from the end of central dir */ | ||
54 | typedef struct unz_global_info_s | ||
55 | { | ||
56 | uLong number_entry; /* total number of entries in | ||
57 | the central dir on this disk */ | ||
58 | uLong size_comment; /* size of the global comment of the zipfile */ | ||
59 | } unz_global_info; | ||
60 | |||
61 | /* | ||
62 | Read extra field from the current file (opened by unzOpenCurrentFile) | ||
63 | This is the local-header version of the extra field (sometimes, there is | ||
64 | more info in the local-header version than in the central-header) | ||
65 | |||
66 | if buf==NULL, it return the size of the local extra field | ||
67 | |||
68 | if buf!=NULL, len is the size of the buffer, the extra header is copied in | ||
69 | buf. | ||
70 | the return value is the number of bytes copied in buf, or (if <0) | ||
71 | the error code | ||
72 | */ | ||
73 | |||
74 | #ifndef CASESENSITIVITYDEFAULT_NO | ||
75 | # if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) | ||
76 | # define CASESENSITIVITYDEFAULT_NO | ||
77 | # endif | ||
78 | #endif | ||
79 | |||
80 | |||
81 | #ifndef UNZ_BUFSIZE | ||
82 | #define UNZ_BUFSIZE (16384) | ||
83 | #endif | ||
84 | |||
85 | #ifndef UNZ_MAXFILENAMEINZIP | ||
86 | #define UNZ_MAXFILENAMEINZIP (256) | ||
87 | #endif | ||
88 | |||
89 | #ifndef ALLOC | ||
90 | # define ALLOC(size) (malloc(size)) | ||
91 | #endif | ||
92 | #ifndef TRYFREE | ||
93 | # define TRYFREE(p) {if (p) free(p);} | ||
94 | #endif | ||
95 | |||
96 | #define SIZECENTRALDIRITEM (0x2e) | ||
97 | #define SIZEZIPLOCALHEADER (0x1e) | ||
98 | |||
99 | |||
100 | const char unz_copyright[] = | ||
101 | " unzip 1.00 Copyright 1998-2003 Gilles Vollant - http://www.winimage.com/zLibDll"; | ||
102 | |||
103 | /* EXTRACTOR_unzip_file_info_interntal contain internal info about a file in zipfile*/ | ||
104 | typedef struct unz_file_info_internal_s | ||
105 | { | ||
106 | uLong offset_curfile;/* relative offset of local header 4 bytes */ | ||
107 | } unz_file_info_internal; | ||
108 | |||
109 | |||
110 | /* file_in_zip_read_info_s contain internal information about a file in zipfile, | ||
111 | when reading and decompress it */ | ||
112 | typedef struct | ||
113 | { | ||
114 | char *read_buffer; /* internal buffer for compressed data */ | ||
115 | z_stream stream; /* zLib stream structure for inflate */ | ||
116 | |||
117 | uLong pos_in_zipfile; /* position in byte on the zipfile, for fseek*/ | ||
118 | uLong stream_initialised; /* flag set if stream structure is initialised*/ | ||
119 | |||
120 | uLong offset_local_extrafield;/* offset of the local extra field */ | ||
121 | uInt size_local_extrafield;/* size of the local extra field */ | ||
122 | uLong pos_local_extrafield; /* position in the local extra field in read*/ | ||
123 | |||
124 | uLong crc32; /* crc32 of all data uncompressed */ | ||
125 | uLong crc32_wait; /* crc32 we must obtain after decompress all */ | ||
126 | uLong rest_read_compressed; /* number of byte to be decompressed */ | ||
127 | uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/ | ||
128 | EXTRACTOR_unzip_filefunc_def z_filefunc; | ||
129 | voidpf filestream; /* io structore of the zipfile */ | ||
130 | uLong compression_method; /* compression method (0==store) */ | ||
131 | uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ | ||
132 | int raw; | ||
133 | } file_in_zip_read_info_s; | ||
134 | |||
135 | |||
136 | /* unz_s contain internal information about the zipfile | ||
137 | */ | ||
138 | typedef struct | ||
139 | { | ||
140 | EXTRACTOR_unzip_filefunc_def z_filefunc; | ||
141 | voidpf filestream; /* io structore of the zipfile */ | ||
142 | unz_global_info gi; /* public global information */ | ||
143 | uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ | ||
144 | uLong num_file; /* number of the current file in the zipfile*/ | ||
145 | uLong pos_in_central_dir; /* pos of the current file in the central dir*/ | ||
146 | uLong current_file_ok; /* flag about the usability of the current file*/ | ||
147 | uLong central_pos; /* position of the beginning of the central dir*/ | ||
148 | |||
149 | uLong size_central_dir; /* size of the central directory */ | ||
150 | uLong offset_central_dir; /* offset of start of central directory with | ||
151 | respect to the starting disk number */ | ||
152 | |||
153 | EXTRACTOR_unzip_file_info cur_file_info; /* public info about the current file in zip*/ | ||
154 | unz_file_info_internal cur_file_info_internal; /* private info about it*/ | ||
155 | file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current | ||
156 | file if we are decompressing it */ | ||
157 | int encrypted; | ||
158 | } unz_s; | ||
159 | |||
160 | |||
161 | |||
162 | /* =========================================================================== | ||
163 | Read a byte from a gz_stream; update next_in and avail_in. Return EOF | ||
164 | for end of file. | ||
165 | IN assertion: the stream s has been sucessfully opened for reading. | ||
166 | */ | ||
167 | |||
168 | |||
169 | static int unzlocal_getByte OF(( | ||
170 | const EXTRACTOR_unzip_filefunc_def* pzlib_filefunc_def, | ||
171 | voidpf filestream, | ||
172 | int *pi)); | ||
173 | |||
174 | static int unzlocal_getByte(pzlib_filefunc_def,filestream,pi) | ||
175 | const EXTRACTOR_unzip_filefunc_def* pzlib_filefunc_def; | ||
176 | voidpf filestream; | ||
177 | int *pi; | ||
178 | { | ||
179 | unsigned char c; | ||
180 | int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1); | ||
181 | if (err==1) | ||
182 | { | ||
183 | *pi = (int)c; | ||
184 | return EXTRACTOR_UNZIP_OK; | ||
185 | } | ||
186 | else | ||
187 | { | ||
188 | if (ZERROR(*pzlib_filefunc_def,filestream)) | ||
189 | return EXTRACTOR_UNZIP_ERRNO; | ||
190 | else | ||
191 | return EXTRACTOR_UNZIP_EOF; | ||
192 | } | ||
193 | } | ||
194 | |||
195 | |||
196 | /* =========================================================================== | ||
197 | Reads a long in LSB order from the given gz_stream. Sets | ||
198 | */ | ||
199 | static int unzlocal_getShort OF(( | ||
200 | const EXTRACTOR_unzip_filefunc_def* pzlib_filefunc_def, | ||
201 | voidpf filestream, | ||
202 | uLong *pX)); | ||
203 | |||
204 | static int unzlocal_getShort (pzlib_filefunc_def,filestream,pX) | ||
205 | const EXTRACTOR_unzip_filefunc_def* pzlib_filefunc_def; | ||
206 | voidpf filestream; | ||
207 | uLong *pX; | ||
208 | { | ||
209 | uLong x ; | ||
210 | int i; | ||
211 | int err; | ||
212 | |||
213 | err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
214 | x = (uLong)i; | ||
215 | |||
216 | if (err==EXTRACTOR_UNZIP_OK) | ||
217 | err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
218 | x += ((uLong)i)<<8; | ||
219 | |||
220 | if (err==EXTRACTOR_UNZIP_OK) | ||
221 | *pX = x; | ||
222 | else | ||
223 | *pX = 0; | ||
224 | return err; | ||
225 | } | ||
226 | |||
227 | static int unzlocal_getLong OF(( | ||
228 | const EXTRACTOR_unzip_filefunc_def* pzlib_filefunc_def, | ||
229 | voidpf filestream, | ||
230 | uLong *pX)); | ||
231 | |||
232 | static int unzlocal_getLong (pzlib_filefunc_def,filestream,pX) | ||
233 | const EXTRACTOR_unzip_filefunc_def* pzlib_filefunc_def; | ||
234 | voidpf filestream; | ||
235 | uLong *pX; | ||
236 | { | ||
237 | uLong x ; | ||
238 | int i; | ||
239 | int err; | ||
240 | |||
241 | err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
242 | x = (uLong)i; | ||
243 | |||
244 | if (err==EXTRACTOR_UNZIP_OK) | ||
245 | err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
246 | x += ((uLong)i)<<8; | ||
247 | |||
248 | if (err==EXTRACTOR_UNZIP_OK) | ||
249 | err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
250 | x += ((uLong)i)<<16; | ||
251 | |||
252 | if (err==EXTRACTOR_UNZIP_OK) | ||
253 | err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
254 | x += ((uLong)i)<<24; | ||
255 | |||
256 | if (err==EXTRACTOR_UNZIP_OK) | ||
257 | *pX = x; | ||
258 | else | ||
259 | *pX = 0; | ||
260 | return err; | ||
261 | } | ||
262 | |||
263 | |||
264 | /* My own strcmpi / strcasecmp */ | ||
265 | static int strcmpcasenosensitive_internal (fileName1,fileName2) | ||
266 | const char* fileName1; | ||
267 | const char* fileName2; | ||
268 | { | ||
269 | for (;;) | ||
270 | { | ||
271 | char c1=*(fileName1++); | ||
272 | char c2=*(fileName2++); | ||
273 | if ((c1>='a') && (c1<='z')) | ||
274 | c1 -= 0x20; | ||
275 | if ((c2>='a') && (c2<='z')) | ||
276 | c2 -= 0x20; | ||
277 | if (c1=='\0') | ||
278 | return ((c2=='\0') ? 0 : -1); | ||
279 | if (c2=='\0') | ||
280 | return 1; | ||
281 | if (c1<c2) | ||
282 | return -1; | ||
283 | if (c1>c2) | ||
284 | return 1; | ||
285 | } | ||
286 | } | ||
287 | |||
288 | |||
289 | #ifdef CASESENSITIVITYDEFAULT_NO | ||
290 | #define CASESENSITIVITYDEFAULTVALUE 2 | ||
291 | #else | ||
292 | #define CASESENSITIVITYDEFAULTVALUE 1 | ||
293 | #endif | ||
294 | |||
295 | #ifndef STRCMPCASENOSENTIVEFUNCTION | ||
296 | #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal | ||
297 | #endif | ||
298 | |||
299 | /* | ||
300 | Compare two filename (fileName1,fileName2). | ||
301 | If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) | ||
302 | If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi | ||
303 | or strcasecmp) | ||
304 | If iCaseSenisivity = 0, case sensitivity is defaut of your operating system | ||
305 | (like 1 on Unix, 2 on Windows) | ||
306 | |||
307 | */ | ||
308 | int EXTRACTOR_common_unzip_string_file_name_compare (fileName1,fileName2,iCaseSensitivity) | ||
309 | const char* fileName1; | ||
310 | const char* fileName2; | ||
311 | int iCaseSensitivity; | ||
312 | { | ||
313 | if (iCaseSensitivity==0) | ||
314 | iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE; | ||
315 | |||
316 | if (iCaseSensitivity==1) | ||
317 | return strcmp(fileName1,fileName2); | ||
318 | |||
319 | return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2); | ||
320 | } | ||
321 | |||
322 | #ifndef BUFREADCOMMENT | ||
323 | #define BUFREADCOMMENT (0x400) | ||
324 | #endif | ||
325 | |||
326 | /* | ||
327 | Locate the Central directory of a zipfile (at the end, just before | ||
328 | the global comment) | ||
329 | */ | ||
330 | static uLong unzlocal_SearchCentralDir OF(( | ||
331 | const EXTRACTOR_unzip_filefunc_def* pzlib_filefunc_def, | ||
332 | voidpf filestream)); | ||
333 | |||
334 | static uLong unzlocal_SearchCentralDir(pzlib_filefunc_def,filestream) | ||
335 | const EXTRACTOR_unzip_filefunc_def* pzlib_filefunc_def; | ||
336 | voidpf filestream; | ||
337 | { | ||
338 | unsigned char* buf; | ||
339 | uLong uSizeFile; | ||
340 | uLong uBackRead; | ||
341 | uLong uMaxBack=0xffff; /* maximum size of global comment */ | ||
342 | uLong uPosFound=0; | ||
343 | |||
344 | if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) | ||
345 | return 0; | ||
346 | |||
347 | |||
348 | uSizeFile = ZTELL(*pzlib_filefunc_def,filestream); | ||
349 | |||
350 | if (uMaxBack>uSizeFile) | ||
351 | uMaxBack = uSizeFile; | ||
352 | |||
353 | buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); | ||
354 | if (buf==NULL) | ||
355 | return 0; | ||
356 | |||
357 | uBackRead = 4; | ||
358 | while (uBackRead<uMaxBack) | ||
359 | { | ||
360 | uLong uReadSize,uReadPos ; | ||
361 | int i; | ||
362 | if (uBackRead+BUFREADCOMMENT>uMaxBack) | ||
363 | uBackRead = uMaxBack; | ||
364 | else | ||
365 | uBackRead+=BUFREADCOMMENT; | ||
366 | uReadPos = uSizeFile-uBackRead ; | ||
367 | |||
368 | uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? | ||
369 | (BUFREADCOMMENT+4) : (uSizeFile-uReadPos); | ||
370 | if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
371 | break; | ||
372 | |||
373 | if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) | ||
374 | break; | ||
375 | |||
376 | for (i=(int)uReadSize-3; (i--)>0;) | ||
377 | if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && | ||
378 | ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) | ||
379 | { | ||
380 | uPosFound = uReadPos+i; | ||
381 | break; | ||
382 | } | ||
383 | |||
384 | if (uPosFound!=0) | ||
385 | break; | ||
386 | } | ||
387 | TRYFREE(buf); | ||
388 | return uPosFound; | ||
389 | } | ||
390 | |||
391 | /* | ||
392 | Translate date/time from Dos format to EXTRACTOR_unzip_tm_unz (readable more easilty) | ||
393 | */ | ||
394 | static void unzlocal_DosDateToTmuDate (ulDosDate, ptm) | ||
395 | uLong ulDosDate; | ||
396 | EXTRACTOR_unzip_tm_unz* ptm; | ||
397 | { | ||
398 | uLong uDate; | ||
399 | uDate = (uLong)(ulDosDate>>16); | ||
400 | ptm->tm_mday = (uInt)(uDate&0x1f) ; | ||
401 | ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ; | ||
402 | ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ; | ||
403 | |||
404 | ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800); | ||
405 | ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ; | ||
406 | ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ; | ||
407 | } | ||
408 | |||
409 | |||
410 | |||
411 | static int unzlocal_GetCurrentFileInfoInternal (file, | ||
412 | pfile_info, | ||
413 | pfile_info_internal, | ||
414 | szFileName, fileNameBufferSize, | ||
415 | extraField, extraFieldBufferSize, | ||
416 | szComment, commentBufferSize) | ||
417 | EXTRACTOR_unzip_file file; | ||
418 | EXTRACTOR_unzip_file_info *pfile_info; | ||
419 | unz_file_info_internal *pfile_info_internal; | ||
420 | char *szFileName; | ||
421 | uLong fileNameBufferSize; | ||
422 | void *extraField; | ||
423 | uLong extraFieldBufferSize; | ||
424 | char *szComment; | ||
425 | uLong commentBufferSize; | ||
426 | { | ||
427 | unz_s* s; | ||
428 | EXTRACTOR_unzip_file_info file_info; | ||
429 | unz_file_info_internal file_info_internal; | ||
430 | int err=EXTRACTOR_UNZIP_OK; | ||
431 | uLong uMagic; | ||
432 | long lSeek=0; | ||
433 | |||
434 | if (file==NULL) | ||
435 | return EXTRACTOR_UNZIP_PARAMERROR; | ||
436 | s=(unz_s*)file; | ||
437 | if (ZSEEK(s->z_filefunc, s->filestream, | ||
438 | s->pos_in_central_dir+s->byte_before_the_zipfile, | ||
439 | ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
440 | err=EXTRACTOR_UNZIP_ERRNO; | ||
441 | |||
442 | |||
443 | /* we check the magic */ | ||
444 | if (err==EXTRACTOR_UNZIP_OK) { | ||
445 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != EXTRACTOR_UNZIP_OK) | ||
446 | err=EXTRACTOR_UNZIP_ERRNO; | ||
447 | else if (uMagic!=0x02014b50) | ||
448 | err=EXTRACTOR_UNZIP_BADZIPFILE; | ||
449 | } | ||
450 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.version) != EXTRACTOR_UNZIP_OK) | ||
451 | err=EXTRACTOR_UNZIP_ERRNO; | ||
452 | |||
453 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.version_needed) != EXTRACTOR_UNZIP_OK) | ||
454 | err=EXTRACTOR_UNZIP_ERRNO; | ||
455 | |||
456 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.flag) != EXTRACTOR_UNZIP_OK) | ||
457 | err=EXTRACTOR_UNZIP_ERRNO; | ||
458 | |||
459 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.compression_method) != EXTRACTOR_UNZIP_OK) | ||
460 | err=EXTRACTOR_UNZIP_ERRNO; | ||
461 | |||
462 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.dosDate) != EXTRACTOR_UNZIP_OK) | ||
463 | err=EXTRACTOR_UNZIP_ERRNO; | ||
464 | |||
465 | unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date); | ||
466 | |||
467 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.crc) != EXTRACTOR_UNZIP_OK) | ||
468 | err=EXTRACTOR_UNZIP_ERRNO; | ||
469 | |||
470 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.compressed_size) != EXTRACTOR_UNZIP_OK) | ||
471 | err=EXTRACTOR_UNZIP_ERRNO; | ||
472 | |||
473 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != EXTRACTOR_UNZIP_OK) | ||
474 | err=EXTRACTOR_UNZIP_ERRNO; | ||
475 | |||
476 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_filename) != EXTRACTOR_UNZIP_OK) | ||
477 | err=EXTRACTOR_UNZIP_ERRNO; | ||
478 | |||
479 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_extra) != EXTRACTOR_UNZIP_OK) | ||
480 | err=EXTRACTOR_UNZIP_ERRNO; | ||
481 | |||
482 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_comment) != EXTRACTOR_UNZIP_OK) | ||
483 | err=EXTRACTOR_UNZIP_ERRNO; | ||
484 | |||
485 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != EXTRACTOR_UNZIP_OK) | ||
486 | err=EXTRACTOR_UNZIP_ERRNO; | ||
487 | |||
488 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.internal_fa) != EXTRACTOR_UNZIP_OK) | ||
489 | err=EXTRACTOR_UNZIP_ERRNO; | ||
490 | |||
491 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.external_fa) != EXTRACTOR_UNZIP_OK) | ||
492 | err=EXTRACTOR_UNZIP_ERRNO; | ||
493 | |||
494 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != EXTRACTOR_UNZIP_OK) | ||
495 | err=EXTRACTOR_UNZIP_ERRNO; | ||
496 | |||
497 | lSeek+=file_info.size_filename; | ||
498 | if ((err==EXTRACTOR_UNZIP_OK) && (szFileName!=NULL)) | ||
499 | { | ||
500 | uLong uSizeRead ; | ||
501 | if (file_info.size_filename<fileNameBufferSize) | ||
502 | { | ||
503 | *(szFileName+file_info.size_filename)='\0'; | ||
504 | uSizeRead = file_info.size_filename; | ||
505 | } | ||
506 | else | ||
507 | uSizeRead = fileNameBufferSize; | ||
508 | |||
509 | if ((file_info.size_filename>0) && (fileNameBufferSize>0)) | ||
510 | if (ZREAD(s->z_filefunc, s->filestream,szFileName,uSizeRead)!=uSizeRead) | ||
511 | err=EXTRACTOR_UNZIP_ERRNO; | ||
512 | lSeek -= uSizeRead; | ||
513 | } | ||
514 | |||
515 | |||
516 | if ((err==EXTRACTOR_UNZIP_OK) && (extraField!=NULL)) | ||
517 | { | ||
518 | uLong uSizeRead ; | ||
519 | if (file_info.size_file_extra<extraFieldBufferSize) | ||
520 | uSizeRead = file_info.size_file_extra; | ||
521 | else | ||
522 | uSizeRead = extraFieldBufferSize; | ||
523 | |||
524 | if (lSeek!=0) { | ||
525 | if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0) | ||
526 | lSeek=0; | ||
527 | else | ||
528 | err=EXTRACTOR_UNZIP_ERRNO; | ||
529 | } | ||
530 | if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0)) | ||
531 | if (ZREAD(s->z_filefunc, s->filestream,extraField,uSizeRead)!=uSizeRead) | ||
532 | err=EXTRACTOR_UNZIP_ERRNO; | ||
533 | lSeek += file_info.size_file_extra - uSizeRead; | ||
534 | } | ||
535 | else | ||
536 | lSeek+=file_info.size_file_extra; | ||
537 | |||
538 | |||
539 | if ((err==EXTRACTOR_UNZIP_OK) && (szComment!=NULL)) | ||
540 | { | ||
541 | uLong uSizeRead ; | ||
542 | if (file_info.size_file_comment<commentBufferSize) | ||
543 | { | ||
544 | *(szComment+file_info.size_file_comment)='\0'; | ||
545 | uSizeRead = file_info.size_file_comment; | ||
546 | } | ||
547 | else | ||
548 | uSizeRead = commentBufferSize; | ||
549 | |||
550 | if (lSeek!=0) { | ||
551 | if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0) | ||
552 | lSeek=0; | ||
553 | else | ||
554 | err=EXTRACTOR_UNZIP_ERRNO; | ||
555 | } | ||
556 | if ((file_info.size_file_comment>0) && (commentBufferSize>0)) | ||
557 | if (ZREAD(s->z_filefunc, s->filestream,szComment,uSizeRead)!=uSizeRead) | ||
558 | err=EXTRACTOR_UNZIP_ERRNO; | ||
559 | lSeek+=file_info.size_file_comment - uSizeRead; | ||
560 | } | ||
561 | else | ||
562 | lSeek+=file_info.size_file_comment; | ||
563 | |||
564 | if ((err==EXTRACTOR_UNZIP_OK) && (pfile_info!=NULL)) | ||
565 | *pfile_info=file_info; | ||
566 | |||
567 | if ((err==EXTRACTOR_UNZIP_OK) && (pfile_info_internal!=NULL)) | ||
568 | *pfile_info_internal=file_info_internal; | ||
569 | |||
570 | return err; | ||
571 | } | ||
572 | |||
573 | /* | ||
574 | Set the current file of the zipfile to the first file. | ||
575 | return UNZ_OK if there is no problem | ||
576 | */ | ||
577 | int EXTRACTOR_common_unzip_go_to_first_file (file) | ||
578 | EXTRACTOR_unzip_file file; | ||
579 | { | ||
580 | int err=EXTRACTOR_UNZIP_OK; | ||
581 | unz_s* s; | ||
582 | if (file==NULL) | ||
583 | return EXTRACTOR_UNZIP_PARAMERROR; | ||
584 | s=(unz_s*)file; | ||
585 | s->pos_in_central_dir=s->offset_central_dir; | ||
586 | s->num_file=0; | ||
587 | err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, | ||
588 | &s->cur_file_info_internal, | ||
589 | NULL,0,NULL,0,NULL,0); | ||
590 | s->current_file_ok = (err == EXTRACTOR_UNZIP_OK); | ||
591 | return err; | ||
592 | } | ||
593 | |||
594 | |||
595 | /* | ||
596 | Open a Zip file. path contain the full pathname (by example, | ||
597 | on a Windows NT computer "c:\\test\\zlib114.zip" or on an Unix computer | ||
598 | "zlib/zlib114.zip". | ||
599 | If the zipfile cannot be opened (file doesn't exist or in not valid), the | ||
600 | return value is NULL. | ||
601 | Else, the return value is a EXTRACTOR_unzip_file Handle, usable with other function | ||
602 | of this unzip package. | ||
603 | */ | ||
604 | EXTRACTOR_unzip_file EXTRACTOR_common_unzip_open2 (path, pzlib_filefunc_def) | ||
605 | const char *path; | ||
606 | EXTRACTOR_unzip_filefunc_def* pzlib_filefunc_def; | ||
607 | { | ||
608 | unz_s us; | ||
609 | unz_s *s; | ||
610 | uLong central_pos,uL; | ||
611 | |||
612 | uLong number_disk; /* number of the current dist, used for | ||
613 | spaning ZIP, unsupported, always 0*/ | ||
614 | uLong number_disk_with_CD; /* number the the disk with central dir, used | ||
615 | for spaning ZIP, unsupported, always 0*/ | ||
616 | uLong number_entry_CD; /* total number of entries in | ||
617 | the central dir | ||
618 | (same than number_entry on nospan) */ | ||
619 | |||
620 | int err=EXTRACTOR_UNZIP_OK; | ||
621 | |||
622 | if (unz_copyright[0]!=' ') | ||
623 | return NULL; | ||
624 | |||
625 | us.z_filefunc = *pzlib_filefunc_def; | ||
626 | |||
627 | us.filestream= (*(us.z_filefunc.zopen_file))(us.z_filefunc.opaque, | ||
628 | path, | ||
629 | ZLIB_FILEFUNC_MODE_READ | | ||
630 | ZLIB_FILEFUNC_MODE_EXISTING); | ||
631 | if (us.filestream==NULL) | ||
632 | return NULL; | ||
633 | |||
634 | central_pos = unzlocal_SearchCentralDir(&us.z_filefunc,us.filestream); | ||
635 | if (central_pos==0) | ||
636 | err=EXTRACTOR_UNZIP_ERRNO; | ||
637 | |||
638 | if (ZSEEK(us.z_filefunc, us.filestream, | ||
639 | central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
640 | err=EXTRACTOR_UNZIP_ERRNO; | ||
641 | |||
642 | /* the signature, already checked */ | ||
643 | if (unzlocal_getLong(&us.z_filefunc, us.filestream,&uL)!=EXTRACTOR_UNZIP_OK) | ||
644 | err=EXTRACTOR_UNZIP_ERRNO; | ||
645 | |||
646 | /* number of this disk */ | ||
647 | if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_disk)!=EXTRACTOR_UNZIP_OK) | ||
648 | err=EXTRACTOR_UNZIP_ERRNO; | ||
649 | |||
650 | /* number of the disk with the start of the central directory */ | ||
651 | if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=EXTRACTOR_UNZIP_OK) | ||
652 | err=EXTRACTOR_UNZIP_ERRNO; | ||
653 | |||
654 | /* total number of entries in the central dir on this disk */ | ||
655 | if (unzlocal_getShort(&us.z_filefunc, us.filestream,&us.gi.number_entry)!=EXTRACTOR_UNZIP_OK) | ||
656 | err=EXTRACTOR_UNZIP_ERRNO; | ||
657 | |||
658 | /* total number of entries in the central dir */ | ||
659 | if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_entry_CD)!=EXTRACTOR_UNZIP_OK) | ||
660 | err=EXTRACTOR_UNZIP_ERRNO; | ||
661 | |||
662 | if ((number_entry_CD!=us.gi.number_entry) || | ||
663 | (number_disk_with_CD!=0) || | ||
664 | (number_disk!=0)) | ||
665 | err=EXTRACTOR_UNZIP_BADZIPFILE; | ||
666 | |||
667 | /* size of the central directory */ | ||
668 | if (unzlocal_getLong(&us.z_filefunc, us.filestream,&us.size_central_dir)!=EXTRACTOR_UNZIP_OK) | ||
669 | err=EXTRACTOR_UNZIP_ERRNO; | ||
670 | |||
671 | /* offset of start of central directory with respect to the | ||
672 | starting disk number */ | ||
673 | if (unzlocal_getLong(&us.z_filefunc, us.filestream,&us.offset_central_dir)!=EXTRACTOR_UNZIP_OK) | ||
674 | err=EXTRACTOR_UNZIP_ERRNO; | ||
675 | |||
676 | /* zipfile comment length */ | ||
677 | if (unzlocal_getShort(&us.z_filefunc, us.filestream,&us.gi.size_comment)!=EXTRACTOR_UNZIP_OK) | ||
678 | err=EXTRACTOR_UNZIP_ERRNO; | ||
679 | |||
680 | if ((central_pos<us.offset_central_dir+us.size_central_dir) && | ||
681 | (err==EXTRACTOR_UNZIP_OK)) | ||
682 | err=EXTRACTOR_UNZIP_BADZIPFILE; | ||
683 | |||
684 | if (err!=EXTRACTOR_UNZIP_OK) | ||
685 | { | ||
686 | ZCLOSE(us.z_filefunc, us.filestream); | ||
687 | return NULL; | ||
688 | } | ||
689 | |||
690 | us.byte_before_the_zipfile = central_pos - | ||
691 | (us.offset_central_dir+us.size_central_dir); | ||
692 | us.central_pos = central_pos; | ||
693 | us.pfile_in_zip_read = NULL; | ||
694 | us.encrypted = 0; | ||
695 | |||
696 | |||
697 | s=(unz_s*)ALLOC(sizeof(unz_s)); | ||
698 | *s=us; | ||
699 | EXTRACTOR_common_unzip_go_to_first_file((EXTRACTOR_unzip_file)s); | ||
700 | return (EXTRACTOR_unzip_file)s; | ||
701 | } | ||
702 | |||
703 | /* | ||
704 | Close the file in zip opened with unzipOpenCurrentFile | ||
705 | Return EXTRACTOR_UNZIP_CRCERROR if all the file was read but the CRC is not good | ||
706 | */ | ||
707 | int EXTRACTOR_common_unzip_close_current_file (file) | ||
708 | EXTRACTOR_unzip_file file; | ||
709 | { | ||
710 | int err=EXTRACTOR_UNZIP_OK; | ||
711 | |||
712 | unz_s* s; | ||
713 | file_in_zip_read_info_s* pfile_in_zip_read_info; | ||
714 | if (file==NULL) | ||
715 | return EXTRACTOR_UNZIP_PARAMERROR; | ||
716 | s=(unz_s*)file; | ||
717 | pfile_in_zip_read_info=s->pfile_in_zip_read; | ||
718 | |||
719 | if (pfile_in_zip_read_info==NULL) | ||
720 | return EXTRACTOR_UNZIP_PARAMERROR; | ||
721 | |||
722 | |||
723 | if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) && | ||
724 | (!pfile_in_zip_read_info->raw)) | ||
725 | { | ||
726 | if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait) | ||
727 | err=EXTRACTOR_UNZIP_CRCERROR; | ||
728 | } | ||
729 | |||
730 | |||
731 | TRYFREE(pfile_in_zip_read_info->read_buffer); | ||
732 | pfile_in_zip_read_info->read_buffer = NULL; | ||
733 | if (pfile_in_zip_read_info->stream_initialised) | ||
734 | inflateEnd(&pfile_in_zip_read_info->stream); | ||
735 | |||
736 | pfile_in_zip_read_info->stream_initialised = 0; | ||
737 | TRYFREE(pfile_in_zip_read_info); | ||
738 | |||
739 | s->pfile_in_zip_read=NULL; | ||
740 | |||
741 | return err; | ||
742 | } | ||
743 | |||
744 | /* | ||
745 | Close a ZipFile opened with unzipOpen. | ||
746 | If there is files inside the .Zip opened with unzipOpenCurrentFile (see later), | ||
747 | these files MUST be closed with unzipCloseCurrentFile before call unzipClose. | ||
748 | return EXTRACTOR_UNZIP_OK if there is no problem. */ | ||
749 | int EXTRACTOR_common_unzip_close (file) | ||
750 | EXTRACTOR_unzip_file file; | ||
751 | { | ||
752 | unz_s* s; | ||
753 | if (file==NULL) | ||
754 | return EXTRACTOR_UNZIP_PARAMERROR; | ||
755 | s=(unz_s*)file; | ||
756 | |||
757 | if (s->pfile_in_zip_read!=NULL) | ||
758 | EXTRACTOR_common_unzip_close_current_file(file); | ||
759 | |||
760 | ZCLOSE(s->z_filefunc, s->filestream); | ||
761 | TRYFREE(s); | ||
762 | return EXTRACTOR_UNZIP_OK; | ||
763 | } | ||
764 | |||
765 | |||
766 | /* | ||
767 | Write info about the ZipFile in the *pglobal_info structure. | ||
768 | No preparation of the structure is needed | ||
769 | return EXTRACTOR_UNZIP_OK if there is no problem. | ||
770 | */ | ||
771 | int EXTRACTOR_common_unzip_get_current_file_info (file, | ||
772 | pfile_info, | ||
773 | szFileName, fileNameBufferSize, | ||
774 | extraField, extraFieldBufferSize, | ||
775 | szComment, commentBufferSize) | ||
776 | EXTRACTOR_unzip_file file; | ||
777 | EXTRACTOR_unzip_file_info *pfile_info; | ||
778 | char *szFileName; | ||
779 | uLong fileNameBufferSize; | ||
780 | void *extraField; | ||
781 | uLong extraFieldBufferSize; | ||
782 | char *szComment; | ||
783 | uLong commentBufferSize; | ||
784 | { | ||
785 | return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL, | ||
786 | szFileName,fileNameBufferSize, | ||
787 | extraField,extraFieldBufferSize, | ||
788 | szComment,commentBufferSize); | ||
789 | } | ||
790 | |||
791 | /* | ||
792 | Set the current file of the zipfile to the next file. | ||
793 | return EXTRACTOR_UNZIP_OK if there is no problem | ||
794 | return EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE if the actual file was the latest. | ||
795 | */ | ||
796 | int EXTRACTOR_common_unzip_go_to_next_file (file) | ||
797 | EXTRACTOR_unzip_file file; | ||
798 | { | ||
799 | unz_s* s; | ||
800 | int err; | ||
801 | |||
802 | if (file==NULL) | ||
803 | return EXTRACTOR_UNZIP_PARAMERROR; | ||
804 | s=(unz_s*)file; | ||
805 | if (!s->current_file_ok) | ||
806 | return EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE; | ||
807 | if (s->num_file+1==s->gi.number_entry) | ||
808 | return EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE; | ||
809 | |||
810 | s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename + | ||
811 | s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ; | ||
812 | s->num_file++; | ||
813 | err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, | ||
814 | &s->cur_file_info_internal, | ||
815 | NULL,0,NULL,0,NULL,0); | ||
816 | s->current_file_ok = (err == EXTRACTOR_UNZIP_OK); | ||
817 | return err; | ||
818 | } | ||
819 | |||
820 | |||
821 | |||
822 | |||
823 | /* | ||
824 | Try locate the file szFileName in the zipfile. | ||
825 | For the iCaseSensitivity signification, see unzipStringFileNameCompare | ||
826 | |||
827 | return value : | ||
828 | EXTRACTOR_UNZIP_OK if the file is found. It becomes the current file. | ||
829 | EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE if the file is not found | ||
830 | */ | ||
831 | int EXTRACTOR_common_unzip_local_file (file, szFileName, iCaseSensitivity) | ||
832 | EXTRACTOR_unzip_file file; | ||
833 | const char *szFileName; | ||
834 | int iCaseSensitivity; | ||
835 | { | ||
836 | unz_s* s; | ||
837 | int err; | ||
838 | |||
839 | /* We remember the 'current' position in the file so that we can jump | ||
840 | * back there if we fail. | ||
841 | */ | ||
842 | EXTRACTOR_unzip_file_info cur_file_infoSaved; | ||
843 | unz_file_info_internal cur_file_info_internalSaved; | ||
844 | uLong num_fileSaved; | ||
845 | uLong pos_in_central_dirSaved; | ||
846 | |||
847 | |||
848 | if (file==NULL) | ||
849 | return EXTRACTOR_UNZIP_PARAMERROR; | ||
850 | |||
851 | if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP) | ||
852 | return EXTRACTOR_UNZIP_PARAMERROR; | ||
853 | |||
854 | s=(unz_s*)file; | ||
855 | if (!s->current_file_ok) | ||
856 | return EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE; | ||
857 | |||
858 | /* Save the current state */ | ||
859 | num_fileSaved = s->num_file; | ||
860 | pos_in_central_dirSaved = s->pos_in_central_dir; | ||
861 | cur_file_infoSaved = s->cur_file_info; | ||
862 | cur_file_info_internalSaved = s->cur_file_info_internal; | ||
863 | |||
864 | err = EXTRACTOR_common_unzip_go_to_first_file(file); | ||
865 | |||
866 | while (err == EXTRACTOR_UNZIP_OK) | ||
867 | { | ||
868 | char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1]; | ||
869 | err = EXTRACTOR_common_unzip_get_current_file_info(file,NULL, | ||
870 | szCurrentFileName,sizeof(szCurrentFileName)-1, | ||
871 | NULL,0,NULL,0); | ||
872 | if (err == EXTRACTOR_UNZIP_OK) | ||
873 | { | ||
874 | if (EXTRACTOR_common_unzip_string_file_name_compare(szCurrentFileName, | ||
875 | szFileName,iCaseSensitivity)==0) | ||
876 | return EXTRACTOR_UNZIP_OK; | ||
877 | err = EXTRACTOR_common_unzip_go_to_next_file(file); | ||
878 | } | ||
879 | } | ||
880 | |||
881 | /* We failed, so restore the state of the 'current file' to where we | ||
882 | * were. | ||
883 | */ | ||
884 | s->num_file = num_fileSaved ; | ||
885 | s->pos_in_central_dir = pos_in_central_dirSaved ; | ||
886 | s->cur_file_info = cur_file_infoSaved; | ||
887 | s->cur_file_info_internal = cur_file_info_internalSaved; | ||
888 | return err; | ||
889 | } | ||
890 | |||
891 | |||
892 | /* | ||
893 | Read bytes from the current file. | ||
894 | buf contain buffer where data must be copied | ||
895 | len the size of buf. | ||
896 | |||
897 | return the number of byte copied if somes bytes are copied | ||
898 | return 0 if the end of file was reached | ||
899 | return <0 with error code if there is an error | ||
900 | (EXTRACTOR_UNZIP_ERRNO for IO error, or zLib error for uncompress error) | ||
901 | */ | ||
902 | int EXTRACTOR_common_unzip_read_current_file (file, buf, len) | ||
903 | EXTRACTOR_unzip_file file; | ||
904 | voidp buf; | ||
905 | unsigned len; | ||
906 | { | ||
907 | int err=EXTRACTOR_UNZIP_OK; | ||
908 | uInt iRead = 0; | ||
909 | unz_s* s; | ||
910 | file_in_zip_read_info_s* pfile_in_zip_read_info; | ||
911 | if (file==NULL) | ||
912 | return EXTRACTOR_UNZIP_PARAMERROR; | ||
913 | s=(unz_s*)file; | ||
914 | pfile_in_zip_read_info=s->pfile_in_zip_read; | ||
915 | |||
916 | if (pfile_in_zip_read_info==NULL) | ||
917 | return EXTRACTOR_UNZIP_PARAMERROR; | ||
918 | |||
919 | |||
920 | if ((pfile_in_zip_read_info->read_buffer == NULL)) | ||
921 | return EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE; | ||
922 | if (len==0) | ||
923 | return 0; | ||
924 | |||
925 | pfile_in_zip_read_info->stream.next_out = (Bytef*)buf; | ||
926 | |||
927 | pfile_in_zip_read_info->stream.avail_out = (uInt)len; | ||
928 | |||
929 | if (len>pfile_in_zip_read_info->rest_read_uncompressed) | ||
930 | pfile_in_zip_read_info->stream.avail_out = | ||
931 | (uInt)pfile_in_zip_read_info->rest_read_uncompressed; | ||
932 | |||
933 | while (pfile_in_zip_read_info->stream.avail_out>0) | ||
934 | { | ||
935 | if ((pfile_in_zip_read_info->stream.avail_in==0) && | ||
936 | (pfile_in_zip_read_info->rest_read_compressed>0)) | ||
937 | { | ||
938 | uInt uReadThis = UNZ_BUFSIZE; | ||
939 | if (pfile_in_zip_read_info->rest_read_compressed<uReadThis) | ||
940 | uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed; | ||
941 | if (uReadThis == 0) | ||
942 | return EXTRACTOR_UNZIP_EOF; | ||
943 | if (ZSEEK(pfile_in_zip_read_info->z_filefunc, | ||
944 | pfile_in_zip_read_info->filestream, | ||
945 | pfile_in_zip_read_info->pos_in_zipfile + | ||
946 | pfile_in_zip_read_info->byte_before_the_zipfile, | ||
947 | ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
948 | return EXTRACTOR_UNZIP_ERRNO; | ||
949 | if (ZREAD(pfile_in_zip_read_info->z_filefunc, | ||
950 | pfile_in_zip_read_info->filestream, | ||
951 | pfile_in_zip_read_info->read_buffer, | ||
952 | uReadThis)!=uReadThis) | ||
953 | return EXTRACTOR_UNZIP_ERRNO; | ||
954 | |||
955 | |||
956 | pfile_in_zip_read_info->pos_in_zipfile += uReadThis; | ||
957 | |||
958 | pfile_in_zip_read_info->rest_read_compressed-=uReadThis; | ||
959 | |||
960 | pfile_in_zip_read_info->stream.next_in = | ||
961 | (Bytef*)pfile_in_zip_read_info->read_buffer; | ||
962 | pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis; | ||
963 | } | ||
964 | |||
965 | if ((pfile_in_zip_read_info->compression_method==0) || (pfile_in_zip_read_info->raw)) | ||
966 | { | ||
967 | uInt uDoCopy,i ; | ||
968 | |||
969 | if ((pfile_in_zip_read_info->stream.avail_in == 0) && | ||
970 | (pfile_in_zip_read_info->rest_read_compressed == 0)) | ||
971 | return (iRead==0) ? EXTRACTOR_UNZIP_EOF : iRead; | ||
972 | |||
973 | if (pfile_in_zip_read_info->stream.avail_out < | ||
974 | pfile_in_zip_read_info->stream.avail_in) | ||
975 | uDoCopy = pfile_in_zip_read_info->stream.avail_out ; | ||
976 | else | ||
977 | uDoCopy = pfile_in_zip_read_info->stream.avail_in ; | ||
978 | |||
979 | for (i=0;i<uDoCopy;i++) | ||
980 | *(pfile_in_zip_read_info->stream.next_out+i) = | ||
981 | *(pfile_in_zip_read_info->stream.next_in+i); | ||
982 | |||
983 | pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, | ||
984 | pfile_in_zip_read_info->stream.next_out, | ||
985 | uDoCopy); | ||
986 | pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy; | ||
987 | pfile_in_zip_read_info->stream.avail_in -= uDoCopy; | ||
988 | pfile_in_zip_read_info->stream.avail_out -= uDoCopy; | ||
989 | pfile_in_zip_read_info->stream.next_out += uDoCopy; | ||
990 | pfile_in_zip_read_info->stream.next_in += uDoCopy; | ||
991 | pfile_in_zip_read_info->stream.total_out += uDoCopy; | ||
992 | iRead += uDoCopy; | ||
993 | } | ||
994 | else | ||
995 | { | ||
996 | uLong uTotalOutBefore,uTotalOutAfter; | ||
997 | const Bytef *bufBefore; | ||
998 | uLong uOutThis; | ||
999 | int flush=Z_SYNC_FLUSH; | ||
1000 | |||
1001 | uTotalOutBefore = pfile_in_zip_read_info->stream.total_out; | ||
1002 | bufBefore = pfile_in_zip_read_info->stream.next_out; | ||
1003 | |||
1004 | /* | ||
1005 | if ((pfile_in_zip_read_info->rest_read_uncompressed == | ||
1006 | pfile_in_zip_read_info->stream.avail_out) && | ||
1007 | (pfile_in_zip_read_info->rest_read_compressed == 0)) | ||
1008 | flush = Z_FINISH; | ||
1009 | */ | ||
1010 | err=inflate(&pfile_in_zip_read_info->stream,flush); | ||
1011 | |||
1012 | uTotalOutAfter = pfile_in_zip_read_info->stream.total_out; | ||
1013 | uOutThis = uTotalOutAfter-uTotalOutBefore; | ||
1014 | |||
1015 | pfile_in_zip_read_info->crc32 = | ||
1016 | crc32(pfile_in_zip_read_info->crc32,bufBefore, | ||
1017 | (uInt)(uOutThis)); | ||
1018 | |||
1019 | pfile_in_zip_read_info->rest_read_uncompressed -= | ||
1020 | uOutThis; | ||
1021 | |||
1022 | iRead += (uInt)(uTotalOutAfter - uTotalOutBefore); | ||
1023 | |||
1024 | if (err==Z_STREAM_END) | ||
1025 | return (iRead==0) ? EXTRACTOR_UNZIP_EOF : iRead; | ||
1026 | if (err!=Z_OK) | ||
1027 | break; | ||
1028 | } | ||
1029 | } | ||
1030 | |||
1031 | if (err==Z_OK) | ||
1032 | return iRead; | ||
1033 | return err; | ||
1034 | } | ||
1035 | |||
1036 | /* | ||
1037 | Read the local header of the current zipfile | ||
1038 | Check the coherency of the local header and info in the end of central | ||
1039 | directory about this file | ||
1040 | store in *piSizeVar the size of extra info in local header | ||
1041 | (filename and size of extra field data) | ||
1042 | */ | ||
1043 | static int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar, | ||
1044 | poffset_local_extrafield, | ||
1045 | psize_local_extrafield) | ||
1046 | unz_s* s; | ||
1047 | uInt* piSizeVar; | ||
1048 | uLong *poffset_local_extrafield; | ||
1049 | uInt *psize_local_extrafield; | ||
1050 | { | ||
1051 | uLong uMagic,uData,uFlags; | ||
1052 | uLong size_filename; | ||
1053 | uLong size_extra_field; | ||
1054 | int err=EXTRACTOR_UNZIP_OK; | ||
1055 | |||
1056 | *piSizeVar = 0; | ||
1057 | *poffset_local_extrafield = 0; | ||
1058 | *psize_local_extrafield = 0; | ||
1059 | |||
1060 | if (ZSEEK(s->z_filefunc, s->filestream,s->cur_file_info_internal.offset_curfile + | ||
1061 | s->byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
1062 | return EXTRACTOR_UNZIP_ERRNO; | ||
1063 | |||
1064 | |||
1065 | if (err==EXTRACTOR_UNZIP_OK) { | ||
1066 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != EXTRACTOR_UNZIP_OK) | ||
1067 | err=EXTRACTOR_UNZIP_ERRNO; | ||
1068 | else if (uMagic!=0x04034b50) | ||
1069 | err=EXTRACTOR_UNZIP_BADZIPFILE; | ||
1070 | } | ||
1071 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uData) != EXTRACTOR_UNZIP_OK) | ||
1072 | err=EXTRACTOR_UNZIP_ERRNO; | ||
1073 | /* | ||
1074 | else if ((err==EXTRACTOR_UNZIP_OK) && (uData!=s->cur_file_info.wVersion)) | ||
1075 | err=EXTRACTOR_UNZIP_BADZIPFILE; | ||
1076 | */ | ||
1077 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uFlags) != EXTRACTOR_UNZIP_OK) | ||
1078 | err=EXTRACTOR_UNZIP_ERRNO; | ||
1079 | |||
1080 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uData) != EXTRACTOR_UNZIP_OK) | ||
1081 | err=EXTRACTOR_UNZIP_ERRNO; | ||
1082 | else if ((err==EXTRACTOR_UNZIP_OK) && (uData!=s->cur_file_info.compression_method)) | ||
1083 | err=EXTRACTOR_UNZIP_BADZIPFILE; | ||
1084 | |||
1085 | if ((err==EXTRACTOR_UNZIP_OK) && (s->cur_file_info.compression_method!=0) && | ||
1086 | (s->cur_file_info.compression_method!=Z_DEFLATED)) | ||
1087 | err=EXTRACTOR_UNZIP_BADZIPFILE; | ||
1088 | |||
1089 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != EXTRACTOR_UNZIP_OK) /* date/time */ | ||
1090 | err=EXTRACTOR_UNZIP_ERRNO; | ||
1091 | |||
1092 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != EXTRACTOR_UNZIP_OK) /* crc */ | ||
1093 | err=EXTRACTOR_UNZIP_ERRNO; | ||
1094 | else if ((err==EXTRACTOR_UNZIP_OK) && (uData!=s->cur_file_info.crc) && | ||
1095 | ((uFlags & 8)==0)) | ||
1096 | err=EXTRACTOR_UNZIP_BADZIPFILE; | ||
1097 | |||
1098 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != EXTRACTOR_UNZIP_OK) /* size compr */ | ||
1099 | err=EXTRACTOR_UNZIP_ERRNO; | ||
1100 | else if ((err==EXTRACTOR_UNZIP_OK) && (uData!=s->cur_file_info.compressed_size) && | ||
1101 | ((uFlags & 8)==0)) | ||
1102 | err=EXTRACTOR_UNZIP_BADZIPFILE; | ||
1103 | |||
1104 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != EXTRACTOR_UNZIP_OK) /* size uncompr */ | ||
1105 | err=EXTRACTOR_UNZIP_ERRNO; | ||
1106 | else if ((err==EXTRACTOR_UNZIP_OK) && (uData!=s->cur_file_info.uncompressed_size) && | ||
1107 | ((uFlags & 8)==0)) | ||
1108 | err=EXTRACTOR_UNZIP_BADZIPFILE; | ||
1109 | |||
1110 | |||
1111 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&size_filename) != EXTRACTOR_UNZIP_OK) | ||
1112 | err=EXTRACTOR_UNZIP_ERRNO; | ||
1113 | else if ((err==EXTRACTOR_UNZIP_OK) && (size_filename!=s->cur_file_info.size_filename)) | ||
1114 | err=EXTRACTOR_UNZIP_BADZIPFILE; | ||
1115 | |||
1116 | *piSizeVar += (uInt)size_filename; | ||
1117 | |||
1118 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&size_extra_field) != EXTRACTOR_UNZIP_OK) | ||
1119 | err=EXTRACTOR_UNZIP_ERRNO; | ||
1120 | *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile + | ||
1121 | SIZEZIPLOCALHEADER + size_filename; | ||
1122 | *psize_local_extrafield = (uInt)size_extra_field; | ||
1123 | |||
1124 | *piSizeVar += (uInt)size_extra_field; | ||
1125 | |||
1126 | return err; | ||
1127 | } | ||
1128 | |||
1129 | |||
1130 | |||
1131 | /* | ||
1132 | Open for reading data the current file in the zipfile. | ||
1133 | If there is no error and the file is opened, the return value is EXTRACTOR_UNZIP_OK. | ||
1134 | */ | ||
1135 | int EXTRACTOR_common_unzip_open_current_file3 (file, method, level, raw) | ||
1136 | EXTRACTOR_unzip_file file; | ||
1137 | int* method; | ||
1138 | int* level; | ||
1139 | int raw; | ||
1140 | { | ||
1141 | int err=EXTRACTOR_UNZIP_OK; | ||
1142 | uInt iSizeVar; | ||
1143 | unz_s* s; | ||
1144 | file_in_zip_read_info_s* pfile_in_zip_read_info; | ||
1145 | uLong offset_local_extrafield; /* offset of the local extra field */ | ||
1146 | uInt size_local_extrafield; /* size of the local extra field */ | ||
1147 | |||
1148 | if (file==NULL) | ||
1149 | return EXTRACTOR_UNZIP_PARAMERROR; | ||
1150 | s=(unz_s*)file; | ||
1151 | if (!s->current_file_ok) | ||
1152 | return EXTRACTOR_UNZIP_PARAMERROR; | ||
1153 | |||
1154 | if (s->pfile_in_zip_read != NULL) | ||
1155 | EXTRACTOR_common_unzip_close_current_file(file); | ||
1156 | if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar, | ||
1157 | &offset_local_extrafield,&size_local_extrafield)!=EXTRACTOR_UNZIP_OK) | ||
1158 | return EXTRACTOR_UNZIP_BADZIPFILE; | ||
1159 | |||
1160 | pfile_in_zip_read_info = (file_in_zip_read_info_s*) | ||
1161 | ALLOC(sizeof(file_in_zip_read_info_s)); | ||
1162 | if (pfile_in_zip_read_info==NULL) | ||
1163 | return EXTRACTOR_UNZIP_INTERNALERROR; | ||
1164 | |||
1165 | pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE); | ||
1166 | pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield; | ||
1167 | pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield; | ||
1168 | pfile_in_zip_read_info->pos_local_extrafield=0; | ||
1169 | pfile_in_zip_read_info->raw=raw; | ||
1170 | |||
1171 | if (pfile_in_zip_read_info->read_buffer==NULL) | ||
1172 | { | ||
1173 | TRYFREE(pfile_in_zip_read_info); | ||
1174 | return EXTRACTOR_UNZIP_INTERNALERROR; | ||
1175 | } | ||
1176 | |||
1177 | pfile_in_zip_read_info->stream_initialised=0; | ||
1178 | |||
1179 | if (method!=NULL) | ||
1180 | *method = (int)s->cur_file_info.compression_method; | ||
1181 | |||
1182 | if (level!=NULL) | ||
1183 | { | ||
1184 | *level = 6; | ||
1185 | switch (s->cur_file_info.flag & 0x06) | ||
1186 | { | ||
1187 | case 6 : *level = 1; break; | ||
1188 | case 4 : *level = 2; break; | ||
1189 | case 2 : *level = 9; break; | ||
1190 | } | ||
1191 | } | ||
1192 | |||
1193 | if ((s->cur_file_info.compression_method!=0) && | ||
1194 | (s->cur_file_info.compression_method!=Z_DEFLATED)) | ||
1195 | err=EXTRACTOR_UNZIP_BADZIPFILE; | ||
1196 | |||
1197 | pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc; | ||
1198 | pfile_in_zip_read_info->crc32=0; | ||
1199 | pfile_in_zip_read_info->compression_method = | ||
1200 | s->cur_file_info.compression_method; | ||
1201 | pfile_in_zip_read_info->filestream=s->filestream; | ||
1202 | pfile_in_zip_read_info->z_filefunc=s->z_filefunc; | ||
1203 | pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile; | ||
1204 | |||
1205 | pfile_in_zip_read_info->stream.total_out = 0; | ||
1206 | |||
1207 | if ((s->cur_file_info.compression_method==Z_DEFLATED) && | ||
1208 | (!raw)) | ||
1209 | { | ||
1210 | pfile_in_zip_read_info->stream.zalloc = (alloc_func)0; | ||
1211 | pfile_in_zip_read_info->stream.zfree = (free_func)0; | ||
1212 | pfile_in_zip_read_info->stream.opaque = (voidpf)0; | ||
1213 | pfile_in_zip_read_info->stream.next_in = (voidpf)0; | ||
1214 | pfile_in_zip_read_info->stream.avail_in = 0; | ||
1215 | |||
1216 | err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS); | ||
1217 | if (err == Z_OK) | ||
1218 | pfile_in_zip_read_info->stream_initialised=1; | ||
1219 | else | ||
1220 | return err; | ||
1221 | /* windowBits is passed < 0 to tell that there is no zlib header. | ||
1222 | * Note that in this case inflate *requires* an extra "dummy" byte | ||
1223 | * after the compressed stream in order to complete decompression and | ||
1224 | * return Z_STREAM_END. | ||
1225 | * In unzip, i don't wait absolutely Z_STREAM_END because I known the | ||
1226 | * size of both compressed and uncompressed data | ||
1227 | */ | ||
1228 | } | ||
1229 | pfile_in_zip_read_info->rest_read_compressed = | ||
1230 | s->cur_file_info.compressed_size ; | ||
1231 | pfile_in_zip_read_info->rest_read_uncompressed = | ||
1232 | s->cur_file_info.uncompressed_size ; | ||
1233 | |||
1234 | |||
1235 | pfile_in_zip_read_info->pos_in_zipfile = | ||
1236 | s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + | ||
1237 | iSizeVar; | ||
1238 | |||
1239 | pfile_in_zip_read_info->stream.avail_in = (uInt)0; | ||
1240 | |||
1241 | s->pfile_in_zip_read = pfile_in_zip_read_info; | ||
1242 | |||
1243 | return EXTRACTOR_UNZIP_OK; | ||
1244 | } | ||
1245 | |||
1246 | typedef struct Ecls { | ||
1247 | char * data; | ||
1248 | size_t size; | ||
1249 | size_t pos; | ||
1250 | } Ecls; | ||
1251 | |||
1252 | voidpf EXTRACTOR_common_unzip_zlib_open_file_func (voidpf opaque, | ||
1253 | const char* filename, | ||
1254 | int mode) { | ||
1255 | if (0 == strcmp(filename, | ||
1256 | "ERROR")) | ||
1257 | return opaque; | ||
1258 | else | ||
1259 | return NULL; | ||
1260 | } | ||
1261 | |||
1262 | uLong EXTRACTOR_common_unzip_zlib_read_file_func(voidpf opaque, | ||
1263 | voidpf stream, | ||
1264 | void* buf, | ||
1265 | uLong size) { | ||
1266 | Ecls * e = opaque; | ||
1267 | uLong ret; | ||
1268 | |||
1269 | ret = e->size - e->pos; | ||
1270 | if (ret > size) | ||
1271 | ret = size; | ||
1272 | memcpy(buf, | ||
1273 | &e->data[e->pos], | ||
1274 | ret); | ||
1275 | e->pos += ret; | ||
1276 | return ret; | ||
1277 | } | ||
1278 | |||
1279 | long EXTRACTOR_common_unzip_zlib_tell_file_func(voidpf opaque, | ||
1280 | voidpf stream) { | ||
1281 | Ecls * e = opaque; | ||
1282 | return e->pos; | ||
1283 | } | ||
1284 | |||
1285 | long EXTRACTOR_common_unzip_zlib_seek_file_func(voidpf opaque, | ||
1286 | voidpf stream, | ||
1287 | uLong offset, | ||
1288 | int origin) { | ||
1289 | Ecls * e = opaque; | ||
1290 | |||
1291 | switch (origin) { | ||
1292 | case ZLIB_FILEFUNC_SEEK_SET: | ||
1293 | if ( (offset > e->size) || | ||
1294 | (offset < 0) ) | ||
1295 | return -1; | ||
1296 | e->pos = offset; | ||
1297 | break; | ||
1298 | case ZLIB_FILEFUNC_SEEK_END: | ||
1299 | if ( (offset > e->size) || | ||
1300 | (offset < 0) ) | ||
1301 | return -1; | ||
1302 | e->pos = e->size - offset; | ||
1303 | break; | ||
1304 | case ZLIB_FILEFUNC_SEEK_CUR: | ||
1305 | if ( (offset < - e->pos) || | ||
1306 | (offset > e->size - e->pos) ) | ||
1307 | return -1; | ||
1308 | e->pos += offset; | ||
1309 | break; | ||
1310 | default: | ||
1311 | return -1; | ||
1312 | } | ||
1313 | return 0; | ||
1314 | } | ||
1315 | |||
1316 | int EXTRACTOR_common_unzip_zlib_close_file_func(voidpf opaque, | ||
1317 | voidpf stream) { | ||
1318 | return 0; | ||
1319 | } | ||
1320 | |||
1321 | int EXTRACTOR_common_unzip_zlib_testerror_file_func(voidpf opaque, | ||
1322 | voidpf stream) { | ||
1323 | return 0; | ||
1324 | } | ||
diff --git a/src/common/unzip.h b/src/common/unzip.h new file mode 100644 index 0000000..f0c2c4e --- /dev/null +++ b/src/common/unzip.h | |||
@@ -0,0 +1,126 @@ | |||
1 | /* | ||
2 | This file is part of libextractor. | ||
3 | (C) 2008 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | libextractor is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | libextractor is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with libextractor; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | #ifndef UNZIP_H_ | ||
22 | #define UNZIP_H_ | ||
23 | |||
24 | #include <zlib.h> | ||
25 | |||
26 | #define EXTRACTOR_UNZIP_OK (0) | ||
27 | #define EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE (-100) | ||
28 | #define EXTRACTOR_UNZIP_ERRNO (Z_ERRNO) | ||
29 | #define EXTRACTOR_UNZIP_EOF (0) | ||
30 | #define EXTRACTOR_UNZIP_PARAMERROR (-102) | ||
31 | #define EXTRACTOR_UNZIP_BADZIPFILE (-103) | ||
32 | #define EXTRACTOR_UNZIP_INTERNALERROR (-104) | ||
33 | #define EXTRACTOR_UNZIP_CRCERROR (-105) | ||
34 | |||
35 | typedef voidp EXTRACTOR_unzip_file; | ||
36 | |||
37 | typedef struct EXTRACTOR_unzip_filefunc_def_s | ||
38 | { | ||
39 | voidpf ( *zopen_file) (voidpf opaque, const char* filename, int mode); | ||
40 | uLong ( *zread_file) (voidpf opaque, voidpf stream, void* buf, uLong size); | ||
41 | uLong ( *zwrite_file) (voidpf opaque, voidpf stream, const void* buf, uLong size); | ||
42 | long ( *ztell_file) (voidpf opaque, voidpf stream); | ||
43 | long ( *zseek_file) (voidpf opaque, voidpf stream, uLong offset, int origin); | ||
44 | int ( *zclose_file) (voidpf opaque, voidpf stream); | ||
45 | int ( *zerror_file) (voidpf opaque, voidpf stream); | ||
46 | voidpf opaque; | ||
47 | } EXTRACTOR_unzip_filefunc_def; | ||
48 | |||
49 | /* tm_unz contain date/time info */ | ||
50 | typedef struct EXTRACTOR_unzip_tm_unz_s | ||
51 | { | ||
52 | uInt tm_sec; /* seconds after the minute - [0,59] */ | ||
53 | uInt tm_min; /* minutes after the hour - [0,59] */ | ||
54 | uInt tm_hour; /* hours since midnight - [0,23] */ | ||
55 | uInt tm_mday; /* day of the month - [1,31] */ | ||
56 | uInt tm_mon; /* months since January - [0,11] */ | ||
57 | uInt tm_year; /* years - [1980..2044] */ | ||
58 | } EXTRACTOR_unzip_tm_unz; | ||
59 | |||
60 | /* unz_file_info contain information about a file in the zipfile */ | ||
61 | typedef struct EXTRACTOR_unzip_file_info_s | ||
62 | { | ||
63 | uLong version; /* version made by 2 bytes */ | ||
64 | uLong version_needed; /* version needed to extract 2 bytes */ | ||
65 | uLong flag; /* general purpose bit flag 2 bytes */ | ||
66 | uLong compression_method; /* compression method 2 bytes */ | ||
67 | uLong dosDate; /* last mod file date in Dos fmt 4 bytes */ | ||
68 | uLong crc; /* crc-32 4 bytes */ | ||
69 | uLong compressed_size; /* compressed size 4 bytes */ | ||
70 | uLong uncompressed_size; /* uncompressed size 4 bytes */ | ||
71 | uLong size_filename; /* filename length 2 bytes */ | ||
72 | uLong size_file_extra; /* extra field length 2 bytes */ | ||
73 | uLong size_file_comment; /* file comment length 2 bytes */ | ||
74 | |||
75 | uLong disk_num_start; /* disk number start 2 bytes */ | ||
76 | uLong internal_fa; /* internal file attributes 2 bytes */ | ||
77 | uLong external_fa; /* external file attributes 4 bytes */ | ||
78 | |||
79 | EXTRACTOR_unzip_tm_unz tmu_date; | ||
80 | } EXTRACTOR_unzip_file_info; | ||
81 | |||
82 | int EXTRACTOR_common_unzip_string_file_name_compare(const char* fileName1, | ||
83 | const char* fileName2, int iCaseSensitivity); | ||
84 | |||
85 | int EXTRACTOR_common_unzip_go_to_first_file(EXTRACTOR_unzip_file file); | ||
86 | |||
87 | EXTRACTOR_unzip_file EXTRACTOR_common_unzip_open2(const char *path, | ||
88 | EXTRACTOR_unzip_filefunc_def* pzlib_filefunc_def); | ||
89 | |||
90 | int EXTRACTOR_common_unzip_close_current_file(EXTRACTOR_unzip_file file); | ||
91 | |||
92 | int EXTRACTOR_common_unzip_close(EXTRACTOR_unzip_file file); | ||
93 | |||
94 | int EXTRACTOR_common_unzip_get_current_file_info(EXTRACTOR_unzip_file file, | ||
95 | EXTRACTOR_unzip_file_info *pfile_info, char *szFileName, uLong fileNameBufferSize, | ||
96 | void *extraField, uLong extraFieldBufferSize, char *szComment, | ||
97 | uLong commentBufferSize); | ||
98 | |||
99 | int EXTRACTOR_common_unzip_go_to_next_file(EXTRACTOR_unzip_file file); | ||
100 | |||
101 | int EXTRACTOR_common_unzip_local_file(EXTRACTOR_unzip_file file, const char *szFileName, | ||
102 | int iCaseSensitivity); | ||
103 | |||
104 | int EXTRACTOR_common_unzip_read_current_file(EXTRACTOR_unzip_file file, voidp buf, | ||
105 | unsigned len); | ||
106 | |||
107 | int EXTRACTOR_common_unzip_open_current_file3(EXTRACTOR_unzip_file file, int* method, | ||
108 | int* level, int raw); | ||
109 | |||
110 | voidpf EXTRACTOR_common_unzip_zlib_open_file_func(voidpf opaque, | ||
111 | const char* filename, int mode); | ||
112 | |||
113 | uLong EXTRACTOR_common_unzip_zlib_read_file_func(voidpf opaque, voidpf stream, | ||
114 | void* buf, uLong size); | ||
115 | |||
116 | long EXTRACTOR_common_unzip_zlib_tell_file_func(voidpf opaque, voidpf stream); | ||
117 | |||
118 | long EXTRACTOR_common_unzip_zlib_seek_file_func(voidpf opaque, voidpf stream, | ||
119 | uLong offset, int origin); | ||
120 | |||
121 | int EXTRACTOR_common_unzip_zlib_close_file_func(voidpf opaque, voidpf stream); | ||
122 | |||
123 | int EXTRACTOR_common_unzip_zlib_testerror_file_func(voidpf opaque, | ||
124 | voidpf stream); | ||
125 | |||
126 | #endif /* UNZIP_H_ */ | ||
diff --git a/src/plugins/oo/Makefile.am b/src/plugins/oo/Makefile.am index 0a328f0..4f79fbf 100644 --- a/src/plugins/oo/Makefile.am +++ b/src/plugins/oo/Makefile.am | |||
@@ -13,4 +13,5 @@ libextractor_oo_la_LDFLAGS = \ | |||
13 | 13 | ||
14 | libextractor_oo_la_LIBADD = \ | 14 | libextractor_oo_la_LIBADD = \ |
15 | $(top_builddir)/src/main/libextractor.la \ | 15 | $(top_builddir)/src/main/libextractor.la \ |
16 | $(top_builddir)/src/common/libextractor_common.la \ | ||
16 | -lz | 17 | -lz |
diff --git a/src/plugins/oo/ooextractor.c b/src/plugins/oo/ooextractor.c index e12c8ab..40558c1 100644 --- a/src/plugins/oo/ooextractor.c +++ b/src/plugins/oo/ooextractor.c | |||
@@ -22,1304 +22,11 @@ | |||
22 | #include <ctype.h> | 22 | #include <ctype.h> |
23 | #include "extractor.h" | 23 | #include "extractor.h" |
24 | #include "zlib.h" | 24 | #include "zlib.h" |
25 | #include "unzip.h" | ||
25 | 26 | ||
26 | #define CASESENSITIVITY (0) | 27 | #define CASESENSITIVITY (0) |
27 | #define MAXFILENAME (256) | 28 | #define MAXFILENAME (256) |
28 | 29 | ||
29 | /* *********************** IOAPI ***************** */ | ||
30 | |||
31 | #define ZLIB_FILEFUNC_SEEK_CUR (1) | ||
32 | #define ZLIB_FILEFUNC_SEEK_END (2) | ||
33 | #define ZLIB_FILEFUNC_SEEK_SET (0) | ||
34 | |||
35 | #define ZLIB_FILEFUNC_MODE_READ (1) | ||
36 | #define ZLIB_FILEFUNC_MODE_WRITE (2) | ||
37 | #define ZLIB_FILEFUNC_MODE_READWRITEFILTER (3) | ||
38 | #define ZLIB_FILEFUNC_MODE_EXISTING (4) | ||
39 | #define ZLIB_FILEFUNC_MODE_CREATE (8) | ||
40 | |||
41 | |||
42 | typedef voidpf ( *open_file_func) (voidpf opaque, const char* filename, int mode); | ||
43 | typedef uLong ( *read_file_func) (voidpf opaque, voidpf stream, void* buf, uLong size); | ||
44 | typedef uLong ( *write_file_func) (voidpf opaque, voidpf stream, const void* buf, uLong size); | ||
45 | typedef long ( *tell_file_func) (voidpf opaque, voidpf stream); | ||
46 | typedef long ( *seek_file_func) (voidpf opaque, voidpf stream, uLong offset, int origin); | ||
47 | typedef int ( *close_file_func) (voidpf opaque, voidpf stream); | ||
48 | typedef int ( *testerror_file_func) (voidpf opaque, voidpf stream); | ||
49 | |||
50 | typedef struct zlib_filefunc_def_s | ||
51 | { | ||
52 | open_file_func zopen_file; | ||
53 | read_file_func zread_file; | ||
54 | write_file_func zwrite_file; | ||
55 | tell_file_func ztell_file; | ||
56 | seek_file_func zseek_file; | ||
57 | close_file_func zclose_file; | ||
58 | testerror_file_func zerror_file; | ||
59 | voidpf opaque; | ||
60 | } zlib_filefunc_def; | ||
61 | |||
62 | #define ZREAD(filefunc,filestream,buf,size) ((*((filefunc).zread_file))((filefunc).opaque,filestream,buf,size)) | ||
63 | #define ZWRITE(filefunc,filestream,buf,size) ((*((filefunc).zwrite_file))((filefunc).opaque,filestream,buf,size)) | ||
64 | #define ZTELL(filefunc,filestream) ((*((filefunc).ztell_file))((filefunc).opaque,filestream)) | ||
65 | #define ZSEEK(filefunc,filestream,pos,mode) ((*((filefunc).zseek_file))((filefunc).opaque,filestream,pos,mode)) | ||
66 | #define ZCLOSE(filefunc,filestream) ((*((filefunc).zclose_file))((filefunc).opaque,filestream)) | ||
67 | #define ZERROR(filefunc,filestream) ((*((filefunc).zerror_file))((filefunc).opaque,filestream)) | ||
68 | |||
69 | |||
70 | /* ******************* former crypt.h ********************* */ | ||
71 | |||
72 | #if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP) | ||
73 | /* like the STRICT of WIN32, we define a pointer that cannot be converted | ||
74 | from (void*) without cast */ | ||
75 | typedef struct TagunzFile__ { int unused; } unzFile__; | ||
76 | typedef unzFile__ *unzFile; | ||
77 | #else | ||
78 | typedef voidp unzFile; | ||
79 | #endif | ||
80 | |||
81 | |||
82 | #define UNZ_OK (0) | ||
83 | #define UNZ_END_OF_LIST_OF_FILE (-100) | ||
84 | #define UNZ_ERRNO (Z_ERRNO) | ||
85 | #define UNZ_EOF (0) | ||
86 | #define UNZ_PARAMERROR (-102) | ||
87 | #define UNZ_BADZIPFILE (-103) | ||
88 | #define UNZ_INTERNALERROR (-104) | ||
89 | #define UNZ_CRCERROR (-105) | ||
90 | |||
91 | /* tm_unz contain date/time info */ | ||
92 | typedef struct tm_unz_s | ||
93 | { | ||
94 | uInt tm_sec; /* seconds after the minute - [0,59] */ | ||
95 | uInt tm_min; /* minutes after the hour - [0,59] */ | ||
96 | uInt tm_hour; /* hours since midnight - [0,23] */ | ||
97 | uInt tm_mday; /* day of the month - [1,31] */ | ||
98 | uInt tm_mon; /* months since January - [0,11] */ | ||
99 | uInt tm_year; /* years - [1980..2044] */ | ||
100 | } tm_unz; | ||
101 | |||
102 | /* unz_global_info structure contain global data about the ZIPfile | ||
103 | These data comes from the end of central dir */ | ||
104 | typedef struct unz_global_info_s | ||
105 | { | ||
106 | uLong number_entry; /* total number of entries in | ||
107 | the central dir on this disk */ | ||
108 | uLong size_comment; /* size of the global comment of the zipfile */ | ||
109 | } unz_global_info; | ||
110 | |||
111 | |||
112 | /* unz_file_info contain information about a file in the zipfile */ | ||
113 | typedef struct unz_file_info_s | ||
114 | { | ||
115 | uLong version; /* version made by 2 bytes */ | ||
116 | uLong version_needed; /* version needed to extract 2 bytes */ | ||
117 | uLong flag; /* general purpose bit flag 2 bytes */ | ||
118 | uLong compression_method; /* compression method 2 bytes */ | ||
119 | uLong dosDate; /* last mod file date in Dos fmt 4 bytes */ | ||
120 | uLong crc; /* crc-32 4 bytes */ | ||
121 | uLong compressed_size; /* compressed size 4 bytes */ | ||
122 | uLong uncompressed_size; /* uncompressed size 4 bytes */ | ||
123 | uLong size_filename; /* filename length 2 bytes */ | ||
124 | uLong size_file_extra; /* extra field length 2 bytes */ | ||
125 | uLong size_file_comment; /* file comment length 2 bytes */ | ||
126 | |||
127 | uLong disk_num_start; /* disk number start 2 bytes */ | ||
128 | uLong internal_fa; /* internal file attributes 2 bytes */ | ||
129 | uLong external_fa; /* external file attributes 4 bytes */ | ||
130 | |||
131 | tm_unz tmu_date; | ||
132 | } unz_file_info; | ||
133 | |||
134 | |||
135 | /* | ||
136 | Read extra field from the current file (opened by unzOpenCurrentFile) | ||
137 | This is the local-header version of the extra field (sometimes, there is | ||
138 | more info in the local-header version than in the central-header) | ||
139 | |||
140 | if buf==NULL, it return the size of the local extra field | ||
141 | |||
142 | if buf!=NULL, len is the size of the buffer, the extra header is copied in | ||
143 | buf. | ||
144 | the return value is the number of bytes copied in buf, or (if <0) | ||
145 | the error code | ||
146 | */ | ||
147 | |||
148 | #ifndef CASESENSITIVITYDEFAULT_NO | ||
149 | # if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) | ||
150 | # define CASESENSITIVITYDEFAULT_NO | ||
151 | # endif | ||
152 | #endif | ||
153 | |||
154 | |||
155 | #ifndef UNZ_BUFSIZE | ||
156 | #define UNZ_BUFSIZE (16384) | ||
157 | #endif | ||
158 | |||
159 | #ifndef UNZ_MAXFILENAMEINZIP | ||
160 | #define UNZ_MAXFILENAMEINZIP (256) | ||
161 | #endif | ||
162 | |||
163 | #ifndef ALLOC | ||
164 | # define ALLOC(size) (malloc(size)) | ||
165 | #endif | ||
166 | #ifndef TRYFREE | ||
167 | # define TRYFREE(p) {if (p) free(p);} | ||
168 | #endif | ||
169 | |||
170 | #define SIZECENTRALDIRITEM (0x2e) | ||
171 | #define SIZEZIPLOCALHEADER (0x1e) | ||
172 | |||
173 | |||
174 | const char unz_copyright[] = | ||
175 | " unzip 1.00 Copyright 1998-2003 Gilles Vollant - http://www.winimage.com/zLibDll"; | ||
176 | |||
177 | /* unz_file_info_interntal contain internal info about a file in zipfile*/ | ||
178 | typedef struct unz_file_info_internal_s | ||
179 | { | ||
180 | uLong offset_curfile;/* relative offset of local header 4 bytes */ | ||
181 | } unz_file_info_internal; | ||
182 | |||
183 | |||
184 | /* file_in_zip_read_info_s contain internal information about a file in zipfile, | ||
185 | when reading and decompress it */ | ||
186 | typedef struct | ||
187 | { | ||
188 | char *read_buffer; /* internal buffer for compressed data */ | ||
189 | z_stream stream; /* zLib stream structure for inflate */ | ||
190 | |||
191 | uLong pos_in_zipfile; /* position in byte on the zipfile, for fseek*/ | ||
192 | uLong stream_initialised; /* flag set if stream structure is initialised*/ | ||
193 | |||
194 | uLong offset_local_extrafield;/* offset of the local extra field */ | ||
195 | uInt size_local_extrafield;/* size of the local extra field */ | ||
196 | uLong pos_local_extrafield; /* position in the local extra field in read*/ | ||
197 | |||
198 | uLong crc32; /* crc32 of all data uncompressed */ | ||
199 | uLong crc32_wait; /* crc32 we must obtain after decompress all */ | ||
200 | uLong rest_read_compressed; /* number of byte to be decompressed */ | ||
201 | uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/ | ||
202 | zlib_filefunc_def z_filefunc; | ||
203 | voidpf filestream; /* io structore of the zipfile */ | ||
204 | uLong compression_method; /* compression method (0==store) */ | ||
205 | uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ | ||
206 | int raw; | ||
207 | } file_in_zip_read_info_s; | ||
208 | |||
209 | |||
210 | /* unz_s contain internal information about the zipfile | ||
211 | */ | ||
212 | typedef struct | ||
213 | { | ||
214 | zlib_filefunc_def z_filefunc; | ||
215 | voidpf filestream; /* io structore of the zipfile */ | ||
216 | unz_global_info gi; /* public global information */ | ||
217 | uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ | ||
218 | uLong num_file; /* number of the current file in the zipfile*/ | ||
219 | uLong pos_in_central_dir; /* pos of the current file in the central dir*/ | ||
220 | uLong current_file_ok; /* flag about the usability of the current file*/ | ||
221 | uLong central_pos; /* position of the beginning of the central dir*/ | ||
222 | |||
223 | uLong size_central_dir; /* size of the central directory */ | ||
224 | uLong offset_central_dir; /* offset of start of central directory with | ||
225 | respect to the starting disk number */ | ||
226 | |||
227 | unz_file_info cur_file_info; /* public info about the current file in zip*/ | ||
228 | unz_file_info_internal cur_file_info_internal; /* private info about it*/ | ||
229 | file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current | ||
230 | file if we are decompressing it */ | ||
231 | int encrypted; | ||
232 | } unz_s; | ||
233 | |||
234 | |||
235 | |||
236 | /* =========================================================================== | ||
237 | Read a byte from a gz_stream; update next_in and avail_in. Return EOF | ||
238 | for end of file. | ||
239 | IN assertion: the stream s has been sucessfully opened for reading. | ||
240 | */ | ||
241 | |||
242 | |||
243 | static int unzlocal_getByte OF(( | ||
244 | const zlib_filefunc_def* pzlib_filefunc_def, | ||
245 | voidpf filestream, | ||
246 | int *pi)); | ||
247 | |||
248 | static int unzlocal_getByte(pzlib_filefunc_def,filestream,pi) | ||
249 | const zlib_filefunc_def* pzlib_filefunc_def; | ||
250 | voidpf filestream; | ||
251 | int *pi; | ||
252 | { | ||
253 | unsigned char c; | ||
254 | int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1); | ||
255 | if (err==1) | ||
256 | { | ||
257 | *pi = (int)c; | ||
258 | return UNZ_OK; | ||
259 | } | ||
260 | else | ||
261 | { | ||
262 | if (ZERROR(*pzlib_filefunc_def,filestream)) | ||
263 | return UNZ_ERRNO; | ||
264 | else | ||
265 | return UNZ_EOF; | ||
266 | } | ||
267 | } | ||
268 | |||
269 | |||
270 | /* =========================================================================== | ||
271 | Reads a long in LSB order from the given gz_stream. Sets | ||
272 | */ | ||
273 | static int unzlocal_getShort OF(( | ||
274 | const zlib_filefunc_def* pzlib_filefunc_def, | ||
275 | voidpf filestream, | ||
276 | uLong *pX)); | ||
277 | |||
278 | static int unzlocal_getShort (pzlib_filefunc_def,filestream,pX) | ||
279 | const zlib_filefunc_def* pzlib_filefunc_def; | ||
280 | voidpf filestream; | ||
281 | uLong *pX; | ||
282 | { | ||
283 | uLong x ; | ||
284 | int i; | ||
285 | int err; | ||
286 | |||
287 | err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
288 | x = (uLong)i; | ||
289 | |||
290 | if (err==UNZ_OK) | ||
291 | err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
292 | x += ((uLong)i)<<8; | ||
293 | |||
294 | if (err==UNZ_OK) | ||
295 | *pX = x; | ||
296 | else | ||
297 | *pX = 0; | ||
298 | return err; | ||
299 | } | ||
300 | |||
301 | static int unzlocal_getLong OF(( | ||
302 | const zlib_filefunc_def* pzlib_filefunc_def, | ||
303 | voidpf filestream, | ||
304 | uLong *pX)); | ||
305 | |||
306 | static int unzlocal_getLong (pzlib_filefunc_def,filestream,pX) | ||
307 | const zlib_filefunc_def* pzlib_filefunc_def; | ||
308 | voidpf filestream; | ||
309 | uLong *pX; | ||
310 | { | ||
311 | uLong x ; | ||
312 | int i; | ||
313 | int err; | ||
314 | |||
315 | err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
316 | x = (uLong)i; | ||
317 | |||
318 | if (err==UNZ_OK) | ||
319 | err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
320 | x += ((uLong)i)<<8; | ||
321 | |||
322 | if (err==UNZ_OK) | ||
323 | err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
324 | x += ((uLong)i)<<16; | ||
325 | |||
326 | if (err==UNZ_OK) | ||
327 | err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
328 | x += ((uLong)i)<<24; | ||
329 | |||
330 | if (err==UNZ_OK) | ||
331 | *pX = x; | ||
332 | else | ||
333 | *pX = 0; | ||
334 | return err; | ||
335 | } | ||
336 | |||
337 | |||
338 | /* My own strcmpi / strcasecmp */ | ||
339 | static int strcmpcasenosensitive_internal (fileName1,fileName2) | ||
340 | const char* fileName1; | ||
341 | const char* fileName2; | ||
342 | { | ||
343 | for (;;) | ||
344 | { | ||
345 | char c1=*(fileName1++); | ||
346 | char c2=*(fileName2++); | ||
347 | if ((c1>='a') && (c1<='z')) | ||
348 | c1 -= 0x20; | ||
349 | if ((c2>='a') && (c2<='z')) | ||
350 | c2 -= 0x20; | ||
351 | if (c1=='\0') | ||
352 | return ((c2=='\0') ? 0 : -1); | ||
353 | if (c2=='\0') | ||
354 | return 1; | ||
355 | if (c1<c2) | ||
356 | return -1; | ||
357 | if (c1>c2) | ||
358 | return 1; | ||
359 | } | ||
360 | } | ||
361 | |||
362 | |||
363 | #ifdef CASESENSITIVITYDEFAULT_NO | ||
364 | #define CASESENSITIVITYDEFAULTVALUE 2 | ||
365 | #else | ||
366 | #define CASESENSITIVITYDEFAULTVALUE 1 | ||
367 | #endif | ||
368 | |||
369 | #ifndef STRCMPCASENOSENTIVEFUNCTION | ||
370 | #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal | ||
371 | #endif | ||
372 | |||
373 | /* | ||
374 | Compare two filename (fileName1,fileName2). | ||
375 | If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) | ||
376 | If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi | ||
377 | or strcasecmp) | ||
378 | If iCaseSenisivity = 0, case sensitivity is defaut of your operating system | ||
379 | (like 1 on Unix, 2 on Windows) | ||
380 | |||
381 | */ | ||
382 | static int ZEXPORT unzStringFileNameCompare (fileName1,fileName2,iCaseSensitivity) | ||
383 | const char* fileName1; | ||
384 | const char* fileName2; | ||
385 | int iCaseSensitivity; | ||
386 | { | ||
387 | if (iCaseSensitivity==0) | ||
388 | iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE; | ||
389 | |||
390 | if (iCaseSensitivity==1) | ||
391 | return strcmp(fileName1,fileName2); | ||
392 | |||
393 | return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2); | ||
394 | } | ||
395 | |||
396 | #ifndef BUFREADCOMMENT | ||
397 | #define BUFREADCOMMENT (0x400) | ||
398 | #endif | ||
399 | |||
400 | /* | ||
401 | Locate the Central directory of a zipfile (at the end, just before | ||
402 | the global comment) | ||
403 | */ | ||
404 | static uLong unzlocal_SearchCentralDir OF(( | ||
405 | const zlib_filefunc_def* pzlib_filefunc_def, | ||
406 | voidpf filestream)); | ||
407 | |||
408 | static uLong unzlocal_SearchCentralDir(pzlib_filefunc_def,filestream) | ||
409 | const zlib_filefunc_def* pzlib_filefunc_def; | ||
410 | voidpf filestream; | ||
411 | { | ||
412 | unsigned char* buf; | ||
413 | uLong uSizeFile; | ||
414 | uLong uBackRead; | ||
415 | uLong uMaxBack=0xffff; /* maximum size of global comment */ | ||
416 | uLong uPosFound=0; | ||
417 | |||
418 | if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) | ||
419 | return 0; | ||
420 | |||
421 | |||
422 | uSizeFile = ZTELL(*pzlib_filefunc_def,filestream); | ||
423 | |||
424 | if (uMaxBack>uSizeFile) | ||
425 | uMaxBack = uSizeFile; | ||
426 | |||
427 | buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); | ||
428 | if (buf==NULL) | ||
429 | return 0; | ||
430 | |||
431 | uBackRead = 4; | ||
432 | while (uBackRead<uMaxBack) | ||
433 | { | ||
434 | uLong uReadSize,uReadPos ; | ||
435 | int i; | ||
436 | if (uBackRead+BUFREADCOMMENT>uMaxBack) | ||
437 | uBackRead = uMaxBack; | ||
438 | else | ||
439 | uBackRead+=BUFREADCOMMENT; | ||
440 | uReadPos = uSizeFile-uBackRead ; | ||
441 | |||
442 | uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? | ||
443 | (BUFREADCOMMENT+4) : (uSizeFile-uReadPos); | ||
444 | if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
445 | break; | ||
446 | |||
447 | if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) | ||
448 | break; | ||
449 | |||
450 | for (i=(int)uReadSize-3; (i--)>0;) | ||
451 | if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && | ||
452 | ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) | ||
453 | { | ||
454 | uPosFound = uReadPos+i; | ||
455 | break; | ||
456 | } | ||
457 | |||
458 | if (uPosFound!=0) | ||
459 | break; | ||
460 | } | ||
461 | TRYFREE(buf); | ||
462 | return uPosFound; | ||
463 | } | ||
464 | |||
465 | /* | ||
466 | Translate date/time from Dos format to tm_unz (readable more easilty) | ||
467 | */ | ||
468 | static void unzlocal_DosDateToTmuDate (ulDosDate, ptm) | ||
469 | uLong ulDosDate; | ||
470 | tm_unz* ptm; | ||
471 | { | ||
472 | uLong uDate; | ||
473 | uDate = (uLong)(ulDosDate>>16); | ||
474 | ptm->tm_mday = (uInt)(uDate&0x1f) ; | ||
475 | ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ; | ||
476 | ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ; | ||
477 | |||
478 | ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800); | ||
479 | ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ; | ||
480 | ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ; | ||
481 | } | ||
482 | |||
483 | |||
484 | |||
485 | static int unzlocal_GetCurrentFileInfoInternal (file, | ||
486 | pfile_info, | ||
487 | pfile_info_internal, | ||
488 | szFileName, fileNameBufferSize, | ||
489 | extraField, extraFieldBufferSize, | ||
490 | szComment, commentBufferSize) | ||
491 | unzFile file; | ||
492 | unz_file_info *pfile_info; | ||
493 | unz_file_info_internal *pfile_info_internal; | ||
494 | char *szFileName; | ||
495 | uLong fileNameBufferSize; | ||
496 | void *extraField; | ||
497 | uLong extraFieldBufferSize; | ||
498 | char *szComment; | ||
499 | uLong commentBufferSize; | ||
500 | { | ||
501 | unz_s* s; | ||
502 | unz_file_info file_info; | ||
503 | unz_file_info_internal file_info_internal; | ||
504 | int err=UNZ_OK; | ||
505 | uLong uMagic; | ||
506 | long lSeek=0; | ||
507 | |||
508 | if (file==NULL) | ||
509 | return UNZ_PARAMERROR; | ||
510 | s=(unz_s*)file; | ||
511 | if (ZSEEK(s->z_filefunc, s->filestream, | ||
512 | s->pos_in_central_dir+s->byte_before_the_zipfile, | ||
513 | ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
514 | err=UNZ_ERRNO; | ||
515 | |||
516 | |||
517 | /* we check the magic */ | ||
518 | if (err==UNZ_OK) { | ||
519 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK) | ||
520 | err=UNZ_ERRNO; | ||
521 | else if (uMagic!=0x02014b50) | ||
522 | err=UNZ_BADZIPFILE; | ||
523 | } | ||
524 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.version) != UNZ_OK) | ||
525 | err=UNZ_ERRNO; | ||
526 | |||
527 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.version_needed) != UNZ_OK) | ||
528 | err=UNZ_ERRNO; | ||
529 | |||
530 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.flag) != UNZ_OK) | ||
531 | err=UNZ_ERRNO; | ||
532 | |||
533 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.compression_method) != UNZ_OK) | ||
534 | err=UNZ_ERRNO; | ||
535 | |||
536 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.dosDate) != UNZ_OK) | ||
537 | err=UNZ_ERRNO; | ||
538 | |||
539 | unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date); | ||
540 | |||
541 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.crc) != UNZ_OK) | ||
542 | err=UNZ_ERRNO; | ||
543 | |||
544 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.compressed_size) != UNZ_OK) | ||
545 | err=UNZ_ERRNO; | ||
546 | |||
547 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != UNZ_OK) | ||
548 | err=UNZ_ERRNO; | ||
549 | |||
550 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_filename) != UNZ_OK) | ||
551 | err=UNZ_ERRNO; | ||
552 | |||
553 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_extra) != UNZ_OK) | ||
554 | err=UNZ_ERRNO; | ||
555 | |||
556 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_comment) != UNZ_OK) | ||
557 | err=UNZ_ERRNO; | ||
558 | |||
559 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != UNZ_OK) | ||
560 | err=UNZ_ERRNO; | ||
561 | |||
562 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.internal_fa) != UNZ_OK) | ||
563 | err=UNZ_ERRNO; | ||
564 | |||
565 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.external_fa) != UNZ_OK) | ||
566 | err=UNZ_ERRNO; | ||
567 | |||
568 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != UNZ_OK) | ||
569 | err=UNZ_ERRNO; | ||
570 | |||
571 | lSeek+=file_info.size_filename; | ||
572 | if ((err==UNZ_OK) && (szFileName!=NULL)) | ||
573 | { | ||
574 | uLong uSizeRead ; | ||
575 | if (file_info.size_filename<fileNameBufferSize) | ||
576 | { | ||
577 | *(szFileName+file_info.size_filename)='\0'; | ||
578 | uSizeRead = file_info.size_filename; | ||
579 | } | ||
580 | else | ||
581 | uSizeRead = fileNameBufferSize; | ||
582 | |||
583 | if ((file_info.size_filename>0) && (fileNameBufferSize>0)) | ||
584 | if (ZREAD(s->z_filefunc, s->filestream,szFileName,uSizeRead)!=uSizeRead) | ||
585 | err=UNZ_ERRNO; | ||
586 | lSeek -= uSizeRead; | ||
587 | } | ||
588 | |||
589 | |||
590 | if ((err==UNZ_OK) && (extraField!=NULL)) | ||
591 | { | ||
592 | uLong uSizeRead ; | ||
593 | if (file_info.size_file_extra<extraFieldBufferSize) | ||
594 | uSizeRead = file_info.size_file_extra; | ||
595 | else | ||
596 | uSizeRead = extraFieldBufferSize; | ||
597 | |||
598 | if (lSeek!=0) { | ||
599 | if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0) | ||
600 | lSeek=0; | ||
601 | else | ||
602 | err=UNZ_ERRNO; | ||
603 | } | ||
604 | if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0)) | ||
605 | if (ZREAD(s->z_filefunc, s->filestream,extraField,uSizeRead)!=uSizeRead) | ||
606 | err=UNZ_ERRNO; | ||
607 | lSeek += file_info.size_file_extra - uSizeRead; | ||
608 | } | ||
609 | else | ||
610 | lSeek+=file_info.size_file_extra; | ||
611 | |||
612 | |||
613 | if ((err==UNZ_OK) && (szComment!=NULL)) | ||
614 | { | ||
615 | uLong uSizeRead ; | ||
616 | if (file_info.size_file_comment<commentBufferSize) | ||
617 | { | ||
618 | *(szComment+file_info.size_file_comment)='\0'; | ||
619 | uSizeRead = file_info.size_file_comment; | ||
620 | } | ||
621 | else | ||
622 | uSizeRead = commentBufferSize; | ||
623 | |||
624 | if (lSeek!=0) { | ||
625 | if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0) | ||
626 | lSeek=0; | ||
627 | else | ||
628 | err=UNZ_ERRNO; | ||
629 | } | ||
630 | if ((file_info.size_file_comment>0) && (commentBufferSize>0)) | ||
631 | if (ZREAD(s->z_filefunc, s->filestream,szComment,uSizeRead)!=uSizeRead) | ||
632 | err=UNZ_ERRNO; | ||
633 | lSeek+=file_info.size_file_comment - uSizeRead; | ||
634 | } | ||
635 | else | ||
636 | lSeek+=file_info.size_file_comment; | ||
637 | |||
638 | if ((err==UNZ_OK) && (pfile_info!=NULL)) | ||
639 | *pfile_info=file_info; | ||
640 | |||
641 | if ((err==UNZ_OK) && (pfile_info_internal!=NULL)) | ||
642 | *pfile_info_internal=file_info_internal; | ||
643 | |||
644 | return err; | ||
645 | } | ||
646 | |||
647 | /* | ||
648 | Set the current file of the zipfile to the first file. | ||
649 | return UNZ_OK if there is no problem | ||
650 | */ | ||
651 | static int ZEXPORT unzGoToFirstFile (file) | ||
652 | unzFile file; | ||
653 | { | ||
654 | int err=UNZ_OK; | ||
655 | unz_s* s; | ||
656 | if (file==NULL) | ||
657 | return UNZ_PARAMERROR; | ||
658 | s=(unz_s*)file; | ||
659 | s->pos_in_central_dir=s->offset_central_dir; | ||
660 | s->num_file=0; | ||
661 | err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, | ||
662 | &s->cur_file_info_internal, | ||
663 | NULL,0,NULL,0,NULL,0); | ||
664 | s->current_file_ok = (err == UNZ_OK); | ||
665 | return err; | ||
666 | } | ||
667 | |||
668 | |||
669 | /* | ||
670 | Open a Zip file. path contain the full pathname (by example, | ||
671 | on a Windows NT computer "c:\\test\\zlib114.zip" or on an Unix computer | ||
672 | "zlib/zlib114.zip". | ||
673 | If the zipfile cannot be opened (file doesn't exist or in not valid), the | ||
674 | return value is NULL. | ||
675 | Else, the return value is a unzFile Handle, usable with other function | ||
676 | of this unzip package. | ||
677 | */ | ||
678 | static unzFile ZEXPORT unzOpen2 (path, pzlib_filefunc_def) | ||
679 | const char *path; | ||
680 | zlib_filefunc_def* pzlib_filefunc_def; | ||
681 | { | ||
682 | unz_s us; | ||
683 | unz_s *s; | ||
684 | uLong central_pos,uL; | ||
685 | |||
686 | uLong number_disk; /* number of the current dist, used for | ||
687 | spaning ZIP, unsupported, always 0*/ | ||
688 | uLong number_disk_with_CD; /* number the the disk with central dir, used | ||
689 | for spaning ZIP, unsupported, always 0*/ | ||
690 | uLong number_entry_CD; /* total number of entries in | ||
691 | the central dir | ||
692 | (same than number_entry on nospan) */ | ||
693 | |||
694 | int err=UNZ_OK; | ||
695 | |||
696 | if (unz_copyright[0]!=' ') | ||
697 | return NULL; | ||
698 | |||
699 | us.z_filefunc = *pzlib_filefunc_def; | ||
700 | |||
701 | us.filestream= (*(us.z_filefunc.zopen_file))(us.z_filefunc.opaque, | ||
702 | path, | ||
703 | ZLIB_FILEFUNC_MODE_READ | | ||
704 | ZLIB_FILEFUNC_MODE_EXISTING); | ||
705 | if (us.filestream==NULL) | ||
706 | return NULL; | ||
707 | |||
708 | central_pos = unzlocal_SearchCentralDir(&us.z_filefunc,us.filestream); | ||
709 | if (central_pos==0) | ||
710 | err=UNZ_ERRNO; | ||
711 | |||
712 | if (ZSEEK(us.z_filefunc, us.filestream, | ||
713 | central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
714 | err=UNZ_ERRNO; | ||
715 | |||
716 | /* the signature, already checked */ | ||
717 | if (unzlocal_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK) | ||
718 | err=UNZ_ERRNO; | ||
719 | |||
720 | /* number of this disk */ | ||
721 | if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK) | ||
722 | err=UNZ_ERRNO; | ||
723 | |||
724 | /* number of the disk with the start of the central directory */ | ||
725 | if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK) | ||
726 | err=UNZ_ERRNO; | ||
727 | |||
728 | /* total number of entries in the central dir on this disk */ | ||
729 | if (unzlocal_getShort(&us.z_filefunc, us.filestream,&us.gi.number_entry)!=UNZ_OK) | ||
730 | err=UNZ_ERRNO; | ||
731 | |||
732 | /* total number of entries in the central dir */ | ||
733 | if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_entry_CD)!=UNZ_OK) | ||
734 | err=UNZ_ERRNO; | ||
735 | |||
736 | if ((number_entry_CD!=us.gi.number_entry) || | ||
737 | (number_disk_with_CD!=0) || | ||
738 | (number_disk!=0)) | ||
739 | err=UNZ_BADZIPFILE; | ||
740 | |||
741 | /* size of the central directory */ | ||
742 | if (unzlocal_getLong(&us.z_filefunc, us.filestream,&us.size_central_dir)!=UNZ_OK) | ||
743 | err=UNZ_ERRNO; | ||
744 | |||
745 | /* offset of start of central directory with respect to the | ||
746 | starting disk number */ | ||
747 | if (unzlocal_getLong(&us.z_filefunc, us.filestream,&us.offset_central_dir)!=UNZ_OK) | ||
748 | err=UNZ_ERRNO; | ||
749 | |||
750 | /* zipfile comment length */ | ||
751 | if (unzlocal_getShort(&us.z_filefunc, us.filestream,&us.gi.size_comment)!=UNZ_OK) | ||
752 | err=UNZ_ERRNO; | ||
753 | |||
754 | if ((central_pos<us.offset_central_dir+us.size_central_dir) && | ||
755 | (err==UNZ_OK)) | ||
756 | err=UNZ_BADZIPFILE; | ||
757 | |||
758 | if (err!=UNZ_OK) | ||
759 | { | ||
760 | ZCLOSE(us.z_filefunc, us.filestream); | ||
761 | return NULL; | ||
762 | } | ||
763 | |||
764 | us.byte_before_the_zipfile = central_pos - | ||
765 | (us.offset_central_dir+us.size_central_dir); | ||
766 | us.central_pos = central_pos; | ||
767 | us.pfile_in_zip_read = NULL; | ||
768 | us.encrypted = 0; | ||
769 | |||
770 | |||
771 | s=(unz_s*)ALLOC(sizeof(unz_s)); | ||
772 | *s=us; | ||
773 | unzGoToFirstFile((unzFile)s); | ||
774 | return (unzFile)s; | ||
775 | } | ||
776 | |||
777 | /* | ||
778 | Close the file in zip opened with unzipOpenCurrentFile | ||
779 | Return UNZ_CRCERROR if all the file was read but the CRC is not good | ||
780 | */ | ||
781 | static int ZEXPORT unzCloseCurrentFile (file) | ||
782 | unzFile file; | ||
783 | { | ||
784 | int err=UNZ_OK; | ||
785 | |||
786 | unz_s* s; | ||
787 | file_in_zip_read_info_s* pfile_in_zip_read_info; | ||
788 | if (file==NULL) | ||
789 | return UNZ_PARAMERROR; | ||
790 | s=(unz_s*)file; | ||
791 | pfile_in_zip_read_info=s->pfile_in_zip_read; | ||
792 | |||
793 | if (pfile_in_zip_read_info==NULL) | ||
794 | return UNZ_PARAMERROR; | ||
795 | |||
796 | |||
797 | if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) && | ||
798 | (!pfile_in_zip_read_info->raw)) | ||
799 | { | ||
800 | if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait) | ||
801 | err=UNZ_CRCERROR; | ||
802 | } | ||
803 | |||
804 | |||
805 | TRYFREE(pfile_in_zip_read_info->read_buffer); | ||
806 | pfile_in_zip_read_info->read_buffer = NULL; | ||
807 | if (pfile_in_zip_read_info->stream_initialised) | ||
808 | inflateEnd(&pfile_in_zip_read_info->stream); | ||
809 | |||
810 | pfile_in_zip_read_info->stream_initialised = 0; | ||
811 | TRYFREE(pfile_in_zip_read_info); | ||
812 | |||
813 | s->pfile_in_zip_read=NULL; | ||
814 | |||
815 | return err; | ||
816 | } | ||
817 | |||
818 | /* | ||
819 | Close a ZipFile opened with unzipOpen. | ||
820 | If there is files inside the .Zip opened with unzipOpenCurrentFile (see later), | ||
821 | these files MUST be closed with unzipCloseCurrentFile before call unzipClose. | ||
822 | return UNZ_OK if there is no problem. */ | ||
823 | static int ZEXPORT unzClose (file) | ||
824 | unzFile file; | ||
825 | { | ||
826 | unz_s* s; | ||
827 | if (file==NULL) | ||
828 | return UNZ_PARAMERROR; | ||
829 | s=(unz_s*)file; | ||
830 | |||
831 | if (s->pfile_in_zip_read!=NULL) | ||
832 | unzCloseCurrentFile(file); | ||
833 | |||
834 | ZCLOSE(s->z_filefunc, s->filestream); | ||
835 | TRYFREE(s); | ||
836 | return UNZ_OK; | ||
837 | } | ||
838 | |||
839 | |||
840 | /* | ||
841 | Write info about the ZipFile in the *pglobal_info structure. | ||
842 | No preparation of the structure is needed | ||
843 | return UNZ_OK if there is no problem. | ||
844 | */ | ||
845 | static int ZEXPORT unzGetCurrentFileInfo (file, | ||
846 | pfile_info, | ||
847 | szFileName, fileNameBufferSize, | ||
848 | extraField, extraFieldBufferSize, | ||
849 | szComment, commentBufferSize) | ||
850 | unzFile file; | ||
851 | unz_file_info *pfile_info; | ||
852 | char *szFileName; | ||
853 | uLong fileNameBufferSize; | ||
854 | void *extraField; | ||
855 | uLong extraFieldBufferSize; | ||
856 | char *szComment; | ||
857 | uLong commentBufferSize; | ||
858 | { | ||
859 | return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL, | ||
860 | szFileName,fileNameBufferSize, | ||
861 | extraField,extraFieldBufferSize, | ||
862 | szComment,commentBufferSize); | ||
863 | } | ||
864 | |||
865 | /* | ||
866 | Set the current file of the zipfile to the next file. | ||
867 | return UNZ_OK if there is no problem | ||
868 | return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. | ||
869 | */ | ||
870 | static int ZEXPORT unzGoToNextFile (file) | ||
871 | unzFile file; | ||
872 | { | ||
873 | unz_s* s; | ||
874 | int err; | ||
875 | |||
876 | if (file==NULL) | ||
877 | return UNZ_PARAMERROR; | ||
878 | s=(unz_s*)file; | ||
879 | if (!s->current_file_ok) | ||
880 | return UNZ_END_OF_LIST_OF_FILE; | ||
881 | if (s->num_file+1==s->gi.number_entry) | ||
882 | return UNZ_END_OF_LIST_OF_FILE; | ||
883 | |||
884 | s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename + | ||
885 | s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ; | ||
886 | s->num_file++; | ||
887 | err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, | ||
888 | &s->cur_file_info_internal, | ||
889 | NULL,0,NULL,0,NULL,0); | ||
890 | s->current_file_ok = (err == UNZ_OK); | ||
891 | return err; | ||
892 | } | ||
893 | |||
894 | |||
895 | |||
896 | |||
897 | /* | ||
898 | Try locate the file szFileName in the zipfile. | ||
899 | For the iCaseSensitivity signification, see unzipStringFileNameCompare | ||
900 | |||
901 | return value : | ||
902 | UNZ_OK if the file is found. It becomes the current file. | ||
903 | UNZ_END_OF_LIST_OF_FILE if the file is not found | ||
904 | */ | ||
905 | static int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity) | ||
906 | unzFile file; | ||
907 | const char *szFileName; | ||
908 | int iCaseSensitivity; | ||
909 | { | ||
910 | unz_s* s; | ||
911 | int err; | ||
912 | |||
913 | /* We remember the 'current' position in the file so that we can jump | ||
914 | * back there if we fail. | ||
915 | */ | ||
916 | unz_file_info cur_file_infoSaved; | ||
917 | unz_file_info_internal cur_file_info_internalSaved; | ||
918 | uLong num_fileSaved; | ||
919 | uLong pos_in_central_dirSaved; | ||
920 | |||
921 | |||
922 | if (file==NULL) | ||
923 | return UNZ_PARAMERROR; | ||
924 | |||
925 | if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP) | ||
926 | return UNZ_PARAMERROR; | ||
927 | |||
928 | s=(unz_s*)file; | ||
929 | if (!s->current_file_ok) | ||
930 | return UNZ_END_OF_LIST_OF_FILE; | ||
931 | |||
932 | /* Save the current state */ | ||
933 | num_fileSaved = s->num_file; | ||
934 | pos_in_central_dirSaved = s->pos_in_central_dir; | ||
935 | cur_file_infoSaved = s->cur_file_info; | ||
936 | cur_file_info_internalSaved = s->cur_file_info_internal; | ||
937 | |||
938 | err = unzGoToFirstFile(file); | ||
939 | |||
940 | while (err == UNZ_OK) | ||
941 | { | ||
942 | char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1]; | ||
943 | err = unzGetCurrentFileInfo(file,NULL, | ||
944 | szCurrentFileName,sizeof(szCurrentFileName)-1, | ||
945 | NULL,0,NULL,0); | ||
946 | if (err == UNZ_OK) | ||
947 | { | ||
948 | if (unzStringFileNameCompare(szCurrentFileName, | ||
949 | szFileName,iCaseSensitivity)==0) | ||
950 | return UNZ_OK; | ||
951 | err = unzGoToNextFile(file); | ||
952 | } | ||
953 | } | ||
954 | |||
955 | /* We failed, so restore the state of the 'current file' to where we | ||
956 | * were. | ||
957 | */ | ||
958 | s->num_file = num_fileSaved ; | ||
959 | s->pos_in_central_dir = pos_in_central_dirSaved ; | ||
960 | s->cur_file_info = cur_file_infoSaved; | ||
961 | s->cur_file_info_internal = cur_file_info_internalSaved; | ||
962 | return err; | ||
963 | } | ||
964 | |||
965 | |||
966 | /* | ||
967 | Read bytes from the current file. | ||
968 | buf contain buffer where data must be copied | ||
969 | len the size of buf. | ||
970 | |||
971 | return the number of byte copied if somes bytes are copied | ||
972 | return 0 if the end of file was reached | ||
973 | return <0 with error code if there is an error | ||
974 | (UNZ_ERRNO for IO error, or zLib error for uncompress error) | ||
975 | */ | ||
976 | static int ZEXPORT unzReadCurrentFile (file, buf, len) | ||
977 | unzFile file; | ||
978 | voidp buf; | ||
979 | unsigned len; | ||
980 | { | ||
981 | int err=UNZ_OK; | ||
982 | uInt iRead = 0; | ||
983 | unz_s* s; | ||
984 | file_in_zip_read_info_s* pfile_in_zip_read_info; | ||
985 | if (file==NULL) | ||
986 | return UNZ_PARAMERROR; | ||
987 | s=(unz_s*)file; | ||
988 | pfile_in_zip_read_info=s->pfile_in_zip_read; | ||
989 | |||
990 | if (pfile_in_zip_read_info==NULL) | ||
991 | return UNZ_PARAMERROR; | ||
992 | |||
993 | |||
994 | if ((pfile_in_zip_read_info->read_buffer == NULL)) | ||
995 | return UNZ_END_OF_LIST_OF_FILE; | ||
996 | if (len==0) | ||
997 | return 0; | ||
998 | |||
999 | pfile_in_zip_read_info->stream.next_out = (Bytef*)buf; | ||
1000 | |||
1001 | pfile_in_zip_read_info->stream.avail_out = (uInt)len; | ||
1002 | |||
1003 | if (len>pfile_in_zip_read_info->rest_read_uncompressed) | ||
1004 | pfile_in_zip_read_info->stream.avail_out = | ||
1005 | (uInt)pfile_in_zip_read_info->rest_read_uncompressed; | ||
1006 | |||
1007 | while (pfile_in_zip_read_info->stream.avail_out>0) | ||
1008 | { | ||
1009 | if ((pfile_in_zip_read_info->stream.avail_in==0) && | ||
1010 | (pfile_in_zip_read_info->rest_read_compressed>0)) | ||
1011 | { | ||
1012 | uInt uReadThis = UNZ_BUFSIZE; | ||
1013 | if (pfile_in_zip_read_info->rest_read_compressed<uReadThis) | ||
1014 | uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed; | ||
1015 | if (uReadThis == 0) | ||
1016 | return UNZ_EOF; | ||
1017 | if (ZSEEK(pfile_in_zip_read_info->z_filefunc, | ||
1018 | pfile_in_zip_read_info->filestream, | ||
1019 | pfile_in_zip_read_info->pos_in_zipfile + | ||
1020 | pfile_in_zip_read_info->byte_before_the_zipfile, | ||
1021 | ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
1022 | return UNZ_ERRNO; | ||
1023 | if (ZREAD(pfile_in_zip_read_info->z_filefunc, | ||
1024 | pfile_in_zip_read_info->filestream, | ||
1025 | pfile_in_zip_read_info->read_buffer, | ||
1026 | uReadThis)!=uReadThis) | ||
1027 | return UNZ_ERRNO; | ||
1028 | |||
1029 | |||
1030 | pfile_in_zip_read_info->pos_in_zipfile += uReadThis; | ||
1031 | |||
1032 | pfile_in_zip_read_info->rest_read_compressed-=uReadThis; | ||
1033 | |||
1034 | pfile_in_zip_read_info->stream.next_in = | ||
1035 | (Bytef*)pfile_in_zip_read_info->read_buffer; | ||
1036 | pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis; | ||
1037 | } | ||
1038 | |||
1039 | if ((pfile_in_zip_read_info->compression_method==0) || (pfile_in_zip_read_info->raw)) | ||
1040 | { | ||
1041 | uInt uDoCopy,i ; | ||
1042 | |||
1043 | if ((pfile_in_zip_read_info->stream.avail_in == 0) && | ||
1044 | (pfile_in_zip_read_info->rest_read_compressed == 0)) | ||
1045 | return (iRead==0) ? UNZ_EOF : iRead; | ||
1046 | |||
1047 | if (pfile_in_zip_read_info->stream.avail_out < | ||
1048 | pfile_in_zip_read_info->stream.avail_in) | ||
1049 | uDoCopy = pfile_in_zip_read_info->stream.avail_out ; | ||
1050 | else | ||
1051 | uDoCopy = pfile_in_zip_read_info->stream.avail_in ; | ||
1052 | |||
1053 | for (i=0;i<uDoCopy;i++) | ||
1054 | *(pfile_in_zip_read_info->stream.next_out+i) = | ||
1055 | *(pfile_in_zip_read_info->stream.next_in+i); | ||
1056 | |||
1057 | pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, | ||
1058 | pfile_in_zip_read_info->stream.next_out, | ||
1059 | uDoCopy); | ||
1060 | pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy; | ||
1061 | pfile_in_zip_read_info->stream.avail_in -= uDoCopy; | ||
1062 | pfile_in_zip_read_info->stream.avail_out -= uDoCopy; | ||
1063 | pfile_in_zip_read_info->stream.next_out += uDoCopy; | ||
1064 | pfile_in_zip_read_info->stream.next_in += uDoCopy; | ||
1065 | pfile_in_zip_read_info->stream.total_out += uDoCopy; | ||
1066 | iRead += uDoCopy; | ||
1067 | } | ||
1068 | else | ||
1069 | { | ||
1070 | uLong uTotalOutBefore,uTotalOutAfter; | ||
1071 | const Bytef *bufBefore; | ||
1072 | uLong uOutThis; | ||
1073 | int flush=Z_SYNC_FLUSH; | ||
1074 | |||
1075 | uTotalOutBefore = pfile_in_zip_read_info->stream.total_out; | ||
1076 | bufBefore = pfile_in_zip_read_info->stream.next_out; | ||
1077 | |||
1078 | /* | ||
1079 | if ((pfile_in_zip_read_info->rest_read_uncompressed == | ||
1080 | pfile_in_zip_read_info->stream.avail_out) && | ||
1081 | (pfile_in_zip_read_info->rest_read_compressed == 0)) | ||
1082 | flush = Z_FINISH; | ||
1083 | */ | ||
1084 | err=inflate(&pfile_in_zip_read_info->stream,flush); | ||
1085 | |||
1086 | uTotalOutAfter = pfile_in_zip_read_info->stream.total_out; | ||
1087 | uOutThis = uTotalOutAfter-uTotalOutBefore; | ||
1088 | |||
1089 | pfile_in_zip_read_info->crc32 = | ||
1090 | crc32(pfile_in_zip_read_info->crc32,bufBefore, | ||
1091 | (uInt)(uOutThis)); | ||
1092 | |||
1093 | pfile_in_zip_read_info->rest_read_uncompressed -= | ||
1094 | uOutThis; | ||
1095 | |||
1096 | iRead += (uInt)(uTotalOutAfter - uTotalOutBefore); | ||
1097 | |||
1098 | if (err==Z_STREAM_END) | ||
1099 | return (iRead==0) ? UNZ_EOF : iRead; | ||
1100 | if (err!=Z_OK) | ||
1101 | break; | ||
1102 | } | ||
1103 | } | ||
1104 | |||
1105 | if (err==Z_OK) | ||
1106 | return iRead; | ||
1107 | return err; | ||
1108 | } | ||
1109 | |||
1110 | /* | ||
1111 | Read the local header of the current zipfile | ||
1112 | Check the coherency of the local header and info in the end of central | ||
1113 | directory about this file | ||
1114 | store in *piSizeVar the size of extra info in local header | ||
1115 | (filename and size of extra field data) | ||
1116 | */ | ||
1117 | static int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar, | ||
1118 | poffset_local_extrafield, | ||
1119 | psize_local_extrafield) | ||
1120 | unz_s* s; | ||
1121 | uInt* piSizeVar; | ||
1122 | uLong *poffset_local_extrafield; | ||
1123 | uInt *psize_local_extrafield; | ||
1124 | { | ||
1125 | uLong uMagic,uData,uFlags; | ||
1126 | uLong size_filename; | ||
1127 | uLong size_extra_field; | ||
1128 | int err=UNZ_OK; | ||
1129 | |||
1130 | *piSizeVar = 0; | ||
1131 | *poffset_local_extrafield = 0; | ||
1132 | *psize_local_extrafield = 0; | ||
1133 | |||
1134 | if (ZSEEK(s->z_filefunc, s->filestream,s->cur_file_info_internal.offset_curfile + | ||
1135 | s->byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
1136 | return UNZ_ERRNO; | ||
1137 | |||
1138 | |||
1139 | if (err==UNZ_OK) { | ||
1140 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK) | ||
1141 | err=UNZ_ERRNO; | ||
1142 | else if (uMagic!=0x04034b50) | ||
1143 | err=UNZ_BADZIPFILE; | ||
1144 | } | ||
1145 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) | ||
1146 | err=UNZ_ERRNO; | ||
1147 | /* | ||
1148 | else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion)) | ||
1149 | err=UNZ_BADZIPFILE; | ||
1150 | */ | ||
1151 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uFlags) != UNZ_OK) | ||
1152 | err=UNZ_ERRNO; | ||
1153 | |||
1154 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) | ||
1155 | err=UNZ_ERRNO; | ||
1156 | else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method)) | ||
1157 | err=UNZ_BADZIPFILE; | ||
1158 | |||
1159 | if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) && | ||
1160 | (s->cur_file_info.compression_method!=Z_DEFLATED)) | ||
1161 | err=UNZ_BADZIPFILE; | ||
1162 | |||
1163 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* date/time */ | ||
1164 | err=UNZ_ERRNO; | ||
1165 | |||
1166 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* crc */ | ||
1167 | err=UNZ_ERRNO; | ||
1168 | else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) && | ||
1169 | ((uFlags & 8)==0)) | ||
1170 | err=UNZ_BADZIPFILE; | ||
1171 | |||
1172 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size compr */ | ||
1173 | err=UNZ_ERRNO; | ||
1174 | else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) && | ||
1175 | ((uFlags & 8)==0)) | ||
1176 | err=UNZ_BADZIPFILE; | ||
1177 | |||
1178 | if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size uncompr */ | ||
1179 | err=UNZ_ERRNO; | ||
1180 | else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && | ||
1181 | ((uFlags & 8)==0)) | ||
1182 | err=UNZ_BADZIPFILE; | ||
1183 | |||
1184 | |||
1185 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&size_filename) != UNZ_OK) | ||
1186 | err=UNZ_ERRNO; | ||
1187 | else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename)) | ||
1188 | err=UNZ_BADZIPFILE; | ||
1189 | |||
1190 | *piSizeVar += (uInt)size_filename; | ||
1191 | |||
1192 | if (unzlocal_getShort(&s->z_filefunc, s->filestream,&size_extra_field) != UNZ_OK) | ||
1193 | err=UNZ_ERRNO; | ||
1194 | *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile + | ||
1195 | SIZEZIPLOCALHEADER + size_filename; | ||
1196 | *psize_local_extrafield = (uInt)size_extra_field; | ||
1197 | |||
1198 | *piSizeVar += (uInt)size_extra_field; | ||
1199 | |||
1200 | return err; | ||
1201 | } | ||
1202 | |||
1203 | |||
1204 | |||
1205 | /* | ||
1206 | Open for reading data the current file in the zipfile. | ||
1207 | If there is no error and the file is opened, the return value is UNZ_OK. | ||
1208 | */ | ||
1209 | static int ZEXPORT unzOpenCurrentFile3 (file, method, level, raw) | ||
1210 | unzFile file; | ||
1211 | int* method; | ||
1212 | int* level; | ||
1213 | int raw; | ||
1214 | { | ||
1215 | int err=UNZ_OK; | ||
1216 | uInt iSizeVar; | ||
1217 | unz_s* s; | ||
1218 | file_in_zip_read_info_s* pfile_in_zip_read_info; | ||
1219 | uLong offset_local_extrafield; /* offset of the local extra field */ | ||
1220 | uInt size_local_extrafield; /* size of the local extra field */ | ||
1221 | |||
1222 | if (file==NULL) | ||
1223 | return UNZ_PARAMERROR; | ||
1224 | s=(unz_s*)file; | ||
1225 | if (!s->current_file_ok) | ||
1226 | return UNZ_PARAMERROR; | ||
1227 | |||
1228 | if (s->pfile_in_zip_read != NULL) | ||
1229 | unzCloseCurrentFile(file); | ||
1230 | if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar, | ||
1231 | &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK) | ||
1232 | return UNZ_BADZIPFILE; | ||
1233 | |||
1234 | pfile_in_zip_read_info = (file_in_zip_read_info_s*) | ||
1235 | ALLOC(sizeof(file_in_zip_read_info_s)); | ||
1236 | if (pfile_in_zip_read_info==NULL) | ||
1237 | return UNZ_INTERNALERROR; | ||
1238 | |||
1239 | pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE); | ||
1240 | pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield; | ||
1241 | pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield; | ||
1242 | pfile_in_zip_read_info->pos_local_extrafield=0; | ||
1243 | pfile_in_zip_read_info->raw=raw; | ||
1244 | |||
1245 | if (pfile_in_zip_read_info->read_buffer==NULL) | ||
1246 | { | ||
1247 | TRYFREE(pfile_in_zip_read_info); | ||
1248 | return UNZ_INTERNALERROR; | ||
1249 | } | ||
1250 | |||
1251 | pfile_in_zip_read_info->stream_initialised=0; | ||
1252 | |||
1253 | if (method!=NULL) | ||
1254 | *method = (int)s->cur_file_info.compression_method; | ||
1255 | |||
1256 | if (level!=NULL) | ||
1257 | { | ||
1258 | *level = 6; | ||
1259 | switch (s->cur_file_info.flag & 0x06) | ||
1260 | { | ||
1261 | case 6 : *level = 1; break; | ||
1262 | case 4 : *level = 2; break; | ||
1263 | case 2 : *level = 9; break; | ||
1264 | } | ||
1265 | } | ||
1266 | |||
1267 | if ((s->cur_file_info.compression_method!=0) && | ||
1268 | (s->cur_file_info.compression_method!=Z_DEFLATED)) | ||
1269 | err=UNZ_BADZIPFILE; | ||
1270 | |||
1271 | pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc; | ||
1272 | pfile_in_zip_read_info->crc32=0; | ||
1273 | pfile_in_zip_read_info->compression_method = | ||
1274 | s->cur_file_info.compression_method; | ||
1275 | pfile_in_zip_read_info->filestream=s->filestream; | ||
1276 | pfile_in_zip_read_info->z_filefunc=s->z_filefunc; | ||
1277 | pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile; | ||
1278 | |||
1279 | pfile_in_zip_read_info->stream.total_out = 0; | ||
1280 | |||
1281 | if ((s->cur_file_info.compression_method==Z_DEFLATED) && | ||
1282 | (!raw)) | ||
1283 | { | ||
1284 | pfile_in_zip_read_info->stream.zalloc = (alloc_func)0; | ||
1285 | pfile_in_zip_read_info->stream.zfree = (free_func)0; | ||
1286 | pfile_in_zip_read_info->stream.opaque = (voidpf)0; | ||
1287 | pfile_in_zip_read_info->stream.next_in = (voidpf)0; | ||
1288 | pfile_in_zip_read_info->stream.avail_in = 0; | ||
1289 | |||
1290 | err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS); | ||
1291 | if (err == Z_OK) | ||
1292 | pfile_in_zip_read_info->stream_initialised=1; | ||
1293 | else | ||
1294 | return err; | ||
1295 | /* windowBits is passed < 0 to tell that there is no zlib header. | ||
1296 | * Note that in this case inflate *requires* an extra "dummy" byte | ||
1297 | * after the compressed stream in order to complete decompression and | ||
1298 | * return Z_STREAM_END. | ||
1299 | * In unzip, i don't wait absolutely Z_STREAM_END because I known the | ||
1300 | * size of both compressed and uncompressed data | ||
1301 | */ | ||
1302 | } | ||
1303 | pfile_in_zip_read_info->rest_read_compressed = | ||
1304 | s->cur_file_info.compressed_size ; | ||
1305 | pfile_in_zip_read_info->rest_read_uncompressed = | ||
1306 | s->cur_file_info.uncompressed_size ; | ||
1307 | |||
1308 | |||
1309 | pfile_in_zip_read_info->pos_in_zipfile = | ||
1310 | s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + | ||
1311 | iSizeVar; | ||
1312 | |||
1313 | pfile_in_zip_read_info->stream.avail_in = (uInt)0; | ||
1314 | |||
1315 | s->pfile_in_zip_read = pfile_in_zip_read_info; | ||
1316 | |||
1317 | return UNZ_OK; | ||
1318 | } | ||
1319 | |||
1320 | |||
1321 | /* *************************** main extractor code *********************** */ | ||
1322 | |||
1323 | 30 | ||
1324 | /** | 31 | /** |
1325 | * Name of the file with the meta-data in OO documents. | 32 | * Name of the file with the meta-data in OO documents. |
@@ -1369,16 +76,16 @@ static Matches tmap[] = { | |||
1369 | * returns either zero when mimetype info is missing | 76 | * returns either zero when mimetype info is missing |
1370 | * or an already malloc'ed string containing the mimetype info. | 77 | * or an already malloc'ed string containing the mimetype info. |
1371 | */ | 78 | */ |
1372 | static char *libextractor_oo_getmimetype(unzFile uf) { | 79 | static char *libextractor_oo_getmimetype(EXTRACTOR_unzip_file uf) { |
1373 | char filename_inzip[MAXFILENAME]; | 80 | char filename_inzip[MAXFILENAME]; |
1374 | unz_file_info file_info; | 81 | EXTRACTOR_unzip_file_info file_info; |
1375 | char * buf = NULL; | 82 | char * buf = NULL; |
1376 | size_t buf_size = 0; | 83 | size_t buf_size = 0; |
1377 | 84 | ||
1378 | if (UNZ_OK == unzLocateFile(uf, | 85 | if (EXTRACTOR_UNZIP_OK == EXTRACTOR_common_unzip_local_file(uf, |
1379 | "mimetype", | 86 | "mimetype", |
1380 | CASESENSITIVITY)) { | 87 | CASESENSITIVITY)) { |
1381 | if ( (UNZ_OK == unzGetCurrentFileInfo(uf, | 88 | if ( (EXTRACTOR_UNZIP_OK == EXTRACTOR_common_unzip_get_current_file_info(uf, |
1382 | &file_info, | 89 | &file_info, |
1383 | filename_inzip, | 90 | filename_inzip, |
1384 | sizeof(filename_inzip), | 91 | sizeof(filename_inzip), |
@@ -1386,14 +93,14 @@ static char *libextractor_oo_getmimetype(unzFile uf) { | |||
1386 | 0, | 93 | 0, |
1387 | NULL, | 94 | NULL, |
1388 | 0) && | 95 | 0) && |
1389 | (UNZ_OK == unzOpenCurrentFile3(uf,NULL, NULL, 0)) ) ) { | 96 | (EXTRACTOR_UNZIP_OK == EXTRACTOR_common_unzip_open_current_file3(uf,NULL, NULL, 0)) ) ) { |
1390 | buf_size = file_info.uncompressed_size; | 97 | buf_size = file_info.uncompressed_size; |
1391 | 98 | ||
1392 | if (buf_size > 1024) { | 99 | if (buf_size > 1024) { |
1393 | /* way too large! */ | 100 | /* way too large! */ |
1394 | } else if (NULL == (buf = malloc(1 + buf_size))) { | 101 | } else if (NULL == (buf = malloc(1 + buf_size))) { |
1395 | /* memory exhausted! */ | 102 | /* memory exhausted! */ |
1396 | } else if (buf_size != (size_t) unzReadCurrentFile(uf,buf,buf_size)) { | 103 | } else if (buf_size != (size_t) EXTRACTOR_common_unzip_read_current_file(uf,buf,buf_size)) { |
1397 | free(buf); | 104 | free(buf); |
1398 | buf = NULL; | 105 | buf = NULL; |
1399 | } else { | 106 | } else { |
@@ -1408,7 +115,7 @@ static char *libextractor_oo_getmimetype(unzFile uf) { | |||
1408 | } | 115 | } |
1409 | } | 116 | } |
1410 | } | 117 | } |
1411 | unzCloseCurrentFile(uf); | 118 | EXTRACTOR_common_unzip_close_current_file(uf); |
1412 | } | 119 | } |
1413 | return buf; | 120 | return buf; |
1414 | } | 121 | } |
@@ -1420,80 +127,6 @@ typedef struct Ecls { | |||
1420 | size_t pos; | 127 | size_t pos; |
1421 | } Ecls; | 128 | } Ecls; |
1422 | 129 | ||
1423 | static voidpf Eopen_file_func (voidpf opaque, | ||
1424 | const char* filename, | ||
1425 | int mode) { | ||
1426 | if (0 == strcmp(filename, | ||
1427 | "ERROR")) | ||
1428 | return opaque; | ||
1429 | else | ||
1430 | return NULL; | ||
1431 | } | ||
1432 | |||
1433 | static uLong Eread_file_func(voidpf opaque, | ||
1434 | voidpf stream, | ||
1435 | void* buf, | ||
1436 | uLong size) { | ||
1437 | Ecls * e = opaque; | ||
1438 | uLong ret; | ||
1439 | |||
1440 | ret = e->size - e->pos; | ||
1441 | if (ret > size) | ||
1442 | ret = size; | ||
1443 | memcpy(buf, | ||
1444 | &e->data[e->pos], | ||
1445 | ret); | ||
1446 | e->pos += ret; | ||
1447 | return ret; | ||
1448 | } | ||
1449 | |||
1450 | static long Etell_file_func(voidpf opaque, | ||
1451 | voidpf stream) { | ||
1452 | Ecls * e = opaque; | ||
1453 | return e->pos; | ||
1454 | } | ||
1455 | |||
1456 | static long Eseek_file_func(voidpf opaque, | ||
1457 | voidpf stream, | ||
1458 | uLong offset, | ||
1459 | int origin) { | ||
1460 | Ecls * e = opaque; | ||
1461 | |||
1462 | switch (origin) { | ||
1463 | case ZLIB_FILEFUNC_SEEK_SET: | ||
1464 | if ( (offset > e->size) || | ||
1465 | (offset < 0) ) | ||
1466 | return -1; | ||
1467 | e->pos = offset; | ||
1468 | break; | ||
1469 | case ZLIB_FILEFUNC_SEEK_END: | ||
1470 | if ( (offset > e->size) || | ||
1471 | (offset < 0) ) | ||
1472 | return -1; | ||
1473 | e->pos = e->size - offset; | ||
1474 | break; | ||
1475 | case ZLIB_FILEFUNC_SEEK_CUR: | ||
1476 | if ( (offset < - e->pos) || | ||
1477 | (offset > e->size - e->pos) ) | ||
1478 | return -1; | ||
1479 | e->pos += offset; | ||
1480 | break; | ||
1481 | default: | ||
1482 | return -1; | ||
1483 | } | ||
1484 | return 0; | ||
1485 | } | ||
1486 | |||
1487 | static int Eclose_file_func(voidpf opaque, | ||
1488 | voidpf stream) { | ||
1489 | return 0; | ||
1490 | } | ||
1491 | |||
1492 | static int Etesterror_file_func(voidpf opaque, | ||
1493 | voidpf stream) { | ||
1494 | return 0; | ||
1495 | } | ||
1496 | |||
1497 | 130 | ||
1498 | struct EXTRACTOR_Keywords * | 131 | struct EXTRACTOR_Keywords * |
1499 | libextractor_oo_extract(const char * filename, | 132 | libextractor_oo_extract(const char * filename, |
@@ -1501,13 +134,13 @@ libextractor_oo_extract(const char * filename, | |||
1501 | size_t size, | 134 | size_t size, |
1502 | struct EXTRACTOR_Keywords * prev) { | 135 | struct EXTRACTOR_Keywords * prev) { |
1503 | char filename_inzip[MAXFILENAME]; | 136 | char filename_inzip[MAXFILENAME]; |
1504 | unzFile uf; | 137 | EXTRACTOR_unzip_file uf; |
1505 | unz_file_info file_info; | 138 | EXTRACTOR_unzip_file_info file_info; |
1506 | char * buf; | 139 | char * buf; |
1507 | char * pbuf; | 140 | char * pbuf; |
1508 | size_t buf_size; | 141 | size_t buf_size; |
1509 | int i; | 142 | int i; |
1510 | zlib_filefunc_def io; | 143 | EXTRACTOR_unzip_filefunc_def io; |
1511 | Ecls cls; | 144 | Ecls cls; |
1512 | char * mimetype; | 145 | char * mimetype; |
1513 | 146 | ||
@@ -1519,16 +152,16 @@ libextractor_oo_extract(const char * filename, | |||
1519 | cls.data = data; | 152 | cls.data = data; |
1520 | cls.size = size; | 153 | cls.size = size; |
1521 | cls.pos = 0; | 154 | cls.pos = 0; |
1522 | io.zopen_file = &Eopen_file_func; | 155 | io.zopen_file = &EXTRACTOR_common_unzip_zlib_open_file_func; |
1523 | io.zread_file = &Eread_file_func; | 156 | io.zread_file = &EXTRACTOR_common_unzip_zlib_read_file_func; |
1524 | io.zwrite_file = NULL; | 157 | io.zwrite_file = NULL; |
1525 | io.ztell_file = &Etell_file_func; | 158 | io.ztell_file = &EXTRACTOR_common_unzip_zlib_tell_file_func; |
1526 | io.zseek_file = &Eseek_file_func; | 159 | io.zseek_file = &EXTRACTOR_common_unzip_zlib_seek_file_func; |
1527 | io.zclose_file = &Eclose_file_func; | 160 | io.zclose_file = &EXTRACTOR_common_unzip_zlib_close_file_func; |
1528 | io.zerror_file = &Etesterror_file_func; | 161 | io.zerror_file = &EXTRACTOR_common_unzip_zlib_testerror_file_func; |
1529 | io.opaque = &cls; | 162 | io.opaque = &cls; |
1530 | 163 | ||
1531 | uf = unzOpen2("ERROR", &io); | 164 | uf = EXTRACTOR_common_unzip_open2("ERROR", &io); |
1532 | if (uf == NULL) | 165 | if (uf == NULL) |
1533 | return prev; | 166 | return prev; |
1534 | mimetype = libextractor_oo_getmimetype(uf); | 167 | mimetype = libextractor_oo_getmimetype(uf); |
@@ -1539,47 +172,47 @@ libextractor_oo_extract(const char * filename, | |||
1539 | EXTRACTOR_MIMETYPE)); | 172 | EXTRACTOR_MIMETYPE)); |
1540 | 173 | ||
1541 | 174 | ||
1542 | if (unzLocateFile(uf, | 175 | if (EXTRACTOR_common_unzip_local_file(uf, |
1543 | METAFILE, | 176 | METAFILE, |
1544 | CASESENSITIVITY) != UNZ_OK) { | 177 | CASESENSITIVITY) != EXTRACTOR_UNZIP_OK) { |
1545 | unzClose(uf); | 178 | EXTRACTOR_common_unzip_close(uf); |
1546 | return prev; /* not found */ | 179 | return prev; /* not found */ |
1547 | } | 180 | } |
1548 | 181 | ||
1549 | if (UNZ_OK != unzGetCurrentFileInfo(uf, | 182 | if (EXTRACTOR_UNZIP_OK != EXTRACTOR_common_unzip_get_current_file_info(uf, |
1550 | &file_info, | 183 | &file_info, |
1551 | filename_inzip, | 184 | filename_inzip, |
1552 | sizeof(filename_inzip), | 185 | sizeof(filename_inzip), |
1553 | NULL,0,NULL,0)) { | 186 | NULL,0,NULL,0)) { |
1554 | unzClose(uf); | 187 | EXTRACTOR_common_unzip_close(uf); |
1555 | return prev; /* problems... */ | 188 | return prev; /* problems... */ |
1556 | } | 189 | } |
1557 | 190 | ||
1558 | if (UNZ_OK != unzOpenCurrentFile3(uf, NULL, NULL, 0)) { | 191 | if (EXTRACTOR_UNZIP_OK != EXTRACTOR_common_unzip_open_current_file3(uf, NULL, NULL, 0)) { |
1559 | unzClose(uf); | 192 | EXTRACTOR_common_unzip_close(uf); |
1560 | return prev; /* problems... */ | 193 | return prev; /* problems... */ |
1561 | } | 194 | } |
1562 | 195 | ||
1563 | buf_size = file_info.uncompressed_size; | 196 | buf_size = file_info.uncompressed_size; |
1564 | if (buf_size > 128 * 1024) { | 197 | if (buf_size > 128 * 1024) { |
1565 | unzCloseCurrentFile(uf); | 198 | EXTRACTOR_common_unzip_close_current_file(uf); |
1566 | unzClose(uf); | 199 | EXTRACTOR_common_unzip_close(uf); |
1567 | return prev; /* hardly meta-data! */ | 200 | return prev; /* hardly meta-data! */ |
1568 | } | 201 | } |
1569 | buf = malloc(buf_size+1); | 202 | buf = malloc(buf_size+1); |
1570 | if (buf == NULL) { | 203 | if (buf == NULL) { |
1571 | unzCloseCurrentFile(uf); | 204 | EXTRACTOR_common_unzip_close_current_file(uf); |
1572 | unzClose(uf); | 205 | EXTRACTOR_common_unzip_close(uf); |
1573 | return prev; /* out of memory */ | 206 | return prev; /* out of memory */ |
1574 | } | 207 | } |
1575 | 208 | ||
1576 | if (buf_size != unzReadCurrentFile(uf,buf,buf_size)) { | 209 | if (buf_size != EXTRACTOR_common_unzip_read_current_file(uf,buf,buf_size)) { |
1577 | free(buf); | 210 | free(buf); |
1578 | unzCloseCurrentFile(uf); | 211 | EXTRACTOR_common_unzip_close_current_file(uf); |
1579 | unzClose(uf); | 212 | EXTRACTOR_common_unzip_close(uf); |
1580 | return prev; | 213 | return prev; |
1581 | } | 214 | } |
1582 | unzCloseCurrentFile(uf); | 215 | EXTRACTOR_common_unzip_close_current_file(uf); |
1583 | /* we don't do "proper" parsing of the meta-data but rather use some heuristics | 216 | /* we don't do "proper" parsing of the meta-data but rather use some heuristics |
1584 | to get values out that we understand */ | 217 | to get values out that we understand */ |
1585 | buf[buf_size] = '\0'; | 218 | buf[buf_size] = '\0'; |
@@ -1609,7 +242,7 @@ libextractor_oo_extract(const char * filename, | |||
1609 | strcat(needle, "=\""); | 242 | strcat(needle, "=\""); |
1610 | spos = strstr(pbuf, needle); | 243 | spos = strstr(pbuf, needle); |
1611 | if (spos == NULL) | 244 | if (spos == NULL) |
1612 | break; | 245 | break; |
1613 | spos += strlen(needle); | 246 | spos += strlen(needle); |
1614 | epos = spos; | 247 | epos = spos; |
1615 | while ( (epos[0] != '\0') && | 248 | while ( (epos[0] != '\0') && |
@@ -1648,7 +281,7 @@ libextractor_oo_extract(const char * filename, | |||
1648 | } | 281 | } |
1649 | } | 282 | } |
1650 | free(buf); | 283 | free(buf); |
1651 | unzClose(uf); | 284 | EXTRACTOR_common_unzip_close(uf); |
1652 | return prev; | 285 | return prev; |
1653 | } | 286 | } |
1654 | 287 | ||