qpak.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * QPAK support routines for PhysicsFS.
  3. *
  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
  6. * archiver handles).
  7. *
  8. * ========================================================================
  9. *
  10. * This format info (in more detail) comes from:
  11. * http://debian.fmi.uni-sofia.bg/~sergei/cgsr/docs/pak.txt
  12. *
  13. * Quake PAK Format
  14. *
  15. * Header
  16. * (4 bytes) signature = 'PACK'
  17. * (4 bytes) directory offset
  18. * (4 bytes) directory length
  19. *
  20. * Directory
  21. * (56 bytes) file name
  22. * (4 bytes) file position
  23. * (4 bytes) file length
  24. *
  25. * ========================================================================
  26. *
  27. * Please see the file LICENSE.txt in the source's root directory.
  28. *
  29. * This file written by Ryan C. Gordon.
  30. */
  31. #if (defined PHYSFS_SUPPORTS_QPAK)
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include "physfs.h"
  36. #define __PHYSICSFS_INTERNAL__
  37. #include "physfs_internal.h"
  38. #if 1 /* Make this case insensitive? */
  39. #define QPAK_strcmp(x, y) __PHYSFS_stricmpASCII(x, y)
  40. #define QPAK_strncmp(x, y, z) __PHYSFS_strnicmpASCII(x, y, z)
  41. #else
  42. #define QPAK_strcmp(x, y) strcmp(x, y)
  43. #define QPAK_strncmp(x, y, z) strncmp(x, y, z)
  44. #endif
  45. typedef struct
  46. {
  47. char name[56];
  48. PHYSFS_uint32 startPos;
  49. PHYSFS_uint32 size;
  50. } QPAKentry;
  51. typedef struct
  52. {
  53. char *filename;
  54. PHYSFS_sint64 last_mod_time;
  55. PHYSFS_uint32 entryCount;
  56. QPAKentry *entries;
  57. } QPAKinfo;
  58. typedef struct
  59. {
  60. void *handle;
  61. QPAKentry *entry;
  62. PHYSFS_uint32 curPos;
  63. } QPAKfileinfo;
  64. /* Magic numbers... */
  65. #define QPAK_SIG 0x4b434150 /* "PACK" in ASCII. */
  66. static void QPAK_dirClose(dvoid *opaque)
  67. {
  68. QPAKinfo *info = ((QPAKinfo *) opaque);
  69. allocator.Free(info->filename);
  70. allocator.Free(info->entries);
  71. allocator.Free(info);
  72. } /* QPAK_dirClose */
  73. static PHYSFS_sint64 QPAK_read(fvoid *opaque, void *buffer,
  74. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  75. {
  76. QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
  77. QPAKentry *entry = finfo->entry;
  78. PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
  79. PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
  80. PHYSFS_sint64 rc;
  81. if (objsLeft < objCount)
  82. objCount = objsLeft;
  83. rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  84. if (rc > 0)
  85. finfo->curPos += (PHYSFS_uint32) (rc * objSize);
  86. return(rc);
  87. } /* QPAK_read */
  88. static PHYSFS_sint64 QPAK_write(fvoid *opaque, const void *buffer,
  89. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  90. {
  91. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  92. } /* QPAK_write */
  93. static int QPAK_eof(fvoid *opaque)
  94. {
  95. QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
  96. QPAKentry *entry = finfo->entry;
  97. return(finfo->curPos >= entry->size);
  98. } /* QPAK_eof */
  99. static PHYSFS_sint64 QPAK_tell(fvoid *opaque)
  100. {
  101. return(((QPAKfileinfo *) opaque)->curPos);
  102. } /* QPAK_tell */
  103. static int QPAK_seek(fvoid *opaque, PHYSFS_uint64 offset)
  104. {
  105. QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
  106. QPAKentry *entry = finfo->entry;
  107. int rc;
  108. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  109. BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
  110. rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
  111. if (rc)
  112. finfo->curPos = (PHYSFS_uint32) offset;
  113. return(rc);
  114. } /* QPAK_seek */
  115. static PHYSFS_sint64 QPAK_fileLength(fvoid *opaque)
  116. {
  117. QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
  118. return((PHYSFS_sint64) finfo->entry->size);
  119. } /* QPAK_fileLength */
  120. static int QPAK_fileClose(fvoid *opaque)
  121. {
  122. QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
  123. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  124. allocator.Free(finfo);
  125. return(1);
  126. } /* QPAK_fileClose */
  127. static int qpak_open(const char *filename, int forWriting,
  128. void **fh, PHYSFS_uint32 *count)
  129. {
  130. PHYSFS_uint32 buf;
  131. *fh = NULL;
  132. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  133. *fh = __PHYSFS_platformOpenRead(filename);
  134. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  135. if (__PHYSFS_platformRead(*fh, &buf, sizeof (PHYSFS_uint32), 1) != 1)
  136. goto openQpak_failed;
  137. buf = PHYSFS_swapULE32(buf);
  138. GOTO_IF_MACRO(buf != QPAK_SIG, ERR_UNSUPPORTED_ARCHIVE, openQpak_failed);
  139. if (__PHYSFS_platformRead(*fh, &buf, sizeof (PHYSFS_uint32), 1) != 1)
  140. goto openQpak_failed;
  141. buf = PHYSFS_swapULE32(buf); /* directory table offset. */
  142. if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
  143. goto openQpak_failed;
  144. *count = PHYSFS_swapULE32(*count);
  145. /* corrupted archive? */
  146. GOTO_IF_MACRO((*count % 64) != 0, ERR_CORRUPTED, openQpak_failed);
  147. if (!__PHYSFS_platformSeek(*fh, buf))
  148. goto openQpak_failed;
  149. *count /= 64;
  150. return(1);
  151. openQpak_failed:
  152. if (*fh != NULL)
  153. __PHYSFS_platformClose(*fh);
  154. *count = -1;
  155. *fh = NULL;
  156. return(0);
  157. } /* qpak_open */
  158. static int QPAK_isArchive(const char *filename, int forWriting)
  159. {
  160. void *fh;
  161. PHYSFS_uint32 fileCount;
  162. int retval = qpak_open(filename, forWriting, &fh, &fileCount);
  163. if (fh != NULL)
  164. __PHYSFS_platformClose(fh);
  165. return(retval);
  166. } /* QPAK_isArchive */
  167. static int qpak_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  168. {
  169. QPAKentry *a = (QPAKentry *) _a;
  170. return(QPAK_strcmp(a[one].name, a[two].name));
  171. } /* qpak_entry_cmp */
  172. static void qpak_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  173. {
  174. QPAKentry tmp;
  175. QPAKentry *first = &(((QPAKentry *) _a)[one]);
  176. QPAKentry *second = &(((QPAKentry *) _a)[two]);
  177. memcpy(&tmp, first, sizeof (QPAKentry));
  178. memcpy(first, second, sizeof (QPAKentry));
  179. memcpy(second, &tmp, sizeof (QPAKentry));
  180. } /* qpak_entry_swap */
  181. static int qpak_load_entries(const char *name, int forWriting, QPAKinfo *info)
  182. {
  183. void *fh = NULL;
  184. PHYSFS_uint32 fileCount;
  185. QPAKentry *entry;
  186. BAIL_IF_MACRO(!qpak_open(name, forWriting, &fh, &fileCount), NULL, 0);
  187. info->entryCount = fileCount;
  188. info->entries = (QPAKentry*) allocator.Malloc(sizeof(QPAKentry)*fileCount);
  189. if (info->entries == NULL)
  190. {
  191. __PHYSFS_platformClose(fh);
  192. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  193. } /* if */
  194. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  195. {
  196. PHYSFS_uint32 loc;
  197. if (__PHYSFS_platformRead(fh,&entry->name,sizeof(entry->name),1) != 1)
  198. {
  199. __PHYSFS_platformClose(fh);
  200. return(0);
  201. } /* if */
  202. if (__PHYSFS_platformRead(fh,&loc,sizeof(loc),1) != 1)
  203. {
  204. __PHYSFS_platformClose(fh);
  205. return(0);
  206. } /* if */
  207. if (__PHYSFS_platformRead(fh,&entry->size,sizeof(entry->size),1) != 1)
  208. {
  209. __PHYSFS_platformClose(fh);
  210. return(0);
  211. } /* if */
  212. entry->size = PHYSFS_swapULE32(entry->size);
  213. entry->startPos = PHYSFS_swapULE32(loc);
  214. } /* for */
  215. __PHYSFS_platformClose(fh);
  216. __PHYSFS_sort(info->entries, info->entryCount,
  217. qpak_entry_cmp, qpak_entry_swap);
  218. return(1);
  219. } /* qpak_load_entries */
  220. static void *QPAK_openArchive(const char *name, int forWriting)
  221. {
  222. QPAKinfo *info = (QPAKinfo *) allocator.Malloc(sizeof (QPAKinfo));
  223. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  224. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
  225. memset(info, '\0', sizeof (QPAKinfo));
  226. info->filename = (char *) allocator.Malloc(strlen(name) + 1);
  227. if (info->filename == NULL)
  228. {
  229. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  230. goto QPAK_openArchive_failed;
  231. } /* if */
  232. if (!qpak_load_entries(name, forWriting, info))
  233. goto QPAK_openArchive_failed;
  234. strcpy(info->filename, name);
  235. info->last_mod_time = modtime;
  236. return(info);
  237. QPAK_openArchive_failed:
  238. if (info != NULL)
  239. {
  240. if (info->filename != NULL)
  241. allocator.Free(info->filename);
  242. if (info->entries != NULL)
  243. allocator.Free(info->entries);
  244. allocator.Free(info);
  245. } /* if */
  246. return(NULL);
  247. } /* QPAK_openArchive */
  248. static PHYSFS_sint32 qpak_find_start_of_dir(QPAKinfo *info, const char *path,
  249. int stop_on_first_find)
  250. {
  251. PHYSFS_sint32 lo = 0;
  252. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  253. PHYSFS_sint32 middle;
  254. PHYSFS_uint32 dlen = strlen(path);
  255. PHYSFS_sint32 retval = -1;
  256. const char *name;
  257. int rc;
  258. if (*path == '\0') /* root dir? */
  259. return(0);
  260. if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
  261. dlen--;
  262. while (lo <= hi)
  263. {
  264. middle = lo + ((hi - lo) / 2);
  265. name = info->entries[middle].name;
  266. rc = QPAK_strncmp(path, name, dlen);
  267. if (rc == 0)
  268. {
  269. char ch = name[dlen];
  270. if (ch < '/') /* make sure this isn't just a substr match. */
  271. rc = -1;
  272. else if (ch > '/')
  273. rc = 1;
  274. else
  275. {
  276. if (stop_on_first_find) /* Just checking dir's existance? */
  277. return(middle);
  278. if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
  279. return(middle + 1);
  280. /* there might be more entries earlier in the list. */
  281. retval = middle;
  282. hi = middle - 1;
  283. } /* else */
  284. } /* if */
  285. if (rc > 0)
  286. lo = middle + 1;
  287. else
  288. hi = middle - 1;
  289. } /* while */
  290. return(retval);
  291. } /* qpak_find_start_of_dir */
  292. /*
  293. * Moved to seperate function so we can use alloca then immediately throw
  294. * away the allocated stack space...
  295. */
  296. static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
  297. const char *odir, const char *str, PHYSFS_sint32 ln)
  298. {
  299. char *newstr = __PHYSFS_smallAlloc(ln + 1);
  300. if (newstr == NULL)
  301. return;
  302. memcpy(newstr, str, ln);
  303. newstr[ln] = '\0';
  304. cb(callbackdata, odir, newstr);
  305. __PHYSFS_smallFree(newstr);
  306. } /* doEnumCallback */
  307. static void QPAK_enumerateFiles(dvoid *opaque, const char *dname,
  308. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  309. const char *origdir, void *callbackdata)
  310. {
  311. QPAKinfo *info = ((QPAKinfo *) opaque);
  312. PHYSFS_sint32 dlen, dlen_inc, max, i;
  313. i = qpak_find_start_of_dir(info, dname, 0);
  314. if (i == -1) /* no such directory. */
  315. return;
  316. dlen = strlen(dname);
  317. if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
  318. dlen--;
  319. dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
  320. max = (PHYSFS_sint32) info->entryCount;
  321. while (i < max)
  322. {
  323. char *add;
  324. char *ptr;
  325. PHYSFS_sint32 ln;
  326. char *e = info->entries[i].name;
  327. if ((dlen) && ((QPAK_strncmp(e, dname, dlen)) || (e[dlen] != '/')))
  328. break; /* past end of this dir; we're done. */
  329. add = e + dlen_inc;
  330. ptr = strchr(add, '/');
  331. ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
  332. doEnumCallback(cb, callbackdata, origdir, add, ln);
  333. ln += dlen_inc; /* point past entry to children... */
  334. /* increment counter and skip children of subdirs... */
  335. while ((++i < max) && (ptr != NULL))
  336. {
  337. char *e_new = info->entries[i].name;
  338. if ((QPAK_strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
  339. break;
  340. } /* while */
  341. } /* while */
  342. } /* QPAK_enumerateFiles */
  343. /*
  344. * This will find the QPAKentry associated with a path in platform-independent
  345. * notation. Directories don't have QPAKentries associated with them, but
  346. * (*isDir) will be set to non-zero if a dir was hit.
  347. */
  348. static QPAKentry *qpak_find_entry(QPAKinfo *info, const char *path, int *isDir)
  349. {
  350. QPAKentry *a = info->entries;
  351. PHYSFS_sint32 pathlen = strlen(path);
  352. PHYSFS_sint32 lo = 0;
  353. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  354. PHYSFS_sint32 middle;
  355. const char *thispath = NULL;
  356. int rc;
  357. while (lo <= hi)
  358. {
  359. middle = lo + ((hi - lo) / 2);
  360. thispath = a[middle].name;
  361. rc = QPAK_strncmp(path, thispath, pathlen);
  362. if (rc > 0)
  363. lo = middle + 1;
  364. else if (rc < 0)
  365. hi = middle - 1;
  366. else /* substring match...might be dir or entry or nothing. */
  367. {
  368. if (isDir != NULL)
  369. {
  370. *isDir = (thispath[pathlen] == '/');
  371. if (*isDir)
  372. return(NULL);
  373. } /* if */
  374. if (thispath[pathlen] == '\0') /* found entry? */
  375. return(&a[middle]);
  376. else
  377. hi = middle - 1; /* adjust search params, try again. */
  378. } /* if */
  379. } /* while */
  380. if (isDir != NULL)
  381. *isDir = 0;
  382. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  383. } /* qpak_find_entry */
  384. static int QPAK_exists(dvoid *opaque, const char *name)
  385. {
  386. int isDir;
  387. QPAKinfo *info = (QPAKinfo *) opaque;
  388. QPAKentry *entry = qpak_find_entry(info, name, &isDir);
  389. return((entry != NULL) || (isDir));
  390. } /* QPAK_exists */
  391. static int QPAK_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  392. {
  393. QPAKinfo *info = (QPAKinfo *) opaque;
  394. int isDir;
  395. QPAKentry *entry = qpak_find_entry(info, name, &isDir);
  396. *fileExists = ((isDir) || (entry != NULL));
  397. if (isDir)
  398. return(1); /* definitely a dir. */
  399. BAIL_MACRO(ERR_NO_SUCH_FILE, 0);
  400. } /* QPAK_isDirectory */
  401. static int QPAK_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  402. {
  403. *fileExists = QPAK_exists(opaque, name);
  404. return(0); /* never symlinks in a quake pak. */
  405. } /* QPAK_isSymLink */
  406. static PHYSFS_sint64 QPAK_getLastModTime(dvoid *opaque,
  407. const char *name,
  408. int *fileExists)
  409. {
  410. int isDir;
  411. QPAKinfo *info = ((QPAKinfo *) opaque);
  412. PHYSFS_sint64 retval = -1;
  413. QPAKentry *entry = qpak_find_entry(info, name, &isDir);
  414. *fileExists = ((isDir) || (entry != NULL));
  415. if (*fileExists) /* use time of QPAK itself in the physical filesystem. */
  416. retval = info->last_mod_time;
  417. return(retval);
  418. } /* QPAK_getLastModTime */
  419. static fvoid *QPAK_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  420. {
  421. QPAKinfo *info = ((QPAKinfo *) opaque);
  422. QPAKfileinfo *finfo;
  423. QPAKentry *entry;
  424. int isDir;
  425. entry = qpak_find_entry(info, fnm, &isDir);
  426. *fileExists = ((entry != NULL) || (isDir));
  427. BAIL_IF_MACRO(isDir, ERR_NOT_A_FILE, NULL);
  428. BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
  429. finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
  430. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  431. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  432. if ( (finfo->handle == NULL) ||
  433. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  434. {
  435. allocator.Free(finfo);
  436. return(NULL);
  437. } /* if */
  438. finfo->curPos = 0;
  439. finfo->entry = entry;
  440. return(finfo);
  441. } /* QPAK_openRead */
  442. static fvoid *QPAK_openWrite(dvoid *opaque, const char *name)
  443. {
  444. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  445. } /* QPAK_openWrite */
  446. static fvoid *QPAK_openAppend(dvoid *opaque, const char *name)
  447. {
  448. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  449. } /* QPAK_openAppend */
  450. static int QPAK_remove(dvoid *opaque, const char *name)
  451. {
  452. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  453. } /* QPAK_remove */
  454. static int QPAK_mkdir(dvoid *opaque, const char *name)
  455. {
  456. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  457. } /* QPAK_mkdir */
  458. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
  459. {
  460. "PAK",
  461. QPAK_ARCHIVE_DESCRIPTION,
  462. "Ryan C. Gordon <icculus@icculus.org>",
  463. "http://icculus.org/physfs/",
  464. };
  465. const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
  466. {
  467. &__PHYSFS_ArchiveInfo_QPAK,
  468. QPAK_isArchive, /* isArchive() method */
  469. QPAK_openArchive, /* openArchive() method */
  470. QPAK_enumerateFiles, /* enumerateFiles() method */
  471. QPAK_exists, /* exists() method */
  472. QPAK_isDirectory, /* isDirectory() method */
  473. QPAK_isSymLink, /* isSymLink() method */
  474. QPAK_getLastModTime, /* getLastModTime() method */
  475. QPAK_openRead, /* openRead() method */
  476. QPAK_openWrite, /* openWrite() method */
  477. QPAK_openAppend, /* openAppend() method */
  478. QPAK_remove, /* remove() method */
  479. QPAK_mkdir, /* mkdir() method */
  480. QPAK_dirClose, /* dirClose() method */
  481. QPAK_read, /* read() method */
  482. QPAK_write, /* write() method */
  483. QPAK_eof, /* eof() method */
  484. QPAK_tell, /* tell() method */
  485. QPAK_seek, /* seek() method */
  486. QPAK_fileLength, /* fileLength() method */
  487. QPAK_fileClose /* fileClose() method */
  488. };
  489. #endif /* defined PHYSFS_SUPPORTS_QPAK */
  490. /* end of qpak.c ... */