IFF.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #define AI_IFF_FOURCC(a,b,c,d) ((uint32_t) (((uint8_t)a << 24u) | \
  30. ((uint8_t)b << 16u) | ((uint8_t)c << 8u) | ((uint8_t)d)))
  31. #define AI_IFF_FOURCC_FORM AI_IFF_FOURCC('F','O','R','M')
  32. /////////////////////////////////////////////////////////////////////////////////
  33. //! Load a chunk header
  34. //! @param outFile Pointer to the file data - points to the chunk data afterwards
  35. //! @return Copy of the chunk header
  36. /////////////////////////////////////////////////////////////////////////////////
  37. inline ChunkHeader LoadChunk(uint8_t*& outFile)
  38. {
  39. ChunkHeader head;
  40. ::memcpy(&head.type, outFile, 4);
  41. outFile += 4;
  42. ::memcpy(&head.length, outFile, 4);
  43. outFile += 4;
  44. AI_LSWAP4(head.length);
  45. AI_LSWAP4(head.type);
  46. return head;
  47. }
  48. /////////////////////////////////////////////////////////////////////////////////
  49. //! Load a sub chunk header
  50. //! @param outFile Pointer to the file data - points to the chunk data afterwards
  51. //! @return Copy of the sub chunk header
  52. /////////////////////////////////////////////////////////////////////////////////
  53. inline SubChunkHeader LoadSubChunk(uint8_t*& outFile)
  54. {
  55. SubChunkHeader head;
  56. ::memcpy(&head.type, outFile, 4);
  57. outFile += 4;
  58. ::memcpy(&head.length, outFile, 2);
  59. outFile += 2;
  60. AI_LSWAP2(head.length);
  61. AI_LSWAP4(head.type);
  62. return head;
  63. }
  64. /////////////////////////////////////////////////////////////////////////////////
  65. //! Read the file header and return the type of the file and its size
  66. //! @param outFile Pointer to the file data. The buffer must at
  67. //! least be 12 bytes large.
  68. //! @param fileType Receives the type of the file
  69. //! @return 0 if everything was OK, otherwise an error message
  70. /////////////////////////////////////////////////////////////////////////////////
  71. inline const char* ReadHeader(uint8_t* outFile, uint32_t& fileType)
  72. {
  73. ChunkHeader head = LoadChunk(outFile);
  74. if(AI_IFF_FOURCC_FORM != head.type)
  75. {
  76. return "The file is not an IFF file: FORM chunk is missing";
  77. }
  78. ::memcpy(&fileType, outFile, 4);
  79. AI_LSWAP4(fileType);
  80. return 0;
  81. }
  82. }}
  83. #endif // !! AI_IFF_H_INCLUDED