author | nemo |
Mon, 10 Apr 2017 09:05:16 -0400 | |
changeset 12217 | ea891871f481 |
parent 10017 | de822cd3df3a |
child 12218 | bb5522e88ab2 |
permissions | -rw-r--r-- |
7768 | 1 |
/* |
2 |
* ZIP support routines for PhysicsFS. |
|
3 |
* |
|
4 |
* Please see the file LICENSE.txt in the source's root directory. |
|
5 |
* |
|
6 |
* This file written by Ryan C. Gordon, with some peeking at "unzip.c" |
|
7 |
* by Gilles Vollant. |
|
8 |
*/ |
|
9 |
||
10 |
#define __PHYSICSFS_INTERNAL__ |
|
11 |
#include "physfs_internal.h" |
|
12 |
||
13 |
#if PHYSFS_SUPPORTS_ZIP |
|
14 |
||
15 |
#include <errno.h> |
|
16 |
#include <time.h> |
|
17 |
||
18 |
#define USE_MINIZ 1 |
|
19 |
#if USE_MINIZ |
|
20 |
#include "physfs_miniz.h" |
|
21 |
#else |
|
22 |
#include <zlib.h> |
|
23 |
#endif |
|
24 |
||
25 |
/* |
|
26 |
* A buffer of ZIP_READBUFSIZE is allocated for each compressed file opened, |
|
27 |
* and is freed when you close the file; compressed data is read into |
|
28 |
* this buffer, and then is decompressed into the buffer passed to |
|
29 |
* PHYSFS_read(). |
|
30 |
* |
|
31 |
* Uncompressed entries in a zipfile do not allocate this buffer; they just |
|
32 |
* read data directly into the buffer passed to PHYSFS_read(). |
|
33 |
* |
|
34 |
* Depending on your speed and memory requirements, you should tweak this |
|
35 |
* value. |
|
36 |
*/ |
|
37 |
#define ZIP_READBUFSIZE (16 * 1024) |
|
38 |
||
39 |
||
40 |
/* |
|
41 |
* Entries are "unresolved" until they are first opened. At that time, |
|
42 |
* local file headers parsed/validated, data offsets will be updated to look |
|
43 |
* at the actual file data instead of the header, and symlinks will be |
|
44 |
* followed and optimized. This means that we don't seek and read around the |
|
45 |
* archive until forced to do so, and after the first time, we had to do |
|
46 |
* less reading and parsing, which is very CD-ROM friendly. |
|
47 |
*/ |
|
48 |
typedef enum |
|
49 |
{ |
|
50 |
ZIP_UNRESOLVED_FILE, |
|
51 |
ZIP_UNRESOLVED_SYMLINK, |
|
52 |
ZIP_RESOLVING, |
|
53 |
ZIP_RESOLVED, |
|
54 |
ZIP_BROKEN_FILE, |
|
55 |
ZIP_BROKEN_SYMLINK |
|
56 |
} ZipResolveType; |
|
57 |
||
58 |
||
59 |
/* |
|
60 |
* One ZIPentry is kept for each file in an open ZIP archive. |
|
61 |
*/ |
|
62 |
typedef struct _ZIPentry |
|
63 |
{ |
|
64 |
char *name; /* Name of file in archive */ |
|
65 |
struct _ZIPentry *symlink; /* NULL or file we symlink to */ |
|
66 |
ZipResolveType resolved; /* Have we resolved file/symlink? */ |
|
67 |
PHYSFS_uint64 offset; /* offset of data in archive */ |
|
68 |
PHYSFS_uint16 version; /* version made by */ |
|
69 |
PHYSFS_uint16 version_needed; /* version needed to extract */ |
|
70 |
PHYSFS_uint16 compression_method; /* compression method */ |
|
71 |
PHYSFS_uint32 crc; /* crc-32 */ |
|
72 |
PHYSFS_uint64 compressed_size; /* compressed size */ |
|
73 |
PHYSFS_uint64 uncompressed_size; /* uncompressed size */ |
|
74 |
PHYSFS_sint64 last_mod_time; /* last file mod time */ |
|
75 |
} ZIPentry; |
|
76 |
||
77 |
/* |
|
78 |
* One ZIPinfo is kept for each open ZIP archive. |
|
79 |
*/ |
|
80 |
typedef struct |
|
81 |
{ |
|
82 |
PHYSFS_Io *io; |
|
83 |
int zip64; /* non-zero if this is a Zip64 archive. */ |
|
84 |
PHYSFS_uint64 entryCount; /* Number of files in ZIP. */ |
|
85 |
ZIPentry *entries; /* info on all files in ZIP. */ |
|
86 |
} ZIPinfo; |
|
87 |
||
88 |
/* |
|
89 |
* One ZIPfileinfo is kept for each open file in a ZIP archive. |
|
90 |
*/ |
|
91 |
typedef struct |
|
92 |
{ |
|
93 |
ZIPentry *entry; /* Info on file. */ |
|
94 |
PHYSFS_Io *io; /* physical file handle. */ |
|
95 |
PHYSFS_uint32 compressed_position; /* offset in compressed data. */ |
|
96 |
PHYSFS_uint32 uncompressed_position; /* tell() position. */ |
|
97 |
PHYSFS_uint8 *buffer; /* decompression buffer. */ |
|
98 |
z_stream stream; /* zlib stream state. */ |
|
99 |
} ZIPfileinfo; |
|
100 |
||
101 |
||
102 |
/* Magic numbers... */ |
|
103 |
#define ZIP_LOCAL_FILE_SIG 0x04034b50 |
|
104 |
#define ZIP_CENTRAL_DIR_SIG 0x02014b50 |
|
105 |
#define ZIP_END_OF_CENTRAL_DIR_SIG 0x06054b50 |
|
106 |
#define ZIP64_END_OF_CENTRAL_DIR_SIG 0x06064b50 |
|
107 |
#define ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIG 0x07064b50 |
|
108 |
#define ZIP64_EXTENDED_INFO_EXTRA_FIELD_SIG 0x0001 |
|
109 |
||
110 |
/* compression methods... */ |
|
111 |
#define COMPMETH_NONE 0 |
|
112 |
/* ...and others... */ |
|
113 |
||
114 |
||
115 |
#define UNIX_FILETYPE_MASK 0170000 |
|
116 |
#define UNIX_FILETYPE_SYMLINK 0120000 |
|
117 |
||
118 |
||
119 |
/* |
|
120 |
* Bridge physfs allocation functions to zlib's format... |
|
121 |
*/ |
|
122 |
static voidpf zlibPhysfsAlloc(voidpf opaque, uInt items, uInt size) |
|
123 |
{ |
|
124 |
return ((PHYSFS_Allocator *) opaque)->Malloc(items * size); |
|
125 |
} /* zlibPhysfsAlloc */ |
|
126 |
||
127 |
/* |
|
128 |
* Bridge physfs allocation functions to zlib's format... |
|
129 |
*/ |
|
130 |
static void zlibPhysfsFree(voidpf opaque, voidpf address) |
|
131 |
{ |
|
132 |
((PHYSFS_Allocator *) opaque)->Free(address); |
|
133 |
} /* zlibPhysfsFree */ |
|
134 |
||
135 |
||
136 |
/* |
|
137 |
* Construct a new z_stream to a sane state. |
|
138 |
*/ |
|
139 |
static void initializeZStream(z_stream *pstr) |
|
140 |
{ |
|
141 |
memset(pstr, '\0', sizeof (z_stream)); |
|
142 |
pstr->zalloc = zlibPhysfsAlloc; |
|
143 |
pstr->zfree = zlibPhysfsFree; |
|
144 |
pstr->opaque = &allocator; |
|
145 |
} /* initializeZStream */ |
|
146 |
||
147 |
||
148 |
static PHYSFS_ErrorCode zlib_error_code(int rc) |
|
149 |
{ |
|
150 |
switch (rc) |
|
151 |
{ |
|
152 |
case Z_OK: return PHYSFS_ERR_OK; /* not an error. */ |
|
153 |
case Z_STREAM_END: return PHYSFS_ERR_OK; /* not an error. */ |
|
154 |
case Z_ERRNO: return PHYSFS_ERR_IO; |
|
155 |
case Z_MEM_ERROR: return PHYSFS_ERR_OUT_OF_MEMORY; |
|
156 |
default: return PHYSFS_ERR_CORRUPT; |
|
157 |
} /* switch */ |
|
158 |
} /* zlib_error_string */ |
|
159 |
||
160 |
||
161 |
/* |
|
162 |
* Wrap all zlib calls in this, so the physfs error state is set appropriately. |
|
163 |
*/ |
|
164 |
static int zlib_err(const int rc) |
|
165 |
{ |
|
166 |
__PHYSFS_setError(zlib_error_code(rc)); |
|
167 |
return rc; |
|
168 |
} /* zlib_err */ |
|
169 |
||
170 |
||
171 |
/* |
|
172 |
* Read an unsigned 64-bit int and swap to native byte order. |
|
173 |
*/ |
|
174 |
static int readui64(PHYSFS_Io *io, PHYSFS_uint64 *val) |
|
175 |
{ |
|
176 |
PHYSFS_uint64 v; |
|
177 |
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &v, sizeof (v)), ERRPASS, 0); |
|
178 |
*val = PHYSFS_swapULE64(v); |
|
179 |
return 1; |
|
180 |
} /* readui64 */ |
|
181 |
||
182 |
/* |
|
183 |
* Read an unsigned 32-bit int and swap to native byte order. |
|
184 |
*/ |
|
185 |
static int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val) |
|
186 |
{ |
|
187 |
PHYSFS_uint32 v; |
|
188 |
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &v, sizeof (v)), ERRPASS, 0); |
|
189 |
*val = PHYSFS_swapULE32(v); |
|
190 |
return 1; |
|
191 |
} /* readui32 */ |
|
192 |
||
193 |
||
194 |
/* |
|
195 |
* Read an unsigned 16-bit int and swap to native byte order. |
|
196 |
*/ |
|
197 |
static int readui16(PHYSFS_Io *io, PHYSFS_uint16 *val) |
|
198 |
{ |
|
199 |
PHYSFS_uint16 v; |
|
200 |
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &v, sizeof (v)), ERRPASS, 0); |
|
201 |
*val = PHYSFS_swapULE16(v); |
|
202 |
return 1; |
|
203 |
} /* readui16 */ |
|
204 |
||
205 |
||
206 |
static PHYSFS_sint64 ZIP_read(PHYSFS_Io *_io, void *buf, PHYSFS_uint64 len) |
|
207 |
{ |
|
208 |
ZIPfileinfo *finfo = (ZIPfileinfo *) _io->opaque; |
|
209 |
PHYSFS_Io *io = finfo->io; |
|
210 |
ZIPentry *entry = finfo->entry; |
|
211 |
PHYSFS_sint64 retval = 0; |
|
212 |
PHYSFS_sint64 maxread = (PHYSFS_sint64) len; |
|
213 |
PHYSFS_sint64 avail = entry->uncompressed_size - |
|
214 |
finfo->uncompressed_position; |
|
215 |
||
216 |
if (avail < maxread) |
|
217 |
maxread = avail; |
|
218 |
||
219 |
BAIL_IF_MACRO(maxread == 0, ERRPASS, 0); /* quick rejection. */ |
|
220 |
||
221 |
if (entry->compression_method == COMPMETH_NONE) |
|
222 |
retval = io->read(io, buf, maxread); |
|
223 |
else |
|
224 |
{ |
|
225 |
finfo->stream.next_out = buf; |
|
226 |
finfo->stream.avail_out = (uInt) maxread; |
|
227 |
||
228 |
while (retval < maxread) |
|
229 |
{ |
|
230 |
PHYSFS_uint32 before = finfo->stream.total_out; |
|
231 |
int rc; |
|
232 |
||
233 |
if (finfo->stream.avail_in == 0) |
|
234 |
{ |
|
235 |
PHYSFS_sint64 br; |
|
236 |
||
237 |
br = entry->compressed_size - finfo->compressed_position; |
|
238 |
if (br > 0) |
|
239 |
{ |
|
240 |
if (br > ZIP_READBUFSIZE) |
|
241 |
br = ZIP_READBUFSIZE; |
|
242 |
||
243 |
br = io->read(io, finfo->buffer, (PHYSFS_uint64) br); |
|
244 |
if (br <= 0) |
|
245 |
break; |
|
246 |
||
247 |
finfo->compressed_position += (PHYSFS_uint32) br; |
|
248 |
finfo->stream.next_in = finfo->buffer; |
|
249 |
finfo->stream.avail_in = (PHYSFS_uint32) br; |
|
250 |
} /* if */ |
|
251 |
} /* if */ |
|
252 |
||
253 |
rc = zlib_err(inflate(&finfo->stream, Z_SYNC_FLUSH)); |
|
254 |
retval += (finfo->stream.total_out - before); |
|
255 |
||
256 |
if (rc != Z_OK) |
|
257 |
break; |
|
258 |
} /* while */ |
|
259 |
} /* else */ |
|
260 |
||
261 |
if (retval > 0) |
|
262 |
finfo->uncompressed_position += (PHYSFS_uint32) retval; |
|
263 |
||
264 |
return retval; |
|
265 |
} /* ZIP_read */ |
|
266 |
||
267 |
||
268 |
static PHYSFS_sint64 ZIP_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len) |
|
269 |
{ |
|
270 |
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, -1); |
|
271 |
} /* ZIP_write */ |
|
272 |
||
273 |
||
274 |
static PHYSFS_sint64 ZIP_tell(PHYSFS_Io *io) |
|
275 |
{ |
|
276 |
return ((ZIPfileinfo *) io->opaque)->uncompressed_position; |
|
277 |
} /* ZIP_tell */ |
|
278 |
||
279 |
||
280 |
static int ZIP_seek(PHYSFS_Io *_io, PHYSFS_uint64 offset) |
|
281 |
{ |
|
282 |
ZIPfileinfo *finfo = (ZIPfileinfo *) _io->opaque; |
|
283 |
ZIPentry *entry = finfo->entry; |
|
284 |
PHYSFS_Io *io = finfo->io; |
|
285 |
||
286 |
BAIL_IF_MACRO(offset > entry->uncompressed_size, PHYSFS_ERR_PAST_EOF, 0); |
|
287 |
||
288 |
if (entry->compression_method == COMPMETH_NONE) |
|
289 |
{ |
|
290 |
const PHYSFS_sint64 newpos = offset + entry->offset; |
|
291 |
BAIL_IF_MACRO(!io->seek(io, newpos), ERRPASS, 0); |
|
292 |
finfo->uncompressed_position = (PHYSFS_uint32) offset; |
|
293 |
} /* if */ |
|
294 |
||
295 |
else |
|
296 |
{ |
|
297 |
/* |
|
298 |
* If seeking backwards, we need to redecode the file |
|
299 |
* from the start and throw away the compressed bits until we hit |
|
300 |
* the offset we need. If seeking forward, we still need to |
|
301 |
* decode, but we don't rewind first. |
|
302 |
*/ |
|
303 |
if (offset < finfo->uncompressed_position) |
|
304 |
{ |
|
305 |
/* we do a copy so state is sane if inflateInit2() fails. */ |
|
306 |
z_stream str; |
|
307 |
initializeZStream(&str); |
|
308 |
if (zlib_err(inflateInit2(&str, -MAX_WBITS)) != Z_OK) |
|
309 |
return 0; |
|
310 |
||
311 |
if (!io->seek(io, entry->offset)) |
|
312 |
return 0; |
|
313 |
||
314 |
inflateEnd(&finfo->stream); |
|
12217
ea891871f481
merge in patch used in arch/fedora - fixes a lazy physfs memcpy that was breaking upstream's stricter checking
nemo
parents:
10017
diff
changeset
|
315 |
inflateCopy(&finfo->stream, &str); |
ea891871f481
merge in patch used in arch/fedora - fixes a lazy physfs memcpy that was breaking upstream's stricter checking
nemo
parents:
10017
diff
changeset
|
316 |
inflateEnd(&str); |
7768 | 317 |
finfo->uncompressed_position = finfo->compressed_position = 0; |
318 |
} /* if */ |
|
319 |
||
320 |
while (finfo->uncompressed_position != offset) |
|
321 |
{ |
|
322 |
PHYSFS_uint8 buf[512]; |
|
323 |
PHYSFS_uint32 maxread; |
|
324 |
||
325 |
maxread = (PHYSFS_uint32) (offset - finfo->uncompressed_position); |
|
326 |
if (maxread > sizeof (buf)) |
|
327 |
maxread = sizeof (buf); |
|
328 |
||
329 |
if (ZIP_read(_io, buf, maxread) != maxread) |
|
330 |
return 0; |
|
331 |
} /* while */ |
|
332 |
} /* else */ |
|
333 |
||
334 |
return 1; |
|
335 |
} /* ZIP_seek */ |
|
336 |
||
337 |
||
338 |
static PHYSFS_sint64 ZIP_length(PHYSFS_Io *io) |
|
339 |
{ |
|
340 |
const ZIPfileinfo *finfo = (ZIPfileinfo *) io->opaque; |
|
341 |
return (PHYSFS_sint64) finfo->entry->uncompressed_size; |
|
342 |
} /* ZIP_length */ |
|
343 |
||
344 |
||
345 |
static PHYSFS_Io *zip_get_io(PHYSFS_Io *io, ZIPinfo *inf, ZIPentry *entry); |
|
346 |
||
347 |
static PHYSFS_Io *ZIP_duplicate(PHYSFS_Io *io) |
|
348 |
{ |
|
349 |
ZIPfileinfo *origfinfo = (ZIPfileinfo *) io->opaque; |
|
350 |
PHYSFS_Io *retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io)); |
|
351 |
ZIPfileinfo *finfo = (ZIPfileinfo *) allocator.Malloc(sizeof (ZIPfileinfo)); |
|
352 |
GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, failed); |
|
353 |
GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, failed); |
|
354 |
memset(finfo, '\0', sizeof (*finfo)); |
|
355 |
||
356 |
finfo->entry = origfinfo->entry; |
|
357 |
finfo->io = zip_get_io(origfinfo->io, NULL, finfo->entry); |
|
358 |
GOTO_IF_MACRO(!finfo->io, ERRPASS, failed); |
|
359 |
||
360 |
if (finfo->entry->compression_method != COMPMETH_NONE) |
|
361 |
{ |
|
362 |
finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE); |
|
363 |
GOTO_IF_MACRO(!finfo->buffer, PHYSFS_ERR_OUT_OF_MEMORY, failed); |
|
364 |
if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK) |
|
365 |
goto failed; |
|
366 |
} /* if */ |
|
367 |
||
368 |
memcpy(retval, io, sizeof (PHYSFS_Io)); |
|
369 |
retval->opaque = finfo; |
|
370 |
return retval; |
|
371 |
||
372 |
failed: |
|
373 |
if (finfo != NULL) |
|
374 |
{ |
|
375 |
if (finfo->io != NULL) |
|
376 |
finfo->io->destroy(finfo->io); |
|
377 |
||
378 |
if (finfo->buffer != NULL) |
|
379 |
{ |
|
380 |
allocator.Free(finfo->buffer); |
|
381 |
inflateEnd(&finfo->stream); |
|
382 |
} /* if */ |
|
383 |
||
384 |
allocator.Free(finfo); |
|
385 |
} /* if */ |
|
386 |
||
387 |
if (retval != NULL) |
|
388 |
allocator.Free(retval); |
|
389 |
||
390 |
return NULL; |
|
391 |
} /* ZIP_duplicate */ |
|
392 |
||
393 |
static int ZIP_flush(PHYSFS_Io *io) { return 1; /* no write support. */ } |
|
394 |
||
395 |
static void ZIP_destroy(PHYSFS_Io *io) |
|
396 |
{ |
|
397 |
ZIPfileinfo *finfo = (ZIPfileinfo *) io->opaque; |
|
398 |
finfo->io->destroy(finfo->io); |
|
399 |
||
400 |
if (finfo->entry->compression_method != COMPMETH_NONE) |
|
401 |
inflateEnd(&finfo->stream); |
|
402 |
||
403 |
if (finfo->buffer != NULL) |
|
404 |
allocator.Free(finfo->buffer); |
|
405 |
||
406 |
allocator.Free(finfo); |
|
407 |
allocator.Free(io); |
|
408 |
} /* ZIP_destroy */ |
|
409 |
||
410 |
||
411 |
static const PHYSFS_Io ZIP_Io = |
|
412 |
{ |
|
413 |
CURRENT_PHYSFS_IO_API_VERSION, NULL, |
|
414 |
ZIP_read, |
|
415 |
ZIP_write, |
|
416 |
ZIP_seek, |
|
417 |
ZIP_tell, |
|
418 |
ZIP_length, |
|
419 |
ZIP_duplicate, |
|
420 |
ZIP_flush, |
|
421 |
ZIP_destroy |
|
422 |
}; |
|
423 |
||
424 |
||
425 |
||
426 |
static PHYSFS_sint64 zip_find_end_of_central_dir(PHYSFS_Io *io, PHYSFS_sint64 *len) |
|
427 |
{ |
|
428 |
PHYSFS_uint8 buf[256]; |
|
429 |
PHYSFS_uint8 extra[4] = { 0, 0, 0, 0 }; |
|
430 |
PHYSFS_sint32 i = 0; |
|
431 |
PHYSFS_sint64 filelen; |
|
432 |
PHYSFS_sint64 filepos; |
|
433 |
PHYSFS_sint32 maxread; |
|
434 |
PHYSFS_sint32 totalread = 0; |
|
435 |
int found = 0; |
|
436 |
||
437 |
filelen = io->length(io); |
|
438 |
BAIL_IF_MACRO(filelen == -1, ERRPASS, 0); |
|
439 |
||
440 |
/* |
|
441 |
* Jump to the end of the file and start reading backwards. |
|
442 |
* The last thing in the file is the zipfile comment, which is variable |
|
443 |
* length, and the field that specifies its size is before it in the |
|
444 |
* file (argh!)...this means that we need to scan backwards until we |
|
445 |
* hit the end-of-central-dir signature. We can then sanity check that |
|
446 |
* the comment was as big as it should be to make sure we're in the |
|
447 |
* right place. The comment length field is 16 bits, so we can stop |
|
448 |
* searching for that signature after a little more than 64k at most, |
|
449 |
* and call it a corrupted zipfile. |
|
450 |
*/ |
|
451 |
||
452 |
if (sizeof (buf) < filelen) |
|
453 |
{ |
|
454 |
filepos = filelen - sizeof (buf); |
|
455 |
maxread = sizeof (buf); |
|
456 |
} /* if */ |
|
457 |
else |
|
458 |
{ |
|
459 |
filepos = 0; |
|
460 |
maxread = (PHYSFS_uint32) filelen; |
|
461 |
} /* else */ |
|
462 |
||
463 |
while ((totalread < filelen) && (totalread < 65557)) |
|
464 |
{ |
|
465 |
BAIL_IF_MACRO(!io->seek(io, filepos), ERRPASS, -1); |
|
466 |
||
467 |
/* make sure we catch a signature between buffers. */ |
|
468 |
if (totalread != 0) |
|
469 |
{ |
|
470 |
if (!__PHYSFS_readAll(io, buf, maxread - 4)) |
|
471 |
return -1; |
|
472 |
memcpy(&buf[maxread - 4], &extra, sizeof (extra)); |
|
473 |
totalread += maxread - 4; |
|
474 |
} /* if */ |
|
475 |
else |
|
476 |
{ |
|
477 |
if (!__PHYSFS_readAll(io, buf, maxread)) |
|
478 |
return -1; |
|
479 |
totalread += maxread; |
|
480 |
} /* else */ |
|
481 |
||
482 |
memcpy(&extra, buf, sizeof (extra)); |
|
483 |
||
484 |
for (i = maxread - 4; i > 0; i--) |
|
485 |
{ |
|
486 |
if ((buf[i + 0] == 0x50) && |
|
487 |
(buf[i + 1] == 0x4B) && |
|
488 |
(buf[i + 2] == 0x05) && |
|
489 |
(buf[i + 3] == 0x06) ) |
|
490 |
{ |
|
491 |
found = 1; /* that's the signature! */ |
|
10017 | 492 |
break; |
7768 | 493 |
} /* if */ |
494 |
} /* for */ |
|
495 |
||
496 |
if (found) |
|
497 |
break; |
|
498 |
||
499 |
filepos -= (maxread - 4); |
|
500 |
if (filepos < 0) |
|
501 |
filepos = 0; |
|
502 |
} /* while */ |
|
503 |
||
504 |
BAIL_IF_MACRO(!found, PHYSFS_ERR_UNSUPPORTED, -1); |
|
505 |
||
506 |
if (len != NULL) |
|
507 |
*len = filelen; |
|
508 |
||
509 |
return (filepos + i); |
|
510 |
} /* zip_find_end_of_central_dir */ |
|
511 |
||
512 |
||
513 |
static int isZip(PHYSFS_Io *io) |
|
514 |
{ |
|
515 |
PHYSFS_uint32 sig = 0; |
|
516 |
int retval = 0; |
|
517 |
||
518 |
/* |
|
519 |
* The first thing in a zip file might be the signature of the |
|
520 |
* first local file record, so it makes for a quick determination. |
|
521 |
*/ |
|
522 |
if (readui32(io, &sig)) |
|
523 |
{ |
|
524 |
retval = (sig == ZIP_LOCAL_FILE_SIG); |
|
525 |
if (!retval) |
|
526 |
{ |
|
527 |
/* |
|
528 |
* No sig...might be a ZIP with data at the start |
|
529 |
* (a self-extracting executable, etc), so we'll have to do |
|
530 |
* it the hard way... |
|
531 |
*/ |
|
532 |
retval = (zip_find_end_of_central_dir(io, NULL) != -1); |
|
533 |
} /* if */ |
|
534 |
} /* if */ |
|
535 |
||
536 |
return retval; |
|
537 |
} /* isZip */ |
|
538 |
||
539 |
||
540 |
static void zip_free_entries(ZIPentry *entries, PHYSFS_uint64 max) |
|
541 |
{ |
|
542 |
PHYSFS_uint64 i; |
|
543 |
for (i = 0; i < max; i++) |
|
544 |
{ |
|
545 |
ZIPentry *entry = &entries[i]; |
|
546 |
if (entry->name != NULL) |
|
547 |
allocator.Free(entry->name); |
|
548 |
} /* for */ |
|
549 |
||
550 |
allocator.Free(entries); |
|
551 |
} /* zip_free_entries */ |
|
552 |
||
553 |
||
554 |
/* |
|
555 |
* This will find the ZIPentry associated with a path in platform-independent |
|
10017 | 556 |
* notation. Directories don't have ZIPentries associated with them, but |
7768 | 557 |
* (*isDir) will be set to non-zero if a dir was hit. |
558 |
*/ |
|
559 |
static ZIPentry *zip_find_entry(const ZIPinfo *info, const char *path, |
|
560 |
int *isDir) |
|
561 |
{ |
|
562 |
ZIPentry *a = info->entries; |
|
563 |
PHYSFS_sint32 pathlen = (PHYSFS_sint32) strlen(path); |
|
564 |
PHYSFS_sint64 lo = 0; |
|
565 |
PHYSFS_sint64 hi = (PHYSFS_sint64) (info->entryCount - 1); |
|
566 |
PHYSFS_sint64 middle; |
|
567 |
const char *thispath = NULL; |
|
568 |
int rc; |
|
569 |
||
570 |
while (lo <= hi) |
|
571 |
{ |
|
572 |
middle = lo + ((hi - lo) / 2); |
|
573 |
thispath = a[middle].name; |
|
574 |
rc = strncmp(path, thispath, pathlen); |
|
575 |
||
576 |
if (rc > 0) |
|
577 |
lo = middle + 1; |
|
578 |
||
579 |
else if (rc < 0) |
|
580 |
hi = middle - 1; |
|
581 |
||
582 |
else /* substring match...might be dir or entry or nothing. */ |
|
583 |
{ |
|
584 |
if (isDir != NULL) |
|
585 |
{ |
|
586 |
*isDir = (thispath[pathlen] == '/'); |
|
587 |
if (*isDir) |
|
588 |
return NULL; |
|
589 |
} /* if */ |
|
590 |
||
591 |
if (thispath[pathlen] == '\0') /* found entry? */ |
|
592 |
return &a[middle]; |
|
593 |
/* adjust search params, try again. */ |
|
594 |
else if (thispath[pathlen] > '/') |
|
595 |
hi = middle - 1; |
|
596 |
else |
|
597 |
lo = middle + 1; |
|
598 |
} /* if */ |
|
599 |
} /* while */ |
|
600 |
||
601 |
if (isDir != NULL) |
|
602 |
*isDir = 0; |
|
603 |
||
604 |
BAIL_MACRO(PHYSFS_ERR_NO_SUCH_PATH, NULL); |
|
605 |
} /* zip_find_entry */ |
|
606 |
||
607 |
||
608 |
/* Convert paths from old, buggy DOS zippers... */ |
|
609 |
static void zip_convert_dos_path(ZIPentry *entry, char *path) |
|
610 |
{ |
|
611 |
PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((entry->version >> 8) & 0xFF); |
|
612 |
if (hosttype == 0) /* FS_FAT_ */ |
|
613 |
{ |
|
614 |
while (*path) |
|
615 |
{ |
|
616 |
if (*path == '\\') |
|
617 |
*path = '/'; |
|
618 |
path++; |
|
619 |
} /* while */ |
|
620 |
} /* if */ |
|
621 |
} /* zip_convert_dos_path */ |
|
622 |
||
623 |
||
624 |
static void zip_expand_symlink_path(char *path) |
|
625 |
{ |
|
626 |
char *ptr = path; |
|
627 |
char *prevptr = path; |
|
628 |
||
629 |
while (1) |
|
630 |
{ |
|
631 |
ptr = strchr(ptr, '/'); |
|
632 |
if (ptr == NULL) |
|
633 |
break; |
|
634 |
||
635 |
if (*(ptr + 1) == '.') |
|
636 |
{ |
|
637 |
if (*(ptr + 2) == '/') |
|
638 |
{ |
|
639 |
/* current dir in middle of string: ditch it. */ |
|
640 |
memmove(ptr, ptr + 2, strlen(ptr + 2) + 1); |
|
641 |
} /* else if */ |
|
642 |
||
643 |
else if (*(ptr + 2) == '\0') |
|
644 |
{ |
|
645 |
/* current dir at end of string: ditch it. */ |
|
646 |
*ptr = '\0'; |
|
647 |
} /* else if */ |
|
648 |
||
649 |
else if (*(ptr + 2) == '.') |
|
650 |
{ |
|
651 |
if (*(ptr + 3) == '/') |
|
652 |
{ |
|
653 |
/* parent dir in middle: move back one, if possible. */ |
|
654 |
memmove(prevptr, ptr + 4, strlen(ptr + 4) + 1); |
|
655 |
ptr = prevptr; |
|
656 |
while (prevptr != path) |
|
657 |
{ |
|
658 |
prevptr--; |
|
659 |
if (*prevptr == '/') |
|
660 |
{ |
|
661 |
prevptr++; |
|
662 |
break; |
|
663 |
} /* if */ |
|
664 |
} /* while */ |
|
665 |
} /* if */ |
|
666 |
||
667 |
if (*(ptr + 3) == '\0') |
|
668 |
{ |
|
669 |
/* parent dir at end: move back one, if possible. */ |
|
670 |
*prevptr = '\0'; |
|
671 |
} /* if */ |
|
672 |
} /* if */ |
|
673 |
} /* if */ |
|
674 |
else |
|
675 |
{ |
|
676 |
prevptr = ptr; |
|
677 |
ptr++; |
|
678 |
} /* else */ |
|
679 |
} /* while */ |
|
680 |
} /* zip_expand_symlink_path */ |
|
681 |
||
682 |
/* (forward reference: zip_follow_symlink and zip_resolve call each other.) */ |
|
683 |
static int zip_resolve(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry); |
|
684 |
||
685 |
/* |
|
686 |
* Look for the entry named by (path). If it exists, resolve it, and return |
|
687 |
* a pointer to that entry. If it's another symlink, keep resolving until you |
|
688 |
* hit a real file and then return a pointer to the final non-symlink entry. |
|
689 |
* If there's a problem, return NULL. |
|
690 |
*/ |
|
691 |
static ZIPentry *zip_follow_symlink(PHYSFS_Io *io, ZIPinfo *info, char *path) |
|
692 |
{ |
|
693 |
ZIPentry *entry; |
|
694 |
||
695 |
zip_expand_symlink_path(path); |
|
696 |
entry = zip_find_entry(info, path, NULL); |
|
697 |
if (entry != NULL) |
|
698 |
{ |
|
699 |
if (!zip_resolve(io, info, entry)) /* recursive! */ |
|
700 |
entry = NULL; |
|
701 |
else |
|
702 |
{ |
|
703 |
if (entry->symlink != NULL) |
|
704 |
entry = entry->symlink; |
|
705 |
} /* else */ |
|
706 |
} /* if */ |
|
707 |
||
708 |
return entry; |
|
709 |
} /* zip_follow_symlink */ |
|
710 |
||
711 |
||
712 |
static int zip_resolve_symlink(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry) |
|
713 |
{ |
|
714 |
const PHYSFS_uint64 size = entry->uncompressed_size; |
|
715 |
char *path = NULL; |
|
716 |
int rc = 0; |
|
717 |
||
718 |
/* |
|
719 |
* We've already parsed the local file header of the symlink at this |
|
720 |
* point. Now we need to read the actual link from the file data and |
|
721 |
* follow it. |
|
722 |
*/ |
|
723 |
||
724 |
BAIL_IF_MACRO(!io->seek(io, entry->offset), ERRPASS, 0); |
|
725 |
||
726 |
path = (char *) __PHYSFS_smallAlloc(size + 1); |
|
727 |
BAIL_IF_MACRO(!path, PHYSFS_ERR_OUT_OF_MEMORY, 0); |
|
10017 | 728 |
|
7768 | 729 |
if (entry->compression_method == COMPMETH_NONE) |
730 |
rc = __PHYSFS_readAll(io, path, size); |
|
731 |
||
732 |
else /* symlink target path is compressed... */ |
|
733 |
{ |
|
734 |
z_stream stream; |
|
735 |
const PHYSFS_uint64 complen = entry->compressed_size; |
|
736 |
PHYSFS_uint8 *compressed = (PHYSFS_uint8*) __PHYSFS_smallAlloc(complen); |
|
737 |
if (compressed != NULL) |
|
738 |
{ |
|
739 |
if (__PHYSFS_readAll(io, compressed, complen)) |
|
740 |
{ |
|
741 |
initializeZStream(&stream); |
|
742 |
stream.next_in = compressed; |
|
743 |
stream.avail_in = complen; |
|
744 |
stream.next_out = (unsigned char *) path; |
|
745 |
stream.avail_out = size; |
|
746 |
if (zlib_err(inflateInit2(&stream, -MAX_WBITS)) == Z_OK) |
|
747 |
{ |
|
748 |
rc = zlib_err(inflate(&stream, Z_FINISH)); |
|
749 |
inflateEnd(&stream); |
|
750 |
||
751 |
/* both are acceptable outcomes... */ |
|
752 |
rc = ((rc == Z_OK) || (rc == Z_STREAM_END)); |
|
753 |
} /* if */ |
|
754 |
} /* if */ |
|
755 |
__PHYSFS_smallFree(compressed); |
|
756 |
} /* if */ |
|
757 |
} /* else */ |
|
758 |
||
759 |
if (rc) |
|
760 |
{ |
|
761 |
path[entry->uncompressed_size] = '\0'; /* null-terminate it. */ |
|
762 |
zip_convert_dos_path(entry, path); |
|
763 |
entry->symlink = zip_follow_symlink(io, info, path); |
|
764 |
} /* else */ |
|
765 |
||
766 |
__PHYSFS_smallFree(path); |
|
767 |
||
768 |
return (entry->symlink != NULL); |
|
769 |
} /* zip_resolve_symlink */ |
|
770 |
||
771 |
||
772 |
/* |
|
773 |
* Parse the local file header of an entry, and update entry->offset. |
|
774 |
*/ |
|
775 |
static int zip_parse_local(PHYSFS_Io *io, ZIPentry *entry) |
|
776 |
{ |
|
777 |
PHYSFS_uint32 ui32; |
|
778 |
PHYSFS_uint16 ui16; |
|
779 |
PHYSFS_uint16 fnamelen; |
|
780 |
PHYSFS_uint16 extralen; |
|
781 |
||
782 |
/* |
|
783 |
* crc and (un)compressed_size are always zero if this is a "JAR" |
|
784 |
* archive created with Sun's Java tools, apparently. We only |
|
785 |
* consider this archive corrupted if those entries don't match and |
|
786 |
* aren't zero. That seems to work well. |
|
787 |
* We also ignore a mismatch if the value is 0xFFFFFFFF here, since it's |
|
788 |
* possible that's a Zip64 thing. |
|
789 |
*/ |
|
790 |
||
791 |
BAIL_IF_MACRO(!io->seek(io, entry->offset), ERRPASS, 0); |
|
792 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
793 |
BAIL_IF_MACRO(ui32 != ZIP_LOCAL_FILE_SIG, PHYSFS_ERR_CORRUPT, 0); |
|
794 |
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); |
|
795 |
BAIL_IF_MACRO(ui16 != entry->version_needed, PHYSFS_ERR_CORRUPT, 0); |
|
796 |
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); /* general bits. */ |
|
797 |
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); |
|
798 |
BAIL_IF_MACRO(ui16 != entry->compression_method, PHYSFS_ERR_CORRUPT, 0); |
|
799 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); /* date/time */ |
|
800 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
801 |
BAIL_IF_MACRO(ui32 && (ui32 != entry->crc), PHYSFS_ERR_CORRUPT, 0); |
|
802 |
||
803 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
804 |
BAIL_IF_MACRO(ui32 && (ui32 != 0xFFFFFFFF) && |
|
805 |
(ui32 != entry->compressed_size), PHYSFS_ERR_CORRUPT, 0); |
|
806 |
||
807 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
808 |
BAIL_IF_MACRO(ui32 && (ui32 != 0xFFFFFFFF) && |
|
809 |
(ui32 != entry->uncompressed_size), PHYSFS_ERR_CORRUPT, 0); |
|
810 |
||
811 |
BAIL_IF_MACRO(!readui16(io, &fnamelen), ERRPASS, 0); |
|
812 |
BAIL_IF_MACRO(!readui16(io, &extralen), ERRPASS, 0); |
|
813 |
||
814 |
entry->offset += fnamelen + extralen + 30; |
|
815 |
return 1; |
|
816 |
} /* zip_parse_local */ |
|
817 |
||
818 |
||
819 |
static int zip_resolve(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry) |
|
820 |
{ |
|
821 |
int retval = 1; |
|
822 |
ZipResolveType resolve_type = entry->resolved; |
|
823 |
||
824 |
/* Don't bother if we've failed to resolve this entry before. */ |
|
825 |
BAIL_IF_MACRO(resolve_type == ZIP_BROKEN_FILE, PHYSFS_ERR_CORRUPT, 0); |
|
826 |
BAIL_IF_MACRO(resolve_type == ZIP_BROKEN_SYMLINK, PHYSFS_ERR_CORRUPT, 0); |
|
827 |
||
828 |
/* uhoh...infinite symlink loop! */ |
|
829 |
BAIL_IF_MACRO(resolve_type == ZIP_RESOLVING, PHYSFS_ERR_SYMLINK_LOOP, 0); |
|
830 |
||
831 |
/* |
|
832 |
* We fix up the offset to point to the actual data on the |
|
833 |
* first open, since we don't want to seek across the whole file on |
|
834 |
* archive open (can be SLOW on large, CD-stored files), but we |
|
835 |
* need to check the local file header...not just for corruption, |
|
836 |
* but since it stores offset info the central directory does not. |
|
837 |
*/ |
|
838 |
if (resolve_type != ZIP_RESOLVED) |
|
839 |
{ |
|
840 |
entry->resolved = ZIP_RESOLVING; |
|
841 |
||
842 |
retval = zip_parse_local(io, entry); |
|
843 |
if (retval) |
|
844 |
{ |
|
845 |
/* |
|
846 |
* If it's a symlink, find the original file. This will cause |
|
847 |
* resolution of other entries (other symlinks and, eventually, |
|
848 |
* the real file) if all goes well. |
|
849 |
*/ |
|
850 |
if (resolve_type == ZIP_UNRESOLVED_SYMLINK) |
|
851 |
retval = zip_resolve_symlink(io, info, entry); |
|
852 |
} /* if */ |
|
853 |
||
854 |
if (resolve_type == ZIP_UNRESOLVED_SYMLINK) |
|
855 |
entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_SYMLINK); |
|
856 |
else if (resolve_type == ZIP_UNRESOLVED_FILE) |
|
857 |
entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_FILE); |
|
858 |
} /* if */ |
|
859 |
||
860 |
return retval; |
|
861 |
} /* zip_resolve */ |
|
862 |
||
863 |
||
864 |
static int zip_version_does_symlinks(PHYSFS_uint32 version) |
|
865 |
{ |
|
866 |
int retval = 0; |
|
867 |
PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((version >> 8) & 0xFF); |
|
868 |
||
869 |
switch (hosttype) |
|
870 |
{ |
|
871 |
/* |
|
872 |
* These are the platforms that can NOT build an archive with |
|
873 |
* symlinks, according to the Info-ZIP project. |
|
874 |
*/ |
|
875 |
case 0: /* FS_FAT_ */ |
|
876 |
case 1: /* AMIGA_ */ |
|
877 |
case 2: /* VMS_ */ |
|
878 |
case 4: /* VM_CSM_ */ |
|
879 |
case 6: /* FS_HPFS_ */ |
|
880 |
case 11: /* FS_NTFS_ */ |
|
881 |
case 14: /* FS_VFAT_ */ |
|
882 |
case 13: /* ACORN_ */ |
|
883 |
case 15: /* MVS_ */ |
|
884 |
case 18: /* THEOS_ */ |
|
885 |
break; /* do nothing. */ |
|
886 |
||
887 |
default: /* assume the rest to be unix-like. */ |
|
888 |
retval = 1; |
|
889 |
break; |
|
890 |
} /* switch */ |
|
891 |
||
892 |
return retval; |
|
893 |
} /* zip_version_does_symlinks */ |
|
894 |
||
895 |
||
896 |
static int zip_entry_is_symlink(const ZIPentry *entry) |
|
897 |
{ |
|
898 |
return ((entry->resolved == ZIP_UNRESOLVED_SYMLINK) || |
|
899 |
(entry->resolved == ZIP_BROKEN_SYMLINK) || |
|
900 |
(entry->symlink)); |
|
901 |
} /* zip_entry_is_symlink */ |
|
902 |
||
903 |
||
904 |
static int zip_has_symlink_attr(ZIPentry *entry, PHYSFS_uint32 extern_attr) |
|
905 |
{ |
|
906 |
PHYSFS_uint16 xattr = ((extern_attr >> 16) & 0xFFFF); |
|
907 |
return ( (zip_version_does_symlinks(entry->version)) && |
|
908 |
(entry->uncompressed_size > 0) && |
|
909 |
((xattr & UNIX_FILETYPE_MASK) == UNIX_FILETYPE_SYMLINK) ); |
|
910 |
} /* zip_has_symlink_attr */ |
|
911 |
||
912 |
||
913 |
static PHYSFS_sint64 zip_dos_time_to_physfs_time(PHYSFS_uint32 dostime) |
|
914 |
{ |
|
915 |
PHYSFS_uint32 dosdate; |
|
916 |
struct tm unixtime; |
|
917 |
memset(&unixtime, '\0', sizeof (unixtime)); |
|
918 |
||
919 |
dosdate = (PHYSFS_uint32) ((dostime >> 16) & 0xFFFF); |
|
920 |
dostime &= 0xFFFF; |
|
921 |
||
922 |
/* dissect date */ |
|
923 |
unixtime.tm_year = ((dosdate >> 9) & 0x7F) + 80; |
|
924 |
unixtime.tm_mon = ((dosdate >> 5) & 0x0F) - 1; |
|
925 |
unixtime.tm_mday = ((dosdate ) & 0x1F); |
|
926 |
||
927 |
/* dissect time */ |
|
928 |
unixtime.tm_hour = ((dostime >> 11) & 0x1F); |
|
929 |
unixtime.tm_min = ((dostime >> 5) & 0x3F); |
|
930 |
unixtime.tm_sec = ((dostime << 1) & 0x3E); |
|
931 |
||
932 |
/* let mktime calculate daylight savings time. */ |
|
933 |
unixtime.tm_isdst = -1; |
|
934 |
||
935 |
return ((PHYSFS_sint64) mktime(&unixtime)); |
|
936 |
} /* zip_dos_time_to_physfs_time */ |
|
937 |
||
938 |
||
939 |
static int zip_load_entry(PHYSFS_Io *io, const int zip64, ZIPentry *entry, |
|
940 |
PHYSFS_uint64 ofs_fixup) |
|
941 |
{ |
|
942 |
PHYSFS_uint16 fnamelen, extralen, commentlen; |
|
943 |
PHYSFS_uint32 external_attr; |
|
944 |
PHYSFS_uint32 starting_disk; |
|
945 |
PHYSFS_uint64 offset; |
|
946 |
PHYSFS_uint16 ui16; |
|
947 |
PHYSFS_uint32 ui32; |
|
948 |
PHYSFS_sint64 si64; |
|
949 |
||
950 |
/* sanity check with central directory signature... */ |
|
951 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
952 |
BAIL_IF_MACRO(ui32 != ZIP_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, 0); |
|
953 |
||
954 |
/* Get the pertinent parts of the record... */ |
|
955 |
BAIL_IF_MACRO(!readui16(io, &entry->version), ERRPASS, 0); |
|
956 |
BAIL_IF_MACRO(!readui16(io, &entry->version_needed), ERRPASS, 0); |
|
957 |
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); /* general bits */ |
|
958 |
BAIL_IF_MACRO(!readui16(io, &entry->compression_method), ERRPASS, 0); |
|
959 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
960 |
entry->last_mod_time = zip_dos_time_to_physfs_time(ui32); |
|
961 |
BAIL_IF_MACRO(!readui32(io, &entry->crc), ERRPASS, 0); |
|
962 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
963 |
entry->compressed_size = (PHYSFS_uint64) ui32; |
|
964 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
965 |
entry->uncompressed_size = (PHYSFS_uint64) ui32; |
|
966 |
BAIL_IF_MACRO(!readui16(io, &fnamelen), ERRPASS, 0); |
|
967 |
BAIL_IF_MACRO(!readui16(io, &extralen), ERRPASS, 0); |
|
968 |
BAIL_IF_MACRO(!readui16(io, &commentlen), ERRPASS, 0); |
|
969 |
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); |
|
970 |
starting_disk = (PHYSFS_uint32) ui16; |
|
971 |
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); /* internal file attribs */ |
|
972 |
BAIL_IF_MACRO(!readui32(io, &external_attr), ERRPASS, 0); |
|
973 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
974 |
offset = (PHYSFS_uint64) ui32; |
|
975 |
||
976 |
entry->symlink = NULL; /* will be resolved later, if necessary. */ |
|
977 |
entry->resolved = (zip_has_symlink_attr(entry, external_attr)) ? |
|
978 |
ZIP_UNRESOLVED_SYMLINK : ZIP_UNRESOLVED_FILE; |
|
979 |
||
980 |
entry->name = (char *) allocator.Malloc(fnamelen + 1); |
|
981 |
BAIL_IF_MACRO(entry->name == NULL, PHYSFS_ERR_OUT_OF_MEMORY, 0); |
|
982 |
if (!__PHYSFS_readAll(io, entry->name, fnamelen)) |
|
983 |
goto zip_load_entry_puked; |
|
984 |
||
985 |
entry->name[fnamelen] = '\0'; /* null-terminate the filename. */ |
|
986 |
zip_convert_dos_path(entry, entry->name); |
|
987 |
||
988 |
si64 = io->tell(io); |
|
989 |
if (si64 == -1) |
|
990 |
goto zip_load_entry_puked; |
|
991 |
||
992 |
/* |
|
993 |
* The actual sizes didn't fit in 32-bits; look for the Zip64 |
|
994 |
* extended information extra field... |
|
995 |
*/ |
|
996 |
if ( (zip64) && |
|
997 |
((offset == 0xFFFFFFFF) || |
|
998 |
(starting_disk == 0xFFFFFFFF) || |
|
999 |
(entry->compressed_size == 0xFFFFFFFF) || |
|
1000 |
(entry->uncompressed_size == 0xFFFFFFFF)) ) |
|
1001 |
{ |
|
1002 |
int found = 0; |
|
1003 |
PHYSFS_uint16 sig, len; |
|
1004 |
while (extralen > 4) |
|
1005 |
{ |
|
1006 |
if (!readui16(io, &sig)) |
|
1007 |
goto zip_load_entry_puked; |
|
1008 |
else if (!readui16(io, &len)) |
|
1009 |
goto zip_load_entry_puked; |
|
1010 |
||
1011 |
si64 += 4 + len; |
|
1012 |
extralen -= 4 + len; |
|
1013 |
if (sig != ZIP64_EXTENDED_INFO_EXTRA_FIELD_SIG) |
|
1014 |
{ |
|
1015 |
if (!io->seek(io, si64)) |
|
1016 |
goto zip_load_entry_puked; |
|
1017 |
continue; |
|
1018 |
} /* if */ |
|
1019 |
||
1020 |
found = 1; |
|
1021 |
break; |
|
1022 |
} /* while */ |
|
1023 |
||
1024 |
GOTO_IF_MACRO(!found, PHYSFS_ERR_CORRUPT, zip_load_entry_puked); |
|
1025 |
||
1026 |
if (entry->uncompressed_size == 0xFFFFFFFF) |
|
1027 |
{ |
|
1028 |
GOTO_IF_MACRO(len < 8, PHYSFS_ERR_CORRUPT, zip_load_entry_puked); |
|
1029 |
if (!readui64(io, &entry->uncompressed_size)) |
|
1030 |
goto zip_load_entry_puked; |
|
1031 |
len -= 8; |
|
1032 |
} /* if */ |
|
1033 |
||
1034 |
if (entry->compressed_size == 0xFFFFFFFF) |
|
1035 |
{ |
|
1036 |
GOTO_IF_MACRO(len < 8, PHYSFS_ERR_CORRUPT, zip_load_entry_puked); |
|
1037 |
if (!readui64(io, &entry->compressed_size)) |
|
1038 |
goto zip_load_entry_puked; |
|
1039 |
len -= 8; |
|
1040 |
} /* if */ |
|
1041 |
||
1042 |
if (offset == 0xFFFFFFFF) |
|
1043 |
{ |
|
1044 |
GOTO_IF_MACRO(len < 8, PHYSFS_ERR_CORRUPT, zip_load_entry_puked); |
|
1045 |
if (!readui64(io, &offset)) |
|
1046 |
goto zip_load_entry_puked; |
|
1047 |
len -= 8; |
|
1048 |
} /* if */ |
|
1049 |
||
1050 |
if (starting_disk == 0xFFFFFFFF) |
|
1051 |
{ |
|
1052 |
GOTO_IF_MACRO(len < 8, PHYSFS_ERR_CORRUPT, zip_load_entry_puked); |
|
1053 |
if (!readui32(io, &starting_disk)) |
|
1054 |
goto zip_load_entry_puked; |
|
1055 |
len -= 4; |
|
1056 |
} /* if */ |
|
1057 |
||
1058 |
GOTO_IF_MACRO(len != 0, PHYSFS_ERR_CORRUPT, zip_load_entry_puked); |
|
1059 |
} /* if */ |
|
1060 |
||
1061 |
GOTO_IF_MACRO(starting_disk != 0, PHYSFS_ERR_CORRUPT, zip_load_entry_puked); |
|
1062 |
||
1063 |
entry->offset = offset + ofs_fixup; |
|
1064 |
||
1065 |
/* seek to the start of the next entry in the central directory... */ |
|
1066 |
if (!io->seek(io, si64 + extralen + commentlen)) |
|
1067 |
goto zip_load_entry_puked; |
|
1068 |
||
1069 |
return 1; /* success. */ |
|
1070 |
||
1071 |
zip_load_entry_puked: |
|
1072 |
allocator.Free(entry->name); |
|
1073 |
return 0; /* failure. */ |
|
1074 |
} /* zip_load_entry */ |
|
1075 |
||
1076 |
||
1077 |
static int zip_entry_cmp(void *_a, size_t one, size_t two) |
|
1078 |
{ |
|
1079 |
if (one != two) |
|
1080 |
{ |
|
1081 |
const ZIPentry *a = (const ZIPentry *) _a; |
|
1082 |
return strcmp(a[one].name, a[two].name); |
|
1083 |
} /* if */ |
|
1084 |
||
1085 |
return 0; |
|
1086 |
} /* zip_entry_cmp */ |
|
1087 |
||
1088 |
||
1089 |
static void zip_entry_swap(void *_a, size_t one, size_t two) |
|
1090 |
{ |
|
1091 |
if (one != two) |
|
1092 |
{ |
|
1093 |
ZIPentry tmp; |
|
1094 |
ZIPentry *first = &(((ZIPentry *) _a)[one]); |
|
1095 |
ZIPentry *second = &(((ZIPentry *) _a)[two]); |
|
1096 |
memcpy(&tmp, first, sizeof (ZIPentry)); |
|
1097 |
memcpy(first, second, sizeof (ZIPentry)); |
|
1098 |
memcpy(second, &tmp, sizeof (ZIPentry)); |
|
1099 |
} /* if */ |
|
1100 |
} /* zip_entry_swap */ |
|
1101 |
||
1102 |
||
1103 |
static int zip_load_entries(PHYSFS_Io *io, ZIPinfo *info, |
|
1104 |
const PHYSFS_uint64 data_ofs, |
|
1105 |
const PHYSFS_uint64 central_ofs) |
|
1106 |
{ |
|
1107 |
const PHYSFS_uint64 max = info->entryCount; |
|
1108 |
const int zip64 = info->zip64; |
|
1109 |
PHYSFS_uint64 i; |
|
1110 |
||
1111 |
BAIL_IF_MACRO(!io->seek(io, central_ofs), ERRPASS, 0); |
|
1112 |
||
1113 |
info->entries = (ZIPentry *) allocator.Malloc(sizeof (ZIPentry) * max); |
|
1114 |
BAIL_IF_MACRO(!info->entries, PHYSFS_ERR_OUT_OF_MEMORY, 0); |
|
1115 |
||
1116 |
for (i = 0; i < max; i++) |
|
1117 |
{ |
|
1118 |
if (!zip_load_entry(io, zip64, &info->entries[i], data_ofs)) |
|
1119 |
{ |
|
1120 |
zip_free_entries(info->entries, i); |
|
1121 |
return 0; |
|
1122 |
} /* if */ |
|
1123 |
} /* for */ |
|
1124 |
||
1125 |
__PHYSFS_sort(info->entries, (size_t) max, zip_entry_cmp, zip_entry_swap); |
|
1126 |
return 1; |
|
1127 |
} /* zip_load_entries */ |
|
1128 |
||
1129 |
||
1130 |
static PHYSFS_sint64 zip64_find_end_of_central_dir(PHYSFS_Io *io, |
|
1131 |
PHYSFS_sint64 _pos, |
|
1132 |
PHYSFS_uint64 offset) |
|
1133 |
{ |
|
1134 |
/* |
|
1135 |
* Naturally, the offset is useless to us; it is the offset from the |
|
1136 |
* start of file, which is meaningless if we've appended this .zip to |
|
1137 |
* a self-extracting .exe. We need to find this on our own. It should |
|
1138 |
* be directly before the locator record, but the record in question, |
|
1139 |
* like the original end-of-central-directory record, ends with a |
|
1140 |
* variable-length field. Unlike the original, which has to store the |
|
1141 |
* size of that variable-length field in a 16-bit int and thus has to be |
|
1142 |
* within 64k, the new one gets 64-bits. |
|
1143 |
* |
|
1144 |
* Fortunately, the only currently-specified record for that variable |
|
1145 |
* length block is some weird proprietary thing that deals with EBCDIC |
|
1146 |
* and tape backups or something. So we don't seek far. |
|
1147 |
*/ |
|
1148 |
||
1149 |
PHYSFS_uint32 ui32; |
|
1150 |
const PHYSFS_uint64 pos = (PHYSFS_uint64) _pos; |
|
1151 |
||
1152 |
assert(_pos > 0); |
|
1153 |
||
1154 |
/* Try offset specified in the Zip64 end of central directory locator. */ |
|
1155 |
/* This works if the entire PHYSFS_Io is the zip file. */ |
|
1156 |
BAIL_IF_MACRO(!io->seek(io, offset), ERRPASS, -1); |
|
1157 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, -1); |
|
1158 |
if (ui32 == ZIP64_END_OF_CENTRAL_DIR_SIG) |
|
1159 |
return offset; |
|
1160 |
||
1161 |
/* Try 56 bytes before the Zip64 end of central directory locator. */ |
|
1162 |
/* This works if the record isn't variable length and is version 1. */ |
|
1163 |
if (pos > 56) |
|
1164 |
{ |
|
1165 |
BAIL_IF_MACRO(!io->seek(io, pos-56), ERRPASS, -1); |
|
1166 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, -1); |
|
1167 |
if (ui32 == ZIP64_END_OF_CENTRAL_DIR_SIG) |
|
1168 |
return pos-56; |
|
1169 |
} /* if */ |
|
1170 |
||
1171 |
/* Try 84 bytes before the Zip64 end of central directory locator. */ |
|
1172 |
/* This works if the record isn't variable length and is version 2. */ |
|
1173 |
if (pos > 84) |
|
1174 |
{ |
|
1175 |
BAIL_IF_MACRO(!io->seek(io, pos-84), ERRPASS, -1); |
|
1176 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, -1); |
|
1177 |
if (ui32 == ZIP64_END_OF_CENTRAL_DIR_SIG) |
|
1178 |
return pos-84; |
|
1179 |
} /* if */ |
|
1180 |
||
1181 |
/* Ok, brute force: we know it's between (offset) and (pos) somewhere. */ |
|
1182 |
/* Just try moving back at most 256k. Oh well. */ |
|
1183 |
if ((offset < pos) && (pos > 4)) |
|
1184 |
{ |
|
1185 |
/* we assume you can eat this stack if you handle Zip64 files. */ |
|
1186 |
PHYSFS_uint8 buf[256 * 1024]; |
|
1187 |
PHYSFS_uint64 len = pos - offset; |
|
1188 |
PHYSFS_sint32 i; |
|
1189 |
||
1190 |
if (len > sizeof (buf)) |
|
1191 |
len = sizeof (buf); |
|
1192 |
||
1193 |
BAIL_IF_MACRO(!io->seek(io, pos - len), ERRPASS, -1); |
|
1194 |
BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, len), ERRPASS, -1); |
|
1195 |
for (i = (PHYSFS_sint32) (len - 4); i >= 0; i--) |
|
1196 |
{ |
|
1197 |
if (buf[i] != 0x50) |
|
1198 |
continue; |
|
1199 |
if ( (buf[i+1] == 0x4b) && |
|
1200 |
(buf[i+2] == 0x06) && |
|
1201 |
(buf[i+3] == 0x06) ) |
|
1202 |
return pos - (len - i); |
|
1203 |
} /* for */ |
|
1204 |
} /* if */ |
|
1205 |
||
1206 |
BAIL_MACRO(PHYSFS_ERR_CORRUPT, -1); /* didn't find it. */ |
|
1207 |
} /* zip64_find_end_of_central_dir */ |
|
1208 |
||
1209 |
||
1210 |
static int zip64_parse_end_of_central_dir(PHYSFS_Io *io, ZIPinfo *info, |
|
1211 |
PHYSFS_uint64 *data_start, |
|
1212 |
PHYSFS_uint64 *dir_ofs, |
|
1213 |
PHYSFS_sint64 pos) |
|
1214 |
{ |
|
1215 |
PHYSFS_uint64 ui64; |
|
1216 |
PHYSFS_uint32 ui32; |
|
1217 |
PHYSFS_uint16 ui16; |
|
1218 |
||
1219 |
/* We should be positioned right past the locator signature. */ |
|
1220 |
||
1221 |
if ((pos < 0) || (!io->seek(io, pos))) |
|
1222 |
return 0; |
|
1223 |
||
1224 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
1225 |
if (ui32 != ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIG) |
|
1226 |
return -1; /* it's not a Zip64 archive. Not an error, though! */ |
|
1227 |
||
1228 |
info->zip64 = 1; |
|
1229 |
||
1230 |
/* number of the disk with the start of the central directory. */ |
|
1231 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
1232 |
BAIL_IF_MACRO(ui32 != 0, PHYSFS_ERR_CORRUPT, 0); |
|
1233 |
||
1234 |
/* offset of Zip64 end of central directory record. */ |
|
1235 |
BAIL_IF_MACRO(!readui64(io, &ui64), ERRPASS, 0); |
|
1236 |
||
1237 |
/* total number of disks */ |
|
1238 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
1239 |
BAIL_IF_MACRO(ui32 != 1, PHYSFS_ERR_CORRUPT, 0); |
|
1240 |
||
1241 |
pos = zip64_find_end_of_central_dir(io, pos, ui64); |
|
1242 |
if (pos < 0) |
|
1243 |
return 0; /* oh well. */ |
|
1244 |
||
1245 |
/* |
|
1246 |
* For self-extracting archives, etc, there's crapola in the file |
|
1247 |
* before the zipfile records; we calculate how much data there is |
|
1248 |
* prepended by determining how far the zip64-end-of-central-directory |
|
1249 |
* offset is from where it is supposed to be...the difference in bytes |
|
1250 |
* is how much arbitrary data is at the start of the physical file. |
|
1251 |
*/ |
|
1252 |
assert(((PHYSFS_uint64) pos) >= ui64); |
|
1253 |
*data_start = ((PHYSFS_uint64) pos) - ui64; |
|
1254 |
||
1255 |
BAIL_IF_MACRO(!io->seek(io, pos), ERRPASS, 0); |
|
1256 |
||
1257 |
/* check signature again, just in case. */ |
|
1258 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
1259 |
BAIL_IF_MACRO(ui32 != ZIP64_END_OF_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, 0); |
|
1260 |
||
1261 |
/* size of Zip64 end of central directory record. */ |
|
1262 |
BAIL_IF_MACRO(!readui64(io, &ui64), ERRPASS, 0); |
|
1263 |
||
1264 |
/* version made by. */ |
|
1265 |
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); |
|
1266 |
||
1267 |
/* version needed to extract. */ |
|
1268 |
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); |
|
1269 |
||
1270 |
/* number of this disk. */ |
|
1271 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
1272 |
BAIL_IF_MACRO(ui32 != 0, PHYSFS_ERR_CORRUPT, 0); |
|
1273 |
||
1274 |
/* number of disk with start of central directory record. */ |
|
1275 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
1276 |
BAIL_IF_MACRO(ui32 != 0, PHYSFS_ERR_CORRUPT, 0); |
|
1277 |
||
1278 |
/* total number of entries in the central dir on this disk */ |
|
1279 |
BAIL_IF_MACRO(!readui64(io, &ui64), ERRPASS, 0); |
|
1280 |
||
1281 |
/* total number of entries in the central dir */ |
|
1282 |
BAIL_IF_MACRO(!readui64(io, &info->entryCount), ERRPASS, 0); |
|
1283 |
BAIL_IF_MACRO(ui64 != info->entryCount, PHYSFS_ERR_CORRUPT, 0); |
|
1284 |
||
1285 |
/* size of the central directory */ |
|
1286 |
BAIL_IF_MACRO(!readui64(io, &ui64), ERRPASS, 0); |
|
1287 |
||
1288 |
/* offset of central directory */ |
|
1289 |
BAIL_IF_MACRO(!readui64(io, dir_ofs), ERRPASS, 0); |
|
1290 |
||
1291 |
/* Since we know the difference, fix up the central dir offset... */ |
|
1292 |
*dir_ofs += *data_start; |
|
1293 |
||
1294 |
/* |
|
1295 |
* There are more fields here, for encryption and feature-specific things, |
|
1296 |
* but we don't care about any of them at the moment. |
|
1297 |
*/ |
|
1298 |
||
1299 |
return 1; /* made it. */ |
|
1300 |
} /* zip64_parse_end_of_central_dir */ |
|
1301 |
||
1302 |
||
1303 |
static int zip_parse_end_of_central_dir(PHYSFS_Io *io, ZIPinfo *info, |
|
1304 |
PHYSFS_uint64 *data_start, |
|
1305 |
PHYSFS_uint64 *dir_ofs) |
|
1306 |
{ |
|
1307 |
PHYSFS_uint16 entryCount16; |
|
1308 |
PHYSFS_uint32 offset32; |
|
1309 |
PHYSFS_uint32 ui32; |
|
1310 |
PHYSFS_uint16 ui16; |
|
1311 |
PHYSFS_sint64 len; |
|
1312 |
PHYSFS_sint64 pos; |
|
1313 |
int rc; |
|
1314 |
||
1315 |
/* find the end-of-central-dir record, and seek to it. */ |
|
1316 |
pos = zip_find_end_of_central_dir(io, &len); |
|
1317 |
BAIL_IF_MACRO(pos == -1, ERRPASS, 0); |
|
1318 |
BAIL_IF_MACRO(!io->seek(io, pos), ERRPASS, 0); |
|
1319 |
||
1320 |
/* check signature again, just in case. */ |
|
1321 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
1322 |
BAIL_IF_MACRO(ui32 != ZIP_END_OF_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, 0); |
|
1323 |
||
1324 |
/* Seek back to see if "Zip64 end of central directory locator" exists. */ |
|
1325 |
/* this record is 20 bytes before end-of-central-dir */ |
|
1326 |
rc = zip64_parse_end_of_central_dir(io, info, data_start, dir_ofs, pos-20); |
|
1327 |
BAIL_IF_MACRO(rc == 0, ERRPASS, 0); |
|
1328 |
if (rc == 1) |
|
1329 |
return 1; /* we're done here. */ |
|
1330 |
||
1331 |
assert(rc == -1); /* no error, just not a Zip64 archive. */ |
|
1332 |
||
1333 |
/* Not Zip64? Seek back to where we were and keep processing. */ |
|
1334 |
BAIL_IF_MACRO(!io->seek(io, pos + 4), ERRPASS, 0); |
|
1335 |
||
1336 |
/* number of this disk */ |
|
1337 |
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); |
|
1338 |
BAIL_IF_MACRO(ui16 != 0, PHYSFS_ERR_CORRUPT, 0); |
|
1339 |
||
1340 |
/* number of the disk with the start of the central directory */ |
|
1341 |
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); |
|
1342 |
BAIL_IF_MACRO(ui16 != 0, PHYSFS_ERR_CORRUPT, 0); |
|
1343 |
||
1344 |
/* total number of entries in the central dir on this disk */ |
|
1345 |
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); |
|
1346 |
||
1347 |
/* total number of entries in the central dir */ |
|
1348 |
BAIL_IF_MACRO(!readui16(io, &entryCount16), ERRPASS, 0); |
|
1349 |
BAIL_IF_MACRO(ui16 != entryCount16, PHYSFS_ERR_CORRUPT, 0); |
|
1350 |
||
1351 |
info->entryCount = entryCount16; |
|
1352 |
||
1353 |
/* size of the central directory */ |
|
1354 |
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); |
|
1355 |
||
1356 |
/* offset of central directory */ |
|
1357 |
BAIL_IF_MACRO(!readui32(io, &offset32), ERRPASS, 0); |
|
1358 |
*dir_ofs = (PHYSFS_uint64) offset32; |
|
1359 |
BAIL_IF_MACRO(pos < (*dir_ofs + ui32), PHYSFS_ERR_CORRUPT, 0); |
|
1360 |
||
1361 |
/* |
|
1362 |
* For self-extracting archives, etc, there's crapola in the file |
|
1363 |
* before the zipfile records; we calculate how much data there is |
|
1364 |
* prepended by determining how far the central directory offset is |
|
1365 |
* from where it is supposed to be (start of end-of-central-dir minus |
|
1366 |
* sizeof central dir)...the difference in bytes is how much arbitrary |
|
1367 |
* data is at the start of the physical file. |
|
1368 |
*/ |
|
1369 |
*data_start = (PHYSFS_uint64) (pos - (*dir_ofs + ui32)); |
|
1370 |
||
1371 |
/* Now that we know the difference, fix up the central dir offset... */ |
|
1372 |
*dir_ofs += *data_start; |
|
1373 |
||
1374 |
/* zipfile comment length */ |
|
1375 |
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); |
|
1376 |
||
1377 |
/* |
|
1378 |
* Make sure that the comment length matches to the end of file... |
|
1379 |
* If it doesn't, we're either in the wrong part of the file, or the |
|
1380 |
* file is corrupted, but we give up either way. |
|
1381 |
*/ |
|
1382 |
BAIL_IF_MACRO((pos + 22 + ui16) != len, PHYSFS_ERR_CORRUPT, 0); |
|
1383 |
||
1384 |
return 1; /* made it. */ |
|
1385 |
} /* zip_parse_end_of_central_dir */ |
|
1386 |
||
1387 |
||
1388 |
static void *ZIP_openArchive(PHYSFS_Io *io, const char *name, int forWriting) |
|
1389 |
{ |
|
1390 |
ZIPinfo *info = NULL; |
|
1391 |
PHYSFS_uint64 data_start; |
|
1392 |
PHYSFS_uint64 cent_dir_ofs; |
|
1393 |
||
1394 |
assert(io != NULL); /* shouldn't ever happen. */ |
|
1395 |
||
1396 |
BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL); |
|
1397 |
BAIL_IF_MACRO(!isZip(io), ERRPASS, NULL); |
|
1398 |
||
1399 |
info = (ZIPinfo *) allocator.Malloc(sizeof (ZIPinfo)); |
|
1400 |
BAIL_IF_MACRO(!info, PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
1401 |
memset(info, '\0', sizeof (ZIPinfo)); |
|
1402 |
info->io = io; |
|
1403 |
||
1404 |
if (!zip_parse_end_of_central_dir(io, info, &data_start, ¢_dir_ofs)) |
|
1405 |
goto ZIP_openarchive_failed; |
|
1406 |
||
1407 |
if (!zip_load_entries(io, info, data_start, cent_dir_ofs)) |
|
1408 |
goto ZIP_openarchive_failed; |
|
1409 |
||
1410 |
return info; |
|
1411 |
||
1412 |
ZIP_openarchive_failed: |
|
1413 |
if (info != NULL) |
|
1414 |
allocator.Free(info); |
|
1415 |
||
1416 |
return NULL; |
|
1417 |
} /* ZIP_openArchive */ |
|
1418 |
||
1419 |
||
1420 |
static PHYSFS_sint64 zip_find_start_of_dir(ZIPinfo *info, const char *path, |
|
1421 |
int stop_on_first_find) |
|
1422 |
{ |
|
1423 |
PHYSFS_sint64 lo = 0; |
|
1424 |
PHYSFS_sint64 hi = (PHYSFS_sint64) (info->entryCount - 1); |
|
1425 |
PHYSFS_sint64 middle; |
|
1426 |
PHYSFS_uint32 dlen = (PHYSFS_uint32) strlen(path); |
|
1427 |
PHYSFS_sint64 retval = -1; |
|
1428 |
const char *name; |
|
1429 |
int rc; |
|
1430 |
||
1431 |
if (*path == '\0') /* root dir? */ |
|
1432 |
return 0; |
|
1433 |
||
1434 |
if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */ |
|
1435 |
dlen--; |
|
1436 |
||
1437 |
while (lo <= hi) |
|
1438 |
{ |
|
1439 |
middle = lo + ((hi - lo) / 2); |
|
1440 |
name = info->entries[middle].name; |
|
1441 |
rc = strncmp(path, name, dlen); |
|
1442 |
if (rc == 0) |
|
1443 |
{ |
|
1444 |
char ch = name[dlen]; |
|
1445 |
if ('/' < ch) /* make sure this isn't just a substr match. */ |
|
1446 |
rc = -1; |
|
1447 |
else if ('/' > ch) |
|
1448 |
rc = 1; |
|
10017 | 1449 |
else |
7768 | 1450 |
{ |
1451 |
if (stop_on_first_find) /* Just checking dir's existance? */ |
|
1452 |
return middle; |
|
1453 |
||
1454 |
if (name[dlen + 1] == '\0') /* Skip initial dir entry. */ |
|
1455 |
return (middle + 1); |
|
1456 |
||
1457 |
/* there might be more entries earlier in the list. */ |
|
1458 |
retval = middle; |
|
1459 |
hi = middle - 1; |
|
1460 |
} /* else */ |
|
1461 |
} /* if */ |
|
1462 |
||
1463 |
if (rc > 0) |
|
1464 |
lo = middle + 1; |
|
1465 |
else |
|
1466 |
hi = middle - 1; |
|
1467 |
} /* while */ |
|
1468 |
||
1469 |
return retval; |
|
1470 |
} /* zip_find_start_of_dir */ |
|
1471 |
||
1472 |
||
1473 |
/* |
|
1474 |
* Moved to seperate function so we can use alloca then immediately throw |
|
1475 |
* away the allocated stack space... |
|
1476 |
*/ |
|
1477 |
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata, |
|
1478 |
const char *odir, const char *str, PHYSFS_sint32 ln) |
|
1479 |
{ |
|
1480 |
char *newstr = __PHYSFS_smallAlloc(ln + 1); |
|
1481 |
if (newstr == NULL) |
|
1482 |
return; |
|
1483 |
||
1484 |
memcpy(newstr, str, ln); |
|
1485 |
newstr[ln] = '\0'; |
|
1486 |
cb(callbackdata, odir, newstr); |
|
1487 |
__PHYSFS_smallFree(newstr); |
|
1488 |
} /* doEnumCallback */ |
|
1489 |
||
1490 |
||
1491 |
static void ZIP_enumerateFiles(PHYSFS_Dir *opaque, const char *dname, |
|
1492 |
int omitSymLinks, PHYSFS_EnumFilesCallback cb, |
|
1493 |
const char *origdir, void *callbackdata) |
|
1494 |
{ |
|
1495 |
ZIPinfo *info = ((ZIPinfo *) opaque); |
|
1496 |
PHYSFS_sint32 dlen, dlen_inc; |
|
1497 |
PHYSFS_sint64 i, max; |
|
1498 |
||
1499 |
i = zip_find_start_of_dir(info, dname, 0); |
|
1500 |
if (i == -1) /* no such directory. */ |
|
1501 |
return; |
|
1502 |
||
1503 |
dlen = (PHYSFS_sint32) strlen(dname); |
|
1504 |
if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */ |
|
1505 |
dlen--; |
|
1506 |
||
1507 |
dlen_inc = ((dlen > 0) ? 1 : 0) + dlen; |
|
1508 |
max = (PHYSFS_sint64) info->entryCount; |
|
1509 |
while (i < max) |
|
1510 |
{ |
|
1511 |
char *e = info->entries[i].name; |
|
1512 |
if ((dlen) && ((strncmp(e, dname, dlen) != 0) || (e[dlen] != '/'))) |
|
1513 |
break; /* past end of this dir; we're done. */ |
|
1514 |
||
1515 |
if ((omitSymLinks) && (zip_entry_is_symlink(&info->entries[i]))) |
|
1516 |
i++; |
|
1517 |
else |
|
1518 |
{ |
|
1519 |
char *add = e + dlen_inc; |
|
1520 |
char *ptr = strchr(add, '/'); |
|
1521 |
PHYSFS_sint32 ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add)); |
|
1522 |
doEnumCallback(cb, callbackdata, origdir, add, ln); |
|
1523 |
ln += dlen_inc; /* point past entry to children... */ |
|
1524 |
||
1525 |
/* increment counter and skip children of subdirs... */ |
|
1526 |
while ((++i < max) && (ptr != NULL)) |
|
1527 |
{ |
|
1528 |
char *e_new = info->entries[i].name; |
|
1529 |
if ((strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/')) |
|
1530 |
break; |
|
1531 |
} /* while */ |
|
1532 |
} /* else */ |
|
1533 |
} /* while */ |
|
1534 |
} /* ZIP_enumerateFiles */ |
|
1535 |
||
1536 |
||
1537 |
static PHYSFS_Io *zip_get_io(PHYSFS_Io *io, ZIPinfo *inf, ZIPentry *entry) |
|
1538 |
{ |
|
1539 |
int success; |
|
1540 |
PHYSFS_Io *retval = io->duplicate(io); |
|
1541 |
BAIL_IF_MACRO(!retval, ERRPASS, NULL); |
|
1542 |
||
1543 |
/* !!! FIXME: if you open a dir here, it should bail ERR_NOT_A_FILE */ |
|
1544 |
||
1545 |
/* (inf) can be NULL if we already resolved. */ |
|
1546 |
success = (inf == NULL) || zip_resolve(retval, inf, entry); |
|
1547 |
if (success) |
|
1548 |
{ |
|
1549 |
PHYSFS_sint64 offset; |
|
1550 |
offset = ((entry->symlink) ? entry->symlink->offset : entry->offset); |
|
1551 |
success = retval->seek(retval, offset); |
|
1552 |
} /* if */ |
|
1553 |
||
1554 |
if (!success) |
|
1555 |
{ |
|
1556 |
retval->destroy(retval); |
|
1557 |
retval = NULL; |
|
1558 |
} /* if */ |
|
1559 |
||
1560 |
return retval; |
|
1561 |
} /* zip_get_io */ |
|
1562 |
||
1563 |
||
1564 |
static PHYSFS_Io *ZIP_openRead(PHYSFS_Dir *opaque, const char *fnm, |
|
1565 |
int *fileExists) |
|
1566 |
{ |
|
1567 |
PHYSFS_Io *retval = NULL; |
|
1568 |
ZIPinfo *info = (ZIPinfo *) opaque; |
|
1569 |
ZIPentry *entry = zip_find_entry(info, fnm, NULL); |
|
1570 |
ZIPfileinfo *finfo = NULL; |
|
1571 |
||
1572 |
*fileExists = (entry != NULL); |
|
1573 |
BAIL_IF_MACRO(!entry, ERRPASS, NULL); |
|
1574 |
||
1575 |
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io)); |
|
1576 |
GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed); |
|
1577 |
||
1578 |
finfo = (ZIPfileinfo *) allocator.Malloc(sizeof (ZIPfileinfo)); |
|
1579 |
GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed); |
|
1580 |
memset(finfo, '\0', sizeof (ZIPfileinfo)); |
|
1581 |
||
1582 |
finfo->io = zip_get_io(info->io, info, entry); |
|
1583 |
GOTO_IF_MACRO(!finfo->io, ERRPASS, ZIP_openRead_failed); |
|
1584 |
finfo->entry = ((entry->symlink != NULL) ? entry->symlink : entry); |
|
1585 |
initializeZStream(&finfo->stream); |
|
1586 |
||
1587 |
if (finfo->entry->compression_method != COMPMETH_NONE) |
|
1588 |
{ |
|
1589 |
finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE); |
|
1590 |
if (!finfo->buffer) |
|
1591 |
GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed); |
|
1592 |
else if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK) |
|
1593 |
goto ZIP_openRead_failed; |
|
1594 |
} /* if */ |
|
1595 |
||
1596 |
memcpy(retval, &ZIP_Io, sizeof (PHYSFS_Io)); |
|
1597 |
retval->opaque = finfo; |
|
1598 |
||
1599 |
return retval; |
|
1600 |
||
1601 |
ZIP_openRead_failed: |
|
1602 |
if (finfo != NULL) |
|
1603 |
{ |
|
1604 |
if (finfo->io != NULL) |
|
1605 |
finfo->io->destroy(finfo->io); |
|
1606 |
||
1607 |
if (finfo->buffer != NULL) |
|
1608 |
{ |
|
1609 |
allocator.Free(finfo->buffer); |
|
1610 |
inflateEnd(&finfo->stream); |
|
1611 |
} /* if */ |
|
1612 |
||
1613 |
allocator.Free(finfo); |
|
1614 |
} /* if */ |
|
1615 |
||
1616 |
if (retval != NULL) |
|
1617 |
allocator.Free(retval); |
|
1618 |
||
1619 |
return NULL; |
|
1620 |
} /* ZIP_openRead */ |
|
1621 |
||
1622 |
||
1623 |
static PHYSFS_Io *ZIP_openWrite(PHYSFS_Dir *opaque, const char *filename) |
|
1624 |
{ |
|
1625 |
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL); |
|
1626 |
} /* ZIP_openWrite */ |
|
1627 |
||
1628 |
||
1629 |
static PHYSFS_Io *ZIP_openAppend(PHYSFS_Dir *opaque, const char *filename) |
|
1630 |
{ |
|
1631 |
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL); |
|
1632 |
} /* ZIP_openAppend */ |
|
1633 |
||
1634 |
||
1635 |
static void ZIP_closeArchive(PHYSFS_Dir *opaque) |
|
1636 |
{ |
|
1637 |
ZIPinfo *zi = (ZIPinfo *) (opaque); |
|
1638 |
zi->io->destroy(zi->io); |
|
1639 |
zip_free_entries(zi->entries, zi->entryCount); |
|
1640 |
allocator.Free(zi); |
|
1641 |
} /* ZIP_closeArchive */ |
|
1642 |
||
1643 |
||
1644 |
static int ZIP_remove(PHYSFS_Dir *opaque, const char *name) |
|
1645 |
{ |
|
1646 |
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0); |
|
1647 |
} /* ZIP_remove */ |
|
1648 |
||
1649 |
||
1650 |
static int ZIP_mkdir(PHYSFS_Dir *opaque, const char *name) |
|
1651 |
{ |
|
1652 |
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0); |
|
1653 |
} /* ZIP_mkdir */ |
|
1654 |
||
1655 |
||
1656 |
static int ZIP_stat(PHYSFS_Dir *opaque, const char *filename, int *exists, |
|
1657 |
PHYSFS_Stat *stat) |
|
1658 |
{ |
|
1659 |
int isDir = 0; |
|
1660 |
const ZIPinfo *info = (const ZIPinfo *) opaque; |
|
1661 |
const ZIPentry *entry = zip_find_entry(info, filename, &isDir); |
|
1662 |
||
1663 |
/* !!! FIXME: does this need to resolve entries here? */ |
|
1664 |
||
1665 |
*exists = isDir || (entry != 0); |
|
1666 |
if (!*exists) |
|
1667 |
return 0; |
|
1668 |
||
1669 |
if (isDir) |
|
1670 |
{ |
|
1671 |
stat->filesize = 0; |
|
1672 |
stat->filetype = PHYSFS_FILETYPE_DIRECTORY; |
|
1673 |
} /* if */ |
|
1674 |
||
1675 |
else if (zip_entry_is_symlink(entry)) |
|
1676 |
{ |
|
1677 |
stat->filesize = 0; |
|
1678 |
stat->filetype = PHYSFS_FILETYPE_SYMLINK; |
|
1679 |
} /* else if */ |
|
1680 |
||
1681 |
else |
|
1682 |
{ |
|
1683 |
stat->filesize = (PHYSFS_sint64) entry->uncompressed_size; |
|
1684 |
stat->filetype = PHYSFS_FILETYPE_REGULAR; |
|
1685 |
} /* else */ |
|
1686 |
||
1687 |
stat->modtime = ((entry) ? entry->last_mod_time : 0); |
|
1688 |
stat->createtime = stat->modtime; |
|
1689 |
stat->accesstime = 0; |
|
1690 |
stat->readonly = 1; /* .zip files are always read only */ |
|
1691 |
||
1692 |
return 1; |
|
1693 |
} /* ZIP_stat */ |
|
1694 |
||
1695 |
||
1696 |
const PHYSFS_Archiver __PHYSFS_Archiver_ZIP = |
|
1697 |
{ |
|
1698 |
{ |
|
1699 |
"ZIP", |
|
1700 |
"PkZip/WinZip/Info-Zip compatible", |
|
1701 |
"Ryan C. Gordon <icculus@icculus.org>", |
|
1702 |
"http://icculus.org/physfs/", |
|
1703 |
}, |
|
1704 |
ZIP_openArchive, /* openArchive() method */ |
|
1705 |
ZIP_enumerateFiles, /* enumerateFiles() method */ |
|
1706 |
ZIP_openRead, /* openRead() method */ |
|
1707 |
ZIP_openWrite, /* openWrite() method */ |
|
1708 |
ZIP_openAppend, /* openAppend() method */ |
|
1709 |
ZIP_remove, /* remove() method */ |
|
1710 |
ZIP_mkdir, /* mkdir() method */ |
|
1711 |
ZIP_closeArchive, /* closeArchive() method */ |
|
1712 |
ZIP_stat /* stat() method */ |
|
1713 |
}; |
|
1714 |
||
1715 |
#endif /* defined PHYSFS_SUPPORTS_ZIP */ |
|
1716 |
||
1717 |
/* end of zip.c ... */ |
|
1718 |