archiver_hog.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 int hogLoadEntries(PHYSFS_Io *io, void *unpkarc)
  35. {
  36. const PHYSFS_uint64 iolen = io->length(io);
  37. PHYSFS_uint32 pos = 3;
  38. while (pos < iolen)
  39. {
  40. PHYSFS_uint32 size;
  41. char name[13];
  42. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 13), 0);
  43. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
  44. name[12] = '\0'; /* just in case. */
  45. pos += 13 + 4;
  46. size = PHYSFS_swapULE32(size);
  47. BAIL_IF_ERRPASS(!UNPK_addEntry(unpkarc, name, 0, pos, size), 0);
  48. pos += size;
  49. /* skip over entry */
  50. BAIL_IF_ERRPASS(!io->seek(io, pos), 0);
  51. } /* while */
  52. return 1;
  53. } /* hogLoadEntries */
  54. static void *HOG_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
  55. {
  56. PHYSFS_uint8 buf[3];
  57. PHYSFS_uint32 count = 0;
  58. void *unpkarc = NULL;
  59. assert(io != NULL); /* shouldn't ever happen. */
  60. BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
  61. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, 3), NULL);
  62. BAIL_IF(memcmp(buf, "DHF", 3) != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
  63. unpkarc = UNPK_openArchive(io, count);
  64. BAIL_IF_ERRPASS(!unpkarc, NULL);
  65. if (!hogLoadEntries(io, unpkarc))
  66. {
  67. UNPK_closeArchive(unpkarc);
  68. return NULL;
  69. } /* if */
  70. return unpkarc;
  71. } /* HOG_openArchive */
  72. const PHYSFS_Archiver __PHYSFS_Archiver_HOG =
  73. {
  74. CURRENT_PHYSFS_ARCHIVER_API_VERSION,
  75. {
  76. "HOG",
  77. "Descent I/II HOG file format",
  78. "Bradley Bell <btb@icculus.org>",
  79. "https://icculus.org/physfs/",
  80. 0, /* supportsSymlinks */
  81. },
  82. HOG_openArchive,
  83. UNPK_enumerateFiles,
  84. UNPK_openRead,
  85. UNPK_openWrite,
  86. UNPK_openAppend,
  87. UNPK_remove,
  88. UNPK_mkdir,
  89. UNPK_stat,
  90. UNPK_closeArchive
  91. };
  92. #endif /* defined PHYSFS_SUPPORTS_HOG */
  93. /* end of archiver_hog.c ... */