archivers/qpak.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 28 Aug 2002 23:27:10 +0000
changeset 481 c2c8d56ec65c
parent 467 99664d9842cb
child 494 364e564a26e2
permissions -rw-r--r--
Quick-rejection optimization in __PHYSFS_verifySecurity().
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * Quake PAK support routines for PhysicsFS.
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * This driver handles id Software Quake PAK files.
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 * Please see the file LICENSE in the source's root directory.
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 *  This file written by Ed Sinjiashvili.
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#if HAVE_CONFIG_H
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#  include <config.h>
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#endif
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#if (defined PHYSFS_SUPPORTS_QPAK)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#include <stdio.h>
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
#include <stdlib.h>
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
#include <string.h>
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
#include <errno.h>
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
#include <fcntl.h>
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
#include <assert.h>
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
#include "physfs.h"
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
#define __PHYSICSFS_INTERNAL__
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
#include "physfs_internal.h"
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
#define QPAK_MAXDIRLEN 60
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
typedef struct
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
    char          name[56];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
    PHYSFS_uint32 offset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
    PHYSFS_uint32 size;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
} QPAKentry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
typedef struct tagQPAKdirentry
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    char                   *name;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    QPAKentry              *entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    struct tagQPAKdirentry *next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
} QPAKdirentry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
typedef struct QPAKDirectory
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    char name[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
    struct QPAKDirectory *dirs, *next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    QPAKdirentry *files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
} QPAKdirectory;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
typedef struct
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    void               *handle;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    char               *filename;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    PHYSFS_uint32       dirOffset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    PHYSFS_uint32       totalEntries;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    QPAKentry          *entries;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    QPAKdirectory      *root;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
} QPAKinfo;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
typedef struct
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    void         *handle;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    QPAKentry    *entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    PHYSFS_sint64 curPos;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
} QPAKfileinfo;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
static int           QPAK_isArchive(const char *filename, int forWriting);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
static DirHandle    *QPAK_openArchive(const char *name, int forWriting);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
static void          QPAK_dirClose(DirHandle *h);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
static LinkedStringList *QPAK_enumerateFiles(DirHandle *h, const char *dirname,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
                                             int omitSymLinks);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
