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)
222 const QPAKentry *a = (const QPAKentry *) _a;
223 return(QPAK_strcmp(a[one].name, a[two].name));
227 } /* qpak_entry_cmp */
230 static void qpak_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
235 QPAKentry *first = &(((QPAKentry *) _a)[one]);
236 QPAKentry *second = &(((QPAKentry *) _a)[two]);
237 memcpy(&tmp, first, sizeof (QPAKentry));
238 memcpy(first, second, sizeof (QPAKentry));
239 memcpy(second, &tmp, sizeof (QPAKentry));
241 } /* qpak_entry_swap */
244 static int qpak_load_entries(const char *name, int forWriting, QPAKinfo *info)
247 PHYSFS_uint32 fileCount;
250 BAIL_IF_MACRO(!qpak_open(name, forWriting, &fh, &fileCount), NULL, 0);
251 info->entryCount = fileCount;
252 info->entries = (QPAKentry*) allocator.Malloc(sizeof(QPAKentry)*fileCount);
253 if (info->entries == NULL)
255 __PHYSFS_platformClose(fh);
256 BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
259 for (entry = info->entries; fileCount > 0; fileCount--, entry++)
263 if (__PHYSFS_platformRead(fh,&entry->name,sizeof(entry->name),1) != 1)
265 __PHYSFS_platformClose(fh);
269 if (__PHYSFS_platformRead(fh,&loc,sizeof(loc),1) != 1)
271 __PHYSFS_platformClose(fh);
275 if (__PHYSFS_platformRead(fh,&entry->size,sizeof(entry->size),1) != 1)
277 __PHYSFS_platformClose(fh);
281 entry->size = PHYSFS_swapULE32(entry->size);
282 entry->startPos = PHYSFS_swapULE32(loc);
285 __PHYSFS_platformClose(fh);
287 __PHYSFS_sort(info->entries, info->entryCount,
288 qpak_entry_cmp, qpak_entry_swap);
290 } /* qpak_load_entries */
293 static void *QPAK_openArchive(const char *name, int forWriting)
295 QPAKinfo *info = (QPAKinfo *) allocator.Malloc(sizeof (QPAKinfo));
296 PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
298 BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
299 memset(info, '\0', sizeof (QPAKinfo));
301 info->filename = (char *) allocator.Malloc(strlen(name) + 1);
302 if (info->filename == NULL)
304 __PHYSFS_setError(ERR_OUT_OF_MEMORY);
305 goto QPAK_openArchive_failed;
308 if (!qpak_load_entries(name, forWriting, info))
309 goto QPAK_openArchive_failed;
311 strcpy(info->filename, name);
312 info->last_mod_time = modtime;
315 QPAK_openArchive_failed:
318 if (info->filename != NULL)
319 allocator.Free(info->filename);
320 if (info->entries != NULL)
321 allocator.Free(info->entries);
322 allocator.Free(info);
326 } /* QPAK_openArchive */
329 static PHYSFS_sint32 qpak_find_start_of_dir(QPAKinfo *info, const char *path,
330 int stop_on_first_find)
332 PHYSFS_sint32 lo = 0;
333 PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
334 PHYSFS_sint32 middle;
335 PHYSFS_uint32 dlen = strlen(path);
336 PHYSFS_sint32 retval = -1;
340 if (*path == '\0') /* root dir? */
343 if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
348 middle = lo + ((hi - lo) / 2);
349 name = info->entries[middle].name;
350 rc = QPAK_strncmp(path, name, dlen);
353 char ch = name[dlen];
354 if (ch < '/') /* make sure this isn't just a substr match. */
360 if (stop_on_first_find) /* Just checking dir's existance? */
363 if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
366 /* there might be more entries earlier in the list. */
379 } /* qpak_find_start_of_dir */
383 * Moved to seperate function so we can use alloca then immediately throw
384 * away the allocated stack space...
386 static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
387 const char *odir, const char *str, PHYSFS_sint32 ln)
389 char *newstr = __PHYSFS_smallAlloc(ln + 1);
393 memcpy(newstr, str, ln);
395 cb(callbackdata, odir, newstr);
396 __PHYSFS_smallFree(newstr);
397 } /* doEnumCallback */
400 static void QPAK_enumerateFiles(dvoid *opaque, const char *dname,
401 int omitSymLinks, PHYSFS_EnumFilesCallback cb,
402 const char *origdir, void *callbackdata)
404 QPAKinfo *info = ((QPAKinfo *) opaque);
405 PHYSFS_sint32 dlen, dlen_inc, max, i;
407 i = qpak_find_start_of_dir(info, dname, 0);
408 if (i == -1) /* no such directory. */
411 dlen = strlen(dname);
412 if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
415 dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
416 max = (PHYSFS_sint32) info->entryCount;
422 char *e = info->entries[i].name;
423 if ((dlen) && ((QPAK_strncmp(e, dname, dlen)) || (e[dlen] != '/')))
424 break; /* past end of this dir; we're done. */
427 ptr = strchr(add, '/');
428 ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
429 doEnumCallback(cb, callbackdata, origdir, add, ln);
430 ln += dlen_inc; /* point past entry to children... */
432 /* increment counter and skip children of subdirs... */
433 while ((++i < max) && (ptr != NULL))
435 char *e_new = info->entries[i].name;
436 if ((QPAK_strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
440 } /* QPAK_enumerateFiles */
444 * This will find the QPAKentry associated with a path in platform-independent
445 * notation. Directories don't have QPAKentries associated with them, but
446 * (*isDir) will be set to non-zero if a dir was hit.
448 static QPAKentry *qpak_find_entry(QPAKinfo *info, const char *path, int *isDir)
450 QPAKentry *a = info->entries;
451 PHYSFS_sint32 pathlen = strlen(path);
452 PHYSFS_sint32 lo = 0;
453 PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
454 PHYSFS_sint32 middle;
455 const char *thispath = NULL;
460 middle = lo + ((hi - lo) / 2);
461 thispath = a[middle].name;
462 rc = QPAK_strncmp(path, thispath, pathlen);
470 else /* substring match...might be dir or entry or nothing. */
474 *isDir = (thispath[pathlen] == '/');
479 if (thispath[pathlen] == '\0') /* found entry? */
481 /* adjust search params, try again. */
482 else if (thispath[pathlen] > '/')
492 BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
493 } /* qpak_find_entry */
496 static int QPAK_exists(dvoid *opaque, const char *name)
499 QPAKinfo *info = (QPAKinfo *) opaque;
500 QPAKentry *entry = qpak_find_entry(info, name, &isDir);
501 return((entry != NULL) || (isDir));
505 static int QPAK_isDirectory(dvoid *opaque, const char *name, int *fileExists)
507 QPAKinfo *info = (QPAKinfo *) opaque;
509 QPAKentry *entry = qpak_find_entry(info, name, &isDir);
511 *fileExists = ((isDir) || (entry != NULL));
513 return(1); /* definitely a dir. */
515 BAIL_MACRO(ERR_NO_SUCH_FILE, 0);
516 } /* QPAK_isDirectory */
519 static int QPAK_isSymLink(dvoid *opaque, const char *name, int *fileExists)
521 *fileExists = QPAK_exists(opaque, name);
522 return(0); /* never symlinks in a quake pak. */
523 } /* QPAK_isSymLink */
526 static PHYSFS_sint64 QPAK_getLastModTime(dvoid *opaque,
531 QPAKinfo *info = ((QPAKinfo *) opaque);
532 PHYSFS_sint64 retval = -1;
533 QPAKentry *entry = qpak_find_entry(info, name, &isDir);
535 *fileExists = ((isDir) || (entry != NULL));
536 if (*fileExists) /* use time of QPAK itself in the physical filesystem. */
537 retval = info->last_mod_time;
540 } /* QPAK_getLastModTime */
543 static fvoid *QPAK_openRead(dvoid *opaque, const char *fnm, int *fileExists)
545 QPAKinfo *info = ((QPAKinfo *) opaque);
550 entry = qpak_find_entry(info, fnm, &isDir);
551 *fileExists = ((entry != NULL) || (isDir));
552 BAIL_IF_MACRO(isDir, ERR_NOT_A_FILE, NULL);
553 BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
555 finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
556 BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
558 finfo->handle = __PHYSFS_platformOpenRead(info->filename);
559 if ( (finfo->handle == NULL) ||
560 (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
562 allocator.Free(finfo);
567 finfo->entry = entry;
569 } /* QPAK_openRead */
572 static fvoid *QPAK_openWrite(dvoid *opaque, const char *name)
574 BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
575 } /* QPAK_openWrite */
578 static fvoid *QPAK_openAppend(dvoid *opaque, const char *name)
580 BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
581 } /* QPAK_openAppend */
584 static int QPAK_remove(dvoid *opaque, const char *name)
586 BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
590 static int QPAK_mkdir(dvoid *opaque, const char *name)
592 BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
596 const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
599 QPAK_ARCHIVE_DESCRIPTION,
600 "Ryan C. Gordon <icculus@icculus.org>",
601 "http://icculus.org/physfs/",
605 const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
607 &__PHYSFS_ArchiveInfo_QPAK,
608 QPAK_isArchive, /* isArchive() method */
609 QPAK_openArchive, /* openArchive() method */
610 QPAK_enumerateFiles, /* enumerateFiles() method */
611 QPAK_exists, /* exists() method */
612 QPAK_isDirectory, /* isDirectory() method */
613 QPAK_isSymLink, /* isSymLink() method */
614 QPAK_getLastModTime, /* getLastModTime() method */
615 QPAK_openRead, /* openRead() method */
616 QPAK_openWrite, /* openWrite() method */
617 QPAK_openAppend, /* openAppend() method */
618 QPAK_remove, /* remove() method */
619 QPAK_mkdir, /* mkdir() method */
620 QPAK_dirClose, /* dirClose() method */
621 QPAK_read, /* read() method */
622 QPAK_write, /* write() method */
623 QPAK_eof, /* eof() method */
624 QPAK_tell, /* tell() method */
625 QPAK_seek, /* seek() method */
626 QPAK_fileLength, /* fileLength() method */
627 QPAK_fileClose /* fileClose() method */
630 #endif /* defined PHYSFS_SUPPORTS_QPAK */
632 /* end of qpak.c ... */