archiver_qpak.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * QPAK support routines for PhysicsFS.
  3. *
  4. * This archiver handles the archive format utilized by Quake 1 and 2.
  5. * Quake3-based games use the PkZip/Info-Zip format (which our zip.c
  6. * archiver handles).
  7. *
  8. * ========================================================================
  9. *
  10. * This format info (in more detail) comes from:
  11. * http://debian.fmi.uni-sofia.bg/~sergei/cgsr/docs/pak.txt
  12. *
  13. * Quake PAK Format
  14. *
  15. * Header
  16. * (4 bytes) signature = 'PACK'
  17. * (4 bytes) directory offset
  18. * (4 bytes) directory length
  19. *
  20. * Directory
  21. * (56 bytes) file name
  22. * (4 bytes) file position
  23. * (4 bytes) file length
  24. *
  25. * ========================================================================
  26. *
  27. * Please see the file LICENSE.txt in the source's root directory.
  28. *
  29. * This file written by Ryan C. Gordon.
  30. */
  31. #define __PHYSICSFS_INTERNAL__
  32. #include "physfs_internal.h"
  33. #if PHYSFS_SUPPORTS_QPAK
  34. #define QPAK_SIG 0x4B434150 /* "PACK" in ASCII. */
  35. static UNPKentry *qpakLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
  36. {
  37. UNPKentry *entries = NULL;
  38. UNPKentry *entry = NULL;
  39. entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
  40. BAIL_IF_MACRO(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  41. for (entry = entries; fileCount > 0; fileCount--, entry++)
  42. {
  43. if (!__PHYSFS_readAll(io, &entry->name, 56)) goto failed;
  44. if (!__PHYSFS_readAll(io, &entry->startPos, 4)) goto failed;
  45. if (!__PHYSFS_readAll(io, &entry->size, 4)) goto failed;
  46. entry->size = PHYSFS_swapULE32(entry->size);
  47. entry->startPos = PHYSFS_swapULE32(entry->startPos);
  48. } /* for */
  49. return entries;
  50. failed:
  51. allocator.Free(entries);
  52. return NULL;
  53. } /* qpakLoadEntries */
  54. static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
  55. {
  56. UNPKentry *entries = NULL;
  57. PHYSFS_uint32 val = 0;
  58. PHYSFS_uint32 pos = 0;
  59. PHYSFS_uint32 count = 0;
  60. assert(io != NULL); /* shouldn't ever happen. */
  61. BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
  62. BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), ERRPASS, NULL);
  63. if (PHYSFS_swapULE32(val) != QPAK_SIG)
  64. BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
  65. BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), ERRPASS, NULL);
  66. pos = PHYSFS_swapULE32(val); /* directory table offset. */
  67. BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), ERRPASS, NULL);
  68. count = PHYSFS_swapULE32(val);
  69. /* corrupted archive? */
  70. BAIL_IF_MACRO((count % 64) != 0, PHYSFS_ERR_CORRUPT, NULL);
  71. count /= 64;
  72. BAIL_IF_MACRO(!io->seek(io, pos), ERRPASS, NULL);
  73. entries = qpakLoadEntries(io, count);
  74. BAIL_IF_MACRO(!entries, ERRPASS, NULL);
  75. return UNPK_openArchive(io, entries, count);
  76. } /* QPAK_openArchive */
  77. const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
  78. {
  79. {
  80. "PAK",
  81. "Quake I/II format",
  82. "Ryan C. Gordon <icculus@icculus.org>",
  83. "http://icculus.org/physfs/",
  84. },
  85. QPAK_openArchive, /* openArchive() method */
  86. UNPK_enumerateFiles, /* enumerateFiles() method */
  87. UNPK_openRead, /* openRead() method */
  88. UNPK_openWrite, /* openWrite() method */
  89. UNPK_openAppend, /* openAppend() method */
  90. UNPK_remove, /* remove() method */
  91. UNPK_mkdir, /* mkdir() method */
  92. UNPK_closeArchive, /* closeArchive() method */
  93. UNPK_stat /* stat() method */
  94. };
  95. #endif /* defined PHYSFS_SUPPORTS_QPAK */
  96. /* end of qpak.c ... */