/
physfs_archiver_qpak.c
126 lines (103 loc) · 3.22 KB
1
2
3
4
/*
* QPAK support routines for PhysicsFS.
*
* This archiver handles the archive format utilized by Quake 1 and 2.
5
* Quake3-based games use the PkZip/Info-Zip format (which our
6
* physfs_archiver_zip.c handles).
7
8
9
10
*
* ========================================================================
*
* This format info (in more detail) comes from:
11
* https://web.archive.org/web/20040209101748/http://debian.fmi.uni-sofia.bg/~sergei/cgsr/docs/pak.txt
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
*
* 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
*
* This file written by Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
35
36
#if PHYSFS_SUPPORTS_QPAK
37
#define QPAK_SIG 0x4B434150 /* "PACK" in ASCII. */
38
39
static int qpakLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
40
{
41
42
PHYSFS_uint32 i;
for (i = 0; i < count; i++)
43
{
44
PHYSFS_uint32 size;
45
PHYSFS_uint32 pos;
46
47
char name[56];
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 56), 0);
48
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &pos, 4), 0);
49
50
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
size = PHYSFS_swapULE32(size);
51
52
pos = PHYSFS_swapULE32(pos);
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0);
53
54
} /* for */
55
return 1;
56
} /* qpakLoadEntries */
57
58
59
60
static void *QPAK_openArchive(PHYSFS_Io *io, const char *name,
int forWriting, int *claimed)
61
{
62
63
64
PHYSFS_uint32 val = 0;
PHYSFS_uint32 pos = 0;
PHYSFS_uint32 count = 0;
65
void *unpkarc;
66
67
assert(io != NULL); /* shouldn't ever happen. */
68
69
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
70
71
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &val, 4), NULL);
72
if (PHYSFS_swapULE32(val) != QPAK_SIG)
73
BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);
74
75
76
*claimed = 1;
77
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &val, 4), NULL);
78
79
pos = PHYSFS_swapULE32(val); /* directory table offset. */
80
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &val, 4), NULL);
81
count = PHYSFS_swapULE32(val);
82
83
/* corrupted archive? */
84
BAIL_IF((count % 64) != 0, PHYSFS_ERR_CORRUPT, NULL);
85
86
count /= 64;
87
BAIL_IF_ERRPASS(!io->seek(io, pos), NULL);
88
89
unpkarc = UNPK_openArchive(io);
90
91
92
93
BAIL_IF_ERRPASS(!unpkarc, NULL);
if (!qpakLoadEntries(io, count, unpkarc))
{
94
UNPK_abandonArchive(unpkarc);
95
96
97
98
return NULL;
} /* if */
return unpkarc;
99
100
101
} /* QPAK_openArchive */
102
103
const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
{
104
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
105
106
107
108
{
"PAK",
"Quake I/II format",
"Ryan C. Gordon <icculus@icculus.org>",
109
"https://icculus.org/physfs/",
110
0, /* supportsSymlinks */
111
},
112
QPAK_openArchive,
113
UNPK_enumerate,
114
115
116
117
118
UNPK_openRead,
UNPK_openWrite,
UNPK_openAppend,
UNPK_remove,
UNPK_mkdir,
119
120
UNPK_stat,
UNPK_closeArchive
121
122
};
123
124
#endif /* defined PHYSFS_SUPPORTS_QPAK */
125
/* end of physfs_archiver_qpak.c ... */