libgig  4.0.0.6svn2980
RIFF.h
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * libgig - C++ cross-platform Gigasampler format file access library *
4  * *
5  * Copyright (C) 2003-2016 by Christian Schoenebeck *
6  * <cuse@users.sourceforge.net> *
7  * *
8  * This library is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  ***************************************************************************/
23 
24 #ifndef __RIFF_H__
25 #define __RIFF_H__
26 
27 #ifdef WIN32
28 # define POSIX 0
29 #endif
30 
31 #ifndef POSIX
32 # define POSIX 1
33 #endif
34 
35 #ifndef DEBUG
36 # define DEBUG 0
37 #endif
38 
39 #include <string>
40 #include <list>
41 #include <map>
42 #include <set>
43 #include <iostream>
44 
45 #ifdef HAVE_CONFIG_H
46 # include <config.h>
47 #endif
48 
49 #if POSIX
50 # include <sys/types.h>
51 # include <sys/stat.h>
52 # include <fcntl.h>
53 # include <unistd.h>
54 #endif // POSIX
55 
56 #ifdef _MSC_VER
57 // Visual C++ 2008 doesn't have stdint.h
58 typedef __int8 int8_t;
59 typedef __int16 int16_t;
60 typedef __int32 int32_t;
61 typedef __int64 int64_t;
62 typedef unsigned __int8 uint8_t;
63 typedef unsigned __int16 uint16_t;
64 typedef unsigned __int32 uint32_t;
65 typedef unsigned __int64 uint64_t;
66 #else
67 #include <stdint.h>
68 #endif
69 
70 #ifdef WIN32
71 # if (_WIN32 && !_WIN64) || (__GNUC__ && !(__x86_64__ || __ppc64__)) /* if 32 bit windows compilation */
72 # define _WIN32_WINNT 0x0501 /* Win XP (no service pack): required for 32 bit compilation for GetFileSizeEx() to be declared by windows.h */
73 # endif
74 # include <windows.h>
75  typedef unsigned int uint;
76 #endif // WIN32
77 
78 #include <stdio.h>
79 
80 #if WORDS_BIGENDIAN
81 # define CHUNK_ID_RIFF 0x52494646
82 # define CHUNK_ID_RIFX 0x52494658
83 # define CHUNK_ID_LIST 0x4C495354
84 
85 # define LIST_TYPE_INFO 0x494E464F
86 # define CHUNK_ID_ICMT 0x49434D54
87 # define CHUNK_ID_ICOP 0x49434F50
88 # define CHUNK_ID_ICRD 0x49435244
89 # define CHUNK_ID_IENG 0x49454E47
90 # define CHUNK_ID_INAM 0x494E414D
91 # define CHUNK_ID_IPRD 0x49505244
92 # define CHUNK_ID_ISFT 0x49534654
93 
94 # define CHUNK_ID_SMPL 0x736D706C
95 
96 #else // little endian
97 # define CHUNK_ID_RIFF 0x46464952
98 # define CHUNK_ID_RIFX 0x58464952
99 # define CHUNK_ID_LIST 0x5453494C
100 
101 # define LIST_TYPE_INFO 0x4F464E49
102 # define CHUNK_ID_ICMT 0x544D4349
103 # define CHUNK_ID_ICOP 0x504F4349
104 # define CHUNK_ID_ICRD 0x44524349
105 # define CHUNK_ID_IENG 0x474E4549
106 # define CHUNK_ID_INAM 0x4D414E49
107 # define CHUNK_ID_IPRD 0x44525049
108 # define CHUNK_ID_ISFT 0x54465349
109 
110 # define CHUNK_ID_SMPL 0x6C706D73
111 
112 #endif // WORDS_BIGENDIAN
113 
114 #define CHUNK_HEADER_SIZE(fileOffsetSize) (4 + fileOffsetSize)
115 #define LIST_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize)
116 #define RIFF_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize)
117 
137 namespace RIFF {
138 
139  /* just symbol prototyping */
140  class Chunk;
141  class List;
142  class File;
143 
144  typedef std::string String;
145 
147  typedef uint64_t file_offset_t;
148 
150  typedef enum {
154  } stream_mode_t;
155 
157  typedef enum {
161  } stream_state_t;
162 
164  typedef enum {
169  } stream_whence_t;
170 
172  typedef enum {
176  } endian_t;
177 
179  enum layout_t {
182  };
183 
189  };
190 
203  struct progress_t {
204  void (*callback)(progress_t*);
205  float factor;
206  void* custom;
207  float __range_min;
208  float __range_max;
209  progress_t();
210  };
211 
217  class Chunk {
218  public:
219  Chunk(File* pFile, file_offset_t StartPos, List* Parent);
220  String GetChunkIDString() const;
221  uint32_t GetChunkID() const { return ChunkID; }
222  File* GetFile() const { return pFile; }
223  List* GetParent() const { return pParent; }
224  file_offset_t GetSize() const { return ullCurrentChunkSize; }
225  file_offset_t GetNewSize() const { return ullNewChunkSize; }
226  file_offset_t GetPos() const { return ullPos; }
227  file_offset_t GetFilePos() const { return ullStartPos + ullPos; }
228  file_offset_t SetPos(file_offset_t Where, stream_whence_t Whence = stream_start);
229  file_offset_t RemainingBytes() const;
230  stream_state_t GetState() const;
231  file_offset_t Read(void* pData, file_offset_t WordCount, file_offset_t WordSize);
232  file_offset_t ReadInt8(int8_t* pData, file_offset_t WordCount = 1);
233  file_offset_t ReadUint8(uint8_t* pData, file_offset_t WordCount = 1);
234  file_offset_t ReadInt16(int16_t* pData, file_offset_t WordCount = 1);
235  file_offset_t ReadUint16(uint16_t* pData, file_offset_t WordCount = 1);
236  file_offset_t ReadInt32(int32_t* pData, file_offset_t WordCount = 1);
237  file_offset_t ReadUint32(uint32_t* pData, file_offset_t WordCount = 1);
238  int8_t ReadInt8();
239  uint8_t ReadUint8();
240  int16_t ReadInt16();
241  uint16_t ReadUint16();
242  int32_t ReadInt32();
243  uint32_t ReadUint32();
244  void ReadString(String& s, int size);
245  file_offset_t Write(void* pData, file_offset_t WordCount, file_offset_t WordSize);
246  file_offset_t WriteInt8(int8_t* pData, file_offset_t WordCount = 1);
247  file_offset_t WriteUint8(uint8_t* pData, file_offset_t WordCount = 1);
248  file_offset_t WriteInt16(int16_t* pData, file_offset_t WordCount = 1);
249  file_offset_t WriteUint16(uint16_t* pData, file_offset_t WordCount = 1);
250  file_offset_t WriteInt32(int32_t* pData, file_offset_t WordCount = 1);
251  file_offset_t WriteUint32(uint32_t* pData, file_offset_t WordCount = 1);
252  void* LoadChunkData();
253  void ReleaseChunkData();
254  void Resize(file_offset_t NewSize);
255  virtual ~Chunk();
256  protected:
257  uint32_t ChunkID;
258  file_offset_t ullCurrentChunkSize; /* in bytes */
259  file_offset_t ullNewChunkSize; /* in bytes (if chunk was scheduled to be resized) */
262  file_offset_t ullStartPos; /* actual position in file where chunk data (without header) starts */
263  file_offset_t ullPos; /* # of bytes from ulStartPos */
264  uint8_t* pChunkData;
265  file_offset_t ullChunkDataSize;
266 
267  Chunk(File* pFile);
268  Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize);
269  void ReadHeader(file_offset_t filePos);
270  void WriteHeader(file_offset_t filePos);
271  file_offset_t ReadSceptical(void* pData, file_offset_t WordCount, file_offset_t WordSize);
272  inline static String convertToString(uint32_t word) {
273  String result;
274  for (int i = 0; i < 4; i++) {
275  uint8_t byte = *((uint8_t*)(&word) + i);
276  char c = byte;
277  result += c;
278  }
279  return result;
280  }
281  virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize);
282  virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL);
283  virtual void __resetPos();
284 
285  friend class List;
286  };
287 
293  class List : public Chunk {
294  public:
295  List(File* pFile, file_offset_t StartPos, List* Parent);
296  String GetListTypeString() const;
297  uint32_t GetListType() const { return ListType; }
298  Chunk* GetSubChunk(uint32_t ChunkID);
299  List* GetSubList(uint32_t ListType);
300  Chunk* GetFirstSubChunk();
301  Chunk* GetNextSubChunk();
302  List* GetFirstSubList();
303  List* GetNextSubList();
304  size_t CountSubChunks();
305  size_t CountSubChunks(uint32_t ChunkID);
306  size_t CountSubLists();
307  size_t CountSubLists(uint32_t ListType);
308  Chunk* AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize);
309  List* AddSubList(uint32_t uiListType);
310  void DeleteSubChunk(Chunk* pSubChunk);
311  void MoveSubChunk(Chunk* pSrc, Chunk* pDst); // read API doc comments !!!
312  void MoveSubChunk(Chunk* pSrc, List* pNewParent);
313  virtual ~List();
314  protected:
315  typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap;
316  typedef std::list<Chunk*> ChunkList;
317  typedef std::set<Chunk*> ChunkSet;
318 
319  uint32_t ListType;
320  ChunkList* pSubChunks;
321  ChunkMap* pSubChunksMap;
322  ChunkList::iterator ChunksIterator;
323  ChunkList::iterator ListIterator;
324 
325  List(File* pFile);
326  List(File* pFile, List* pParent, uint32_t uiListID);
327  void ReadHeader(file_offset_t filePos);
328  void WriteHeader(file_offset_t filePos);
329  void LoadSubChunks(progress_t* pProgress = NULL);
330  void LoadSubChunksRecursively(progress_t* pProgress = NULL);
331  virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize);
332  virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL);
333  virtual void __resetPos();
334  void DeleteChunkList();
335  };
336 
343  class File : public List {
344  public:
345  File(uint32_t FileType);
346  File(const String& path);
347  File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize = offset_size_auto);
348  stream_mode_t GetMode() const;
349  bool SetMode(stream_mode_t NewMode);
350  void SetByteOrder(endian_t Endian);
351  String GetFileName() const;
352  void SetFileName(const String& path);
353  bool IsNew() const;
354  layout_t GetLayout() const;
355  file_offset_t GetCurrentFileSize() const;
356  file_offset_t GetRequiredFileSize();
357  file_offset_t GetRequiredFileSize(offset_size_t fileOffsetSize);
358  int GetFileOffsetSize() const;
359  int GetRequiredFileOffsetSize();
360 
361  virtual void Save(progress_t* pProgress = NULL);
362  virtual void Save(const String& path, progress_t* pProgress = NULL);
363  virtual ~File();
364  protected:
365  #if POSIX
366  int hFileRead;
368  #elif defined(WIN32)
369  HANDLE hFileRead;
370  HANDLE hFileWrite;
371  #else
372  FILE* hFileRead;
373  FILE* hFileWrite;
374  #endif // POSIX
375  String Filename;
381 
382  friend class Chunk;
383  friend class List;
384  private:
385  stream_mode_t Mode;
386 
387  void __openExistingFile(const String& path, uint32_t* FileType = NULL);
388  void ResizeFile(file_offset_t ullNewSize);
389  #if POSIX
390  file_offset_t __GetFileSize(int hFile) const;
391  #elif defined(WIN32)
392  file_offset_t __GetFileSize(HANDLE hFile) const;
393  #else
394  file_offset_t __GetFileSize(FILE* hFile) const;
395  #endif
396  int FileOffsetSizeFor(file_offset_t fileSize) const;
397  void Cleanup();
398  };
399 
403  class Exception {
404  public:
405  String Message;
406 
407  Exception(String Message) { Exception::Message = Message; }
408  void PrintMessage();
409  virtual ~Exception() {}
410  };
411 
412  String libraryName();
413  String libraryVersion();
414 
415 } // namespace RIFF
416 #endif // __RIFF_H__
file_offset_t GetFilePos() const
Current, actual offset of chunk data body start in file.
Definition: RIFF.h:227
file_offset_t GetNewSize() const
New chunk size if it was modified with Resize(), otherwise value returned will be equal to GetSize()...
Definition: RIFF.h:225
bool bEndianNative
Definition: RIFF.h:376
int FileOffsetSize
Size of file offsets (in bytes) when this file was opened (or saved the last time).
Definition: RIFF.h:380
File * GetFile() const
Returns pointer to the chunk&#39;s File object.
Definition: RIFF.h:222
List * pParent
Definition: RIFF.h:260
stream_whence_t
File stream position dependent to these relations.
Definition: RIFF.h:164
String libraryName()
Returns the name of this C++ library.
Definition: RIFF.cpp:2267
file_offset_t ullCurrentChunkSize
Definition: RIFF.h:258
static String convertToString(uint32_t word)
Definition: RIFF.h:272
layout_t Layout
An ordinary RIFF file is always set to layout_standard.
Definition: RIFF.h:378
void(* callback)(progress_t *)
Callback function pointer which has to be assigned to a function for progress notification.
Definition: RIFF.h:204
stream_state_t
Current state of the file stream.
Definition: RIFF.h:157
bool bIsNewFile
Definition: RIFF.h:377
List * GetParent() const
Returns pointer to the chunk&#39;s parent list chunk.
Definition: RIFF.h:223
String libraryVersion()
Returns version of this C++ library.
Definition: RIFF.cpp:2275
Use 32 bit offsets for files smaller than 4 GB, use 64 bit offsets for files equal or larger than 4 G...
Definition: RIFF.h:186
int hFileWrite
handle / descriptor for writing to (some) file
Definition: RIFF.h:367
String Filename
Definition: RIFF.h:375
file_offset_t GetPos() const
Position within the chunk data body (starting with 0).
Definition: RIFF.h:226
Exception(String Message)
Definition: RIFF.h:407
stream_mode_t
Whether file stream is open in read or in read/write mode.
Definition: RIFF.h:150
std::list< Chunk * > ChunkList
Definition: RIFF.h:316
std::string String
Definition: RIFF.h:142
file_offset_t ullPos
Definition: RIFF.h:263
file_offset_t ullChunkDataSize
Definition: RIFF.h:265
std::set< Chunk * > ChunkSet
Definition: RIFF.h:317
RIFF List Chunk.
Definition: RIFF.h:293
file_offset_t GetSize() const
Chunk size in bytes (without header, thus the chunk data body)
Definition: RIFF.h:224
File * pFile
Definition: RIFF.h:261
offset_size_t
Size of RIFF file offsets used in all RIFF chunks&#39; headers.
Definition: RIFF.h:185
ChunkList::iterator ListIterator
Definition: RIFF.h:323
String Message
Definition: RIFF.h:405
uint64_t file_offset_t
Type used by libgig for handling file positioning during file I/O tasks.
Definition: RIFF.h:147
float __range_min
Only for internal usage, do not modify!
Definition: RIFF.h:207
offset_size_t FileOffsetPreference
Definition: RIFF.h:379
file_offset_t ullStartPos
Definition: RIFF.h:262
layout_t
General RIFF chunk structure of a RIFF file.
Definition: RIFF.h:179
Ordinary RIFF Chunk.
Definition: RIFF.h:217
uint32_t GetChunkID() const
Chunk ID in unsigned integer representation.
Definition: RIFF.h:221
ChunkList::iterator ChunksIterator
Definition: RIFF.h:322
void * custom
This pointer can be used for arbitrary data.
Definition: RIFF.h:206
Used for indicating the progress of a certain task.
Definition: RIFF.h:203
ChunkList * pSubChunks
Definition: RIFF.h:320
file_offset_t ullNewChunkSize
Definition: RIFF.h:259
int hFileRead
handle / descriptor for reading from file
Definition: RIFF.h:366
Not a "real" RIFF file: First chunk in file is an ordinary data chunk, not a List chunk...
Definition: RIFF.h:181
ChunkMap * pSubChunksMap
Definition: RIFF.h:321
uint8_t * pChunkData
Definition: RIFF.h:264
endian_t
Alignment of data bytes in memory (system dependant).
Definition: RIFF.h:172
Standard RIFF file layout: First chunk in file is a List chunk which contains all other chunks and th...
Definition: RIFF.h:180
RIFF File.
Definition: RIFF.h:343
RIFF specific classes and definitions.
Definition: RIFF.h:137
float factor
Reflects current progress as value between 0.0 and 1.0.
Definition: RIFF.h:205
Always use 32 bit offsets (even for files larger than 4 GB).
Definition: RIFF.h:187
virtual ~Exception()
Definition: RIFF.h:409
float __range_max
Only for internal usage, do not modify!
Definition: RIFF.h:208
Will be thrown whenever an error occurs while handling a RIFF file.
Definition: RIFF.h:403
Always use 64 bit offsets (even for files smaller than 4 GB).
Definition: RIFF.h:188
uint32_t GetListType() const
Returns unsigned integer representation of the list&#39;s ID.
Definition: RIFF.h:297
uint32_t ChunkID
Definition: RIFF.h:257
std::map< uint32_t, RIFF::Chunk * > ChunkMap
Definition: RIFF.h:315
uint32_t ListType
Definition: RIFF.h:319