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-2011 by Christian Schoenebeck * |
* Copyright (C) 2003-2016 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 * |
39 |
#include <string> |
#include <string> |
40 |
#include <list> |
#include <list> |
41 |
#include <map> |
#include <map> |
42 |
|
#include <set> |
43 |
#include <iostream> |
#include <iostream> |
44 |
|
|
45 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
108 |
|
|
109 |
#endif // WORDS_BIGENDIAN |
#endif // WORDS_BIGENDIAN |
110 |
|
|
111 |
#define CHUNK_HEADER_SIZE 8 |
#define CHUNK_HEADER_SIZE(fileOffsetSize) (4 + fileOffsetSize) |
112 |
#define LIST_HEADER_SIZE 12 |
#define LIST_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize) |
113 |
#define RIFF_HEADER_SIZE 12 |
#define RIFF_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize) |
|
|
|
114 |
|
|
115 |
/** |
/** |
116 |
* @brief RIFF specific classes and definitions |
* @brief RIFF specific classes and definitions |
140 |
|
|
141 |
typedef std::string String; |
typedef std::string String; |
142 |
|
|
143 |
|
/** Type used by libgig for handling file positioning during file I/O tasks. */ |
144 |
|
typedef uint64_t file_offset_t; |
145 |
|
|
146 |
/** Whether file stream is open in read or in read/write mode. */ |
/** Whether file stream is open in read or in read/write mode. */ |
147 |
typedef enum { |
typedef enum { |
148 |
stream_mode_read = 0, |
stream_mode_read = 0, |
172 |
endian_native = 2 |
endian_native = 2 |
173 |
} endian_t; |
} endian_t; |
174 |
|
|
175 |
|
/** General RIFF chunk structure of a RIFF file. */ |
176 |
|
enum layout_t { |
177 |
|
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. |
178 |
|
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. |
179 |
|
}; |
180 |
|
|
181 |
|
/** Size of RIFF file offsets used in all RIFF chunks' headers. @see File::GetFileOffsetSize() */ |
182 |
|
enum offset_size_t { |
183 |
|
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. |
184 |
|
offset_size_32bit = 4, ///< Always use 32 bit offsets (even for files larger than 4 GB). |
185 |
|
offset_size_64bit = 8 ///< Always use 64 bit offsets (even for files smaller than 4 GB). |
186 |
|
}; |
187 |
|
|
188 |
|
/** |
189 |
|
* @brief Used for indicating the progress of a certain task. |
190 |
|
* |
191 |
|
* The function pointer argument has to be supplied with a valid |
192 |
|
* function of the given signature which will then be called on |
193 |
|
* progress changes. An equivalent progress_t structure will be passed |
194 |
|
* back as argument to the callback function on each progress change. |
195 |
|
* The factor field of the supplied progress_t structure will then |
196 |
|
* reflect the current progress as value between 0.0 and 1.0. You might |
197 |
|
* want to use the custom field for data needed in your callback |
198 |
|
* function. |
199 |
|
*/ |
200 |
|
struct progress_t { |
201 |
|
void (*callback)(progress_t*); ///< Callback function pointer which has to be assigned to a function for progress notification. |
202 |
|
float factor; ///< Reflects current progress as value between 0.0 and 1.0. |
203 |
|
void* custom; ///< This pointer can be used for arbitrary data. |
204 |
|
float __range_min; ///< Only for internal usage, do not modify! |
205 |
|
float __range_max; ///< Only for internal usage, do not modify! |
206 |
|
progress_t(); |
207 |
|
}; |
208 |
|
|
209 |
/** @brief Ordinary RIFF Chunk |
/** @brief Ordinary RIFF Chunk |
210 |
* |
* |
211 |
* Provides convenient methods to access data of ordinary RIFF chunks |
* Provides convenient methods to access data of ordinary RIFF chunks |
213 |
*/ |
*/ |
214 |
class Chunk { |
class Chunk { |
215 |
public: |
public: |
216 |
Chunk(File* pFile, unsigned long StartPos, List* Parent); |
Chunk(File* pFile, file_offset_t StartPos, List* Parent); |
217 |
String GetChunkIDString(); |
String GetChunkIDString() const; |
218 |
uint32_t GetChunkID() { return ChunkID; } ///< Chunk ID in unsigned integer representation. |
uint32_t GetChunkID() const { return ChunkID; } ///< Chunk ID in unsigned integer representation. |
219 |
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. |
220 |
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. |
221 |
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) |
222 |
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(). |
223 |
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). |
224 |
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. |
225 |
unsigned long RemainingBytes(); |
file_offset_t SetPos(file_offset_t Where, stream_whence_t Whence = stream_start); |
226 |
stream_state_t GetState(); |
file_offset_t RemainingBytes() const; |
227 |
unsigned long Read(void* pData, unsigned long WordCount, unsigned long WordSize); |
stream_state_t GetState() const; |
228 |
unsigned long ReadInt8(int8_t* pData, unsigned long WordCount = 1); |
file_offset_t Read(void* pData, file_offset_t WordCount, file_offset_t WordSize); |
229 |
unsigned long ReadUint8(uint8_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadInt8(int8_t* pData, file_offset_t WordCount = 1); |
230 |
unsigned long ReadInt16(int16_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadUint8(uint8_t* pData, file_offset_t WordCount = 1); |
231 |
unsigned long ReadUint16(uint16_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadInt16(int16_t* pData, file_offset_t WordCount = 1); |
232 |
unsigned long ReadInt32(int32_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadUint16(uint16_t* pData, file_offset_t WordCount = 1); |
233 |
unsigned long ReadUint32(uint32_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadInt32(int32_t* pData, file_offset_t WordCount = 1); |
234 |
|
file_offset_t ReadUint32(uint32_t* pData, file_offset_t WordCount = 1); |
235 |
int8_t ReadInt8(); |
int8_t ReadInt8(); |
236 |
uint8_t ReadUint8(); |
uint8_t ReadUint8(); |
237 |
int16_t ReadInt16(); |
int16_t ReadInt16(); |
238 |
uint16_t ReadUint16(); |
uint16_t ReadUint16(); |
239 |
int32_t ReadInt32(); |
int32_t ReadInt32(); |
240 |
uint32_t ReadUint32(); |
uint32_t ReadUint32(); |
241 |
unsigned long Write(void* pData, unsigned long WordCount, unsigned long WordSize); |
void ReadString(String& s, int size); |
242 |
unsigned long WriteInt8(int8_t* pData, unsigned long WordCount = 1); |
file_offset_t Write(void* pData, file_offset_t WordCount, file_offset_t WordSize); |
243 |
unsigned long WriteUint8(uint8_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteInt8(int8_t* pData, file_offset_t WordCount = 1); |
244 |
unsigned long WriteInt16(int16_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteUint8(uint8_t* pData, file_offset_t WordCount = 1); |
245 |
unsigned long WriteUint16(uint16_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteInt16(int16_t* pData, file_offset_t WordCount = 1); |
246 |
unsigned long WriteInt32(int32_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteUint16(uint16_t* pData, file_offset_t WordCount = 1); |
247 |
unsigned long WriteUint32(uint32_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteInt32(int32_t* pData, file_offset_t WordCount = 1); |
248 |
|
file_offset_t WriteUint32(uint32_t* pData, file_offset_t WordCount = 1); |
249 |
void* LoadChunkData(); |
void* LoadChunkData(); |
250 |
void ReleaseChunkData(); |
void ReleaseChunkData(); |
251 |
void Resize(int iNewSize); |
void Resize(file_offset_t NewSize); |
252 |
virtual ~Chunk(); |
virtual ~Chunk(); |
253 |
protected: |
protected: |
254 |
uint32_t ChunkID; |
uint32_t ChunkID; |
255 |
uint32_t CurrentChunkSize; /* in bytes */ |
file_offset_t ullCurrentChunkSize; /* in bytes */ |
256 |
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) */ |
257 |
List* pParent; |
List* pParent; |
258 |
File* pFile; |
File* pFile; |
259 |
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 */ |
260 |
unsigned long ulPos; /* # of bytes from ulStartPos */ |
file_offset_t ullPos; /* # of bytes from ulStartPos */ |
261 |
uint8_t* pChunkData; |
uint8_t* pChunkData; |
262 |
unsigned long ulChunkDataSize; |
file_offset_t ullChunkDataSize; |
263 |
|
|
264 |
Chunk(File* pFile); |
Chunk(File* pFile); |
265 |
Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize); |
Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize); |
266 |
void ReadHeader(unsigned long fPos); |
void ReadHeader(file_offset_t filePos); |
267 |
void WriteHeader(unsigned long fPos); |
void WriteHeader(file_offset_t filePos); |
268 |
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); |
269 |
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) { |
|
270 |
String result; |
String result; |
271 |
for (int i = 0; i < 4; i++) { |
for (int i = 0; i < 4; i++) { |
272 |
uint8_t byte = *((uint8_t*)(&word) + i); |
uint8_t byte = *((uint8_t*)(&word) + i); |
275 |
} |
} |
276 |
return result; |
return result; |
277 |
} |
} |
278 |
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); |
virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize); |
279 |
|
virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL); |
280 |
virtual void __resetPos(); ///< Sets Chunk's read/write position to zero. |
virtual void __resetPos(); ///< Sets Chunk's read/write position to zero. |
281 |
|
|
282 |
friend class List; |
friend class List; |
289 |
*/ |
*/ |
290 |
class List : public Chunk { |
class List : public Chunk { |
291 |
public: |
public: |
292 |
List(File* pFile, unsigned long StartPos, List* Parent); |
List(File* pFile, file_offset_t StartPos, List* Parent); |
293 |
String GetListTypeString(); |
String GetListTypeString() const; |
294 |
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 |
295 |
Chunk* GetSubChunk(uint32_t ChunkID); |
Chunk* GetSubChunk(uint32_t ChunkID); |
296 |
List* GetSubList(uint32_t ListType); |
List* GetSubList(uint32_t ListType); |
297 |
Chunk* GetFirstSubChunk(); |
Chunk* GetFirstSubChunk(); |
302 |
unsigned int CountSubChunks(uint32_t ChunkID); |
unsigned int CountSubChunks(uint32_t ChunkID); |
303 |
unsigned int CountSubLists(); |
unsigned int CountSubLists(); |
304 |
unsigned int CountSubLists(uint32_t ListType); |
unsigned int CountSubLists(uint32_t ListType); |
305 |
Chunk* AddSubChunk(uint32_t uiChunkID, uint uiBodySize); |
Chunk* AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize); |
306 |
List* AddSubList(uint32_t uiListType); |
List* AddSubList(uint32_t uiListType); |
307 |
void DeleteSubChunk(Chunk* pSubChunk); |
void DeleteSubChunk(Chunk* pSubChunk); |
308 |
void MoveSubChunk(Chunk* pSrc, Chunk* pDst); |
void MoveSubChunk(Chunk* pSrc, Chunk* pDst); // read API doc comments !!! |
309 |
|
void MoveSubChunk(Chunk* pSrc, List* pNewParent); |
310 |
virtual ~List(); |
virtual ~List(); |
311 |
protected: |
protected: |
312 |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
313 |
typedef std::list<Chunk*> ChunkList; |
typedef std::list<Chunk*> ChunkList; |
314 |
|
typedef std::set<Chunk*> ChunkSet; |
315 |
|
|
316 |
uint32_t ListType; |
uint32_t ListType; |
317 |
ChunkList* pSubChunks; |
ChunkList* pSubChunks; |
321 |
|
|
322 |
List(File* pFile); |
List(File* pFile); |
323 |
List(File* pFile, List* pParent, uint32_t uiListID); |
List(File* pFile, List* pParent, uint32_t uiListID); |
324 |
void ReadHeader(unsigned long fPos); |
void ReadHeader(file_offset_t filePos); |
325 |
void WriteHeader(unsigned long fPos); |
void WriteHeader(file_offset_t filePos); |
326 |
void LoadSubChunks(); |
void LoadSubChunks(progress_t* pProgress = NULL); |
327 |
void LoadSubChunksRecursively(); |
void LoadSubChunksRecursively(progress_t* pProgress = NULL); |
328 |
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); |
virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize); |
329 |
|
virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL); |
330 |
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. |
331 |
void DeleteChunkList(); |
void DeleteChunkList(); |
332 |
}; |
}; |
341 |
public: |
public: |
342 |
File(uint32_t FileType); |
File(uint32_t FileType); |
343 |
File(const String& path); |
File(const String& path); |
344 |
stream_mode_t GetMode(); |
File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize = offset_size_auto); |
345 |
|
stream_mode_t GetMode() const; |
346 |
bool SetMode(stream_mode_t NewMode); |
bool SetMode(stream_mode_t NewMode); |
347 |
void SetByteOrder(endian_t Endian); |
void SetByteOrder(endian_t Endian); |
348 |
String GetFileName(); |
String GetFileName() const; |
349 |
virtual void Save(); |
void SetFileName(const String& path); |
350 |
virtual void Save(const String& path); |
bool IsNew() const; |
351 |
|
layout_t GetLayout() const; |
352 |
|
file_offset_t GetCurrentFileSize() const; |
353 |
|
file_offset_t GetRequiredFileSize(); |
354 |
|
file_offset_t GetRequiredFileSize(offset_size_t fileOffsetSize); |
355 |
|
int GetFileOffsetSize() const; |
356 |
|
int GetRequiredFileOffsetSize(); |
357 |
|
|
358 |
|
virtual void Save(progress_t* pProgress = NULL); |
359 |
|
virtual void Save(const String& path, progress_t* pProgress = NULL); |
360 |
virtual ~File(); |
virtual ~File(); |
361 |
protected: |
protected: |
362 |
#if POSIX |
#if POSIX |
371 |
#endif // POSIX |
#endif // POSIX |
372 |
String Filename; |
String Filename; |
373 |
bool bEndianNative; |
bool bEndianNative; |
374 |
|
bool bIsNewFile; |
375 |
|
layout_t Layout; ///< An ordinary RIFF file is always set to layout_standard. |
376 |
|
offset_size_t FileOffsetPreference; |
377 |
|
int FileOffsetSize; ///< Size of file offsets (in bytes) when this file was opened (or saved the last time). |
378 |
|
|
|
void LogAsResized(Chunk* pResizedChunk); |
|
|
void UnlogResized(Chunk* pResizedChunk); |
|
379 |
friend class Chunk; |
friend class Chunk; |
380 |
friend class List; |
friend class List; |
381 |
private: |
private: |
382 |
stream_mode_t Mode; |
stream_mode_t Mode; |
|
ChunkList ResizedChunks; ///< All chunks which have been resized (enlarged / shortened). |
|
383 |
|
|
384 |
unsigned long GetFileSize(); |
void __openExistingFile(const String& path, uint32_t* FileType = NULL); |
385 |
void ResizeFile(unsigned long ulNewSize); |
void ResizeFile(file_offset_t ullNewSize); |
386 |
#if POSIX |
#if POSIX |
387 |
unsigned long __GetFileSize(int hFile); |
file_offset_t __GetFileSize(int hFile) const; |
388 |
#elif defined(WIN32) |
#elif defined(WIN32) |
389 |
unsigned long __GetFileSize(HANDLE hFile); |
file_offset_t __GetFileSize(HANDLE hFile) const; |
390 |
#else |
#else |
391 |
unsigned long __GetFileSize(FILE* hFile); |
file_offset_t __GetFileSize(FILE* hFile) const; |
392 |
#endif |
#endif |
393 |
|
int FileOffsetSizeFor(file_offset_t fileSize) const; |
394 |
void Cleanup(); |
void Cleanup(); |
395 |
}; |
}; |
396 |
|
|