archiver_qpak.c 16 KB

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