/
physfs_archiver_qpak.c
123 lines (101 loc) · 3.15 KB
1
2
3
4
/*
* QPAK support routines for PhysicsFS.
*
* This archiver handles the archive format utilized by Quake 1 and 2.
5
6
* Quake3-based games use the PkZip/Info-Zip format (which our
* 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
static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
60
{
61
62
63
PHYSFS_uint32 val = 0;
PHYSFS_uint32 pos = 0;
PHYSFS_uint32 count = 0;
64
void *unpkarc;
65
66
assert(io != NULL); /* shouldn't ever happen. */
67
68
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
69
70
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &val, 4), NULL);
71
if (PHYSFS_swapULE32(val) != QPAK_SIG)
72
BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);
73
74
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &val, 4), NULL);
75
76
pos = PHYSFS_swapULE32(val); /* directory table offset. */
77
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &val, 4), NULL);
78
count = PHYSFS_swapULE32(val);
79
80
/* corrupted archive? */
81
BAIL_IF((count % 64) != 0, PHYSFS_ERR_CORRUPT, NULL);
82
83
count /= 64;
84
BAIL_IF_ERRPASS(!io->seek(io, pos), NULL);
85
86
unpkarc = UNPK_openArchive(io);
87
88
89
90
BAIL_IF_ERRPASS(!unpkarc, NULL);
if (!qpakLoadEntries(io, count, unpkarc))
{
91
UNPK_abandonArchive(unpkarc);
92
93
94
95
return NULL;
} /* if */
return unpkarc;
96
97
98
} /* QPAK_openArchive */
99
100
const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
{
101
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
102
103
104
105
{
"PAK",
"Quake I/II format",
"Ryan C. Gordon <icculus@icculus.org>",
106
"https://icculus.org/physfs/",
107
0, /* supportsSymlinks */
108
},
109
110
111
112
113
114
115
QPAK_openArchive,
UNPK_enumerateFiles,
UNPK_openRead,
UNPK_openWrite,
UNPK_openAppend,
UNPK_remove,
UNPK_mkdir,
116
117
UNPK_stat,
UNPK_closeArchive
118
119
};
120
121
#endif /* defined PHYSFS_SUPPORTS_QPAK */
122
/* end of archiver_qpak.c ... */