misc/libphysfs/archiver_lzma.c
author unC0Rr
Fri, 05 Feb 2016 11:32:23 +0300
branch0.9.22
changeset 12400 c7d73e5840c8
parent 8524 a65e9bcf0a03
child 12213 bb5522e88ab2
permissions -rw-r--r--
Racer maps #18 and #19
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     1
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     2
 * LZMA support routines for PhysicsFS.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     3
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     4
 * Please see the file lzma.txt in the lzma/ directory.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     5
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     6
 *  This file was written by Dennis Schridde, with some peeking at "7zMain.c"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     7
 *   by Igor Pavlov.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     8
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     9
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    10
#define __PHYSICSFS_INTERNAL__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    11
#include "physfs_internal.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    12
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    13
#if PHYSFS_SUPPORTS_7Z
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    14
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    15
#include "lzma/C/7zCrc.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    16
#include "lzma/C/Archive/7z/7zIn.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    17
#include "lzma/C/Archive/7z/7zExtract.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    18
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    19
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    20
/* 7z internal from 7zIn.c */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    21
extern int TestSignatureCandidate(Byte *testBytes);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    22
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    23
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    24
#ifdef _LZMA_IN_CB
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    25
# define BUFFER_SIZE (1 << 12)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    26
#endif /* _LZMA_IN_CB */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    27
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    28
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    29
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    30
 * Carries filestream metadata through 7z
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    31
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    32
typedef struct _FileInputStream
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    33
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    34
    ISzAlloc allocImp; /* Allocation implementation, used by 7z */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    35
    ISzAlloc allocTempImp; /* Temporary allocation implementation, used by 7z */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    36
    ISzInStream inStream; /* Input stream with read callbacks, used by 7z */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    37
    PHYSFS_Io *io;  /* Filehandle, used by read implementation */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    38
#ifdef _LZMA_IN_CB
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    39
    Byte buffer[BUFFER_SIZE]; /* Buffer, used by read implementation */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    40
