wad.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 PHYSFS_sint64 WAD_read(fvoid *opaque, void *buffer,
  75. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  76. static PHYSFS_sint64 WAD_write(fvoid *opaque, const void *buffer,
  77. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  78. static int WAD_eof(fvoid *opaque);
  79. static PHYSFS_sint64 WAD_tell(fvoid *opaque);
  80. static int WAD_seek(fvoid *opaque, PHYSFS_uint64 offset);
  81. static PHYSFS_sint64 WAD_fileLength(fvoid *opaque);
  82. static int WAD_fileClose(fvoid *opaque);
  83. static int WAD_isArchive(const char *filename, int forWriting);
  84. static void *WAD_openArchive(const char *name, int forWriting);
  85. static LinkedStringList *WAD_enumerateFiles(dvoid *opaque,
  86. const char *dirname,
  87. int omitSymLinks);
  88. static int WAD_exists(dvoid *opaque, const char *name);
  89. static int WAD_isDirectory(dvoid *opaque, const char *name, int *fileExists);
  90. static int WAD_isSymLink(dvoid *opaque, const char *name, int *fileExists);
  91. static PHYSFS_sint64 WAD_getLastModTime(dvoid *opaque, const char *n, int *e);
  92. static fvoid *WAD_openRead(dvoid *opaque, const char *name, int *exist);
  93. static fvoid *WAD_openWrite(dvoid *opaque, const char *name);
  94. static fvoid *WAD_openAppend(dvoid *opaque, const char *name);
  95. static int WAD_remove(dvoid *opaque, const char *name);
  96. static int WAD_mkdir(dvoid *opaque, const char *name);
  97. static void WAD_dirClose(dvoid *opaque);
  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. const PHYSFS_Archiver __PHYSFS_Archiver_WAD =
  106. {
  107. &__PHYSFS_ArchiveInfo_WAD,
  108. WAD_isArchive, /* isArchive() method */
  109. WAD_openArchive, /* openArchive() method */
  110. WAD_enumerateFiles, /* enumerateFiles() method */
  111. WAD_exists, /* exists() method */
  112. WAD_isDirectory, /* isDirectory() method */
  113. WAD_isSymLink, /* isSymLink() method */
  114. WAD_getLastModTime, /* getLastModTime() method */
  115. WAD_openRead, /* openRead() method */
  116. WAD_openWrite, /* openWrite() method */
  117. WAD_openAppend, /* openAppend() method */
  118. WAD_remove, /* remove() method */
  119. WAD_mkdir, /* mkdir() method */
  120. WAD_dirClose, /* dirClose() method */
  121. WAD_read, /* read() method */
  122. WAD_write, /* write() method */
  123. WAD_eof, /* eof() method */
  124. WAD_tell, /* tell() method */
  125. WAD_seek, /* seek() method */
  126. WAD_fileLength, /* fileLength() method */
  127. WAD_fileClose /* fileClose() method */
  128. };
  129. static void WAD_dirClose(dvoid *opaque)
  130. {
  131. WADinfo *info = ((WADinfo *) opaque);
  132. free(info->filename);
  133. free(info->entries);
  134. free(info);
  135. } /* WAD_dirClose */
  136. static PHYSFS_sint64 WAD_read(fvoid *opaque, void *buffer,
  137. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  138. {
  139. WADfileinfo *finfo = (WADfileinfo *) opaque;
  140. WADentry *entry = finfo->entry;
  141. PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
  142. PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
  143. PHYSFS_sint64 rc;
  144. if (objsLeft < objCount)
  145. objCount = objsLeft;
  146. rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  147. if (rc > 0)
  148. finfo->curPos += (PHYSFS_uint32) (rc * objSize);
  149. return(rc);
  150. } /* WAD_read */
  151. static PHYSFS_sint64 WAD_write(fvoid *opaque, const void *buffer,
  152. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  153. {
  154. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  155. } /* WAD_write */
  156. static int WAD_eof(fvoid *opaque)
  157. {
  158. WADfileinfo *finfo = (WADfileinfo *) opaque;
  159. WADentry *entry = finfo->entry;
  160. return(finfo->curPos >= entry->size);
  161. } /* WAD_eof */
  162. static PHYSFS_sint64 WAD_tell(fvoid *opaque)
  163. {
  164. return(((WADfileinfo *) opaque)->curPos);
  165. } /* WAD_tell */
  166. static int WAD_seek(fvoid *opaque, PHYSFS_uint64 offset)
  167. {
  168. WADfileinfo *finfo = (WADfileinfo *) opaque;
  169. WADentry *entry = finfo->entry;
  170. int rc;
  171. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  172. BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
  173. rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
  174. if (rc)
  175. finfo->curPos = (PHYSFS_uint32) offset;
  176. return(rc);
  177. } /* WAD_seek */
  178. static PHYSFS_sint64 WAD_fileLength(fvoid *opaque)
  179. {
  180. WADfileinfo *finfo = (WADfileinfo *) opaque;
  181. return((PHYSFS_sint64) finfo->entry->size);
  182. } /* WAD_fileLength */
  183. static int WAD_fileClose(fvoid *opaque)
  184. {
  185. WADfileinfo *finfo = (WADfileinfo *) opaque;
  186. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  187. free(finfo);
  188. return(1);
  189. } /* WAD_fileClose */
  190. static int wad_open(const char *filename, int forWriting,
  191. void **fh, PHYSFS_uint32 *count,PHYSFS_uint32 *offset)
  192. {
  193. PHYSFS_uint8 buf[4];
  194. *fh = NULL;
  195. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  196. *fh = __PHYSFS_platformOpenRead(filename);
  197. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  198. if (__PHYSFS_platformRead(*fh, buf, 4, 1) != 1)
  199. goto openWad_failed;
  200. if (memcmp(buf, "IWAD", 4) != 0 && memcmp(buf, "PWAD", 4) != 0)
  201. {
  202. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  203. goto openWad_failed;
  204. } /* if */
  205. if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
  206. goto openWad_failed;
  207. *count = PHYSFS_swapULE32(*count);
  208. if (__PHYSFS_platformRead(*fh, offset, sizeof (PHYSFS_uint32), 1) != 1)
  209. goto openWad_failed;
  210. *offset = PHYSFS_swapULE32(*offset);
  211. return(1);
  212. openWad_failed:
  213. if (*fh != NULL)
  214. __PHYSFS_platformClose(*fh);
  215. *count = -1;
  216. *fh = NULL;
  217. return(0);
  218. } /* wad_open */
  219. static int WAD_isArchive(const char *filename, int forWriting)
  220. {
  221. void *fh;
  222. PHYSFS_uint32 fileCount,offset;
  223. int retval = wad_open(filename, forWriting, &fh, &fileCount,&offset);
  224. if (fh != NULL)
  225. __PHYSFS_platformClose(fh);
  226. return(retval);
  227. } /* WAD_isArchive */
  228. static int wad_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  229. {
  230. WADentry *a = (WADentry *) _a;
  231. return(strcmp(a[one].name, a[two].name));
  232. } /* wad_entry_cmp */
  233. static void wad_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  234. {
  235. WADentry tmp;
  236. WADentry *first = &(((WADentry *) _a)[one]);
  237. WADentry *second = &(((WADentry *) _a)[two]);
  238. memcpy(&tmp, first, sizeof (WADentry));
  239. memcpy(first, second, sizeof (WADentry));
  240. memcpy(second, &tmp, sizeof (WADentry));
  241. } /* wad_entry_swap */
  242. static int wad_load_entries(const char *name, int forWriting, WADinfo *info)
  243. {
  244. void *fh = NULL;
  245. PHYSFS_uint32 fileCount;
  246. PHYSFS_uint32 directoryOffset;
  247. WADentry *entry;
  248. char lastDirectory[9];
  249. lastDirectory[8] = 0; /* Make sure lastDirectory stays null-terminated. */
  250. BAIL_IF_MACRO(!wad_open(name, forWriting, &fh, &fileCount,&directoryOffset), NULL, 0);
  251. info->entryCount = fileCount;
  252. info->entries = (WADentry *) malloc(sizeof (WADentry) * fileCount);
  253. if (info->entries == NULL)
  254. {
  255. __PHYSFS_platformClose(fh);
  256. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  257. } /* if */
  258. __PHYSFS_platformSeek(fh,directoryOffset);
  259. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  260. {
  261. if (__PHYSFS_platformRead(fh, &entry->startPos, 4, 1) != 1)
  262. {
  263. __PHYSFS_platformClose(fh);
  264. return(0);
  265. } /* if */
  266. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  267. {
  268. __PHYSFS_platformClose(fh);
  269. return(0);
  270. } /* if */
  271. if (__PHYSFS_platformRead(fh, &entry->name, 8, 1) != 1)
  272. {
  273. __PHYSFS_platformClose(fh);
  274. return(0);
  275. } /* if */
  276. entry->name[8] = '\0'; /* name might not be null-terminated in file. */
  277. entry->size = PHYSFS_swapULE32(entry->size);
  278. entry->startPos = PHYSFS_swapULE32(entry->startPos);
  279. } /* for */
  280. __PHYSFS_platformClose(fh);
  281. __PHYSFS_sort(info->entries, info->entryCount,
  282. wad_entry_cmp, wad_entry_swap);
  283. return(1);
  284. } /* wad_load_entries */
  285. static void *WAD_openArchive(const char *name, int forWriting)
  286. {
  287. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  288. WADinfo *info = malloc(sizeof (WADinfo));
  289. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
  290. memset(info, '\0', sizeof (WADinfo));
  291. info->filename = (char *) malloc(strlen(name) + 1);
  292. if (info->filename == NULL)
  293. {
  294. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  295. goto WAD_openArchive_failed;
  296. } /* if */
  297. if (!wad_load_entries(name, forWriting, info))
  298. goto WAD_openArchive_failed;
  299. strcpy(info->filename, name);
  300. info->last_mod_time = modtime;
  301. return(info);
  302. WAD_openArchive_failed:
  303. if (info != NULL)
  304. {
  305. if (info->filename != NULL)
  306. free(info->filename);
  307. if (info->entries != NULL)
  308. free(info->entries);
  309. free(info);
  310. } /* if */
  311. return(NULL);
  312. } /* WAD_openArchive */
  313. static LinkedStringList *WAD_enumerateFiles(dvoid *opaque,
  314. const char *dirname,
  315. int omitSymLinks)
  316. {
  317. WADinfo *info = ((WADinfo *) opaque);
  318. WADentry *entry = info->entries;
  319. LinkedStringList *retval = NULL, *p = NULL;
  320. PHYSFS_uint32 max = info->entryCount;
  321. PHYSFS_uint32 i;
  322. char *sep;
  323. if (dirname[0] == 0)
  324. {
  325. for (i = 0; i < max; i++, entry++)
  326. {
  327. if (strchr(entry->name, '/') == NULL)
  328. {
  329. retval = __PHYSFS_addToLinkedStringList(retval, &p,
  330. entry->name, -1);
  331. } /* if */
  332. } /* for */
  333. } /* if */
  334. else
  335. {
  336. for (i = 0; i < max; i++, entry++)
  337. {
  338. sep = strchr(entry->name, '/');
  339. if (sep != NULL)
  340. {
  341. if (strncmp(dirname, entry->name, (sep-entry->name)) == 0)
  342. {
  343. retval = __PHYSFS_addToLinkedStringList(retval, &p,
  344. sep + 1, -1);
  345. } /* if */
  346. } /* if */
  347. } /* for */
  348. } /* else */
  349. return(retval);
  350. } /* WAD_enumerateFiles */
  351. static WADentry *wad_find_entry(WADinfo *info, const char *name)
  352. {
  353. WADentry *a = info->entries;
  354. PHYSFS_sint32 lo = 0;
  355. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  356. PHYSFS_sint32 middle;
  357. int rc;
  358. while (lo <= hi)
  359. {
  360. middle = lo + ((hi - lo) / 2);
  361. rc = strcmp(name, a[middle].name);
  362. if (rc == 0) /* found it! */
  363. return(&a[middle]);
  364. else if (rc > 0)
  365. lo = middle + 1;
  366. else
  367. hi = middle - 1;
  368. } /* while */
  369. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  370. } /* wad_find_entry */
  371. static int WAD_exists(dvoid *opaque, const char *name)
  372. {
  373. return(wad_find_entry(((WADinfo *) opaque), name) != NULL);
  374. } /* WAD_exists */
  375. static int WAD_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  376. {
  377. WADentry *entry = wad_find_entry(((WADinfo *) opaque), name);
  378. if (entry != NULL)
  379. {
  380. char *n;
  381. *fileExists = 1;
  382. /* Can't be a directory if it's a subdirectory. */
  383. if (strchr(entry->name, '/') != NULL)
  384. return(0);
  385. /* Check if it matches "MAP??" or "E?M?" ... */
  386. n = entry->name;
  387. if ((n[0] == 'E' && n[2] == 'M') ||
  388. (n[0] == 'M' && n[1] == 'A' && n[2] == 'P' && n[6] == 0))
  389. {
  390. return(1);
  391. } /* if */
  392. return(0);
  393. } /* if */
  394. else
  395. {
  396. *fileExists = 0;
  397. return(0);
  398. } /* else */
  399. } /* WAD_isDirectory */
  400. static int WAD_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  401. {
  402. *fileExists = WAD_exists(opaque, name);
  403. return(0); /* never symlinks in a wad. */
  404. } /* WAD_isSymLink */
  405. static PHYSFS_sint64 WAD_getLastModTime(dvoid *opaque,
  406. const char *name,
  407. int *fileExists)
  408. {
  409. WADinfo *info = ((WADinfo *) opaque);
  410. PHYSFS_sint64 retval = -1;
  411. *fileExists = (wad_find_entry(info, name) != NULL);
  412. if (*fileExists) /* use time of WAD itself in the physical filesystem. */
  413. retval = info->last_mod_time;
  414. return(retval);
  415. } /* WAD_getLastModTime */
  416. static fvoid *WAD_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  417. {
  418. WADinfo *info = ((WADinfo *) opaque);
  419. WADfileinfo *finfo;
  420. WADentry *entry;
  421. entry = wad_find_entry(info, fnm);
  422. *fileExists = (entry != NULL);
  423. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  424. finfo = (WADfileinfo *) malloc(sizeof (WADfileinfo));
  425. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  426. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  427. if ( (finfo->handle == NULL) ||
  428. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  429. {
  430. free(finfo);
  431. return(NULL);
  432. } /* if */
  433. finfo->curPos = 0;
  434. finfo->entry = entry;
  435. return(finfo);
  436. } /* WAD_openRead */
  437. static fvoid *WAD_openWrite(dvoid *opaque, const char *name)
  438. {
  439. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  440. } /* WAD_openWrite */
  441. static fvoid *WAD_openAppend(dvoid *opaque, const char *name)
  442. {
  443. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  444. } /* WAD_openAppend */
  445. static int WAD_remove(dvoid *opaque, const char *name)
  446. {
  447. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  448. } /* WAD_remove */
  449. static int WAD_mkdir(dvoid *opaque, const char *name)
  450. {
  451. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  452. } /* WAD_mkdir */
  453. #endif /* defined PHYSFS_SUPPORTS_WAD */
  454. /* end of wad.c ... */