2 * QPAK support routines for PhysicsFS.
4 * This archiver handles the archive format utilized by Quake 1 and 2.
5 * Quake3-based games use the PkZip/Info-Zip format (which our zip.c
8 * ========================================================================
10 * This format info (in more detail) comes from:
11 * http://debian.fmi.uni-sofia.bg/~sergei/cgsr/docs/pak.txt
16 * (4 bytes) signature = 'PACK'
17 * (4 bytes) directory offset
18 * (4 bytes) directory length
21 * (56 bytes) file name
22 * (4 bytes) file position
23 * (4 bytes) file length
25 * ========================================================================
27 * Please see the file LICENSE.txt in the source's root directory.
29 * This file written by Ryan C. Gordon.
32 #if (defined PHYSFS_SUPPORTS_QPAK)
39 #define __PHYSICSFS_INTERNAL__
40 #include "physfs_internal.h"
42 #if 1 /* Make this case insensitive? */
43 #define QPAK_strcmp(x, y) __PHYSFS_stricmpASCII(x, y)
44 #define QPAK_strncmp(x, y, z) __PHYSFS_strnicmpASCII(x, y, z)
46 #define QPAK_strcmp(x, y) strcmp(x, y)
47 #define QPAK_strncmp(x, y, z) strncmp(x, y, z)
54 PHYSFS_uint32 startPos;
61 PHYSFS_sint64 last_mod_time;
62 PHYSFS_uint32 entryCount;
73 /* Magic numbers... */
74 #define QPAK_SIG 0x4b434150 /* "PACK" in ASCII. */
77 static void QPAK_dirClose(dvoid *opaque)
79 QPAKinfo *info = ((QPAKinfo *) opaque);
80 allocator.Free(info->filename);
81 allocator.Free(info->entries);
86 static PHYSFS_sint64 QPAK_read(fvoid *opaque, void *buffer,
87 PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
89 QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
90 QPAKentry *entry = finfo->entry;
91 PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
92 PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
95 if (objsLeft < objCount)
98 rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
100 finfo->curPos += (PHYSFS_uint32) (rc * objSize);
106 static PHYSFS_sint64 QPAK_write(fvoid *opaque, const void *buffer,
107 PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
109 BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
113 static int QPAK_eof(fvoid *opaque)
115 QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
116 QPAKentry *entry = finfo->entry;
117 return(finfo->curPos >= entry->size);
121 static PHYSFS_sint64 QPAK_tell(fvoid *opaque)
123 return(((QPAKfileinfo *) opaque)->curPos);
127 static int QPAK_seek(fvoid *opaque, PHYSFS_uint64 offset)
129 QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
130 QPAKentry *entry = finfo->entry;
133 BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
134 BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
135 rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
137 finfo->curPos = (PHYSFS_uint32) offset;
143 static PHYSFS_sint64 QPAK_fileLength(fvoid *opaque)
145 QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
146 return((PHYSFS_sint64) finfo->entry->size);
147 } /* QPAK_fileLength */
150 static int QPAK_fileClose(fvoid *opaque)
152 QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
153 BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
154 allocator.Free(finfo);
156 } /* QPAK_fileClose */
159 static int qpak_open(const char *filename, int forWriting,
160 void **fh, PHYSFS_uint32 *count)
165 BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
167 *fh = __PHYSFS_platformOpenRead(filename);
168 BAIL_IF_MACRO(*fh == NULL, NULL, 0);
170 if (__PHYSFS_platformRead(*fh, &buf, sizeof (PHYSFS_uint32), 1) != 1)
171 goto openQpak_failed;
173 buf = PHYSFS_swapULE32(buf);
174 GOTO_IF_MACRO(buf != QPAK_SIG, ERR_UNSUPPORTED_ARCHIVE, openQpak_failed);
176 if (__PHYSFS_platformRead(*fh, &buf, sizeof (PHYSFS_uint32), 1) != 1)
177 goto openQpak_failed;
179 buf = PHYSFS_swapULE32(buf); /* directory table offset. */
181 if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
182 goto openQpak_failed;
184 *count = PHYSFS_swapULE32(*count);
186 /* corrupted archive? */
187 GOTO_IF_MACRO((*count % 64) != 0, ERR_CORRUPTED, openQpak_failed);
189 if (!__PHYSFS_platformSeek(*fh, buf))
190 goto openQpak_failed;
197 __PHYSFS_platformClose(*fh);
205 static int QPAK_isArchive(const char *filename, int forWriting)
208 PHYSFS_uint32 fileCount;
209 int retval = qpak_open(filename, forWriting, &fh, &fileCount);
212 __PHYSFS_platformClose(fh);
215 } /* QPAK_isArchive */
218 static int qpak_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
220 QPAKentry *a = (QPAKentry *) _a;
221 return(QPAK_strcmp(a[one].name, a[two].name));
222 } /* qpak_entry_cmp */
225 static void qpak_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
228 QPAKentry *first = &(((QPAKentry *) _a)[one]);
229 QPAKentry *second = &(((QPAKentry *) _a)[two]);
230 memcpy(&tmp, first, sizeof (QPAKentry));
231 memcpy(first, second, sizeof (QPAKentry));
232 memcpy(second, &tmp, sizeof (QPAKentry));
233 } /* qpak_entry_swap */
236 static int qpak_load_entries(const char *name, int forWriting, QPAKinfo *info)
239 PHYSFS_uint32 fileCount;
242 BAIL_IF_MACRO(!qpak_open(name, forWriting, &fh, &fileCount), NULL, 0);
243 info->entryCount = fileCount;
244 info->entries = (QPAKentry*) allocator.Malloc(sizeof(QPAKentry)*fileCount);
245 if (info->entries == NULL)
247 __PHYSFS_platformClose(fh);
248 BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
251 for (entry = info->entries; fileCount > 0; fileCount--, entry++)
255 if (__PHYSFS_platformRead(fh,&entry->name,sizeof(entry->name),1) != 1)
257 __PHYSFS_platformClose(fh);
261 if (__PHYSFS_platformRead(fh,&loc,sizeof(loc),1) != 1)
263 __PHYSFS_platformClose(fh);
267 if (__PHYSFS_platformRead(fh,&entry->size,sizeof(entry->size),1) != 1)
269 __PHYSFS_platformClose(fh);
273 entry->size = PHYSFS_swapULE32(entry->size);
274 entry->startPos = PHYSFS_swapULE32(loc);
277 __PHYSFS_platformClose(fh);
279 __PHYSFS_sort(info->entries, info->entryCount,
280 qpak_entry_cmp, qpak_entry_swap);
282 } /* qpak_load_entries */
285 static void *QPAK_openArchive(const char *name, int forWriting)
287 QPAKinfo *info = (QPAKinfo *) allocator.Malloc(sizeof (QPAKinfo));
288 PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
290 BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
291 memset(info, '\0', sizeof (QPAKinfo));
293 info->filename = (char *) allocator.Malloc(strlen(name) + 1);
294 if (info->filename == NULL)
296 __PHYSFS_setError(ERR_OUT_OF_MEMORY);
297 goto QPAK_openArchive_failed;
300 if (!qpak_load_entries(name, forWriting, info))
301 goto QPAK_openArchive_failed;
303 strcpy(info->filename, name);
304 info->last_mod_time = modtime;
307 QPAK_openArchive_failed:
310 if (info->filename != NULL)
311 allocator.Free(info->filename);
312 if (info->entries != NULL)
313 allocator.Free(info->entries);
314 allocator.Free(info);
318 } /* QPAK_openArchive */
321 static PHYSFS_sint32 qpak_find_start_of_dir(QPAKinfo *info, const char *path,
322 int stop_on_first_find)
324 PHYSFS_sint32 lo = 0;
325 PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
326 PHYSFS_sint32 middle;
327 PHYSFS_uint32 dlen = strlen(path);
328 PHYSFS_sint32 retval = -1;
332 if (*path == '\0') /* root dir? */
335 if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
340 middle = lo + ((hi - lo) / 2);
341 name = info->entries[middle].name;
342 rc = QPAK_strncmp(path, name, dlen);
345 char ch = name[dlen];
346 if (ch < '/') /* make sure this isn't just a substr match. */
352 if (stop_on_first_find) /* Just checking dir's existance? */
355 if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
358 /* there might be more entries earlier in the list. */
371 } /* qpak_find_start_of_dir */
375 * Moved to seperate function so we can use alloca then immediately throw
376 * away the allocated stack space...
378 static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
379 const char *odir, const char *str, PHYSFS_sint32 ln)
381 char *newstr = __PHYSFS_smallAlloc(ln + 1);
385 memcpy(newstr, str, ln);
387 cb(callbackdata, odir, newstr);
388 __PHYSFS_smallFree(newstr);
389 } /* doEnumCallback */
392 static void QPAK_enumerateFiles(dvoid *opaque, const char *dname,
393 int omitSymLinks, PHYSFS_EnumFilesCallback cb,
394 const char *origdir, void *callbackdata)
396 QPAKinfo *info = ((QPAKinfo *) opaque);
397 PHYSFS_sint32 dlen, dlen_inc, max, i;
399 i = qpak_find_start_of_dir(info, dname, 0);
400 if (i == -1) /* no such directory. */
403 dlen = strlen(dname);
404 if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
407 dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
408 max = (PHYSFS_sint32) info->entryCount;
414 char *e = info->entries[i].name;
415 if ((dlen) && ((QPAK_strncmp(e, dname, dlen)) || (e[dlen] != '/')))
416 break; /* past end of this dir; we're done. */
419 ptr = strchr(add, '/');
420 ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
421 doEnumCallback(cb, callbackdata, origdir, add, ln);
422 ln += dlen_inc; /* point past entry to children... */
424 /* increment counter and skip children of subdirs... */
425 while ((++i < max) && (ptr != NULL))
427 char *e_new = info->entries[i].name;
428 if ((QPAK_strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
432 } /* QPAK_enumerateFiles */
436 * This will find the QPAKentry associated with a path in platform-independent
437 * notation. Directories don't have QPAKentries associated with them, but
438 * (*isDir) will be set to non-zero if a dir was hit.
440 static QPAKentry *qpak_find_entry(QPAKinfo *info, const char *path, int *isDir)
442 QPAKentry *a = info->entries;
443 PHYSFS_sint32 pathlen = strlen(path);
444 PHYSFS_sint32 lo = 0;
445 PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
446 PHYSFS_sint32 middle;
447 const char *thispath = NULL;
452 middle = lo + ((hi - lo) / 2);
453 thispath = a[middle].name;
454 rc = QPAK_strncmp(path, thispath, pathlen);
462 else /* substring match...might be dir or entry or nothing. */
466 *isDir = (thispath[pathlen] == '/');
471 if (thispath[pathlen] == '\0') /* found entry? */
474 hi = middle - 1; /* adjust search params, try again. */
481 BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
482 } /* qpak_find_entry */
485 static int QPAK_exists(dvoid *opaque, const char *name)
488 QPAKinfo *info = (QPAKinfo *) opaque;
489 QPAKentry *entry = qpak_find_entry(info, name, &isDir);
490 return((entry != NULL) || (isDir));
494 static int QPAK_isDirectory(dvoid *opaque, const char *name, int *fileExists)
496 QPAKinfo *info = (QPAKinfo *) opaque;
498 QPAKentry *entry = qpak_find_entry(info, name, &isDir);
500 *fileExists = ((isDir) || (entry != NULL));
502 return(1); /* definitely a dir. */
504 BAIL_MACRO(ERR_NO_SUCH_FILE, 0);
505 } /* QPAK_isDirectory */
508 static int QPAK_isSymLink(dvoid *opaque, const char *name, int *fileExists)
510 *fileExists = QPAK_exists(opaque, name);
511 return(0); /* never symlinks in a quake pak. */
512 } /* QPAK_isSymLink */
515 static PHYSFS_sint64 QPAK_getLastModTime(dvoid *opaque,
520 QPAKinfo *info = ((QPAKinfo *) opaque);
521 PHYSFS_sint64 retval = -1;
522 QPAKentry *entry = qpak_find_entry(info, name, &isDir);
524 *fileExists = ((isDir) || (entry != NULL));
525 if (*fileExists) /* use time of QPAK itself in the physical filesystem. */
526 retval = info->last_mod_time;
529 } /* QPAK_getLastModTime */
532 static fvoid *QPAK_openRead(dvoid *opaque, const char *fnm, int *fileExists)
534 QPAKinfo *info = ((QPAKinfo *) opaque);
539 entry = qpak_find_entry(info, fnm, &isDir);
540 *fileExists = ((entry != NULL) || (isDir));
541 BAIL_IF_MACRO(isDir, ERR_NOT_A_FILE, NULL);
542 BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
544 finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
545 BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
547 finfo->handle = __PHYSFS_platformOpenRead(info->filename);
548 if ( (finfo->handle == NULL) ||
549 (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
551 allocator.Free(finfo);
556 finfo->entry = entry;
558 } /* QPAK_openRead */
561 static fvoid *QPAK_openWrite(dvoid *opaque, const char *name)
563 BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
564 } /* QPAK_openWrite */
567 static fvoid *QPAK_openAppend(dvoid *opaque, const char *name)
569 BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
570 } /* QPAK_openAppend */
573 static int QPAK_remove(dvoid *opaque, const char *name)
575 BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
579 static int QPAK_mkdir(dvoid *opaque, const char *name)
581 BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
585 const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
588 QPAK_ARCHIVE_DESCRIPTION,
589 "Ryan C. Gordon <icculus@icculus.org>",
590 "http://icculus.org/physfs/",
594 const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
596 &__PHYSFS_ArchiveInfo_QPAK,
597 QPAK_isArchive, /* isArchive() method */
598 QPAK_openArchive, /* openArchive() method */
599 QPAK_enumerateFiles, /* enumerateFiles() method */
600 QPAK_exists, /* exists() method */
601 QPAK_isDirectory, /* isDirectory() method */
602 QPAK_isSymLink, /* isSymLink() method */
603 QPAK_getLastModTime, /* getLastModTime() method */
604 QPAK_openRead, /* openRead() method */
605 QPAK_openWrite, /* openWrite() method */
606 QPAK_openAppend, /* openAppend() method */
607 QPAK_remove, /* remove() method */
608 QPAK_mkdir, /* mkdir() method */
609 QPAK_dirClose, /* dirClose() method */
610 QPAK_read, /* read() method */
611 QPAK_write, /* write() method */
612 QPAK_eof, /* eof() method */
613 QPAK_tell, /* tell() method */
614 QPAK_seek, /* seek() method */
615 QPAK_fileLength, /* fileLength() method */
616 QPAK_fileClose /* fileClose() method */
619 #endif /* defined PHYSFS_SUPPORTS_QPAK */
621 /* end of qpak.c ... */