archiver_qpak.c 15 KB

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