/[svn]/libgig/trunk/src/RIFF.h
ViewVC logotype

Diff of /libgig/trunk/src/RIFF.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2450 by persson, Wed May 8 17:53:07 2013 UTC revision 3478 by schoenebeck, Thu Feb 21 20:10:08 2019 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file access library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2013 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2019 by Christian Schoenebeck                      *
6   *                              <cuse@users.sourceforge.net>               *   *                              <cuse@users.sourceforge.net>               *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
# Line 36  Line 36 
36  # define DEBUG 0  # define DEBUG 0
37  #endif  #endif
38    
39    #ifndef OVERRIDE
40    # if defined(__cplusplus) && __cplusplus >= 201103L
41    #  define OVERRIDE override
42    # endif
43    #endif
44    
45  #include <string>  #include <string>
46  #include <list>  #include <list>
47  #include <map>  #include <map>
48    #include <set>
49    #include <vector>
50  #include <iostream>  #include <iostream>
51    #include <stdarg.h>
52    
53  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
54  # include <config.h>  # include <config.h>
# Line 52  Line 61 
61  # include <unistd.h>  # include <unistd.h>
62  #endif // POSIX  #endif // POSIX
63    
64  #ifdef _MSC_VER  #if defined _MSC_VER && _MSC_VER < 1600
65  // Visual C++ 2008 doesn't have stdint.h  // Visual C++ 2008 doesn't have stdint.h
66  typedef __int8 int8_t;  typedef __int8 int8_t;
67  typedef __int16 int16_t;  typedef __int16 int16_t;
# Line 67  typedef unsigned __int64 uint64_t; Line 76  typedef unsigned __int64 uint64_t;
76  #endif  #endif
77    
78  #ifdef WIN32  #ifdef WIN32
79    # if (_WIN32 && !_WIN64) || (__GNUC__ && !(__x86_64__ || __ppc64__)) /* if 32 bit windows compilation */
80    #  if _WIN32_WINNT < 0x0501
81    #   undef _WIN32_WINNT
82    #   define _WIN32_WINNT 0x0501 /* Win XP (no service pack): required for 32 bit compilation for GetFileSizeEx() to be declared by windows.h */
83    #  endif
84    # endif
85  # include <windows.h>  # include <windows.h>
86    typedef unsigned int   uint;    typedef unsigned int   uint;
87  #endif // WIN32  #endif // WIN32
# Line 107  typedef unsigned __int64 uint64_t; Line 122  typedef unsigned __int64 uint64_t;
122    
123  #endif // WORDS_BIGENDIAN  #endif // WORDS_BIGENDIAN
124    
125  #define CHUNK_HEADER_SIZE       8  #define CHUNK_HEADER_SIZE(fileOffsetSize)   (4 + fileOffsetSize)
126  #define LIST_HEADER_SIZE        12  #define LIST_HEADER_SIZE(fileOffsetSize)    (8 + fileOffsetSize)
127  #define RIFF_HEADER_SIZE        12  #define RIFF_HEADER_SIZE(fileOffsetSize)    (8 + fileOffsetSize)
   
