author | Ryan C. Gordon <icculus@icculus.org> |
Mon, 01 Apr 2002 18:43:59 +0000 | |
changeset 148 | 5b95e3fa52d3 |
parent 143 | 337505f94c10 |
child 176 | 4a996749e39d |
permissions | -rw-r--r-- |
47 | 1 |
/* unzip.c -- IO on .zip files using zlib |
2 |
Version 0.15 beta, Mar 19th, 1998, |
|
3 |
||
4 |
Read unzip.h for more info |
|
5 |
*/ |
|
6 |
||
7 |
||
8 |
#include <stdio.h> |
|
9 |
#include <stdlib.h> |
|
10 |
#include <string.h> |
|
11 |
#include "zlib.h" |
|
12 |
#include "unzip.h" |
|
13 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
14 |
#define __PHYSICSFS_INTERNAL__ |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
15 |
#include "physfs_internal.h" |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
16 |
|
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
17 |
#if (!defined PHYSFS_SUPPORTS_ZIP) |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
18 |
#error PHYSFS_SUPPORTS_ZIP must be defined. |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
19 |
#endif |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
20 |
|
47 | 21 |
#ifdef STDC |
22 |
# include <stddef.h> |
|
23 |
# include <string.h> |
|
24 |
# include <stdlib.h> |
|
25 |
#endif |
|
26 |
#ifdef NO_ERRNO_H |
|
27 |
extern int errno; |
|
28 |
#else |
|
29 |
# include <errno.h> |
|
30 |
#endif |
|
31 |
||
32 |
||
33 |
#ifndef local |
|
34 |
# define local static |
|
35 |
#endif |
|
36 |
/* compile with -Dlocal if your debugger can't find static symbols */ |
|
37 |
||
38 |
||
39 |
||
40 |
#if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \ |
|
41 |
!defined(CASESENSITIVITYDEFAULT_NO) |
|
42 |
#define CASESENSITIVITYDEFAULT_NO |
|
43 |
#endif |
|
44 |
||
45 |
||
46 |
#ifndef UNZ_BUFSIZE |
|
47 |
#define UNZ_BUFSIZE (16384) |
|
48 |
#endif |
|
49 |
||
50 |
#ifndef UNZ_MAXFILENAMEINZIP |
|
51 |
#define UNZ_MAXFILENAMEINZIP (256) |
|
52 |
#endif |
|
53 |
||
54 |
#ifndef ALLOC |
|
55 |
# define ALLOC(size) (malloc(size)) |
|
56 |
#endif |
|
57 |
#ifndef TRYFREE |
|
58 |
# define TRYFREE(p) {if (p) free(p);} |
|
59 |
#endif |
|
60 |
||
61 |
#define SIZECENTRALDIRITEM (0x2e) |
|
62 |
#define SIZEZIPLOCALHEADER (0x1e) |
|
63 |
||
64 |
||
65 |
/* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ |
|
66 |
||
67 |
#ifndef SEEK_CUR |
|
68 |
#define SEEK_CUR 1 |
|
69 |
#endif |
|
70 |
||
71 |
#ifndef SEEK_END |
|
72 |
#define SEEK_END 2 |
|
73 |
#endif |
|
74 |
||
75 |
#ifndef SEEK_SET |
|
76 |
#define SEEK_SET 0 |
|
77 |
#endif |
|
78 |
||
79 |
const char unz_copyright[] = |
|
80 |
" unzip 0.15 Copyright 1998 Gilles Vollant "; |
|
81 |
||
82 |
/* unz_file_info_interntal contain internal info about a file in zipfile*/ |
|
83 |
typedef struct unz_file_info_internal_s |
|
84 |
{ |
|
85 |
uLong offset_curfile;/* relative offset of local header 4 bytes */ |
|
86 |
} unz_file_info_internal; |
|
87 |
||
88 |
||
89 |
/* file_in_zip_read_info_s contain internal information about a file in zipfile, |
|
90 |
when reading and decompress it */ |
|
91 |
typedef struct |
|
92 |
{ |
|
93 |
char *read_buffer; /* internal buffer for compressed data */ |
|
94 |
z_stream stream; /* zLib stream structure for inflate */ |
|
95 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
96 |
uLong pos_in_zipfile; /* position in byte on the zipfile, for seek*/ |
47 | 97 |
uLong stream_initialised; /* flag set if stream structure is initialised*/ |
98 |
||
99 |
uLong offset_local_extrafield;/* offset of the local extra field */ |
|
100 |
uInt size_local_extrafield;/* size of the local extra field */ |
|
101 |
uLong pos_local_extrafield; /* position in the local extra field in read*/ |
|
102 |
||
103 |
uLong crc32; /* crc32 of all data uncompressed */ |
|
104 |
uLong crc32_wait; /* crc32 we must obtain after decompress all */ |
|
105 |
uLong rest_read_compressed; /* number of byte to be decompressed */ |
|
106 |
uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/ |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
107 |
void* file; /* io structore of the zipfile */ |
47 | 108 |
uLong compression_method; /* compression method (0==store) */ |
109 |
uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ |
|
110 |
} file_in_zip_read_info_s; |
|
111 |
||
112 |
||
113 |
/* unz_s contain internal information about the zipfile |
|
114 |
*/ |
|
115 |
typedef struct |
|
116 |
{ |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
117 |
void* file; /* io structore of the zipfile */ |
47 | 118 |
unz_global_info gi; /* public global information */ |
119 |
uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ |
|
120 |
uLong num_file; /* number of the current file in the zipfile*/ |
|
121 |
uLong pos_in_central_dir; /* pos of the current file in the central dir*/ |
|
122 |
uLong current_file_ok; /* flag about the usability of the current file*/ |
|
123 |
uLong central_pos; /* position of the beginning of the central dir*/ |
|
124 |
||
125 |
uLong size_central_dir; /* size of the central directory */ |
|
126 |
uLong offset_central_dir; /* offset of start of central directory with |
|
127 |
respect to the starting disk number */ |
|
128 |
||
129 |
unz_file_info cur_file_info; /* public info about the current file in zip*/ |
|
130 |
unz_file_info_internal cur_file_info_internal; /* private info about it*/ |
|
131 |
file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current |
|
132 |
file if we are decompressing it */ |
|
133 |
} unz_s; |
|
134 |
||
135 |
||
136 |
/* =========================================================================== |
|
137 |
Read a byte from a gz_stream; update next_in and avail_in. Return EOF |
|
138 |
for end of file. |
|
139 |
IN assertion: the stream s has been sucessfully opened for reading. |
|
140 |
*/ |
|
141 |
||
142 |
||
143 |
local int unzlocal_getByte(fin,pi) |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
144 |
void *fin; |
47 | 145 |
int *pi; |
146 |
{ |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
147 |
PHYSFS_uint8 c; |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
148 |
PHYSFS_sint64 err = __PHYSFS_platformRead(fin, &c, 1, 1); |
47 | 149 |
if (err==1) |
150 |
{ |
|
151 |
*pi = (int)c; |
|
152 |
return UNZ_OK; |
|
153 |
} |
|
154 |
else |
|
155 |
{ |
|
148
5b95e3fa52d3
Missed an ferror(); fixed to use platform abstraction instead.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
156 |
if (__PHYSFS_platformEOF(fin)) |
5b95e3fa52d3
Missed an ferror(); fixed to use platform abstraction instead.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
157 |
return UNZ_EOF; |
5b95e3fa52d3
Missed an ferror(); fixed to use platform abstraction instead.
Ryan C. Gordon <icculus@icculus.org>
parents:
143
diff
changeset
|
158 |
else |
47 | 159 |
return UNZ_ERRNO; |
160 |
} |
|
161 |
} |
|
162 |
||
163 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
164 |
/* !!! FIXME: Use PhysFS byteswap routines. */ |
47 | 165 |
/* =========================================================================== |
166 |
Reads a long in LSB order from the given gz_stream. Sets |
|
167 |
*/ |
|
168 |
local int unzlocal_getShort (fin,pX) |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
169 |
void* fin; |
47 | 170 |
uLong *pX; |
171 |
{ |
|
172 |
uLong x ; |
|
173 |
int i; |
|
174 |
int err; |
|
175 |
||
176 |
err = unzlocal_getByte(fin,&i); |
|
177 |
x = (uLong)i; |
|
178 |
||
179 |
if (err==UNZ_OK) |
|
180 |
err = unzlocal_getByte(fin,&i); |
|
181 |
x += ((uLong)i)<<8; |
|
182 |
||
183 |
if (err==UNZ_OK) |
|
184 |
*pX = x; |
|
185 |
else |
|
186 |
*pX = 0; |
|
187 |
return err; |
|
188 |
} |
|
189 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
190 |
/* !!! FIXME: Use PhysFS byteswap routines. */ |
47 | 191 |
local int unzlocal_getLong (fin,pX) |
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
192 |
void* fin; |
47 | 193 |
uLong *pX; |
194 |
{ |
|
195 |
uLong x ; |
|
196 |
int i; |
|
197 |
int err; |
|
198 |
||
199 |
err = unzlocal_getByte(fin,&i); |
|
200 |
x = (uLong)i; |
|
201 |
||
202 |
if (err==UNZ_OK) |
|
203 |
err = unzlocal_getByte(fin,&i); |
|
204 |
x += ((uLong)i)<<8; |
|
205 |
||
206 |
if (err==UNZ_OK) |
|
207 |
err = unzlocal_getByte(fin,&i); |
|
208 |
x += ((uLong)i)<<16; |
|
209 |
||
210 |
if (err==UNZ_OK) |
|
211 |
err = unzlocal_getByte(fin,&i); |
|
212 |
x += ((uLong)i)<<24; |
|
213 |
||
214 |
if (err==UNZ_OK) |
|
215 |
*pX = x; |
|
216 |
else |
|
217 |
*pX = 0; |
|
218 |
return err; |
|
219 |
} |
|
220 |
||
221 |
||
222 |
/* My own strcmpi / strcasecmp */ |
|
223 |
local int strcmpcasenosensitive_internal (fileName1,fileName2) |
|
224 |
const char* fileName1; |
|
225 |
const char* fileName2; |
|
226 |
{ |
|
227 |
for (;;) |
|
228 |
{ |
|
229 |
char c1=*(fileName1++); |
|
230 |
char c2=*(fileName2++); |
|
231 |
if ((c1>='a') && (c1<='z')) |
|
232 |
c1 -= 0x20; |
|
233 |
if ((c2>='a') && (c2<='z')) |
|
234 |
c2 -= 0x20; |
|
235 |
if (c1=='\0') |
|
236 |
return ((c2=='\0') ? 0 : -1); |
|
237 |
if (c2=='\0') |
|
238 |
return 1; |
|
239 |
if (c1<c2) |
|
240 |
return -1; |
|
241 |
if (c1>c2) |
|
242 |
return 1; |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
||
247 |
#ifdef CASESENSITIVITYDEFAULT_NO |
|
248 |
#define CASESENSITIVITYDEFAULTVALUE 2 |
|
249 |
#else |
|
250 |
#define CASESENSITIVITYDEFAULTVALUE 1 |
|
251 |
#endif |
|
252 |
||
253 |
#ifndef STRCMPCASENOSENTIVEFUNCTION |
|
254 |
#define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal |
|
255 |
#endif |
|
256 |
||
257 |
/* |
|
258 |
Compare two filename (fileName1,fileName2). |
|
259 |
If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) |
|
260 |
If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi |
|
261 |
or strcasecmp) |
|
262 |
If iCaseSenisivity = 0, case sensitivity is defaut of your operating system |
|
263 |
(like 1 on Unix, 2 on Windows) |
|
264 |
||
265 |
*/ |
|
266 |
extern int ZEXPORT unzStringFileNameCompare (fileName1,fileName2,iCaseSensitivity) |
|
267 |
const char* fileName1; |
|
268 |
const char* fileName2; |
|
269 |
int iCaseSensitivity; |
|
270 |
{ |
|
271 |
if (iCaseSensitivity==0) |
|
272 |
iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE; |
|
273 |
||
274 |
if (iCaseSensitivity==1) |
|
275 |
return strcmp(fileName1,fileName2); |
|
276 |
||
277 |
return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2); |
|
278 |
} |
|
279 |
||
280 |
#define BUFREADCOMMENT (0x400) |
|
281 |
||
282 |
/* |
|
283 |
Locate the Central directory of a zipfile (at the end, just before |
|
284 |
the global comment) |
|
285 |
*/ |
|
286 |
local uLong unzlocal_SearchCentralDir(fin) |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
287 |
void *fin; |
47 | 288 |
{ |
289 |
unsigned char* buf; |
|
290 |
uLong uSizeFile; |
|
291 |
uLong uBackRead; |
|
292 |
uLong uMaxBack=0xffff; /* maximum size of global comment */ |
|
293 |
uLong uPosFound=0; |
|
294 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
295 |
uSizeFile = (uLong) __PHYSFS_platformFileLength( fin ); |
47 | 296 |
|
297 |
if (uMaxBack>uSizeFile) |
|
298 |
uMaxBack = uSizeFile; |
|
299 |
||
300 |
buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); |
|
301 |
if (buf==NULL) |
|
302 |
return 0; |
|
303 |
||
304 |
uBackRead = 4; |
|
305 |
while (uBackRead<uMaxBack) |
|
306 |
{ |
|
307 |
uLong uReadSize,uReadPos ; |
|
308 |
int i; |
|
309 |
if (uBackRead+BUFREADCOMMENT>uMaxBack) |
|
310 |
uBackRead = uMaxBack; |
|
311 |
else |
|
312 |
uBackRead+=BUFREADCOMMENT; |
|
313 |
uReadPos = uSizeFile-uBackRead ; |
|
314 |
||
315 |
uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? |
|
316 |
(BUFREADCOMMENT+4) : (uSizeFile-uReadPos); |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
317 |
if (!__PHYSFS_platformSeek(fin,uReadPos)) |
47 | 318 |
break; |
319 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
320 |
if (__PHYSFS_platformRead(fin,buf,(uInt)uReadSize,1)!=1) |
47 | 321 |
break; |
322 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
323 |
for (i=(int)uReadSize-3; (i--)>0;) |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
324 |
{ |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
325 |
if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && |
47 | 326 |
((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) |
327 |
{ |
|
328 |
uPosFound = uReadPos+i; |
|
329 |
break; |
|
330 |
} |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
331 |
} |
47 | 332 |
|
333 |
if (uPosFound!=0) |
|
334 |
break; |
|
335 |
} |
|
336 |
TRYFREE(buf); |
|
337 |
return uPosFound; |
|
338 |
} |
|
339 |
||
340 |
/* |
|
341 |
Open a Zip file. path contain the full pathname (by example, |
|
342 |
on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer |
|
343 |
"zlib/zlib109.zip". |
|
344 |
If the zipfile cannot be opened (file don't exist or in not valid), the |
|
345 |
return value is NULL. |
|
346 |
Else, the return value is a unzFile Handle, usable with other function |
|
347 |
of this unzip package. |
|
348 |
*/ |
|
349 |
extern unzFile ZEXPORT unzOpen (path) |
|
350 |
const char *path; |
|
351 |
{ |
|
352 |
unz_s us; |
|
353 |
unz_s *s; |
|
354 |
uLong central_pos,uL; |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
355 |
void* fin ; |
47 | 356 |
|
357 |
uLong number_disk; /* number of the current dist, used for |
|
358 |
spaning ZIP, unsupported, always 0*/ |
|
359 |
uLong number_disk_with_CD; /* number the the disk with central dir, used |
|
360 |
for spaning ZIP, unsupported, always 0*/ |
|
361 |
uLong number_entry_CD; /* total number of entries in |
|
362 |
the central dir |
|
363 |
(same than number_entry on nospan) */ |
|
364 |
||
365 |
int err=UNZ_OK; |
|
366 |
||
367 |
if (unz_copyright[0]!=' ') |
|
368 |
return NULL; |
|
369 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
370 |
fin=__PHYSFS_platformOpenRead(path); |
47 | 371 |
if (fin==NULL) |
372 |
return NULL; |
|
373 |
||
374 |
central_pos = unzlocal_SearchCentralDir(fin); |
|
375 |
if (central_pos==0) |
|
376 |
err=UNZ_ERRNO; |
|
377 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
378 |
if (!__PHYSFS_platformSeek(fin,central_pos)) |
47 | 379 |
err=UNZ_ERRNO; |
380 |
||
381 |
/* the signature, already checked */ |
|
382 |
if (unzlocal_getLong(fin,&uL)!=UNZ_OK) |
|
383 |
err=UNZ_ERRNO; |
|
384 |
||
385 |
/* number of this disk */ |
|
386 |
if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK) |
|
387 |
err=UNZ_ERRNO; |
|
388 |
||
389 |
/* number of the disk with the start of the central directory */ |
|
390 |
if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK) |
|
391 |
err=UNZ_ERRNO; |
|
392 |
||
393 |
/* total number of entries in the central dir on this disk */ |
|
394 |
if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK) |
|
395 |
err=UNZ_ERRNO; |
|
396 |
||
397 |
/* total number of entries in the central dir */ |
|
398 |
if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK) |
|
399 |
err=UNZ_ERRNO; |
|
400 |
||
401 |
if ((number_entry_CD!=us.gi.number_entry) || |
|
402 |
(number_disk_with_CD!=0) || |
|
403 |
(number_disk!=0)) |
|
404 |
err=UNZ_BADZIPFILE; |
|
405 |
||
406 |
/* size of the central directory */ |
|
407 |
if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK) |
|
408 |
err=UNZ_ERRNO; |
|
409 |
||
410 |
/* offset of start of central directory with respect to the |
|
411 |
starting disk number */ |
|
412 |
if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK) |
|
413 |
err=UNZ_ERRNO; |
|
414 |
||
415 |
/* zipfile comment length */ |
|
416 |
if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK) |
|
417 |
err=UNZ_ERRNO; |
|
418 |
||
419 |
if ((central_pos<us.offset_central_dir+us.size_central_dir) && |
|
420 |
(err==UNZ_OK)) |
|
421 |
err=UNZ_BADZIPFILE; |
|
422 |
||
423 |
if (err!=UNZ_OK) |
|
424 |
{ |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
425 |
__PHYSFS_platformClose(fin); |
47 | 426 |
return NULL; |
427 |
} |
|
428 |
||
429 |
us.file=fin; |
|
430 |
us.byte_before_the_zipfile = central_pos - |
|
431 |
(us.offset_central_dir+us.size_central_dir); |
|
432 |
us.central_pos = central_pos; |
|
433 |
us.pfile_in_zip_read = NULL; |
|
434 |
||
435 |
||
436 |
s=(unz_s*)ALLOC(sizeof(unz_s)); |
|
437 |
*s=us; |
|
438 |
unzGoToFirstFile((unzFile)s); |
|
439 |
return (unzFile)s; |
|
440 |
} |
|
441 |
||
442 |
||
443 |
/* |
|
444 |
Close a ZipFile opened with unzipOpen. |
|
445 |
If there is files inside the .Zip opened with unzipOpenCurrentFile (see later), |
|
446 |
these files MUST be closed with unzipCloseCurrentFile before call unzipClose. |
|
447 |
return UNZ_OK if there is no problem. */ |
|
448 |
extern int ZEXPORT unzClose (file) |
|
449 |
unzFile file; |
|
450 |
{ |
|
451 |
unz_s* s; |
|
452 |
if (file==NULL) |
|
453 |
return UNZ_PARAMERROR; |
|
454 |
s=(unz_s*)file; |
|
455 |
||
456 |
if (s->pfile_in_zip_read!=NULL) |
|
457 |
unzCloseCurrentFile(file); |
|
458 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
459 |
__PHYSFS_platformClose(s->file); |
47 | 460 |
TRYFREE(s); |
461 |
return UNZ_OK; |
|
462 |
} |
|
463 |
||
464 |
||
465 |
/* |
|
466 |
Write info about the ZipFile in the *pglobal_info structure. |
|
467 |
No preparation of the structure is needed |
|
468 |
return UNZ_OK if there is no problem. */ |
|
469 |
extern int ZEXPORT unzGetGlobalInfo (file,pglobal_info) |
|
470 |
unzFile file; |
|
471 |
unz_global_info *pglobal_info; |
|
472 |
{ |
|
473 |
unz_s* s; |
|
474 |
if (file==NULL) |
|
475 |
return UNZ_PARAMERROR; |
|
476 |
s=(unz_s*)file; |
|
477 |
*pglobal_info=s->gi; |
|
478 |
return UNZ_OK; |
|
479 |
} |
|
480 |
||
481 |
||
482 |
/* |
|
483 |
Translate date/time from Dos format to tm_unz (readable more easilty) |
|
484 |
*/ |
|
485 |
local void unzlocal_DosDateToTmuDate (ulDosDate, ptm) |
|
486 |
uLong ulDosDate; |
|
487 |
tm_unz* ptm; |
|
488 |
{ |
|
489 |
uLong uDate; |
|
490 |
uDate = (uLong)(ulDosDate>>16); |
|
491 |
ptm->tm_mday = (uInt)(uDate&0x1f) ; |
|
492 |
ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ; |
|
493 |
ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ; |
|
494 |
||
495 |
ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800); |
|
496 |
ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ; |
|
497 |
ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ; |
|
498 |
} |
|
499 |
||
500 |
/* |
|
501 |
Get Info about the current file in the zipfile, with internal only info |
|
502 |
*/ |
|
503 |
local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file, |
|
504 |
unz_file_info *pfile_info, |
|
505 |
unz_file_info_internal |
|
506 |
*pfile_info_internal, |
|
507 |
char *szFileName, |
|
508 |
uLong fileNameBufferSize, |
|
509 |
void *extraField, |
|
510 |
uLong extraFieldBufferSize, |
|
511 |
char *szComment, |
|
512 |
uLong commentBufferSize)); |
|
513 |
||
514 |
local int unzlocal_GetCurrentFileInfoInternal (file, |
|
515 |
pfile_info, |
|
516 |
pfile_info_internal, |
|
517 |
szFileName, fileNameBufferSize, |
|
518 |
extraField, extraFieldBufferSize, |
|
519 |
szComment, commentBufferSize) |
|
520 |
unzFile file; |
|
521 |
unz_file_info *pfile_info; |
|
522 |
unz_file_info_internal *pfile_info_internal; |
|
523 |
char *szFileName; |
|
524 |
uLong fileNameBufferSize; |
|
525 |
void *extraField; |
|
526 |
uLong extraFieldBufferSize; |
|
527 |
char *szComment; |
|
528 |
uLong commentBufferSize; |
|
529 |
{ |
|
530 |
unz_s* s; |
|
531 |
unz_file_info file_info; |
|
532 |
unz_file_info_internal file_info_internal; |
|
533 |
int err=UNZ_OK; |
|
534 |
uLong uMagic; |
|
535 |
long lSeek=0; |
|
536 |
||
537 |
if (file==NULL) |
|
538 |
return UNZ_PARAMERROR; |
|
539 |
s=(unz_s*)file; |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
540 |
if (!__PHYSFS_platformSeek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile)) |
47 | 541 |
err=UNZ_ERRNO; |
542 |
||
543 |
||
544 |
/* we check the magic */ |
|
545 |
if (err==UNZ_OK) |
|
546 |
{ |
|
547 |
if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK) |
|
548 |
err=UNZ_ERRNO; |
|
549 |
else if (uMagic!=0x02014b50) |
|
550 |
err=UNZ_BADZIPFILE; |
|
551 |
} |
|
552 |
||
553 |
if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK) |
|
554 |
err=UNZ_ERRNO; |
|
555 |
||
556 |
if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK) |
|
557 |
err=UNZ_ERRNO; |
|
558 |
||
559 |
if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK) |
|
560 |
err=UNZ_ERRNO; |
|
561 |
||
562 |
if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK) |
|
563 |
err=UNZ_ERRNO; |
|
564 |
||
565 |
if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK) |
|
566 |
err=UNZ_ERRNO; |
|
567 |
||
568 |
unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date); |
|
569 |
||
570 |
if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK) |
|
571 |
err=UNZ_ERRNO; |
|
572 |
||
573 |
if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK) |
|
574 |
err=UNZ_ERRNO; |
|
575 |
||
576 |
if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK) |
|
577 |
err=UNZ_ERRNO; |
|
578 |
||
579 |
if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK) |
|
580 |
err=UNZ_ERRNO; |
|
581 |
||
582 |
if (unzlocal_getShort(s->file,&file_info.size_file_extra) != UNZ_OK) |
|
583 |
err=UNZ_ERRNO; |
|
584 |
||
585 |
if (unzlocal_getShort(s->file,&file_info.size_file_comment) != UNZ_OK) |
|
586 |
err=UNZ_ERRNO; |
|
587 |
||
588 |
if (unzlocal_getShort(s->file,&file_info.disk_num_start) != UNZ_OK) |
|
589 |
err=UNZ_ERRNO; |
|
590 |
||
591 |
if (unzlocal_getShort(s->file,&file_info.internal_fa) != UNZ_OK) |
|
592 |
err=UNZ_ERRNO; |
|
593 |
||
594 |
if (unzlocal_getLong(s->file,&file_info.external_fa) != UNZ_OK) |
|
595 |
err=UNZ_ERRNO; |
|
596 |
||
597 |
if (unzlocal_getLong(s->file,&file_info_internal.offset_curfile) != UNZ_OK) |
|
598 |
err=UNZ_ERRNO; |
|
599 |
||
600 |
lSeek+=file_info.size_filename; |
|
601 |
if ((err==UNZ_OK) && (szFileName!=NULL)) |
|
602 |
{ |
|
603 |
uLong uSizeRead ; |
|
604 |
if (file_info.size_filename<fileNameBufferSize) |
|
605 |
{ |
|
606 |
*(szFileName+file_info.size_filename)='\0'; |
|
607 |
uSizeRead = file_info.size_filename; |
|
608 |
} |
|
609 |
else |
|
610 |
uSizeRead = fileNameBufferSize; |
|
611 |
||
612 |
if ((file_info.size_filename>0) && (fileNameBufferSize>0)) |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
613 |
{ |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
614 |
if (__PHYSFS_platformRead(s->file,szFileName,(uInt)uSizeRead,1)!=1) |
47 | 615 |
err=UNZ_ERRNO; |
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
616 |
} |
47 | 617 |
lSeek -= uSizeRead; |
618 |
} |
|
619 |
||
620 |
||
621 |
if ((err==UNZ_OK) && (extraField!=NULL)) |
|
622 |
{ |
|
623 |
uLong uSizeRead ; |
|
624 |
if (file_info.size_file_extra<extraFieldBufferSize) |
|
625 |
uSizeRead = file_info.size_file_extra; |
|
626 |
else |
|
627 |
uSizeRead = extraFieldBufferSize; |
|
628 |
||
629 |
if (lSeek!=0) |
|
630 |
{ |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
631 |
PHYSFS_sint64 curpos = __PHYSFS_platformTell(s->file); |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
632 |
if ((curpos >= 0) && (__PHYSFS_platformSeek(s->file,lSeek+curpos))) |
47 | 633 |
lSeek=0; |
634 |
else |
|
635 |
err=UNZ_ERRNO; |
|
636 |
} |
|
637 |
if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0)) |
|
638 |
{ |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
639 |
if (__PHYSFS_platformRead(s->file,extraField,(uInt)uSizeRead,1)!=1) |
47 | 640 |
err=UNZ_ERRNO; |
641 |
} |
|
642 |
lSeek += file_info.size_file_extra - uSizeRead; |
|
643 |
} |
|
644 |
else |
|
645 |
lSeek+=file_info.size_file_extra; |
|
646 |
||
647 |
||
648 |
if ((err==UNZ_OK) && (szComment!=NULL)) |
|
649 |
{ |
|
650 |
uLong uSizeRead ; |
|
651 |
if (file_info.size_file_comment<commentBufferSize) |
|
652 |
{ |
|
653 |
*(szComment+file_info.size_file_comment)='\0'; |
|
654 |
uSizeRead = file_info.size_file_comment; |
|
655 |
} |
|
656 |
else |
|
657 |
uSizeRead = commentBufferSize; |
|
658 |
||
659 |
if (lSeek!=0) |
|
660 |
{ |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
661 |
PHYSFS_sint64 curpos = __PHYSFS_platformTell(s->file); |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
662 |
if (__PHYSFS_platformSeek(s->file,lSeek+curpos)) |
47 | 663 |
lSeek=0; |
664 |
else |
|
665 |
err=UNZ_ERRNO; |
|
666 |
} |
|
667 |
if ((file_info.size_file_comment>0) && (commentBufferSize>0)) |
|
668 |
{ |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
669 |
if (__PHYSFS_platformRead(s->file,szComment,(uInt)uSizeRead,1)!=1) |
47 | 670 |
err=UNZ_ERRNO; |
671 |
} |
|
672 |
lSeek+=file_info.size_file_comment - uSizeRead; |
|
673 |
} |
|
674 |
else |
|
675 |
lSeek+=file_info.size_file_comment; |
|
676 |
||
677 |
if ((err==UNZ_OK) && (pfile_info!=NULL)) |
|
678 |
*pfile_info=file_info; |
|
679 |
||
680 |
if ((err==UNZ_OK) && (pfile_info_internal!=NULL)) |
|
681 |
*pfile_info_internal=file_info_internal; |
|
682 |
||
683 |
return err; |
|
684 |
} |
|
685 |
||
686 |
||
687 |
||
688 |
/* |
|
689 |
Write info about the ZipFile in the *pglobal_info structure. |
|
690 |
No preparation of the structure is needed |
|
691 |
return UNZ_OK if there is no problem. |
|
692 |
*/ |
|
693 |
extern int ZEXPORT unzGetCurrentFileInfo (file, |
|
694 |
pfile_info, |
|
695 |
szFileName, fileNameBufferSize, |
|
696 |
extraField, extraFieldBufferSize, |
|
697 |
szComment, commentBufferSize) |
|
698 |
unzFile file; |
|
699 |
unz_file_info *pfile_info; |
|
700 |
char *szFileName; |
|
701 |
uLong fileNameBufferSize; |
|
702 |
void *extraField; |
|
703 |
uLong extraFieldBufferSize; |
|
704 |
char *szComment; |
|
705 |
uLong commentBufferSize; |
|
706 |
{ |
|
707 |
return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL, |
|
708 |
szFileName,fileNameBufferSize, |
|
709 |
extraField,extraFieldBufferSize, |
|
710 |
szComment,commentBufferSize); |
|
711 |
} |
|
712 |
||
713 |
/* |
|
714 |
Set the current file of the zipfile to the first file. |
|
715 |
return UNZ_OK if there is no problem |
|
716 |
*/ |
|
717 |
extern int ZEXPORT unzGoToFirstFile (file) |
|
718 |
unzFile file; |
|
719 |
{ |
|
720 |
int err=UNZ_OK; |
|
721 |
unz_s* s; |
|
722 |
if (file==NULL) |
|
723 |
return UNZ_PARAMERROR; |
|
724 |
s=(unz_s*)file; |
|
725 |
s->pos_in_central_dir=s->offset_central_dir; |
|
726 |
s->num_file=0; |
|
727 |
err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, |
|
728 |
&s->cur_file_info_internal, |
|
729 |
NULL,0,NULL,0,NULL,0); |
|
730 |
s->current_file_ok = (err == UNZ_OK); |
|
731 |
return err; |
|
732 |
} |
|
733 |
||
734 |
||
735 |
/* |
|
736 |
Set the current file of the zipfile to the next file. |
|
737 |
return UNZ_OK if there is no problem |
|
738 |
return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. |
|
739 |
*/ |
|
740 |
extern int ZEXPORT unzGoToNextFile (file) |
|
741 |
unzFile file; |
|
742 |
{ |
|
743 |
unz_s* s; |
|
744 |
int err; |
|
745 |
||
746 |
if (file==NULL) |
|
747 |
return UNZ_PARAMERROR; |
|
748 |
s=(unz_s*)file; |
|
749 |
if (!s->current_file_ok) |
|
750 |
return UNZ_END_OF_LIST_OF_FILE; |
|
751 |
if (s->num_file+1==s->gi.number_entry) |
|
752 |
return UNZ_END_OF_LIST_OF_FILE; |
|
753 |
||
754 |
s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename + |
|
755 |
s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ; |
|
756 |
s->num_file++; |
|
757 |
err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, |
|
758 |
&s->cur_file_info_internal, |
|
759 |
NULL,0,NULL,0,NULL,0); |
|
760 |
s->current_file_ok = (err == UNZ_OK); |
|
761 |
return err; |
|
762 |
} |
|
763 |
||
764 |
||
765 |
/* |
|
766 |
Try locate the file szFileName in the zipfile. |
|
767 |
For the iCaseSensitivity signification, see unzipStringFileNameCompare |
|
768 |
||
769 |
return value : |
|
770 |
UNZ_OK if the file is found. It becomes the current file. |
|
771 |
UNZ_END_OF_LIST_OF_FILE if the file is not found |
|
772 |
*/ |
|
773 |
extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity) |
|
774 |
unzFile file; |
|
775 |
const char *szFileName; |
|
776 |
int iCaseSensitivity; |
|
777 |
{ |
|
778 |
unz_s* s; |
|
779 |
int err; |
|
780 |
||
781 |
||
782 |
uLong num_fileSaved; |
|
783 |
uLong pos_in_central_dirSaved; |
|
784 |
||
785 |
||
786 |
if (file==NULL) |
|
787 |
return UNZ_PARAMERROR; |
|
788 |
||
789 |
if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP) |
|
790 |
return UNZ_PARAMERROR; |
|
791 |
||
792 |
s=(unz_s*)file; |
|
793 |
if (!s->current_file_ok) |
|
794 |
return UNZ_END_OF_LIST_OF_FILE; |
|
795 |
||
796 |
num_fileSaved = s->num_file; |
|
797 |
pos_in_central_dirSaved = s->pos_in_central_dir; |
|
798 |
||
799 |
err = unzGoToFirstFile(file); |
|
800 |
||
801 |
while (err == UNZ_OK) |
|
802 |
{ |
|
803 |
char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1]; |
|
804 |
unzGetCurrentFileInfo(file,NULL, |
|
805 |
szCurrentFileName,sizeof(szCurrentFileName)-1, |
|
806 |
NULL,0,NULL,0); |
|
807 |
if (unzStringFileNameCompare(szCurrentFileName, |
|
808 |
szFileName,iCaseSensitivity)==0) |
|
809 |
return UNZ_OK; |
|
810 |
err = unzGoToNextFile(file); |
|
811 |
} |
|
812 |
||
813 |
s->num_file = num_fileSaved ; |
|
814 |
s->pos_in_central_dir = pos_in_central_dirSaved ; |
|
815 |
return err; |
|
816 |
} |
|
817 |
||
818 |
||
819 |
/* |
|
820 |
Read the local header of the current zipfile |
|
821 |
Check the coherency of the local header and info in the end of central |
|
822 |
directory about this file |
|
823 |
store in *piSizeVar the size of extra info in local header |
|
824 |
(filename and size of extra field data) |
|
825 |
*/ |
|
826 |
local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar, |
|
827 |
poffset_local_extrafield, |
|
828 |
psize_local_extrafield) |
|
829 |
unz_s* s; |
|
830 |
uInt* piSizeVar; |
|
831 |
uLong *poffset_local_extrafield; |
|
832 |
uInt *psize_local_extrafield; |
|
833 |
{ |
|
834 |
uLong uMagic,uData,uFlags; |
|
835 |
uLong size_filename; |
|
836 |
uLong size_extra_field; |
|
837 |
int err=UNZ_OK; |
|
838 |
||
839 |
*piSizeVar = 0; |
|
840 |
*poffset_local_extrafield = 0; |
|
841 |
*psize_local_extrafield = 0; |
|
842 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
843 |
if (!__PHYSFS_platformSeek(s->file, |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
844 |
s->cur_file_info_internal.offset_curfile + |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
845 |
s->byte_before_the_zipfile)) |
47 | 846 |
{ |
847 |
return UNZ_ERRNO; |
|
848 |
} |
|
849 |
||
850 |
if (err==UNZ_OK) |
|
851 |
{ |
|
852 |
if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK) |
|
853 |
err=UNZ_ERRNO; |
|
854 |
else if (uMagic!=0x04034b50) |
|
855 |
err=UNZ_BADZIPFILE; |
|
856 |
} |
|
857 |
||
858 |
if (unzlocal_getShort(s->file,&uData) != UNZ_OK) |
|
859 |
err=UNZ_ERRNO; |
|
860 |
/* |
|
861 |
else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion)) |
|
862 |
err=UNZ_BADZIPFILE; |
|
863 |
*/ |
|
864 |
if (unzlocal_getShort(s->file,&uFlags) != UNZ_OK) |
|
865 |
err=UNZ_ERRNO; |
|
866 |
||
867 |
if (unzlocal_getShort(s->file,&uData) != UNZ_OK) |
|
868 |
err=UNZ_ERRNO; |
|
869 |
else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method)) |
|
870 |
err=UNZ_BADZIPFILE; |
|
871 |
||
872 |
if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) && |
|
873 |
(s->cur_file_info.compression_method!=Z_DEFLATED)) |
|
874 |
err=UNZ_BADZIPFILE; |
|
875 |
||
876 |
if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* date/time */ |
|
877 |
err=UNZ_ERRNO; |
|
878 |
||
879 |
if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* crc */ |
|
880 |
err=UNZ_ERRNO; |
|
881 |
else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) && |
|
882 |
((uFlags & 8)==0)) |
|
883 |
err=UNZ_BADZIPFILE; |
|
884 |
||
885 |
if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size compr */ |
|
886 |
err=UNZ_ERRNO; |
|
887 |
else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) && |
|
888 |
((uFlags & 8)==0)) |
|
889 |
err=UNZ_BADZIPFILE; |
|
890 |
||
891 |
if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size uncompr */ |
|
892 |
err=UNZ_ERRNO; |
|
893 |
else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && |
|
894 |
((uFlags & 8)==0)) |
|
895 |
err=UNZ_BADZIPFILE; |
|
896 |
||
897 |
||
898 |
if (unzlocal_getShort(s->file,&size_filename) != UNZ_OK) |
|
899 |
err=UNZ_ERRNO; |
|
900 |
else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename)) |
|
901 |
err=UNZ_BADZIPFILE; |
|
902 |
||
903 |
*piSizeVar += (uInt)size_filename; |
|
904 |
||
905 |
if (unzlocal_getShort(s->file,&size_extra_field) != UNZ_OK) |
|
906 |
err=UNZ_ERRNO; |
|
907 |
*poffset_local_extrafield= s->cur_file_info_internal.offset_curfile + |
|
908 |
SIZEZIPLOCALHEADER + size_filename; |
|
909 |
*psize_local_extrafield = (uInt)size_extra_field; |
|
910 |
||
911 |
*piSizeVar += (uInt)size_extra_field; |
|
912 |
||
913 |
return err; |
|
914 |
} |
|
915 |
||
916 |
/* |
|
917 |
Open for reading data the current file in the zipfile. |
|
918 |
If there is no error and the file is opened, the return value is UNZ_OK. |
|
919 |
*/ |
|
920 |
extern int ZEXPORT unzOpenCurrentFile (file) |
|
921 |
unzFile file; |
|
922 |
{ |
|
923 |
int err=UNZ_OK; |
|
924 |
int Store; |
|
925 |
uInt iSizeVar; |
|
926 |
unz_s* s; |
|
927 |
file_in_zip_read_info_s* pfile_in_zip_read_info; |
|
928 |
uLong offset_local_extrafield; /* offset of the local extra field */ |
|
929 |
uInt size_local_extrafield; /* size of the local extra field */ |
|
930 |
||
931 |
if (file==NULL) |
|
932 |
return UNZ_PARAMERROR; |
|
933 |
s=(unz_s*)file; |
|
934 |
if (!s->current_file_ok) |
|
935 |
return UNZ_PARAMERROR; |
|
936 |
||
937 |
if (s->pfile_in_zip_read != NULL) |
|
938 |
unzCloseCurrentFile(file); |
|
939 |
||
940 |
if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar, |
|
941 |
&offset_local_extrafield,&size_local_extrafield)!=UNZ_OK) |
|
942 |
return UNZ_BADZIPFILE; |
|
943 |
||
944 |
pfile_in_zip_read_info = (file_in_zip_read_info_s*) |
|
945 |
ALLOC(sizeof(file_in_zip_read_info_s)); |
|
946 |
if (pfile_in_zip_read_info==NULL) |
|
947 |
return UNZ_INTERNALERROR; |
|
948 |
||
949 |
pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE); |
|
950 |
pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield; |
|
951 |
pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield; |
|
952 |
pfile_in_zip_read_info->pos_local_extrafield=0; |
|
953 |
||
954 |
if (pfile_in_zip_read_info->read_buffer==NULL) |
|
955 |
{ |
|
956 |
TRYFREE(pfile_in_zip_read_info); |
|
957 |
return UNZ_INTERNALERROR; |
|
958 |
} |
|
959 |
||
960 |
pfile_in_zip_read_info->stream_initialised=0; |
|
961 |
||
962 |
if ((s->cur_file_info.compression_method!=0) && |
|
963 |
(s->cur_file_info.compression_method!=Z_DEFLATED)) |
|
964 |
err=UNZ_BADZIPFILE; |
|
965 |
Store = s->cur_file_info.compression_method==0; |
|
966 |
||
967 |
pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc; |
|
968 |
pfile_in_zip_read_info->crc32=0; |
|
969 |
pfile_in_zip_read_info->compression_method = |
|
970 |
s->cur_file_info.compression_method; |
|
971 |
pfile_in_zip_read_info->file=s->file; |
|
972 |
pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile; |
|
973 |
||
974 |
pfile_in_zip_read_info->stream.total_out = 0; |
|
975 |
||
976 |
if (!Store) |
|
977 |
{ |
|
978 |
pfile_in_zip_read_info->stream.zalloc = (alloc_func)0; |
|
979 |
pfile_in_zip_read_info->stream.zfree = (free_func)0; |
|
980 |
pfile_in_zip_read_info->stream.opaque = (voidpf)0; |
|
981 |
||
982 |
err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS); |
|
983 |
if (err == Z_OK) |
|
984 |
pfile_in_zip_read_info->stream_initialised=1; |
|
985 |
/* windowBits is passed < 0 to tell that there is no zlib header. |
|
986 |
* Note that in this case inflate *requires* an extra "dummy" byte |
|
987 |
* after the compressed stream in order to complete decompression and |
|
988 |
* return Z_STREAM_END. |
|
989 |
* In unzip, i don't wait absolutely Z_STREAM_END because I known the |
|
990 |
* size of both compressed and uncompressed data |
|
991 |
*/ |
|
992 |
} |
|
993 |
pfile_in_zip_read_info->rest_read_compressed = |
|
994 |
s->cur_file_info.compressed_size ; |
|
995 |
pfile_in_zip_read_info->rest_read_uncompressed = |
|
996 |
s->cur_file_info.uncompressed_size ; |
|
997 |
||
998 |
||
999 |
pfile_in_zip_read_info->pos_in_zipfile = |
|
1000 |
s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + |
|
1001 |
iSizeVar; |
|
1002 |
||
1003 |
pfile_in_zip_read_info->stream.avail_in = (uInt)0; |
|
1004 |
||
1005 |
||
1006 |
s->pfile_in_zip_read = pfile_in_zip_read_info; |
|
1007 |
return UNZ_OK; |
|
1008 |
} |
|
1009 |
||
1010 |
||
1011 |
/* |
|
1012 |
Read bytes from the current file. |
|
1013 |
buf contain buffer where data must be copied |
|
1014 |
len the size of buf. |
|
1015 |
||
1016 |
return the number of byte copied if somes bytes are copied |
|
1017 |
return 0 if the end of file was reached |
|
1018 |
return <0 with error code if there is an error |
|
1019 |
(UNZ_ERRNO for IO error, or zLib error for uncompress error) |
|
1020 |
*/ |
|
1021 |
extern int ZEXPORT unzReadCurrentFile (file, buf, len) |
|
1022 |
unzFile file; |
|
1023 |
voidp buf; |
|
1024 |
unsigned len; |
|
1025 |
{ |
|
1026 |
int err=UNZ_OK; |
|
1027 |
uInt iRead = 0; |
|
1028 |
unz_s* s; |
|
1029 |
file_in_zip_read_info_s* pfile_in_zip_read_info; |
|
1030 |
if (file==NULL) |
|
1031 |
return UNZ_PARAMERROR; |
|
1032 |
s=(unz_s*)file; |
|
1033 |
pfile_in_zip_read_info=s->pfile_in_zip_read; |
|
1034 |
||
1035 |
if (pfile_in_zip_read_info==NULL) |
|
1036 |
return UNZ_PARAMERROR; |
|
1037 |
||
1038 |
||
1039 |
if ((pfile_in_zip_read_info->read_buffer == NULL)) |
|
1040 |
return UNZ_END_OF_LIST_OF_FILE; |
|
1041 |
if (len==0) |
|
1042 |
return 0; |
|
1043 |
||
1044 |
pfile_in_zip_read_info->stream.next_out = (Bytef*)buf; |
|
1045 |
||
1046 |
pfile_in_zip_read_info->stream.avail_out = (uInt)len; |
|
1047 |
||
1048 |
if (len>pfile_in_zip_read_info->rest_read_uncompressed) |
|
1049 |
pfile_in_zip_read_info->stream.avail_out = |
|
1050 |
(uInt)pfile_in_zip_read_info->rest_read_uncompressed; |
|
1051 |
||
1052 |
while (pfile_in_zip_read_info->stream.avail_out>0) |
|
1053 |
{ |
|
1054 |
if ((pfile_in_zip_read_info->stream.avail_in==0) && |
|
1055 |
(pfile_in_zip_read_info->rest_read_compressed>0)) |
|
1056 |
{ |
|
1057 |
uInt uReadThis = UNZ_BUFSIZE; |
|
1058 |
if (pfile_in_zip_read_info->rest_read_compressed<uReadThis) |
|
1059 |
uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed; |
|
1060 |
if (uReadThis == 0) |
|
1061 |
return UNZ_EOF; |
|
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
1062 |
if (!__PHYSFS_platformSeek(pfile_in_zip_read_info->file, |
47 | 1063 |
pfile_in_zip_read_info->pos_in_zipfile + |
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
1064 |
pfile_in_zip_read_info->byte_before_the_zipfile)) |
47 | 1065 |
return UNZ_ERRNO; |
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
1066 |
if (__PHYSFS_platformRead(pfile_in_zip_read_info->file, |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
1067 |
pfile_in_zip_read_info->read_buffer, |
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
1068 |
uReadThis,1)!=1) |
47 | 1069 |
return UNZ_ERRNO; |
1070 |
pfile_in_zip_read_info->pos_in_zipfile += uReadThis; |
|
1071 |
||
1072 |
pfile_in_zip_read_info->rest_read_compressed-=uReadThis; |
|
1073 |
||
1074 |
pfile_in_zip_read_info->stream.next_in = |
|
1075 |
(Bytef*)pfile_in_zip_read_info->read_buffer; |
|
1076 |
pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis; |
|
1077 |
} |
|
1078 |
||
1079 |
if (pfile_in_zip_read_info->compression_method==0) |
|
1080 |
{ |
|
1081 |
uInt uDoCopy,i ; |
|
1082 |
if (pfile_in_zip_read_info->stream.avail_out < |
|
1083 |
pfile_in_zip_read_info->stream.avail_in) |
|
1084 |
uDoCopy = pfile_in_zip_read_info->stream.avail_out ; |
|
1085 |
else |
|
1086 |
uDoCopy = pfile_in_zip_read_info->stream.avail_in ; |
|
1087 |
||
1088 |
for (i=0;i<uDoCopy;i++) |
|
1089 |
*(pfile_in_zip_read_info->stream.next_out+i) = |
|
1090 |
*(pfile_in_zip_read_info->stream.next_in+i); |
|
1091 |
||
1092 |
pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, |
|
1093 |
pfile_in_zip_read_info->stream.next_out, |
|
1094 |
uDoCopy); |
|
1095 |
pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy; |
|
1096 |
pfile_in_zip_read_info->stream.avail_in -= uDoCopy; |
|
1097 |
pfile_in_zip_read_info->stream.avail_out -= uDoCopy; |
|
1098 |
pfile_in_zip_read_info->stream.next_out += uDoCopy; |
|
1099 |
pfile_in_zip_read_info->stream.next_in += uDoCopy; |
|
1100 |
pfile_in_zip_read_info->stream.total_out += uDoCopy; |
|
1101 |
iRead += uDoCopy; |
|
1102 |
} |
|
1103 |
else |
|
1104 |
{ |
|
1105 |
uLong uTotalOutBefore,uTotalOutAfter; |
|
1106 |
const Bytef *bufBefore; |
|
1107 |
uLong uOutThis; |
|
1108 |
int flush=Z_SYNC_FLUSH; |
|
1109 |
||
1110 |
uTotalOutBefore = pfile_in_zip_read_info->stream.total_out; |
|
1111 |
bufBefore = pfile_in_zip_read_info->stream.next_out; |
|
1112 |
||
1113 |
/* |
|
1114 |
if ((pfile_in_zip_read_info->rest_read_uncompressed == |
|
1115 |
pfile_in_zip_read_info->stream.avail_out) && |
|
1116 |
(pfile_in_zip_read_info->rest_read_compressed == 0)) |
|
1117 |
flush = Z_FINISH; |
|
1118 |
*/ |
|
1119 |
err=inflate(&pfile_in_zip_read_info->stream,flush); |
|
1120 |
||
1121 |
uTotalOutAfter = pfile_in_zip_read_info->stream.total_out; |
|
1122 |
uOutThis = uTotalOutAfter-uTotalOutBefore; |
|
1123 |
||
1124 |
pfile_in_zip_read_info->crc32 = |
|
1125 |
crc32(pfile_in_zip_read_info->crc32,bufBefore, |
|
1126 |
(uInt)(uOutThis)); |
|
1127 |
||
1128 |
pfile_in_zip_read_info->rest_read_uncompressed -= |
|
1129 |
uOutThis; |
|
1130 |
||
1131 |
iRead += (uInt)(uTotalOutAfter - uTotalOutBefore); |
|
1132 |
||
1133 |
if (err==Z_STREAM_END) |
|
1134 |
return (iRead==0) ? UNZ_EOF : iRead; |
|
1135 |
if (err!=Z_OK) |
|
1136 |
break; |
|
1137 |
} |
|
1138 |
} |
|
1139 |
||
1140 |
if (err==Z_OK) |
|
1141 |
return iRead; |
|
1142 |
return err; |
|
1143 |
} |
|
1144 |
||
1145 |
||
1146 |
/* |
|
1147 |
Give the current position in uncompressed data |
|
1148 |
*/ |
|
1149 |
extern z_off_t ZEXPORT unztell (file) |
|
1150 |
unzFile file; |
|
1151 |
{ |
|
1152 |
unz_s* s; |
|
1153 |
file_in_zip_read_info_s* pfile_in_zip_read_info; |
|
1154 |
if (file==NULL) |
|
1155 |
return UNZ_PARAMERROR; |
|
1156 |
s=(unz_s*)file; |
|
1157 |
pfile_in_zip_read_info=s->pfile_in_zip_read; |
|
1158 |
||
1159 |
if (pfile_in_zip_read_info==NULL) |
|
1160 |
return UNZ_PARAMERROR; |
|
1161 |
||
1162 |
return (z_off_t)pfile_in_zip_read_info->stream.total_out; |
|
1163 |
} |
|
1164 |
||
1165 |
||
1166 |
/* |
|
1167 |
return 1 if the end of file was reached, 0 elsewhere |
|
1168 |
*/ |
|
1169 |
extern int ZEXPORT unzeof (file) |
|
1170 |
unzFile file; |
|
1171 |
{ |
|
1172 |
unz_s* s; |
|
1173 |
file_in_zip_read_info_s* pfile_in_zip_read_info; |
|
1174 |
if (file==NULL) |
|
1175 |
return UNZ_PARAMERROR; |
|
1176 |
s=(unz_s*)file; |
|
1177 |
pfile_in_zip_read_info=s->pfile_in_zip_read; |
|
1178 |
||
1179 |
if (pfile_in_zip_read_info==NULL) |
|
1180 |
return UNZ_PARAMERROR; |
|
1181 |
||
1182 |
if (pfile_in_zip_read_info->rest_read_uncompressed == 0) |
|
1183 |
return 1; |
|
1184 |
else |
|
1185 |
return 0; |
|
1186 |
} |
|
1187 |
||
1188 |
||
1189 |
||
1190 |
/* |
|
1191 |
Read extra field from the current file (opened by unzOpenCurrentFile) |
|
1192 |
This is the local-header version of the extra field (sometimes, there is |
|
1193 |
more info in the local-header version than in the central-header) |
|
1194 |
||
1195 |
if buf==NULL, it return the size of the local extra field that can be read |
|
1196 |
||
1197 |
if buf!=NULL, len is the size of the buffer, the extra header is copied in |
|
1198 |
buf. |
|
1199 |
the return value is the number of bytes copied in buf, or (if <0) |
|
1200 |
the error code |
|
1201 |
*/ |
|
1202 |
extern int ZEXPORT unzGetLocalExtrafield (file,buf,len) |
|
1203 |
unzFile file; |
|
1204 |
voidp buf; |
|
1205 |
unsigned len; |
|
1206 |
{ |
|
1207 |
unz_s* s; |
|
1208 |
file_in_zip_read_info_s* pfile_in_zip_read_info; |
|
1209 |
uInt read_now; |
|
1210 |
uLong size_to_read; |
|
1211 |
||
1212 |
if (file==NULL) |
|
1213 |
return UNZ_PARAMERROR; |
|
1214 |
s=(unz_s*)file; |
|
1215 |
pfile_in_zip_read_info=s->pfile_in_zip_read; |
|
1216 |
||
1217 |
if (pfile_in_zip_read_info==NULL) |
|
1218 |
return UNZ_PARAMERROR; |
|
1219 |
||
1220 |
size_to_read = (pfile_in_zip_read_info->size_local_extrafield - |
|
1221 |
pfile_in_zip_read_info->pos_local_extrafield); |
|
1222 |
||
1223 |
if (buf==NULL) |
|
1224 |
return (int)size_to_read; |
|
1225 |
||
1226 |
if (len>size_to_read) |
|
1227 |
read_now = (uInt)size_to_read; |
|
1228 |
else |
|
1229 |
read_now = (uInt)len ; |
|
1230 |
||
1231 |
if (read_now==0) |
|
1232 |
return 0; |
|
1233 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
1234 |
if (!__PHYSFS_platformSeek(pfile_in_zip_read_info->file, |
47 | 1235 |
pfile_in_zip_read_info->offset_local_extrafield + |
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
1236 |
pfile_in_zip_read_info->pos_local_extrafield)) |
47 | 1237 |
return UNZ_ERRNO; |
1238 |
||
143
337505f94c10
Platform abstracted i/o, other bugfixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
1239 |
if (__PHYSFS_platformRead(pfile_in_zip_read_info->file,buf,(uInt)size_to_read,1)!=1) |
47 | 1240 |
return UNZ_ERRNO; |
1241 |
||
1242 |
return (int)read_now; |
|
1243 |
} |
|
1244 |
||
1245 |
/* |
|
1246 |
Close the file in zip opened with unzipOpenCurrentFile |
|
1247 |
Return UNZ_CRCERROR if all the file was read but the CRC is not good |
|
1248 |
*/ |
|
1249 |
extern int ZEXPORT unzCloseCurrentFile (file) |
|
1250 |
unzFile file; |
|
1251 |
{ |
|
1252 |
int err=UNZ_OK; |
|
1253 |
||
1254 |
unz_s* s; |
|
1255 |
file_in_zip_read_info_s* pfile_in_zip_read_info; |
|
1256 |
if (file==NULL) |
|
1257 |
return UNZ_PARAMERROR; |
|
1258 |
s=(unz_s*)file; |
|
1259 |
pfile_in_zip_read_info=s->pfile_in_zip_read; |
|
1260 |
||
1261 |
if (pfile_in_zip_read_info==NULL) |
|
1262 |
return UNZ_PARAMERROR; |
|
1263 |
||
1264 |
||
1265 |
if (pfile_in_zip_read_info->rest_read_uncompressed == 0) |
|
1266 |
{ |
|
1267 |
if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait) |
|
1268 |
err=UNZ_CRCERROR; |
|
1269 |
} |
|
1270 |
||
1271 |
||
1272 |
TRYFREE(pfile_in_zip_read_info->read_buffer); |
|
1273 |
pfile_in_zip_read_info->read_buffer = NULL; |
|
1274 |
if (pfile_in_zip_read_info->stream_initialised) |
|
1275 |
inflateEnd(&pfile_in_zip_read_info->stream); |
|
1276 |
||
1277 |
pfile_in_zip_read_info->stream_initialised = 0; |
|
1278 |
TRYFREE(pfile_in_zip_read_info); |
|
1279 |
||
1280 |
s->pfile_in_zip_read=NULL; |
|
1281 |
||
1282 |
return err; |
|
1283 |
} |
|
1284 |
||
1285 |
||
1286 |
/* |
|
1287 |
Get the global comment string of the ZipFile, in the szComment buffer. |
|
1288 |
uSizeBuf is the size of the szComment buffer. |
|
1289 |
return the number of byte copied or an error code <0 |
|
1290 |
*/ |
|