physfs_archiver_hog.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * HOG support routines for PhysicsFS.
  3. *
  4. * This driver handles Descent I/II/III HOG archives.
  5. *
  6. * The Descent I/II format is very simple:
  7. *
  8. * The file always starts with the 3-byte signature "DHF" (Descent
  9. * HOG file). After that the files of a HOG are just attached after
  10. * another, divided by a 17 bytes header, which specifies the name
  11. * and length (in bytes) of the forthcoming file! So you just read
  12. * the header with its information of how big the following file is,
  13. * and then skip exact that number of bytes to get to the next file
  14. * in that HOG.
  15. *
  16. * char sig[3] = {'D', 'H', 'F'}; // "DHF"=Descent HOG File
  17. *
  18. * struct {
  19. * char file_name[13]; // Filename, padded to 13 bytes with 0s
  20. * int file_size; // filesize in bytes
  21. * char data[file_size]; // The file data
  22. * } FILE_STRUCT; // Repeated until the end of the file.
  23. *
  24. * (That info is from http://www.descent2.com/ddn/specs/hog/)
  25. *
  26. * Descent 3 moved to HOG2 format, which starts with the chars "HOG2",
  27. * then 32-bits for the number of contained files, 32 bits for the offset
  28. * to the first file's data, then 56 bytes of 0xFF (reserved?). Then for
  29. * each file, there's 36 bytes for filename (null-terminated, rest of bytes
  30. * are garbage), 32-bits unknown/reserved (always zero?), 32-bits of length
  31. * of file data, 32-bits of time since Unix epoch. Then immediately following,
  32. * for each file is their uncompressed content, you can find its offset
  33. * by starting at the initial data offset and adding the filesize of each
  34. * prior file.
  35. *
  36. * This information was found at:
  37. * https://web.archive.org/web/20020213004051/http://descent-3.com/ddn/specs/hog/
  38. *
  39. *
  40. * Please see the file LICENSE.txt in the source's root directory.
  41. *
  42. * This file written by Bradley Bell and Ryan C. Gordon.
  43. */
  44. #define __PHYSICSFS_INTERNAL__
  45. #include "physfs_internal.h"
  46. #if PHYSFS_SUPPORTS_HOG
  47. static int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val)
  48. {
  49. PHYSFS_uint32 v;
  50. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &v, sizeof (v)), 0);
  51. *val = PHYSFS_swapULE32(v);
  52. return 1;
  53. } /* readui32 */
  54. static int hog1LoadEntries(PHYSFS_Io *io, void *arc)
  55. {
  56. const PHYSFS_uint64 iolen = io->length(io);
  57. PHYSFS_uint32 pos = 3;
  58. while (pos < iolen)
  59. {
  60. PHYSFS_uint32 size;
  61. char name[13];
  62. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 13), 0);
  63. BAIL_IF_ERRPASS(!readui32(io, &size), 0);
  64. name[12] = '\0'; /* just in case. */
  65. pos += 13 + 4;
  66. BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0);
  67. pos += size;
  68. /* skip over entry */
  69. BAIL_IF_ERRPASS(!io->seek(io, pos), 0);
  70. } /* while */
  71. return 1;
  72. } /* hogLoadEntries */
  73. static int hog2LoadEntries(PHYSFS_Io *io, void *arc)
  74. {
  75. PHYSFS_uint32 numfiles;
  76. PHYSFS_uint32 pos;
  77. PHYSFS_uint32 i;
  78. BAIL_IF_ERRPASS(!readui32(io, &numfiles), 0);
  79. BAIL_IF_ERRPASS(!readui32(io, &pos), 0);
  80. BAIL_IF_ERRPASS(!io->seek(io, 68), 0); /* skip to end of header. */
  81. for (i = 0; i < numfiles; i++) {
  82. char name[37];
  83. PHYSFS_uint32 reserved;
  84. PHYSFS_uint32 size;
  85. PHYSFS_uint32 mtime;
  86. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 36), 0);
  87. BAIL_IF_ERRPASS(!readui32(io, &reserved), 0);
  88. BAIL_IF_ERRPASS(!readui32(io, &size), 0);
  89. BAIL_IF_ERRPASS(!readui32(io, &mtime), 0);
  90. name[36] = '\0'; /* just in case */
  91. BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, mtime, mtime, pos, size), 0);
  92. pos += size;
  93. }
  94. return 1;
  95. } /* hog2LoadEntries */
  96. static void *HOG_openArchive(PHYSFS_Io *io, const char *name,
  97. int forWriting, int *claimed)
  98. {
  99. PHYSFS_uint8 buf[3];
  100. void *unpkarc = NULL;
  101. int hog1 = 0;
  102. assert(io != NULL); /* shouldn't ever happen. */
  103. BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
  104. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, 3), NULL);
  105. if (memcmp(buf, "DHF", 3) == 0)
  106. hog1 = 1; /* original HOG (Descent 1 and 2) archive */
  107. else
  108. {
  109. BAIL_IF(memcmp(buf, "HOG", 3) != 0, PHYSFS_ERR_UNSUPPORTED, NULL); /* Not HOG2 */
  110. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, 1), NULL);
  111. BAIL_IF(buf[0] != '2', PHYSFS_ERR_UNSUPPORTED, NULL); /* Not HOG2 */
  112. } /* else */
  113. *claimed = 1;
  114. unpkarc = UNPK_openArchive(io);
  115. BAIL_IF_ERRPASS(!unpkarc, NULL);
  116. if (!(hog1 ? hog1LoadEntries(io, unpkarc) : hog2LoadEntries(io, unpkarc)))
  117. {
  118. UNPK_abandonArchive(unpkarc);
  119. return NULL;
  120. } /* if */
  121. return unpkarc;
  122. } /* HOG_openArchive */
  123. const PHYSFS_Archiver __PHYSFS_Archiver_HOG =
  124. {
  125. CURRENT_PHYSFS_ARCHIVER_API_VERSION,
  126. {
  127. "HOG",
  128. "Descent I/II/III HOG file format",
  129. "Bradley Bell <btb@icculus.org>",
  130. "https://icculus.org/physfs/",
  131. 0, /* supportsSymlinks */
  132. },
  133. HOG_openArchive,
  134. UNPK_enumerate,
  135. UNPK_openRead,
  136. UNPK_openWrite,
  137. UNPK_openAppend,
  138. UNPK_remove,
  139. UNPK_mkdir,
  140. UNPK_stat,
  141. UNPK_closeArchive
  142. };
  143. #endif /* defined PHYSFS_SUPPORTS_HOG */
  144. /* end of physfs_archiver_hog.c ... */