archivers/wad.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 26 Sep 2004 00:25:04 +0000
changeset 648 5c993684b8f2
parent 622 c8e67ca63ad6
child 650 298b8bb26775
permissions -rw-r--r--
Cleaned up archiver interface to not deal with DirHandles anymore, which simplifies things, removes some responsibility and code duplication from the archivers, and trims some malloc pressure. Also ripped up the allocation hook code a little. We'll try to screw with memory locking later, since it makes everything ugly and complex. Oh well.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * WAD support routines for PhysicsFS.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * This driver handles DOOM engine archives ("wads"). 
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 * This format (but not this driver) was designed by id Software for use
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *  with the DOOM engine.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 * The specs of the format are from the unofficial doom specs v1.666
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 * found here: http://www.gamers.org/dhs/helpdocs/dmsp1666.html
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 * The format of the archive: (from the specs)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *  A WAD file has three parts:
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 *  (1) a twelve-byte header
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *  (2) one or more "lumps"
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *  (3) a directory or "info table" that contains the names, offsets, and
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
 *      sizes of all the lumps in the WAD
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
 *  The header consists of three four-byte parts:
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
 *    (a) an ASCII string which must be either "IWAD" or "PWAD"
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
 *    (b) a 4-byte (long) integer which is the number of lumps in the wad
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
 *    (c) a long integer which is the file offset to the start of
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
 *    the directory
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *  The directory has one 16-byte entry for every lump. Each entry consists
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
 *  of three parts:
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
 *    (a) a long integer, the file offset to the start of the lump
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
 *    (b) a long integer, the size of the lump in bytes
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
 *    (c) an 8-byte ASCII string, the name of the lump, padded with zeros.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
 *        For example, the "DEMO1" entry in hexadecimal would be
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
 *        (44 45 4D 4F 31 00 00 00)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
 * 
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
 * Note that there is no way to tell if an opened WAD archive is a
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
 *  IWAD or PWAD with this archiver.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
 * I couldn't think of a way to provide that information, without being too
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
 *  hacky.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
 * I don't think it's really that important though.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
 * Please see the file LICENSE in the source's root directory.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
 *
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
 * This file written by Travis Wells, based on the GRP archiver by
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
 *  Ryan C. Gordon.
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
 */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
#if HAVE_CONFIG_H
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
#  include <config.h>
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
#endif
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
#if (defined PHYSFS_SUPPORTS_WAD)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
#include <stdio.h>
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
#include <stdlib.h>
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
#include <string.h>
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
#include "physfs.h"
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
#define __PHYSICSFS_INTERNAL__
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
#include "physfs_internal.h"
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
typedef struct
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
{
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
    61
    char name[18];
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    PHYSFS_uint32 startPos;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    PHYSFS_uint32 size;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
} WADentry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
typedef struct
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    char *filename;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    PHYSFS_sint64 last_mod_time;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    PHYSFS_uint32 entryCount;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    PHYSFS_uint32 entryOffset;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    WADentry *entries;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
} WADinfo;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
typedef struct
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    void *handle;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    WADentry *entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    PHYSFS_uint32 curPos;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
} WADfileinfo;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
    83
