/
archiver_qpak.c
663 lines (524 loc) · 17.9 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
#if 1 /* Make this case insensitive? */
43
44
#define QPAK_strcmp(x, y) __PHYSFS_stricmpASCII(x, y)
#define QPAK_strncmp(x, y, z) __PHYSFS_strnicmpASCII(x, y, z)
45
46
47
48
49
50
#else
#define QPAK_strcmp(x, y) strcmp(x, y)
#define QPAK_strncmp(x, y, z) strncmp(x, y, z)
#endif
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
typedef struct
{
char name[56];
PHYSFS_uint32 startPos;
PHYSFS_uint32 size;
} QPAKentry;
typedef struct
{
char *filename;
PHYSFS_sint64 last_mod_time;
PHYSFS_uint32 entryCount;
QPAKentry *entries;
} QPAKinfo;
typedef struct
{
void *handle;
QPAKentry *entry;
PHYSFS_uint32 curPos;
} QPAKfileinfo;
/* Magic numbers... */
74
#define QPAK_SIG 0x4b434150 /* "PACK" in ASCII. */
75
76
77
static void QPAK_dirClose(dvoid *opaque)
78
{
79
QPAKinfo *info = ((QPAKinfo *) opaque);
80
81
82
allocator.Free(info->filename);
allocator.Free(info->entries);
allocator.Free(info);
83
84
85
} /* QPAK_dirClose */
86
static PHYSFS_sint64 QPAK_read(fvoid *opaque, void *buffer,
87
88
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{
89
QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
90
91
92
93
94
95
96
97
98
99
100
101
QPAKentry *entry = finfo->entry;
PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
PHYSFS_sint64 rc;
if (objsLeft < objCount)
objCount = objsLeft;
rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
if (rc > 0)
finfo->curPos += (PHYSFS_uint32) (rc * objSize);
102
return rc;
103
104
105
} /* QPAK_read */
106
static PHYSFS_sint64 QPAK_write(fvoid *opaque, const void *buffer,
107
108
109
110
111
112
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* QPAK_write */
113
static int QPAK_eof(fvoid *opaque)
114
{
115
QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
116
QPAKentry *entry = finfo->entry;
117
return (finfo->curPos >= entry->size);
118
119
120
} /* QPAK_eof */
121
static PHYSFS_sint64 QPAK_tell(fvoid *opaque)
122
{
123
return ((QPAKfileinfo *) opaque)->curPos;
124
125
126
} /* QPAK_tell */
127
static int QPAK_seek(fvoid *opaque, PHYSFS_uint64 offset)
128
{
129
QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
130
131
132
133
134
135
136
137
138
QPAKentry *entry = finfo->entry;
int rc;
BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
if (rc)
finfo->curPos = (PHYSFS_uint32) offset;
139
return rc;
140
141
142
} /* QPAK_seek */
143
static PHYSFS_sint64 QPAK_fileLength(fvoid *opaque)
144
{
145
QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
146
return ((PHYSFS_sint64) finfo->entry->size);
147
148
149
} /* QPAK_fileLength */
150
static int QPAK_fileClose(fvoid *opaque)
151
{
152
QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
153
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
154
allocator.Free(finfo);
155
return 1;
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
} /* QPAK_fileClose */
static int qpak_open(const char *filename, int forWriting,
void **fh, PHYSFS_uint32 *count)
{
PHYSFS_uint32 buf;
*fh = NULL;
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
*fh = __PHYSFS_platformOpenRead(filename);
BAIL_IF_MACRO(*fh == NULL, NULL, 0);
if (__PHYSFS_platformRead(*fh, &buf, sizeof (PHYSFS_uint32), 1) != 1)
goto openQpak_failed;
buf = PHYSFS_swapULE32(buf);
174
GOTO_IF_MACRO(buf != QPAK_SIG, ERR_UNSUPPORTED_ARCHIVE, openQpak_failed);
175
176
177
178
179
180
181
182
183
184
185
if (__PHYSFS_platformRead(*fh, &buf, sizeof (PHYSFS_uint32), 1) != 1)
goto openQpak_failed;
buf = PHYSFS_swapULE32(buf); /* directory table offset. */
if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
goto openQpak_failed;
*count = PHYSFS_swapULE32(*count);
186
187
/* corrupted archive? */
GOTO_IF_MACRO((*count % 64) != 0, ERR_CORRUPTED, openQpak_failed);
188
189
190
191
192
if (!__PHYSFS_platformSeek(*fh, buf))
goto openQpak_failed;
*count /= 64;
193
return 1;
194
195
196
197
198
199
200
openQpak_failed:
if (*fh != NULL)
__PHYSFS_platformClose(*fh);
*count = -1;
*fh = NULL;
201
return 0;
202
203
204
205
206
207
208
209
210
211
212
213
} /* qpak_open */
static int QPAK_isArchive(const char *filename, int forWriting)
{
void *fh;
PHYSFS_uint32 fileCount;
int retval = qpak_open(filename, forWriting, &fh, &fileCount);
if (fh != NULL)
__PHYSFS_platformClose(fh);
214
return retval;
215
216
217
218
219
} /* QPAK_isArchive */
static int qpak_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
{
220
221
222
if (one != two)
{
const QPAKentry *a = (const QPAKentry *) _a;
223
return QPAK_strcmp(a[one].name, a[two].name);
224
225
226
} /* if */
return 0;
227
228
229
230
231
} /* qpak_entry_cmp */
static void qpak_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
{
232
233
234
235
236
237
238
239
240
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 */
241
242
243
244
245
246
247
248
249
250
251
} /* qpak_entry_swap */
static int qpak_load_entries(const char *name, int forWriting, QPAKinfo *info)
{
void *fh = NULL;
PHYSFS_uint32 fileCount;
QPAKentry *entry;
BAIL_IF_MACRO(!qpak_open(name, forWriting, &fh, &fileCount), NULL, 0);
info->entryCount = fileCount;
252
info->entries = (QPAKentry*) allocator.Malloc(sizeof(QPAKentry)*fileCount);
253
254
255
256
257
258
259
260
261
262
263
264
265
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
} /* if */
for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
PHYSFS_uint32 loc;
if (__PHYSFS_platformRead(fh,&entry->name,sizeof(entry->name),1) != 1)
{
__PHYSFS_platformClose(fh);
266
return 0;
267
268
269
270
271
} /* if */
if (__PHYSFS_platformRead(fh,&loc,sizeof(loc),1) != 1)
{
__PHYSFS_platformClose(fh);
272
return 0;
273
274
275
276
277
} /* if */
if (__PHYSFS_platformRead(fh,&entry->size,sizeof(entry->size),1) != 1)
{
__PHYSFS_platformClose(fh);
278
return 0;
279
280
281
282
283
284
285
286
287
288
} /* if */
entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = PHYSFS_swapULE32(loc);
} /* for */
__PHYSFS_platformClose(fh);
__PHYSFS_sort(info->entries, info->entryCount,
qpak_entry_cmp, qpak_entry_swap);
289
return 1;
290
291
292
} /* qpak_load_entries */
293
static void *QPAK_openArchive(const char *name, int forWriting)
294
{
295
QPAKinfo *info = (QPAKinfo *) allocator.Malloc(sizeof (QPAKinfo));
296
297
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
298
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
299
300
memset(info, '\0', sizeof (QPAKinfo));
301
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
302
303
304
305
306
307
308
309
310
311
312
if (info->filename == NULL)
{
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
goto QPAK_openArchive_failed;
} /* if */
if (!qpak_load_entries(name, forWriting, info))
goto QPAK_openArchive_failed;
strcpy(info->filename, name);
info->last_mod_time = modtime;
313
return info;
314
315
QPAK_openArchive_failed:
316
if (info != NULL)
317
{
318
if (info->filename != NULL)
319
allocator.Free(info->filename);
320
if (info->entries != NULL)
321
322
allocator.Free(info->entries);
allocator.Free(info);
323
324
} /* if */
325
return NULL;
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
} /* 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? */
341
return 0;
342
343
344
345
346
347
348
349
if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
dlen--;
while (lo <= hi)
{
middle = lo + ((hi - lo) / 2);
name = info->entries[middle].name;
350
rc = QPAK_strncmp(path, name, dlen);
351
352
353
354
355
356
357
358
359
360
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? */
361
return middle;
362
363
if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
364
return (middle + 1);
365
366
367
368
369
370
371
372
373
374
375
376
377
/* 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 */
378
return retval;
379
380
381
} /* qpak_find_start_of_dir */
382
383
384
385
/*
* Moved to seperate function so we can use alloca then immediately throw
* away the allocated stack space...
*/
386
387
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
const char *odir, const char *str, PHYSFS_sint32 ln)
388
{
389
char *newstr = __PHYSFS_smallAlloc(ln + 1);
390
391
392
393
394
if (newstr == NULL)
return;
memcpy(newstr, str, ln);
newstr[ln] = '\0';
395
cb(callbackdata, odir, newstr);
396
__PHYSFS_smallFree(newstr);
397
398
399
400
} /* doEnumCallback */
static void QPAK_enumerateFiles(dvoid *opaque, const char *dname,
401
402
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
403
{
404
QPAKinfo *info = ((QPAKinfo *) opaque);
405
406
PHYSFS_sint32 dlen, dlen_inc, max, i;
407
408
409
i = qpak_find_start_of_dir(info, dname, 0);
if (i == -1) /* no such directory. */
return;
410
411
412
dlen = strlen(dname);
if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
413
414
415
416
417
418
419
420
421
422
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;
423
if ((dlen) && ((QPAK_strncmp(e, dname, dlen)) || (e[dlen] != '/')))
424
425
426
427
428
break; /* past end of this dir; we're done. */
add = e + dlen_inc;
ptr = strchr(add, '/');
ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
429
doEnumCallback(cb, callbackdata, origdir, add, ln);
430
431
432
433
434
435
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;
436
if ((QPAK_strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
437
438
439
440
441
442
443
444
445
446
447
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.
*/
448
449
static QPAKentry *qpak_find_entry(const QPAKinfo *info, const char *path,
int *isDir)
450
451
452
453
454
455
456
457
458
459
460
461
462
{
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;
463
rc = QPAK_strncmp(path, thispath, pathlen);
464
465
466
467
468
469
470
471
472
473
474
475
476
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)
477
return NULL;
478
479
480
} /* if */
if (thispath[pathlen] == '\0') /* found entry? */
481
return &a[middle];
482
483
484
485
486
487
488
489
490
491
492
493
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 */
494
static int QPAK_exists(dvoid *opaque, const char *name)
495
496
{
int isDir;
497
QPAKinfo *info = (QPAKinfo *) opaque;
498
QPAKentry *entry = qpak_find_entry(info, name, &isDir);
499
return ((entry != NULL) || (isDir));
500
501
502
} /* QPAK_exists */
503
static int QPAK_isDirectory(dvoid *opaque, const char *name, int *fileExists)
504
{
505
QPAKinfo *info = (QPAKinfo *) opaque;
506
507
508
509
510
int isDir;
QPAKentry *entry = qpak_find_entry(info, name, &isDir);
*fileExists = ((isDir) || (entry != NULL));
if (isDir)
511
return 1; /* definitely a dir. */
512
513
514
515
516
BAIL_MACRO(ERR_NO_SUCH_FILE, 0);
} /* QPAK_isDirectory */
517
static int QPAK_isSymLink(dvoid *opaque, const char *name, int *fileExists)
518
{
519
*fileExists = QPAK_exists(opaque, name);
520
return 0; /* never symlinks in a quake pak. */
521
522
523
} /* QPAK_isSymLink */
524
static PHYSFS_sint64 QPAK_getLastModTime(dvoid *opaque,
525
526
527
528
const char *name,
int *fileExists)
{
int isDir;
529
QPAKinfo *info = ((QPAKinfo *) opaque);
530
531
532
533
534
535
536
PHYSFS_sint64 retval = -1;
QPAKentry *entry = qpak_find_entry(info, name, &isDir);
*fileExists = ((isDir) || (entry != NULL));
if (*fileExists) /* use time of QPAK itself in the physical filesystem. */
retval = info->last_mod_time;
537
return retval;
538
539
540
} /* QPAK_getLastModTime */
541
static fvoid *QPAK_openRead(dvoid *opaque, const char *fnm, int *fileExists)
542
{
543
QPAKinfo *info = ((QPAKinfo *) opaque);
544
545
546
547
548
549
550
551
552
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);
553
finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
554
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
555
556
557
558
559
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
if ( (finfo->handle == NULL) ||
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
{
560
allocator.Free(finfo);
561
return NULL;
562
563
564
565
} /* if */
finfo->curPos = 0;
finfo->entry = entry;
566
return finfo;
567
568
569
} /* QPAK_openRead */
570
static fvoid *QPAK_openWrite(dvoid *opaque, const char *name)
571
572
573
574
575
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* QPAK_openWrite */
576
static fvoid *QPAK_openAppend(dvoid *opaque, const char *name)
577
578
579
580
581
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* QPAK_openAppend */
582
static int QPAK_remove(dvoid *opaque, const char *name)
583
584
585
586
587
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* QPAK_remove */
588
static int QPAK_mkdir(dvoid *opaque, const char *name)
589
590
591
592
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* QPAK_mkdir */
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
static int QPAK_stat(fvoid *opaque, const char *filename, int *exists,
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 */
stat->modtime = info->last_mod_time;
stat->createtime = info->last_mod_time;
stat->accesstime = 0;
stat->readonly = 1;
return 0;
} /* QPAK_stat */
625
626
627
628
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
{
"PAK",
QPAK_ARCHIVE_DESCRIPTION,
629
"Ryan C. Gordon <icculus@icculus.org>",
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
{
&__PHYSFS_ArchiveInfo_QPAK,
QPAK_isArchive, /* isArchive() method */
QPAK_openArchive, /* openArchive() method */
QPAK_enumerateFiles, /* enumerateFiles() method */
QPAK_exists, /* exists() method */
QPAK_isDirectory, /* isDirectory() method */
QPAK_isSymLink, /* isSymLink() method */
QPAK_getLastModTime, /* getLastModTime() 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 */
QPAK_read, /* read() method */
QPAK_write, /* write() method */
QPAK_eof, /* eof() method */
QPAK_tell, /* tell() method */
QPAK_seek, /* seek() method */
QPAK_fileLength, /* fileLength() method */
656
657
QPAK_fileClose, /* fileClose() method */
QPAK_stat /* stat() method */
658
659
};
660
661
662
#endif /* defined PHYSFS_SUPPORTS_QPAK */
/* end of qpak.c ... */