wad.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * WAD support routines for PhysicsFS.
  3. *
  4. * This driver handles DOOM engine archives ("wads").
  5. * This format (but not this driver) was designed by id Software for use
  6. * with the DOOM engine.
  7. * The specs of the format are from the unofficial doom specs v1.666
  8. * found here: http://www.gamers.org/dhs/helpdocs/dmsp1666.html
  9. * The format of the archive: (from the specs)
  10. *
  11. * A WAD file has three parts:
  12. * (1) a twelve-byte header
  13. * (2) one or more "lumps"
  14. * (3) a directory or "info table" that contains the names, offsets, and
  15. * sizes of all the lumps in the WAD
  16. *
  17. * The header consists of three four-byte parts:
  18. * (a) an ASCII string which must be either "IWAD" or "PWAD"
  19. * (b) a 4-byte (long) integer which is the number of lumps in the wad
  20. * (c) a long integer which is the file offset to the start of
  21. * the directory
  22. *
  23. * The directory has one 16-byte entry for every lump. Each entry consists
  24. * of three parts:
  25. *
  26. * (a) a long integer, the file offset to the start of the lump
  27. * (b) a long integer, the size of the lump in bytes
  28. * (c) an 8-byte ASCII string, the name of the lump, padded with zeros.
  29. * For example, the "DEMO1" entry in hexadecimal would be
  30. * (44 45 4D 4F 31 00 00 00)
  31. *
  32. * Note that there is no way to tell if an opened WAD archive is a
  33. * IWAD or PWAD with this archiver.
  34. * I couldn't think of a way to provide that information, without being too
  35. * hacky.
  36. * I don't think it's really that important though.
  37. *
  38. *
  39. * Please see the file LICENSE in the source's root directory.
  40. *
  41. * This file written by Travis Wells, based on the GRP archiver by
  42. * Ryan C. Gordon.
  43. */
  44. #if HAVE_CONFIG_H
  45. # include <config.h>
  46. #endif
  47. #if (defined PHYSFS_SUPPORTS_WAD)
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include "physfs.h"
  52. #define __PHYSICSFS_INTERNAL__
  53. #include "physfs_internal.h"
  54. typedef struct
  55. {
  56. char name[18];
  57. PHYSFS_uint32 startPos;
  58. PHYSFS_uint32 size;
  59. } WADentry;
  60. typedef struct
  61. {
  62. char *filename;
  63. PHYSFS_sint64 last_mod_time;
  64. PHYSFS_uint32 entryCount;
  65. PHYSFS_uint32 entryOffset;
  66. WADentry *entries;
  67. } WADinfo;
  68. typedef struct
  69. {
  70. void *handle;
  71. WADentry *entry;
  72. PHYSFS_uint32 curPos;
  73. } WADfileinfo;
  74. static void WAD_dirClose(DirHandle *h);
  75. static PHYSFS_sint64 WAD_read(FileHandle *handle, void *buffer,
  76. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  77. static PHYSFS_sint64 WAD_write(FileHandle *handle, const void *buffer,
  78. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  79. static int WAD_eof(FileHandle *handle);
  80. static PHYSFS_sint64 WAD_tell(FileHandle *handle);
  81. static int WAD_seek(FileHandle *handle, PHYSFS_uint64 offset);
  82. static PHYSFS_sint64 WAD_fileLength(FileHandle *handle);
  83. static int WAD_fileClose(FileHandle *handle);
  84. static int WAD_isArchive(const char *filename, int forWriting);
  85. static DirHandle *WAD_openArchive(const char *name, int forWriting);
  86. static LinkedStringList *WAD_enumerateFiles(DirHandle *h,
  87. const char *dirname,
  88. int omitSymLinks);
  89. static int WAD_exists(DirHandle *h, const char *name);
  90. static int WAD_isDirectory(DirHandle *h, const char *name, int *fileExists);
  91. static int WAD_isSymLink(DirHandle *h, const char *name, int *fileExists);
  92. static PHYSFS_sint64 WAD_getLastModTime(DirHandle *h, const char *n, int *e);
  93. static FileHandle *WAD_openRead(DirHandle *h, const char *name, int *exist);
  94. static FileHandle *WAD_openWrite(DirHandle *h, const char *name);
  95. static FileHandle *WAD_openAppend(DirHandle *h, const char *name);
  96. static int WAD_remove(DirHandle *h, const char *name);
  97. static int WAD_mkdir(DirHandle *h, const char *name);
  98. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD =
  99. {
  100. "WAD",
  101. WAD_ARCHIVE_DESCRIPTION,
  102. "Travis Wells <traviswells@mchsi.com>",
  103. "http://www.3dmm2.com/doom/",
  104. };
  105. static const FileFunctions __PHYSFS_FileFunctions_WAD =
  106. {
  107. WAD_read, /* read() method */
  108. WAD_write, /* write() method */
  109. WAD_eof, /* eof() method */
  110. WAD_tell, /* tell() method */
  111. WAD_seek, /* seek() method */
  112. WAD_fileLength, /* fileLength() method */
  113. WAD_fileClose /* fileClose() method */
  114. };
  115. const DirFunctions __PHYSFS_DirFunctions_WAD =
  116. {
  117. &__PHYSFS_ArchiveInfo_WAD,
  118. WAD_isArchive, /* isArchive() method */
  119. WAD_openArchive, /* openArchive() method */
  120. WAD_enumerateFiles, /* enumerateFiles() method */
  121. WAD_exists, /* exists() method */
  122. WAD_isDirectory, /* isDirectory() method */
  123. WAD_isSymLink, /* isSymLink() method */
  124. WAD_getLastModTime, /* getLastModTime() method */
  125. WAD_openRead, /* openRead() method */
  126. WAD_openWrite, /* openWrite() method */
  127. WAD_openAppend, /* openAppend() method */
  128. WAD_remove, /* remove() method */
  129. WAD_mkdir, /* mkdir() method */
  130. WAD_dirClose /* dirClose() method */
  131. };
  132. static void WAD_dirClose(DirHandle *h)
  133. {
  134. WADinfo *info = ((WADinfo *) h->opaque);
  135. free(info->filename);
  136. free(info->entries);
  137. free(info);
  138. free(h);
  139. } /* WAD_dirClose */
  140. static PHYSFS_sint64 WAD_read(FileHandle *handle, void *buffer,
  141. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  142. {
  143. WADfileinfo *finfo = (WADfileinfo *) (handle->opaque);
  144. WADentry *entry = finfo->entry;
  145. PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
  146. PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
  147. PHYSFS_sint64 rc;
  148. if (objsLeft < objCount)
  149. objCount = objsLeft;
  150. rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  151. if (rc > 0)
  152. finfo->curPos += (PHYSFS_uint32) (rc * objSize);
  153. return(rc);
  154. } /* WAD_read */
  155. static PHYSFS_sint64 WAD_write(FileHandle *handle, const void *buffer,
  156. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  157. {
  158. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  159. } /* WAD_write */
  160. static int WAD_eof(FileHandle *handle)
  161. {
  162. WADfileinfo *finfo = (WADfileinfo *) (handle->opaque);
  163. WADentry *entry = finfo->entry;
  164. return(finfo->curPos >= entry->size);
  165. } /* WAD_eof */
  166. static PHYSFS_sint64 WAD_tell(FileHandle *handle)
  167. {
  168. return(((WADfileinfo *) (handle->opaque))->curPos);
  169. } /* WAD_tell */
  170. static int WAD_seek(FileHandle *handle, PHYSFS_uint64 offset)
  171. {
  172. WADfileinfo *finfo = (WADfileinfo *) (handle->opaque);
  173. WADentry *entry = finfo->entry;
  174. int rc;
  175. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  176. BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
  177. rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
  178. if (rc)
  179. finfo->curPos = (PHYSFS_uint32) offset;
  180. return(rc);
  181. } /* WAD_seek */
  182. static PHYSFS_sint64 WAD_fileLength(FileHandle *handle)
  183. {
  184. WADfileinfo *finfo = ((WADfileinfo *) handle->opaque);
  185. return((PHYSFS_sint64) finfo->entry->size);
  186. } /* WAD_fileLength */
  187. static int WAD_fileClose(FileHandle *handle)
  188. {
  189. WADfileinfo *finfo = ((WADfileinfo *) handle->opaque);
  190. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  191. free(finfo);
  192. free(handle);
  193. return(1);
  194. } /* WAD_fileClose */
  195. static int wad_open(const char *filename, int forWriting,
  196. void **fh, PHYSFS_uint32 *count,PHYSFS_uint32 *offset)
  197. {
  198. PHYSFS_uint8 buf[4];
  199. *fh = NULL;
  200. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  201. *fh = __PHYSFS_platformOpenRead(filename);
  202. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  203. if (__PHYSFS_platformRead(*fh, buf, 4, 1) != 1)
  204. goto openWad_failed;
  205. if (memcmp(buf, "IWAD", 4) != 0 && memcmp(buf, "PWAD", 4) != 0)
  206. {
  207. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  208. goto openWad_failed;
  209. } /* if */
  210. if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
  211. goto openWad_failed;
  212. *count = PHYSFS_swapULE32(*count);
  213. if (__PHYSFS_platformRead(*fh, offset, sizeof (PHYSFS_uint32), 1) != 1)
  214. goto openWad_failed;
  215. *offset = PHYSFS_swapULE32(*offset);
  216. return(1);
  217. openWad_failed:
  218. if (*fh != NULL)
  219. __PHYSFS_platformClose(*fh);
  220. *count = -1;
  221. *fh = NULL;
  222. return(0);
  223. } /* wad_open */
  224. static int WAD_isArchive(const char *filename, int forWriting)
  225. {
  226. void *fh;
  227. PHYSFS_uint32 fileCount,offset;
  228. int retval = wad_open(filename, forWriting, &fh, &fileCount,&offset);
  229. if (fh != NULL)
  230. __PHYSFS_platformClose(fh);
  231. return(retval);
  232. } /* WAD_isArchive */
  233. static int wad_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  234. {
  235. if (one != two)
  236. {
  237. const WADentry *a = (const WADentry *) _a;
  238. return(strcmp(a[one].name, a[two].name));
  239. } /* if */
  240. return 0;
  241. } /* wad_entry_cmp */
  242. static void wad_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  243. {
  244. if (one != two)
  245. {
  246. WADentry tmp;
  247. WADentry *first = &(((WADentry *) _a)[one]);
  248. WADentry *second = &(((WADentry *) _a)[two]);
  249. memcpy(&tmp, first, sizeof (WADentry));
  250. memcpy(first, second, sizeof (WADentry));
  251. memcpy(second, &tmp, sizeof (WADentry));
  252. } /* if */
  253. } /* wad_entry_swap */
  254. static int wad_load_entries(const char *name, int forWriting, WADinfo *info)
  255. {
  256. void *fh = NULL;
  257. PHYSFS_uint32 fileCount;
  258. PHYSFS_uint32 directoryOffset;
  259. WADentry *entry;
  260. char lastDirectory[9];
  261. lastDirectory[8] = 0; /* Make sure lastDirectory stays null-terminated. */
  262. BAIL_IF_MACRO(!wad_open(name, forWriting, &fh, &fileCount,&directoryOffset), NULL, 0);
  263. info->entryCount = fileCount;
  264. info->entries = (WADentry *) malloc(sizeof (WADentry) * fileCount);
  265. if (info->entries == NULL)
  266. {
  267. __PHYSFS_platformClose(fh);
  268. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  269. } /* if */
  270. __PHYSFS_platformSeek(fh,directoryOffset);
  271. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  272. {
  273. if (__PHYSFS_platformRead(fh, &entry->startPos, 4, 1) != 1)
  274. {
  275. __PHYSFS_platformClose(fh);
  276. return(0);
  277. } /* if */
  278. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  279. {
  280. __PHYSFS_platformClose(fh);
  281. return(0);
  282. } /* if */
  283. if (__PHYSFS_platformRead(fh, &entry->name, 8, 1) != 1)
  284. {
  285. __PHYSFS_platformClose(fh);
  286. return(0);
  287. } /* if */
  288. entry->name[8] = '\0'; /* name might not be null-terminated in file. */
  289. entry->size = PHYSFS_swapULE32(entry->size);
  290. entry->startPos = PHYSFS_swapULE32(entry->startPos);
  291. } /* for */
  292. __PHYSFS_platformClose(fh);
  293. __PHYSFS_sort(info->entries, info->entryCount,
  294. wad_entry_cmp, wad_entry_swap);
  295. return(1);
  296. } /* wad_load_entries */
  297. static DirHandle *WAD_openArchive(const char *name, int forWriting)
  298. {
  299. WADinfo *info;
  300. DirHandle *retval = malloc(sizeof (DirHandle));
  301. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  302. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  303. info = retval->opaque = malloc(sizeof (WADinfo));
  304. if (info == NULL)
  305. {
  306. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  307. goto WAD_openArchive_failed;
  308. } /* if */
  309. memset(info, '\0', sizeof (WADinfo));
  310. info->filename = (char *) malloc(strlen(name) + 1);
  311. if (info->filename == NULL)
  312. {
  313. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  314. goto WAD_openArchive_failed;
  315. } /* if */
  316. if (!wad_load_entries(name, forWriting, info))
  317. goto WAD_openArchive_failed;
  318. strcpy(info->filename, name);
  319. info->last_mod_time = modtime;
  320. retval->funcs = &__PHYSFS_DirFunctions_WAD;
  321. return(retval);
  322. WAD_openArchive_failed:
  323. if (retval != NULL)
  324. {
  325. if (retval->opaque != NULL)
  326. {
  327. if (info->filename != NULL)
  328. free(info->filename);
  329. if (info->entries != NULL)
  330. free(info->entries);
  331. free(info);
  332. } /* if */
  333. free(retval);
  334. } /* if */
  335. return(NULL);
  336. } /* WAD_openArchive */
  337. static LinkedStringList *WAD_enumerateFiles(DirHandle *h,
  338. const char *dirname,
  339. int omitSymLinks)
  340. {
  341. WADinfo *info = ((WADinfo *) h->opaque);
  342. WADentry *entry = info->entries;
  343. LinkedStringList *retval = NULL, *p = NULL;
  344. PHYSFS_uint32 max = info->entryCount;
  345. PHYSFS_uint32 i;
  346. char *sep;
  347. if (dirname[0] == 0)
  348. {
  349. for (i = 0; i < max; i++, entry++)
  350. {
  351. if (strchr(entry->name, '/') == NULL)
  352. {
  353. retval = __PHYSFS_addToLinkedStringList(retval, &p,
  354. entry->name, -1);
  355. } /* if */
  356. } /* for */
  357. } /* if */
  358. else
  359. {
  360. for (i = 0; i < max; i++, entry++)
  361. {
  362. sep = strchr(entry->name, '/');
  363. if (sep != NULL)
  364. {
  365. if (strncmp(dirname, entry->name, (sep-entry->name)) == 0)
  366. {
  367. retval = __PHYSFS_addToLinkedStringList(retval, &p,
  368. sep + 1, -1);
  369. } /* if */
  370. } /* if */
  371. } /* for */
  372. } /* else */
  373. return(retval);
  374. } /* WAD_enumerateFiles */
  375. static WADentry *wad_find_entry(WADinfo *info, const char *name)
  376. {
  377. WADentry *a = info->entries;
  378. PHYSFS_sint32 lo = 0;
  379. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  380. PHYSFS_sint32 middle;
  381. int rc;
  382. while (lo <= hi)
  383. {
  384. middle = lo + ((hi - lo) / 2);
  385. rc = strcmp(name, a[middle].name);
  386. if (rc == 0) /* found it! */
  387. return(&a[middle]);
  388. else if (rc > 0)
  389. lo = middle + 1;
  390. else
  391. hi = middle - 1;
  392. } /* while */
  393. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  394. } /* wad_find_entry */
  395. static int WAD_exists(DirHandle *h, const char *name)
  396. {
  397. return(wad_find_entry(((WADinfo *) h->opaque), name) != NULL);
  398. } /* WAD_exists */
  399. static int WAD_isDirectory(DirHandle *h, const char *name, int *fileExists)
  400. {
  401. WADentry *entry = wad_find_entry(((WADinfo *) h->opaque), name);
  402. if (entry != NULL)
  403. {
  404. char *n;
  405. *fileExists = 1;
  406. /* Can't be a directory if it's a subdirectory. */
  407. if (strchr(entry->name, '/') != NULL)
  408. return(0);
  409. /* Check if it matches "MAP??" or "E?M?" ... */
  410. n = entry->name;
  411. if ((n[0] == 'E' && n[2] == 'M') ||
  412. (n[0] == 'M' && n[1] == 'A' && n[2] == 'P' && n[6] == 0))
  413. {
  414. return(1);
  415. } /* if */
  416. return(0);
  417. } /* if */
  418. else
  419. {
  420. *fileExists = 0;
  421. return(0);
  422. } /* else */
  423. } /* WAD_isDirectory */
  424. static int WAD_isSymLink(DirHandle *h, const char *name, int *fileExists)
  425. {
  426. *fileExists = WAD_exists(h, name);
  427. return(0); /* never symlinks in a wad. */
  428. } /* WAD_isSymLink */
  429. static PHYSFS_sint64 WAD_getLastModTime(DirHandle *h,
  430. const char *name,
  431. int *fileExists)
  432. {
  433. WADinfo *info = ((WADinfo *) h->opaque);
  434. PHYSFS_sint64 retval = -1;
  435. *fileExists = (wad_find_entry(info, name) != NULL);
  436. if (*fileExists) /* use time of WAD itself in the physical filesystem. */
  437. retval = ((WADinfo *) h->opaque)->last_mod_time;
  438. return(retval);
  439. } /* WAD_getLastModTime */
  440. static FileHandle *WAD_openRead(DirHandle *h, const char *fnm, int *fileExists)
  441. {
  442. WADinfo *info = ((WADinfo *) h->opaque);
  443. FileHandle *retval;
  444. WADfileinfo *finfo;
  445. WADentry *entry;
  446. entry = wad_find_entry(info, fnm);
  447. *fileExists = (entry != NULL);
  448. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  449. retval = (FileHandle *) malloc(sizeof (FileHandle));
  450. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  451. finfo = (WADfileinfo *) malloc(sizeof (WADfileinfo));
  452. if (finfo == NULL)
  453. {
  454. free(retval);
  455. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  456. } /* if */
  457. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  458. if ( (finfo->handle == NULL) ||
  459. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  460. {
  461. free(finfo);
  462. free(retval);
  463. return(NULL);
  464. } /* if */
  465. finfo->curPos = 0;
  466. finfo->entry = entry;
  467. retval->opaque = (void *) finfo;
  468. retval->funcs = &__PHYSFS_FileFunctions_WAD;
  469. retval->dirHandle = h;
  470. return(retval);
  471. } /* WAD_openRead */
  472. static FileHandle *WAD_openWrite(DirHandle *h, const char *name)
  473. {
  474. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  475. } /* WAD_openWrite */
  476. static FileHandle *WAD_openAppend(DirHandle *h, const char *name)
  477. {
  478. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  479. } /* WAD_openAppend */
  480. static int WAD_remove(DirHandle *h, const char *name)
  481. {
  482. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  483. } /* WAD_remove */
  484. static int WAD_mkdir(DirHandle *h, const char *name)
  485. {
  486. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  487. } /* WAD_mkdir */
  488. #endif /* defined PHYSFS_SUPPORTS_WAD */
  489. /* end of wad.c ... */