|
@@ -5,10 +5,8 @@
|
|
|
* formats that can just hand back a list of files and the offsets of their
|
|
* formats that can just hand back a list of files and the offsets of their
|
|
|
* uncompressed data. There are an alarming number of formats like this.
|
|
* uncompressed data. There are an alarming number of formats like this.
|
|
|
*
|
|
*
|
|
|
- * RULES: Archive entries must be uncompressed, must not have separate subdir
|
|
|
|
|
- * entries (but can have subdirs), must be case insensitive LOW ASCII
|
|
|
|
|
- * filenames <= 64 bytes. No symlinks, etc. We can relax some of these rules
|
|
|
|
|
- * as necessary.
|
|
|
|
|
|
|
+ * RULES: Archive entries must be uncompressed. Dirs and files allowed, but no
|
|
|
|
|
+ * symlinks, etc. We can relax some of these rules as necessary.
|
|
|
*
|
|
*
|
|
|
* Please see the file LICENSE.txt in the source's root directory.
|
|
* Please see the file LICENSE.txt in the source's root directory.
|
|
|
*
|
|
*
|
|
@@ -20,11 +18,16 @@
|
|
|
|
|
|
|
|
typedef struct
|
|
typedef struct
|
|
|
{
|
|
{
|
|
|
|
|
+ __PHYSFS_DirTree tree;
|
|
|
PHYSFS_Io *io;
|
|
PHYSFS_Io *io;
|
|
|
- PHYSFS_uint32 entryCount;
|
|
|
|
|
- UNPKentry *entries;
|
|
|
|
|
} UNPKinfo;
|
|
} UNPKinfo;
|
|
|
|
|
|
|
|
|
|
+typedef struct
|
|
|
|
|
+{
|
|
|
|
|
+ __PHYSFS_DirTreeEntry tree;
|
|
|
|
|
+ PHYSFS_uint64 startPos;
|
|
|
|
|
+ PHYSFS_uint64 size;
|
|
|
|
|
+} UNPKentry;
|
|
|
|
|
|
|
|
typedef struct
|
|
typedef struct
|
|
|
{
|
|
{
|
|
@@ -37,9 +40,15 @@ typedef struct
|
|
|
void UNPK_closeArchive(void *opaque)
|
|
void UNPK_closeArchive(void *opaque)
|
|
|
{
|
|
{
|
|
|
UNPKinfo *info = ((UNPKinfo *) opaque);
|
|
UNPKinfo *info = ((UNPKinfo *) opaque);
|
|
|
- info->io->destroy(info->io);
|
|
|
|
|
- allocator.Free(info->entries);
|
|
|
|
|
- allocator.Free(info);
|
|
|
|
|
|
|
+ if (info)
|
|
|
|
|
+ {
|
|
|
|
|
+ __PHYSFS_DirTreeDeinit(&info->tree);
|
|
|
|
|
+
|
|
|
|
|
+ if (info->io)
|
|
|
|
|
+ info->io->destroy(info->io);
|
|
|
|
|
+
|
|
|
|
|
+ allocator.Free(info);
|
|
|
|
|
+ } /* if */
|
|
|
} /* UNPK_closeArchive */
|
|
} /* UNPK_closeArchive */
|
|
|
|
|
|
|
|
|
|
|
|
@@ -145,202 +154,9 @@ static const PHYSFS_Io UNPK_Io =
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
-static int entryCmp(void *_a, size_t one, size_t two)
|
|
|
|
|
-{
|
|
|
|
|
- if (one != two)
|
|
|
|
|
- {
|
|
|
|
|
- const UNPKentry *a = (const UNPKentry *) _a;
|
|
|
|
|
- return __PHYSFS_stricmpASCII(a[one].name, a[two].name);
|
|
|
|
|
- } /* if */
|
|
|
|
|
-
|
|
|
|
|
- return 0;
|
|
|
|
|
-} /* entryCmp */
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-static void entrySwap(void *_a, size_t one, size_t two)
|
|
|
|
|
-{
|
|
|
|
|
- if (one != two)
|
|
|
|
|
- {
|
|
|
|
|
- UNPKentry tmp;
|
|
|
|
|
- UNPKentry *first = &(((UNPKentry *) _a)[one]);
|
|
|
|
|
- UNPKentry *second = &(((UNPKentry *) _a)[two]);
|
|
|
|
|
- memcpy(&tmp, first, sizeof (UNPKentry));
|
|
|
|
|
- memcpy(first, second, sizeof (UNPKentry));
|
|
|
|
|
- memcpy(second, &tmp, sizeof (UNPKentry));
|
|
|
|
|
- } /* if */
|
|
|
|
|
-} /* entrySwap */
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-static PHYSFS_sint32 findStartOfDir(UNPKinfo *info, const char *path,
|
|
|
|
|
- int stop_on_first_find)
|
|
|
|
|
-{
|
|
|
|
|
- PHYSFS_sint32 lo = 0;
|
|
|
|
|
- PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
|
|
|
|
|
- PHYSFS_sint32 middle;
|
|
|
|
|
- PHYSFS_uint32 dlen = (PHYSFS_uint32) strlen(path);
|
|
|
|
|
- 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--;
|
|
|
|
|
-
|
|
|
|
|
- while (lo <= hi)
|
|
|
|
|
- {
|
|
|
|
|
- middle = lo + ((hi - lo) / 2);
|
|
|
|
|
- name = info->entries[middle].name;
|
|
|
|
|
- rc = __PHYSFS_strnicmpASCII(path, name, dlen);
|
|
|
|
|
- if (rc == 0)
|
|
|
|
|
- {
|
|
|
|
|
- char ch = name[dlen];
|
|
|
|
|
- if (ch < '/') /* make sure this isn't just a substr match. */
|
|
|
|
|
- rc = -1;
|
|
|
|
|
- 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. */
|
|
|
|
|
- return (middle + 1);
|
|
|
|
|
-
|
|
|
|
|
- /* there might be more entries earlier in the list. */
|
|
|
|
|
- retval = middle;
|
|
|
|
|
- hi = middle - 1;
|
|
|
|
|
- } /* else */
|
|
|
|
|
- } /* if */
|
|
|
|
|
-
|
|
|
|
|
- if (rc > 0)
|
|
|
|
|
- lo = middle + 1;
|
|
|
|
|
- else
|
|
|
|
|
- hi = middle - 1;
|
|
|
|
|
- } /* while */
|
|
|
|
|
-
|
|
|
|
|
- return retval;
|
|
|
|
|
-} /* findStartOfDir */
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-/*
|
|
|
|
|
- * Moved to seperate function so we can use alloca then immediately throw
|
|
|
|
|
- * away the allocated stack space...
|
|
|
|
|
- */
|
|
|
|
|
-static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
|
|
|
|
|
- const char *odir, const char *str, PHYSFS_sint32 ln)
|
|
|
|
|
-{
|
|
|
|
|
- char *newstr = __PHYSFS_smallAlloc(ln + 1);
|
|
|
|
|
- if (newstr == NULL)
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
- memcpy(newstr, str, ln);
|
|
|
|
|
- newstr[ln] = '\0';
|
|
|
|
|
- cb(callbackdata, odir, newstr);
|
|
|
|
|
- __PHYSFS_smallFree(newstr);
|
|
|
|
|
-} /* doEnumCallback */
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-void UNPK_enumerateFiles(void *opaque, const char *dname,
|
|
|
|
|
- PHYSFS_EnumFilesCallback cb,
|
|
|
|
|
- const char *origdir, void *callbackdata)
|
|
|
|
|
|
|
+static inline UNPKentry *findEntry(UNPKinfo *info, const char *path)
|
|
|
{
|
|
{
|
|
|
- UNPKinfo *info = ((UNPKinfo *) opaque);
|
|
|
|
|
- PHYSFS_sint32 dlen, dlen_inc, max, i;
|
|
|
|
|
-
|
|
|
|
|
- i = findStartOfDir(info, dname, 0);
|
|
|
|
|
- if (i == -1) /* no such directory. */
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
- dlen = (PHYSFS_sint32) strlen(dname);
|
|
|
|
|
- if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
|
|
|
|
|
- dlen--;
|
|
|
|
|
-
|
|
|
|
|
- dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
|
|
|
|
|
- max = (PHYSFS_sint32) info->entryCount;
|
|
|
|
|
- while (i < max)
|
|
|
|
|
- {
|
|
|
|
|
- char *add;
|
|
|
|
|
- char *ptr;
|
|
|
|
|
- PHYSFS_sint32 ln;
|
|
|
|
|
- char *e = info->entries[i].name;
|
|
|
|
|
- if ((dlen) &&
|
|
|
|
|
- ((__PHYSFS_strnicmpASCII(e, dname, dlen)) || (e[dlen] != '/')))
|
|
|
|
|
- {
|
|
|
|
|
- break; /* past end of this dir; we're done. */
|
|
|
|
|
- } /* if */
|
|
|
|
|
-
|
|
|
|
|
- add = e + dlen_inc;
|
|
|
|
|
- ptr = strchr(add, '/');
|
|
|
|
|
- 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... */
|
|
|
|
|
- while ((++i < max) && (ptr != NULL))
|
|
|
|
|
- {
|
|
|
|
|
- char *e_new = info->entries[i].name;
|
|
|
|
|
- if ((__PHYSFS_strnicmpASCII(e, e_new, ln) != 0) ||
|
|
|
|
|
- (e_new[ln] != '/'))
|
|
|
|
|
- {
|
|
|
|
|
- break;
|
|
|
|
|
- } /* if */
|
|
|
|
|
- } /* while */
|
|
|
|
|
- } /* while */
|
|
|
|
|
-} /* UNPK_enumerateFiles */
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-/*
|
|
|
|
|
- * This will find the UNPKentry associated with a path in platform-independent
|
|
|
|
|
- * notation. Directories don't have UNPKentries associated with them, but
|
|
|
|
|
- * (*isDir) will be set to non-zero if a dir was hit.
|
|
|
|
|
- */
|
|
|
|
|
-static UNPKentry *findEntry(const UNPKinfo *info, const char *path, int *isDir)
|
|
|
|
|
-{
|
|
|
|
|
- UNPKentry *a = info->entries;
|
|
|
|
|
- PHYSFS_sint32 pathlen = (PHYSFS_sint32) strlen(path);
|
|
|
|
|
- PHYSFS_sint32 lo = 0;
|
|
|
|
|
- PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
|
|
|
|
|
- PHYSFS_sint32 middle;
|
|
|
|
|
- const char *thispath = NULL;
|
|
|
|
|
- int rc;
|
|
|
|
|
-
|
|
|
|
|
- while (lo <= hi)
|
|
|
|
|
- {
|
|
|
|
|
- middle = lo + ((hi - lo) / 2);
|
|
|
|
|
- thispath = a[middle].name;
|
|
|
|
|
- rc = __PHYSFS_strnicmpASCII(path, thispath, pathlen);
|
|
|
|
|
-
|
|
|
|
|
- if (rc > 0)
|
|
|
|
|
- lo = middle + 1;
|
|
|
|
|
-
|
|
|
|
|
- else if (rc < 0)
|
|
|
|
|
- hi = middle - 1;
|
|
|
|
|
-
|
|
|
|
|
- else /* substring match...might be dir or entry or nothing. */
|
|
|
|
|
- {
|
|
|
|
|
- if (isDir != NULL)
|
|
|
|
|
- {
|
|
|
|
|
- *isDir = (thispath[pathlen] == '/');
|
|
|
|
|
- if (*isDir)
|
|
|
|
|
- return NULL;
|
|
|
|
|
- } /* if */
|
|
|
|
|
-
|
|
|
|
|
- if (thispath[pathlen] == '\0') /* found entry? */
|
|
|
|
|
- return &a[middle];
|
|
|
|
|
- /* adjust search params, try again. */
|
|
|
|
|
- else if (thispath[pathlen] > '/')
|
|
|
|
|
- hi = middle - 1;
|
|
|
|
|
- else
|
|
|
|
|
- lo = middle + 1;
|
|
|
|
|
- } /* if */
|
|
|
|
|
- } /* while */
|
|
|
|
|
-
|
|
|
|
|
- if (isDir != NULL)
|
|
|
|
|
- *isDir = 0;
|
|
|
|
|
-
|
|
|
|
|
- BAIL(PHYSFS_ERR_NOT_FOUND, NULL);
|
|
|
|
|
|
|
+ return (UNPKentry *) __PHYSFS_DirTreeFind(&info->tree, path);
|
|
|
} /* findEntry */
|
|
} /* findEntry */
|
|
|
|
|
|
|
|
|
|
|
|
@@ -349,11 +165,10 @@ PHYSFS_Io *UNPK_openRead(void *opaque, const char *name)
|
|
|
PHYSFS_Io *retval = NULL;
|
|
PHYSFS_Io *retval = NULL;
|
|
|
UNPKinfo *info = (UNPKinfo *) opaque;
|
|
UNPKinfo *info = (UNPKinfo *) opaque;
|
|
|
UNPKfileinfo *finfo = NULL;
|
|
UNPKfileinfo *finfo = NULL;
|
|
|
- int isdir = 0;
|
|
|
|
|
- UNPKentry *entry = findEntry(info, name, &isdir);
|
|
|
|
|
|
|
+ UNPKentry *entry = findEntry(info, name);
|
|
|
|
|
|
|
|
- GOTO_IF(isdir, PHYSFS_ERR_NOT_A_FILE, UNPK_openRead_failed);
|
|
|
|
|
- GOTO_IF_ERRPASS(!entry, UNPK_openRead_failed);
|
|
|
|
|
|
|
+ BAIL_IF_ERRPASS(!entry, NULL);
|
|
|
|
|
+ BAIL_IF(entry->tree.isdir, PHYSFS_ERR_NOT_A_FILE, NULL);
|
|
|
|
|
|
|
|
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
|
|
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
|
|
|
GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
|
|
GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
|
|
@@ -413,25 +228,22 @@ int UNPK_mkdir(void *opaque, const char *name)
|
|
|
} /* UNPK_mkdir */
|
|
} /* UNPK_mkdir */
|
|
|
|
|
|
|
|
|
|
|
|
|
-int UNPK_stat(void *opaque, const char *filename, PHYSFS_Stat *stat)
|
|
|
|
|
|
|
+int UNPK_stat(void *opaque, const char *path, PHYSFS_Stat *stat)
|
|
|
{
|
|
{
|
|
|
- int isDir = 0;
|
|
|
|
|
- const UNPKinfo *info = (const UNPKinfo *) opaque;
|
|
|
|
|
- const UNPKentry *entry = findEntry(info, filename, &isDir);
|
|
|
|
|
|
|
+ UNPKinfo *info = (UNPKinfo *) opaque;
|
|
|
|
|
+ const UNPKentry *entry = findEntry(info, path);
|
|
|
|
|
|
|
|
- if (isDir)
|
|
|
|
|
|
|
+ BAIL_IF_ERRPASS(!entry, 0);
|
|
|
|
|
+
|
|
|
|
|
+ if (entry->tree.isdir)
|
|
|
{
|
|
{
|
|
|
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
|
|
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
|
|
|
stat->filesize = 0;
|
|
stat->filesize = 0;
|
|
|
} /* if */
|
|
} /* if */
|
|
|
- else if (entry != NULL)
|
|
|
|
|
|
|
+ else
|
|
|
{
|
|
{
|
|
|
stat->filetype = PHYSFS_FILETYPE_REGULAR;
|
|
stat->filetype = PHYSFS_FILETYPE_REGULAR;
|
|
|
stat->filesize = entry->size;
|
|
stat->filesize = entry->size;
|
|
|
- } /* else if */
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- return 0;
|
|
|
|
|
} /* else */
|
|
} /* else */
|
|
|
|
|
|
|
|
stat->modtime = -1;
|
|
stat->modtime = -1;
|
|
@@ -443,19 +255,34 @@ int UNPK_stat(void *opaque, const char *filename, PHYSFS_Stat *stat)
|
|
|
} /* UNPK_stat */
|
|
} /* UNPK_stat */
|
|
|
|
|
|
|
|
|
|
|
|
|
-void *UNPK_openArchive(PHYSFS_Io *io, UNPKentry *e, const PHYSFS_uint32 num)
|
|
|
|
|
|
|
+void *UNPK_addEntry(void *opaque, char *name, const int isdir,
|
|
|
|
|
+ const PHYSFS_uint64 pos, const PHYSFS_uint64 len)
|
|
|
|
|
+{
|
|
|
|
|
+ UNPKinfo *info = (UNPKinfo *) opaque;
|
|
|
|
|
+ UNPKentry *entry;
|
|
|
|
|
+
|
|
|
|
|
+ entry = (UNPKentry *) __PHYSFS_DirTreeAdd(&info->tree, name, isdir);
|
|
|
|
|
+ BAIL_IF_ERRPASS(!entry, NULL);
|
|
|
|
|
+
|
|
|
|
|
+ entry->startPos = pos;
|
|
|
|
|
+ entry->size = len;
|
|
|
|
|
+
|
|
|
|
|
+ return entry;
|
|
|
|
|
+} /* UNPK_addEntry */
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+void *UNPK_openArchive(PHYSFS_Io *io, const PHYSFS_uint64 entry_count)
|
|
|
{
|
|
{
|
|
|
UNPKinfo *info = (UNPKinfo *) allocator.Malloc(sizeof (UNPKinfo));
|
|
UNPKinfo *info = (UNPKinfo *) allocator.Malloc(sizeof (UNPKinfo));
|
|
|
- if (info == NULL)
|
|
|
|
|
|
|
+ BAIL_IF(!info, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
|
+
|
|
|
|
|
+ if (!__PHYSFS_DirTreeInit(&info->tree, entry_count, sizeof (UNPKentry)))
|
|
|
{
|
|
{
|
|
|
- allocator.Free(e);
|
|
|
|
|
- BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
|
|
|
+ allocator.Free(info);
|
|
|
|
|
+ return NULL;
|
|
|
} /* if */
|
|
} /* if */
|
|
|
|
|
|
|
|
- __PHYSFS_sort(e, (size_t) num, entryCmp, entrySwap);
|
|
|
|
|
info->io = io;
|
|
info->io = io;
|
|
|
- info->entryCount = num;
|
|
|
|
|
- info->entries = e;
|
|
|
|
|
|
|
|
|
|
return info;
|
|
return info;
|
|
|
} /* UNPK_openArchive */
|
|
} /* UNPK_openArchive */
|