Skip to content

Latest commit

 

History

History
129 lines (108 loc) · 4.04 KB

archiver_wad.c

File metadata and controls

129 lines (108 loc) · 4.04 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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* 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"
* (b) a 4-byte (long) integer which is the number of lumps in the wad
* (c) a long integer which is the file offset to the start of
* the directory
*
* The directory has one 16-byte entry for every lump. Each entry consists
* of three parts:
*
* (a) a long integer, the file offset to the start of the lump
* (b) a long integer, the size of the lump in bytes
* (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
Sep 6, 2010
Sep 6, 2010
50
static UNPKentry *wadLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
Dec 15, 2003
Dec 15, 2003
51
52
{
PHYSFS_uint32 directoryOffset;
Sep 6, 2010
Sep 6, 2010
53
54
UNPKentry *entries = NULL;
UNPKentry *entry = NULL;
Dec 18, 2003
Dec 18, 2003
55
Mar 20, 2012
Mar 20, 2012
56
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &directoryOffset, 4), ERRPASS, 0);
Aug 30, 2010
Aug 30, 2010
57
directoryOffset = PHYSFS_swapULE32(directoryOffset);
Dec 15, 2003
Dec 15, 2003
58
Mar 20, 2012
Mar 20, 2012
59
BAIL_IF_MACRO(!io->seek(io, directoryOffset), ERRPASS, 0);
Dec 15, 2003
Dec 15, 2003
60
Sep 6, 2010
Sep 6, 2010
61
entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
Mar 20, 2012
Mar 20, 2012
62
BAIL_IF_MACRO(!entries, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Sep 6, 2010
Sep 6, 2010
63
64
for (entry = entries; fileCount > 0; fileCount--, entry++)
Dec 15, 2003
Dec 15, 2003
65
{
Mar 20, 2012
Mar 20, 2012
66
67
68
if (!__PHYSFS_readAll(io, &entry->startPos, 4)) goto failed;
if (!__PHYSFS_readAll(io, &entry->size, 4)) goto failed;
if (!__PHYSFS_readAll(io, &entry->name, 8)) goto failed;
Dec 15, 2003
Dec 15, 2003
69
70
71
72
73
74
entry->name[8] = '\0'; /* name might not be null-terminated in file. */
entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = PHYSFS_swapULE32(entry->startPos);
} /* for */
Sep 6, 2010
Sep 6, 2010
75
76
return entries;
Mar 9, 2012
Mar 9, 2012
77
failed:
Sep 6, 2010
Sep 6, 2010
78
79
80
allocator.Free(entries);
return NULL;
} /* wadLoadEntries */
Dec 15, 2003
Dec 15, 2003
81
82
Aug 30, 2010
Aug 30, 2010
83
static void *WAD_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
Dec 15, 2003
Dec 15, 2003
84
{
Aug 30, 2010
Aug 30, 2010
85
PHYSFS_uint8 buf[4];
Sep 6, 2010
Sep 6, 2010
86
UNPKentry *entries = NULL;
Mar 9, 2012
Mar 9, 2012
87
PHYSFS_uint32 count = 0;
Dec 15, 2003
Dec 15, 2003
88
Aug 30, 2010
Aug 30, 2010
89
90
assert(io != NULL); /* shouldn't ever happen. */
Mar 20, 2012
Mar 20, 2012
91
92
BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, sizeof (buf)), ERRPASS, NULL);
Aug 30, 2010
Aug 30, 2010
93
if ((memcmp(buf, "IWAD", 4) != 0) && (memcmp(buf, "PWAD", 4) != 0))
Mar 20, 2012
Mar 20, 2012
94
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
Dec 15, 2003
Dec 15, 2003
95
Mar 20, 2012
Mar 20, 2012
96
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof (count)), ERRPASS, NULL);
Mar 9, 2012
Mar 9, 2012
97
count = PHYSFS_swapULE32(count);
Dec 15, 2003
Dec 15, 2003
98
Mar 9, 2012
Mar 9, 2012
99
entries = wadLoadEntries(io, count);
Mar 20, 2012
Mar 20, 2012
100
BAIL_IF_MACRO(!entries, ERRPASS, NULL);
Mar 9, 2012
Mar 9, 2012
101
return UNPK_openArchive(io, entries, count);
Dec 15, 2003
Dec 15, 2003
102
103
104
} /* WAD_openArchive */
Sep 29, 2004
Sep 29, 2004
105
106
const PHYSFS_Archiver __PHYSFS_Archiver_WAD =
{
Nov 28, 2012
Nov 28, 2012
107
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
Mar 25, 2012
Mar 25, 2012
108
109
110
111
112
{
"WAD",
"DOOM engine format",
"Travis Wells <traviswells@mchsi.com>",
"http://www.3dmm2.com/doom/",
Nov 30, 2012
Nov 30, 2012
113
0, /* supportsSymlinks */
Mar 25, 2012
Mar 25, 2012
114
},
Nov 30, 2012
Nov 30, 2012
115
116
117
118
119
120
121
122
123
WAD_openArchive,
UNPK_enumerateFiles,
UNPK_openRead,
UNPK_openWrite,
UNPK_openAppend,
UNPK_remove,
UNPK_mkdir,
UNPK_closeArchive,
UNPK_stat
Sep 29, 2004
Sep 29, 2004
124
125
};
Dec 15, 2003
Dec 15, 2003
126
127
#endif /* defined PHYSFS_SUPPORTS_WAD */
Nov 27, 2012
Nov 27, 2012
128
/* end of archiver_wad.c ... */