wad.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. char lastDirectory[9];
  198. lastDirectory[8] = 0; /* Make sure lastDirectory stays null-terminated. */
  199. BAIL_IF_MACRO(!wad_open(name, forWriting, &fh, &fileCount,&directoryOffset), NULL, 0);
  200. info->entryCount = fileCount;
  201. info->entries = (WADentry *) allocator.Malloc(sizeof(WADentry)*fileCount);
  202. if (info->entries == NULL)
  203. {
  204. __PHYSFS_platformClose(fh);
  205. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  206. } /* if */
  207. __PHYSFS_platformSeek(fh,directoryOffset);
  208. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  209. {
  210. if (__PHYSFS_platformRead(fh, &entry->startPos, 4, 1) != 1)
  211. {
  212. __PHYSFS_platformClose(fh);
  213. return(0);
  214. } /* if */
  215. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  216. {
  217. __PHYSFS_platformClose(fh);
  218. return(0);
  219. } /* if */
  220. if (__PHYSFS_platformRead(fh, &entry->name, 8, 1) != 1)
  221. {
  222. __PHYSFS_platformClose(fh);
  223. return(0);
  224. } /* if */
  225. entry->name[8] = '\0'; /* name might not be null-terminated in file. */
  226. entry->size = PHYSFS_swapULE32(entry->size);
  227. entry->startPos = PHYSFS_swapULE32(entry->startPos);
  228. } /* for */
  229. __PHYSFS_platformClose(fh);
  230. __PHYSFS_sort(info->entries, info->entryCount,
  231. wad_entry_cmp, wad_entry_swap);
  232. return(1);
  233. } /* wad_load_entries */
  234. static void *WAD_openArchive(const char *name, int forWriting)
  235. {
  236. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  237. WADinfo *info = (WADinfo *) allocator.Malloc(sizeof (WADinfo));
  238. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
  239. memset(info, '\0', sizeof (WADinfo));
  240. info->filename = (char *) allocator.Malloc(strlen(name) + 1);
  241. GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, WAD_openArchive_failed);
  242. if (!wad_load_entries(name, forWriting, info))
  243. goto WAD_openArchive_failed;
  244. strcpy(info->filename, name);
  245. info->last_mod_time = modtime;
  246. return(info);
  247. WAD_openArchive_failed:
  248. if (info != NULL)
  249. {
  250. if (info->filename != NULL)
  251. allocator.Free(info->filename);
  252. if (info->entries != NULL)
  253. allocator.Free(info->entries);
  254. allocator.Free(info);
  255. } /* if */
  256. return(NULL);
  257. } /* WAD_openArchive */
  258. static void WAD_enumerateFiles(dvoid *opaque, const char *dname,
  259. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  260. const char *origdir, void *callbackdata)
  261. {
  262. WADinfo *info = ((WADinfo *) opaque);
  263. WADentry *entry = info->entries;
  264. PHYSFS_uint32 max = info->entryCount;
  265. PHYSFS_uint32 i;
  266. const char *name;
  267. char *sep;
  268. if (*dname == '\0') /* root directory enumeration? */
  269. {
  270. for (i = 0; i < max; i++, entry++)
  271. {
  272. name = entry->name;
  273. if (strchr(name, '/') == NULL)
  274. cb(callbackdata, origdir, name);
  275. } /* for */
  276. } /* if */
  277. else
  278. {
  279. for (i = 0; i < max; i++, entry++)
  280. {
  281. name = entry->name;
  282. sep = strchr(name, '/');
  283. if (sep != NULL)
  284. {
  285. if (strncmp(dname, name, (sep - name)) == 0)
  286. cb(callbackdata, origdir, sep + 1);
  287. } /* if */
  288. } /* for */
  289. } /* else */
  290. } /* WAD_enumerateFiles */
  291. static WADentry *wad_find_entry(WADinfo *info, const char *name)
  292. {
  293. WADentry *a = info->entries;
  294. PHYSFS_sint32 lo = 0;
  295. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  296. PHYSFS_sint32 middle;
  297. int rc;
  298. while (lo <= hi)
  299. {
  300. middle = lo + ((hi - lo) / 2);
  301. rc = strcmp(name, a[middle].name);
  302. if (rc == 0) /* found it! */
  303. return(&a[middle]);
  304. else if (rc > 0)
  305. lo = middle + 1;
  306. else
  307. hi = middle - 1;
  308. } /* while */
  309. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  310. } /* wad_find_entry */
  311. static int WAD_exists(dvoid *opaque, const char *name)
  312. {
  313. return(wad_find_entry(((WADinfo *) opaque), name) != NULL);
  314. } /* WAD_exists */
  315. static int WAD_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  316. {
  317. WADentry *entry = wad_find_entry(((WADinfo *) opaque), name);
  318. if (entry != NULL)
  319. {
  320. char *n;
  321. *fileExists = 1;
  322. /* Can't be a directory if it's a subdirectory. */
  323. if (strchr(entry->name, '/') != NULL)
  324. return(0);
  325. /* Check if it matches "MAP??" or "E?M?" ... */
  326. n = entry->name;
  327. if ((n[0] == 'E' && n[2] == 'M') ||
  328. (n[0] == 'M' && n[1] == 'A' && n[2] == 'P' && n[6] == 0))
  329. {
  330. return(1);
  331. } /* if */
  332. return(0);
  333. } /* if */
  334. else
  335. {
  336. *fileExists = 0;
  337. return(0);
  338. } /* else */
  339. } /* WAD_isDirectory */
  340. static int WAD_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  341. {
  342. *fileExists = WAD_exists(opaque, name);
  343. return(0); /* never symlinks in a wad. */
  344. } /* WAD_isSymLink */
  345. static PHYSFS_sint64 WAD_getLastModTime(dvoid *opaque,
  346. const char *name,
  347. int *fileExists)
  348. {
  349. WADinfo *info = ((WADinfo *) opaque);
  350. PHYSFS_sint64 retval = -1;
  351. *fileExists = (wad_find_entry(info, name) != NULL);
  352. if (*fileExists) /* use time of WAD itself in the physical filesystem. */
  353. retval = info->last_mod_time;
  354. return(retval);
  355. } /* WAD_getLastModTime */
  356. static fvoid *WAD_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  357. {
  358. WADinfo *info = ((WADinfo *) opaque);
  359. WADfileinfo *finfo;
  360. WADentry *entry;
  361. entry = wad_find_entry(info, fnm);
  362. *fileExists = (entry != NULL);
  363. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  364. finfo = (WADfileinfo *) allocator.Malloc(sizeof (WADfileinfo));
  365. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  366. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  367. if ( (finfo->handle == NULL) ||
  368. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  369. {
  370. allocator.Free(finfo);
  371. return(NULL);
  372. } /* if */
  373. finfo->curPos = 0;
  374. finfo->entry = entry;
  375. return(finfo);
  376. } /* WAD_openRead */
  377. static fvoid *WAD_openWrite(dvoid *opaque, const char *name)
  378. {
  379. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  380. } /* WAD_openWrite */
  381. static fvoid *WAD_openAppend(dvoid *opaque, const char *name)
  382. {
  383. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  384. } /* WAD_openAppend */
  385. static int WAD_remove(dvoid *opaque, const char *name)
  386. {
  387. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  388. } /* WAD_remove */
  389. static int WAD_mkdir(dvoid *opaque, const char *name)
  390. {
  391. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  392. } /* WAD_mkdir */
  393. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD =
  394. {
  395. "WAD",
  396. WAD_ARCHIVE_DESCRIPTION,
  397. "Travis Wells <traviswells@mchsi.com>",
  398. "http://www.3dmm2.com/doom/",
  399. };
  400. const PHYSFS_Archiver __PHYSFS_Archiver_WAD =
  401. {
  402. &__PHYSFS_ArchiveInfo_WAD,
  403. WAD_isArchive, /* isArchive() method */
  404. WAD_openArchive, /* openArchive() method */
  405. WAD_enumerateFiles, /* enumerateFiles() method */
  406. WAD_exists, /* exists() method */
  407. WAD_isDirectory, /* isDirectory() method */
  408. WAD_isSymLink, /* isSymLink() method */
  409. WAD_getLastModTime, /* getLastModTime() method */
  410. WAD_openRead, /* openRead() method */
  411. WAD_openWrite, /* openWrite() method */
  412. WAD_openAppend, /* openAppend() method */
  413. WAD_remove, /* remove() method */
  414. WAD_mkdir, /* mkdir() method */
  415. WAD_dirClose, /* dirClose() method */
  416. WAD_read, /* read() method */
  417. WAD_write, /* write() method */
  418. WAD_eof, /* eof() method */
  419. WAD_tell, /* tell() method */
  420. WAD_seek, /* seek() method */
  421. WAD_fileLength, /* fileLength() method */
  422. WAD_fileClose /* fileClose() method */
  423. };
  424. #endif /* defined PHYSFS_SUPPORTS_WAD */
  425. /* end of wad.c ... */