archiver_hog.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * HOG support routines for PhysicsFS.
  3. *
  4. * This driver handles Descent I/II HOG archives.
  5. *
  6. * The 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. * Please see the file LICENSE.txt in the source's root directory.
  27. *
  28. * This file written by Bradley Bell.
  29. * Based on grp.c by Ryan C. Gordon.
  30. */
  31. #define __PHYSICSFS_INTERNAL__
  32. #include "physfs_internal.h"
  33. #if PHYSFS_SUPPORTS_HOG
  34. static UNPKentry *hogLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 *_entCount)
  35. {
  36. const PHYSFS_uint64 iolen = io->length(io);
  37. PHYSFS_uint32 entCount = 0;
  38. void *ptr = NULL;
  39. UNPKentry *entries = NULL;
  40. UNPKentry *entry = NULL;
  41. PHYSFS_uint32 size = 0;
  42. PHYSFS_uint32 pos = 3;
  43. while (pos < iolen)
  44. {
  45. entCount++;
  46. ptr = allocator.Realloc(ptr, sizeof (UNPKentry) * entCount);
  47. GOTO_IF(ptr == NULL, PHYSFS_ERR_OUT_OF_MEMORY, failed);
  48. entries = (UNPKentry *) ptr;
  49. entry = &entries[entCount-1];
  50. GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->name, 13), failed);
  51. pos += 13;
  52. GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), failed);
  53. pos += 4;
  54. entry->size = PHYSFS_swapULE32(size);
  55. entry->startPos = pos;
  56. pos += size;
  57. /* skip over entry */
  58. GOTO_IF_ERRPASS(!io->seek(io, pos), failed);
  59. } /* while */
  60. *_entCount = entCount;
  61. return entries;
  62. failed:
  63. allocator.Free(entries);
  64. return NULL;
  65. } /* hogLoadEntries */
  66. static void *HOG_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
  67. {
  68. PHYSFS_uint8 buf[3];
  69. PHYSFS_uint32 count = 0;
  70. UNPKentry *entries = NULL;
  71. assert(io != NULL); /* shouldn't ever happen. */
  72. BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
  73. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, 3), NULL);
  74. BAIL_IF(memcmp(buf, "DHF", 3) != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
  75. entries = hogLoadEntries(io, &count);
  76. BAIL_IF_ERRPASS(!entries, NULL);
  77. return UNPK_openArchive(io, entries, count);
  78. } /* HOG_openArchive */
  79. const PHYSFS_Archiver __PHYSFS_Archiver_HOG =
  80. {
  81. CURRENT_PHYSFS_ARCHIVER_API_VERSION,
  82. {
  83. "HOG",
  84. "Descent I/II HOG file format",
  85. "Bradley Bell <btb@icculus.org>",
  86. "https://icculus.org/physfs/",
  87. 0, /* supportsSymlinks */
  88. },
  89. HOG_openArchive,
  90. UNPK_enumerateFiles,
  91. UNPK_openRead,
  92. UNPK_openWrite,
  93. UNPK_openAppend,
  94. UNPK_remove,
  95. UNPK_mkdir,
  96. UNPK_stat,
  97. UNPK_closeArchive
  98. };
  99. #endif /* defined PHYSFS_SUPPORTS_HOG */
  100. /* end of archiver_hog.c ... */