1 |
/*************************************************************************** |
2 |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
5 |
* Copyright (C) 2003-2007 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 <iostream> |
43 |
|
44 |
#ifdef HAVE_CONFIG_H |
45 |
# include <config.h> |
46 |
#endif |
47 |
|
48 |
#if POSIX |
49 |
# include <sys/types.h> |
50 |
# include <sys/stat.h> |
51 |
# include <fcntl.h> |
52 |
# include <unistd.h> |
53 |
#endif // POSIX |
54 |
|
55 |
#include <stdint.h> |
56 |
|
57 |
#ifdef WIN32 |
58 |
# include "../win32/libgig_private.h" // like config.h, automatically generated by Dev-C++ |
59 |
# include <windows.h> |
60 |
typedef unsigned int uint; |
61 |
typedef unsigned char uint8_t; |
62 |
typedef unsigned short uint16_t; |
63 |
typedef unsigned int uint32_t; |
64 |
# define PACKAGE "libgig" |
65 |
# define VERSION VER_STRING // VER_STRING defined in libgig_private.h |
66 |
#endif // WIN32 |
67 |
|
68 |
#include <stdio.h> |
69 |
|
70 |
#if WORDS_BIGENDIAN |
71 |
# define CHUNK_ID_RIFF 0x52494646 |
72 |
# define CHUNK_ID_RIFX 0x52494658 |
73 |
# define CHUNK_ID_LIST 0x4C495354 |
74 |
#else // little endian |
75 |
# define CHUNK_ID_RIFF 0x46464952 |
76 |
# define CHUNK_ID_RIFX 0x58464952 |
77 |
# define CHUNK_ID_LIST 0x5453494C |
78 |
#endif // WORDS_BIGENDIAN |
79 |
|
80 |
#define CHUNK_HEADER_SIZE 8 |
81 |
#define LIST_HEADER_SIZE 12 |
82 |
#define RIFF_HEADER_SIZE 12 |
83 |
|
84 |
|
85 |
/** RIFF specific classes and definitions */ |
86 |
namespace RIFF { |
87 |
|
88 |
/* just symbol prototyping */ |
89 |
class Chunk; |
90 |
class List; |
91 |
class File; |
92 |
|
93 |
typedef std::string String; |
94 |
|
95 |
/** Whether file stream is open in read or in read/write mode. */ |
96 |
typedef enum { |
97 |
stream_mode_read = 0, |
98 |
stream_mode_read_write = 1, |
99 |
stream_mode_closed = 2 |
100 |
} stream_mode_t; |
101 |
|
102 |
/** Current state of the file stream. */ |
103 |
typedef enum { |
104 |
stream_ready = 0, |
105 |
stream_end_reached = 1, |
106 |
stream_closed = 2 |
107 |
} stream_state_t; |
108 |
|
109 |
/** File stream position dependent to these relations. */ |
110 |
typedef enum { |
111 |
stream_start = 0, |
112 |
stream_curpos = 1, |
113 |
stream_backward = 2, |
114 |
stream_end = 3 |
115 |
} stream_whence_t; |
116 |
|
117 |
typedef enum { |
118 |
endian_little = 0, |
119 |
endian_big = 1, |
120 |
endian_native = 2 |
121 |
} endian_t; |
122 |
|
123 |
/** Provides convenient methods to access data of RIFF chunks in general. */ |
124 |
class Chunk { |
125 |
public: |
126 |
Chunk(File* pFile, unsigned long StartPos, List* Parent); |
127 |
String GetChunkIDString(); |
128 |
uint32_t GetChunkID() { return ChunkID; }; ///< Chunk ID in unsigned integer representation. |
129 |
List* GetParent() { return pParent; }; ///< Returns pointer to the chunk's parent list chunk. |
130 |
unsigned long GetSize() { return CurrentChunkSize; }; ///< Chunk size in bytes (without header, thus the chunk data body) |
131 |
unsigned long GetNewSize() { return NewChunkSize; }; ///< New chunk size if it was modified with Resize(). |
132 |
unsigned long GetPos() { return ulPos; }; ///< Position within the chunk data body |
133 |
unsigned long GetFilePos() { return ulStartPos + ulPos; }; ///< Current, actual offset in file. |
134 |
unsigned long SetPos(unsigned long Where, stream_whence_t Whence = stream_start); |
135 |
unsigned long RemainingBytes(); |
136 |
stream_state_t GetState(); |
137 |
unsigned long Read(void* pData, unsigned long WordCount, unsigned long WordSize); |
138 |
unsigned long ReadInt8(int8_t* pData, unsigned long WordCount = 1); |
139 |
unsigned long ReadUint8(uint8_t* pData, unsigned long WordCount = 1); |
140 |
unsigned long ReadInt16(int16_t* pData, unsigned long WordCount = 1); |
141 |
unsigned long ReadUint16(uint16_t* pData, unsigned long WordCount = 1); |
142 |
unsigned long ReadInt32(int32_t* pData, unsigned long WordCount = 1); |
143 |
unsigned long ReadUint32(uint32_t* pData, unsigned long WordCount = 1); |
144 |
int8_t ReadInt8(); |
145 |
uint8_t ReadUint8(); |
146 |
int16_t ReadInt16(); |
147 |
uint16_t ReadUint16(); |
148 |
int32_t ReadInt32(); |
149 |
uint32_t ReadUint32(); |
150 |
unsigned long Write(void* pData, unsigned long WordCount, unsigned long WordSize); |
151 |
unsigned long WriteInt8(int8_t* pData, unsigned long WordCount = 1); |
152 |
unsigned long WriteUint8(uint8_t* pData, unsigned long WordCount = 1); |
153 |
unsigned long WriteInt16(int16_t* pData, unsigned long WordCount = 1); |
154 |
unsigned long WriteUint16(uint16_t* pData, unsigned long WordCount = 1); |
155 |
unsigned long WriteInt32(int32_t* pData, unsigned long WordCount = 1); |
156 |
unsigned long WriteUint32(uint32_t* pData, unsigned long WordCount = 1); |
157 |
void* LoadChunkData(); |
158 |
void ReleaseChunkData(); |
159 |
void Resize(int iNewSize); |
160 |
virtual ~Chunk(); |
161 |
protected: |
162 |
uint32_t ChunkID; |
163 |
uint32_t CurrentChunkSize; /* in bytes */ |
164 |
uint32_t NewChunkSize; /* in bytes (if chunk was scheduled to be resized) */ |
165 |
List* pParent; |
166 |
File* pFile; |
167 |
unsigned long ulStartPos; /* actual position in file where chunk (without header) starts */ |
168 |
unsigned long ulPos; /* # of bytes from ulStartPos */ |
169 |
uint8_t* pChunkData; |
170 |
unsigned long ulChunkDataSize; |
171 |
|
172 |
Chunk(File* pFile); |
173 |
Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize); |
174 |
void ReadHeader(unsigned long fPos); |
175 |
void WriteHeader(unsigned long fPos); |
176 |
unsigned long ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize); |
177 |
inline void swapBytes_16(void* Word) { |
178 |
uint8_t byteCache = *((uint8_t*) Word); |
179 |
*((uint8_t*) Word) = *((uint8_t*) Word + 1); |
180 |
*((uint8_t*) Word + 1) = byteCache; |
181 |
} |
182 |
inline void swapBytes_32(void* Word) { |
183 |
uint8_t byteCache = *((uint8_t*) Word); |
184 |
*((uint8_t*) Word) = *((uint8_t*) Word + 3); |
185 |
*((uint8_t*) Word + 3) = byteCache; |
186 |
byteCache = *((uint8_t*) Word + 1); |
187 |
*((uint8_t*) Word + 1) = *((uint8_t*) Word + 2); |
188 |
*((uint8_t*) Word + 2) = byteCache; |
189 |
} |
190 |
inline void swapBytes(void* Word, unsigned long WordSize) { |
191 |
uint8_t byteCache; |
192 |
unsigned long lo = 0, hi = WordSize - 1; |
193 |
for (; lo < hi; hi--, lo++) { |
194 |
byteCache = *((uint8_t*) Word + lo); |
195 |
*((uint8_t*) Word + lo) = *((uint8_t*) Word + hi); |
196 |
*((uint8_t*) Word + hi) = byteCache; |
197 |
} |
198 |
} |
199 |
inline String convertToString(uint32_t word) { |
200 |
String result; |
201 |
for (int i = 0; i < 4; i++) { |
202 |
uint8_t byte = *((uint8_t*)(&word) + i); |
203 |
char c = byte; |
204 |
result += c; |
205 |
} |
206 |
return result; |
207 |
} |
208 |
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); |
209 |
virtual void __resetPos(); ///< Sets Chunk's read/write position to zero. |
210 |
|
211 |
friend class List; |
212 |
}; |
213 |
|
214 |
/** Provides convenient methods to access data of RIFF list chunks and their subchunks. */ |
215 |
class List : public Chunk { |
216 |
public: |
217 |
List(File* pFile, unsigned long StartPos, List* Parent); |
218 |
String GetListTypeString(); |
219 |
uint32_t GetListType() { return ListType; } ///< Returns unsigned integer representation of the list's ID |
220 |
Chunk* GetSubChunk(uint32_t ChunkID); |
221 |
List* GetSubList(uint32_t ListType); |
222 |
Chunk* GetFirstSubChunk(); |
223 |
Chunk* GetNextSubChunk(); |
224 |
List* GetFirstSubList(); |
225 |
List* GetNextSubList(); |
226 |
unsigned int CountSubChunks(); |
227 |
unsigned int CountSubChunks(uint32_t ChunkID); |
228 |
unsigned int CountSubLists(); |
229 |
unsigned int CountSubLists(uint32_t ListType); |
230 |
Chunk* AddSubChunk(uint32_t uiChunkID, uint uiBodySize); |
231 |
List* AddSubList(uint32_t uiListType); |
232 |
void DeleteSubChunk(Chunk* pSubChunk); |
233 |
void MoveSubChunk(Chunk* pSrc, Chunk* pDst); |
234 |
virtual ~List(); |
235 |
protected: |
236 |
typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap; |
237 |
typedef std::list<Chunk*> ChunkList; |
238 |
|
239 |
uint32_t ListType; |
240 |
ChunkList* pSubChunks; |
241 |
ChunkMap* pSubChunksMap; |
242 |
ChunkList::iterator ChunksIterator; |
243 |
ChunkList::iterator ListIterator; |
244 |
|
245 |
List(File* pFile); |
246 |
List(File* pFile, List* pParent, uint32_t uiListID); |
247 |
void ReadHeader(unsigned long fPos); |
248 |
void WriteHeader(unsigned long fPos); |
249 |
void LoadSubChunks(); |
250 |
void LoadSubChunksRecursively(); |
251 |
virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); |
252 |
virtual void __resetPos(); ///< Sets List Chunk's read/write position to zero and causes all sub chunks to do the same. |
253 |
}; |
254 |
|
255 |
/** Parses arbitrary RIFF files and provides together with it's base classes convenient methods to walk through the RIFF tree. */ |
256 |
class File : public List { |
257 |
public: |
258 |
File(uint32_t FileType); |
259 |
File(uint32_t FileType, endian_t Endian); |
260 |
File(const String& path); |
261 |
stream_mode_t GetMode(); |
262 |
bool SetMode(stream_mode_t NewMode); |
263 |
String GetFileName(); |
264 |
virtual void Save(); |
265 |
virtual void Save(const String& path); |
266 |
virtual ~File(); |
267 |
protected: |
268 |
#if POSIX |
269 |
int hFileRead; ///< handle / descriptor for reading from file |
270 |
int hFileWrite; ///< handle / descriptor for writing to (some) file |
271 |
#elif defined(WIN32) |
272 |
HANDLE hFileRead; ///< handle / descriptor for reading from file |
273 |
HANDLE hFileWrite; ///< handle / descriptor for writing to (some) file |
274 |
#else |
275 |
FILE* hFileRead; ///< handle / descriptor for reading from file |
276 |
FILE* hFileWrite; ///< handle / descriptor for writing to (some) file |
277 |
#endif // POSIX |
278 |
String Filename; |
279 |
bool bEndianNative; |
280 |
|
281 |
void LogAsResized(Chunk* pResizedChunk); |
282 |
void UnlogResized(Chunk* pResizedChunk); |
283 |
friend class Chunk; |
284 |
friend class List; |
285 |
private: |
286 |
stream_mode_t Mode; |
287 |
ChunkList ResizedChunks; ///< All chunks which have been resized (enlarged / shortened). |
288 |
|
289 |
unsigned long GetFileSize(); |
290 |
void ResizeFile(unsigned long ulNewSize); |
291 |
#if POSIX |
292 |
unsigned long __GetFileSize(int hFile); |
293 |
#elif defined(WIN32) |
294 |
unsigned long __GetFileSize(HANDLE hFile); |
295 |
#else |
296 |
unsigned long __GetFileSize(FILE* hFile); |
297 |
#endif |
298 |
}; |
299 |
|
300 |
/** Will be thrown whenever an error occurs while parsing a RIFF file. */ |
301 |
class Exception { |
302 |
public: |
303 |
String Message; |
304 |
|
305 |
Exception(String Message) { Exception::Message = Message; }; |
306 |
void PrintMessage(); |
307 |
virtual ~Exception() {}; |
308 |
}; |
309 |
|
310 |
String libraryName(); |
311 |
String libraryVersion(); |
312 |
|
313 |
} // namespace RIFF |
314 |
#endif // __RIFF_H__ |