Skip to content

Latest commit

 

History

History
132 lines (111 loc) · 3.87 KB

archiver_wad.c

File metadata and controls

132 lines (111 loc) · 3.87 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 30, 2010
Aug 30, 2010
73
static void *WAD_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
Dec 15, 2003
Dec 15, 2003
74
{
Aug 30, 2010
Aug 30, 2010
75
PHYSFS_uint8 buf[4];
Jul 16, 2017
Jul 16, 2017
76
77
78
PHYSFS_uint32 count;
PHYSFS_uint32 directoryOffset;
void *unpkarc;
Dec 15, 2003
Dec 15, 2003
79
Aug 30, 2010
Aug 30, 2010
80
81
assert(io != NULL); /* shouldn't ever happen. */
Jul 6, 2017
Jul 6, 2017
82
83
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, sizeof (buf)), NULL);
Aug 30, 2010
Aug 30, 2010
84
if ((memcmp(buf, "IWAD", 4) != 0) && (memcmp(buf, "PWAD", 4) != 0))
Jul 6, 2017
Jul 6, 2017
85
BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);
Dec 15, 2003
Dec 15, 2003
86
Jul 6, 2017
Jul 6, 2017
87
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL);
Mar 9, 2012
Mar 9, 2012
88
count = PHYSFS_swapULE32(count);
Dec 15, 2003
Dec 15, 2003
89
Jul 16, 2017
Jul 16, 2017
90
91
92
93
94
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
95
unpkarc = UNPK_openArchive(io);
Jul 16, 2017
Jul 16, 2017
96
97
98
99
BAIL_IF_ERRPASS(!unpkarc, NULL);
if (!wadLoadEntries(io, count, unpkarc))
{
Jul 21, 2017
Jul 21, 2017
100
UNPK_abandonArchive(unpkarc);
Jul 16, 2017
Jul 16, 2017
101
102
103
104
return NULL;
} /* if */
return unpkarc;
Dec 15, 2003
Dec 15, 2003
105
106
107
} /* WAD_openArchive */
Sep 29, 2004
Sep 29, 2004
108
109
const PHYSFS_Archiver __PHYSFS_Archiver_WAD =
{
Nov 28, 2012
Nov 28, 2012
110
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
Mar 25, 2012
Mar 25, 2012
111
112
113
114
115
{
"WAD",
"DOOM engine format",
"Travis Wells <traviswells@mchsi.com>",
"http://www.3dmm2.com/doom/",
Nov 30, 2012
Nov 30, 2012
116
0, /* supportsSymlinks */
Mar 25, 2012
Mar 25, 2012
117
},
Nov 30, 2012
Nov 30, 2012
118
119
120
121
122
123
124
WAD_openArchive,
UNPK_enumerateFiles,
UNPK_openRead,
UNPK_openWrite,
UNPK_openAppend,
UNPK_remove,
UNPK_mkdir,
Nov 30, 2012
Nov 30, 2012
125
126
UNPK_stat,
UNPK_closeArchive
Sep 29, 2004
Sep 29, 2004
127
128
};
Dec 15, 2003
Dec 15, 2003
129
130
#endif /* defined PHYSFS_SUPPORTS_WAD */
Nov 27, 2012
Nov 27, 2012
131
/* end of archiver_wad.c ... */