Skip to content

Latest commit

 

History

History
613 lines (486 loc) · 16.4 KB

archiver_qpak.c

File metadata and controls

613 lines (486 loc) · 16.4 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
37
38
39
40
41
*
* This file written by Ryan C. Gordon.
*/
#if (defined PHYSFS_SUPPORTS_QPAK)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "physfs.h"
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
42
/* !!! FIXME: what is this here for? */
43
#if 1 /* Make this case insensitive? */
44
45
#define QPAK_strcmp(x, y) __PHYSFS_stricmpASCII(x, y)
#define QPAK_strncmp(x, y, z) __PHYSFS_strnicmpASCII(x, y, z)
46
47
48
49
50
51
#else
#define QPAK_strcmp(x, y) strcmp(x, y)
#define QPAK_strncmp(x, y, z) strncmp(x, y, z)
#endif
52
53
54
55
56
57
58
59
60
typedef struct
{
char name[56];
PHYSFS_uint32 startPos;
PHYSFS_uint32 size;
} QPAKentry;
typedef struct
{
62
63
64
65
66
67
PHYSFS_uint32 entryCount;
QPAKentry *entries;
} QPAKinfo;
typedef struct
{
69
70
71
72
73
QPAKentry *entry;
PHYSFS_uint32 curPos;
} QPAKfileinfo;
/* Magic numbers... */
74
#define QPAK_SIG 0x4b434150 /* "PACK" in ASCII. */
77
78
79
80
81
82
static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
{
return (io->read(io, buf, len) == len);
} /* readAll */
86
info->io->destroy(info->io);
87
88
allocator.Free(info->entries);
allocator.Free(info);
89
90
91
} /* QPAK_dirClose */
92
static PHYSFS_sint64 QPAK_read(PHYSFS_Io *io, void *buffer, PHYSFS_uint64 len)
94
QPAKfileinfo *finfo = (QPAKfileinfo *) io->opaque;
95
96
const QPAKentry *entry = finfo->entry;
const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
99
100
if (bytesLeft < len)
len = bytesLeft;
102
rc = finfo->io->read(finfo->io, buffer, len);
104
finfo->curPos += (PHYSFS_uint32) rc;
107
108
109
} /* QPAK_read */
110
static PHYSFS_sint64 QPAK_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
111
112
113
114
115
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* QPAK_write */
116
static PHYSFS_sint64 QPAK_tell(PHYSFS_Io *io)
118
return ((QPAKfileinfo *) io->opaque)->curPos;
119
120
121
} /* QPAK_tell */
122
static int QPAK_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
124
125
QPAKfileinfo *finfo = (QPAKfileinfo *) io->opaque;
const QPAKentry *entry = finfo->entry;
126
127
128
int rc;
BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
129
rc = finfo->io->seek(finfo->io, entry->startPos + offset);
130
131
132
if (rc)
finfo->curPos = (PHYSFS_uint32) offset;
134
135
136
} /* QPAK_seek */
137
static PHYSFS_sint64 QPAK_length(PHYSFS_Io *io)
139
const QPAKfileinfo *finfo = (QPAKfileinfo *) io->opaque;
140
return ((PHYSFS_sint64) finfo->entry->size);
144
static PHYSFS_Io *QPAK_duplicate(PHYSFS_Io *_io)
146
147
148
149
150
151
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);
153
154
155
156
157
158
159
160
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;
162
163
164
165
166
167
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 */
169
static int QPAK_flush(PHYSFS_Io *io) { return 1; /* no write support. */ }
172
173
174
175
176
177
178
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 */
181
182
183
184
185
186
187
188
189
190
191
192
static const PHYSFS_Io QPAK_Io =
{
QPAK_read,
QPAK_write,
QPAK_seek,
QPAK_tell,
QPAK_length,
QPAK_duplicate,
QPAK_flush,
QPAK_destroy,
NULL
};
196
static int qpakEntryCmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
198
199
200
if (one != two)
{
const QPAKentry *a = (const QPAKentry *) _a;
201
return QPAK_strcmp(a[one].name, a[two].name);
208
static void qpakEntrySwap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
210
211
212
213
214
215
216
217
218
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 */
222
static int qpak_load_entries(QPAKinfo *info)
224
225
PHYSFS_Io *io = info->io;
PHYSFS_uint32 fileCount = info->entryCount;
226
227
QPAKentry *entry;
228
info->entries = (QPAKentry*) allocator.Malloc(sizeof(QPAKentry)*fileCount);
229
BAIL_IF_MACRO(info->entries == NULL, ERR_OUT_OF_MEMORY, 0);
230
231
232
for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
233
234
235
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);
236
entry->size = PHYSFS_swapULE32(entry->size);
237
entry->startPos = PHYSFS_swapULE32(entry->startPos);
240
__PHYSFS_sort(info->entries, info->entryCount, qpakEntryCmp, qpakEntrySwap);
242
243
244
} /* qpak_load_entries */
245
static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
247
248
249
250
QPAKinfo *info = NULL;
PHYSFS_uint32 val = 0;
PHYSFS_uint32 pos = 0;
PHYSFS_uint32 count = 0;
252
assert(io != NULL); /* shouldn't ever happen. */
254
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
256
257
BAIL_IF_MACRO(!readAll(io, &val, 4), NULL, NULL);
BAIL_IF_MACRO(PHYSFS_swapULE32(val) != QPAK_SIG, ERR_NOT_AN_ARCHIVE, NULL);
259
260
261
262
263
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);
265
266
267
268
269
270
271
272
273
274
275
276
277
/* corrupted archive? */
BAIL_IF_MACRO((count % 64) != 0, ERR_CORRUPTED, NULL);
count /= 64;
BAIL_IF_MACRO(io->seek(io, pos), NULL, NULL);
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))
280
281
allocator.Free(info->entries);
allocator.Free(info);
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
} /* 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? */
302
303
304
305
306
307
308
309
if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
dlen--;
while (lo <= hi)
{
middle = lo + ((hi - lo) / 2);
name = info->entries[middle].name;
310
rc = QPAK_strncmp(path, name, dlen);
311
312
313
314
315
316
317
318
319
320
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? */
322
323
if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
325
326
327
328
329
330
331
332
333
334
335
336
337
/* 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 */
339
340
341
} /* qpak_find_start_of_dir */
342
343
344
345
/*
* Moved to seperate function so we can use alloca then immediately throw
* away the allocated stack space...
*/
346
347
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
const char *odir, const char *str, PHYSFS_sint32 ln)
350
351
352
353
354
if (newstr == NULL)
return;
memcpy(newstr, str, ln);
newstr[ln] = '\0';
357
358
359
360
} /* doEnumCallback */
static void QPAK_enumerateFiles(dvoid *opaque, const char *dname,
361
362
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
365
366
PHYSFS_sint32 dlen, dlen_inc, max, i;
367
368
369
i = qpak_find_start_of_dir(info, dname, 0);
if (i == -1) /* no such directory. */
return;
371
372
dlen = strlen(dname);
if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
373
374
375
376
377
378
379
380
381
382
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;
383
if ((dlen) && ((QPAK_strncmp(e, dname, dlen)) || (e[dlen] != '/')))
384
385
386
387
388
break; /* past end of this dir; we're done. */
add = e + dlen_inc;
ptr = strchr(add, '/');
ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
389
doEnumCallback(cb, callbackdata, origdir, add, ln);
390
391
392
393
394
395
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;
396
if ((QPAK_strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
397
398
399
400
401
402
403
404
405
406
407
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.
*/
408
409
static QPAKentry *qpak_find_entry(const QPAKinfo *info, const char *path,
int *isDir)
410
411
412
413
414
415
416
417
418
419
420
421
422
{
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;
423
rc = QPAK_strncmp(path, thispath, pathlen);
424
425
426
427
428
429
430
431
432
433
434
435
436
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)
438
439
440
} /* if */
if (thispath[pathlen] == '\0') /* found entry? */
442
443
444
445
446
447
448
449
450
451
452
453
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 */
454
static int QPAK_exists(dvoid *opaque, const char *name)
458
QPAKentry *entry = qpak_find_entry(info, name, &isDir);
460
461
462
} /* QPAK_exists */
463
static int QPAK_isDirectory(dvoid *opaque, const char *name, int *fileExists)
466
467
468
469
470
int isDir;
QPAKentry *entry = qpak_find_entry(info, name, &isDir);
*fileExists = ((isDir) || (entry != NULL));
if (isDir)
472
473
474
475
476
BAIL_MACRO(ERR_NO_SUCH_FILE, 0);
} /* QPAK_isDirectory */
477
static int QPAK_isSymLink(dvoid *opaque, const char *name, int *fileExists)
479
*fileExists = QPAK_exists(opaque, name);
480
return 0; /* never symlinks in a quake pak. */
481
482
483
} /* QPAK_isSymLink */
484
static PHYSFS_Io *QPAK_openRead(dvoid *opaque, const char *fnm, int *fileExists)
488
489
490
491
492
493
494
495
496
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);
497
finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
498
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
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)
517
518
if (finfo->io != NULL)
finfo->io->destroy(finfo->io);
522
523
524
525
if (io != NULL)
allocator.Free(io);
return NULL;
526
527
528
} /* QPAK_openRead */
529
static PHYSFS_Io *QPAK_openWrite(dvoid *opaque, const char *name)
530
531
532
533
534
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* QPAK_openWrite */
535
static PHYSFS_Io *QPAK_openAppend(dvoid *opaque, const char *name)
536
537
538
539
540
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* QPAK_openAppend */
541
static int QPAK_remove(dvoid *opaque, const char *name)
542
543
544
545
546
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* QPAK_remove */
547
static int QPAK_mkdir(dvoid *opaque, const char *name)
548
549
550
551
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* QPAK_mkdir */
553
static int QPAK_stat(dvoid *opaque, const char *filename, int *exists,
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
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 */
575
576
577
stat->modtime = -1;
stat->createtime = -1;
stat->accesstime = -1;
578
579
stat->readonly = 1;
581
582
583
} /* QPAK_stat */
584
585
586
587
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
{
"PAK",
QPAK_ARCHIVE_DESCRIPTION,
588
"Ryan C. Gordon <icculus@icculus.org>",
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
{
&__PHYSFS_ArchiveInfo_QPAK,
QPAK_openArchive, /* openArchive() method */
QPAK_enumerateFiles, /* enumerateFiles() method */
QPAK_exists, /* exists() method */
QPAK_isDirectory, /* isDirectory() method */
QPAK_isSymLink, /* isSymLink() 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 */
607
QPAK_stat /* stat() method */
610
611
612
#endif /* defined PHYSFS_SUPPORTS_QPAK */
/* end of qpak.c ... */