qpak.c 17 KB

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