/
physfs_archiver_wad.c
135 lines (113 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
* WAD support routines for PhysicsFS.
*
* This driver handles DOOM engine archives ("wads").
* This format (but not this driver) was designed by id Software for use
* with the DOOM engine.
* The specs of the format are from the unofficial doom specs v1.666
* found here: http://www.gamers.org/dhs/helpdocs/dmsp1666.html
* The format of the archive: (from the specs)
*
* A WAD file has three parts:
* (1) a twelve-byte header
* (2) one or more "lumps"
* (3) a directory or "info table" that contains the names, offsets, and
* sizes of all the lumps in the WAD
*
* The header consists of three four-byte parts:
* (a) an ASCII string which must be either "IWAD" or "PWAD"
19
20
* (b) a uint32 which is the number of lumps in the wad
* (c) a uint32 which is the file offset to the start of
21
22
23
24
25
* the directory
*
* The directory has one 16-byte entry for every lump. Each entry consists
* of three parts:
*
26
27
* (a) a uint32, the file offset to the start of the lump
* (b) a uint32, the size of the lump in bytes
28
29
30
31
32
33
34
35
36
37
38
* (c) an 8-byte ASCII string, the name of the lump, padded with zeros.
* For example, the "DEMO1" entry in hexadecimal would be
* (44 45 4D 4F 31 00 00 00)
*
* Note that there is no way to tell if an opened WAD archive is a
* IWAD or PWAD with this archiver.
* I couldn't think of a way to provide that information, without being too
* hacky.
* I don't think it's really that important though.
*
*
39
* Please see the file LICENSE.txt in the source's root directory.
40
41
42
43
44
45
46
47
*
* This file written by Travis Wells, based on the GRP archiver by
* Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
48
49
#if PHYSFS_SUPPORTS_WAD
50
static int wadLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
51
{
52
53
PHYSFS_uint32 i;
for (i = 0; i < count; i++)
54
{
55
PHYSFS_uint32 pos;
56
57
58
PHYSFS_uint32 size;
char name[9];
59
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &pos, 4), 0);
60
61
62
63
64
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 8), 0);
name[8] = '\0'; /* name might not be null-terminated in file. */
size = PHYSFS_swapULE32(size);
65
66
pos = PHYSFS_swapULE32(pos);
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0);
67
68
} /* for */
69
return 1;
70
} /* wadLoadEntries */
71
72
73
74
static void *WAD_openArchive(PHYSFS_Io *io, const char *name,
int forWriting, int *claimed)
75
{
76
PHYSFS_uint8 buf[4];
77
78
79
PHYSFS_uint32 count;
PHYSFS_uint32 directoryOffset;
void *unpkarc;
80
81
82
assert(io != NULL); /* shouldn't ever happen. */
83
84
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, sizeof (buf)), NULL);
85
if ((memcmp(buf, "IWAD", 4) != 0) && (memcmp(buf, "PWAD", 4) != 0))
86
BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);
87
88
89
*claimed = 1;
90
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL);
91
count = PHYSFS_swapULE32(count);
92
93
94
95
96
97
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &directoryOffset, 4), 0);
directoryOffset = PHYSFS_swapULE32(directoryOffset);
BAIL_IF_ERRPASS(!io->seek(io, directoryOffset), 0);
98
unpkarc = UNPK_openArchive(io);
99
100
101
102
BAIL_IF_ERRPASS(!unpkarc, NULL);
if (!wadLoadEntries(io, count, unpkarc))
{
103
UNPK_abandonArchive(unpkarc);
104
105
106
107
return NULL;
} /* if */
return unpkarc;
108
109
110
} /* WAD_openArchive */
111
112
const PHYSFS_Archiver __PHYSFS_Archiver_WAD =
{
113
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
114
115
116
117
118
{
"WAD",
"DOOM engine format",
"Travis Wells <traviswells@mchsi.com>",
"http://www.3dmm2.com/doom/",
119
0, /* supportsSymlinks */
120
},
121
WAD_openArchive,
122
UNPK_enumerate,
123
124
125
126
127
UNPK_openRead,
UNPK_openWrite,
UNPK_openAppend,
UNPK_remove,
UNPK_mkdir,
128
129
UNPK_stat,
UNPK_closeArchive
130
131
};
132
133
#endif /* defined PHYSFS_SUPPORTS_WAD */
134
/* end of physfs_archiver_wad.c ... */