wad.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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[9];
  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. BAIL_IF_MACRO(!wad_open(name, forWriting, &fh, &fileCount,&directoryOffset), NULL, 0);
  254. info->entryCount = fileCount;
  255. info->entries = (WADentry *) malloc(sizeof (WADentry) * fileCount);
  256. if (info->entries == NULL)
  257. {
  258. __PHYSFS_platformClose(fh);
  259. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  260. } /* if */
  261. __PHYSFS_platformSeek(fh,directoryOffset);
  262. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  263. {
  264. if (__PHYSFS_platformRead(fh, &entry->startPos, 4, 1) != 1)
  265. {
  266. __PHYSFS_platformClose(fh);
  267. return(0);
  268. } /* if */
  269. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  270. {
  271. __PHYSFS_platformClose(fh);
  272. return(0);
  273. } /* if */
  274. if (__PHYSFS_platformRead(fh, &entry->name, 8, 1) != 1)
  275. {
  276. __PHYSFS_platformClose(fh);
  277. return(0);
  278. } /* if */
  279. entry->name[8] = '\0'; /* name might not be null-terminated in file. */
  280. entry->size = PHYSFS_swapULE32(entry->size);
  281. entry->startPos = PHYSFS_swapULE32(entry->startPos);
  282. } /* for */
  283. __PHYSFS_platformClose(fh);
  284. __PHYSFS_sort(info->entries, info->entryCount,
  285. wad_entry_cmp, wad_entry_swap);
  286. return(1);
  287. } /* wad_load_entries */
  288. static DirHandle *WAD_openArchive(const char *name, int forWriting)
  289. {
  290. WADinfo *info;
  291. DirHandle *retval = malloc(sizeof (DirHandle));
  292. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  293. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  294. info = retval->opaque = malloc(sizeof (WADinfo));
  295. if (info == NULL)
  296. {
  297. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  298. goto WAD_openArchive_failed;
  299. } /* if */
  300. memset(info, '\0', sizeof (WADinfo));
  301. info->filename = (char *) malloc(strlen(name) + 1);
  302. if (info->filename == NULL)
  303. {
  304. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  305. goto WAD_openArchive_failed;
  306. } /* if */
  307. if (!wad_load_entries(name, forWriting, info))
  308. goto WAD_openArchive_failed;
  309. strcpy(info->filename, name);
  310. info->last_mod_time = modtime;
  311. retval->funcs = &__PHYSFS_DirFunctions_WAD;
  312. return(retval);
  313. WAD_openArchive_failed:
  314. if (retval != NULL)
  315. {
  316. if (retval->opaque != NULL)
  317. {
  318. if (info->filename != NULL)
  319. free(info->filename);
  320. if (info->entries != NULL)
  321. free(info->entries);
  322. free(info);
  323. } /* if */
  324. free(retval);
  325. } /* if */
  326. return(NULL);
  327. } /* WAD_openArchive */
  328. static LinkedStringList *WAD_enumerateFiles(DirHandle *h,
  329. const char *dirname,
  330. int omitSymLinks)
  331. {
  332. WADinfo *info = ((WADinfo *) h->opaque);
  333. WADentry *entry = info->entries;
  334. LinkedStringList *retval = NULL, *p = NULL;
  335. PHYSFS_uint32 max = info->entryCount;
  336. PHYSFS_uint32 i;
  337. /* no directories in WAD files. */
  338. BAIL_IF_MACRO(*dirname != '\0', ERR_NOT_A_DIR, NULL);
  339. for (i = 0; i < max; i++, entry++)
  340. retval = __PHYSFS_addToLinkedStringList(retval, &p, entry->name, -1);
  341. return(retval);
  342. } /* WAD_enumerateFiles */
  343. static WADentry *wad_find_entry(WADinfo *info, const char *name)
  344. {
  345. char *ptr = strchr(name, '.');
  346. WADentry *a = info->entries;
  347. PHYSFS_sint32 lo = 0;
  348. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  349. PHYSFS_sint32 middle;
  350. int rc;
  351. /*
  352. * Rule out filenames to avoid unneeded processing...no dirs,
  353. * big filenames, or extensions > 3 chars.
  354. */
  355. BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
  356. BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
  357. BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
  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(DirHandle *h, const char *name)
  372. {
  373. return(wad_find_entry(((WADinfo *) h->opaque), name) != NULL);
  374. } /* WAD_exists */
  375. static int WAD_isDirectory(DirHandle *h, const char *name, int *fileExists)
  376. {
  377. *fileExists = WAD_exists(h, name);
  378. return(0); /* never directories in a wad. */
  379. } /* WAD_isDirectory */
  380. static int WAD_isSymLink(DirHandle *h, const char *name, int *fileExists)
  381. {
  382. *fileExists = WAD_exists(h, name);
  383. return(0); /* never symlinks in a wad. */
  384. } /* WAD_isSymLink */
  385. static PHYSFS_sint64 WAD_getLastModTime(DirHandle *h,
  386. const char *name,
  387. int *fileExists)
  388. {
  389. WADinfo *info = ((WADinfo *) h->opaque);
  390. PHYSFS_sint64 retval = -1;
  391. *fileExists = (wad_find_entry(info, name) != NULL);
  392. if (*fileExists) /* use time of WAD itself in the physical filesystem. */
  393. retval = ((WADinfo *) h->opaque)->last_mod_time;
  394. return(retval);
  395. } /* WAD_getLastModTime */
  396. static FileHandle *WAD_openRead(DirHandle *h, const char *fnm, int *fileExists)
  397. {
  398. WADinfo *info = ((WADinfo *) h->opaque);
  399. FileHandle *retval;
  400. WADfileinfo *finfo;
  401. WADentry *entry;
  402. entry = wad_find_entry(info, fnm);
  403. *fileExists = (entry != NULL);
  404. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  405. retval = (FileHandle *) malloc(sizeof (FileHandle));
  406. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  407. finfo = (WADfileinfo *) malloc(sizeof (WADfileinfo));
  408. if (finfo == NULL)
  409. {
  410. free(retval);
  411. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  412. } /* if */
  413. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  414. if ( (finfo->handle == NULL) ||
  415. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  416. {
  417. free(finfo);
  418. free(retval);
  419. return(NULL);
  420. } /* if */
  421. finfo->curPos = 0;
  422. finfo->entry = entry;
  423. retval->opaque = (void *) finfo;
  424. retval->funcs = &__PHYSFS_FileFunctions_WAD;
  425. retval->dirHandle = h;
  426. return(retval);
  427. } /* WAD_openRead */
  428. static FileHandle *WAD_openWrite(DirHandle *h, const char *name)
  429. {
  430. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  431. } /* WAD_openWrite */
  432. static FileHandle *WAD_openAppend(DirHandle *h, const char *name)
  433. {
  434. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  435. } /* WAD_openAppend */
  436. static int WAD_remove(DirHandle *h, const char *name)
  437. {
  438. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  439. } /* WAD_remove */
  440. static int WAD_mkdir(DirHandle *h, const char *name)
  441. {
  442. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  443. } /* WAD_mkdir */
  444. #endif /* defined PHYSFS_SUPPORTS_WAD */
  445. /* end of wad.c ... */