static int           QPAK_exists(DirHandle *h, const char *name);
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    76
static int           QPAK_isDirectory(DirHandle *h, const char *name, int *e);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    77
static int           QPAK_isSymLink(DirHandle *h, const char *name, int *e);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    78
static PHYSFS_sint64 QPAK_getLastModTime(DirHandle *h, const char *n, int *e);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    79
static FileHandle   *QPAK_openRead(DirHandle *h, const char *name, int *e);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    80
static FileHandle   *QPAK_openWrite(DirHandle *h, const char *name);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    81
static FileHandle   *QPAK_openAppend(DirHandle *h, const char *name);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
static PHYSFS_sint64 QPAK_read(FileHandle *handle, void *buffer,
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    85
                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    86
static PHYSFS_sint64 QPAK_write(FileHandle *handle, const void *buffer,
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    87
                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
static int           QPAK_eof(FileHandle *handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
static PHYSFS_sint64 QPAK_tell(FileHandle *handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
static int           QPAK_seek(FileHandle *handle, PHYSFS_uint64 offset);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
static PHYSFS_sint64 QPAK_fileLength(FileHandle *handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
static int           QPAK_fileClose(FileHandle *handle);
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    93
static int           QPAK_remove(DirHandle *h, const char *name);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    94
static int           QPAK_mkdir(DirHandle *h, const char *name);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    "PAK",
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
    "Quake PAK file format",
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    "Ed Sinjiashvili <slimb@swes.saren.ru>",
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    "http://icculus.org/physfs/",
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
};
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
static const FileFunctions __PHYSFS_FileFunctions_QPAK =
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    QPAK_read,               /* read() method       */
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   108
    QPAK_write,              /* write() method      */
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    QPAK_eof,                /* eof() method        */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    QPAK_tell,               /* tell() method       */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
    QPAK_seek,               /* seek() method       */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    QPAK_fileLength,         /* fileLength() method */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    QPAK_fileClose           /* fileClose() method  */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
};
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
const DirFunctions __PHYSFS_DirFunctions_QPAK =
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    &__PHYSFS_ArchiveInfo_QPAK,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    QPAK_isArchive,          /* isArchive() method      */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    QPAK_openArchive,        /* openArchive() method    */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    QPAK_enumerateFiles,     /* enumerateFiles() method */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    QPAK_exists,             /* exists() method         */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    QPAK_isDirectory,        /* isDirectory() method    */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
    QPAK_isSymLink,          /* isSymLink() method      */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    QPAK_getLastModTime,     /* getLastModTime() method */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
    QPAK_openRead,           /* openRead() method       */
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   127
    QPAK_openWrite,          /* openWrite() method      */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   128
    QPAK_openAppend,         /* openAppend() method     */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   129
    QPAK_remove,             /* remove() method         */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   130
    QPAK_mkdir,              /* mkdir() method          */
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    QPAK_dirClose            /* dirClose() method       */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
};
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
#define QPAK_MAGIC 0x4B434150  /* look like "PACK" in ascii. */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
/*
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
 * Read an unsigned 32-bit int and swap to native byte order.
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
 */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
static int readui32(void *in, PHYSFS_uint32 *val)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    PHYSFS_uint32 v;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    *val = PHYSFS_swapULE32(v);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
} /* readui32 */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
static int openQPak(const char *filename, int forWriting, void **fileHandle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    PHYSFS_uint32 sig;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    *fileHandle = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    *fileHandle = __PHYSFS_platformOpenRead(filename);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    BAIL_IF_MACRO(*fileHandle == NULL, NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    if (!readui32(*fileHandle, &sig))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
        goto openPak_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    
456
ecaab6f9e19f Fixed logic bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   163
    if (sig != QPAK_MAGIC)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
        __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
        goto openPak_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
openPak_failed:
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
    if (*fileHandle != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
        __PHYSFS_platformClose(*fileHandle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    *fileHandle = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
} /* openQPak */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
static int QPAK_isArchive(const char *filename, int forWriting)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
    void *fileHandle;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
    int retval = openQPak(filename, forWriting, &fileHandle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
    if (fileHandle != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
        __PHYSFS_platformClose(fileHandle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
} /* QPAK_isArchive */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
static int qpak_loadEntries(void *fh, int dirOffset, int numEntries,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
                            QPAKentry *entries)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
    PHYSFS_uint32 i;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
    BAIL_IF_MACRO(__PHYSFS_platformSeek(fh, dirOffset) == 0, NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
    for (i = 0; i < numEntries; i++, entries++)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
        PHYSFS_sint64 r = __PHYSFS_platformRead(fh, entries->name, 56, 1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
        BAIL_IF_MACRO(r == 0, NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
        BAIL_IF_MACRO(!readui32(fh, &entries->offset), NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
        BAIL_IF_MACRO(!readui32(fh, &entries->size), NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    } /* for */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
} /* qpak_loadEntries */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
static QPAKdirentry *qpak_newDirentry(char *name, QPAKentry *entry)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
    QPAKdirentry *retval = (QPAKdirentry *) malloc(sizeof (QPAKdirentry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
    retval->name  = name;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
    retval->entry = entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
    retval->next  = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
} /* qpak_newDirentry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
static void qpak_deleteDirentry(QPAKdirentry *e)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
    while (e != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
        QPAKdirentry *next = e->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
        free(e);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
        e = next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
} /* qpak_deleteDirentry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
static QPAKdirectory *qpak_newDirectory(char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    QPAKdirectory *dir = (QPAKdirectory *) malloc(sizeof (QPAKdirectory));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    BAIL_IF_MACRO(dir == NULL, ERR_OUT_OF_MEMORY, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    strcpy(dir->name, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
    dir->dirs = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
    dir->next = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
    dir->files = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
    return dir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
} /* qpak_newDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
static void qpak_deleteDirectory(QPAKdirectory *d)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    while (d != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
        QPAKdirectory *next = d->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
        qpak_deleteDirentry(d->files);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
        qpak_deleteDirectory(d->dirs);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
        free(d);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
        d = next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
    } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
} /* qpak_deleteDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
static int qpak_addFile(QPAKdirectory *dir, char *name, QPAKentry *entry)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
    QPAKdirentry *file = qpak_newDirentry(name, entry);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    if (file == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
        return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
    /* !!! FIXME: Traversing a linkedlist gets slower with each added file. */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    if (dir->files == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
        dir->files = file;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
        QPAKdirentry *tail = dir->files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
        while (tail->next != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
            tail = tail->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
        tail->next = file;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
} /* qpak_addFile */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
static QPAKdirectory *qpak_findDirectory(QPAKdirectory *root, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
    char *p = strchr(name, '/');
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
    if (p == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
        QPAKdirectory *thisDir = root->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
        while (thisDir != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
            if (strcmp(thisDir->name, name) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
                return(thisDir);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
            thisDir = thisDir->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
        char temp[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
        QPAKdirectory *thisDir = root->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
        strncpy (temp, name, p - name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
        temp[p - name] = '\0';
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
        while (thisDir != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
            if (strcmp(thisDir->name, temp) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
                return(qpak_findDirectory(thisDir, p + 1));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
            thisDir = thisDir->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   317
    BAIL_MACRO(ERR_NO_SUCH_PATH, 0);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
} /* qpak_findDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
static QPAKdirectory *qpak_addDir(QPAKdirectory *dir, char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
    QPAKdirectory *newDir = qpak_findDirectory(dir, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
    if (newDir != 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
        return(newDir);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
    newDir = qpak_newDirectory(name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
    if (newDir == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
        return 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
    if (dir->dirs == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
        dir->dirs = newDir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
        QPAKdirectory *tail = dir->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
        while (tail->next != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
            tail = tail->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
        tail->next = newDir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
    return(newDir);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
} /* qpak_addDir */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
static int qpak_addEntry(QPAKdirectory *dir, char *name, QPAKentry *entry)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    char tempName[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
    QPAKdirectory *child;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
    char *p = strchr(name, '/');
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    if (p == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
        return(qpak_addFile(dir, name, entry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
    strncpy(tempName, name, p - name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    tempName[p - name] = '\0';
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
    child = qpak_addDir(dir, tempName);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
    return(qpak_addEntry(child, p + 1, entry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
} /* qpak_addEntry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
static QPAKentry *qpak_findEntry(QPAKdirectory *root, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
    QPAKdirectory *dir = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
    QPAKdirentry *thisFile = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    const char *t = strrchr (name, '/');
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
    if (t == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
        dir = root;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
        t = name;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
        char temp[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
        strncpy(temp, name, t - name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
        temp[t - name] = '\0';
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
        dir = qpak_findDirectory(root, temp);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
        t++;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
    if (dir == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
        return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
    thisFile = dir->files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
    while (thisFile != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
        if (strcmp(thisFile->name, t) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
            return(thisFile->entry);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
        thisFile = thisFile->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
    } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   399
    BAIL_MACRO(ERR_NO_SUCH_FILE, 0);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
} /* qpak_findEntry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
static int qpak_populateDirectories(QPAKentry *entries, int numEntries,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
                                    QPAKdirectory *root)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
    PHYSFS_uint32 i;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
    QPAKentry *entry = entries;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
    for (i = 0; i < numEntries; i++, entry++)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
        if (qpak_addEntry(root, entry->name, entry) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
            return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
    } /* for */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   416
} /* qpak_populateDirectories */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
464
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   419
static void qpak_deletePakInfo(QPAKinfo *pakInfo)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
{
457
92e898299c7d Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 456
diff changeset
   421
    if (pakInfo->handle != NULL)
92e898299c7d Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 456
diff changeset
   422
        __PHYSFS_platformClose(pakInfo->handle);
92e898299c7d Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 456
diff changeset
   423
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
    if (pakInfo->filename != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
        free(pakInfo->filename);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
    if (pakInfo->entries != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
        free(pakInfo->entries);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
    qpak_deleteDirectory(pakInfo->root);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
    free(pakInfo);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
} /* qpak_deletePakInfo */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
464
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   436
static int qpak_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   437
{
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   438
    QPAKentry *a = (QPAKentry *) _a;
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   439
    return(strcmp(a[one].name, a[two].name));
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   440
} /* qpak_entry_cmp */
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   441
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   442
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   443
static void qpak_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   444
{
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   445
    QPAKentry tmp;
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   446
    QPAKentry *first = &(((QPAKentry *) _a)[one]);
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   447
    QPAKentry *second = &(((QPAKentry *) _a)[two]);
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   448
    memcpy(&tmp, first, sizeof (QPAKentry));
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   449
    memcpy(first, second, sizeof (QPAKentry));
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   450
    memcpy(second, &tmp, sizeof (QPAKentry));
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   451
} /* qpak_entry_swap */
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   452
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   453
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
static DirHandle *QPAK_openArchive(const char *name, int forWriting)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
    void *fh = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
    PHYSFS_uint32 dirOffset, dirLength;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
    QPAKinfo *pi;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
    DirHandle *retval;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
    retval = (DirHandle *) malloc(sizeof (DirHandle));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
    pi = (QPAKinfo *) malloc(sizeof (QPAKinfo));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
    if (pi == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
        free(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   470
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
    retval->opaque = pi;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
    pi->filename = (char *) malloc(strlen(name) + 1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
    if (pi->filename == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
    if (!openQPak(name, forWriting, &fh))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
    if (!readui32(fh, &dirOffset))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
    if (!readui32(fh, &dirLength))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
    if (__PHYSFS_platformFileLength(fh) < dirOffset + dirLength)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
    strcpy(pi->filename, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
    pi->handle = fh;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
    pi->dirOffset = dirOffset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
    pi->totalEntries = dirLength / 64;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
    pi->entries = (QPAKentry *) malloc(pi->totalEntries * sizeof (QPAKentry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
    if (pi->entries == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
    if (qpak_loadEntries(fh, dirOffset, pi->totalEntries, pi->entries) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
464
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   507
    __PHYSFS_sort(pi->entries, pi->totalEntries,
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   508
                  qpak_entry_cmp, qpak_entry_swap);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
    pi->root = qpak_newDirectory("");
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
    if (pi->root == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
    if (qpak_populateDirectories(pi->entries, pi->totalEntries, pi->root) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
    retval->funcs = &__PHYSFS_DirFunctions_QPAK;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
QPAK_openArchive_failed:
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
    if (retval != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
        if (retval->opaque != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
            qpak_deletePakInfo((QPAKinfo *) retval->opaque);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
        free(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
    if (fh != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
        __PHYSFS_platformClose(fh);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
} /* QPAK_openArchive */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   535
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
static void QPAK_dirClose(DirHandle *dirHandle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
{
457
92e898299c7d Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 456
diff changeset
   538
    qpak_deletePakInfo((QPAKinfo *) dirHandle->opaque);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
    free(dirHandle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
} /* QPAK_dirClose */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
static LinkedStringList *QPAK_enumerateFiles(DirHandle *h, const char *dirname,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
                                             int omitSymLinks)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
    LinkedStringList *retval = NULL, *p = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
    QPAKdirectory *dir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
    QPAKinfo *info = (QPAKinfo *) h->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
    if ((dirname == NULL) || (*dirname == '\0'))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
        dir = info->root;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
        dir = qpak_findDirectory(info->root, dirname);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
    if (dir != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   556
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
        QPAKdirectory *child = dir->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
        QPAKdirentry *file = dir->files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
        while (child != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
            retval = __PHYSFS_addToLinkedStringList(retval, &p, child->name, -1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
            child = child->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
        while (file != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
            retval = __PHYSFS_addToLinkedStringList(retval, &p, file->name, -1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
            file = file->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
} /* QPAK_enumerateFiles */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
static int QPAK_exists(DirHandle *h, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
    QPAKinfo *driver = (QPAKinfo *) h->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
    if ((name == NULL) || (*name == '\0'))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
        return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
    if (qpak_findDirectory(driver->root, name) != 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
        return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
    if (qpak_findEntry(driver->root, name) != 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
        return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
} /* QPAK_exists */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   594
static int QPAK_isDirectory(DirHandle *h, const char *name, int *fileExists)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   595
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
    QPAKinfo *info = (QPAKinfo *) h->opaque;
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   597
    *fileExists = (qpak_findDirectory(info->root, name) != 0);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   598
    return(*fileExists);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
} /* QPAK_isDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   602
static int QPAK_isSymLink(DirHandle *h, const char *name, int *fileExists)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
{
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   604
    *fileExists = QPAK_exists(h, name);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
    return(0); /* we don't support symlinks for now */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
} /* QPAK_isSymlink */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   607
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   609
static int QPAK_remove(DirHandle *h, const char *name)
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   610
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   611
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   612
} /* QPAK_remove */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   613
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   614
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   615
static int QPAK_mkdir(DirHandle *h, const char *name)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
{
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   617
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   618
} /* QPAK_mkdir */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   619
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   620
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   621
static PHYSFS_sint64 QPAK_getLastModTime(DirHandle *h,
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   622
                                         const char *name,
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   623
                                         int *fileExists)
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   624
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   625
    QPAKinfo *info = (QPAKinfo *) h->opaque;
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   626
    PHYSFS_sint64 retval = -1;
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   627
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   628
    *fileExists = QPAK_exists(h, name);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   629
    if (*fileExists)
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   630
        retval = __PHYSFS_platformGetLastModTime(info->filename);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   631
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   632
    return(retval);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
} /* QPAK_getLastModTime */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
static void *qpak_getFileHandle(const char *name, QPAKentry *entry)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
    void *retval = __PHYSFS_platformOpenRead(name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
    if (retval == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
    if (!__PHYSFS_platformSeek(retval, entry->offset))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   643
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
        __PHYSFS_platformClose(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   645
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   646
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   648
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
} /* qpak_getFileHandle */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   652
static FileHandle *QPAK_openRead(DirHandle *h, const char *fnm, int *fileExists)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   653
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   654
    QPAKinfo *driver = (QPAKinfo *) h->opaque;
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   655
    QPAKentry *entry = qpak_findEntry(driver->root, fnm);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
    QPAKfileinfo *fileDriver = 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
    FileHandle *result = 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   658
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   659
    *fileExists = (entry != NULL);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   660
    if (entry == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   661
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
 
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   663
    fileDriver = (QPAKfileinfo *) malloc(sizeof (QPAKfileinfo));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   664
    BAIL_IF_MACRO(fileDriver == NULL, ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   666
    fileDriver->handle = qpak_getFileHandle(driver->filename, entry);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
    if (fileDriver->handle == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
        free(fileDriver);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   672
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   673
    fileDriver->entry = entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
    fileDriver->curPos = 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   675
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   676
    result = (FileHandle *)malloc(sizeof (FileHandle));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   677
    if (result == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   678
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   679
        __PHYSFS_platformClose(fileDriver->handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   680
        free(fileDriver);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   681
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   682
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   683
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   684
    result->opaque = fileDriver;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   685
    result->dirHandle = h;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
    result->funcs = &__PHYSFS_FileFunctions_QPAK;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   687
    return(result);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   688
} /* QPAK_openRead */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   689
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   690
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   691
static FileHandle *QPAK_openWrite(DirHandle *h, const char *name)
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   692
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   693
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   694
} /* QPAK_openWrite */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   695
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   696
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   697
static FileHandle *QPAK_openAppend(DirHandle *h, const char *name)
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   698
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   699
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   700
} /* QPAK_openAppend */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   701
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   702
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   703
static PHYSFS_sint64 QPAK_read(FileHandle *handle, void *buffer,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   704
                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   705
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   706
    QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
    QPAKentry *entry = finfo->entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   708
    PHYSFS_uint64 bytesLeft = entry->size - finfo->curPos;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
    PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   710
    PHYSFS_sint64 rc;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   711
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   712
    if (objsLeft < objCount)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   713
        objCount = (PHYSFS_uint32) objsLeft;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   714
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   715
    rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   716
    if (rc > 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   717
        finfo->curPos += (rc * objSize);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   718
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   719
    return(rc);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   720
} /* QPAK_read */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   721
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   722
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   723
static PHYSFS_sint64 QPAK_write(FileHandle *handle, const void *buffer,
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   724
                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   725
{
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   726
    BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   727
} /* QPAK_write */
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   728
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
   729
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   730
static int QPAK_eof(FileHandle *handle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   731
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   732
    QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   733
    QPAKentry *entry = finfo->entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   734
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   735
    return(finfo->curPos >= (PHYSFS_sint64) entry->size);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   736
} /* QPAK_eof */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   737
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   738
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   739
static PHYSFS_sint64 QPAK_tell(FileHandle *handle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   740
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   741
    return(((QPAKfileinfo *) handle->opaque)->curPos);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   742
} /* QPAK_tell */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   743
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   744
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   745
static int QPAK_seek(FileHandle *handle, PHYSFS_uint64 offset)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   746
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   747
    QPAKfileinfo *finfo = (QPAKfileinfo *) handle->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   748
    QPAKentry *entry = finfo->entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   749
    PHYSFS_uint64 newPos = entry->offset + offset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   750
    int rc;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   751
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   752
    BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   753
    BAIL_IF_MACRO(newPos > entry->offset + entry->size, ERR_PAST_EOF, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   754
    rc = __PHYSFS_platformSeek(finfo->handle, newPos);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   755
    if (rc)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   756
        finfo->curPos = offset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   757
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   758
    return(rc);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   759
}  /* QPAK_seek */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   760
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   761
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   762
static PHYSFS_sint64 QPAK_fileLength(FileHandle *handle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   763
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   764
    return ((QPAKfileinfo *) handle->opaque)->entry->size;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   765
} /* QPAK_fileLength */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   766
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   767
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   768
static int QPAK_fileClose(FileHandle *handle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   769
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   770
    QPAKfileinfo *finfo = (QPAKfileinfo *) handle->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   771
    BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   772
    free(finfo);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   773
    free(handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   774
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   775
} /* QPAK_fileClose */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   776
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   777
#endif  /* defined PHYSFS_SUPPORTS_QPAK */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   778
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   779
/* end of qpak.c ... */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   780
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   781