wad.c 14 KB

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