archivers/mix.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 26 Sep 2004 00:25:04 +0000
changeset 648 5c993684b8f2
parent 631 17fdeeaf7b51
child 650 298b8bb26775
permissions -rw-r--r--
Cleaned up archiver interface to not deal with DirHandles anymore, which simplifies things, removes some responsibility and code duplication from the archivers, and trims some malloc pressure. Also ripped up the allocation hook code a little. We'll try to screw with memory locking later, since it makes everything ugly and complex. Oh well.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
    83
static void MIX_dirClose(void *opaque);
631
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);
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
    94
static void *MIX_openArchive(const char *name, int forWriting);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
    95
static LinkedStringList *MIX_enumerateFiles(void *opaque,
631
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);
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
    98
static int MIX_exists(void *opaque, const char *name);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
    99
static int MIX_isDirectory(void *opaque, const char *name, int *fileExists);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   100
static int MIX_isSymLink(void *opaque, const char *name, int *fileExists);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   101
static PHYSFS_sint64 MIX_getLastModTime(void *opaque, const char *n, int *e);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   102
static FileHandle *MIX_openRead(void *opaque, const char *name, int *exist);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   103
static FileHandle *MIX_openWrite(void *opaque, const char *name);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   104
static FileHandle *MIX_openAppend(void *opaque, const char *name);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   105
static int MIX_remove(void *opaque, const char *name);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   106
static int MIX_mkdir(void *opaque, const char *name);
631
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
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   179
static void MIX_dirClose(void *opaque)
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   181
    MIXinfo *info = ((MIXinfo *) opaque);
