archivers/qpak.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 09 Aug 2002 19:45:54 +0000
changeset 450 b60e00958aad
child 456 ecaab6f9e19f
permissions -rw-r--r--
Initial add.
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
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    if (sig == QPAK_MAGIC)
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 void qpak_insertion_sort(QPAKentry *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi)
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
    PHYSFS_uint32 i;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
    PHYSFS_uint32 j;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    QPAKentry tmp;
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
    for (i = lo + 1; i <= hi; i++)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
        memcpy(&tmp, &a[i], sizeof (QPAKentry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
        j = i;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
        while ((j > lo) && (strcmp(a[j - 1].name, tmp.name) > 0))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
            memcpy(&a[j], &a[j - 1], sizeof (QPAKentry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
            j--;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
        memcpy(&a[j], &tmp, sizeof (QPAKentry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
    } /* for */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
} /* qpak_insertion_sort */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
static void qpak_sort_entries(QPAKentry *entries, PHYSFS_uint32 max)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
    qpak_insertion_sort(entries, 0, max - 1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
} /* qpak_sort_entries */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
static int qpak_loadEntries(void *fh, int dirOffset, int numEntries,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
                            QPAKentry *entries)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
    PHYSFS_uint32 i;
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
    BAIL_IF_MACRO(__PHYSFS_platformSeek(fh, dirOffset) == 0, NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
    for (i = 0; i < numEntries; i++, entries++)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
        PHYSFS_sint64 r = __PHYSFS_platformRead(fh, entries->name, 56, 1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
        BAIL_IF_MACRO(r == 0, NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
        BAIL_IF_MACRO(!readui32(fh, &entries->offset), NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
        BAIL_IF_MACRO(!readui32(fh, &entries->size), NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    } /* for */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
} /* qpak_loadEntries */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
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
static QPAKdirentry *qpak_newDirentry(char *name, QPAKentry *entry)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
    QPAKdirentry *retval = (QPAKdirentry *) malloc(sizeof (QPAKdirentry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    retval->name  = name;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    retval->entry = entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    retval->next  = NULL;
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
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
} /* qpak_newDirentry */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
static void qpak_deleteDirentry(QPAKdirentry *e)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
    while (e != NULL)
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
        QPAKdirentry *next = e->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
        free(e);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
        e = next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
} /* qpak_deleteDirentry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
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
static QPAKdirectory *qpak_newDirectory(char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
    QPAKdirectory *dir = (QPAKdirectory *) malloc(sizeof (QPAKdirectory));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
    BAIL_IF_MACRO(dir == NULL, ERR_OUT_OF_MEMORY, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
    strcpy(dir->name, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
    dir->dirs = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
    dir->next = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
    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
    return dir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
} /* qpak_newDirectory */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
static void qpak_deleteDirectory(QPAKdirectory *d)
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
    while (d != NULL)
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
        QPAKdirectory *next = d->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
        qpak_deleteDirentry(d->files);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
        qpak_deleteDirectory(d->dirs);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
        free(d);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
        d = next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
    } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
} /* qpak_deleteDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
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
static int qpak_addFile(QPAKdirectory *dir, char *name, QPAKentry *entry)
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
    QPAKdirentry *file = qpak_newDirentry(name, entry);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
    if (file == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
        return(0);
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
    /* !!! FIXME: Traversing a linkedlist gets slower with each added file. */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    if (dir->files == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
        dir->files = file;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
    else
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
        QPAKdirentry *tail = dir->files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
        while (tail->next != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
            tail = tail->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
        tail->next = file;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
} /* qpak_addFile */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
static QPAKdirectory *qpak_findDirectory(QPAKdirectory *root, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
    char *p = strchr(name, '/');
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
    if (p == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
        QPAKdirectory *thisDir = root->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
        while (thisDir != NULL)
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
            if (strcmp(thisDir->name, name) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
                return(thisDir);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
            thisDir = thisDir->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
    } /* if */
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
    else
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
        char temp[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
        QPAKdirectory *thisDir = root->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
        strncpy (temp, name, p - name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
        temp[p - name] = '\0';
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
        while (thisDir != NULL)
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 (strcmp(thisDir->name, temp) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
                return(qpak_findDirectory(thisDir, p + 1));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
            thisDir = thisDir->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
    } /* else */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
} /* qpak_findDirectory */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
static QPAKdirectory *qpak_addDir(QPAKdirectory *dir, char *name)
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
    QPAKdirectory *newDir = qpak_findDirectory(dir, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
    if (newDir != 0)
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
    newDir = qpak_newDirectory(name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
    if (newDir == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
        return 0;
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
    if (dir->dirs == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
        dir->dirs = newDir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
    else
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
        QPAKdirectory *tail = dir->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
        while (tail->next != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
            tail = tail->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
        tail->next = newDir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
    } /* else */
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
    return(newDir);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
} /* qpak_addDir */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
static int qpak_addEntry(QPAKdirectory *dir, char *name, QPAKentry *entry)
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
    char tempName[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    QPAKdirectory *child;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
    char *p = strchr(name, '/');
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
    if (p == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
        return(qpak_addFile(dir, name, entry));
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
    strncpy(tempName, name, p - name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    tempName[p - name] = '\0';
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
    child = qpak_addDir(dir, tempName);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
    return(qpak_addEntry(child, p + 1, entry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
} /* qpak_addEntry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
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
static QPAKentry *qpak_findEntry(QPAKdirectory *root, const char *name)
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
    QPAKdirectory *dir = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
    QPAKdirentry *thisFile = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
    const char *t = strrchr (name, '/');
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
    if (t == 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
        dir = root;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
        t = name;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
    } /* if */
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
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
        char temp[QPAK_MAXDIRLEN];
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
        strncpy(temp, name, t - name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
        temp[t - name] = '\0';
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
        dir = qpak_findDirectory(root, temp);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
        t++;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
    } /* else */
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
    if (dir == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
        return(0);
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
    thisFile = dir->files;
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
    while (thisFile != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
        if (strcmp(thisFile->name, t) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
            return(thisFile->entry);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   416
        thisFile = thisFile->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
    } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
} /* qpak_findEntry */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
static int qpak_populateDirectories(QPAKentry *entries, int numEntries,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
                                    QPAKdirectory *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
    PHYSFS_uint32 i;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
    QPAKentry *entry = entries;
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
    for (i = 0; i < numEntries; i++, entry++)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
        if (qpak_addEntry(root, entry->name, entry) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
            return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
    } /* for */
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
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
} /* qpak_populateDirectories */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
static void qpak_deletePakInfo (QPAKinfo *pakInfo)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
    if (pakInfo->filename != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
        free(pakInfo->filename);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
    if (pakInfo->entries != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
        free(pakInfo->entries);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
    qpak_deleteDirectory(pakInfo->root);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
    free(pakInfo);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
} /* qpak_deletePakInfo */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
static DirHandle *QPAK_openArchive(const char *name, int forWriting)
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
    void *fh = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
    PHYSFS_uint32 dirOffset, dirLength;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
    QPAKinfo *pi;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
    DirHandle *retval;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
    retval = (DirHandle *) malloc(sizeof (DirHandle));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
    pi = (QPAKinfo *) malloc(sizeof (QPAKinfo));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
    if (pi == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
        free(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
    } /* if */
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
    retval->opaque = pi;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
    pi->filename = (char *) malloc(strlen(name) + 1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
    if (pi->filename == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
    if (!openQPak(name, forWriting, &fh))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
    if (!readui32(fh, &dirOffset))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
    if (!readui32(fh, &dirLength))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
    if (__PHYSFS_platformFileLength(fh) < dirOffset + dirLength)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
        goto QPAK_openArchive_failed;
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
    strcpy(pi->filename, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
    pi->handle = fh;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
    pi->dirOffset = dirOffset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
    pi->totalEntries = dirLength / 64;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
    pi->entries = (QPAKentry *) malloc(pi->totalEntries * sizeof (QPAKentry));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
    if (pi->entries == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
    if (qpak_loadEntries(fh, dirOffset, pi->totalEntries, pi->entries) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
    qpak_sort_entries(pi->entries, pi->totalEntries);
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
    pi->root = qpak_newDirectory("");
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
    if (pi->root == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
    if (qpak_populateDirectories(pi->entries, pi->totalEntries, pi->root) == 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
        goto QPAK_openArchive_failed;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
    retval->funcs = &__PHYSFS_DirFunctions_QPAK;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
QPAK_openArchive_failed:
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
    if (retval != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
        if (retval->opaque != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
            qpak_deletePakInfo((QPAKinfo *) retval->opaque);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
        free(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
    if (fh != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
        __PHYSFS_platformClose(fh);
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
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
} /* QPAK_openArchive */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
static void QPAK_dirClose(DirHandle *dirHandle)
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
    QPAKinfo *info = (QPAKinfo *) dirHandle->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
    __PHYSFS_platformClose(info->handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
    free(info->filename);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
    free(info);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
    free(dirHandle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
} /* QPAK_dirClose */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
static LinkedStringList *QPAK_enumerateFiles(DirHandle *h, const char *dirname,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
                                             int omitSymLinks)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
    LinkedStringList *retval = NULL, *p = NULL;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
    QPAKdirectory *dir;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
    QPAKinfo *info = (QPAKinfo *) h->opaque;
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
    if ((dirname == NULL) || (*dirname == '\0'))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
        dir = info->root;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
    else
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
        dir = qpak_findDirectory(info->root, dirname);
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
    if (dir != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
        QPAKdirectory *child = dir->dirs;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
        QPAKdirentry *file = dir->files;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
        while (child != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
            retval = __PHYSFS_addToLinkedStringList(retval, &p, child->name, -1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
            child = child->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
        } /* while */
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
        while (file != NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
        {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
            retval = __PHYSFS_addToLinkedStringList(retval, &p, file->name, -1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
            file = file->next;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
        } /* while */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
} /* QPAK_enumerateFiles */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
static int QPAK_exists(DirHandle *h, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
    QPAKinfo *driver = (QPAKinfo *) h->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
    if ((name == NULL) || (*name == '\0'))
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
        return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
    if (qpak_findDirectory(driver->root, name) != 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
        return(1);
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
    if (qpak_findEntry(driver->root, name) != 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
        return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
    return(0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
} /* QPAK_exists */
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_isDirectory(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
    QPAKinfo *info = (QPAKinfo *) h->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
    return(qpak_findDirectory(info->root, name) != 0);
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
static int QPAK_isSymLink(DirHandle *h, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   604
    return(0); /* we don't support symlinks for now */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
} /* QPAK_isSymlink */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
static PHYSFS_sint64 QPAK_getLastModTime(DirHandle *h, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   609
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   610
    return(__PHYSFS_platformGetLastModTime(((QPAKinfo *) h->opaque)->filename));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   611
} /* QPAK_getLastModTime */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   614
static void *qpak_getFileHandle(const char *name, QPAKentry *entry)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
    void *retval = __PHYSFS_platformOpenRead(name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
    if (retval == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   618
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   619
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
    if (!__PHYSFS_platformSeek(retval, entry->offset))
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
        __PHYSFS_platformClose(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   623
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   624
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   625
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   626
    return(retval);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
} /* qpak_getFileHandle */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   628
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
static FileHandle *QPAK_openRead(DirHandle *h, const char *name)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   632
    QPAKinfo *driver = (QPAKinfo *) h->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
    QPAKentry *entry = qpak_findEntry(driver->root, name);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
    QPAKfileinfo *fileDriver = 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
    FileHandle *result = 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
    if (entry == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
 
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
    fileDriver = (QPAKfileinfo *) malloc(sizeof (QPAKfileinfo));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
    BAIL_IF_MACRO(fileDriver == NULL, ERR_OUT_OF_MEMORY, NULL);
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->handle = qpak_getFileHandle(driver->filename, entry);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
    if (fileDriver->handle == NULL)
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
        free(fileDriver);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
        return(NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   648
    } /* if */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
    fileDriver->entry = entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
    fileDriver->curPos = 0;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   653
    result = (FileHandle *)malloc(sizeof (FileHandle));
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   654
    if (result == NULL)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
    {
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
        __PHYSFS_platformClose(fileDriver->handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
        free(fileDriver);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   658
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   659
    } /* if */
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
    result->opaque = fileDriver;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
    result->dirHandle = h;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   663
    result->funcs = &__PHYSFS_FileFunctions_QPAK;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   664
    return(result);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
} /* QPAK_openRead */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   666
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
static PHYSFS_sint64 QPAK_read(FileHandle *handle, void *buffer,
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
    QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   672
    QPAKentry *entry = finfo->entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   673
    PHYSFS_uint64 bytesLeft = entry->size - finfo->curPos;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
    PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   675
    PHYSFS_sint64 rc;
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
    if (objsLeft < objCount)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   678
        objCount = (PHYSFS_uint32) objsLeft;
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
    rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   681
    if (rc > 0)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   682
        finfo->curPos += (rc * objSize);
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
    return(rc);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   685
} /* QPAK_read */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   687
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   688
static int QPAK_eof(FileHandle *handle)
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
    QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   691
    QPAKentry *entry = finfo->entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   692
    
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   693
    return(finfo->curPos >= (PHYSFS_sint64) entry->size);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   694
} /* QPAK_eof */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   697
static PHYSFS_sint64 QPAK_tell(FileHandle *handle)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   698
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   699
    return(((QPAKfileinfo *) handle->opaque)->curPos);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   700
} /* QPAK_tell */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   701
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
static int QPAK_seek(FileHandle *handle, PHYSFS_uint64 offset)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   704
{
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   705
    QPAKfileinfo *finfo = (QPAKfileinfo *) handle->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   706
    QPAKentry *entry = finfo->entry;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
    PHYSFS_uint64 newPos = entry->offset + offset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   708
    int rc;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   710
    BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   711
    BAIL_IF_MACRO(newPos > entry->offset + entry->size, ERR_PAST_EOF, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   712
    rc = __PHYSFS_platformSeek(finfo->handle, newPos);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   713
    if (rc)
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   714
        finfo->curPos = offset;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   715
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   716
    return(rc);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   717
}  /* QPAK_seek */
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
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   720
static PHYSFS_sint64 QPAK_fileLength(FileHandle *handle)
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
    return ((QPAKfileinfo *) handle->opaque)->entry->size;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   723
} /* QPAK_fileLength */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   724
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   725
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   726
static int QPAK_fileClose(FileHandle *handle)
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
    QPAKfileinfo *finfo = (QPAKfileinfo *) handle->opaque;
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   729
    BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   730
    free(finfo);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   731
    free(handle);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   732
    return(1);
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   733
} /* QPAK_fileClose */
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
#endif  /* defined PHYSFS_SUPPORTS_QPAK */
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   736
b60e00958aad Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   737
/* end of qpak.c ... */
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