qpak.c 17 KB

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