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-2014 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 |
68 |
#endif |
#endif |
69 |
|
|
70 |
#ifdef WIN32 |
#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> |
# include <windows.h> |
75 |
typedef unsigned int uint; |
typedef unsigned int uint; |
76 |
#endif // WIN32 |
#endif // WIN32 |
111 |
|
|
112 |
#endif // WORDS_BIGENDIAN |
#endif // WORDS_BIGENDIAN |
113 |
|
|
114 |
#define CHUNK_HEADER_SIZE 8 |
#define CHUNK_HEADER_SIZE(fileOffsetSize) (4 + fileOffsetSize) |
115 |
#define LIST_HEADER_SIZE 12 |
#define LIST_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize) |
116 |
#define RIFF_HEADER_SIZE 12 |
#define RIFF_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize) |
|
|
|
117 |
|
|
118 |
/** |
/** |
119 |
* @brief RIFF specific classes and definitions |
* @brief RIFF specific classes and definitions |
143 |
|
|
144 |
typedef std::string String; |
typedef std::string String; |
145 |
|
|
146 |
|
/** Type used by libgig for handling file positioning during file I/O tasks. */ |
147 |
|
typedef uint64_t file_offset_t; |
148 |
|
|
149 |
/** Whether file stream is open in read or in read/write mode. */ |
/** Whether file stream is open in read or in read/write mode. */ |
150 |
typedef enum { |
typedef enum { |
151 |
stream_mode_read = 0, |
stream_mode_read = 0, |
175 |
endian_native = 2 |
endian_native = 2 |
176 |
} endian_t; |
} endian_t; |
177 |
|
|
178 |
/** General chunk structure of a file. */ |
/** General RIFF chunk structure of a RIFF file. */ |
179 |
enum layout_t { |
enum layout_t { |
180 |
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. |
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. |
181 |
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. |
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. |
182 |
}; |
}; |
183 |
|
|
184 |
|
/** Size of RIFF file offsets used in all RIFF chunks' headers. @see File::GetFileOffsetSize() */ |
185 |
|
enum offset_size_t { |
186 |
|
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. |
187 |
|
offset_size_32bit = 4, ///< Always use 32 bit offsets (even for files larger than 4 GB). |
188 |
|
offset_size_64bit = 8 ///< Always use 64 bit offsets (even for files smaller than 4 GB). |
189 |
|
}; |
190 |
|
|
191 |
/** |
/** |
192 |
* @brief Used for indicating the progress of a certain task. |
* @brief Used for indicating the progress of a certain task. |
193 |
* |
* |
216 |
*/ |
*/ |
217 |
class Chunk { |
class Chunk { |
218 |
public: |
public: |
219 |
Chunk(File* pFile, unsigned long StartPos, List* Parent); |
Chunk(File* pFile, file_offset_t StartPos, List* Parent); |
220 |
String GetChunkIDString(); |
String GetChunkIDString() const; |
221 |
uint32_t GetChunkID() { return ChunkID; } ///< Chunk ID in unsigned integer representation. |
uint32_t GetChunkID() const { return ChunkID; } ///< Chunk ID in unsigned integer representation. |
222 |
File* GetFile() { return pFile; } ///< Returns pointer to the chunk's File object. |
File* GetFile() const { return pFile; } ///< Returns pointer to the chunk's File object. |
223 |
List* GetParent() { return pParent; } ///< Returns pointer to the chunk's parent list chunk. |
List* GetParent() const { return pParent; } ///< Returns pointer to the chunk's parent list chunk. |
224 |
unsigned long GetSize() const { return CurrentChunkSize; } ///< Chunk size in bytes (without header, thus the chunk data body) |
file_offset_t GetSize() const { return ullCurrentChunkSize; } ///< Chunk size in bytes (without header, thus the chunk data body) |
225 |
unsigned long GetNewSize() { return NewChunkSize; } ///< New chunk size if it was modified with Resize(). |
file_offset_t GetNewSize() const { return ullNewChunkSize; } ///< New chunk size if it was modified with Resize(), otherwise value returned will be equal to GetSize(). |
226 |
unsigned long GetPos() { return ulPos; } ///< Position within the chunk data body |
file_offset_t GetPos() const { return ullPos; } ///< Position within the chunk data body (starting with 0). |
227 |
unsigned long GetFilePos() { return ulStartPos + ulPos; } ///< Current, actual offset in file. |
file_offset_t GetFilePos() const { return ullStartPos + ullPos; } ///< Current, actual offset of chunk data body start in file. |
228 |
unsigned long SetPos(unsigned long Where, stream_whence_t Whence = stream_start); |
file_offset_t SetPos(file_offset_t Where, stream_whence_t Whence = stream_start); |
229 |
unsigned long RemainingBytes(); |
file_offset_t RemainingBytes() const; |
230 |
stream_state_t GetState(); |
stream_state_t GetState() const; |
231 |
unsigned long Read(void* pData, unsigned long WordCount, unsigned long WordSize); |
file_offset_t Read(void* pData, file_offset_t WordCount, file_offset_t WordSize); |
232 |
unsigned long ReadInt8(int8_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadInt8(int8_t* pData, file_offset_t WordCount = 1); |
233 |
unsigned long ReadUint8(uint8_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadUint8(uint8_t* pData, file_offset_t WordCount = 1); |
234 |
unsigned long ReadInt16(int16_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadInt16(int16_t* pData, file_offset_t WordCount = 1); |
235 |
unsigned long ReadUint16(uint16_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadUint16(uint16_t* pData, file_offset_t WordCount = 1); |
236 |
unsigned long ReadInt32(int32_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadInt32(int32_t* pData, file_offset_t WordCount = 1); |
237 |
unsigned long ReadUint32(uint32_t* pData, unsigned long WordCount = 1); |
file_offset_t ReadUint32(uint32_t* pData, file_offset_t WordCount = 1); |
238 |
int8_t ReadInt8(); |
int8_t ReadInt8(); |
239 |
uint8_t ReadUint8(); |
uint8_t ReadUint8(); |
240 |
int16_t ReadInt16(); |
int16_t ReadInt16(); |
242 |
int32_t ReadInt32(); |
int32_t ReadInt32(); |
243 |
uint32_t ReadUint32(); |
uint32_t ReadUint32(); |
244 |
void ReadString(String& s, int size); |
void ReadString(String& s, int size); |
245 |
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); |
246 |
unsigned long WriteInt8(int8_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteInt8(int8_t* pData, file_offset_t WordCount = 1); |
247 |
unsigned long WriteUint8(uint8_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteUint8(uint8_t* pData, file_offset_t WordCount = 1); |
248 |
unsigned long WriteInt16(int16_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteInt16(int16_t* pData, file_offset_t WordCount = 1); |
249 |
unsigned long WriteUint16(uint16_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteUint16(uint16_t* pData, file_offset_t WordCount = 1); |
250 |
unsigned long WriteInt32(int32_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteInt32(int32_t* pData, file_offset_t WordCount = 1); |
251 |
unsigned long WriteUint32(uint32_t* pData, unsigned long WordCount = 1); |
file_offset_t WriteUint32(uint32_t* pData, file_offset_t WordCount = 1); |
252 |
void* LoadChunkData(); |
void* LoadChunkData(); |
253 |
void ReleaseChunkData(); |
void ReleaseChunkData(); |
254 |
void Resize(int iNewSize); |
void Resize(file_offset_t NewSize); |
255 |
virtual ~Chunk(); |
virtual ~Chunk(); |
256 |
protected: |
protected: |
257 |
uint32_t ChunkID; |
uint32_t ChunkID; |
258 |
uint32_t CurrentChunkSize; /* in bytes */ |
file_offset_t ullCurrentChunkSize; /* in bytes */ |
259 |
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) */ |
260 |
List* pParent; |
List* pParent; |
261 |
File* pFile; |
File* pFile; |
262 |
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 */ |
263 |
unsigned long ulPos; /* # of bytes from ulStartPos */ |
file_offset_t ullPos; /* # of bytes from ulStartPos */ |
264 |
uint8_t* pChunkData; |
uint8_t* pChunkData; |
265 |
unsigned long ulChunkDataSize; |
file_offset_t ullChunkDataSize; |
266 |
|
|
267 |
Chunk(File* pFile); |
Chunk(File* pFile); |
268 |
Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize); |
Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize); |
269 |
void ReadHeader(unsigned long fPos); |
void ReadHeader(file_offset_t filePos); |
270 |
void WriteHeader(unsigned long fPos); |
void WriteHeader(file_offset_t filePos); |
271 |
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); |
272 |
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) { |
|
273 |
String result; |
String result; |
274 |
for (int i = 0; i < 4; i++) { |
for (int i = 0; i < 4; i++) { |
275 |
uint8_t byte = *((uint8_t*)(&word) + i); |
uint8_t byte = *((uint8_t*)(&word) + i); |
278 |
} |
} |
279 |
return result; |
return result; |
280 |
} |
} |
281 |
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset, progress_t* pProgress = NULL); |
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(); ///< Sets Chunk's read/write position to zero. |
virtual void __resetPos(); ///< Sets Chunk's read/write position to zero. |
284 |
|
|
285 |
friend class List; |
friend class List; |
292 |
*/ |
*/ |
293 |
class List : public Chunk { |
class List : public Chunk { |
294 |
public: |
public: |
295 |
List(File* pFile, unsigned long StartPos, List* Parent); |
List(File* pFile, file_offset_t StartPos, List* Parent); |
296 |
String GetListTypeString(); |
String GetListTypeString() const; |
297 |
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 |
298 |
Chunk* GetSubChunk(uint32_t ChunkID); |
Chunk* GetSubChunk(uint32_t ChunkID); |
299 |
List* GetSubList(uint32_t ListType); |
List* GetSubList(uint32_t ListType); |
300 |
Chunk* GetFirstSubChunk(); |
Chunk* GetFirstSubChunk(); |
301 |
Chunk* GetNextSubChunk(); |
Chunk* GetNextSubChunk(); |
302 |
List* GetFirstSubList(); |
List* GetFirstSubList(); |
303 |
List* GetNextSubList(); |
List* GetNextSubList(); |
304 |
unsigned int CountSubChunks(); |
size_t CountSubChunks(); |
305 |
unsigned int CountSubChunks(uint32_t ChunkID); |
size_t CountSubChunks(uint32_t ChunkID); |
306 |
unsigned int CountSubLists(); |
size_t CountSubLists(); |
307 |
unsigned int CountSubLists(uint32_t ListType); |
size_t CountSubLists(uint32_t ListType); |
308 |
Chunk* AddSubChunk(uint32_t uiChunkID, uint uiBodySize); |
Chunk* AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize); |
309 |
List* AddSubList(uint32_t uiListType); |
List* AddSubList(uint32_t uiListType); |
310 |
void DeleteSubChunk(Chunk* pSubChunk); |
void DeleteSubChunk(Chunk* pSubChunk); |
311 |
void MoveSubChunk(Chunk* pSrc, Chunk* pDst); // read API doc comments !!! |
void MoveSubChunk(Chunk* pSrc, Chunk* pDst); // read API doc comments !!! |
314 |
protected: |
protected: |
315 |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
316 |
typedef std::list<Chunk*> ChunkList; |
typedef std::list<Chunk*> ChunkList; |
317 |
|
typedef std::set<Chunk*> ChunkSet; |
318 |
|
|
319 |
uint32_t ListType; |
uint32_t ListType; |
320 |
ChunkList* pSubChunks; |
ChunkList* pSubChunks; |
324 |
|
|
325 |
List(File* pFile); |
List(File* pFile); |
326 |
List(File* pFile, List* pParent, uint32_t uiListID); |
List(File* pFile, List* pParent, uint32_t uiListID); |
327 |
void ReadHeader(unsigned long fPos); |
void ReadHeader(file_offset_t filePos); |
328 |
void WriteHeader(unsigned long fPos); |
void WriteHeader(file_offset_t filePos); |
329 |
void LoadSubChunks(progress_t* pProgress = NULL); |
void LoadSubChunks(progress_t* pProgress = NULL); |
330 |
void LoadSubChunksRecursively(progress_t* pProgress = NULL); |
void LoadSubChunksRecursively(progress_t* pProgress = NULL); |
331 |
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset, progress_t* pProgress = NULL); |
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(); ///< 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. |
334 |
void DeleteChunkList(); |
void DeleteChunkList(); |
335 |
}; |
}; |
344 |
public: |
public: |
345 |
File(uint32_t FileType); |
File(uint32_t FileType); |
346 |
File(const String& path); |
File(const String& path); |
347 |
File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout); |
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(); |
stream_mode_t GetMode() const; |
349 |
bool SetMode(stream_mode_t NewMode); |
bool SetMode(stream_mode_t NewMode); |
350 |
void SetByteOrder(endian_t Endian); |
void SetByteOrder(endian_t Endian); |
351 |
String GetFileName(); |
String GetFileName() const; |
352 |
void SetFileName(const String& path); |
void SetFileName(const String& path); |
353 |
bool IsNew() const; |
bool IsNew() const; |
354 |
layout_t GetLayout() const; |
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); |
virtual void Save(progress_t* pProgress = NULL); |
362 |
virtual void Save(const String& path, progress_t* pProgress = NULL); |
virtual void Save(const String& path, progress_t* pProgress = NULL); |
363 |
virtual ~File(); |
virtual ~File(); |
376 |
bool bEndianNative; |
bool bEndianNative; |
377 |
bool bIsNewFile; |
bool bIsNewFile; |
378 |
layout_t Layout; ///< An ordinary RIFF file is always set to layout_standard. |
layout_t Layout; ///< An ordinary RIFF file is always set to layout_standard. |
379 |
|
offset_size_t FileOffsetPreference; |
380 |
|
int FileOffsetSize; ///< Size of file offsets (in bytes) when this file was opened (or saved the last time). |
381 |
|
|
|
void LogAsResized(Chunk* pResizedChunk); |
|
|
void UnlogResized(Chunk* pResizedChunk); |
|
382 |
friend class Chunk; |
friend class Chunk; |
383 |
friend class List; |
friend class List; |
384 |
private: |
private: |
385 |
stream_mode_t Mode; |
stream_mode_t Mode; |
|
ChunkList ResizedChunks; ///< All chunks which have been resized (enlarged / shortened). |
|
386 |
|
|
387 |
void __openExistingFile(const String& path, uint32_t* FileType = NULL); |
void __openExistingFile(const String& path, uint32_t* FileType = NULL); |
388 |
unsigned long GetFileSize(); |
void ResizeFile(file_offset_t ullNewSize); |
|
void ResizeFile(unsigned long ulNewSize); |
|
389 |
#if POSIX |
#if POSIX |
390 |
unsigned long __GetFileSize(int hFile); |
file_offset_t __GetFileSize(int hFile) const; |
391 |
#elif defined(WIN32) |
#elif defined(WIN32) |
392 |
unsigned long __GetFileSize(HANDLE hFile); |
file_offset_t __GetFileSize(HANDLE hFile) const; |
393 |
#else |
#else |
394 |
unsigned long __GetFileSize(FILE* hFile); |
file_offset_t __GetFileSize(FILE* hFile) const; |
395 |
#endif |
#endif |
396 |
|
int FileOffsetSizeFor(file_offset_t fileSize) const; |
397 |
void Cleanup(); |
void Cleanup(); |
398 |
}; |
}; |
399 |
|
|