2
* High-level PhysicsFS archiver for simple unpacked file formats.
4
* This is a framework that basic archivers build on top of. It's for simple
5
* formats that can just hand back a list of files and the offsets of their
6
* uncompressed data. There are an alarming number of formats like this.
8
* RULES: Archive entries must be uncompressed, must not have separate subdir
9
* entries (but can have subdirs), must be case insensitive LOW ASCII
10
* filenames <= 56 bytes. No symlinks, etc. We can relax some of these rules
13
* Please see the file LICENSE.txt in the source's root directory.
15
* This file written by Ryan C. Gordon.
18
#define __PHYSICSFS_INTERNAL__
19
#include "physfs_internal.h"
24
PHYSFS_uint32 entryCount;
37
void UNPK_closeArchive(PHYSFS_Dir *opaque)
39
UNPKinfo *info = ((UNPKinfo *) opaque);
40
info->io->destroy(info->io);
41
allocator.Free(info->entries);
43
} /* UNPK_closeArchive */
46
static PHYSFS_sint64 UNPK_read(PHYSFS_Io *io, void *buffer, PHYSFS_uint64 len)
48
UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
49
const UNPKentry *entry = finfo->entry;
50
const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
56
rc = finfo->io->read(finfo->io, buffer, len);
58
finfo->curPos += (PHYSFS_uint32) rc;
64
static PHYSFS_sint64 UNPK_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
66
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, -1);
70
static PHYSFS_sint64 UNPK_tell(PHYSFS_Io *io)
72
return ((UNPKfileinfo *) io->opaque)->curPos;
76
static int UNPK_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
78
UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
79
const UNPKentry *entry = finfo->entry;
82
BAIL_IF_MACRO(offset >= entry->size, PHYSFS_ERR_PAST_EOF, 0);
83
rc = finfo->io->seek(finfo->io, entry->startPos + offset);
85
finfo->curPos = (PHYSFS_uint32) offset;
91
static PHYSFS_sint64 UNPK_length(PHYSFS_Io *io)
93
const UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
94
return ((PHYSFS_sint64) finfo->entry->size);
98
static PHYSFS_Io *UNPK_duplicate(PHYSFS_Io *_io)
100
UNPKfileinfo *origfinfo = (UNPKfileinfo *) _io->opaque;
101
PHYSFS_Io *io = NULL;
102
PHYSFS_Io *retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
103
UNPKfileinfo *finfo = (UNPKfileinfo *) allocator.Malloc(sizeof (UNPKfileinfo));
104
GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_duplicate_failed);
105
GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_duplicate_failed);
107
io = origfinfo->io->duplicate(origfinfo->io);
108
if (!io) goto UNPK_duplicate_failed;
110
finfo->entry = origfinfo->entry;
112
memcpy(retval, _io, sizeof (PHYSFS_Io));
113
retval->opaque = finfo;
116
UNPK_duplicate_failed:
117
if (finfo != NULL) allocator.Free(finfo);
118
if (retval != NULL) allocator.Free(retval);
119
if (io != NULL) io->destroy(io);
121
} /* UNPK_duplicate */
123
static int UNPK_flush(PHYSFS_Io *io) { return 1; /* no write support. */ }
125
static void UNPK_destroy(PHYSFS_Io *io)
127
UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
128
finfo->io->destroy(finfo->io);
129
allocator.Free(finfo);
134
static const PHYSFS_Io UNPK_Io =
136
CURRENT_PHYSFS_IO_API_VERSION, NULL,
148
static int entryCmp(void *_a, size_t one, size_t two)
152
const UNPKentry *a = (const UNPKentry *) _a;
153
return __PHYSFS_stricmpASCII(a[one].name, a[two].name);
160
static void entrySwap(void *_a, size_t one, size_t two)
165
UNPKentry *first = &(((UNPKentry *) _a)[one]);
166
UNPKentry *second = &(((UNPKentry *) _a)[two]);
167
memcpy(&tmp, first, sizeof (UNPKentry));
168
memcpy(first, second, sizeof (UNPKentry));
169
memcpy(second, &tmp, sizeof (UNPKentry));
174
static PHYSFS_sint32 findStartOfDir(UNPKinfo *info, const char *path,
175
int stop_on_first_find)
177
PHYSFS_sint32 lo = 0;
178
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
179
PHYSFS_sint32 middle;
180
PHYSFS_uint32 dlen = (PHYSFS_uint32) strlen(path);
181
PHYSFS_sint32 retval = -1;
185
if (*path == '\0') /* root dir? */
188
if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
193
middle = lo + ((hi - lo) / 2);
194
name = info->entries[middle].name;
195
rc = __PHYSFS_strnicmpASCII(path, name, dlen);
198
char ch = name[dlen];
199
if (ch < '/') /* make sure this isn't just a substr match. */
205
if (stop_on_first_find) /* Just checking dir's existance? */
208
if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
211
/* there might be more entries earlier in the list. */
224
} /* findStartOfDir */
228
* Moved to seperate function so we can use alloca then immediately throw
229
* away the allocated stack space...
231
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
232
const char *odir, const char *str, PHYSFS_sint32 ln)
234
char *newstr = __PHYSFS_smallAlloc(ln + 1);
238
memcpy(newstr, str, ln);
240
cb(callbackdata, odir, newstr);
241
__PHYSFS_smallFree(newstr);
242
} /* doEnumCallback */
245
void UNPK_enumerateFiles(PHYSFS_Dir *opaque, const char *dname,
246
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
247
const char *origdir, void *callbackdata)
249
UNPKinfo *info = ((UNPKinfo *) opaque);
250
PHYSFS_sint32 dlen, dlen_inc, max, i;
252
i = findStartOfDir(info, dname, 0);
253
if (i == -1) /* no such directory. */
256
dlen = (PHYSFS_sint32) strlen(dname);
257
if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
260
dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
261
max = (PHYSFS_sint32) info->entryCount;
267
char *e = info->entries[i].name;
269
((__PHYSFS_strnicmpASCII(e, dname, dlen)) || (e[dlen] != '/')))
271
break; /* past end of this dir; we're done. */
275
ptr = strchr(add, '/');
276
ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
277
doEnumCallback(cb, callbackdata, origdir, add, ln);
278
ln += dlen_inc; /* point past entry to children... */
280
/* increment counter and skip children of subdirs... */
281
while ((++i < max) && (ptr != NULL))
283
char *e_new = info->entries[i].name;
284
if ((__PHYSFS_strnicmpASCII(e, e_new, ln) != 0) ||
291
} /* UNPK_enumerateFiles */
295
* This will find the UNPKentry associated with a path in platform-independent
296
* notation. Directories don't have UNPKentries associated with them, but
297
* (*isDir) will be set to non-zero if a dir was hit.
299
static UNPKentry *findEntry(const UNPKinfo *info, const char *path, int *isDir)
301
UNPKentry *a = info->entries;
302
PHYSFS_sint32 pathlen = (PHYSFS_sint32) strlen(path);
303
PHYSFS_sint32 lo = 0;
304
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
305
PHYSFS_sint32 middle;
306
const char *thispath = NULL;
311
middle = lo + ((hi - lo) / 2);
312
thispath = a[middle].name;
313
rc = __PHYSFS_strnicmpASCII(path, thispath, pathlen);
321
else /* substring match...might be dir or entry or nothing. */
325
*isDir = (thispath[pathlen] == '/');
330
if (thispath[pathlen] == '\0') /* found entry? */
332
/* adjust search params, try again. */
333
else if (thispath[pathlen] > '/')
343
BAIL_MACRO(PHYSFS_ERR_NO_SUCH_PATH, NULL);
347
PHYSFS_Io *UNPK_openRead(PHYSFS_Dir *opaque, const char *fnm, int *fileExists)
349
PHYSFS_Io *retval = NULL;
350
UNPKinfo *info = (UNPKinfo *) opaque;
351
UNPKfileinfo *finfo = NULL;
353
UNPKentry *entry = findEntry(info, fnm, &isdir);
355
*fileExists = (entry != NULL);
356
GOTO_IF_MACRO(isdir, PHYSFS_ERR_NOT_A_FILE, UNPK_openRead_failed);
357
GOTO_IF_MACRO(!entry, ERRPASS, UNPK_openRead_failed);
359
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
360
GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
362
finfo = (UNPKfileinfo *) allocator.Malloc(sizeof (UNPKfileinfo));
363
GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
365
finfo->io = info->io->duplicate(info->io);
366
GOTO_IF_MACRO(!finfo->io, ERRPASS, UNPK_openRead_failed);
368
if (!finfo->io->seek(finfo->io, entry->startPos))
369
goto UNPK_openRead_failed;
372
finfo->entry = entry;
374
memcpy(retval, &UNPK_Io, sizeof (*retval));
375
retval->opaque = finfo;
378
UNPK_openRead_failed:
381
if (finfo->io != NULL)
382
finfo->io->destroy(finfo->io);
383
allocator.Free(finfo);
387
allocator.Free(retval);
390
} /* UNPK_openRead */
393
PHYSFS_Io *UNPK_openWrite(PHYSFS_Dir *opaque, const char *name)
395
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
396
} /* UNPK_openWrite */
399
PHYSFS_Io *UNPK_openAppend(PHYSFS_Dir *opaque, const char *name)
401
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
402
} /* UNPK_openAppend */
405
int UNPK_remove(PHYSFS_Dir *opaque, const char *name)
407
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
411
int UNPK_mkdir(PHYSFS_Dir *opaque, const char *name)
413
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
417
int UNPK_stat(PHYSFS_Dir *opaque, const char *filename,
418
int *exists, PHYSFS_Stat *stat)
421
const UNPKinfo *info = (const UNPKinfo *) opaque;
422
const UNPKentry *entry = findEntry(info, filename, &isDir);
427
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
430
else if (entry != NULL)
433
stat->filetype = PHYSFS_FILETYPE_REGULAR;
434
stat->filesize = entry->size;
443
stat->createtime = -1;
444
stat->accesstime = -1;
451
PHYSFS_Dir *UNPK_openArchive(PHYSFS_Io *io, UNPKentry *e,
452
const PHYSFS_uint32 num)
454
UNPKinfo *info = (UNPKinfo *) allocator.Malloc(sizeof (UNPKinfo));
458
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
461
__PHYSFS_sort(e, (size_t) num, entryCmp, entrySwap);
463
info->entryCount = num;
467
} /* UNPK_openArchive */
469
/* end of archiver_unpacked.c ... */