1
0

qpak.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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_platformStricmp(x, y)
  40. #define QPAK_strncmp(x, y, z) __PHYSFS_platformStrnicmp(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 = alloca(ln + 1);
  300. if (newstr == NULL)
  301. return;
  302. memcpy(newstr, str, ln);
  303. newstr[ln] = '\0';
  304. cb(callbackdata, odir, newstr);
  305. } /* doEnumCallback */
  306. static void QPAK_enumerateFiles(dvoid *opaque, const char *dname,
  307. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  308. const char *origdir, void *callbackdata)
  309. {
  310. QPAKinfo *info = ((QPAKinfo *) opaque);
  311. PHYSFS_sint32 dlen, dlen_inc, max, i;
  312. i = qpak_find_start_of_dir(info, dname, 0);
  313. if (i == -1) /* no such directory. */
  314. return;
  315. dlen = strlen(dname);
  316. if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
  317. dlen--;
  318. dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
  319. max = (PHYSFS_sint32) info->entryCount;
  320. while (i < max)
  321. {
  322. char *add;
  323. char *ptr;
  324. PHYSFS_sint32 ln;
  325. char *e = info->entries[i].name;
  326. if ((dlen) && ((QPAK_strncmp(e, dname, dlen)) || (e[dlen] != '/')))
  327. break; /* past end of this dir; we're done. */
  328. add = e + dlen_inc;
  329. ptr = strchr(add, '/');
  330. ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
  331. doEnumCallback(cb, callbackdata, origdir, add, ln);
  332. ln += dlen_inc; /* point past entry to children... */
  333. /* increment counter and skip children of subdirs... */
  334. while ((++i < max) && (ptr != NULL))
  335. {
  336. char *e_new = info->entries[i].name;
  337. if ((QPAK_strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
  338. break;
  339. } /* while */
  340. } /* while */
  341. } /* QPAK_enumerateFiles */
  342. /*
  343. * This will find the QPAKentry associated with a path in platform-independent
  344. * notation. Directories don't have QPAKentries associated with them, but
  345. * (*isDir) will be set to non-zero if a dir was hit.
  346. */
  347. static QPAKentry *qpak_find_entry(QPAKinfo *info, const char *path, int *isDir)
  348. {
  349. QPAKentry *a = info->entries;
  350. PHYSFS_sint32 pathlen = strlen(path);
  351. PHYSFS_sint32 lo = 0;
  352. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  353. PHYSFS_sint32 middle;
  354. const char *thispath = NULL;
  355. int rc;
  356. while (lo <= hi)
  357. {
  358. middle = lo + ((hi - lo) / 2);
  359. thispath = a[middle].name;
  360. rc = QPAK_strncmp(path, thispath, pathlen);
  361. if (rc > 0)
  362. lo = middle + 1;
  363. else if (rc < 0)
  364. hi = middle - 1;
  365. else /* substring match...might be dir or entry or nothing. */
  366. {
  367. if (isDir != NULL)
  368. {
  369. *isDir = (thispath[pathlen] == '/');
  370. if (*isDir)
  371. return(NULL);
  372. } /* if */
  373. if (thispath[pathlen] == '\0') /* found entry? */
  374. return(&a[middle]);
  375. else
  376. hi = middle - 1; /* adjust search params, try again. */
  377. } /* if */
  378. } /* while */
  379. if (isDir != NULL)
  380. *isDir = 0;
  381. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  382. } /* qpak_find_entry */
  383. static int QPAK_exists(dvoid *opaque, const char *name)
  384. {
  385. int isDir;
  386. QPAKinfo *info = (QPAKinfo *) opaque;
  387. QPAKentry *entry = qpak_find_entry(info, name, &isDir);
  388. return((entry != NULL) || (isDir));
  389. } /* QPAK_exists */
  390. static int QPAK_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  391. {
  392. QPAKinfo *info = (QPAKinfo *) opaque;
  393. int isDir;
  394. QPAKentry *entry = qpak_find_entry(info, name, &isDir);
  395. *fileExists = ((isDir) || (entry != NULL));
  396. if (isDir)
  397. return(1); /* definitely a dir. */
  398. BAIL_MACRO(ERR_NO_SUCH_FILE, 0);
  399. } /* QPAK_isDirectory */
  400. static int QPAK_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  401. {
  402. *fileExists = QPAK_exists(opaque, name);
  403. return(0); /* never symlinks in a quake pak. */
  404. } /* QPAK_isSymLink */
  405. static PHYSFS_sint64 QPAK_getLastModTime(dvoid *opaque,
  406. const char *name,
  407. int *fileExists)
  408. {
  409. int isDir;
  410. QPAKinfo *info = ((QPAKinfo *) opaque);
  411. PHYSFS_sint64 retval = -1;
  412. QPAKentry *entry = qpak_find_entry(info, name, &isDir);
  413. *fileExists = ((isDir) || (entry != NULL));
  414. if (*fileExists) /* use time of QPAK itself in the physical filesystem. */
  415. retval = info->last_mod_time;
  416. return(retval);
  417. } /* QPAK_getLastModTime */
  418. static fvoid *QPAK_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  419. {
  420. QPAKinfo *info = ((QPAKinfo *) opaque);
  421. QPAKfileinfo *finfo;
  422. QPAKentry *entry;
  423. int isDir;
  424. entry = qpak_find_entry(info, fnm, &isDir);
  425. *fileExists = ((entry != NULL) || (isDir));
  426. BAIL_IF_MACRO(isDir, ERR_NOT_A_FILE, NULL);
  427. BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
  428. finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
  429. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  430. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  431. if ( (finfo->handle == NULL) ||
  432. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  433. {
  434. allocator.Free(finfo);
  435. return(NULL);
  436. } /* if */
  437. finfo->curPos = 0;
  438. finfo->entry = entry;
  439. return(finfo);
  440. } /* QPAK_openRead */
  441. static fvoid *QPAK_openWrite(dvoid *opaque, const char *name)
  442. {
  443. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  444. } /* QPAK_openWrite */
  445. static fvoid *QPAK_openAppend(dvoid *opaque, const char *name)
  446. {
  447. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  448. } /* QPAK_openAppend */
  449. static int QPAK_remove(dvoid *opaque, const char *name)
  450. {
  451. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  452. } /* QPAK_remove */
  453. static int QPAK_mkdir(dvoid *opaque, const char *name)
  454. {
  455. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  456. } /* QPAK_mkdir */
  457. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
  458. {
  459. "PAK",
  460. QPAK_ARCHIVE_DESCRIPTION,
  461. "Ryan C. Gordon <icculus@icculus.org>",
  462. "http://icculus.org/physfs/",
  463. };
  464. const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
  465. {
  466. &__PHYSFS_ArchiveInfo_QPAK,
  467. QPAK_isArchive, /* isArchive() method */
  468. QPAK_openArchive, /* openArchive() method */
  469. QPAK_enumerateFiles, /* enumerateFiles() method */
  470. QPAK_exists, /* exists() method */
  471. QPAK_isDirectory, /* isDirectory() method */
  472. QPAK_isSymLink, /* isSymLink() method */
  473. QPAK_getLastModTime, /* getLastModTime() method */
  474. QPAK_openRead, /* openRead() method */
  475. QPAK_openWrite, /* openWrite() method */
  476. QPAK_openAppend, /* openAppend() method */
  477. QPAK_remove, /* remove() method */
  478. QPAK_mkdir, /* mkdir() method */
  479. QPAK_dirClose, /* dirClose() method */
  480. QPAK_read, /* read() method */
  481. QPAK_write, /* write() method */
  482. QPAK_eof, /* eof() method */
  483. QPAK_tell, /* tell() method */
  484. QPAK_seek, /* seek() method */
  485. QPAK_fileLength, /* fileLength() method */
  486. QPAK_fileClose /* fileClose() method */
  487. };
  488. #endif /* defined PHYSFS_SUPPORTS_QPAK */
  489. /* end of qpak.c ... */