631
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
} /* MIX_dirClose */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
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
static PHYSFS_sint64 MIX_read(FileHandle *handle, void *buffer,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
                              PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    MIXfileinfo *finfo = (MIXfileinfo*)handle->opaque;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    MIXentry *entry = finfo->entry;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
    PHYSFS_uint32 read;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    /* set position in the archive */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
    __PHYSFS_platformSeek(finfo->handle,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
                             finfo->info->delta +
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
                             entry->start_offset +
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
                             finfo->cur_pos);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
    /* read n bytes */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
    read = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
    /* keep filepointer up to date */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
    if (read)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
        finfo->cur_pos += read * objSize;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
    return(read);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
} /* MIX_read */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
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
static PHYSFS_sint64 MIX_write(FileHandle *handle, const void *buffer,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
    BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
} /* MIX_write */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
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
static int MIX_eof(FileHandle *handle)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
    MIXfileinfo *fifo = (MIXfileinfo *) handle->opaque;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
    return(fifo->cur_pos >= fifo->size);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
} /* MIX_eof */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
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
static PHYSFS_sint64 MIX_tell(FileHandle *handle)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    return(((MIXfileinfo *) (handle->opaque))->cur_pos);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
} /* MIX_tell */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
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
static int MIX_seek(FileHandle *handle, PHYSFS_uint64 offset)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
    MIXfileinfo *h = (MIXfileinfo *) (handle->opaque);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    BAIL_IF_MACRO(offset >= h->size, ERR_PAST_EOF, 0);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    h->cur_pos = offset;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    return(1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
} /* MIX_seek */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
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
static PHYSFS_sint64 MIX_fileLength(FileHandle *handle)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
    return (((MIXfileinfo *) (handle->opaque))->size);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
} /* MIX_fileLength */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
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
static int MIX_fileClose(FileHandle *handle)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    MIXfileinfo *finfo = (MIXfileinfo *) handle->opaque;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    __PHYSFS_platformClose(finfo->handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
    free(finfo);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
    free(handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
    return(1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
} /* MIX_fileClose */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
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
static int MIX_isArchive(const char *filename, int forWriting)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
    /* TODO:
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
            write a simple detection routine for MIX files.
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
            Unfortunaly MIX files have no ID in the header.
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
    */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
    return(1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
} /* MIX_isArchive */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
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
 * 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
   270
 */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
static int readui32(void *in, PHYSFS_uint32 *val)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
    PHYSFS_uint32 v;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
    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
   275
    *val = PHYSFS_swapULE32(v);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
    return(1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
} /* readui32 */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
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
 * 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
   282
 */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
static int readui16(void *in, PHYSFS_uint16 *val)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
    PHYSFS_uint16 v;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
    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
   287
    *val = PHYSFS_swapULE16(v);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
    return(1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
} /* readui16 */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   292
static void *MIX_openArchive(const char *name, int forWriting)
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   294
    PHYSFS_uint32 i = 0;
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   295
    MIXinfo *info = NULL;
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   296
    void *handle = NULL;
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    info = (MIXinfo *) malloc(sizeof (MIXinfo));
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   299
    BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   300
    memset(info, '\0', sizeof (MIXinfo));
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   302
    info->filename = (char *) malloc(strlen(name) + 1);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   303
    if (info->filename == NULL)
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
    {
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   305
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   306
        goto MIX_openArchive_failed;
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
    } /* if */
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
    /* store filename */
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   310
    strcpy(info->filename, name);
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
    /* open the file */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
    handle = __PHYSFS_platformOpenRead(name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
    if (!handle)
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   315
        goto MIX_openArchive_failed;
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   316
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
    /* read the MIX header */
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   318
    if ( (!readui16(handle, &info->header.num_files)) ||
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   319
         (!readui32(handle, &info->header.filesize)) )
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   320
        goto MIX_openArchive_failed;
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   321
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   322
    info->delta = 6 + (info->header.num_files * 12);
631
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
    /* allocate space for the entries and read the entries */
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   325
    info->entry = malloc(sizeof (MIXentry) * info->header.num_files);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   326
    if (info->entry == NULL)
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
    {
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   328
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   329
        goto MIX_openArchive_failed;
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
    } /* if */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
    /* read the directory list */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
    for (i = 0; i < header.num_files; i++)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
    {
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   335
        if ( (!readui32(handle, &info->entry[i].hash)) ||
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   336
             (!readui32(handle, &info->entry[i].start_offset)) ||
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   337
             (!readui32(handle, &info->entry[i].end_offset)) )
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   338
            goto MIX_openArchive_failed;
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
    } /* for */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
    __PHYSFS_platformClose(handle);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   343
    return(info);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   344
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   345
MIX_openArchive_failed:
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   346
    if (info != NULL)
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   347
    {
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   348
        if (info->filename != NULL)
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   349
            free(info->filename);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   350
        if (info->entry != NULL)
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   351
            free(info->entry);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   352
        free(info);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   353
    } /* if */
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   354
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   355
    if (handle != NULL)
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   356
        __PHYSFS_platformClose(handle);
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   357
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   358
    return(NULL);
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
} /* MIX_openArchive */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   362
static LinkedStringList *MIX_enumerateFiles(void *opaque,
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
                                            const char *dirname,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
                                            int omitSymLinks)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
    LinkedStringList *retval = NULL, *p = NULL;
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   367
    MIXinfo *info = (MIXinfo*) opaque;
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
    MIXentry *entry = info->entry;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    int i;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
    char buffer[32];
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    for (i = 0; i < info->header.num_files; i++, entry++)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
        sprintf(buffer, "%X", entry->hash);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
        retval = __PHYSFS_addToLinkedStringList(retval, &p, buffer, -1);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
    } /* for */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
    return(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
} /* MIX_enumerateFiles */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
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
static MIXentry *MIX_find_entry(MIXinfo *info, const char *name)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
    MIXentry *entry = info->entry;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
    PHYSFS_uint32 i, id;
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
    /* create hash */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
    id = MIX_hash(name);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
    /* look for this hash */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
    for (i = 0; i < info->header.num_files; i++, entry++)
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
        if (entry->hash == id)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
            return(entry);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
    } /* for */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
    /* nothing found... :( */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
    return(NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
} /* MIX_find_entry */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   402
static int MIX_exists(void *opaque, const char *name)
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   404
    return(MIX_find_entry(((MIXinfo *) opaque), name) != NULL);
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
} /* MIX_exists */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   408
static int MIX_isDirectory(void *opaque, const char *name, int *fileExists)
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   410
    *fileExists = MIX_exists(opaque, name);
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
    return(0);  /* never directories in a MIX */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
} /* MIX_isDirectory */
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
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   415
static int MIX_isSymLink(void *opaque, const char *name, int *fileExists)
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   416
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   417
    *fileExists = MIX_exists(opaque, name);
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
    return(0);  /* never symlinks in a MIX. */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
} /* MIX_isSymLink */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   422
static PHYSFS_sint64 MIX_getLastModTime(void *opaque,
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
                                        const char *name,
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
                                        int *fileExists)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
{
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   426
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);  /* !!! FIXME: return .MIX's modtime. */
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
} /* MIX_getLastModTime */
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
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   430
static FileHandle *MIX_openRead(void *opaque, const char *fnm, int *fileExists)
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
    FileHandle *retval;
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   433
    MIXinfo *info = ((MIXinfo*) opaque);
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
    MIXfileinfo *finfo;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
    MIXentry *entry;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
    /* try to find this file */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
    entry = MIX_find_entry(info,fnm);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
    BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
    /* allocate a MIX handle */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
    finfo = (MIXfileinfo *) malloc(sizeof (MIXfileinfo));
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
    BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
    /* allocate a filehandle */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
    retval = (FileHandle*) malloc(sizeof (FileHandle));
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
    if (!retval)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
        free(finfo);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
    } /* if */
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
    /* open the archive */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
    finfo->handle = __PHYSFS_platformOpenRead(info->filename);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
    if(!finfo->handle)
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
    {
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
        free(finfo);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
        free(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
        return(NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
    };
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
    /* setup structures */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
    finfo->cur_pos = 0;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
    finfo->info = info;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
    finfo->entry = entry;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
    finfo->size = entry->end_offset - entry->start_offset;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
    
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
    retval->opaque = (void *) finfo;
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
    retval->funcs = &__PHYSFS_FileFunctions_MIX;
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   470
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
    return(retval);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
} /* MIX_openRead */
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
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   475
static FileHandle *MIX_openWrite(void *opaque, const char *name)
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
} /* MIX_openWrite */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   481
static FileHandle *MIX_openAppend(void *opaque, const char *name)
631
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
    BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
} /* MIX_openAppend */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   487
static int MIX_remove(void *opaque, const char *name)
631
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
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
} /* MIX_remove */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
648
5c993684b8f2 Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents: 631
diff changeset
   493
static int MIX_mkdir(void *opaque, const char *name)
631
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
{
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
    BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
} /* MIX_mkdir */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
#endif  /* defined PHYSFS_SUPPORTS_MIX */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
/* end of mix.c ... */
17fdeeaf7b51 Westwood MIX archive support.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501