128    
129  /**  /**
130   * @brief RIFF specific classes and definitions   * @brief RIFF specific classes and definitions
# Line 140  namespace RIFF { Line 154  namespace RIFF {
154    
155      typedef std::string String;      typedef std::string String;
156    
157        /** Type used by libgig for handling file positioning during file I/O tasks. */
158        typedef uint64_t file_offset_t;
159    
160      /** Whether file stream is open in read or in read/write mode. */      /** Whether file stream is open in read or in read/write mode. */
161      typedef enum {      enum stream_mode_t {
162          stream_mode_read       = 0,          stream_mode_read       = 0,
163          stream_mode_read_write = 1,          stream_mode_read_write = 1,
164          stream_mode_closed     = 2          stream_mode_closed     = 2
165      } stream_mode_t;      };
166    
167      /** Current state of the file stream. */      /** Current state of the file stream. */
168      typedef enum {      enum stream_state_t {
169          stream_ready       = 0,          stream_ready       = 0,
170          stream_end_reached = 1,          stream_end_reached = 1,
171          stream_closed      = 2          stream_closed      = 2
172      } stream_state_t;      };
173    
174      /** File stream position dependent to these relations. */      /** File stream position dependent to these relations. */
175      typedef enum {      enum stream_whence_t {
176          stream_start    = 0,          stream_start    = 0,
177          stream_curpos   = 1,          stream_curpos   = 1,
178          stream_backward = 2,          stream_backward = 2,
179          stream_end      = 3          stream_end      = 3
180      } stream_whence_t;      };
181    
182      /** Alignment of data bytes in memory (system dependant). */      /** Alignment of data bytes in memory (system dependant). */
183      typedef enum {      enum endian_t {
184          endian_little = 0,          endian_little = 0,
185          endian_big    = 1,          endian_big    = 1,
186          endian_native = 2          endian_native = 2
187      } endian_t;      };
188    
189        /** General RIFF chunk structure of a RIFF file. */
190        enum layout_t {
191            layout_standard = 0, ///< Standard RIFF file layout: First chunk in file is a List chunk which contains all other chunks and there are no chunks outside the scope of that very first (List) chunk.
192            layout_flat     = 1  ///< Not a "real" RIFF file: First chunk in file is an ordinary data chunk, not a List chunk, and there might be other chunks after that first chunk.
193        };
194    
195        /** Size of RIFF file offsets used in all RIFF chunks' headers. @see File::GetFileOffsetSize() */
196        enum offset_size_t {
197            offset_size_auto  = 0, ///< Use 32 bit offsets for files smaller than 4 GB, use 64 bit offsets for files equal or larger than 4 GB.
198            offset_size_32bit = 4, ///< Always use 32 bit offsets (even for files larger than 4 GB).
199            offset_size_64bit = 8  ///< Always use 64 bit offsets (even for files smaller than 4 GB).
200        };
201    
202        /**
203         * @brief Used for indicating the progress of a certain task.
204         *
205         * The function pointer argument has to be supplied with a valid
206         * function of the given signature which will then be called on
207         * progress changes. An equivalent progress_t structure will be passed
208         * back as argument to the callback function on each progress change.
209         * The factor field of the supplied progress_t structure will then
210         * reflect the current progress as value between 0.0 and 1.0. You might
211         * want to use the custom field for data needed in your callback
212         * function.
213         */
214        struct progress_t {
215            void (*callback)(progress_t*); ///< Callback function pointer which has to be assigned to a function for progress notification.
216            float factor;                  ///< Reflects current progress as value between 0.0 and 1.0.
217            void* custom;                  ///< This pointer can be used for arbitrary data.
218            float __range_min;             ///< Only for internal usage, do not modify!
219            float __range_max;             ///< Only for internal usage, do not modify!
220            progress_t();
221            std::vector<progress_t> subdivide(int iSubtasks);
222        };
223    
224      /** @brief Ordinary RIFF Chunk      /** @brief Ordinary RIFF Chunk
225       *       *
# Line 176  namespace RIFF { Line 228  namespace RIFF {
228       */       */
229      class Chunk {      class Chunk {
230          public:          public:
231              Chunk(File* pFile, unsigned long StartPos, List* Parent);              Chunk(File* pFile, file_offset_t StartPos, List* Parent);
232              String         GetChunkIDString();              String         GetChunkIDString() const;
233              uint32_t       GetChunkID() { return ChunkID; }             ///< Chunk ID in unsigned integer representation.              uint32_t       GetChunkID() const { return ChunkID; }       ///< Chunk ID in unsigned integer representation.
234              List*          GetParent()  { return pParent; }             ///< Returns pointer to the chunk's parent list chunk.              File*          GetFile() const    { return pFile;   }       ///< Returns pointer to the chunk's File object.
235              unsigned long  GetSize()    { return CurrentChunkSize; }    ///< Chunk size in bytes (without header, thus the chunk data body)              List*          GetParent() const  { return pParent; }       ///< Returns pointer to the chunk's parent list chunk.
236              unsigned long  GetNewSize() { return NewChunkSize;     }    ///< New chunk size if it was modified with Resize().              file_offset_t  GetSize() const { return ullCurrentChunkSize; } ///< Chunk size in bytes (without header, thus the chunk data body)
237              unsigned long  GetPos()     { return ulPos; }               ///< Position within the chunk data body              file_offset_t  GetNewSize() const { return ullNewChunkSize; } ///< New chunk size if it was modified with Resize(), otherwise value returned will be equal to GetSize().
238              unsigned long  GetFilePos() { return ulStartPos + ulPos; }  ///< Current, actual offset in file.              file_offset_t  GetPos() const { return ullPos; }            ///< Position within the chunk data body (starting with 0).
239              unsigned long  SetPos(unsigned long Where, stream_whence_t Whence = stream_start);              file_offset_t  GetFilePos() const { return ullStartPos + ullPos; } ///< Current, actual offset of chunk data body start in file.
240              unsigned long  RemainingBytes();              file_offset_t  SetPos(file_offset_t Where, stream_whence_t Whence = stream_start);
241              stream_state_t GetState();              file_offset_t  RemainingBytes() const;
242              unsigned long  Read(void* pData, unsigned long WordCount, unsigned long WordSize);              stream_state_t GetState() const;
243              unsigned long  ReadInt8(int8_t* pData,     unsigned long WordCount = 1);              file_offset_t  Read(void* pData, file_offset_t WordCount, file_offset_t WordSize);
244              unsigned long  ReadUint8(uint8_t* pData,   unsigned long WordCount = 1);              file_offset_t  ReadInt8(int8_t* pData,     file_offset_t WordCount = 1);
245              unsigned long  ReadInt16(int16_t* pData,   unsigned long WordCount = 1);              file_offset_t  ReadUint8(uint8_t* pData,   file_offset_t WordCount = 1);
246              unsigned long  ReadUint16(uint16_t* pData, unsigned long WordCount = 1);              file_offset_t  ReadInt16(int16_t* pData,   file_offset_t WordCount = 1);
247              unsigned long  ReadInt32(int32_t* pData,   unsigned long WordCount = 1);              file_offset_t  ReadUint16(uint16_t* pData, file_offset_t WordCount = 1);
248              unsigned long  ReadUint32(uint32_t* pData, unsigned long WordCount = 1);              file_offset_t  ReadInt32(int32_t* pData,   file_offset_t WordCount = 1);
249                file_offset_t  ReadUint32(uint32_t* pData, file_offset_t WordCount = 1);
250              int8_t         ReadInt8();              int8_t         ReadInt8();
251              uint8_t        ReadUint8();              uint8_t        ReadUint8();
252              int16_t        ReadInt16();              int16_t        ReadInt16();
# Line 201  namespace RIFF { Line 254  namespace RIFF {
254              int32_t        ReadInt32();              int32_t        ReadInt32();
255              uint32_t       ReadUint32();              uint32_t       ReadUint32();
256              void           ReadString(String& s, int size);              void           ReadString(String& s, int size);
257              unsigned long  Write(void* pData, unsigned long WordCount, unsigned long WordSize);              file_offset_t  Write(void* pData, file_offset_t WordCount, file_offset_t WordSize);
258              unsigned long  WriteInt8(int8_t* pData,     unsigned long WordCount = 1);              file_offset_t  WriteInt8(int8_t* pData,     file_offset_t WordCount = 1);
259              unsigned long  WriteUint8(uint8_t* pData,   unsigned long WordCount = 1);              file_offset_t  WriteUint8(uint8_t* pData,   file_offset_t WordCount = 1);
260              unsigned long  WriteInt16(int16_t* pData,   unsigned long WordCount = 1);              file_offset_t  WriteInt16(int16_t* pData,   file_offset_t WordCount = 1);
261              unsigned long  WriteUint16(uint16_t* pData, unsigned long WordCount = 1);              file_offset_t  WriteUint16(uint16_t* pData, file_offset_t WordCount = 1);
262              unsigned long  WriteInt32(int32_t* pData,   unsigned long WordCount = 1);              file_offset_t  WriteInt32(int32_t* pData,   file_offset_t WordCount = 1);
263              unsigned long  WriteUint32(uint32_t* pData, unsigned long WordCount = 1);              file_offset_t  WriteUint32(uint32_t* pData, file_offset_t WordCount = 1);
264              void*          LoadChunkData();              void*          LoadChunkData();
265              void           ReleaseChunkData();              void           ReleaseChunkData();
266              void           Resize(int iNewSize);              void           Resize(file_offset_t NewSize);
267              virtual ~Chunk();              virtual ~Chunk();
268          protected:          protected:
269              uint32_t      ChunkID;              uint32_t      ChunkID;
270              uint32_t      CurrentChunkSize;             /* in bytes */              file_offset_t ullCurrentChunkSize;          /* in bytes */
271              uint32_t      NewChunkSize;                 /* in bytes (if chunk was scheduled to be resized) */              file_offset_t ullNewChunkSize;                      /* in bytes (if chunk was scheduled to be resized) */
272              List*         pParent;              List*         pParent;
273              File*         pFile;              File*         pFile;
274              unsigned long ulStartPos;           /* actual position in file where chunk (without header) starts */              file_offset_t ullStartPos;  /* actual position in file where chunk data (without header) starts */
275              unsigned long ulPos;                /* # of bytes from ulStartPos */              file_offset_t ullPos;       /* # of bytes from ulStartPos */
276              uint8_t*      pChunkData;              uint8_t*      pChunkData;
277              unsigned long ulChunkDataSize;              file_offset_t ullChunkDataSize;
278    
279              Chunk(File* pFile);              Chunk(File* pFile);
280              Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize);              Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize);
281              void          ReadHeader(unsigned long fPos);              void ReadHeader(file_offset_t filePos);
282              void          WriteHeader(unsigned long fPos);              void WriteHeader(file_offset_t filePos);
283              unsigned long ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize);              file_offset_t ReadSceptical(void* pData, file_offset_t WordCount, file_offset_t WordSize);
284              inline void   swapBytes_16(void* Word) {              inline static String convertToString(uint32_t word) {
                 uint8_t byteCache = *((uint8_t*) Word);  
                 *((uint8_t*) Word)     = *((uint8_t*) Word + 1);  
                 *((uint8_t*) Word + 1) = byteCache;  
             }  
             inline void   swapBytes_32(void* Word) {  
                 uint8_t byteCache = *((uint8_t*) Word);  
                 *((uint8_t*) Word)     = *((uint8_t*) Word + 3);  
                 *((uint8_t*) Word + 3) = byteCache;  
                 byteCache = *((uint8_t*) Word + 1);  
                 *((uint8_t*) Word + 1) = *((uint8_t*) Word + 2);  
                 *((uint8_t*) Word + 2) = byteCache;  
             }  
             inline void   swapBytes(void* Word, unsigned long WordSize) {  
                 uint8_t byteCache;  
                 unsigned long lo = 0, hi = WordSize - 1;  
                 for (; lo < hi; hi--, lo++) {  
                     byteCache = *((uint8_t*) Word + lo);  
                     *((uint8_t*) Word + lo) = *((uint8_t*) Word + hi);  
                     *((uint8_t*) Word + hi) = byteCache;  
                 }  
             }  
             inline String convertToString(uint32_t word) {  
285                  String result;                  String result;
286                  for (int i = 0; i < 4; i++) {                  for (int i = 0; i < 4; i++) {
287                      uint8_t byte = *((uint8_t*)(&word) + i);                      uint8_t byte = *((uint8_t*)(&word) + i);
# Line 259  namespace RIFF { Line 290  namespace RIFF {
290                  }                  }
291                  return result;                  return result;
292              }              }
293              virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset);              virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize);
294                virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL);
295              virtual void __resetPos(); ///< Sets Chunk's read/write position to zero.              virtual void __resetPos(); ///< Sets Chunk's read/write position to zero.
296    
297              friend class List;              friend class List;
# Line 272  namespace RIFF { Line 304  namespace RIFF {
304       */       */
305      class List : public Chunk {      class List : public Chunk {
306          public:          public:
307              List(File* pFile, unsigned long StartPos, List* Parent);              List(File* pFile, file_offset_t StartPos, List* Parent);
308              String       GetListTypeString();              String       GetListTypeString() const;
309              uint32_t     GetListType() { return ListType; }   ///< Returns unsigned integer representation of the list's ID              uint32_t     GetListType() const { return ListType; } ///< Returns unsigned integer representation of the list's ID
310              Chunk*       GetSubChunk(uint32_t ChunkID);              Chunk*       GetSubChunk(uint32_t ChunkID);
311              List*        GetSubList(uint32_t ListType);              List*        GetSubList(uint32_t ListType);
312              Chunk*       GetFirstSubChunk();              Chunk*       GetFirstSubChunk();
313              Chunk*       GetNextSubChunk();              Chunk*       GetNextSubChunk();
314              List*        GetFirstSubList();              List*        GetFirstSubList();
315              List*        GetNextSubList();              List*        GetNextSubList();
316              unsigned int CountSubChunks();              size_t       CountSubChunks();
317              unsigned int CountSubChunks(uint32_t ChunkID);              size_t       CountSubChunks(uint32_t ChunkID);
318              unsigned int CountSubLists();              size_t       CountSubLists();
319              unsigned int CountSubLists(uint32_t ListType);              size_t       CountSubLists(uint32_t ListType);
320              Chunk*       AddSubChunk(uint32_t uiChunkID, uint uiBodySize);              Chunk*       AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize);
321              List*        AddSubList(uint32_t uiListType);              List*        AddSubList(uint32_t uiListType);
322              void         DeleteSubChunk(Chunk* pSubChunk);              void         DeleteSubChunk(Chunk* pSubChunk);
323              void         MoveSubChunk(Chunk* pSrc, Chunk* pDst);              void         MoveSubChunk(Chunk* pSrc, Chunk* pDst); // read API doc comments !!!
324                void         MoveSubChunk(Chunk* pSrc, List* pNewParent);
325              virtual ~List();              virtual ~List();
326          protected:          protected:
327              typedef std::map<uint32_t, RIFF::Chunk*>  ChunkMap;              typedef std::map<uint32_t, RIFF::Chunk*>  ChunkMap;
328              typedef std::list<Chunk*>                 ChunkList;              typedef std::list<Chunk*>                 ChunkList;
329                typedef std::set<Chunk*>                  ChunkSet;
330    
331              uint32_t   ListType;              uint32_t   ListType;
332              ChunkList* pSubChunks;              ChunkList* pSubChunks;
# Line 302  namespace RIFF { Line 336  namespace RIFF {
336    
337              List(File* pFile);              List(File* pFile);
338              List(File* pFile, List* pParent, uint32_t uiListID);              List(File* pFile, List* pParent, uint32_t uiListID);
339              void ReadHeader(unsigned long fPos);              void ReadHeader(file_offset_t filePos);
340              void WriteHeader(unsigned long fPos);              void WriteHeader(file_offset_t filePos);
341              void LoadSubChunks();              void LoadSubChunks(progress_t* pProgress = NULL);
342              void LoadSubChunksRecursively();              void LoadSubChunksRecursively(progress_t* pProgress = NULL);
343              virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset);              virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize);
344                virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL);
345              virtual void __resetPos(); ///< Sets List Chunk's read/write position to zero and causes all sub chunks to do the same.              virtual void __resetPos(); ///< Sets List Chunk's read/write position to zero and causes all sub chunks to do the same.
346              void DeleteChunkList();              void DeleteChunkList();
347      };      };
# Line 321  namespace RIFF { Line 356  namespace RIFF {
356          public:          public:
357              File(uint32_t FileType);              File(uint32_t FileType);
358              File(const String& path);              File(const String& path);
359              stream_mode_t GetMode();              File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize = offset_size_auto);
360                stream_mode_t GetMode() const;
361              bool          SetMode(stream_mode_t NewMode);              bool          SetMode(stream_mode_t NewMode);
362              void SetByteOrder(endian_t Endian);              void SetByteOrder(endian_t Endian);
363              String GetFileName();              String GetFileName() const;
364              virtual void Save();              void SetFileName(const String& path);
365              virtual void Save(const String& path);              bool IsNew() const;
366                layout_t GetLayout() const;
367                file_offset_t GetCurrentFileSize() const;
368                file_offset_t GetRequiredFileSize();
369                file_offset_t GetRequiredFileSize(offset_size_t fileOffsetSize);
370                int GetFileOffsetSize() const;
371                int GetRequiredFileOffsetSize();
372    
373                virtual void Save(progress_t* pProgress = NULL);
374                virtual void Save(const String& path, progress_t* pProgress = NULL);
375              virtual ~File();              virtual ~File();
376          protected:          protected:
377              #if POSIX              #if POSIX
# Line 341  namespace RIFF { Line 386  namespace RIFF {
386              #endif // POSIX              #endif // POSIX
387              String Filename;              String Filename;
388              bool   bEndianNative;              bool   bEndianNative;
389                bool   bIsNewFile;
390                layout_t Layout; ///< An ordinary RIFF file is always set to layout_standard.
391                offset_size_t FileOffsetPreference;
392                int FileOffsetSize; ///< Size of file offsets (in bytes) when this file was opened (or saved the last time).
393    
             void LogAsResized(Chunk* pResizedChunk);  
             void UnlogResized(Chunk* pResizedChunk);  
