Many improvements to lzma.c (thanks, Dennis!).

This commit is contained in:
Ryan C. Gordon 2008-01-23 04:57:47 +00:00
parent 4e457760a2
commit 66631578f8
1 changed files with 320 additions and 287 deletions

View File

@ -1,9 +1,9 @@
/* /*
* LZMA support routines for PhysicsFS. * LZMA support routines for PhysicsFS.
* *
* Please see the file LICENSE.txt in the source's root directory. * Please see the file lzma.txt in the lzma/ directory.
* *
* This file is written by Dennis Schridde, with some peeking at "7zMain.c" * This file was written by Dennis Schridde, with some peeking at "7zMain.c"
* by Igor Pavlov. * by Igor Pavlov.
*/ */
@ -11,56 +11,51 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include "physfs.h" #include "physfs.h"
#define __PHYSICSFS_INTERNAL__ #define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h" #include "physfs_internal.h"
#ifndef _LZMA_IN_CB #include "lzma/C/7zCrc.h"
#define _LZMA_IN_CB #include "lzma/C/Archive/7z/7zIn.h"
/* Use callback for input data */ #include "lzma/C/Archive/7z/7zExtract.h"
#endif
/* #define _LZMA_OUT_READ */
/* Use read function for output data */
#ifndef _LZMA_PROB32
#define _LZMA_PROB32
/* It can increase speed on some 32-bit CPUs,
but memory usage will be doubled in that case */
#endif
#ifndef _LZMA_SYSTEM_SIZE_T
#define _LZMA_SYSTEM_SIZE_T
/* Use system's size_t. You can use it to enable 64-bit sizes supporting */
#endif
#include "7zIn.h"
#include "7zCrc.h"
#include "7zExtract.h"
/* 7z internal from 7zIn.c */ /* 7z internal from 7zIn.c */
int TestSignatureCandidate(Byte *testBytes); extern int TestSignatureCandidate(Byte *testBytes);
typedef struct _CFileInStream #ifdef _LZMA_IN_CB
{ # define BUFFER_SIZE (1 << 12)
ISzInStream InStream; #endif /* _LZMA_IN_CB */
void *File;
} CFileInStream;
/* /*
* In LZMA the archive is splited in blocks, those are called folders * Carries filestream metadata through 7z
*/
typedef struct _FileInputStream
{
ISzAlloc allocImp; /* Allocation implementation, used by 7z */
ISzAlloc allocTempImp; /* Temporary allocation implementation, used by 7z */
ISzInStream inStream; /* Input stream with read callbacks, used by 7z */
void *file; /* Filehandle, used by read implementation */
#ifdef _LZMA_IN_CB
Byte buffer[BUFFER_SIZE]; /* Buffer, used by read implementation */
#endif /* _LZMA_IN_CB */
} FileInputStream;
/*
* In the 7z format archives are splited into blocks, those are called folders
* Set by LZMA_read() * Set by LZMA_read()
*/ */
typedef struct _LZMAfolder typedef struct _LZMAfolder
{ {
PHYSFS_uint8 *cache; /* Cached folder */
PHYSFS_uint32 size; /* Size of folder */
PHYSFS_uint32 index; /* Index of folder in archive */ PHYSFS_uint32 index; /* Index of folder in archive */
PHYSFS_uint32 references; /* Number of files using this block */ PHYSFS_uint32 references; /* Number of files using this block */
PHYSFS_uint8 *cache; /* Cached folder */
size_t size; /* Size of folder */
} LZMAfolder; } LZMAfolder;
/* /*
@ -69,25 +64,22 @@ typedef struct _LZMAfolder
*/ */
typedef struct _LZMAarchive typedef struct _LZMAarchive
{ {
struct _LZMAentry *firstEntry; /* Used for cleanup on shutdown */ struct _LZMAfile *files; /* Array of files, size == archive->db.Database.NumFiles */
struct _LZMAentry *lastEntry; LZMAfolder *folders; /* Array of folders, size == archive->db.Database.NumFolders */
LZMAfolder *folder; /* Array of folders */
CArchiveDatabaseEx db; /* For 7z: Database */ CArchiveDatabaseEx db; /* For 7z: Database */
CFileInStream stream; /* For 7z: Input file incl. read and seek callbacks */ FileInputStream stream; /* For 7z: Input file incl. read and seek callbacks */
} LZMAarchive; } LZMAarchive;
/* Set by LZMA_openRead(), except offset which is set by LZMA_read() */ /* Set by LZMA_openArchive(), except offset which is set by LZMA_read() */
typedef struct _LZMAentry typedef struct _LZMAfile
{ {
struct _LZMAentry *next; /* Used for cleanup on shutdown */ PHYSFS_uint32 index; /* Index of file in archive */
struct _LZMAentry *previous;
LZMAarchive *archive; /* Link to corresponding archive */ LZMAarchive *archive; /* Link to corresponding archive */
CFileItem *file; /* For 7z: File info, eg. name, size */ LZMAfolder *folder; /* Link to corresponding folder */
PHYSFS_uint32 fileIndex; /* Index of file in archive */ CFileItem *item; /* For 7z: File info, eg. name, size */
PHYSFS_uint32 folderIndex; /* Index of folder in archive */
size_t offset; /* Offset in folder */ size_t offset; /* Offset in folder */
PHYSFS_uint64 position; /* Current "virtual" position in file */ size_t position; /* Current "virtual" position in file */
} LZMAentry; } LZMAfile;
/* Memory management implementations to be passed to 7z */ /* Memory management implementations to be passed to 7z */
@ -109,30 +101,37 @@ static void SzFreePhysicsFS(void *address)
#ifdef _LZMA_IN_CB #ifdef _LZMA_IN_CB
#define kBufferSize (1 << 12) /*
static Byte g_Buffer[kBufferSize]; /* !!! FIXME: not thread safe! */ * Read implementation, to be passed to 7z
* WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
*/
SZ_RESULT SzFileReadImp(void *object, void **buffer, size_t maxReqSize, SZ_RESULT SzFileReadImp(void *object, void **buffer, size_t maxReqSize,
size_t *processedSize) size_t *processedSize)
{ {
CFileInStream *s = (CFileInStream *)object; FileInputStream *s = (FileInputStream *)(object - offsetof(FileInputStream, inStream)); // HACK!
PHYSFS_sint64 processedSizeLoc; PHYSFS_sint64 processedSizeLoc = 0;
if (maxReqSize > kBufferSize)
maxReqSize = kBufferSize; if (maxReqSize > BUFFER_SIZE)
processedSizeLoc = __PHYSFS_platformRead(s->File, g_Buffer, 1, maxReqSize); maxReqSize = BUFFER_SIZE;
*buffer = g_Buffer; processedSizeLoc = __PHYSFS_platformRead(s->file, s->buffer, 1, maxReqSize);
*buffer = s->buffer;
if (processedSize != NULL) if (processedSize != NULL)
*processedSize = (size_t) processedSizeLoc; *processedSize = (size_t) processedSizeLoc;
return SZ_OK; return SZ_OK;
} /* SzFileReadImp */ } /* SzFileReadImp */
#else #else
/*
* Read implementation, to be passed to 7z
* WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
*/
SZ_RESULT SzFileReadImp(void *object, void *buffer, size_t size, SZ_RESULT SzFileReadImp(void *object, void *buffer, size_t size,
size_t *processedSize) size_t *processedSize)
{ {
CFileInStream *s = (CFileInStream *)object; FileInputStream *s = (FileInputStream *)(object - offsetof(FileInputStream, inStream)); // HACK!
size_t processedSizeLoc = __PHYSFS_platformRead(s->File, buffer, 1, size); size_t processedSizeLoc = __PHYSFS_platformRead(s->file, buffer, 1, size);
if (processedSize != 0) if (processedSize != 0)
*processedSize = processedSizeLoc; *processedSize = processedSizeLoc;
return SZ_OK; return SZ_OK;
@ -140,87 +139,153 @@ SZ_RESULT SzFileReadImp(void *object, void *buffer, size_t size,
#endif #endif
/*
* Seek implementation, to be passed to 7z
* WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
*/
SZ_RESULT SzFileSeekImp(void *object, CFileSize pos) SZ_RESULT SzFileSeekImp(void *object, CFileSize pos)
{ {
CFileInStream *s = (CFileInStream *) object; FileInputStream *s = (FileInputStream *)(object - offsetof(FileInputStream, inStream)); // HACK!
if (__PHYSFS_platformSeek(s->File, (PHYSFS_uint64) pos)) if (__PHYSFS_platformSeek(s->file, (PHYSFS_uint64) pos))
return SZ_OK; return SZ_OK;
return SZE_FAIL; return SZE_FAIL;
} /* SzFileSeekImp */ } /* SzFileSeekImp */
/* /*
* Find entry 'name' in 'archive' and report the 'index' back * Translate Microsoft FILETIME (used by 7zip) into UNIX timestamp
*/ */
static int lzma_find_entry(LZMAarchive *archive, const char *name, static PHYSFS_sint64 lzma_filetime_to_unix_timestamp(CArchiveFileTime *ft)
PHYSFS_uint32 *index)
{ {
for (*index = 0; *index < archive->db.Database.NumFiles; (*index)++) /* MS counts in nanoseconds ... */
{ const PHYSFS_uint64 FILETIME_NANOTICKS_PER_SECOND = __PHYSFS_UI64(10000000);
if (strcmp(archive->db.Database.Files[*index].Name, name) == 0) /* MS likes to count seconds since 01.01.1601 ... */
return 1; const PHYSFS_uint64 FILETIME_UNIX_DIFF = __PHYSFS_UI64(11644473600);
} /* for */
BAIL_MACRO(ERR_NO_SUCH_FILE, 0); PHYSFS_uint64 filetime = ft->Low | ((PHYSFS_uint64)ft->High << 32);
} /* lzma_find_entry */ return filetime/FILETIME_NANOTICKS_PER_SECOND - FILETIME_UNIX_DIFF;
} /* lzma_filetime_to_unix_timestamp */
/* /*
* Report the first file index of a directory * Compare a file with a given name, C89 stdlib variant
* Used for sorting
*/ */
static PHYSFS_sint32 lzma_find_start_of_dir(LZMAarchive *archive, static int lzma_file_cmp_stdlib(const void *key, const void *object)
const char *path,
int stop_on_first_find)
{ {
PHYSFS_sint32 lo = 0; const char *name = (const char *) key;
PHYSFS_sint32 hi = (PHYSFS_sint32) (archive->db.Database.NumFiles - 1); LZMAfile *file = (LZMAfile *) object;
PHYSFS_sint32 middle; return(strcmp(name, file->item->Name));
PHYSFS_uint32 dlen = strlen(path); } /* lzma_file_cmp_posix */
PHYSFS_sint32 retval = -1;
const char *name;
int rc;
if (*path == '\0') /* root dir? */
return(0);
if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */ /*
dlen--; * Compare two files with each other based on the name
* Used for sorting
*/
static int lzma_file_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
{
LZMAfile *files = (LZMAfile *) _a;
return(strcmp(files[one].item->Name, files[two].item->Name));
} /* lzma_file_cmp */
while (lo <= hi)
/*
* Swap two entries in the file array
*/
static void lzma_file_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
{
LZMAfile tmp;
LZMAfile *first = &(((LZMAfile *) _a)[one]);
LZMAfile *second = &(((LZMAfile *) _a)[two]);
memcpy(&tmp, first, sizeof (LZMAfile));
memcpy(first, second, sizeof (LZMAfile));
memcpy(second, &tmp, sizeof (LZMAfile));
} /* lzma_file_swap */
/*
* Find entry 'name' in 'archive'
*/
static LZMAfile * lzma_find_file(LZMAarchive *archive, const char *name)
{
LZMAfile *file = bsearch(name, archive->files, archive->db.Database.NumFiles, sizeof(*archive->files), lzma_file_cmp_stdlib); // FIXME: Should become __PHYSFS_search!!!
BAIL_IF_MACRO(file == NULL, ERR_NO_SUCH_FILE, NULL);
return(file);
} /* lzma_find_file */
/*
* Load metadata for the file at given index
*/
static int lzma_file_init(LZMAarchive *archive, PHYSFS_uint32 fileIndex)
{
LZMAfile *file = &archive->files[fileIndex];
PHYSFS_uint32 folderIndex = archive->db.FileIndexToFolderIndexMap[fileIndex];
file->index = fileIndex; // Store index into 7z array, since we sort our own.
file->archive = archive;
file->folder = (folderIndex != (PHYSFS_uint32)-1 ? &archive->folders[folderIndex] : NULL); // Directories don't have a folder (they contain no own data...)
file->item = &archive->db.Database.Files[fileIndex]; // Holds crucial data and is often referenced -> Store link
file->position = 0;
file->offset = 0; /* Offset will be set by LZMA_read() */
return(1);
} /* lzma_load_file */
/*
* Load metadata for all files
*/
static int lzma_files_init(LZMAarchive *archive)
{
PHYSFS_uint32 fileIndex = 0, numFiles = archive->db.Database.NumFiles;
for (fileIndex = 0; fileIndex < numFiles; fileIndex++ )
{ {
middle = lo + ((hi - lo) / 2); if (!lzma_file_init(archive, fileIndex))
name = archive->db.Database.Files[middle].Name;
rc = strncmp(path, name, dlen);
if (rc == 0)
{ {
char ch = name[dlen]; return(0); // FALSE on failure
if ('/' < ch) /* make sure this isn't just a substr match. */ }
rc = -1; } /* for */
else if ('/' > ch)
rc = 1;
else
{
if (stop_on_first_find) /* Just checking dir's existance? */
return(middle);
if (name[dlen + 1] == '\0') /* Skip initial dir entry. */ __PHYSFS_sort(archive->files, numFiles, lzma_file_cmp, lzma_file_swap);
return(middle + 1);
/* there might be more entries earlier in the list. */ return(1);
retval = middle; } /* lzma_load_files */
hi = middle - 1;
} /* else */
} /* if */
if (rc > 0)
lo = middle + 1;
else
hi = middle - 1;
} /* while */
return(retval); /*
} /* lzma_find_start_of_dir */ * Initialise specified archive
*/
static void lzma_archive_init(LZMAarchive *archive)
{
memset(archive, 0, sizeof(*archive));
/* Prepare callbacks for 7z */
archive->stream.inStream.Read = SzFileReadImp;
archive->stream.inStream.Seek = SzFileSeekImp;
archive->stream.allocImp.Alloc = SzAllocPhysicsFS;
archive->stream.allocImp.Free = SzFreePhysicsFS;
archive->stream.allocTempImp.Alloc = SzAllocPhysicsFS;
archive->stream.allocTempImp.Free = SzFreePhysicsFS;
}
/*
* Deinitialise archive
*/
static void lzma_archive_exit(LZMAarchive *archive)
{
/* Free arrays */
allocator.Free(archive->folders);
allocator.Free(archive->files);
allocator.Free(archive);
}
/* /*
* Wrap all 7z calls in this, so the physfs error state is set appropriately. * Wrap all 7z calls in this, so the physfs error state is set appropriately.
@ -260,14 +325,11 @@ static int lzma_err(SZ_RESULT rc)
static PHYSFS_sint64 LZMA_read(fvoid *opaque, void *outBuffer, static PHYSFS_sint64 LZMA_read(fvoid *opaque, void *outBuffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount) PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{ {
LZMAentry *entry = (LZMAentry *) opaque; LZMAfile *file = (LZMAfile *) opaque;
PHYSFS_sint64 wantedSize = objSize*objCount; size_t wantedSize = objSize*objCount;
PHYSFS_sint64 remainingSize = entry->file->Size - entry->position; size_t remainingSize = file->item->Size - file->position;
size_t fileSize = 0;
size_t fileSize;
ISzAlloc allocImp;
ISzAlloc allocTempImp;
BAIL_IF_MACRO(wantedSize == 0, NULL, 0); /* quick rejection. */ BAIL_IF_MACRO(wantedSize == 0, NULL, 0); /* quick rejection. */
BAIL_IF_MACRO(remainingSize == 0, ERR_PAST_EOF, 0); BAIL_IF_MACRO(remainingSize == 0, ERR_PAST_EOF, 0);
@ -280,44 +342,36 @@ static PHYSFS_sint64 LZMA_read(fvoid *opaque, void *outBuffer,
__PHYSFS_setError(ERR_PAST_EOF); /* this is always true here. */ __PHYSFS_setError(ERR_PAST_EOF); /* this is always true here. */
} /* if */ } /* if */
/* Prepare callbacks for 7z */
allocImp.Alloc = SzAllocPhysicsFS;
allocImp.Free = SzFreePhysicsFS;
allocTempImp.Alloc = SzAllocPhysicsFS;
allocTempImp.Free = SzFreePhysicsFS;
/* Only decompress the folder if it is not allready cached */ /* Only decompress the folder if it is not allready cached */
if (entry->archive->folder[entry->folderIndex].cache == NULL) if (file->folder->cache == NULL)
{ {
size_t tmpsize = entry->archive->folder[entry->folderIndex].size;
int rc = lzma_err(SzExtract( int rc = lzma_err(SzExtract(
&entry->archive->stream.InStream, /* compressed data */ &file->archive->stream.inStream, /* compressed data */
&entry->archive->db, &file->archive->db, /* 7z's database, containing everything */
entry->fileIndex, file->index, /* Index into database arrays */
/* Index of cached folder, will be changed by SzExtract */ /* Index of cached folder, will be changed by SzExtract */
&entry->archive->folder[entry->folderIndex].index, &file->folder->index,
/* Cache for decompressed folder, allocated/freed by SzExtract */ /* Cache for decompressed folder, allocated/freed by SzExtract */
&entry->archive->folder[entry->folderIndex].cache, &file->folder->cache,
/* Size of cache, will be changed by SzExtract */ /* Size of cache, will be changed by SzExtract */
&tmpsize, &file->folder->size,
/* Offset of this file inside the cache, set by SzExtract */ /* Offset of this file inside the cache, set by SzExtract */
&entry->offset, &file->offset,
&fileSize, /* Size of this file */ &fileSize, /* Size of this file */
&allocImp, &file->archive->stream.allocImp,
&allocTempImp)); &file->archive->stream.allocTempImp));
entry->archive->folder[entry->folderIndex].size = tmpsize;
if (rc != SZ_OK) if (rc != SZ_OK)
return -1; return -1;
} /* if */ } /* if */
/* Copy wanted bytes over from cache to outBuffer */ /* Copy wanted bytes over from cache to outBuffer */
memcpy(outBuffer, memcpy(outBuffer,
(entry->archive->folder[entry->folderIndex].cache + (file->folder->cache +
entry->offset + entry->position), file->offset + file->position),
(size_t) wantedSize); wantedSize);
entry->position += wantedSize; file->position += wantedSize; /* Increase virtual position */
return objCount; return objCount;
} /* LZMA_read */ } /* LZMA_read */
@ -331,63 +385,54 @@ static PHYSFS_sint64 LZMA_write(fvoid *opaque, const void *buf,
static int LZMA_eof(fvoid *opaque) static int LZMA_eof(fvoid *opaque)
{ {
LZMAentry *entry = (LZMAentry *) opaque; LZMAfile *file = (LZMAfile *) opaque;
return (entry->position >= entry->file->Size); return (file->position >= file->item->Size);
} /* LZMA_eof */ } /* LZMA_eof */
static PHYSFS_sint64 LZMA_tell(fvoid *opaque) static PHYSFS_sint64 LZMA_tell(fvoid *opaque)
{ {
LZMAentry *entry = (LZMAentry *) opaque; LZMAfile *file = (LZMAfile *) opaque;
return (entry->position); return (file->position);
} /* LZMA_tell */ } /* LZMA_tell */
static int LZMA_seek(fvoid *opaque, PHYSFS_uint64 offset) static int LZMA_seek(fvoid *opaque, PHYSFS_uint64 offset)
{ {
LZMAentry *entry = (LZMAentry *) opaque; LZMAfile *file = (LZMAfile *) opaque;
BAIL_IF_MACRO(offset < 0, ERR_SEEK_OUT_OF_RANGE, 0); BAIL_IF_MACRO(offset < 0, ERR_SEEK_OUT_OF_RANGE, 0);
BAIL_IF_MACRO(offset > entry->file->Size, ERR_PAST_EOF, 0); BAIL_IF_MACRO(offset > file->item->Size, ERR_PAST_EOF, 0);
file->position = offset; /* We only use a virtual position... */
entry->position = offset;
return 1; return 1;
} /* LZMA_seek */ } /* LZMA_seek */
static PHYSFS_sint64 LZMA_fileLength(fvoid *opaque) static PHYSFS_sint64 LZMA_fileLength(fvoid *opaque)
{ {
LZMAentry *entry = (LZMAentry *) opaque; LZMAfile *file = (LZMAfile *) opaque;
return (entry->file->Size); return (file->item->Size);
} /* LZMA_fileLength */ } /* LZMA_fileLength */
static int LZMA_fileClose(fvoid *opaque) static int LZMA_fileClose(fvoid *opaque)
{ {
LZMAentry *entry = (LZMAentry *) opaque; LZMAfile *file = (LZMAfile *) opaque;
/* Fix archive */ BAIL_IF_MACRO(file->folder == NULL, ERR_NOT_A_FILE, 0);
if (entry == entry->archive->firstEntry)
entry->archive->firstEntry = entry->next;
if (entry == entry->archive->lastEntry)
entry->archive->lastEntry = entry->previous;
/* Fix neighbours */ /* Only decrease refcount if someone actually requested this file... Prevents from overflows and close-on-open... */
if (entry->previous != NULL) if (file->folder->references > 0)
entry->previous->next = entry->next; file->folder->references--;
if (entry->next != NULL) if (file->folder->references == 0)
entry->next->previous = entry->previous;
entry->archive->folder[entry->folderIndex].references--;
if (entry->archive->folder[entry->folderIndex].references == 0)
{ {
allocator.Free(entry->archive->folder[entry->folderIndex].cache); /* Free the cache which might have been allocated by LZMA_read() */
entry->archive->folder[entry->folderIndex].cache = NULL; allocator.Free(file->folder->cache);
file->folder->cache = NULL;
} }
allocator.Free(entry);
entry = NULL;
return(1); return(1);
} /* LZMA_fileClose */ } /* LZMA_fileClose */
@ -395,7 +440,6 @@ static int LZMA_fileClose(fvoid *opaque)
static int LZMA_isArchive(const char *filename, int forWriting) static int LZMA_isArchive(const char *filename, int forWriting)
{ {
PHYSFS_uint8 sig[k7zSignatureSize]; PHYSFS_uint8 sig[k7zSignatureSize];
PHYSFS_uint8 res;
void *in; void *in;
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0); BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
@ -403,24 +447,24 @@ static int LZMA_isArchive(const char *filename, int forWriting)
in = __PHYSFS_platformOpenRead(filename); in = __PHYSFS_platformOpenRead(filename);
BAIL_IF_MACRO(in == NULL, NULL, 0); BAIL_IF_MACRO(in == NULL, NULL, 0);
/* Read signature bytes */
if (__PHYSFS_platformRead(in, sig, k7zSignatureSize, 1) != 1) if (__PHYSFS_platformRead(in, sig, k7zSignatureSize, 1) != 1)
{
__PHYSFS_platformClose(in); // Don't forget to close the file before returning...
BAIL_MACRO(NULL, 0); BAIL_MACRO(NULL, 0);
}
/* Test whether sig is the 7z signature */
res = TestSignatureCandidate(sig);
__PHYSFS_platformClose(in); __PHYSFS_platformClose(in);
return res; /* Test whether sig is the 7z signature */
return(TestSignatureCandidate(sig));
} /* LZMA_isArchive */ } /* LZMA_isArchive */
static void *LZMA_openArchive(const char *name, int forWriting) static void *LZMA_openArchive(const char *name, int forWriting)
{ {
PHYSFS_uint64 len; size_t len = 0;
LZMAarchive *archive = NULL; LZMAarchive *archive = NULL;
ISzAlloc allocImp;
ISzAlloc allocTempImp;
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, NULL); BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, NULL);
BAIL_IF_MACRO(!LZMA_isArchive(name,forWriting), ERR_UNSUPPORTED_ARCHIVE, 0); BAIL_IF_MACRO(!LZMA_isArchive(name,forWriting), ERR_UNSUPPORTED_ARCHIVE, 0);
@ -428,44 +472,67 @@ static void *LZMA_openArchive(const char *name, int forWriting)
archive = (LZMAarchive *) allocator.Malloc(sizeof (LZMAarchive)); archive = (LZMAarchive *) allocator.Malloc(sizeof (LZMAarchive));
BAIL_IF_MACRO(archive == NULL, ERR_OUT_OF_MEMORY, NULL); BAIL_IF_MACRO(archive == NULL, ERR_OUT_OF_MEMORY, NULL);
archive->firstEntry = NULL; lzma_archive_init(archive);
archive->lastEntry = NULL;
if ((archive->stream.File = __PHYSFS_platformOpenRead(name)) == NULL) if ( (archive->stream.file = __PHYSFS_platformOpenRead(name)) == NULL )
{ {
allocator.Free(archive); __PHYSFS_platformClose(archive->stream.file);
return NULL; lzma_archive_exit(archive);
} /* if */ return(NULL); // Error is set by platformOpenRead!
}
/* Prepare structs for 7z */ CrcGenerateTable();
archive->stream.InStream.Read = SzFileReadImp;
archive->stream.InStream.Seek = SzFileSeekImp;
allocImp.Alloc = SzAllocPhysicsFS;
allocImp.Free = SzFreePhysicsFS;
allocTempImp.Alloc = SzAllocPhysicsFS;
allocTempImp.Free = SzFreePhysicsFS;
InitCrcTable();
SzArDbExInit(&archive->db); SzArDbExInit(&archive->db);
if (lzma_err(SzArchiveOpen(&archive->stream.InStream, &archive->db, if (lzma_err(SzArchiveOpen(&archive->stream.inStream,
&allocImp, &allocTempImp)) != SZ_OK) &archive->db,
&archive->stream.allocImp,
&archive->stream.allocTempImp)) != SZ_OK)
{ {
__PHYSFS_platformClose(archive->stream.File); SzArDbExFree(&archive->db, SzFreePhysicsFS);
allocator.Free(archive); __PHYSFS_platformClose(archive->stream.file);
return NULL; lzma_archive_exit(archive);
return NULL; // Error is set by lzma_err!
} /* if */ } /* if */
len = archive->db.Database.NumFiles * sizeof (LZMAfile);
archive->files = (LZMAfile *) allocator.Malloc(len);
if (archive->files == NULL)
{
SzArDbExFree(&archive->db, SzFreePhysicsFS);
__PHYSFS_platformClose(archive->stream.file);
lzma_archive_exit(archive);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
}
/*
* Init with 0 so we know when a folder is already cached
* Values will be set by LZMA_openRead()
*/
memset(archive->files, 0, len);
len = archive->db.Database.NumFolders * sizeof (LZMAfolder); len = archive->db.Database.NumFolders * sizeof (LZMAfolder);
archive->folder = (LZMAfolder *) allocator.Malloc(len); archive->folders = (LZMAfolder *) allocator.Malloc(len);
BAIL_IF_MACRO(archive->folder == NULL, ERR_OUT_OF_MEMORY, NULL); if (archive->folders == NULL)
{
SzArDbExFree(&archive->db, SzFreePhysicsFS);
__PHYSFS_platformClose(archive->stream.file);
lzma_archive_exit(archive);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
}
/* /*
* Init with 0 so we know when a folder is already cached * Init with 0 so we know when a folder is already cached
* Values will be set by LZMA_read() * Values will be set by LZMA_read()
*/ */
memset(archive->folder, 0, (size_t) len); memset(archive->folders, 0, len);
if(!lzma_files_init(archive))
{
SzArDbExFree(&archive->db, SzFreePhysicsFS);
__PHYSFS_platformClose(archive->stream.file);
lzma_archive_exit(archive);
BAIL_MACRO(ERR_UNKNOWN_ERROR, NULL);
}
return(archive); return(archive);
} /* LZMA_openArchive */ } /* LZMA_openArchive */
@ -476,14 +543,14 @@ static void *LZMA_openArchive(const char *name, int forWriting)
* away the allocated stack space... * away the allocated stack space...
*/ */
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata, static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
const char *odir, const char *str, PHYSFS_sint32 ln) const char *odir, const char *str, size_t flen)
{ {
char *newstr = __PHYSFS_smallAlloc(ln + 1); char *newstr = __PHYSFS_smallAlloc(flen + 1);
if (newstr == NULL) if (newstr == NULL)
return; return;
memcpy(newstr, str, ln); memcpy(newstr, str, flen);
newstr[ln] = '\0'; newstr[flen] = '\0';
cb(callbackdata, odir, newstr); cb(callbackdata, odir, newstr);
__PHYSFS_smallFree(newstr); __PHYSFS_smallFree(newstr);
} /* doEnumCallback */ } /* doEnumCallback */
@ -493,53 +560,40 @@ static void LZMA_enumerateFiles(dvoid *opaque, const char *dname,
int omitSymLinks, PHYSFS_EnumFilesCallback cb, int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata) const char *origdir, void *callbackdata)
{ {
size_t dlen = strlen(dname),
dlen_inc = dlen + ((dlen > 0) ? 1 : 0);
LZMAarchive *archive = (LZMAarchive *) opaque; LZMAarchive *archive = (LZMAarchive *) opaque;
PHYSFS_sint32 dlen; LZMAfile *file = (dlen ? lzma_find_file(archive, dname) + 1 : archive->files),
PHYSFS_sint32 dlen_inc; *lastFile = &archive->files[archive->db.Database.NumFiles];
PHYSFS_sint32 max;
PHYSFS_sint32 i;
i = lzma_find_start_of_dir(archive, dname, 0); BAIL_IF_MACRO(file == NULL, ERR_NO_SUCH_FILE, );
if (i == -1) /* no such directory. */
return;
dlen = strlen(dname); while (file < lastFile)
if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
dlen--;
dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
max = (PHYSFS_sint32) archive->db.Database.NumFiles;
while (i < max)
{ {
char *add; const char * fname = file->item->Name;
char *ptr; const char * dirNameEnd = fname + dlen_inc;
PHYSFS_sint32 ln;
char *e = archive->db.Database.Files[i].Name;
if ((dlen) && ((strncmp(e, dname, dlen)) || (e[dlen] != '/')))
break; /* past end of this dir; we're done. */
add = e + dlen_inc; if (strncmp(dname, fname, dlen) != 0) /* Stop after mismatch, archive->files is sorted */
ptr = strchr(add, '/'); break;
ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
doEnumCallback(cb, callbackdata, origdir, add, ln);
ln += dlen_inc; /* point past entry to children... */
/* increment counter and skip children of subdirs... */ if (strchr(dirNameEnd, '/')) /* Skip subdirs */
while ((++i < max) && (ptr != NULL))
{ {
char *e_new = archive->db.Database.Files[i].Name; file++;
if ((strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/')) continue;
break; }
} /* while */
} /* while */ /* Do the actual callback... */
doEnumCallback(cb, callbackdata, origdir, dirNameEnd, strlen(dirNameEnd));
file++;
}
} /* LZMA_enumerateFiles */ } /* LZMA_enumerateFiles */
static int LZMA_exists(dvoid *opaque, const char *name) static int LZMA_exists(dvoid *opaque, const char *name)
{ {
LZMAarchive *archive = (LZMAarchive *) opaque; LZMAarchive *archive = (LZMAarchive *) opaque;
PHYSFS_uint32 index = 0; return(lzma_find_file(archive, name) != NULL);
return(lzma_find_entry(archive, name, &index));
} /* LZMA_exists */ } /* LZMA_exists */
@ -547,19 +601,26 @@ static PHYSFS_sint64 LZMA_getLastModTime(dvoid *opaque,
const char *name, const char *name,
int *fileExists) int *fileExists)
{ {
/* !!! FIXME: Lacking support in the LZMA C SDK. */ LZMAarchive *archive = (LZMAarchive *) opaque;
BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1); LZMAfile *file = lzma_find_file(archive, name);
*fileExists = (file != NULL);
BAIL_IF_MACRO(file == NULL, NULL, -1);
BAIL_IF_MACRO(!file->item->IsLastWriteTimeDefined, NULL, -1); // write-time may not be defined for every file
return(lzma_filetime_to_unix_timestamp(&file->item->LastWriteTime));
} /* LZMA_getLastModTime */ } /* LZMA_getLastModTime */
static int LZMA_isDirectory(dvoid *opaque, const char *name, int *fileExists) static int LZMA_isDirectory(dvoid *opaque, const char *name, int *fileExists)
{ {
LZMAarchive *archive = (LZMAarchive *) opaque; LZMAarchive *archive = (LZMAarchive *) opaque;
PHYSFS_uint32 index = 0; LZMAfile *file = lzma_find_file(archive, name);
*fileExists = lzma_find_entry(archive, name, &index); *fileExists = (file != NULL);
return(archive->db.Database.Files[index].IsDirectory); return(file->item->IsDirectory);
} /* LZMA_isDirectory */ } /* LZMA_isDirectory */
@ -572,37 +633,15 @@ static int LZMA_isSymLink(dvoid *opaque, const char *name, int *fileExists)
static fvoid *LZMA_openRead(dvoid *opaque, const char *name, int *fileExists) static fvoid *LZMA_openRead(dvoid *opaque, const char *name, int *fileExists)
{ {
LZMAarchive *archive = (LZMAarchive *) opaque; LZMAarchive *archive = (LZMAarchive *) opaque;
LZMAentry *entry = NULL; LZMAfile *file = lzma_find_file(archive, name);
PHYSFS_uint32 fileIndex = 0;
PHYSFS_uint32 folderIndex = 0;
*fileExists = lzma_find_entry(archive, name, &fileIndex); *fileExists = (file != NULL);
BAIL_IF_MACRO(!*fileExists, ERR_NO_SUCH_FILE, NULL); BAIL_IF_MACRO(file == NULL, ERR_NO_SUCH_FILE, NULL);
BAIL_IF_MACRO(file->folder == NULL, ERR_NOT_A_FILE, NULL);
folderIndex = archive->db.FileIndexToFolderIndexMap[fileIndex]; file->folder->references++; // Increase refcount for automatic cleanup...
BAIL_IF_MACRO(folderIndex == (PHYSFS_uint32)-1, ERR_UNKNOWN_ERROR, NULL);
entry = (LZMAentry *) allocator.Malloc(sizeof (LZMAentry)); return(file);
BAIL_IF_MACRO(entry == NULL, ERR_OUT_OF_MEMORY, NULL);
entry->fileIndex = fileIndex;
entry->folderIndex = folderIndex;
entry->archive = archive;
entry->file = archive->db.Database.Files + entry->fileIndex;
entry->offset = 0; /* Offset will be set by LZMA_read() */
entry->position = 0;
archive->folder[folderIndex].references++;
entry->next = NULL;
entry->previous = entry->archive->lastEntry;
if (entry->previous != NULL)
entry->previous->next = entry;
entry->archive->lastEntry = entry;
if (entry->archive->firstEntry == NULL)
entry->archive->firstEntry = entry;
return(entry);
} /* LZMA_openRead */ } /* LZMA_openRead */
@ -621,22 +660,16 @@ static fvoid *LZMA_openAppend(dvoid *opaque, const char *filename)
static void LZMA_dirClose(dvoid *opaque) static void LZMA_dirClose(dvoid *opaque)
{ {
LZMAarchive *archive = (LZMAarchive *) opaque; LZMAarchive *archive = (LZMAarchive *) opaque;
LZMAentry *entry = archive->firstEntry; PHYSFS_uint32 fileIndex = 0, numFiles = archive->db.Database.NumFiles;
LZMAentry *tmpEntry = entry;
while (entry != NULL) for (fileIndex = 0; fileIndex < numFiles; fileIndex++)
{ {
tmpEntry = entry->next; LZMA_fileClose(&archive->files[fileIndex]);
LZMA_fileClose(entry); } /* for */
entry = tmpEntry;
} /* while */
SzArDbExFree(&archive->db, SzFreePhysicsFS); SzArDbExFree(&archive->db, SzFreePhysicsFS);
__PHYSFS_platformClose(archive->stream.File); __PHYSFS_platformClose(archive->stream.file);
lzma_archive_exit(archive);
/* Free the cache which might have been allocated by LZMA_read() */
allocator.Free(archive->folder);
allocator.Free(archive);
} /* LZMA_dirClose */ } /* LZMA_dirClose */