wad.c 14 KB

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