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