Skip to content

Latest commit

 

History

History
135 lines (113 loc) · 3.93 KB

physfs_archiver_wad.c

File metadata and controls

135 lines (113 loc) · 3.93 KB
 
Dec 15, 2003
Dec 15, 2003
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"
Jul 16, 2017
Jul 16, 2017
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
Dec 15, 2003
Dec 15, 2003
21
22
23
24
25
* the directory
*
* The directory has one 16-byte entry for every lump. Each entry consists
* of three parts:
*
Jul 16, 2017
Jul 16, 2017
26
27
* (a) a uint32, the file offset to the start of the lump
* (b) a uint32, the size of the lump in bytes
Dec 15, 2003
Dec 15, 2003
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.
*
*
Mar 11, 2007
Mar 11, 2007
39
* Please see the file LICENSE.txt in the source's root directory.
Dec 15, 2003
Dec 15, 2003
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"
Mar 23, 2012
Mar 23, 2012
48
49
#if PHYSFS_SUPPORTS_WAD
Jul 16, 2017
Jul 16, 2017
50
static int wadLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
Dec 15, 2003
Dec 15, 2003
51
{
Jul 16, 2017
Jul 16, 2017
52
53
PHYSFS_uint32 i;
for (i = 0; i < count; i++)
Dec 15, 2003
Dec 15, 2003
54
{
Jul 22, 2017
Jul 22, 2017
55
PHYSFS_uint32 pos;
Jul 16, 2017
Jul 16, 2017
56
57
58
PHYSFS_uint32 size;
char name[9];
Jul 22, 2017
Jul 22, 2017
59
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &pos, 4), 0);
Jul 16, 2017
Jul 16, 2017
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);
Jul 22, 2017
Jul 22, 2017
65
66
pos = PHYSFS_swapULE32(pos);
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0);
Dec 15, 2003
Dec 15, 2003
67
68
} /* for */
Jul 16, 2017
Jul 16, 2017
69
return 1;
Sep 6, 2010
Sep 6, 2010
70
} /* wadLoadEntries */
Dec 15, 2003
Dec 15, 2003
71
72
Aug 14, 2017
Aug 14, 2017
73
74
static void *WAD_openArchive(PHYSFS_Io *io, const char *name,
int forWriting, int *claimed)
Dec 15, 2003
Dec 15, 2003
75
{
Aug 30, 2010
Aug 30, 2010
76
PHYSFS_uint8 buf[4];
Jul 16, 2017
Jul 16, 2017
77
78
79
PHYSFS_uint32 count;
PHYSFS_uint32 directoryOffset;
void *unpkarc;
Dec 15, 2003
Dec 15, 2003
80
Aug 30, 2010
Aug 30, 2010
81
82
assert(io != NULL); /* shouldn't ever happen. */
Jul 6, 2017
Jul 6, 2017
83
84
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, sizeof (buf)), NULL);
Aug 30, 2010
Aug 30, 2010
85
if ((memcmp(buf, "IWAD", 4) != 0) && (memcmp(buf, "PWAD", 4) != 0))
Jul 6, 2017
Jul 6, 2017
86
BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);
Dec 15, 2003
Dec 15, 2003
87
Aug 14, 2017
Aug 14, 2017
88
89
*claimed = 1;
Jul 6, 2017
Jul 6, 2017
90
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL);
Mar 9, 2012
Mar 9, 2012
91
count = PHYSFS_swapULE32(count);
Dec 15, 2003
Dec 15, 2003
92
Jul 16, 2017
Jul 16, 2017
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);
Jul 21, 2017
Jul 21, 2017
98
unpkarc = UNPK_openArchive(io);
Jul 16, 2017
Jul 16, 2017
99
100
101
102
BAIL_IF_ERRPASS(!unpkarc, NULL);
if (!wadLoadEntries(io, count, unpkarc))
{
Jul 21, 2017
Jul 21, 2017
103
UNPK_abandonArchive(unpkarc);
Jul 16, 2017
Jul 16, 2017
104
105
106
107
return NULL;
} /* if */
return unpkarc;
Dec 15, 2003
Dec 15, 2003
108
109
110
} /* WAD_openArchive */
Sep 29, 2004
Sep 29, 2004
111
112
const PHYSFS_Archiver __PHYSFS_Archiver_WAD =
{
Nov 28, 2012
Nov 28, 2012
113
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
Mar 25, 2012
Mar 25, 2012
114
115
116
117
118
{
"WAD",
"DOOM engine format",
"Travis Wells <traviswells@mchsi.com>",
"http://www.3dmm2.com/doom/",
Nov 30, 2012
Nov 30, 2012
119
0, /* supportsSymlinks */
Mar 25, 2012
Mar 25, 2012
120
},
Nov 30, 2012
Nov 30, 2012
121
WAD_openArchive,
Aug 12, 2017
Aug 12, 2017
122
UNPK_enumerate,
Nov 30, 2012
Nov 30, 2012
123
124
125
126
127
UNPK_openRead,
UNPK_openWrite,
UNPK_openAppend,
UNPK_remove,
UNPK_mkdir,
Nov 30, 2012
Nov 30, 2012
128
129
UNPK_stat,
UNPK_closeArchive
Sep 29, 2004
Sep 29, 2004
130
131
};
Dec 15, 2003
Dec 15, 2003
132
133
#endif /* defined PHYSFS_SUPPORTS_WAD */
Jul 22, 2017
Jul 22, 2017
134
/* end of physfs_archiver_wad.c ... */