archiver_mvl.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * MVL support routines for PhysicsFS.
  3. *
  4. * This driver handles Descent II Movielib archives.
  5. *
  6. * The file format of MVL is quite easy...
  7. *
  8. * //MVL File format - Written by Heiko Herrmann
  9. * char sig[4] = {'D','M', 'V', 'L'}; // "DMVL"=Descent MoVie Library
  10. *
  11. * int num_files; // the number of files in this MVL
  12. *
  13. * struct {
  14. * char file_name[13]; // Filename, padded to 13 bytes with 0s
  15. * int file_size; // filesize in bytes
  16. * }DIR_STRUCT[num_files];
  17. *
  18. * struct {
  19. * char data[file_size]; // The file data
  20. * }FILE_STRUCT[num_files];
  21. *
  22. * (That info is from http://www.descent2.com/ddn/specs/mvl/)
  23. *
  24. * Please see the file LICENSE.txt in the source's root directory.
  25. *
  26. * This file written by Bradley Bell.
  27. * Based on grp.c by Ryan C. Gordon.
  28. */
  29. #if (defined PHYSFS_SUPPORTS_MVL)
  30. #define __PHYSICSFS_INTERNAL__
  31. #include "physfs_internal.h"
  32. static UNPKentry *mvlLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
  33. {
  34. PHYSFS_uint32 location = 8; /* sizeof sig. */
  35. UNPKentry *entries = NULL;
  36. UNPKentry *entry = NULL;
  37. entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
  38. BAIL_IF_MACRO(entries == NULL, ERR_OUT_OF_MEMORY, NULL);
  39. location += (17 * fileCount);
  40. for (entry = entries; fileCount > 0; fileCount--, entry++)
  41. {
  42. GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 13), NULL, failed);
  43. GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), NULL, failed);
  44. entry->size = PHYSFS_swapULE32(entry->size);
  45. entry->startPos = location;
  46. location += entry->size;
  47. } /* for */
  48. return entries;
  49. failed:
  50. allocator.Free(entries);
  51. return NULL;
  52. } /* mvlLoadEntries */
  53. static void *MVL_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
  54. {
  55. PHYSFS_uint8 buf[4];
  56. PHYSFS_uint32 count = 0;
  57. UNPKentry *entries = NULL;
  58. assert(io != NULL); /* shouldn't ever happen. */
  59. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  60. BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, 4), NULL, NULL);
  61. BAIL_IF_MACRO(memcmp(buf, "DMVL", 4) != 0, ERR_NOT_AN_ARCHIVE, NULL);
  62. BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL, NULL);
  63. count = PHYSFS_swapULE32(count);
  64. entries = mvlLoadEntries(io, count);
  65. BAIL_IF_MACRO(entries == NULL, NULL, NULL);
  66. return UNPK_openArchive(io, entries, count);
  67. } /* MVL_openArchive */
  68. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL =
  69. {
  70. "MVL",
  71. MVL_ARCHIVE_DESCRIPTION,
  72. "Bradley Bell <btb@icculus.org>",
  73. "http://icculus.org/physfs/",
  74. };
  75. const PHYSFS_Archiver __PHYSFS_Archiver_MVL =
  76. {
  77. &__PHYSFS_ArchiveInfo_MVL,
  78. MVL_openArchive, /* openArchive() method */
  79. UNPK_enumerateFiles, /* enumerateFiles() method */
  80. UNPK_openRead, /* openRead() method */
  81. UNPK_openWrite, /* openWrite() method */
  82. UNPK_openAppend, /* openAppend() method */
  83. UNPK_remove, /* remove() method */
  84. UNPK_mkdir, /* mkdir() method */
  85. UNPK_dirClose, /* dirClose() method */
  86. UNPK_stat /* stat() method */
  87. };
  88. #endif /* defined PHYSFS_SUPPORTS_MVL */
  89. /* end of mvl.c ... */