🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.9 KiB

  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 uint32 which is the number of lumps in the wad
  20. * (c) a uint32 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 uint32, the file offset to the start of the lump
  27. * (b) a uint32, 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. #define __PHYSICSFS_INTERNAL__
  45. #include "physfs_internal.h"
  46. #if PHYSFS_SUPPORTS_WAD
  47. static int wadLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
  48. {
  49. PHYSFS_uint32 i;
  50. for (i = 0; i < count; i++)
  51. {
  52. PHYSFS_uint32 pos;
  53. PHYSFS_uint32 size;
  54. char name[9];
  55. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &pos, 4), 0);
  56. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
  57. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 8), 0);
  58. name[8] = '\0'; /* name might not be null-terminated in file. */
  59. size = PHYSFS_swapULE32(size);
  60. pos = PHYSFS_swapULE32(pos);
  61. BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0);
  62. } /* for */
  63. return 1;
  64. } /* wadLoadEntries */
  65. static void *WAD_openArchive(PHYSFS_Io *io, const char *name,
  66. int forWriting, int *claimed)
  67. {
  68. PHYSFS_uint8 buf[4];
  69. PHYSFS_uint32 count;
  70. PHYSFS_uint32 directoryOffset;
  71. void *unpkarc;
  72. assert(io != NULL); /* shouldn't ever happen. */
  73. BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
  74. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, sizeof (buf)), NULL);
  75. if ((memcmp(buf, "IWAD", 4) != 0) && (memcmp(buf, "PWAD", 4) != 0))
  76. BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);
  77. *claimed = 1;
  78. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL);
  79. count = PHYSFS_swapULE32(count);
  80. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &directoryOffset, 4), 0);
  81. directoryOffset = PHYSFS_swapULE32(directoryOffset);
  82. BAIL_IF_ERRPASS(!io->seek(io, directoryOffset), 0);
  83. unpkarc = UNPK_openArchive(io);
  84. BAIL_IF_ERRPASS(!unpkarc, NULL);
  85. if (!wadLoadEntries(io, count, unpkarc))
  86. {
  87. UNPK_abandonArchive(unpkarc);
  88. return NULL;
  89. } /* if */
  90. return unpkarc;
  91. } /* WAD_openArchive */
  92. const PHYSFS_Archiver __PHYSFS_Archiver_WAD =
  93. {
  94. CURRENT_PHYSFS_ARCHIVER_API_VERSION,
  95. {
  96. "WAD",
  97. "DOOM engine format",
  98. "Travis Wells <traviswells@mchsi.com>",
  99. "http://www.3dmm2.com/doom/",
  100. 0, /* supportsSymlinks */
  101. },
  102. WAD_openArchive,
  103. UNPK_enumerate,
  104. UNPK_openRead,
  105. UNPK_openWrite,
  106. UNPK_openAppend,
  107. UNPK_remove,
  108. UNPK_mkdir,
  109. UNPK_stat,
  110. UNPK_closeArchive
  111. };
  112. #endif /* defined PHYSFS_SUPPORTS_WAD */
  113. /* end of physfs_archiver_wad.c ... */