IFF.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Definitions for the Interchange File Format (IFF)
  2. // Alexander Gessler, 2006
  3. // Adapted to Assimp August 2008
  4. #ifndef AI_IFF_H_INCLUDED
  5. #define AI_IFF_H_INCLUDED
  6. #include <assimp/ByteSwapper.h>
  7. namespace Assimp {
  8. namespace IFF {
  9. /////////////////////////////////////////////////////////////////////////////////
  10. //! Describes an IFF chunk header
  11. /////////////////////////////////////////////////////////////////////////////////
  12. struct ChunkHeader
  13. {
  14. //! Type of the chunk header - FourCC
  15. uint32_t type;
  16. //! Length of the chunk data, in bytes
  17. uint32_t length;
  18. };
  19. /////////////////////////////////////////////////////////////////////////////////
  20. //! Describes an IFF sub chunk header
  21. /////////////////////////////////////////////////////////////////////////////////
  22. struct SubChunkHeader
  23. {
  24. //! Type of the chunk header - FourCC
  25. uint32_t type;
  26. //! Length of the chunk data, in bytes
  27. uint16_t length;
  28. };
  29. /////////////////////////////////////////////////////////////////////////////////
  30. //! Describes an IFF form header
  31. /////////////////////////////////////////////////////////////////////////////////
  32. struct FormHeader
  33. {
  34. //! Length of the chunk data, in bytes
  35. uint32_t length;
  36. //! Type of the chunk header - FourCC
  37. uint32_t type;
  38. };
  39. #define AI_IFF_FOURCC(a,b,c,d) ((uint32_t) (((uint8_t)a << 24u) | \
  40. ((uint8_t)b << 16u) | ((uint8_t)c << 8u) | ((uint8_t)d)))
  41. #define AI_IFF_FOURCC_FORM AI_IFF_FOURCC('F','O','R','M')
  42. /////////////////////////////////////////////////////////////////////////////////
  43. //! Load a chunk header
  44. //! @param outFile Pointer to the file data - points to the chunk data afterwards
  45. //! @return Copy of the chunk header
  46. /////////////////////////////////////////////////////////////////////////////////
  47. inline ChunkHeader LoadChunk(uint8_t*& outFile)
  48. {
  49. ChunkHeader head;
  50. ::memcpy(&head.type, outFile, 4);
  51. outFile += 4;
  52. ::memcpy(&head.length, outFile, 4);
  53. outFile += 4;
  54. AI_LSWAP4(head.length);
  55. AI_LSWAP4(head.type);
  56. return head;
  57. }
  58. /////////////////////////////////////////////////////////////////////////////////
  59. //! Load a sub chunk header
  60. //! @param outFile Pointer to the file data - points to the chunk data afterwards
  61. //! @return Copy of the sub chunk header
  62. /////////////////////////////////////////////////////////////////////////////////
  63. inline SubChunkHeader LoadSubChunk(uint8_t*& outFile)
  64. {
  65. SubChunkHeader head;
  66. ::memcpy(&head.type, outFile, 4);
  67. outFile += 4;
  68. ::memcpy(&head.length, outFile, 2);
  69. outFile += 2;
  70. AI_LSWAP2(head.length);
  71. AI_LSWAP4(head.type);
  72. return head;
  73. }
  74. /////////////////////////////////////////////////////////////////////////////////
  75. //! Load a chunk header
  76. //! @param outFile Pointer to the file data - points to the chunk data afterwards
  77. //! @return Copy of the chunk header
  78. /////////////////////////////////////////////////////////////////////////////////
  79. inline ChunkHeader LoadForm(uint8_t*& outFile)
  80. {
  81. ChunkHeader head;
  82. outFile += 4;
  83. ::memcpy(&head.length, outFile, 4);
  84. outFile += 4;
  85. ::memcpy(&head.type, outFile, 4);
  86. AI_LSWAP4(head.length);
  87. AI_LSWAP4(head.type);
  88. return head;
  89. }
  90. /////////////////////////////////////////////////////////////////////////////////
  91. //! Read the file header and return the type of the file and its size
  92. //! @param outFile Pointer to the file data. The buffer must at
  93. //! least be 12 bytes large.
  94. //! @param fileType Receives the type of the file
  95. //! @return 0 if everything was OK, otherwise an error message
  96. /////////////////////////////////////////////////////////////////////////////////
  97. inline const char* ReadHeader(uint8_t* outFile, uint32_t& fileType)
  98. {
  99. ChunkHeader head = LoadChunk(outFile);
  100. if(AI_IFF_FOURCC_FORM != head.type)
  101. {
  102. return "The file is not an IFF file: FORM chunk is missing";
  103. }
  104. ::memcpy(&fileType, outFile, 4);
  105. AI_LSWAP4(fileType);
  106. return nullptr;
  107. }
  108. }}
  109. #endif // !! AI_IFF_H_INCLUDED