archiver_wad.c 14 KB

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