#endif /* _LZMA_IN_CB */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    41
} FileInputStream;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    42
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    43
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    44
 * In the 7z format archives are splited into blocks, those are called folders
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
 * Set by LZMA_read()
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    46
*/
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    47
typedef struct _LZMAfolder
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    48
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    49
    PHYSFS_uint32 index; /* Index of folder in archive */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    50
    PHYSFS_uint32 references; /* Number of files using this block */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    51
    PHYSFS_uint8 *cache; /* Cached folder */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    52
    size_t size; /* Size of folder */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    53
} LZMAfolder;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    54
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    55
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    56
 * Set by LZMA_openArchive(), except folder which gets it's values
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    57
 *  in LZMA_read()
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    58
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    59
typedef struct _LZMAarchive
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    60
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    61
    struct _LZMAfile *files; /* Array of files, size == archive->db.Database.NumFiles */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    62
    LZMAfolder *folders; /* Array of folders, size == archive->db.Database.NumFolders */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    63
    CArchiveDatabaseEx db; /* For 7z: Database */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    64
    FileInputStream stream; /* For 7z: Input file incl. read and seek callbacks */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    65
} LZMAarchive;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    66
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    67
/* Set by LZMA_openArchive(), except offset which is set by LZMA_read() */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    68
typedef struct _LZMAfile
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    69
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    70
    PHYSFS_uint32 index; /* Index of file in archive */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    71
    LZMAarchive *archive; /* Link to corresponding archive */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    72
    LZMAfolder *folder; /* Link to corresponding folder */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    73
    CFileItem *item; /* For 7z: File info, eg. name, size */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    74
    size_t offset; /* Offset in folder */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    75
    size_t position; /* Current "virtual" position in file */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    76
} LZMAfile;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    77
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    78
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    79
/* Memory management implementations to be passed to 7z */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    80
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    81
static void *SzAllocPhysicsFS(size_t size)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    82
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    83
    return ((size == 0) ? NULL : allocator.Malloc(size));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    84
} /* SzAllocPhysicsFS */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    85
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    86
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    87
static void SzFreePhysicsFS(void *address)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    88
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    89
    if (address != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    90
        allocator.Free(address);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    91
} /* SzFreePhysicsFS */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    92
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    93
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    94
/* Filesystem implementations to be passed to 7z */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    95
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    96
#ifdef _LZMA_IN_CB
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    97
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    98
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    99
 * Read implementation, to be passed to 7z
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
 * WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   101
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   102
SZ_RESULT SzFileReadImp(void *object, void **buffer, size_t maxReqSize,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   103
                        size_t *processedSize)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   104
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   105
    FileInputStream *s = (FileInputStream *)(object - offsetof(FileInputStream, inStream)); /* HACK! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   106
    PHYSFS_sint64 processedSizeLoc = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   107
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   108
    if (maxReqSize > BUFFER_SIZE)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   109
        maxReqSize = BUFFER_SIZE;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   110
    processedSizeLoc = s->io->read(s->io, s->buffer, maxReqSize);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   111
    *buffer = s->buffer;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   112
    if (processedSize != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   113
        *processedSize = (size_t) processedSizeLoc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   114
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   115
    return SZ_OK;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   116
} /* SzFileReadImp */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   117
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   118
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   119
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   120
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   121
 * Read implementation, to be passed to 7z
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   122
 * WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   123
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   124
SZ_RESULT SzFileReadImp(void *object, void *buffer, size_t size,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   125
                        size_t *processedSize)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   126
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   127
    FileInputStream *s = (FileInputStream *)((unsigned long)object - offsetof(FileInputStream, inStream)); /* HACK! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   128
    const size_t processedSizeLoc = s->io->read(s->io, buffer, size);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   129
    if (processedSize != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   130
        *processedSize = processedSizeLoc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   131
    return SZ_OK;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   132
} /* SzFileReadImp */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   133
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   134
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   135
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   136
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   137
 * Seek implementation, to be passed to 7z
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   138
 * WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   139
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   140
SZ_RESULT SzFileSeekImp(void *object, CFileSize pos)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   141
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   142
    FileInputStream *s = (FileInputStream *)((unsigned long)object - offsetof(FileInputStream, inStream)); /* HACK! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   143
    if (s->io->seek(s->io, (PHYSFS_uint64) pos))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   144
        return SZ_OK;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   145
    return SZE_FAIL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   146
} /* SzFileSeekImp */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   147
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   148
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   149
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   150
 * Translate Microsoft FILETIME (used by 7zip) into UNIX timestamp
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   151
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   152
static PHYSFS_sint64 lzma_filetime_to_unix_timestamp(CArchiveFileTime *ft)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   153
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   154
    /* MS counts in nanoseconds ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   155
    const PHYSFS_uint64 FILETIME_NANOTICKS_PER_SECOND = __PHYSFS_UI64(10000000);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   156
    /* MS likes to count seconds since 01.01.1601 ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   157
    const PHYSFS_uint64 FILETIME_UNIX_DIFF = __PHYSFS_UI64(11644473600);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   158
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   159
    PHYSFS_uint64 filetime = ft->Low | ((PHYSFS_uint64)ft->High << 32);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   160
    return filetime/FILETIME_NANOTICKS_PER_SECOND - FILETIME_UNIX_DIFF;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   161
} /* lzma_filetime_to_unix_timestamp */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   162
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   163
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   164
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   165
 * Compare a file with a given name, C89 stdlib variant
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   166
 * Used for sorting
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   167
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   168
static int lzma_file_cmp_stdlib(const void *key, const void *object)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   169
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   170
    const char *name = (const char *) key;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   171
    LZMAfile *file = (LZMAfile *) object;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   172
    return strcmp(name, file->item->Name);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   173
} /* lzma_file_cmp_posix */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   174
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   175
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   176
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   177
 * Compare two files with each other based on the name
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   178
 * Used for sorting
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   179
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   180
static int lzma_file_cmp(void *_a, size_t one, size_t two)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   181
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   182
    LZMAfile *files = (LZMAfile *) _a;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   183
    return strcmp(files[one].item->Name, files[two].item->Name);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   184
} /* lzma_file_cmp */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   185
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   186
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   187
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   188
 * Swap two entries in the file array
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   189
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   190
static void lzma_file_swap(void *_a, size_t one, size_t two)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   191
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   192
    LZMAfile tmp;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   193
    LZMAfile *first = &(((LZMAfile *) _a)[one]);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   194
    LZMAfile *second = &(((LZMAfile *) _a)[two]);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   195
    memcpy(&tmp, first, sizeof (LZMAfile));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   196
    memcpy(first, second, sizeof (LZMAfile));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   197
    memcpy(second, &tmp, sizeof (LZMAfile));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   198
} /* lzma_file_swap */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   199
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   200
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   201
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   202
 * Find entry 'name' in 'archive'
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   203
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   204
static LZMAfile * lzma_find_file(const LZMAarchive *archive, const char *name)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   205
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   206
    LZMAfile *file = bsearch(name, archive->files, archive->db.Database.NumFiles, sizeof(*archive->files), lzma_file_cmp_stdlib); /* FIXME: Should become __PHYSFS_search!!! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   207
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   208
    BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_NO_SUCH_PATH, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   209
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   210
    return file;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   211
} /* lzma_find_file */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   212
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   213
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   214
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   215
 * Load metadata for the file at given index
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   216
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   217
static int lzma_file_init(LZMAarchive *archive, PHYSFS_uint32 fileIndex)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   218
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   219
    LZMAfile *file = &archive->files[fileIndex];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   220
    PHYSFS_uint32 folderIndex = archive->db.FileIndexToFolderIndexMap[fileIndex];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   221
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   222
    file->index = fileIndex; /* Store index into 7z array, since we sort our own. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   223
    file->archive = archive;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   224
    file->folder = (folderIndex != (PHYSFS_uint32)-1 ? &archive->folders[folderIndex] : NULL); /* Directories don't have a folder (they contain no own data...) */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   225
    file->item = &archive->db.Database.Files[fileIndex]; /* Holds crucial data and is often referenced -> Store link */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   226
    file->position = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   227
    file->offset = 0; /* Offset will be set by LZMA_read() */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   228
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   229
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   230
} /* lzma_load_file */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   231
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   232
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   233
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   234
 * Load metadata for all files
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   235
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   236
static int lzma_files_init(LZMAarchive *archive)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   237
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   238
    PHYSFS_uint32 fileIndex = 0, numFiles = archive->db.Database.NumFiles;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   239
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   240
    for (fileIndex = 0; fileIndex < numFiles; fileIndex++ )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   241
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   242
        if (!lzma_file_init(archive, fileIndex))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   243
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   244
            return 0; /* FALSE on failure */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   245
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   246
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   247
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   248
   __PHYSFS_sort(archive->files, (size_t) numFiles, lzma_file_cmp, lzma_file_swap);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   249
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   250
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   251
} /* lzma_load_files */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   252
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   253
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   254
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   255
 * Initialise specified archive
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   256
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   257
static void lzma_archive_init(LZMAarchive *archive)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   258
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   259
    memset(archive, 0, sizeof(*archive));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   260
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   261
    /* Prepare callbacks for 7z */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   262
    archive->stream.inStream.Read = SzFileReadImp;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   263
    archive->stream.inStream.Seek = SzFileSeekImp;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   264
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   265
    archive->stream.allocImp.Alloc = SzAllocPhysicsFS;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   266
    archive->stream.allocImp.Free = SzFreePhysicsFS;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   267
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   268
    archive->stream.allocTempImp.Alloc = SzAllocPhysicsFS;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   269
    archive->stream.allocTempImp.Free = SzFreePhysicsFS;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   270
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   271
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   272
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   273
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   274
 * Deinitialise archive
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   275
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   276
static void lzma_archive_exit(LZMAarchive *archive)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   277
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   278
    /* Free arrays */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   279
    allocator.Free(archive->folders);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   280
    allocator.Free(archive->files);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   281
    allocator.Free(archive);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   282
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   283
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   284
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   285
 * Wrap all 7z calls in this, so the physfs error state is set appropriately.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   286
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   287
static int lzma_err(SZ_RESULT rc)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   288
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   289
    switch (rc)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   290
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   291
        case SZ_OK: /* Same as LZMA_RESULT_OK */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   292
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   293
        case SZE_DATA_ERROR: /* Same as LZMA_RESULT_DATA_ERROR */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   294
            __PHYSFS_setError(PHYSFS_ERR_CORRUPT); /*!!!FIXME: was "PHYSFS_ERR_DATA_ERROR" */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   295
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   296
        case SZE_OUTOFMEMORY:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   297
            __PHYSFS_setError(PHYSFS_ERR_OUT_OF_MEMORY);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   298
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   299
        case SZE_CRC_ERROR:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   300
            __PHYSFS_setError(PHYSFS_ERR_CORRUPT);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   301
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   302
        case SZE_NOTIMPL:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   303
            __PHYSFS_setError(PHYSFS_ERR_UNSUPPORTED);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   304
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   305
        case SZE_FAIL:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   306
            __PHYSFS_setError(PHYSFS_ERR_OTHER_ERROR);  /* !!! FIXME: right? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   307
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   308
        case SZE_ARCHIVE_ERROR:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   309
            __PHYSFS_setError(PHYSFS_ERR_CORRUPT);  /* !!! FIXME: right? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   310
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   311
        default:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   312
            __PHYSFS_setError(PHYSFS_ERR_OTHER_ERROR);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   313
    } /* switch */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   314
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   315
    return rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   316
} /* lzma_err */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   317
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   318
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   319
static PHYSFS_sint64 LZMA_read(PHYSFS_Io *io, void *outBuf, PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   320
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   321
    LZMAfile *file = (LZMAfile *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   322
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   323
    size_t wantedSize = (size_t) len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   324
    const size_t remainingSize = file->item->Size - file->position;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   325
    size_t fileSize = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   326
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   327
    BAIL_IF_MACRO(wantedSize == 0, ERRPASS, 0); /* quick rejection. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   328
    BAIL_IF_MACRO(remainingSize == 0, PHYSFS_ERR_PAST_EOF, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   329
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   330
    if (wantedSize > remainingSize)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   331
        wantedSize = remainingSize;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   332
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   333
    /* Only decompress the folder if it is not already cached */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   334
    if (file->folder->cache == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   335
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   336
        const int rc = lzma_err(SzExtract(
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   337
            &file->archive->stream.inStream, /* compressed data */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   338
            &file->archive->db, /* 7z's database, containing everything */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   339
            file->index, /* Index into database arrays */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   340
            /* Index of cached folder, will be changed by SzExtract */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   341
            &file->folder->index,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   342
            /* Cache for decompressed folder, allocated/freed by SzExtract */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   343
            &file->folder->cache,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   344
            /* Size of cache, will be changed by SzExtract */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   345
            &file->folder->size,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   346
            /* Offset of this file inside the cache, set by SzExtract */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   347
            &file->offset,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   348
            &fileSize, /* Size of this file */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   349
            &file->archive->stream.allocImp,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   350
            &file->archive->stream.allocTempImp));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   351
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   352
        if (rc != SZ_OK)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   353
            return -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   354
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   355
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   356
    /* Copy wanted bytes over from cache to outBuf */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   357
    memcpy(outBuf, (file->folder->cache + file->offset + file->position),
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   358
            wantedSize);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   359
    file->position += wantedSize; /* Increase virtual position */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   360
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   361
    return wantedSize;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   362
} /* LZMA_read */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   363
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   364
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   365
static PHYSFS_sint64 LZMA_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   366
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   367
    BAIL_MACRO(PHYSFS_ERR_READ_ONLY, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   368
} /* LZMA_write */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   369
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   370
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   371
static PHYSFS_sint64 LZMA_tell(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   372
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   373
    LZMAfile *file = (LZMAfile *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   374
    return file->position;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   375
} /* LZMA_tell */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   376
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   377
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   378
static int LZMA_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   379
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   380
    LZMAfile *file = (LZMAfile *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   381
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   382
    BAIL_IF_MACRO(offset > file->item->Size, PHYSFS_ERR_PAST_EOF, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   383
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   384
    file->position = offset; /* We only use a virtual position... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   385
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   386
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   387
} /* LZMA_seek */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   388
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   389
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   390
static PHYSFS_sint64 LZMA_length(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   391
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   392
    const LZMAfile *file = (LZMAfile *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   393
    return (file->item->Size);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   394
} /* LZMA_length */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   395
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   396
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   397
static PHYSFS_Io *LZMA_duplicate(PHYSFS_Io *_io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   398
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   399
    /* !!! FIXME: this archiver needs to be reworked to allow multiple
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   400
     * !!! FIXME:  opens before we worry about duplication. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   401
    BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   402
} /* LZMA_duplicate */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   403
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   404
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   405
static int LZMA_flush(PHYSFS_Io *io) { return 1;  /* no write support. */ }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   406
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   407
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   408
static void LZMA_destroy(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   409
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   410
    LZMAfile *file = (LZMAfile *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   411
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   412
    if (file->folder != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   413
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   414
        /* Only decrease refcount if someone actually requested this file... Prevents from overflows and close-on-open... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   415
        if (file->folder->references > 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   416
            file->folder->references--;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   417
        if (file->folder->references == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   418
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   419
            /* Free the cache which might have been allocated by LZMA_read() */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   420
            allocator.Free(file->folder->cache);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   421
            file->folder->cache = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   422
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   423
        /* !!! FIXME: we don't free (file) or (file->folder)?! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   424
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   425
} /* LZMA_destroy */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   426
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   427
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   428
static const PHYSFS_Io LZMA_Io =
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   429
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   430
    CURRENT_PHYSFS_IO_API_VERSION, NULL,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   431
    LZMA_read,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   432
    LZMA_write,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   433
    LZMA_seek,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   434
    LZMA_tell,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   435
    LZMA_length,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   436
    LZMA_duplicate,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   437
    LZMA_flush,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   438
    LZMA_destroy
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   439
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   440
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   441
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   442
static void *LZMA_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   443
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   444
    PHYSFS_uint8 sig[k7zSignatureSize];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   445
    size_t len = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   446
    LZMAarchive *archive = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   447
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   448
    assert(io != NULL);  /* shouldn't ever happen. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   449
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   450
    BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   451
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   452
    if (io->read(io, sig, k7zSignatureSize) != k7zSignatureSize)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   453
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   454
    BAIL_IF_MACRO(!TestSignatureCandidate(sig), PHYSFS_ERR_UNSUPPORTED, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   455
    BAIL_IF_MACRO(!io->seek(io, 0), ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   456
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   457
    archive = (LZMAarchive *) allocator.Malloc(sizeof (LZMAarchive));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   458
    BAIL_IF_MACRO(archive == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   459
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   460
    lzma_archive_init(archive);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   461
    archive->stream.io = io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   462
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   463
    CrcGenerateTable();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   464
    SzArDbExInit(&archive->db);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   465
    if (lzma_err(SzArchiveOpen(&archive->stream.inStream,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   466
                               &archive->db,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   467
                               &archive->stream.allocImp,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   468
                               &archive->stream.allocTempImp)) != SZ_OK)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   469
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   470
        SzArDbExFree(&archive->db, SzFreePhysicsFS);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   471
        lzma_archive_exit(archive);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   472
        return NULL; /* Error is set by lzma_err! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   473
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   474
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   475
    len = archive->db.Database.NumFiles * sizeof (LZMAfile);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   476
    archive->files = (LZMAfile *) allocator.Malloc(len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   477
    if (archive->files == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   478
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   479
        SzArDbExFree(&archive->db, SzFreePhysicsFS);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   480
        lzma_archive_exit(archive);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   481
        BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   482
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   483
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   484
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   485
     * Init with 0 so we know when a folder is already cached
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   486
     * Values will be set by LZMA_openRead()
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   487
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   488
    memset(archive->files, 0, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   489
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   490
    len = archive->db.Database.NumFolders * sizeof (LZMAfolder);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   491
    archive->folders = (LZMAfolder *) allocator.Malloc(len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   492
    if (archive->folders == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   493
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   494
        SzArDbExFree(&archive->db, SzFreePhysicsFS);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   495
        lzma_archive_exit(archive);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   496
        BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   497
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   498
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   499
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   500
     * Init with 0 so we know when a folder is already cached
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   501
     * Values will be set by LZMA_read()
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   502
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   503
    memset(archive->folders, 0, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   504
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   505
    if(!lzma_files_init(archive))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   506
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   507
        SzArDbExFree(&archive->db, SzFreePhysicsFS);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   508
        lzma_archive_exit(archive);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   509
        BAIL_MACRO(PHYSFS_ERR_OTHER_ERROR, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   510
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   511
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   512
    return archive;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   513
} /* LZMA_openArchive */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   514
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   515
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   516
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   517
 * Moved to seperate function so we can use alloca then immediately throw
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   518
 *  away the allocated stack space...
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   519
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   520
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   521
                           const char *odir, const char *str, size_t flen)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   522
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   523
    char *newstr = __PHYSFS_smallAlloc(flen + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   524
    if (newstr == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   525
        return;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   526
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   527
    memcpy(newstr, str, flen);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   528
    newstr[flen] = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   529
    cb(callbackdata, odir, newstr);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   530
    __PHYSFS_smallFree(newstr);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   531
} /* doEnumCallback */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   532
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   533
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   534
static void LZMA_enumerateFiles(PHYSFS_Dir *opaque, const char *dname,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   535
                                int omitSymLinks, PHYSFS_EnumFilesCallback cb,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   536
                                const char *origdir, void *callbackdata)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   537
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   538
    size_t dlen = strlen(dname),
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   539
           dlen_inc = dlen + ((dlen > 0) ? 1 : 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   540
    LZMAarchive *archive = (LZMAarchive *) opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   541
    LZMAfile *file = NULL,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   542
            *lastFile = &archive->files[archive->db.Database.NumFiles];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   543
        if (dlen)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   544
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   545
            file = lzma_find_file(archive, dname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   546
            if (file != NULL) /* if 'file' is NULL it should stay so, otherwise errors will not be handled */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   547
                file += 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   548
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   549
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   550
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   551
            file = archive->files;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   552
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   553
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   554
    BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_NO_SUCH_PATH, );
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   555
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   556
    while (file < lastFile)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   557
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   558
        const char * fname = file->item->Name;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   559
        const char * dirNameEnd = fname + dlen_inc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   560
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   561
        if (strncmp(dname, fname, dlen) != 0) /* Stop after mismatch, archive->files is sorted */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   562
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   563
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   564
        if (strchr(dirNameEnd, '/')) /* Skip subdirs */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   565
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   566
            file++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   567
            continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   568
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   569
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   570
        /* Do the actual callback... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   571
        doEnumCallback(cb, callbackdata, origdir, dirNameEnd, strlen(dirNameEnd));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   572
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   573
        file++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   574
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   575
} /* LZMA_enumerateFiles */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   576
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   577
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   578
static PHYSFS_Io *LZMA_openRead(PHYSFS_Dir *opaque, const char *name,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   579
                                int *fileExists)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   580
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   581
    LZMAarchive *archive = (LZMAarchive *) opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   582
    LZMAfile *file = lzma_find_file(archive, name);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   583
    PHYSFS_Io *io = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   584
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   585
    *fileExists = (file != NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   586
    BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_NO_SUCH_PATH, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   587
    BAIL_IF_MACRO(file->folder == NULL, PHYSFS_ERR_NOT_A_FILE, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   588
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   589
    file->position = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   590
    file->folder->references++; /* Increase refcount for automatic cleanup... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   591
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   592
    io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   593
    BAIL_IF_MACRO(io == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   594
    memcpy(io, &LZMA_Io, sizeof (*io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   595
    io->opaque = file;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   596
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   597
    return io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   598
} /* LZMA_openRead */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   599
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   600
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   601
static PHYSFS_Io *LZMA_openWrite(PHYSFS_Dir *opaque, const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   602
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   603
    BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   604
} /* LZMA_openWrite */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   605
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   606
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   607
static PHYSFS_Io *LZMA_openAppend(PHYSFS_Dir *opaque, const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   608
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   609
    BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   610
} /* LZMA_openAppend */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   611
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   612
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   613
static void LZMA_closeArchive(PHYSFS_Dir *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   614
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   615
    LZMAarchive *archive = (LZMAarchive *) opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   616
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   617
#if 0  /* !!! FIXME: you shouldn't have to do this. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   618
    PHYSFS_uint32 fileIndex = 0, numFiles = archive->db.Database.NumFiles;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   619
    for (fileIndex = 0; fileIndex < numFiles; fileIndex++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   620
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   621
        LZMA_fileClose(&archive->files[fileIndex]);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   622
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   623
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   624
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   625
    SzArDbExFree(&archive->db, SzFreePhysicsFS);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   626
    archive->stream.io->destroy(archive->stream.io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   627
    lzma_archive_exit(archive);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   628
} /* LZMA_closeArchive */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   629
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   630
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   631
static int LZMA_remove(PHYSFS_Dir *opaque, const char *name)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   632
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   633
    BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   634
} /* LZMA_remove */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   635
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   636
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   637
static int LZMA_mkdir(PHYSFS_Dir *opaque, const char *name)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   638
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   639
    BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   640
} /* LZMA_mkdir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   641
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   642
static int LZMA_stat(PHYSFS_Dir *opaque, const char *filename,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   643
                     int *exists, PHYSFS_Stat *stat)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   644
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   645
    const LZMAarchive *archive = (const LZMAarchive *) opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   646
    const LZMAfile *file = lzma_find_file(archive, filename);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   647
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   648
    *exists = (file != 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   649
    if (!file)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   650
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   651
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   652
    if(file->item->IsDirectory)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   653
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   654
        stat->filesize = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   655
        stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   656
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   657
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   658
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   659
        stat->filesize = (PHYSFS_sint64) file->item->Size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   660
        stat->filetype = PHYSFS_FILETYPE_REGULAR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   661
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   662
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   663
    /* !!! FIXME: the 0's should be -1's? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   664
    if (file->item->IsLastWriteTimeDefined)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   665
        stat->modtime = lzma_filetime_to_unix_timestamp(&file->item->LastWriteTime);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   666
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   667
        stat->modtime = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   668
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   669
    /* real create and accesstype are currently not in the lzma SDK */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   670
    stat->createtime = stat->modtime;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   671
    stat->accesstime = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   672
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   673
    stat->readonly = 1;  /* 7zips are always read only */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   674
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   675
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   676
} /* LZMA_stat */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   677
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   678
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   679
const PHYSFS_Archiver __PHYSFS_Archiver_LZMA =
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   680
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   681
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   682
        "7Z",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   683
        "LZMA (7zip) format",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   684
        "Dennis Schridde <devurandom@gmx.net>",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   685
        "http://icculus.org/physfs/",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   686
    },
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   687
    LZMA_openArchive,        /* openArchive() method    */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   688
    LZMA_enumerateFiles,     /* enumerateFiles() method */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   689
    LZMA_openRead,           /* openRead() method       */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   690
    LZMA_openWrite,          /* openWrite() method      */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   691
    LZMA_openAppend,         /* openAppend() method     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   692
    LZMA_remove,             /* remove() method         */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   693
    LZMA_mkdir,              /* mkdir() method          */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   694
    LZMA_closeArchive,       /* closeArchive() method   */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   695
    LZMA_stat                /* stat() method           */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   696
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   697
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   698
#endif  /* defined PHYSFS_SUPPORTS_7Z */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   699
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   700
/* end of lzma.c ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   701