2003-12-15 05:01:18 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*
|
2007-03-11 11:10:28 +01:00
|
|
|
* Please see the file LICENSE.txt in the source's root directory.
|
2003-12-15 05:01:18 +01:00
|
|
|
*
|
|
|
|
* This file written by Travis Wells, based on the GRP archiver by
|
|
|
|
* Ryan C. Gordon.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_WAD
|
|
|
|
|
2010-09-06 08:50:29 +02:00
|
|
|
static UNPKentry *wadLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
|
2003-12-15 05:01:18 +01:00
|
|
|
{
|
|
|
|
PHYSFS_uint32 directoryOffset;
|
2010-09-06 08:50:29 +02:00
|
|
|
UNPKentry *entries = NULL;
|
|
|
|
UNPKentry *entry = NULL;
|
2003-12-18 20:53:20 +01:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &directoryOffset, 4), ERRPASS, 0);
|
2010-08-30 09:01:57 +02:00
|
|
|
directoryOffset = PHYSFS_swapULE32(directoryOffset);
|
2003-12-15 05:01:18 +01:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!io->seek(io, directoryOffset), ERRPASS, 0);
|
2003-12-15 05:01:18 +01:00
|
|
|
|
2010-09-06 08:50:29 +02:00
|
|
|
entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!entries, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2010-09-06 08:50:29 +02:00
|
|
|
|
|
|
|
for (entry = entries; fileCount > 0; fileCount--, entry++)
|
2003-12-15 05:01:18 +01:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
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;
|
2003-12-15 05:01:18 +01:00
|
|
|
|
|
|
|
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 */
|
|
|
|
|
2010-09-06 08:50:29 +02:00
|
|
|
return entries;
|
|
|
|
|
2012-03-09 10:50:27 +01:00
|
|
|
failed:
|
2010-09-06 08:50:29 +02:00
|
|
|
allocator.Free(entries);
|
|
|
|
return NULL;
|
|
|
|
} /* wadLoadEntries */
|
2003-12-15 05:01:18 +01:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static void *WAD_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
|
2003-12-15 05:01:18 +01:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_uint8 buf[4];
|
2010-09-06 08:50:29 +02:00
|
|
|
UNPKentry *entries = NULL;
|
2012-03-09 10:50:27 +01:00
|
|
|
PHYSFS_uint32 count = 0;
|
2003-12-15 05:01:18 +01:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
assert(io != NULL); /* shouldn't ever happen. */
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
|
|
|
|
BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, sizeof (buf)), ERRPASS, NULL);
|
2010-08-30 09:01:57 +02:00
|
|
|
if ((memcmp(buf, "IWAD", 4) != 0) && (memcmp(buf, "PWAD", 4) != 0))
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
|
2003-12-15 05:01:18 +01:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof (count)), ERRPASS, NULL);
|
2012-03-09 10:50:27 +01:00
|
|
|
count = PHYSFS_swapULE32(count);
|
2003-12-15 05:01:18 +01:00
|
|
|
|
2012-03-09 10:50:27 +01:00
|
|
|
entries = wadLoadEntries(io, count);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!entries, ERRPASS, NULL);
|
2012-03-09 10:50:27 +01:00
|
|
|
return UNPK_openArchive(io, entries, count);
|
2003-12-15 05:01:18 +01:00
|
|
|
} /* WAD_openArchive */
|
|
|
|
|
|
|
|
|
2004-09-29 08:18:04 +02:00
|
|
|
const PHYSFS_Archiver __PHYSFS_Archiver_WAD =
|
|
|
|
{
|
2012-03-25 23:17:56 +02:00
|
|
|
{
|
|
|
|
"WAD",
|
|
|
|
"DOOM engine format",
|
|
|
|
"Travis Wells <traviswells@mchsi.com>",
|
|
|
|
"http://www.3dmm2.com/doom/",
|
|
|
|
},
|
2004-09-29 08:18:04 +02:00
|
|
|
WAD_openArchive, /* openArchive() method */
|
2010-09-06 08:50:29 +02:00
|
|
|
UNPK_enumerateFiles, /* enumerateFiles() method */
|
|
|
|
UNPK_openRead, /* openRead() method */
|
|
|
|
UNPK_openWrite, /* openWrite() method */
|
|
|
|
UNPK_openAppend, /* openAppend() method */
|
|
|
|
UNPK_remove, /* remove() method */
|
|
|
|
UNPK_mkdir, /* mkdir() method */
|
2012-03-24 15:36:11 +01:00
|
|
|
UNPK_closeArchive, /* closeArchive() method */
|
2010-09-06 08:50:29 +02:00
|
|
|
UNPK_stat /* stat() method */
|
2004-09-29 08:18:04 +02:00
|
|
|
};
|
|
|
|
|
2003-12-15 05:01:18 +01:00
|
|
|
#endif /* defined PHYSFS_SUPPORTS_WAD */
|
|
|
|
|
|
|
|
/* end of wad.c ... */
|
|
|
|
|