archiver_grp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * GRP support routines for PhysicsFS.
  3. *
  4. * This driver handles BUILD engine archives ("groupfiles"). This format
  5. * (but not this driver) was put together by Ken Silverman.
  6. *
  7. * The format is simple enough. In Ken's words:
  8. *
  9. * What's the .GRP file format?
  10. *
  11. * The ".grp" file format is just a collection of a lot of files stored
  12. * into 1 big one. I tried to make the format as simple as possible: The
  13. * first 12 bytes contains my name, "KenSilverman". The next 4 bytes is
  14. * the number of files that were compacted into the group file. Then for
  15. * each file, there is a 16 byte structure, where the first 12 bytes are
  16. * the filename, and the last 4 bytes are the file's size. The rest of
  17. * the group file is just the raw data packed one after the other in the
  18. * same order as the list of files.
  19. *
  20. * (That info is from http://www.advsys.net/ken/build.htm ...)
  21. *
  22. * Please see the file LICENSE.txt in the source's root directory.
  23. *
  24. * This file written by Ryan C. Gordon.
  25. */
  26. #if (defined PHYSFS_SUPPORTS_GRP)
  27. #define __PHYSICSFS_INTERNAL__
  28. #include "physfs_internal.h"
  29. static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
  30. {
  31. return (io->read(io, buf, len) == len);
  32. } /* readAll */
  33. static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
  34. {
  35. PHYSFS_uint32 location = 16; /* sizeof sig. */
  36. UNPKentry *entries = NULL;
  37. UNPKentry *entry = NULL;
  38. char *ptr = NULL;
  39. entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
  40. BAIL_IF_MACRO(entries == NULL, ERR_OUT_OF_MEMORY, NULL);
  41. location += (16 * fileCount);
  42. for (entry = entries; fileCount > 0; fileCount--, entry++)
  43. {
  44. GOTO_IF_MACRO(!readAll(io, &entry->name, 12), NULL, grpLoad_failed);
  45. GOTO_IF_MACRO(!readAll(io, &entry->size, 4), NULL, grpLoad_failed);
  46. entry->name[12] = '\0'; /* name isn't null-terminated in file. */
  47. if ((ptr = strchr(entry->name, ' ')) != NULL)
  48. *ptr = '\0'; /* trim extra spaces. */
  49. entry->size = PHYSFS_swapULE32(entry->size);
  50. entry->startPos = location;
  51. location += entry->size;
  52. } /* for */
  53. return entries;
  54. grpLoad_failed:
  55. allocator.Free(entries);
  56. return NULL;
  57. } /* grpLoadEntries */
  58. static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
  59. {
  60. PHYSFS_uint8 buf[12];
  61. PHYSFS_uint32 entryCount = 0;
  62. UNPKentry *entries = NULL;
  63. assert(io != NULL); /* shouldn't ever happen. */
  64. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  65. BAIL_IF_MACRO(!readAll(io, buf, sizeof (buf)), NULL, NULL);
  66. if (memcmp(buf, "KenSilverman", sizeof (buf)) != 0)
  67. BAIL_MACRO(ERR_NOT_AN_ARCHIVE, NULL);
  68. BAIL_IF_MACRO(!readAll(io, &entryCount, sizeof (entryCount)), NULL, NULL);
  69. entryCount = PHYSFS_swapULE32(entryCount);
  70. entries = grpLoadEntries(io, entryCount);
  71. BAIL_IF_MACRO(entries == NULL, NULL, NULL);
  72. return UNPK_openArchive(io, entries, entryCount);
  73. } /* GRP_openArchive */
  74. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
  75. {
  76. "GRP",
  77. GRP_ARCHIVE_DESCRIPTION,
  78. "Ryan C. Gordon <icculus@icculus.org>",
  79. "http://icculus.org/physfs/",
  80. };
  81. const PHYSFS_Archiver __PHYSFS_Archiver_GRP =
  82. {
  83. &__PHYSFS_ArchiveInfo_GRP,
  84. GRP_openArchive, /* openArchive() method */
  85. UNPK_enumerateFiles, /* enumerateFiles() method */
  86. UNPK_openRead, /* openRead() method */
  87. UNPK_openWrite, /* openWrite() method */
  88. UNPK_openAppend, /* openAppend() method */
  89. UNPK_remove, /* remove() method */
  90. UNPK_mkdir, /* mkdir() method */
  91. UNPK_dirClose, /* dirClose() method */
  92. UNPK_stat /* stat() method */
  93. };
  94. #endif /* defined PHYSFS_SUPPORTS_GRP */
  95. /* end of grp.c ... */