394              friend class Chunk;              friend class Chunk;
395              friend class List;              friend class List;
396          private:          private:
397              stream_mode_t  Mode;              stream_mode_t  Mode;
             ChunkList ResizedChunks; ///< All chunks which have been resized (enlarged / shortened).  
398    
399              unsigned long GetFileSize();              void __openExistingFile(const String& path, uint32_t* FileType = NULL);
400              void ResizeFile(unsigned long ulNewSize);              void ResizeFile(file_offset_t ullNewSize);
401              #if POSIX              #if POSIX
402              unsigned long __GetFileSize(int hFile);              file_offset_t __GetFileSize(int hFile) const;
403              #elif defined(WIN32)              #elif defined(WIN32)
404              unsigned long __GetFileSize(HANDLE hFile);              file_offset_t __GetFileSize(HANDLE hFile) const;
405              #else              #else
406              unsigned long __GetFileSize(FILE* hFile);              file_offset_t __GetFileSize(FILE* hFile) const;
407              #endif              #endif
408                int FileOffsetSizeFor(file_offset_t fileSize) const;
409              void Cleanup();              void Cleanup();
410      };      };
411    
# Line 369  namespace RIFF { Line 416  namespace RIFF {
416          public:          public:
417              String Message;              String Message;
418    
419              Exception(String Message) { Exception::Message = Message; }              Exception(String format, ...);
420                Exception(String format, va_list arg);
421              void PrintMessage();              void PrintMessage();
422              virtual ~Exception() {}              virtual ~Exception() {}
423    
424            protected:
425                Exception();
426                static String assemble(String format, va_list arg);
427      };      };
428    
429      String libraryName();      String libraryName();

Legend:
Removed from v.2450  
changed lines
  Added in v.3478

  ViewVC Help
Powered by ViewVC