qpak.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. if (one != two)
  170. {
  171. const QPAKentry *a = (const QPAKentry *) _a;
  172. return(QPAK_strcmp(a[one].name, a[two].name));
  173. } /* if */
  174. return 0;
  175. } /* qpak_entry_cmp */
  176. static void qpak_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  177. {
  178. if (one != two)
  179. {
  180. QPAKentry tmp;
  181. QPAKentry *first = &(((QPAKentry *) _a)[one]);
  182. QPAKentry *second = &(((QPAKentry *) _a)[two]);
  183. memcpy(&tmp, first, sizeof (QPAKentry));
  184. memcpy(first, second, sizeof (QPAKentry));
  185. memcpy(second, &tmp, sizeof (QPAKentry));
  186. } /* if */
  187. } /* qpak_entry_swap */
  188. static int qpak_load_entries(const char *name, int forWriting, QPAKinfo *info)
  189. {
  190. void *fh = NULL;
  191. PHYSFS_uint32 fileCount;
  192. QPAKentry *entry;
  193. BAIL_IF_MACRO(!qpak_open(name, forWriting, &fh, &fileCount), NULL, 0);
  194. info->entryCount = fileCount;
  195. info->entries = (QPAKentry*) allocator.Malloc(sizeof(QPAKentry)*fileCount);
  196. if (info->entries == NULL)
  197. {
  198. __PHYSFS_platformClose(fh);
  199. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  200. } /* if */
  201. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  202. {
  203. PHYSFS_uint32 loc;
  204. if (__PHYSFS_platformRead(fh,&entry->name,sizeof(entry->name),1) != 1)
  205. {
  206. __PHYSFS_platformClose(fh);
  207. return(0);
  208. } /* if */
  209. if (__PHYSFS_platformRead(fh,&loc,sizeof(loc),1) != 1)
  210. {
  211. __PHYSFS_platformClose(fh);
  212. return(0);
  213. } /* if */
  214. if (__PHYSFS_platformRead(fh,&entry->size,sizeof(entry->size),1) != 1)
  215. {
  216. __PHYSFS_platformClose(fh);
  217. return(0);
  218. } /* if */
  219. entry->size = PHYSFS_swapULE32(entry->size);
  220. entry->startPos = PHYSFS_swapULE32(loc);
  221. } /* for */
  222. __PHYSFS_platformClose(fh);
  223. __PHYSFS_sort(info->entries, info->entryCount,
  224. qpak_entry_cmp, qpak_entry_swap);
  225. return(1);
  226. } /* qpak_load_entries */
  227. static void *QPAK_openArchive(const char *name, int forWriting)
  228. {
  229. QPAKinfo *info = (QPAKinfo *) allocator.Malloc(sizeof (QPAKinfo));
  230. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  231. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
  232. memset(info, '\0', sizeof (QPAKinfo));
  233. info->filename = (char *) allocator.Malloc(strlen(name) + 1);
  234. if (info->filename == NULL)
  235. {
  236. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  237. goto QPAK_openArchive_failed;
  238. } /* if */
  239. if (!qpak_load_entries(name, forWriting, info))
  240. goto QPAK_openArchive_failed;
  241. strcpy(info->filename, name);
  242. info->last_mod_time = modtime;
  243. return(info);
  244. QPAK_openArchive_failed:
  245. if (info != NULL)
  246. {
  247. if (info->filename != NULL)
  248. allocator.Free(info->filename);
  249. if (info->entries != NULL)
  250. allocator.Free(info->entries);
  251. allocator.Free(info);
  252. } /* if */
  253. return(NULL);
  254. } /* QPAK_openArchive */
  255. static PHYSFS_sint32 qpak_find_start_of_dir(QPAKinfo *info, const char *path,
  256. int stop_on_first_find)
  257. {
  258. PHYSFS_sint32 lo = 0;
  259. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  260. PHYSFS_sint32 middle;
  261. PHYSFS_uint32 dlen = strlen(path);
  262. PHYSFS_sint32 retval = -1;
  263. const char *name;
  264. int rc;
  265. if (*path == '\0') /* root dir? */
  266. return(0);
  267. if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
  268. dlen--;
  269. while (lo <= hi)
  270. {
  271. middle = lo + ((hi - lo) / 2);
  272. name = info->entries[middle].name;
  273. rc = QPAK_strncmp(path, name, dlen);
  274. if (rc == 0)
  275. {
  276. char ch = name[dlen];
  277. if (ch < '/') /* make sure this isn't just a substr match. */
  278. rc = -1;
  279. else if (ch > '/')
  280. rc = 1;
  281. else
  282. {
  283. if (stop_on_first_find) /* Just checking dir's existance? */
  284. return(middle);
  285. if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
  286. return(middle + 1);
  287. /* there might be more entries earlier in the list. */
  288. retval = middle;
  289. hi = middle - 1;
  290. } /* else */
  291. } /* if */
  292. if (rc > 0)
  293. lo = middle + 1;
  294. else
  295. hi = middle - 1;
  296. } /* while */
  297. return(retval);
  298. } /* qpak_find_start_of_dir */
  299. /*
  300. * Moved to seperate function so we can use alloca then immediately throw
  301. * away the allocated stack space...
  302. */
  303. static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
  304. const char *odir, const char *str, PHYSFS_sint32 ln)
  305. {
  306. char *newstr = __PHYSFS_smallAlloc(ln + 1);
  307. if (newstr == NULL)
  308. return;
  309. memcpy(newstr, str, ln);
  310. newstr[ln] = '\0';
  311. cb(callbackdata, odir, newstr);
  312. __PHYSFS_smallFree(newstr);
  313. } /* doEnumCallback */
  314. static void QPAK_enumerateFiles(dvoid *opaque, const char *dname,
  315. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  316. const char *origdir, void *callbackdata)
  317. {
  318. QPAKinfo *info = ((QPAKinfo *) opaque);
  319. PHYSFS_sint32 dlen, dlen_inc, max, i;
  320. i = qpak_find_start_of_dir(info, dname, 0);
  321. if (i == -1) /* no such directory. */
  322. return;
  323. dlen = strlen(dname);
  324. if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
  325. dlen--;
  326. dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
  327. max = (PHYSFS_sint32) info->entryCount;
  328. while (i < max)
  329. {
  330. char *add;
  331. char *ptr;
  332. PHYSFS_sint32 ln;
  333. char *e = info->entries[i].name;
  334. if ((dlen) && ((QPAK_strncmp(e, dname, dlen)) || (e[dlen] != '/')))
  335. break; /* past end of this dir; we're done. */
  336. add = e + dlen_inc;
  337. ptr = strchr(add, '/');
  338. ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
  339. doEnumCallback(cb, callbackdata, origdir, add, ln);
  340. ln += dlen_inc; /* point past entry to children... */
  341. /* increment counter and skip children of subdirs... */
  342. while ((++i < max) && (ptr != NULL))
  343. {
  344. char *e_new = info->entries[i].name;
  345. if ((QPAK_strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
  346. break;
  347. } /* while */
  348. } /* while */
  349. } /* QPAK_enumerateFiles */
  350. /*
  351. * This will find the QPAKentry associated with a path in platform-independent
  352. * notation. Directories don't have QPAKentries associated with them, but
  353. * (*isDir) will be set to non-zero if a dir was hit.
  354. */
  355. static QPAKentry *qpak_find_entry(QPAKinfo *info, const char *path, int *isDir)
  356. {
  357. QPAKentry *a = info->entries;
  358. PHYSFS_sint32 pathlen = strlen(path);
  359. PHYSFS_sint32 lo = 0;
  360. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  361. PHYSFS_sint32 middle;
  362. const char *thispath = NULL;
  363. int rc;
  364. while (lo <= hi)
  365. {
  366. middle = lo + ((hi - lo) / 2);
  367. thispath = a[middle].name;
  368. rc = QPAK_strncmp(path, thispath, pathlen);
  369. if (rc > 0)
  370. lo = middle + 1;
  371. else if (rc < 0)
  372. hi = middle - 1;
  373. else /* substring match...might be dir or entry or nothing. */
  374. {
  375. if (isDir != NULL)
  376. {
  377. *isDir = (thispath[pathlen] == '/');
  378. if (*isDir)
  379. return(NULL);
  380. } /* if */
  381. if (thispath[pathlen] == '\0') /* found entry? */
  382. return(&a[middle]);
  383. /* adjust search params, try again. */
  384. else if (thispath[pathlen] > '/')
  385. hi = middle - 1;
  386. else
  387. lo = middle + 1;
  388. } /* if */
  389. } /* while */
  390. if (isDir != NULL)
  391. *isDir = 0;
  392. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  393. } /* qpak_find_entry */
  394. static int QPAK_exists(dvoid *opaque, const char *name)
  395. {
  396. int isDir;
  397. QPAKinfo *info = (QPAKinfo *) opaque;
  398. QPAKentry *entry = qpak_find_entry(info, name, &isDir);
  399. return((entry != NULL) || (isDir));
  400. } /* QPAK_exists */
  401. static int QPAK_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  402. {
  403. QPAKinfo *info = (QPAKinfo *) opaque;
  404. int isDir;
  405. QPAKentry *entry = qpak_find_entry(info, name, &isDir);
  406. *fileExists = ((isDir) || (entry != NULL));
  407. if (isDir)
  408. return(1); /* definitely a dir. */
  409. BAIL_MACRO(ERR_NO_SUCH_FILE, 0);
  410. } /* QPAK_isDirectory */
  411. static int QPAK_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  412. {
  413. *fileExists = QPAK_exists(opaque, name);
  414. return(0); /* never symlinks in a quake pak. */
  415. } /* QPAK_isSymLink */
  416. static PHYSFS_sint64 QPAK_getLastModTime(dvoid *opaque,
  417. const char *name,
  418. int *fileExists)
  419. {
  420. int isDir;
  421. QPAKinfo *info = ((QPAKinfo *) opaque);
  422. PHYSFS_sint64 retval = -1;
  423. QPAKentry *entry = qpak_find_entry(info, name, &isDir);
  424. *fileExists = ((isDir) || (entry != NULL));
  425. if (*fileExists) /* use time of QPAK itself in the physical filesystem. */
  426. retval = info->last_mod_time;
  427. return(retval);
  428. } /* QPAK_getLastModTime */
  429. static fvoid *QPAK_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  430. {
  431. QPAKinfo *info = ((QPAKinfo *) opaque);
  432. QPAKfileinfo *finfo;
  433. QPAKentry *entry;
  434. int isDir;
  435. entry = qpak_find_entry(info, fnm, &isDir);
  436. *fileExists = ((entry != NULL) || (isDir));
  437. BAIL_IF_MACRO(isDir, ERR_NOT_A_FILE, NULL);
  438. BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
  439. finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
  440. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  441. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  442. if ( (finfo->handle == NULL) ||
  443. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  444. {
  445. allocator.Free(finfo);
  446. return(NULL);
  447. } /* if */
  448. finfo->curPos = 0;
  449. finfo->entry = entry;
  450. return(finfo);
  451. } /* QPAK_openRead */
  452. static fvoid *QPAK_openWrite(dvoid *opaque, const char *name)
  453. {
  454. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  455. } /* QPAK_openWrite */
  456. static fvoid *QPAK_openAppend(dvoid *opaque, const char *name)
  457. {
  458. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  459. } /* QPAK_openAppend */
  460. static int QPAK_remove(dvoid *opaque, const char *name)
  461. {
  462. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  463. } /* QPAK_remove */
  464. static int QPAK_mkdir(dvoid *opaque, const char *name)
  465. {
  466. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  467. } /* QPAK_mkdir */
  468. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
  469. {
  470. "PAK",
  471. QPAK_ARCHIVE_DESCRIPTION,
  472. "Ryan C. Gordon <icculus@icculus.org>",
  473. "http://icculus.org/physfs/",
  474. };
  475. const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
  476. {
  477. &__PHYSFS_ArchiveInfo_QPAK,
  478. QPAK_isArchive, /* isArchive() method */
  479. QPAK_openArchive, /* openArchive() method */
  480. QPAK_enumerateFiles, /* enumerateFiles() method */
  481. QPAK_exists, /* exists() method */
  482. QPAK_isDirectory, /* isDirectory() method */
  483. QPAK_isSymLink, /* isSymLink() method */
  484. QPAK_getLastModTime, /* getLastModTime() method */
  485. QPAK_openRead, /* openRead() method */
  486. QPAK_openWrite, /* openWrite() method */
  487. QPAK_openAppend, /* openAppend() method */
  488. QPAK_remove, /* remove() method */
  489. QPAK_mkdir, /* mkdir() method */
  490. QPAK_dirClose, /* dirClose() method */
  491. QPAK_read, /* read() method */
  492. QPAK_write, /* write() method */
  493. QPAK_eof, /* eof() method */
  494. QPAK_tell, /* tell() method */
  495. QPAK_seek, /* seek() method */
  496. QPAK_fileLength, /* fileLength() method */
  497. QPAK_fileClose /* fileClose() method */
  498. };
  499. #endif /* defined PHYSFS_SUPPORTS_QPAK */
  500. /* end of qpak.c ... */