archiver_qpak.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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(ERR_NOT_SUPPORTED, -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, 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 == NULL, ERR_OUT_OF_MEMORY, QPAK_duplicate_failed);
  113. GOTO_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, QPAK_duplicate_failed);
  114. io = origfinfo->io->duplicate(origfinfo->io);
  115. GOTO_IF_MACRO(io == NULL, NULL, 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, 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), NULL, 0);
  179. BAIL_IF_MACRO(!__PHYSFS_readAll(io, &entry->startPos, 4), NULL, 0);
  180. BAIL_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), NULL, 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, ERR_ARC_IS_READ_ONLY, 0);
  195. BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), NULL, NULL);
  196. BAIL_IF_MACRO(PHYSFS_swapULE32(val) != QPAK_SIG, ERR_NOT_AN_ARCHIVE, NULL);
  197. BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), NULL, NULL);
  198. pos = PHYSFS_swapULE32(val); /* directory table offset. */
  199. BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), NULL, NULL);
  200. count = PHYSFS_swapULE32(val);
  201. /* corrupted archive? */
  202. BAIL_IF_MACRO((count % 64) != 0, ERR_CORRUPTED, NULL);
  203. count /= 64;
  204. BAIL_IF_MACRO(!io->seek(io, pos), NULL, NULL);
  205. info = (QPAKinfo *) allocator.Malloc(sizeof (QPAKinfo));
  206. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
  207. memset(info, '\0', sizeof (QPAKinfo));
  208. info->io = io;
  209. info->entryCount = count;
  210. if (!qpak_load_entries(info))
  211. {
  212. if (info->entries != NULL)
  213. allocator.Free(info->entries);
  214. allocator.Free(info);
  215. return NULL;
  216. } /* if */
  217. return info;
  218. } /* QPAK_openArchive */
  219. static PHYSFS_sint32 qpak_find_start_of_dir(QPAKinfo *info, const char *path,
  220. int stop_on_first_find)
  221. {
  222. PHYSFS_sint32 lo = 0;
  223. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  224. PHYSFS_sint32 middle;
  225. PHYSFS_uint32 dlen = strlen(path);
  226. PHYSFS_sint32 retval = -1;
  227. const char *name;
  228. int rc;
  229. if (*path == '\0') /* root dir? */
  230. return 0;
  231. if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
  232. dlen--;
  233. while (lo <= hi)
  234. {
  235. middle = lo + ((hi - lo) / 2);
  236. name = info->entries[middle].name;
  237. rc = QPAK_strncmp(path, name, dlen);
  238. if (rc == 0)
  239. {
  240. char ch = name[dlen];
  241. if (ch < '/') /* make sure this isn't just a substr match. */
  242. rc = -1;
  243. else if (ch > '/')
  244. rc = 1;
  245. else
  246. {
  247. if (stop_on_first_find) /* Just checking dir's existance? */
  248. return middle;
  249. if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
  250. return (middle + 1);
  251. /* there might be more entries earlier in the list. */
  252. retval = middle;
  253. hi = middle - 1;
  254. } /* else */
  255. } /* if */
  256. if (rc > 0)
  257. lo = middle + 1;
  258. else
  259. hi = middle - 1;
  260. } /* while */
  261. return retval;
  262. } /* qpak_find_start_of_dir */
  263. /*
  264. * Moved to seperate function so we can use alloca then immediately throw
  265. * away the allocated stack space...
  266. */
  267. static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
  268. const char *odir, const char *str, PHYSFS_sint32 ln)
  269. {
  270. char *newstr = __PHYSFS_smallAlloc(ln + 1);
  271. if (newstr == NULL)
  272. return;
  273. memcpy(newstr, str, ln);
  274. newstr[ln] = '\0';
  275. cb(callbackdata, odir, newstr);
  276. __PHYSFS_smallFree(newstr);
  277. } /* doEnumCallback */
  278. static void QPAK_enumerateFiles(dvoid *opaque, const char *dname,
  279. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  280. const char *origdir, void *callbackdata)
  281. {
  282. QPAKinfo *info = ((QPAKinfo *) opaque);
  283. PHYSFS_sint32 dlen, dlen_inc, max, i;
  284. i = qpak_find_start_of_dir(info, dname, 0);
  285. if (i == -1) /* no such directory. */
  286. return;
  287. dlen = strlen(dname);
  288. if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
  289. dlen--;
  290. dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
  291. max = (PHYSFS_sint32) info->entryCount;
  292. while (i < max)
  293. {
  294. char *add;
  295. char *ptr;
  296. PHYSFS_sint32 ln;
  297. char *e = info->entries[i].name;
  298. if ((dlen) && ((QPAK_strncmp(e, dname, dlen)) || (e[dlen] != '/')))
  299. break; /* past end of this dir; we're done. */
  300. add = e + dlen_inc;
  301. ptr = strchr(add, '/');
  302. ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
  303. doEnumCallback(cb, callbackdata, origdir, add, ln);
  304. ln += dlen_inc; /* point past entry to children... */
  305. /* increment counter and skip children of subdirs... */
  306. while ((++i < max) && (ptr != NULL))
  307. {
  308. char *e_new = info->entries[i].name;
  309. if ((QPAK_strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
  310. break;
  311. } /* while */
  312. } /* while */
  313. } /* QPAK_enumerateFiles */
  314. /*
  315. * This will find the QPAKentry associated with a path in platform-independent
  316. * notation. Directories don't have QPAKentries associated with them, but
  317. * (*isDir) will be set to non-zero if a dir was hit.
  318. */
  319. static QPAKentry *qpak_find_entry(const QPAKinfo *info, const char *path,
  320. int *isDir)
  321. {
  322. QPAKentry *a = info->entries;
  323. PHYSFS_sint32 pathlen = strlen(path);
  324. PHYSFS_sint32 lo = 0;
  325. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  326. PHYSFS_sint32 middle;
  327. const char *thispath = NULL;
  328. int rc;
  329. while (lo <= hi)
  330. {
  331. middle = lo + ((hi - lo) / 2);
  332. thispath = a[middle].name;
  333. rc = QPAK_strncmp(path, thispath, pathlen);
  334. if (rc > 0)
  335. lo = middle + 1;
  336. else if (rc < 0)
  337. hi = middle - 1;
  338. else /* substring match...might be dir or entry or nothing. */
  339. {
  340. if (isDir != NULL)
  341. {
  342. *isDir = (thispath[pathlen] == '/');
  343. if (*isDir)
  344. return NULL;
  345. } /* if */
  346. if (thispath[pathlen] == '\0') /* found entry? */
  347. return &a[middle];
  348. /* adjust search params, try again. */
  349. else if (thispath[pathlen] > '/')
  350. hi = middle - 1;
  351. else
  352. lo = middle + 1;
  353. } /* if */
  354. } /* while */
  355. if (isDir != NULL)
  356. *isDir = 0;
  357. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  358. } /* qpak_find_entry */
  359. static PHYSFS_Io *QPAK_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  360. {
  361. PHYSFS_Io *io = NULL;
  362. QPAKinfo *info = ((QPAKinfo *) opaque);
  363. QPAKfileinfo *finfo;
  364. QPAKentry *entry;
  365. int isDir;
  366. entry = qpak_find_entry(info, fnm, &isDir);
  367. *fileExists = ((entry != NULL) || (isDir));
  368. BAIL_IF_MACRO(isDir, ERR_NOT_A_FILE, NULL);
  369. BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
  370. finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
  371. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  372. finfo->io = info->io->duplicate(info->io);
  373. GOTO_IF_MACRO(finfo->io == NULL, NULL, QPAK_openRead_failed);
  374. if (!finfo->io->seek(finfo->io, entry->startPos))
  375. GOTO_MACRO(NULL, QPAK_openRead_failed);
  376. finfo->curPos = 0;
  377. finfo->entry = entry;
  378. io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  379. GOTO_IF_MACRO(io == NULL, ERR_OUT_OF_MEMORY, QPAK_openRead_failed);
  380. memcpy(io, &QPAK_Io, sizeof (PHYSFS_Io));
  381. io->opaque = finfo;
  382. return io;
  383. QPAK_openRead_failed:
  384. if (finfo != NULL)
  385. {
  386. if (finfo->io != NULL)
  387. finfo->io->destroy(finfo->io);
  388. allocator.Free(finfo);
  389. } /* if */
  390. if (io != NULL)
  391. allocator.Free(io);
  392. return NULL;
  393. } /* QPAK_openRead */
  394. static PHYSFS_Io *QPAK_openWrite(dvoid *opaque, const char *name)
  395. {
  396. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  397. } /* QPAK_openWrite */
  398. static PHYSFS_Io *QPAK_openAppend(dvoid *opaque, const char *name)
  399. {
  400. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  401. } /* QPAK_openAppend */
  402. static int QPAK_remove(dvoid *opaque, const char *name)
  403. {
  404. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  405. } /* QPAK_remove */
  406. static int QPAK_mkdir(dvoid *opaque, const char *name)
  407. {
  408. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  409. } /* QPAK_mkdir */
  410. static int QPAK_stat(dvoid *opaque, const char *filename, int *exists,
  411. PHYSFS_Stat *stat)
  412. {
  413. int isDir = 0;
  414. const QPAKinfo *info = (const QPAKinfo *) opaque;
  415. const QPAKentry *entry = qpak_find_entry(info, filename, &isDir);
  416. if (isDir)
  417. {
  418. *exists = 1;
  419. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  420. stat->filesize = 0;
  421. } /* if */
  422. else if (entry != NULL)
  423. {
  424. *exists = 1;
  425. stat->filetype = PHYSFS_FILETYPE_REGULAR;
  426. stat->filesize = entry->size;
  427. } /* else if */
  428. else
  429. {
  430. *exists = 0;
  431. return 0;
  432. } /* else */
  433. stat->modtime = -1;
  434. stat->createtime = -1;
  435. stat->accesstime = -1;
  436. stat->readonly = 1;
  437. return 1;
  438. } /* QPAK_stat */
  439. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
  440. {
  441. "PAK",
  442. QPAK_ARCHIVE_DESCRIPTION,
  443. "Ryan C. Gordon <icculus@icculus.org>",
  444. "http://icculus.org/physfs/",
  445. };
  446. const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
  447. {
  448. &__PHYSFS_ArchiveInfo_QPAK,
  449. QPAK_openArchive, /* openArchive() method */
  450. QPAK_enumerateFiles, /* enumerateFiles() method */
  451. QPAK_openRead, /* openRead() method */
  452. QPAK_openWrite, /* openWrite() method */
  453. QPAK_openAppend, /* openAppend() method */
  454. QPAK_remove, /* remove() method */
  455. QPAK_mkdir, /* mkdir() method */
  456. QPAK_dirClose, /* dirClose() method */
  457. QPAK_stat /* stat() method */
  458. };
  459. #endif /* defined PHYSFS_SUPPORTS_QPAK */
  460. /* end of qpak.c ... */