static void WAD_dirClose(void *opaque);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
static PHYSFS_sint64 WAD_read(FileHandle *handle, void *buffer,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
                              PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
static PHYSFS_sint64 WAD_write(FileHandle *handle, const void *buffer,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
static int WAD_eof(FileHandle *handle);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
static PHYSFS_sint64 WAD_tell(FileHandle *handle);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
static int WAD_seek(FileHandle *handle, PHYSFS_uint64 offset);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
static PHYSFS_sint64 WAD_fileLength(FileHandle *handle);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
static int WAD_fileClose(FileHandle *handle);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
static int WAD_isArchive(const char *filename, int forWriting);
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
    94
static void *WAD_openArchive(const char *name, int forWriting);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
    95
static LinkedStringList *WAD_enumerateFiles(void *opaque,
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
                                            const char *dirname,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
                                            int omitSymLinks);
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
    98
static int WAD_exists(void *opaque, const char *name);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
    99
static int WAD_isDirectory(void *opaque, const char *name, int *fileExists);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   100
static int WAD_isSymLink(void *opaque, const char *name, int *fileExists);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   101
static PHYSFS_sint64 WAD_getLastModTime(void *opaque, const char *n, int *e);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   102
static FileHandle *WAD_openRead(void *opaque, const char *name, int *exist);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   103
static FileHandle *WAD_openWrite(void *opaque, const char *name);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   104
static FileHandle *WAD_openAppend(void *opaque, const char *name);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   105
static int WAD_remove(void *opaque, const char *name);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   106
static int WAD_mkdir(void *opaque, const char *name);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD =
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    "WAD",
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
    /* !!! FIXME WAD_ARCHIVE_DESCRIPTION, */ "DOOM engine format",
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    "Travis Wells <traviswells@mchsi.com>",
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    "http://www.3dmm2.com/doom/",
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
};
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
static const FileFunctions __PHYSFS_FileFunctions_WAD =
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    WAD_read,       /* read() method       */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    WAD_write,      /* write() method      */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    WAD_eof,        /* eof() method        */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    WAD_tell,       /* tell() method       */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    WAD_seek,       /* seek() method       */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
    WAD_fileLength, /* fileLength() method */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    WAD_fileClose   /* fileClose() method  */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
};
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
const DirFunctions __PHYSFS_DirFunctions_WAD =
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    &__PHYSFS_ArchiveInfo_WAD,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    WAD_isArchive,          /* isArchive() method      */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
    WAD_openArchive,        /* openArchive() method    */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
    WAD_enumerateFiles,     /* enumerateFiles() method */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
    WAD_exists,             /* exists() method         */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    WAD_isDirectory,        /* isDirectory() method    */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    WAD_isSymLink,          /* isSymLink() method      */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    WAD_getLastModTime,     /* getLastModTime() method */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    WAD_openRead,           /* openRead() method       */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    WAD_openWrite,          /* openWrite() method      */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    WAD_openAppend,         /* openAppend() method     */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    WAD_remove,             /* remove() method         */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    WAD_mkdir,              /* mkdir() method          */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    WAD_dirClose            /* dirClose() method       */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
};
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   149
static void WAD_dirClose(void *opaque)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   151
    WADinfo *info = ((WADinfo *) opaque);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    free(info->filename);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
    free(info->entries);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    free(info);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
} /* WAD_dirClose */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
static PHYSFS_sint64 WAD_read(FileHandle *handle, void *buffer,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
                              PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    WADfileinfo *finfo = (WADfileinfo *) (handle->opaque);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    WADentry *entry = finfo->entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
    PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    PHYSFS_sint64 rc;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
    if (objsLeft < objCount)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
        objCount = objsLeft;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
    rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
    if (rc > 0)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
        finfo->curPos += (PHYSFS_uint32) (rc * objSize);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
    return(rc);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
} /* WAD_read */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
static PHYSFS_sint64 WAD_write(FileHandle *handle, const void *buffer,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
    BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
} /* WAD_write */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
static int WAD_eof(FileHandle *handle)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
    WADfileinfo *finfo = (WADfileinfo *) (handle->opaque);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
    WADentry *entry = finfo->entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
    return(finfo->curPos >= entry->size);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
} /* WAD_eof */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
static PHYSFS_sint64 WAD_tell(FileHandle *handle)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
    return(((WADfileinfo *) (handle->opaque))->curPos);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
} /* WAD_tell */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
static int WAD_seek(FileHandle *handle, PHYSFS_uint64 offset)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
    WADfileinfo *finfo = (WADfileinfo *) (handle->opaque);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
    WADentry *entry = finfo->entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
    int rc;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
    rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
    if (rc)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
        finfo->curPos = (PHYSFS_uint32) offset;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
    return(rc);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
} /* WAD_seek */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
static PHYSFS_sint64 WAD_fileLength(FileHandle *handle)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
    WADfileinfo *finfo = ((WADfileinfo *) handle->opaque);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
    return((PHYSFS_sint64) finfo->entry->size);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
} /* WAD_fileLength */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
static int WAD_fileClose(FileHandle *handle)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
    WADfileinfo *finfo = ((WADfileinfo *) handle->opaque);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
    free(finfo);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    free(handle);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    return(1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
} /* WAD_fileClose */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
static int wad_open(const char *filename, int forWriting,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
                    void **fh, PHYSFS_uint32 *count,PHYSFS_uint32 *offset)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    PHYSFS_uint8 buf[4];
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    *fh = NULL;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    *fh = __PHYSFS_platformOpenRead(filename);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
    BAIL_IF_MACRO(*fh == NULL, NULL, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
    
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
    if (__PHYSFS_platformRead(*fh, buf, 4, 1) != 1)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
        goto openWad_failed;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
    if (memcmp(buf, "IWAD", 4) != 0 && memcmp(buf, "PWAD", 4) != 0)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
    {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
        __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
        goto openWad_failed;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
    if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
        goto openWad_failed;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
    *count = PHYSFS_swapULE32(*count);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
    if (__PHYSFS_platformRead(*fh, offset, sizeof (PHYSFS_uint32), 1) != 1)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
        goto openWad_failed;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
    *offset = PHYSFS_swapULE32(*offset);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
    return(1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
openWad_failed:
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    if (*fh != NULL)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
        __PHYSFS_platformClose(*fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
    *count = -1;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    *fh = NULL;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
    return(0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
} /* wad_open */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
static int WAD_isArchive(const char *filename, int forWriting)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
    void *fh;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
    PHYSFS_uint32 fileCount,offset;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
    int retval = wad_open(filename, forWriting, &fh, &fileCount,&offset);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
    if (fh != NULL)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
        __PHYSFS_platformClose(fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
    return(retval);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
} /* WAD_isArchive */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
static int wad_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    WADentry *a = (WADentry *) _a;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
    return(strcmp(a[one].name, a[two].name));
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
} /* wad_entry_cmp */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
static void wad_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
    WADentry tmp;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
    WADentry *first = &(((WADentry *) _a)[one]);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    WADentry *second = &(((WADentry *) _a)[two]);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
    memcpy(&tmp, first, sizeof (WADentry));
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
    memcpy(first, second, sizeof (WADentry));
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
    memcpy(second, &tmp, sizeof (WADentry));
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
} /* wad_entry_swap */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
static int wad_load_entries(const char *name, int forWriting, WADinfo *info)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
    void *fh = NULL;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
    PHYSFS_uint32 fileCount;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
    PHYSFS_uint32 directoryOffset;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
    WADentry *entry;
622
c8e67ca63ad6 Patches to get this building on Mac Classic again.
Ryan C. Gordon <icculus@icculus.org>
parents: 610
diff changeset
   311
    char lastDirectory[9];
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   312
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   313
    lastDirectory[8] = 0; /* Make sure lastDirectory stays null-terminated. */
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
    BAIL_IF_MACRO(!wad_open(name, forWriting, &fh, &fileCount,&directoryOffset), NULL, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
    info->entryCount = fileCount;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
    info->entries = (WADentry *) malloc(sizeof (WADentry) * fileCount);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
    if (info->entries == NULL)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
    {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
        __PHYSFS_platformClose(fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
        BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
    } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
    __PHYSFS_platformSeek(fh,directoryOffset);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
    for (entry = info->entries; fileCount > 0; fileCount--, entry++)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
    {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
        if (__PHYSFS_platformRead(fh, &entry->startPos, 4, 1) != 1)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
        {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
            __PHYSFS_platformClose(fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
            return(0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
        } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
        
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
        if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
        {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
            __PHYSFS_platformClose(fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
            return(0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
        } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
        if (__PHYSFS_platformRead(fh, &entry->name, 8, 1) != 1)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
        {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
            __PHYSFS_platformClose(fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
            return(0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
        } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
        entry->name[8] = '\0'; /* name might not be null-terminated in file. */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
        entry->size = PHYSFS_swapULE32(entry->size);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
        entry->startPos = PHYSFS_swapULE32(entry->startPos);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
    } /* for */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    __PHYSFS_platformClose(fh);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
    __PHYSFS_sort(info->entries, info->entryCount,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
                  wad_entry_cmp, wad_entry_swap);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
    return(1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
} /* wad_load_entries */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   359
static void *WAD_openArchive(const char *name, int forWriting)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
    PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   362
    WADinfo *info = malloc(sizeof (WADinfo));
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   364
    BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
    memset(info, '\0', sizeof (WADinfo));
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
    info->filename = (char *) malloc(strlen(name) + 1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
    if (info->filename == NULL)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
        goto WAD_openArchive_failed;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
    if (!wad_load_entries(name, forWriting, info))
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
        goto WAD_openArchive_failed;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
    strcpy(info->filename, name);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    info->last_mod_time = modtime;
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   379
    return(info);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
WAD_openArchive_failed:
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   382
    if (info != NULL)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
    {
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   384
        if (info->filename != NULL)
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   385
            free(info->filename);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   386
        if (info->entries != NULL)
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   387
            free(info->entries);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   388
        free(info);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
    } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
    return(NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
} /* WAD_openArchive */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   395
static LinkedStringList *WAD_enumerateFiles(void *opaque,
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
                                            const char *dirname,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
                                            int omitSymLinks)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   399
    WADinfo *info = ((WADinfo *) opaque);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
    WADentry *entry = info->entries;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
    LinkedStringList *retval = NULL, *p = NULL;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
    PHYSFS_uint32 max = info->entryCount;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
    PHYSFS_uint32 i;
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   404
    char *sep;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   406
    if (dirname[0] == 0)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   407
    {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   408
        for (i = 0; i < max; i++, entry++)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   409
        {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   410
            if (strchr(entry->name, '/') == NULL)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   411
            {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   412
                retval = __PHYSFS_addToLinkedStringList(retval, &p,
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   413
                                                        entry->name, -1);
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   414
            } /* if */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   415
        } /* for */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   416
    } /* if */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   417
    else
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   418
    {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   419
        for (i = 0; i < max; i++, entry++)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   420
        {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   421
            sep = strchr(entry->name, '/');
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   422
            if (sep != NULL)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   423
            {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   424
                if (strncmp(dirname, entry->name, (sep-entry->name)) == 0)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   425
                {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   426
                    retval = __PHYSFS_addToLinkedStringList(retval, &p,
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   427
                                                            sep + 1, -1);
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   428
                } /* if */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   429
            } /* if */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   430
        } /* for */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   431
    } /* else */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   432
    
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
    return(retval);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
} /* WAD_enumerateFiles */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
static WADentry *wad_find_entry(WADinfo *info, const char *name)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
    WADentry *a = info->entries;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
    PHYSFS_sint32 lo = 0;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
    PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
    PHYSFS_sint32 middle;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
    int rc;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
    while (lo <= hi)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
    {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
        middle = lo + ((hi - lo) / 2);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
        rc = strcmp(name, a[middle].name);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
        if (rc == 0)  /* found it! */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
            return(&a[middle]);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
        else if (rc > 0)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
            lo = middle + 1;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
        else
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
            hi = middle - 1;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
    } /* while */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
    BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
} /* wad_find_entry */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   461
static int WAD_exists(void *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   463
    return(wad_find_entry(((WADinfo *) opaque), name) != NULL);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
} /* WAD_exists */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   467
static int WAD_isDirectory(void *opaque, const char *name, int *fileExists)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   469
    WADentry *entry = wad_find_entry(((WADinfo *) opaque), name);
610
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   470
    if (entry != NULL)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   471
    {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   472
        char *n;
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   473
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   474
        *fileExists = 1;
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   475
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   476
        /* Can't be a directory if it's a subdirectory. */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   477
        if (strchr(entry->name, '/') != NULL)
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   478
            return(0);
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   479
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   480
        /* Check if it matches "MAP??" or "E?M?" ... */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   481
        n = entry->name;
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   482
        if ((n[0] == 'E' && n[2] == 'M') ||
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   483
            (n[0] == 'M' && n[1] == 'A' && n[2] == 'P' && n[6] == 0))
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   484
        {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   485
            return(1);
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   486
        } /* if */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   487
        return(0);
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   488
    } /* if */
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   489
    else
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   490
    {
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   491
        *fileExists = 0;
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   492
        return(0);
1d8057a5d08e Says Travis Wells:
Ryan C. Gordon <icculus@icculus.org>
parents: 609
diff changeset
   493
    } /* else */
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
} /* WAD_isDirectory */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   497
static int WAD_isSymLink(void *opaque, const char *name, int *fileExists)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   499
    *fileExists = WAD_exists(opaque, name);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
    return(0);  /* never symlinks in a wad. */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
} /* WAD_isSymLink */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   504
static PHYSFS_sint64 WAD_getLastModTime(void *opaque,
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
                                        const char *name,
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
                                        int *fileExists)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   508
    WADinfo *info = ((WADinfo *) opaque);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
    PHYSFS_sint64 retval = -1;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
    *fileExists = (wad_find_entry(info, name) != NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
    if (*fileExists)  /* use time of WAD itself in the physical filesystem. */
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   513
        retval = info->last_mod_time;
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
    return(retval);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
} /* WAD_getLastModTime */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   519
static FileHandle *WAD_openRead(void *opaque, const char *fnm, int *fileExists)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   521
    WADinfo *info = ((WADinfo *) opaque);
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
    FileHandle *retval;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
    WADfileinfo *finfo;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
    WADentry *entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
    entry = wad_find_entry(info, fnm);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
    *fileExists = (entry != NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
    BAIL_IF_MACRO(entry == NULL, NULL, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
    retval = (FileHandle *) malloc(sizeof (FileHandle));
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
    finfo = (WADfileinfo *) malloc(sizeof (WADfileinfo));
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
    if (finfo == NULL)
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
    {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   535
        free(retval);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
    } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
    finfo->handle = __PHYSFS_platformOpenRead(info->filename);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
    if ( (finfo->handle == NULL) ||
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
         (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
    {
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
        free(finfo);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
        free(retval);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
        return(NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
    } /* if */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
    finfo->curPos = 0;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
    finfo->entry = entry;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
    retval->opaque = (void *) finfo;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
    retval->funcs = &__PHYSFS_FileFunctions_WAD;
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
    return(retval);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
} /* WAD_openRead */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   556
static FileHandle *WAD_openWrite(void *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
} /* WAD_openWrite */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   562
static FileHandle *WAD_openAppend(void *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
} /* WAD_openAppend */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   568
static int WAD_remove(void *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
} /* WAD_remove */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 622
diff changeset
   574
static int WAD_mkdir(void *opaque, const char *name)
609
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
{
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
} /* WAD_mkdir */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
#endif  /* defined PHYSFS_SUPPORTS_WAD */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
/* end of wad.c ... */
3a91332776e0 Added Doom WAD support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582