archivers/qpak.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 20 Aug 2002 01:34:27 +0000
changeset 464 21c8e0d1578c
parent 457 92e898299c7d
child 467 99664d9842cb
permissions -rw-r--r--
Generalized sorting routines, and removed individual implementations.
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);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
static int           QPAK_isDirectory(DirHandle *h, const char *name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
static int           QPAK_isSymLink(DirHandle *h, const char *name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
static PHYSFS_sint64 QPAK_getLastModTime(DirHandle *h, const char *name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
static FileHandle   *QPAK_openRead(DirHandle *h, const char *name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
static PHYSFS_sint64 QPAK_read(FileHandle *handle, void *buffer,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
                              PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
static int           QPAK_eof(FileHandle *handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
static PHYSFS_sint64 QPAK_tell(FileHandle *handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
static int           QPAK_seek(FileHandle *handle, PHYSFS_uint64 offset);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
static PHYSFS_sint64 QPAK_fileLength(FileHandle *handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
static int           QPAK_fileClose(FileHandle *handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
    "PAK",
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
    "Quake PAK file format",
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
    "Ed Sinjiashvili <slimb@swes.saren.ru>",
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    "http://icculus.org/physfs/",
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
};
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
static const FileFunctions __PHYSFS_FileFunctions_QPAK =
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    QPAK_read,               /* read() method       */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    NULL,                    /* write() method      */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
    QPAK_eof,                /* eof() method        */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
    QPAK_tell,               /* tell() method       */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    QPAK_seek,               /* seek() method       */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
    QPAK_fileLength,         /* fileLength() method */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    QPAK_fileClose           /* fileClose() method  */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
};
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
const DirFunctions __PHYSFS_DirFunctions_QPAK =
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    &__PHYSFS_ArchiveInfo_QPAK,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    QPAK_isArchive,          /* isArchive() method      */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
    QPAK_openArchive,        /* openArchive() method    */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
    QPAK_enumerateFiles,     /* enumerateFiles() method */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    QPAK_exists,             /* exists() method         */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    QPAK_isDirectory,        /* isDirectory() method    */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    QPAK_isSymLink,          /* isSymLink() method      */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    QPAK_getLastModTime,     /* getLastModTime() method */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    QPAK_openRead,           /* openRead() method       */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    NULL,                    /* openWrite() method      */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    NULL,                    /* openAppend() method     */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    NULL,                    /* remove() method         */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
    NULL,                    /* mkdir() method          */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    QPAK_dirClose            /* dirClose() method       */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
};
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
#define QPAK_MAGIC 0x4B434150  /* look like "PACK" in ascii. */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
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
 * Read an unsigned 32-bit int and swap to native byte order.
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
static int readui32(void *in, PHYSFS_uint32 *val)
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
    PHYSFS_uint32 v;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    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
   139
    *val = PHYSFS_swapULE32(v);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
} /* readui32 */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
static int openQPak(const char *filename, int forWriting, void **fileHandle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    PHYSFS_uint32 sig;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
    *fileHandle = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    *fileHandle = __PHYSFS_platformOpenRead(filename);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    BAIL_IF_MACRO(*fileHandle == NULL, NULL, 0);
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
    if (!readui32(*fileHandle, &sig))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
        goto openPak_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
    
456
ecaab6f9e19f Fixed logic bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   157
    if (sig != QPAK_MAGIC)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
        __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
        goto openPak_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
    return(1);
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
openPak_failed:
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
    if (*fileHandle != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
        __PHYSFS_platformClose(*fileHandle);
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
    *fileHandle = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
} /* openQPak */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
static int QPAK_isArchive(const char *filename, int forWriting)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
    void *fileHandle;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
    int retval = openQPak(filename, forWriting, &fileHandle);
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
    if (fileHandle != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
        __PHYSFS_platformClose(fileHandle);
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
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
} /* QPAK_isArchive */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
static int qpak_loadEntries(void *fh, int dirOffset, int numEntries,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
                            QPAKentry *entries)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
    PHYSFS_uint32 i;
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
    BAIL_IF_MACRO(__PHYSFS_platformSeek(fh, dirOffset) == 0, NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    for (i = 0; i < numEntries; i++, 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_sint64 r = __PHYSFS_platformRead(fh, entries->name, 56, 1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
        BAIL_IF_MACRO(r == 0, NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
        BAIL_IF_MACRO(!readui32(fh, &entries->offset), NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
        BAIL_IF_MACRO(!readui32(fh, &entries->size), NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
    } /* for */
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
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
} /* qpak_loadEntries */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
static QPAKdirentry *qpak_newDirentry(char *name, QPAKentry *entry)
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
    QPAKdirentry *retval = (QPAKdirentry *) malloc(sizeof (QPAKdirentry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, 0);
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
    retval->name  = name;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
    retval->entry = entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
    retval->next  = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
} /* qpak_newDirentry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
static void qpak_deleteDirentry(QPAKdirentry *e)
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
    while (e != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
        QPAKdirentry *next = e->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
        free(e);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
        e = next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
} /* qpak_deleteDirentry */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
static QPAKdirectory *qpak_newDirectory(char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    QPAKdirectory *dir = (QPAKdirectory *) malloc(sizeof (QPAKdirectory));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
    BAIL_IF_MACRO(dir == NULL, ERR_OUT_OF_MEMORY, 0);
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
    strcpy(dir->name, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    dir->dirs = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    dir->next = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    dir->files = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
    return dir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
} /* qpak_newDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
static void qpak_deleteDirectory(QPAKdirectory *d)
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
    while (d != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
        QPAKdirectory *next = d->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
        qpak_deleteDirentry(d->files);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
        qpak_deleteDirectory(d->dirs);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
        free(d);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
        d = next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
    } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
} /* qpak_deleteDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
static int qpak_addFile(QPAKdirectory *dir, char *name, QPAKentry *entry)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
    QPAKdirentry *file = qpak_newDirentry(name, entry);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
    if (file == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
        return(0);
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
    /* !!! FIXME: Traversing a linkedlist gets slower with each added file. */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
    if (dir->files == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
        dir->files = file;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
        QPAKdirentry *tail = dir->files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
        while (tail->next != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
            tail = tail->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
        tail->next = file;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
} /* qpak_addFile */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
static QPAKdirectory *qpak_findDirectory(QPAKdirectory *root, const char *name)
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
    char *p = strchr(name, '/');
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
    if (p == NULL)
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
        QPAKdirectory *thisDir = root->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
        while (thisDir != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
            if (strcmp(thisDir->name, name) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
                return(thisDir);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
            thisDir = thisDir->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
    } /* if */
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
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
        char temp[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
        QPAKdirectory *thisDir = root->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
        strncpy (temp, name, p - name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
        temp[p - name] = '\0';
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
        while (thisDir != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
            if (strcmp(thisDir->name, temp) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
                return(qpak_findDirectory(thisDir, p + 1));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
            thisDir = thisDir->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
    } /* else */
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
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
} /* qpak_findDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
static QPAKdirectory *qpak_addDir(QPAKdirectory *dir, char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
    QPAKdirectory *newDir = qpak_findDirectory(dir, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
    if (newDir != 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
        return(newDir);
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
    newDir = qpak_newDirectory(name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
    if (newDir == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
        return 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
    if (dir->dirs == NULL)
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
        dir->dirs = newDir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
        QPAKdirectory *tail = dir->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
        while (tail->next != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
            tail = tail->next;
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
        tail->next = newDir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
    return(newDir);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
} /* qpak_addDir */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
static int qpak_addEntry(QPAKdirectory *dir, char *name, QPAKentry *entry)
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
    char tempName[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
    QPAKdirectory *child;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
    char *p = strchr(name, '/');
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
    if (p == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
        return(qpak_addFile(dir, name, 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
    strncpy(tempName, name, p - name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
    tempName[p - name] = '\0';
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    child = qpak_addDir(dir, tempName);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
    return(qpak_addEntry(child, p + 1, entry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
} /* qpak_addEntry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
static QPAKentry *qpak_findEntry(QPAKdirectory *root, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
    QPAKdirectory *dir = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
    QPAKdirentry *thisFile = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
    const char *t = strrchr (name, '/');
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
    if (t == NULL)
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
        dir = root;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
        t = name;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    } /* if */
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
    else
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
        char temp[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
        strncpy(temp, name, t - name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
        temp[t - name] = '\0';
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
        dir = qpak_findDirectory(root, temp);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
        t++;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
    if (dir == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
        return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
    thisFile = dir->files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
    while (thisFile != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
        if (strcmp(thisFile->name, t) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
            return(thisFile->entry);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
        thisFile = thisFile->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
    } /* while */
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
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
} /* qpak_findEntry */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
static int qpak_populateDirectories(QPAKentry *entries, int numEntries,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
                                    QPAKdirectory *root)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
    PHYSFS_uint32 i;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
    QPAKentry *entry = entries;
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
    for (i = 0; i < numEntries; i++, entry++)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
        if (qpak_addEntry(root, entry->name, entry) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
            return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
    } /* for */
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
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
} /* qpak_populateDirectories */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
464
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   413
static void qpak_deletePakInfo(QPAKinfo *pakInfo)
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
{
457
92e898299c7d Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 456
diff changeset
   415
    if (pakInfo->handle != NULL)
92e898299c7d Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 456
diff changeset
   416
        __PHYSFS_platformClose(pakInfo->handle);
92e898299c7d Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 456
diff changeset
   417
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
    if (pakInfo->filename != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
        free(pakInfo->filename);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
    if (pakInfo->entries != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
        free(pakInfo->entries);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
    qpak_deleteDirectory(pakInfo->root);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
    free(pakInfo);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
} /* qpak_deletePakInfo */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
464
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   430
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
   431
{
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   432
    QPAKentry *a = (QPAKentry *) _a;
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   433
    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
   434
} /* qpak_entry_cmp */
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   435
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   436
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   437
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
   438
{
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   439
    QPAKentry tmp;
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   440
    QPAKentry *first = &(((QPAKentry *) _a)[one]);
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   441
    QPAKentry *second = &(((QPAKentry *) _a)[two]);
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   442
    memcpy(&tmp, first, sizeof (QPAKentry));
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   443
    memcpy(first, second, sizeof (QPAKentry));
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   444
    memcpy(second, &tmp, sizeof (QPAKentry));
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   445
} /* qpak_entry_swap */
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   446
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   447
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
static DirHandle *QPAK_openArchive(const char *name, int forWriting)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
    void *fh = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
    PHYSFS_uint32 dirOffset, dirLength;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
    QPAKinfo *pi;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
    DirHandle *retval;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
    retval = (DirHandle *) malloc(sizeof (DirHandle));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
    pi = (QPAKinfo *) malloc(sizeof (QPAKinfo));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
    if (pi == NULL)
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
        free(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
    retval->opaque = pi;
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
    pi->filename = (char *) malloc(strlen(name) + 1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
    if (pi->filename == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   470
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
    if (!openQPak(name, forWriting, &fh))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
    if (!readui32(fh, &dirOffset))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
        goto QPAK_openArchive_failed;
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 (!readui32(fh, &dirLength))
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 (__PHYSFS_platformFileLength(fh) < dirOffset + dirLength)
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
    strcpy(pi->filename, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
    pi->handle = fh;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
    pi->dirOffset = dirOffset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
    pi->totalEntries = dirLength / 64;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
    pi->entries = (QPAKentry *) malloc(pi->totalEntries * sizeof (QPAKentry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
    if (pi->entries == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
    if (qpak_loadEntries(fh, dirOffset, pi->totalEntries, pi->entries) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
464
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   501
    __PHYSFS_sort(pi->entries, pi->totalEntries,
21c8e0d1578c Generalized sorting routines, and removed individual implementations.
Ryan C. Gordon <icculus@icculus.org>
parents: 457
diff changeset
   502
                  qpak_entry_cmp, qpak_entry_swap);
450
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
    pi->root = qpak_newDirectory("");
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
    if (pi->root == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
    if (qpak_populateDirectories(pi->entries, pi->totalEntries, pi->root) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
    retval->funcs = &__PHYSFS_DirFunctions_QPAK;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
    return(retval);
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
QPAK_openArchive_failed:
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
    if (retval != NULL)
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
        if (retval->opaque != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
            qpak_deletePakInfo((QPAKinfo *) retval->opaque);
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
        free(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
    } /* if */
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 (fh != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
        __PHYSFS_platformClose(fh);
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
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
} /* QPAK_openArchive */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
static void QPAK_dirClose(DirHandle *dirHandle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
{
457
92e898299c7d Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 456
diff changeset
   532
    qpak_deletePakInfo((QPAKinfo *) dirHandle->opaque);
450
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
    free(dirHandle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
} /* QPAK_dirClose */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
static LinkedStringList *QPAK_enumerateFiles(DirHandle *h, const char *dirname,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
                                             int omitSymLinks)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
    LinkedStringList *retval = NULL, *p = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
    QPAKdirectory *dir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
    QPAKinfo *info = (QPAKinfo *) h->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
    if ((dirname == NULL) || (*dirname == '\0'))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
        dir = info->root;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
        dir = qpak_findDirectory(info->root, dirname);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
    if (dir != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
        QPAKdirectory *child = dir->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
        QPAKdirentry *file = dir->files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
        while (child != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   556
            retval = __PHYSFS_addToLinkedStringList(retval, &p, child->name, -1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
            child = child->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
        } /* while */
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 (file != 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, file->name, -1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
            file = file->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
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
} /* QPAK_enumerateFiles */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
static int QPAK_exists(DirHandle *h, const char *name)
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
    QPAKinfo *driver = (QPAKinfo *) h->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
    if ((name == NULL) || (*name == '\0'))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
        return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
    if (qpak_findDirectory(driver->root, name) != 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
        return(1);
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 (qpak_findEntry(driver->root, name) != 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
        return(1);
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
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
} /* QPAK_exists */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
static int QPAK_isDirectory(DirHandle *h, const char *name)
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
    QPAKinfo *info = (QPAKinfo *) h->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
    return(qpak_findDirectory(info->root, name) != 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
} /* QPAK_isDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   594
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   595
static int QPAK_isSymLink(DirHandle *h, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
    return(0); /* we don't support symlinks for now */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
} /* QPAK_isSymlink */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
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
static PHYSFS_sint64 QPAK_getLastModTime(DirHandle *h, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
    return(__PHYSFS_platformGetLastModTime(((QPAKinfo *) h->opaque)->filename));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   604
} /* QPAK_getLastModTime */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   607
static void *qpak_getFileHandle(const char *name, QPAKentry *entry)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   609
    void *retval = __PHYSFS_platformOpenRead(name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   610
    if (retval == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   611
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   612
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
    if (!__PHYSFS_platformSeek(retval, entry->offset))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   614
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
        __PHYSFS_platformClose(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   618
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   619
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
} /* qpak_getFileHandle */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   621
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   622
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   623
static FileHandle *QPAK_openRead(DirHandle *h, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   624
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   625
    QPAKinfo *driver = (QPAKinfo *) h->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   626
    QPAKentry *entry = qpak_findEntry(driver->root, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
    QPAKfileinfo *fileDriver = 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   628
    FileHandle *result = 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   629
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   630
    if (entry == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   632
 
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
    fileDriver = (QPAKfileinfo *) malloc(sizeof (QPAKfileinfo));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
    BAIL_IF_MACRO(fileDriver == NULL, ERR_OUT_OF_MEMORY, NULL);
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
    fileDriver->handle = qpak_getFileHandle(driver->filename, entry);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
    if (fileDriver->handle == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
        free(fileDriver);
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
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   643
    fileDriver->entry = entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
    fileDriver->curPos = 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   645
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   646
    result = (FileHandle *)malloc(sizeof (FileHandle));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
    if (result == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   648
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
        __PHYSFS_platformClose(fileDriver->handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
        free(fileDriver);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
    } /* if */
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
    result->opaque = fileDriver;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
    result->dirHandle = h;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
    result->funcs = &__PHYSFS_FileFunctions_QPAK;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
    return(result);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   658
} /* QPAK_openRead */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   659
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   660
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   661
static PHYSFS_sint64 QPAK_read(FileHandle *handle, void *buffer,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   663
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   664
    QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
    QPAKentry *entry = finfo->entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   666
    PHYSFS_uint64 bytesLeft = entry->size - finfo->curPos;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
    PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
    PHYSFS_sint64 rc;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
    if (objsLeft < objCount)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
        objCount = (PHYSFS_uint32) objsLeft;
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
    rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
    if (rc > 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   675
        finfo->curPos += (rc * objSize);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   676
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   677
    return(rc);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   678
} /* QPAK_read */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   679
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   680
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   681
static int QPAK_eof(FileHandle *handle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   682
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   683
    QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   684
    QPAKentry *entry = finfo->entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   685
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
    return(finfo->curPos >= (PHYSFS_sint64) entry->size);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   687
} /* QPAK_eof */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   688
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
static PHYSFS_sint64 QPAK_tell(FileHandle *handle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   691
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   692
    return(((QPAKfileinfo *) handle->opaque)->curPos);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   693
} /* QPAK_tell */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   694
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   695
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   696
static int QPAK_seek(FileHandle *handle, PHYSFS_uint64 offset)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   697
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   698
    QPAKfileinfo *finfo = (QPAKfileinfo *) handle->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   699
    QPAKentry *entry = finfo->entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   700
    PHYSFS_uint64 newPos = entry->offset + offset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   701
    int rc;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   702
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   703
    BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   704
    BAIL_IF_MACRO(newPos > entry->offset + entry->size, ERR_PAST_EOF, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   705
    rc = __PHYSFS_platformSeek(finfo->handle, newPos);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   706
    if (rc)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
        finfo->curPos = offset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   708
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
    return(rc);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   710
}  /* QPAK_seek */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   713
static PHYSFS_sint64 QPAK_fileLength(FileHandle *handle)
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
    return ((QPAKfileinfo *) handle->opaque)->entry->size;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   716
} /* QPAK_fileLength */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   717
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
static int QPAK_fileClose(FileHandle *handle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   720
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   721
    QPAKfileinfo *finfo = (QPAKfileinfo *) handle->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   722
    BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   723
    free(finfo);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   724
    free(handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   725
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   726
} /* QPAK_fileClose */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   727
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   728
#endif  /* defined PHYSFS_SUPPORTS_QPAK */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   729
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   730
/* end of qpak.c ... */
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