/
archiver_qpak.c
121 lines (98 loc) · 3.25 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
*
* 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 UNPKentry *qpakLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
40
{
41
42
UNPKentry *entries = NULL;
UNPKentry *entry = NULL;
43
44
45
entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
BAIL_IF_MACRO(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
46
47
for (entry = entries; fileCount > 0; fileCount--, entry++)
48
{
49
50
51
if (!__PHYSFS_readAll(io, &entry->name, 56)) goto failed;
if (!__PHYSFS_readAll(io, &entry->startPos, 4)) goto failed;
if (!__PHYSFS_readAll(io, &entry->size, 4)) goto failed;
52
entry->size = PHYSFS_swapULE32(entry->size);
53
entry->startPos = PHYSFS_swapULE32(entry->startPos);
54
55
} /* for */
56
57
58
59
60
61
return entries;
failed:
allocator.Free(entries);
return NULL;
} /* qpakLoadEntries */
62
63
64
static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
65
{
66
UNPKentry *entries = NULL;
67
68
69
PHYSFS_uint32 val = 0;
PHYSFS_uint32 pos = 0;
PHYSFS_uint32 count = 0;
70
71
assert(io != NULL); /* shouldn't ever happen. */
72
73
BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
74
75
76
77
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), ERRPASS, NULL);
if (PHYSFS_swapULE32(val) != QPAK_SIG)
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
78
79
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), ERRPASS, NULL);
80
81
pos = PHYSFS_swapULE32(val); /* directory table offset. */
82
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), ERRPASS, NULL);
83
count = PHYSFS_swapULE32(val);
84
85
/* corrupted archive? */
86
BAIL_IF_MACRO((count % 64) != 0, PHYSFS_ERR_CORRUPT, NULL);
87
88
count /= 64;
89
BAIL_IF_MACRO(!io->seek(io, pos), ERRPASS, NULL);
90
91
92
93
entries = qpakLoadEntries(io, count);
BAIL_IF_MACRO(!entries, ERRPASS, NULL);
return UNPK_openArchive(io, entries, count);
94
95
96
} /* QPAK_openArchive */
97
98
const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
{
99
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
100
101
102
103
104
{
"PAK",
"Quake I/II format",
"Ryan C. Gordon <icculus@icculus.org>",
"http://icculus.org/physfs/",
105
0, /* supportsSymlinks */
106
},
107
108
109
110
111
112
113
QPAK_openArchive,
UNPK_enumerateFiles,
UNPK_openRead,
UNPK_openWrite,
UNPK_openAppend,
UNPK_remove,
UNPK_mkdir,
114
115
UNPK_stat,
UNPK_closeArchive
116
117
};
118
119
#endif /* defined PHYSFS_SUPPORTS_QPAK */
120
/* end of archiver_qpak.c ... */