physfs_archiver_vdf.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * VDF support routines for PhysicsFS.
  3. *
  4. * This driver handles Gothic I/II VDF archives.
  5. * This format (but not this driver) was designed by Piranha Bytes for
  6. * use wih the ZenGin engine.
  7. *
  8. * This file was written by Francesco Bertolaccini, based on the UNPK archiver
  9. * by Ryan C. Gordon and the works of degenerated1123 and Nico Bendlin.
  10. */
  11. #define __PHYSICSFS_INTERNAL__
  12. #include "physfs_internal.h"
  13. #if PHYSFS_SUPPORTS_VDF
  14. #include <time.h>
  15. #define VDF_COMMENT_LENGTH 256
  16. #define VDF_SIGNATURE_LENGTH 16
  17. #define VDF_ENTRY_NAME_LENGTH 64
  18. static const char* VDF_SIGNATURE_G1 = "PSVDSC_V2.00\r\n\r\n";
  19. static const char* VDF_SIGNATURE_G2 = "PSVDSC_V2.00\n\r\n\r";
  20. static inline int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val)
  21. {
  22. PHYSFS_uint32 v;
  23. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &v, sizeof (v)), 0);
  24. *val = PHYSFS_swapULE32(v);
  25. return 1;
  26. } /* readui32 */
  27. static PHYSFS_sint64 vdfDosTimeToEpoch(const PHYSFS_uint32 dostime)
  28. {
  29. /* VDF stores timestamps as 32bit DOS dates: the seconds are counted in
  30. 2-seconds intervals and the years are counted since 1 Jan. 1980 */
  31. struct tm t;
  32. memset(&t, '\0', sizeof (t));
  33. t.tm_year = ((int) ((dostime >> 25) & 0x7F)) + 80; /* 1980 to 1900 */
  34. t.tm_mon = ((int) ((dostime >> 21) & 0xF)) - 1; /* 1-12 to 0-11 */
  35. t.tm_mday = (int) ((dostime >> 16) & 0x1F);
  36. t.tm_hour = (int) ((dostime >> 11) & 0x1F);
  37. t.tm_min = (int) ((dostime >> 5) & 0x3F);
  38. t.tm_sec = ((int) ((dostime >> 0) & 0x1F)) * 2; /* 2 seconds to 1. */
  39. return (PHYSFS_sint64) mktime(&t);
  40. } /* vdfDosTimeToEpoch */
  41. static int vdfLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count,
  42. const PHYSFS_sint64 ts, void *arc)
  43. {
  44. PHYSFS_uint32 i;
  45. for (i = 0; i < count; i++)
  46. {
  47. char name[VDF_ENTRY_NAME_LENGTH + 1];
  48. int namei;
  49. PHYSFS_uint32 jump, size, type, attr;
  50. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, sizeof (name) - 1), 0);
  51. BAIL_IF_ERRPASS(!readui32(io, &jump), 0);
  52. BAIL_IF_ERRPASS(!readui32(io, &size), 0);
  53. BAIL_IF_ERRPASS(!readui32(io, &type), 0);
  54. BAIL_IF_ERRPASS(!readui32(io, &attr), 0);
  55. /* Trim whitespace off the end of the filename */
  56. name[VDF_ENTRY_NAME_LENGTH] = '\0'; /* always null-terminated. */
  57. for (namei = VDF_ENTRY_NAME_LENGTH - 1; namei >= 0; namei--)
  58. {
  59. if (name[namei] == ' ')
  60. name[namei] = '\0';
  61. else
  62. break;
  63. } /* for */
  64. BAIL_IF(!name[0], PHYSFS_ERR_CORRUPT, 0);
  65. /* !!! FIXME: we assume the filenames are low-ASCII; if they use
  66. any high-ASCII chars, they will be invalid UTF-8. */
  67. BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, ts, ts, jump, size), 0);
  68. } /* for */
  69. return 1;
  70. } /* vdfLoadEntries */
  71. static void *VDF_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
  72. {
  73. PHYSFS_uint8 ignore[16];
  74. PHYSFS_uint8 sig[VDF_SIGNATURE_LENGTH];
  75. PHYSFS_uint32 count, timestamp, version, dataSize, rootCatOffset;
  76. void *unpkarc;
  77. assert(io != NULL); /* shouldn't ever happen. */
  78. BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
  79. /* skip the 256-byte comment field. */
  80. BAIL_IF_ERRPASS(!io->seek(io, VDF_COMMENT_LENGTH), NULL);
  81. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, sig, sizeof (sig)), NULL);
  82. BAIL_IF_ERRPASS(!readui32(io, &count), NULL);
  83. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, ignore, 4), NULL); /* numFiles */
  84. BAIL_IF_ERRPASS(!readui32(io, &timestamp), NULL);
  85. BAIL_IF_ERRPASS(!readui32(io, &dataSize), NULL); /* dataSize */
  86. BAIL_IF_ERRPASS(!readui32(io, &rootCatOffset), NULL); /* rootCatOff */
  87. BAIL_IF_ERRPASS(!readui32(io, &version), NULL);
  88. BAIL_IF(version != 0x50, PHYSFS_ERR_UNSUPPORTED, NULL);
  89. if ((memcmp(sig, VDF_SIGNATURE_G1, VDF_SIGNATURE_LENGTH) != 0) &&
  90. (memcmp(sig, VDF_SIGNATURE_G2, VDF_SIGNATURE_LENGTH) != 0))
  91. {
  92. BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);
  93. } /* if */
  94. BAIL_IF_ERRPASS(!io->seek(io, rootCatOffset), NULL);
  95. unpkarc = UNPK_openArchive(io);
  96. BAIL_IF_ERRPASS(!unpkarc, NULL);
  97. if (!vdfLoadEntries(io, count, vdfDosTimeToEpoch(timestamp), unpkarc))
  98. {
  99. UNPK_abandonArchive(unpkarc);
  100. return NULL;
  101. } /* if */
  102. return unpkarc;
  103. } /* VDF_openArchive */
  104. const PHYSFS_Archiver __PHYSFS_Archiver_VDF =
  105. {
  106. CURRENT_PHYSFS_ARCHIVER_API_VERSION,
  107. {
  108. "VDF",
  109. "Gothic I/II engine format",
  110. "Francesco Bertolaccini <bertolaccinifrancesco@gmail.com>",
  111. "https://github.com/frabert",
  112. 0, /* supportsSymlinks */
  113. },
  114. VDF_openArchive,
  115. UNPK_enumerateFiles,
  116. UNPK_openRead,
  117. UNPK_openWrite,
  118. UNPK_openAppend,
  119. UNPK_remove,
  120. UNPK_mkdir,
  121. UNPK_stat,
  122. UNPK_closeArchive
  123. };
  124. #endif /* defined PHYSFS_SUPPORTS_VDF */
  125. /* end of physfs_archiver_vdf.c ... */