/
archiver_qpak.c
575 lines (457 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
* QPAK support routines for PhysicsFS.
*
* This archiver handles the archive format utilized by Quake 1 and 2.
* Quake3-based games use the PkZip/Info-Zip format (which our zip.c
* archiver handles).
*
* ========================================================================
*
* This format info (in more detail) comes from:
* http://debian.fmi.uni-sofia.bg/~sergei/cgsr/docs/pak.txt
*
* Quake PAK Format
*
* Header
* (4 bytes) signature = 'PACK'
* (4 bytes) directory offset
* (4 bytes) directory length
*
* Directory
* (56 bytes) file name
* (4 bytes) file position
* (4 bytes) file length
*
* ========================================================================
*
27
* Please see the file LICENSE.txt in the source's root directory.
28
29
30
31
32
33
34
35
36
*
* This file written by Ryan C. Gordon.
*/
#if (defined PHYSFS_SUPPORTS_QPAK)
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
37
/* !!! FIXME: what is this here for? */
38
#if 1 /* Make this case insensitive? */
39
40
#define QPAK_strcmp(x, y) __PHYSFS_stricmpASCII(x, y)
#define QPAK_strncmp(x, y, z) __PHYSFS_strnicmpASCII(x, y, z)
41
42
43
44
45
46
#else
#define QPAK_strcmp(x, y) strcmp(x, y)
#define QPAK_strncmp(x, y, z) strncmp(x, y, z)
#endif
47
48
49
50
51
52
53
54
55
typedef struct
{
char name[56];
PHYSFS_uint32 startPos;
PHYSFS_uint32 size;
} QPAKentry;
typedef struct
{
56
PHYSFS_Io *io;
57
58
59
60
61
62
PHYSFS_uint32 entryCount;
QPAKentry *entries;
} QPAKinfo;
typedef struct
{
63
PHYSFS_Io *io;
64
65
66
67
68
QPAKentry *entry;
PHYSFS_uint32 curPos;
} QPAKfileinfo;
/* Magic numbers... */
69
#define QPAK_SIG 0x4b434150 /* "PACK" in ASCII. */
70
71
72
73
74
75
76
77
static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
{
return (io->read(io, buf, len) == len);
} /* readAll */
78
static void QPAK_dirClose(dvoid *opaque)
79
{
80
QPAKinfo *info = ((QPAKinfo *) opaque);
81
info->io->destroy(info->io);
82
83
allocator.Free(info->entries);
allocator.Free(info);
84
85
86
} /* QPAK_dirClose */
87
static PHYSFS_sint64 QPAK_read(PHYSFS_Io *io, void *buffer, PHYSFS_uint64 len)
88
{
89
QPAKfileinfo *finfo = (QPAKfileinfo *) io->opaque;
90
91
const QPAKentry *entry = finfo->entry;
const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
92
93
PHYSFS_sint64 rc;
94
95
if (bytesLeft < len)
len = bytesLeft;
96
97
rc = finfo->io->read(finfo->io, buffer, len);
98
if (rc > 0)
99
finfo->curPos += (PHYSFS_uint32) rc;
100
101
return rc;
102
103
104
} /* QPAK_read */
105
static PHYSFS_sint64 QPAK_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
106
107
108
109
110
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* QPAK_write */
111
static PHYSFS_sint64 QPAK_tell(PHYSFS_Io *io)
112
{
113
return ((QPAKfileinfo *) io->opaque)->curPos;
114
115
116
} /* QPAK_tell */
117
static int QPAK_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
118
{
119
120
QPAKfileinfo *finfo = (QPAKfileinfo *) io->opaque;
const QPAKentry *entry = finfo->entry;
121
122
123
int rc;
BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
124
rc = finfo->io->seek(finfo->io, entry->startPos + offset);
125
126
127
if (rc)
finfo->curPos = (PHYSFS_uint32) offset;
128
return rc;
129
130
131
} /* QPAK_seek */
132
static PHYSFS_sint64 QPAK_length(PHYSFS_Io *io)
133
{
134
const QPAKfileinfo *finfo = (QPAKfileinfo *) io->opaque;
135
return ((PHYSFS_sint64) finfo->entry->size);
136
} /* QPAK_length */
137
138
139
static PHYSFS_Io *QPAK_duplicate(PHYSFS_Io *_io)
140
{
141
142
143
144
145
146
QPAKfileinfo *origfinfo = (QPAKfileinfo *) _io->opaque;
PHYSFS_Io *io = NULL;
PHYSFS_Io *retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
QPAKfileinfo *finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
GOTO_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, QPAK_duplicate_failed);
GOTO_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, QPAK_duplicate_failed);
147
148
149
150
151
152
153
154
155
io = origfinfo->io->duplicate(origfinfo->io);
GOTO_IF_MACRO(io == NULL, NULL, QPAK_duplicate_failed);
finfo->io = io;
finfo->entry = origfinfo->entry;
finfo->curPos = 0;
memcpy(retval, _io, sizeof (PHYSFS_Io));
retval->opaque = finfo;
return retval;
156
157
158
159
160
161
162
QPAK_duplicate_failed:
if (finfo != NULL) allocator.Free(finfo);
if (retval != NULL) allocator.Free(retval);
if (io != NULL) io->destroy(io);
return NULL;
} /* QPAK_duplicate */
163
164
static int QPAK_flush(PHYSFS_Io *io) { return 1; /* no write support. */ }
165
166
167
168
169
170
171
172
173
static void QPAK_destroy(PHYSFS_Io *io)
{
QPAKfileinfo *finfo = (QPAKfileinfo *) io->opaque;
finfo->io->destroy(finfo->io);
allocator.Free(finfo);
allocator.Free(io);
} /* QPAK_destroy */
174
175
176
177
178
179
180
181
182
183
184
185
186
187
static const PHYSFS_Io QPAK_Io =
{
QPAK_read,
QPAK_write,
QPAK_seek,
QPAK_tell,
QPAK_length,
QPAK_duplicate,
QPAK_flush,
QPAK_destroy,
NULL
};
188
189
190
191
static int qpakEntryCmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
192
{
193
194
195
if (one != two)
{
const QPAKentry *a = (const QPAKentry *) _a;
196
return QPAK_strcmp(a[one].name, a[two].name);
197
198
199
} /* if */
return 0;
200
} /* qpakEntryCmp */
201
202
203
static void qpakEntrySwap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
204
{
205
206
207
208
209
210
211
212
213
if (one != two)
{
QPAKentry tmp;
QPAKentry *first = &(((QPAKentry *) _a)[one]);
QPAKentry *second = &(((QPAKentry *) _a)[two]);
memcpy(&tmp, first, sizeof (QPAKentry));
memcpy(first, second, sizeof (QPAKentry));
memcpy(second, &tmp, sizeof (QPAKentry));
} /* if */
214
} /* qpakEntrySwap */
215
216
217
static int qpak_load_entries(QPAKinfo *info)
218
{
219
220
PHYSFS_Io *io = info->io;
PHYSFS_uint32 fileCount = info->entryCount;
221
222
QPAKentry *entry;
223
info->entries = (QPAKentry*) allocator.Malloc(sizeof(QPAKentry)*fileCount);
224
BAIL_IF_MACRO(info->entries == NULL, ERR_OUT_OF_MEMORY, 0);
225
226
227
for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
228
229
230
BAIL_IF_MACRO(!readAll(io, &entry->name, 56), NULL, 0);
BAIL_IF_MACRO(!readAll(io, &entry->startPos, 4), NULL, 0);
BAIL_IF_MACRO(!readAll(io, &entry->size, 4), NULL, 0);
231
entry->size = PHYSFS_swapULE32(entry->size);
232
entry->startPos = PHYSFS_swapULE32(entry->startPos);
233
234
} /* for */
235
__PHYSFS_sort(info->entries, info->entryCount, qpakEntryCmp, qpakEntrySwap);
236
return 1;
237
238
239
} /* qpak_load_entries */
240
static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
241
{
242
243
244
245
QPAKinfo *info = NULL;
PHYSFS_uint32 val = 0;
PHYSFS_uint32 pos = 0;
PHYSFS_uint32 count = 0;
246
247
assert(io != NULL); /* shouldn't ever happen. */
248
249
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
250
251
252
BAIL_IF_MACRO(!readAll(io, &val, 4), NULL, NULL);
BAIL_IF_MACRO(PHYSFS_swapULE32(val) != QPAK_SIG, ERR_NOT_AN_ARCHIVE, NULL);
253
254
255
256
257
258
BAIL_IF_MACRO(!readAll(io, &val, 4), NULL, NULL);
pos = PHYSFS_swapULE32(val); /* directory table offset. */
BAIL_IF_MACRO(!readAll(io, &val, 4), NULL, NULL);
count = PHYSFS_swapULE32(val);
259
260
261
262
263
/* corrupted archive? */
BAIL_IF_MACRO((count % 64) != 0, ERR_CORRUPTED, NULL);
count /= 64;
264
BAIL_IF_MACRO(!io->seek(io, pos), NULL, NULL);
265
266
267
268
269
270
271
272
info = (QPAKinfo *) allocator.Malloc(sizeof (QPAKinfo));
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
memset(info, '\0', sizeof (QPAKinfo));
info->io = io;
info->entryCount = count;
if (!qpak_load_entries(info))
273
{
274
if (info->entries != NULL)
275
276
allocator.Free(info->entries);
allocator.Free(info);
277
return NULL;
278
279
} /* if */
280
return info;
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
} /* QPAK_openArchive */
static PHYSFS_sint32 qpak_find_start_of_dir(QPAKinfo *info, const char *path,
int stop_on_first_find)
{
PHYSFS_sint32 lo = 0;
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
PHYSFS_sint32 middle;
PHYSFS_uint32 dlen = strlen(path);
PHYSFS_sint32 retval = -1;
const char *name;
int rc;
if (*path == '\0') /* root dir? */
296
return 0;
297
298
299
300
301
302
303
304
if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
dlen--;
while (lo <= hi)
{
middle = lo + ((hi - lo) / 2);
name = info->entries[middle].name;
305
rc = QPAK_strncmp(path, name, dlen);
306
307
308
309
310
311
312
313
314
315
if (rc == 0)
{
char ch = name[dlen];
if (ch < '/') /* make sure this isn't just a substr match. */
rc = -1;
else if (ch > '/')
rc = 1;
else
{
if (stop_on_first_find) /* Just checking dir's existance? */
316
return middle;
317
318
if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
319
return (middle + 1);
320
321
322
323
324
325
326
327
328
329
330
331
332
/* there might be more entries earlier in the list. */
retval = middle;
hi = middle - 1;
} /* else */
} /* if */
if (rc > 0)
lo = middle + 1;
else
hi = middle - 1;
} /* while */
333
return retval;
334
335
336
} /* qpak_find_start_of_dir */
337
338
339
340
/*
* Moved to seperate function so we can use alloca then immediately throw
* away the allocated stack space...
*/
341
342
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
const char *odir, const char *str, PHYSFS_sint32 ln)
343
{
344
char *newstr = __PHYSFS_smallAlloc(ln + 1);
345
346
347
348
349
if (newstr == NULL)
return;
memcpy(newstr, str, ln);
newstr[ln] = '\0';
350
cb(callbackdata, odir, newstr);
351
__PHYSFS_smallFree(newstr);
352
353
354
355
} /* doEnumCallback */
static void QPAK_enumerateFiles(dvoid *opaque, const char *dname,
356
357
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
358
{
359
QPAKinfo *info = ((QPAKinfo *) opaque);
360
361
PHYSFS_sint32 dlen, dlen_inc, max, i;
362
363
364
i = qpak_find_start_of_dir(info, dname, 0);
if (i == -1) /* no such directory. */
return;
365
366
367
dlen = strlen(dname);
if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
368
369
370
371
372
373
374
375
376
377
dlen--;
dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
max = (PHYSFS_sint32) info->entryCount;
while (i < max)
{
char *add;
char *ptr;
PHYSFS_sint32 ln;
char *e = info->entries[i].name;
378
if ((dlen) && ((QPAK_strncmp(e, dname, dlen)) || (e[dlen] != '/')))
379
380
381
382
383
break; /* past end of this dir; we're done. */
add = e + dlen_inc;
ptr = strchr(add, '/');
ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
384
doEnumCallback(cb, callbackdata, origdir, add, ln);
385
386
387
388
389
390
ln += dlen_inc; /* point past entry to children... */
/* increment counter and skip children of subdirs... */
while ((++i < max) && (ptr != NULL))
{
char *e_new = info->entries[i].name;
391
if ((QPAK_strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
392
393
394
395
396
397
398
399
400
401
402
break;
} /* while */
} /* while */
} /* QPAK_enumerateFiles */
/*
* This will find the QPAKentry associated with a path in platform-independent
* notation. Directories don't have QPAKentries associated with them, but
* (*isDir) will be set to non-zero if a dir was hit.
*/
403
404
static QPAKentry *qpak_find_entry(const QPAKinfo *info, const char *path,
int *isDir)
405
406
407
408
409
410
411
412
413
414
415
416
417
{
QPAKentry *a = info->entries;
PHYSFS_sint32 pathlen = strlen(path);
PHYSFS_sint32 lo = 0;
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
PHYSFS_sint32 middle;
const char *thispath = NULL;
int rc;
while (lo <= hi)
{
middle = lo + ((hi - lo) / 2);
thispath = a[middle].name;
418
rc = QPAK_strncmp(path, thispath, pathlen);
419
420
421
422
423
424
425
426
427
428
429
430
431
if (rc > 0)
lo = middle + 1;
else if (rc < 0)
hi = middle - 1;
else /* substring match...might be dir or entry or nothing. */
{
if (isDir != NULL)
{
*isDir = (thispath[pathlen] == '/');
if (*isDir)
432
return NULL;
433
434
435
} /* if */
if (thispath[pathlen] == '\0') /* found entry? */
436
return &a[middle];
437
438
439
440
441
442
443
444
445
446
447
448
else
hi = middle - 1; /* adjust search params, try again. */
} /* if */
} /* while */
if (isDir != NULL)
*isDir = 0;
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
} /* qpak_find_entry */
449
static PHYSFS_Io *QPAK_openRead(dvoid *opaque, const char *fnm, int *fileExists)
450
{
451
PHYSFS_Io *io = NULL;
452
QPAKinfo *info = ((QPAKinfo *) opaque);
453
454
455
456
457
458
459
460
461
QPAKfileinfo *finfo;
QPAKentry *entry;
int isDir;
entry = qpak_find_entry(info, fnm, &isDir);
*fileExists = ((entry != NULL) || (isDir));
BAIL_IF_MACRO(isDir, ERR_NOT_A_FILE, NULL);
BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
462
finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
463
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
finfo->io = info->io->duplicate(info->io);
GOTO_IF_MACRO(finfo->io == NULL, NULL, QPAK_openRead_failed);
if (!finfo->io->seek(finfo->io, entry->startPos))
GOTO_MACRO(NULL, QPAK_openRead_failed);
finfo->curPos = 0;
finfo->entry = entry;
io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
GOTO_IF_MACRO(io == NULL, ERR_OUT_OF_MEMORY, QPAK_openRead_failed);
memcpy(io, &QPAK_Io, sizeof (PHYSFS_Io));
io->opaque = finfo;
return io;
QPAK_openRead_failed:
if (finfo != NULL)
481
{
482
483
if (finfo->io != NULL)
finfo->io->destroy(finfo->io);
484
allocator.Free(finfo);
485
486
} /* if */
487
488
489
490
if (io != NULL)
allocator.Free(io);
return NULL;
491
492
493
} /* QPAK_openRead */
494
static PHYSFS_Io *QPAK_openWrite(dvoid *opaque, const char *name)
495
496
497
498
499
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* QPAK_openWrite */
500
static PHYSFS_Io *QPAK_openAppend(dvoid *opaque, const char *name)
501
502
503
504
505
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* QPAK_openAppend */
506
static int QPAK_remove(dvoid *opaque, const char *name)
507
508
509
510
511
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* QPAK_remove */
512
static int QPAK_mkdir(dvoid *opaque, const char *name)
513
514
515
516
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* QPAK_mkdir */
517
518
static int QPAK_stat(dvoid *opaque, const char *filename, int *exists,
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
PHYSFS_Stat *stat)
{
int isDir = 0;
const QPAKinfo *info = (const QPAKinfo *) opaque;
const QPAKentry *entry = qpak_find_entry(info, filename, &isDir);
*exists = ((isDir) || (entry != NULL));
if (!exists)
return 0;
if (isDir)
{
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
stat->filesize = 0;
} /* if */
else
{
stat->filetype = PHYSFS_FILETYPE_REGULAR;
stat->filesize = entry->size;
} /* else */
540
541
542
stat->modtime = -1;
stat->createtime = -1;
stat->accesstime = -1;
543
544
stat->readonly = 1;
545
return 1;
546
547
548
} /* QPAK_stat */
549
550
551
552
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
{
"PAK",
QPAK_ARCHIVE_DESCRIPTION,
553
"Ryan C. Gordon <icculus@icculus.org>",
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
{
&__PHYSFS_ArchiveInfo_QPAK,
QPAK_openArchive, /* openArchive() method */
QPAK_enumerateFiles, /* enumerateFiles() method */
QPAK_openRead, /* openRead() method */
QPAK_openWrite, /* openWrite() method */
QPAK_openAppend, /* openAppend() method */
QPAK_remove, /* remove() method */
QPAK_mkdir, /* mkdir() method */
QPAK_dirClose, /* dirClose() method */
569
QPAK_stat /* stat() method */
570
571
};
572
573
574
#endif /* defined PHYSFS_SUPPORTS_QPAK */
/* end of qpak.c ... */