archivers/mix.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 09 Apr 2004 06:36:09 +0000
changeset 631 17fdeeaf7b51
child 648 5c993684b8f2
permissions -rw-r--r--
Westwood MIX archive support.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * MIX support routines for PhysicsFS.
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * This driver handles old archives used in the famous games
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 * Command&Conquer Tiberium Dawn and Command&Conquer Red Alert.
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 * Newer MIX files as they are used in C&C Tiberium Sun and C&C Red Alert 2
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 * aren't supported yet. Keep your eyes open for future updates.
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 *
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 * A MIX file has three parts:
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *   (1) Header
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 *        16bit integer  -> number of files stored in this MIX
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *        32bit integer  -> filesize
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *   (2) "Directory"
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
 *        32bit integer  -> hash of the filename
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 *        32bit integer  -> starting offset in the MIX
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
 *        32bit integer  -> end offset in the MIX
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
 *   (3) Data (BODY)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
 *        All data comes here
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
 *
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
 * NOTES:
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
 *  The offsets are relative to the body. So offset 0 is directly after
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *  the directory.
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
 *
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 *  Filenames only exist as hashes. So enumerate_files() will only report all
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
 *  hashes. Searching a filename in hashes is extremly quick so I decided not
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
 *  to include any sorting routines after then opening of the archive.
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
 *
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
 *
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
 * I found the structure of MIX files here: 
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
 * http://www.geocities.com/SiliconValley/8682/cncmap1f.txt
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
 *
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
 *
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
 * Please see the file LICENSE in the source's root directory.
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
 *
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
 *  This file written by Sebastian Steinhauer <steini@steini-welt.de>
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
 */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
 
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
#if HAVE_CONFIG_H
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
#  include <config.h>
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
#endif
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
#if (defined PHYSFS_SUPPORTS_MIX)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
#include <stdio.h>
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
#include <stdlib.h>
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
#include <string.h>
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
#include "physfs.h"
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
#define __PHYSICSFS_INTERNAL__
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
#include "physfs_internal.h"
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
typedef struct
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    PHYSFS_uint16 num_files;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    PHYSFS_uint32 filesize;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
} MIXheader;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
typedef struct
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    PHYSFS_uint32 hash;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    PHYSFS_uint32 start_offset;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    PHYSFS_uint32 end_offset;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
} MIXentry;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
typedef struct
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    char *filename; /* filename of the archive */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    MIXentry *entry; /* list of entries */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    MIXheader header; /* the header of the MIX file */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    PHYSFS_uint32 delta; /* size of header + entries */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
} MIXinfo;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
typedef struct
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    PHYSFS_uint64 size; /* filesize */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    PHYSFS_uint64 cur_pos; /* position in this file */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    MIXentry *entry; /* pointer to the MIX entry */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    MIXinfo *info; /* pointer to our MIXinfo */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    void *handle; /* filehandle */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
} MIXfileinfo;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
static void MIX_dirClose(DirHandle *h);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
static PHYSFS_sint64 MIX_read(FileHandle *handle, void *buffer,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
                              PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
static PHYSFS_sint64 MIX_write(FileHandle *handle, const void *buffer,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
static int MIX_eof(FileHandle *handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
static PHYSFS_sint64 MIX_tell(FileHandle *handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
static int MIX_seek(FileHandle *handle, PHYSFS_uint64 offset);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
static PHYSFS_sint64 MIX_fileLength(FileHandle *handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
static int MIX_fileClose(FileHandle *handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
static int MIX_isArchive(const char *filename, int forWriting);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
static DirHandle *MIX_openArchive(const char *name, int forWriting);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
static LinkedStringList *MIX_enumerateFiles(DirHandle *h,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
                                            const char *dirname,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
                                            int omitSymLinks);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
static int MIX_exists(DirHandle *h, const char *name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
static int MIX_isDirectory(DirHandle *h, const char *name, int *fileExists);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
static int MIX_isSymLink(DirHandle *h, const char *name, int *fileExists);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
static PHYSFS_sint64 MIX_getLastModTime(DirHandle *h, const char *n, int *e);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
static FileHandle *MIX_openRead(DirHandle *h, const char *name, int *exist);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
static FileHandle *MIX_openWrite(DirHandle *h, const char *name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
static FileHandle *MIX_openAppend(DirHandle *h, const char *name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
static int MIX_remove(DirHandle *h, const char *name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
static int MIX_mkdir(DirHandle *h, const char *name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MIX =
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    "MIX",
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
    "Westwood archive (Tiberian Dawn / Red Alert)",
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    "Sebastian Steinhauer <steini@steini-welt.de>",
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    "http://icculus.org/physfs/",
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
};
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
static const FileFunctions __PHYSFS_FileFunctions_MIX =
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    MIX_read,       /* read() method       */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    MIX_write,      /* write() method      */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    MIX_eof,        /* eof() method        */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    MIX_tell,       /* tell() method       */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    MIX_seek,       /* seek() method       */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
    MIX_fileLength, /* fileLength() method */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    MIX_fileClose   /* fileClose() method  */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
};
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
const DirFunctions __PHYSFS_DirFunctions_MIX =
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    &__PHYSFS_ArchiveInfo_MIX,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    MIX_isArchive,          /* isArchive() method      */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
    MIX_openArchive,        /* openArchive() method    */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
    MIX_enumerateFiles,     /* enumerateFiles() method */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
    MIX_exists,             /* exists() method         */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    MIX_isDirectory,        /* isDirectory() method    */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    MIX_isSymLink,          /* isSymLink() method      */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    MIX_getLastModTime,     /* getLastModTime() method */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    MIX_openRead,           /* openRead() method       */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    MIX_openWrite,          /* openWrite() method      */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    MIX_openAppend,         /* openAppend() method     */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    MIX_remove,             /* remove() method         */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    MIX_mkdir,              /* mkdir() method          */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    MIX_dirClose            /* dirClose() method       */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
};
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
static PHYSFS_uint32 MIX_hash(const char *name)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    PHYSFS_uint32 id = 0;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    PHYSFS_uint32 a = 0;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    PHYSFS_uint32 i = 0;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    PHYSFS_uint32 l;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
    PHYSFS_uint32 j;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    l = strlen(name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
    while (i < l)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
        a = 0;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
        for(j = 0; j < 4; j++)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
        {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
            a >>= 8;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
            if (i < l)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
            {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
                a += (unsigned int) (name[i]) << 24;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
                i++;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
            } /* if */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
        } /* for */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
        id = (id << 1 | id >> 31) + a;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
    } /* while */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
    /* a bit debuggin :)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
    /printf("Filename %s -> %X\n",name,id); */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    return(id);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
} /* MIX_hash */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
static void MIX_dirClose(DirHandle *h)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
    MIXinfo *info = ((MIXinfo *) h->opaque);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
    free(info->entry);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
    free(info->filename);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
    free(h);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
} /* MIX_dirClose */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
static PHYSFS_sint64 MIX_read(FileHandle *handle, void *buffer,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
                              PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    MIXfileinfo *finfo = (MIXfileinfo*)handle->opaque;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
    MIXentry *entry = finfo->entry;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    PHYSFS_uint32 read;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
    /* set position in the archive */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
    __PHYSFS_platformSeek(finfo->handle,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
                             finfo->info->delta +
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
                             entry->start_offset +
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
                             finfo->cur_pos);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
    /* read n bytes */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
    read = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
    /* keep filepointer up to date */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    if (read)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
        finfo->cur_pos += read * objSize;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
    return(read);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
} /* MIX_read */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
static PHYSFS_sint64 MIX_write(FileHandle *handle, const void *buffer,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
    BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
} /* MIX_write */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
static int MIX_eof(FileHandle *handle)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
    MIXfileinfo *fifo = (MIXfileinfo *) handle->opaque;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
    return(fifo->cur_pos >= fifo->size);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
} /* MIX_eof */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
static PHYSFS_sint64 MIX_tell(FileHandle *handle)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    return(((MIXfileinfo *) (handle->opaque))->cur_pos);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
} /* MIX_tell */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
static int MIX_seek(FileHandle *handle, PHYSFS_uint64 offset)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    MIXfileinfo *h = (MIXfileinfo *) (handle->opaque);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    BAIL_IF_MACRO(offset >= h->size, ERR_PAST_EOF, 0);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    h->cur_pos = offset;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
    return(1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
} /* MIX_seek */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
static PHYSFS_sint64 MIX_fileLength(FileHandle *handle)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
    return (((MIXfileinfo *) (handle->opaque))->size);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
} /* MIX_fileLength */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
static int MIX_fileClose(FileHandle *handle)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    MIXfileinfo *finfo = (MIXfileinfo *) handle->opaque;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
    __PHYSFS_platformClose(finfo->handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
    free(finfo);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
    free(handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
    return(1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
} /* MIX_fileClose */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
static int MIX_isArchive(const char *filename, int forWriting)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
    /* TODO:
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
            write a simple detection routine for MIX files.
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
            Unfortunaly MIX files have no ID in the header.
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
    */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    return(1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
} /* MIX_isArchive */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
/*
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
 * Read an unsigned 32-bit int and swap to native byte order.
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
 */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
static int readui32(void *in, PHYSFS_uint32 *val)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
    PHYSFS_uint32 v;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
    BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
    *val = PHYSFS_swapULE32(v);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
    return(1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
} /* readui32 */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
/*
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
 * Read an unsigned 16-bit int and swap to native byte order.
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
 */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
static int readui16(void *in, PHYSFS_uint16 *val)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
    PHYSFS_uint16 v;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
    BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
    *val = PHYSFS_swapULE16(v);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    return(1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
} /* readui16 */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
static DirHandle *MIX_openArchive(const char *name, int forWriting)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
    PHYSFS_uint32 i;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
    DirHandle *retval;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
    MIXheader header;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    MIXinfo *info;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
    MIXentry *entries;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
    void *handle;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
    char *fname;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
    retval = (DirHandle *) malloc(sizeof (DirHandle));
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
    info = (MIXinfo *) malloc(sizeof (MIXinfo));
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
    if (info == NULL)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
        free(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    } /* if */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
    fname = (char*) malloc(strlen(name) + 1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
    if (fname == NULL)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
        free(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
        free(info);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
        BAIL_MACRO(ERR_OUT_OF_MEMORY,NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
    } /* if */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
    /* store filename */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
    strcpy(fname, name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
    /* open the file */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
    handle = __PHYSFS_platformOpenRead(name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
    if (!handle)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
        free(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
        free(info);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
        return(NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
    } /* if */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
        
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
    /* read the MIX header */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
    if ( (!readui16(handle, &header.num_files)) ||
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
         (!readui32(handle, &header.filesize)) )
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
        free(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
        free(info);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
        __PHYSFS_platformClose(handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
        return(NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
    } /* if */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
    /* allocate space for the entries and read the entries */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
    entries = (MIXentry *) malloc(sizeof (MIXentry) * header.num_files);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
    if (!entries)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
        free(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
        free(info);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
        __PHYSFS_platformClose(handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    } /* if */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
    /* read the directory list */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    for (i = 0; i < header.num_files; i++)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
        if ( (!readui32(handle, &entries[i].hash)) ||
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
             (!readui32(handle, &entries[i].start_offset)) ||
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
             (!readui32(handle, &entries[i].end_offset)) )
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
        {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
            free(entries);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
            free(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
            free(info);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
            __PHYSFS_platformClose(handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
            return(NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
        } /* if */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
    } /* for */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
    __PHYSFS_platformClose(handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
    /* create our driver structure and create the DirHandle */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
    info->header = header;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    info->entry = entries;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
    info->filename = fname;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
    info->delta = 6 + (header.num_files*12);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
    retval->funcs = &__PHYSFS_DirFunctions_MIX;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
    retval->opaque = info;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
    return(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
} /* MIX_openArchive */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
static LinkedStringList *MIX_enumerateFiles(DirHandle *h,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
                                            const char *dirname,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
                                            int omitSymLinks)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
    LinkedStringList *retval = NULL, *p = NULL;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
    MIXinfo *info = (MIXinfo*)h->opaque;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
    MIXentry *entry = info->entry;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
    int i;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
    char buffer[32];
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
    for (i = 0; i < info->header.num_files; i++, entry++)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
        sprintf(buffer, "%X", entry->hash);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
        retval = __PHYSFS_addToLinkedStringList(retval, &p, buffer, -1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
    } /* for */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
    return(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
} /* MIX_enumerateFiles */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
static MIXentry *MIX_find_entry(MIXinfo *info, const char *name)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
    MIXentry *entry = info->entry;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
    PHYSFS_uint32 i, id;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
    /* create hash */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
    id = MIX_hash(name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
    /* look for this hash */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
    for (i = 0; i < info->header.num_files; i++, entry++)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
        if (entry->hash == id)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
            return(entry);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   416
    } /* for */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
    /* nothing found... :( */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
    return(NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
} /* MIX_find_entry */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
static int MIX_exists(DirHandle *h, const char *name)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
    return(MIX_find_entry(((MIXinfo *) h->opaque), name) != NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
} /* MIX_exists */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
static int MIX_isDirectory(DirHandle *h, const char *name, int *fileExists)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
    *fileExists = MIX_exists(h, name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
    return(0);  /* never directories in a MIX */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
} /* MIX_isDirectory */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
static int MIX_isSymLink(DirHandle *h, const char *name, int *fileExists)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
    *fileExists = MIX_exists(h, name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
    return(0);  /* never symlinks in a MIX. */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
} /* MIX_isSymLink */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
static PHYSFS_sint64 MIX_getLastModTime(DirHandle *h,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
                                        const char *name,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
                                        int *fileExists)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
} /* MIX_getLastModTime */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
static FileHandle *MIX_openRead(DirHandle *h, const char *fnm, int *fileExists)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
    FileHandle *retval;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
    MIXinfo *info = ((MIXinfo*)h->opaque);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
    MIXfileinfo *finfo;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
    MIXentry *entry;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
    /* try to find this file */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
    entry = MIX_find_entry(info,fnm);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
    BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
    /* allocate a MIX handle */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
    finfo = (MIXfileinfo *) malloc(sizeof (MIXfileinfo));
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
    BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
    /* allocate a filehandle */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
    retval = (FileHandle*) malloc(sizeof (FileHandle));
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
    if (!retval)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   470
        free(finfo);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
    } /* if */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
    /* open the archive */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
    finfo->handle = __PHYSFS_platformOpenRead(info->filename);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
    if(!finfo->handle)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
        free(finfo);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
        free(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
        return(NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
    };
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
    /* setup structures */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
    finfo->cur_pos = 0;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
    finfo->info = info;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
    finfo->entry = entry;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
    finfo->size = entry->end_offset - entry->start_offset;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
    retval->opaque = (void *) finfo;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
    retval->funcs = &__PHYSFS_FileFunctions_MIX;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
    retval->dirHandle = h;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
    return(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
} /* MIX_openRead */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
static FileHandle *MIX_openWrite(DirHandle *h, const char *name)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
} /* MIX_openWrite */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
static FileHandle *MIX_openAppend(DirHandle *h, const char *name)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
} /* MIX_openAppend */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
static int MIX_remove(DirHandle *h, const char *name)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
} /* MIX_remove */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
static int MIX_mkdir(DirHandle *h, const char *name)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
} /* MIX_mkdir */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
#endif  /* defined PHYSFS_SUPPORTS_MIX */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
/* end of mix.c ... */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523