wad.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. /* !!! FIXME WAD_ARCHIVE_DESCRIPTION, */ "DOOM engine format",
  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. WADentry *a = (WADentry *) _a;
  236. return(strcmp(a[one].name, a[two].name));
  237. } /* wad_entry_cmp */
  238. static void wad_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  239. {
  240. WADentry tmp;
  241. WADentry *first = &(((WADentry *) _a)[one]);
  242. WADentry *second = &(((WADentry *) _a)[two]);
  243. memcpy(&tmp, first, sizeof (WADentry));
  244. memcpy(first, second, sizeof (WADentry));
  245. memcpy(second, &tmp, sizeof (WADentry));
  246. } /* wad_entry_swap */
  247. static int wad_load_entries(const char *name, int forWriting, WADinfo *info)
  248. {
  249. void *fh = NULL;
  250. PHYSFS_uint32 fileCount;
  251. PHYSFS_uint32 directoryOffset;
  252. WADentry *entry;
  253. char lastDirectory[9];
  254. lastDirectory[8] = 0; /* Make sure lastDirectory stays null-terminated. */
  255. BAIL_IF_MACRO(!wad_open(name, forWriting, &fh, &fileCount,&directoryOffset), NULL, 0);
  256. info->entryCount = fileCount;
  257. info->entries = (WADentry *) malloc(sizeof (WADentry) * fileCount);
  258. if (info->entries == NULL)
  259. {
  260. __PHYSFS_platformClose(fh);
  261. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  262. } /* if */
  263. __PHYSFS_platformSeek(fh,directoryOffset);
  264. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  265. {
  266. if (__PHYSFS_platformRead(fh, &entry->startPos, 4, 1) != 1)
  267. {
  268. __PHYSFS_platformClose(fh);
  269. return(0);
  270. } /* if */
  271. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  272. {
  273. __PHYSFS_platformClose(fh);
  274. return(0);
  275. } /* if */
  276. if (__PHYSFS_platformRead(fh, &entry->name, 8, 1) != 1)
  277. {
  278. __PHYSFS_platformClose(fh);
  279. return(0);
  280. } /* if */
  281. entry->name[8] = '\0'; /* name might not be null-terminated in file. */
  282. entry->size = PHYSFS_swapULE32(entry->size);
  283. entry->startPos = PHYSFS_swapULE32(entry->startPos);
  284. } /* for */
  285. __PHYSFS_platformClose(fh);
  286. __PHYSFS_sort(info->entries, info->entryCount,
  287. wad_entry_cmp, wad_entry_swap);
  288. return(1);
  289. } /* wad_load_entries */
  290. static DirHandle *WAD_openArchive(const char *name, int forWriting)
  291. {
  292. WADinfo *info;
  293. DirHandle *retval = malloc(sizeof (DirHandle));
  294. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  295. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  296. info = retval->opaque = malloc(sizeof (WADinfo));
  297. if (info == NULL)
  298. {
  299. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  300. goto WAD_openArchive_failed;
  301. } /* if */
  302. memset(info, '\0', sizeof (WADinfo));
  303. info->filename = (char *) malloc(strlen(name) + 1);
  304. if (info->filename == NULL)
  305. {
  306. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  307. goto WAD_openArchive_failed;
  308. } /* if */
  309. if (!wad_load_entries(name, forWriting, info))
  310. goto WAD_openArchive_failed;
  311. strcpy(info->filename, name);
  312. info->last_mod_time = modtime;
  313. retval->funcs = &__PHYSFS_DirFunctions_WAD;
  314. return(retval);
  315. WAD_openArchive_failed:
  316. if (retval != NULL)
  317. {
  318. if (retval->opaque != NULL)
  319. {
  320. if (info->filename != NULL)
  321. free(info->filename);
  322. if (info->entries != NULL)
  323. free(info->entries);
  324. free(info);
  325. } /* if */
  326. free(retval);
  327. } /* if */
  328. return(NULL);
  329. } /* WAD_openArchive */
  330. static LinkedStringList *WAD_enumerateFiles(DirHandle *h,
  331. const char *dirname,
  332. int omitSymLinks)
  333. {
  334. WADinfo *info = ((WADinfo *) h->opaque);
  335. WADentry *entry = info->entries;
  336. LinkedStringList *retval = NULL, *p = NULL;
  337. PHYSFS_uint32 max = info->entryCount;
  338. PHYSFS_uint32 i;
  339. char *sep;
  340. if (dirname[0] == 0)
  341. {
  342. for (i = 0; i < max; i++, entry++)
  343. {
  344. if (strchr(entry->name, '/') == NULL)
  345. {
  346. retval = __PHYSFS_addToLinkedStringList(retval, &p,
  347. entry->name, -1);
  348. } /* if */
  349. } /* for */
  350. } /* if */
  351. else
  352. {
  353. for (i = 0; i < max; i++, entry++)
  354. {
  355. sep = strchr(entry->name, '/');
  356. if (sep != NULL)
  357. {
  358. if (strncmp(dirname, entry->name, (sep-entry->name)) == 0)
  359. {
  360. retval = __PHYSFS_addToLinkedStringList(retval, &p,
  361. sep + 1, -1);
  362. } /* if */
  363. } /* if */
  364. } /* for */
  365. } /* else */
  366. return(retval);
  367. } /* WAD_enumerateFiles */
  368. static WADentry *wad_find_entry(WADinfo *info, const char *name)
  369. {
  370. WADentry *a = info->entries;
  371. PHYSFS_sint32 lo = 0;
  372. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  373. PHYSFS_sint32 middle;
  374. int rc;
  375. while (lo <= hi)
  376. {
  377. middle = lo + ((hi - lo) / 2);
  378. rc = strcmp(name, a[middle].name);
  379. if (rc == 0) /* found it! */
  380. return(&a[middle]);
  381. else if (rc > 0)
  382. lo = middle + 1;
  383. else
  384. hi = middle - 1;
  385. } /* while */
  386. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  387. } /* wad_find_entry */
  388. static int WAD_exists(DirHandle *h, const char *name)
  389. {
  390. return(wad_find_entry(((WADinfo *) h->opaque), name) != NULL);
  391. } /* WAD_exists */
  392. static int WAD_isDirectory(DirHandle *h, const char *name, int *fileExists)
  393. {
  394. WADentry *entry = wad_find_entry(((WADinfo *) h->opaque), name);
  395. if (entry != NULL)
  396. {
  397. char *n;
  398. *fileExists = 1;
  399. /* Can't be a directory if it's a subdirectory. */
  400. if (strchr(entry->name, '/') != NULL)
  401. return(0);
  402. /* Check if it matches "MAP??" or "E?M?" ... */
  403. n = entry->name;
  404. if ((n[0] == 'E' && n[2] == 'M') ||
  405. (n[0] == 'M' && n[1] == 'A' && n[2] == 'P' && n[6] == 0))
  406. {
  407. return(1);
  408. } /* if */
  409. return(0);
  410. } /* if */
  411. else
  412. {
  413. *fileExists = 0;
  414. return(0);
  415. } /* else */
  416. } /* WAD_isDirectory */
  417. static int WAD_isSymLink(DirHandle *h, const char *name, int *fileExists)
  418. {
  419. *fileExists = WAD_exists(h, name);
  420. return(0); /* never symlinks in a wad. */
  421. } /* WAD_isSymLink */
  422. static PHYSFS_sint64 WAD_getLastModTime(DirHandle *h,
  423. const char *name,
  424. int *fileExists)
  425. {
  426. WADinfo *info = ((WADinfo *) h->opaque);
  427. PHYSFS_sint64 retval = -1;
  428. *fileExists = (wad_find_entry(info, name) != NULL);
  429. if (*fileExists) /* use time of WAD itself in the physical filesystem. */
  430. retval = ((WADinfo *) h->opaque)->last_mod_time;
  431. return(retval);
  432. } /* WAD_getLastModTime */
  433. static FileHandle *WAD_openRead(DirHandle *h, const char *fnm, int *fileExists)
  434. {
  435. WADinfo *info = ((WADinfo *) h->opaque);
  436. FileHandle *retval;
  437. WADfileinfo *finfo;
  438. WADentry *entry;
  439. entry = wad_find_entry(info, fnm);
  440. *fileExists = (entry != NULL);
  441. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  442. retval = (FileHandle *) malloc(sizeof (FileHandle));
  443. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  444. finfo = (WADfileinfo *) malloc(sizeof (WADfileinfo));
  445. if (finfo == NULL)
  446. {
  447. free(retval);
  448. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  449. } /* if */
  450. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  451. if ( (finfo->handle == NULL) ||
  452. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  453. {
  454. free(finfo);
  455. free(retval);
  456. return(NULL);
  457. } /* if */
  458. finfo->curPos = 0;
  459. finfo->entry = entry;
  460. retval->opaque = (void *) finfo;
  461. retval->funcs = &__PHYSFS_FileFunctions_WAD;
  462. retval->dirHandle = h;
  463. return(retval);
  464. } /* WAD_openRead */
  465. static FileHandle *WAD_openWrite(DirHandle *h, const char *name)
  466. {
  467. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  468. } /* WAD_openWrite */
  469. static FileHandle *WAD_openAppend(DirHandle *h, const char *name)
  470. {
  471. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  472. } /* WAD_openAppend */
  473. static int WAD_remove(DirHandle *h, const char *name)
  474. {
  475. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  476. } /* WAD_remove */
  477. static int WAD_mkdir(DirHandle *h, const char *name)
  478. {
  479. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  480. } /* WAD_mkdir */
  481. #endif /* defined PHYSFS_SUPPORTS_WAD */
  482. /* end of wad.c ... */