archiver_grp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #define __PHYSICSFS_INTERNAL__
  27. #include "physfs_internal.h"
  28. #if PHYSFS_SUPPORTS_GRP
  29. static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
  30. {
  31. PHYSFS_uint32 location = 16; /* sizeof sig. */
  32. UNPKentry *entries = NULL;
  33. UNPKentry *entry = NULL;
  34. char *ptr = NULL;
  35. entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
  36. BAIL_IF_MACRO(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  37. location += (16 * fileCount);
  38. for (entry = entries; fileCount > 0; fileCount--, entry++)
  39. {
  40. GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 12), ERRPASS, failed);
  41. GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), ERRPASS, failed);
  42. entry->name[12] = '\0'; /* name isn't null-terminated in file. */
  43. if ((ptr = strchr(entry->name, ' ')) != NULL)
  44. *ptr = '\0'; /* trim extra spaces. */
  45. entry->size = PHYSFS_swapULE32(entry->size);
  46. entry->startPos = location;
  47. location += entry->size;
  48. } /* for */
  49. return entries;
  50. failed:
  51. allocator.Free(entries);
  52. return NULL;
  53. } /* grpLoadEntries */
  54. static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
  55. {
  56. PHYSFS_uint8 buf[12];
  57. PHYSFS_uint32 count = 0;
  58. UNPKentry *entries = NULL;
  59. assert(io != NULL); /* shouldn't ever happen. */
  60. BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
  61. BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, sizeof (buf)), ERRPASS, NULL);
  62. if (memcmp(buf, "KenSilverman", sizeof (buf)) != 0)
  63. BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
  64. BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof(count)), ERRPASS, NULL);
  65. count = PHYSFS_swapULE32(count);
  66. entries = grpLoadEntries(io, count);
  67. BAIL_IF_MACRO(!entries, ERRPASS, NULL);
  68. return UNPK_openArchive(io, entries, count);
  69. } /* GRP_openArchive */
  70. const PHYSFS_Archiver __PHYSFS_Archiver_GRP =
  71. {
  72. CURRENT_PHYSFS_ARCHIVER_API_VERSION,
  73. {
  74. "GRP",
  75. "Build engine Groupfile format",
  76. "Ryan C. Gordon <[email protected]>",
  77. "http://icculus.org/physfs/",
  78. 0, /* supportsSymlinks */
  79. },
  80. GRP_openArchive,
  81. UNPK_enumerateFiles,
  82. UNPK_openRead,
  83. UNPK_openWrite,
  84. UNPK_openAppend,
  85. UNPK_remove,
  86. UNPK_mkdir,
  87. UNPK_stat,
  88. UNPK_closeArchive
  89. };
  90. #endif /* defined PHYSFS_SUPPORTS_GRP */
  91. /* end of archiver_grp.c ... */