physfs_archiver_hog.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 *arc)
  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(arc, name, 0, -1, -1, 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. void *unpkarc = NULL;
  58. assert(io != NULL); /* shouldn't ever happen. */
  59. BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
  60. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, 3), NULL);
  61. BAIL_IF(memcmp(buf, "DHF", 3) != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
  62. unpkarc = UNPK_openArchive(io);
  63. BAIL_IF_ERRPASS(!unpkarc, NULL);
  64. if (!hogLoadEntries(io, unpkarc))
  65. {
  66. UNPK_abandonArchive(unpkarc);
  67. return NULL;
  68. } /* if */
  69. return unpkarc;
  70. } /* HOG_openArchive */
  71. const PHYSFS_Archiver __PHYSFS_Archiver_HOG =
  72. {
  73. CURRENT_PHYSFS_ARCHIVER_API_VERSION,
  74. {
  75. "HOG",
  76. "Descent I/II HOG file format",
  77. "Bradley Bell <btb@icculus.org>",
  78. "https://icculus.org/physfs/",
  79. 0, /* supportsSymlinks */
  80. },
  81. HOG_openArchive,
  82. UNPK_enumerate,
  83. UNPK_openRead,
  84. UNPK_openWrite,
  85. UNPK_openAppend,
  86. UNPK_remove,
  87. UNPK_mkdir,
  88. UNPK_stat,
  89. UNPK_closeArchive
  90. };
  91. #endif /* defined PHYSFS_SUPPORTS_HOG */
  92. /* end of physfs_archiver_hog.c ... */