1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2005 by Christian Schoenebeck * |
* Copyright (C) 2003-2009 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 * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
|
#include <algorithm> |
25 |
#include <string.h> |
#include <string.h> |
26 |
|
|
27 |
#include "RIFF.h" |
#include "RIFF.h" |
30 |
|
|
31 |
namespace RIFF { |
namespace RIFF { |
32 |
|
|
33 |
|
// *************** Internal functions ************** |
34 |
|
// * |
35 |
|
|
36 |
|
/// Returns a human readable path of the given chunk. |
37 |
|
static String __resolveChunkPath(Chunk* pCk) { |
38 |
|
String sPath; |
39 |
|
for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { |
40 |
|
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
41 |
|
List* pList = (List*) pChunk; |
42 |
|
sPath = "->'" + pList->GetListTypeString() + "'" + sPath; |
43 |
|
} else { |
44 |
|
sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath; |
45 |
|
} |
46 |
|
} |
47 |
|
return sPath; |
48 |
|
} |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
// *************** Chunk ************** |
// *************** Chunk ************** |
53 |
// * |
// * |
54 |
|
|
59 |
ulPos = 0; |
ulPos = 0; |
60 |
pParent = NULL; |
pParent = NULL; |
61 |
pChunkData = NULL; |
pChunkData = NULL; |
62 |
|
CurrentChunkSize = 0; |
63 |
|
NewChunkSize = 0; |
64 |
ulChunkDataSize = 0; |
ulChunkDataSize = 0; |
65 |
ChunkID = CHUNK_ID_RIFF; |
ChunkID = CHUNK_ID_RIFF; |
66 |
this->pFile = pFile; |
this->pFile = pFile; |
75 |
pParent = Parent; |
pParent = Parent; |
76 |
ulPos = 0; |
ulPos = 0; |
77 |
pChunkData = NULL; |
pChunkData = NULL; |
78 |
|
CurrentChunkSize = 0; |
79 |
|
NewChunkSize = 0; |
80 |
ulChunkDataSize = 0; |
ulChunkDataSize = 0; |
81 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
82 |
} |
} |
87 |
this->pParent = pParent; |
this->pParent = pParent; |
88 |
ulPos = 0; |
ulPos = 0; |
89 |
pChunkData = NULL; |
pChunkData = NULL; |
|
ulChunkDataSize = 0; |
|
90 |
ChunkID = uiChunkID; |
ChunkID = uiChunkID; |
91 |
|
ulChunkDataSize = 0; |
92 |
CurrentChunkSize = 0; |
CurrentChunkSize = 0; |
93 |
NewChunkSize = uiBodySize; |
NewChunkSize = uiBodySize; |
94 |
} |
} |
95 |
|
|
96 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
97 |
|
pFile->UnlogResized(this); |
98 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
99 |
} |
} |
100 |
|
|
102 |
#if DEBUG |
#if DEBUG |
103 |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
104 |
#endif // DEBUG |
#endif // DEBUG |
105 |
|
ChunkID = 0; |
106 |
|
NewChunkSize = CurrentChunkSize = 0; |
107 |
#if POSIX |
#if POSIX |
108 |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
109 |
read(pFile->hFileRead, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
110 |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
111 |
|
#elif defined(WIN32) |
112 |
|
if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
113 |
|
DWORD dwBytesRead; |
114 |
|
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
115 |
|
ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL); |
116 |
#else |
#else |
117 |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
118 |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
134 |
} |
} |
135 |
#if DEBUG |
#if DEBUG |
136 |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
137 |
std::cout << "ckSize=" << ChunkSize << " "; |
std::cout << "ckSize=" << CurrentChunkSize << " "; |
138 |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl; |
139 |
#endif // DEBUG |
#endif // DEBUG |
140 |
NewChunkSize = CurrentChunkSize; |
NewChunkSize = CurrentChunkSize; |
141 |
} |
} |
161 |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
162 |
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
163 |
} |
} |
164 |
|
#elif defined(WIN32) |
165 |
|
if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
166 |
|
DWORD dwBytesWritten; |
167 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
168 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL); |
169 |
|
} |
170 |
#else |
#else |
171 |
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
172 |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
231 |
#if DEBUG |
#if DEBUG |
232 |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
233 |
#endif // DEBUG |
#endif // DEBUG |
234 |
return CurrentChunkSize - ulPos; |
return (CurrentChunkSize > ulPos) ? CurrentChunkSize - ulPos : 0; |
235 |
} |
} |
236 |
|
|
237 |
/** |
/** |
242 |
* - RIFF::stream_closed : |
* - RIFF::stream_closed : |
243 |
* the data stream was closed somehow, no more reading possible |
* the data stream was closed somehow, no more reading possible |
244 |
* - RIFF::stream_end_reached : |
* - RIFF::stream_end_reached : |
245 |
* alreaady reached the end of the chunk data, no more reading |
* already reached the end of the chunk data, no more reading |
246 |
* possible without SetPos() |
* possible without SetPos() |
247 |
*/ |
*/ |
248 |
stream_state_t Chunk::GetState() { |
stream_state_t Chunk::GetState() { |
250 |
std::cout << "Chunk::GetState()" << std::endl; |
std::cout << "Chunk::GetState()" << std::endl; |
251 |
#endif // DEBUG |
#endif // DEBUG |
252 |
#if POSIX |
#if POSIX |
253 |
if (pFile->hFileRead == 0) return stream_closed; |
if (pFile->hFileRead == 0) return stream_closed; |
254 |
|
#elif defined (WIN32) |
255 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) |
256 |
|
return stream_closed; |
257 |
#else |
#else |
258 |
if (pFile->hFileRead == NULL) return stream_closed; |
if (pFile->hFileRead == NULL) return stream_closed; |
259 |
#endif // POSIX |
#endif // POSIX |
280 |
#if DEBUG |
#if DEBUG |
281 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
282 |
#endif // DEBUG |
#endif // DEBUG |
283 |
|
if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
284 |
if (ulPos >= CurrentChunkSize) return 0; |
if (ulPos >= CurrentChunkSize) return 0; |
285 |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
286 |
#if POSIX |
#if POSIX |
288 |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
289 |
if (readWords < 1) return 0; |
if (readWords < 1) return 0; |
290 |
readWords /= WordSize; |
readWords /= WordSize; |
291 |
|
#elif defined(WIN32) |
292 |
|
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
293 |
|
DWORD readWords; |
294 |
|
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); |
295 |
|
if (readWords < 1) return 0; |
296 |
|
readWords /= WordSize; |
297 |
#else // standard C functions |
#else // standard C functions |
298 |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
299 |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
363 |
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
364 |
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
365 |
writtenWords /= WordSize; |
writtenWords /= WordSize; |
366 |
|
#elif defined(WIN32) |
367 |
|
if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
368 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
369 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
370 |
|
} |
371 |
|
DWORD writtenWords; |
372 |
|
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
373 |
|
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
374 |
|
writtenWords /= WordSize; |
375 |
#else // standard C functions |
#else // standard C functions |
376 |
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
377 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
throw Exception("Could not seek to position " + ToString(ulPos) + |
733 |
* @see ReleaseChunkData() |
* @see ReleaseChunkData() |
734 |
*/ |
*/ |
735 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
736 |
if (!pChunkData && pFile->Filename != "") { |
if (!pChunkData && pFile->Filename != "" && ulStartPos != 0) { |
737 |
#if POSIX |
#if POSIX |
738 |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
739 |
|
#elif defined(WIN32) |
740 |
|
if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL; |
741 |
#else |
#else |
742 |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
743 |
#endif // POSIX |
#endif // POSIX |
747 |
memset(pChunkData, 0, ulBufferSize); |
memset(pChunkData, 0, ulBufferSize); |
748 |
#if POSIX |
#if POSIX |
749 |
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
750 |
|
#elif defined(WIN32) |
751 |
|
DWORD readWords; |
752 |
|
ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); |
753 |
#else |
#else |
754 |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
755 |
#endif // POSIX |
#endif // POSIX |
802 |
* @see File::Save() |
* @see File::Save() |
803 |
*/ |
*/ |
804 |
void Chunk::Resize(int iNewSize) { |
void Chunk::Resize(int iNewSize) { |
805 |
if (iNewSize <= 0) throw Exception("Chunk size must be at least one byte"); |
if (iNewSize <= 0) |
806 |
|
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this)); |
807 |
if (NewChunkSize == iNewSize) return; |
if (NewChunkSize == iNewSize) return; |
808 |
NewChunkSize = iNewSize; |
NewChunkSize = iNewSize; |
809 |
pFile->LogAsResized(this); |
pFile->LogAsResized(this); |
838 |
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
839 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
840 |
} |
} |
841 |
|
#elif defined(WIN32) |
842 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN); |
843 |
|
DWORD dwBytesWritten; |
844 |
|
WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL); |
845 |
|
if (dwBytesWritten != NewChunkSize) { |
846 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
847 |
|
} |
848 |
#else |
#else |
849 |
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
850 |
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
855 |
// move chunk data from the end of the file to the appropriate position |
// move chunk data from the end of the file to the appropriate position |
856 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
857 |
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
858 |
|
#if defined(WIN32) |
859 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
860 |
|
#else |
861 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
862 |
|
#endif |
863 |
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
864 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
865 |
#if POSIX |
#if POSIX |
867 |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
868 |
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
869 |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
870 |
|
#elif defined(WIN32) |
871 |
|
SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
872 |
|
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
873 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
874 |
|
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
875 |
#else |
#else |
876 |
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
877 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
897 |
#if POSIX |
#if POSIX |
898 |
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
899 |
write(pFile->hFileWrite, &cPadByte, 1); |
write(pFile->hFileWrite, &cPadByte, 1); |
900 |
|
#elif defined(WIN32) |
901 |
|
SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN); |
902 |
|
DWORD dwBytesWritten; |
903 |
|
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
904 |
#else |
#else |
905 |
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
906 |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
1160 |
pSubChunks->push_back(pNewChunk); |
pSubChunks->push_back(pNewChunk); |
1161 |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1162 |
pNewChunk->Resize(uiBodySize); |
pNewChunk->Resize(uiBodySize); |
1163 |
|
NewChunkSize += CHUNK_HEADER_SIZE; |
1164 |
|
pFile->LogAsResized(this); |
1165 |
return pNewChunk; |
return pNewChunk; |
1166 |
} |
} |
1167 |
|
|
1168 |
|
/** @brief Moves a sub chunk. |
1169 |
|
* |
1170 |
|
* Moves a sub chunk from one position in a list to another |
1171 |
|
* position in the same list. The pSrc chunk is placed before the |
1172 |
|
* pDst chunk. |
1173 |
|
* |
1174 |
|
* @param pSrc - sub chunk to be moved |
1175 |
|
* @param pDst - the position to move to. pSrc will be placed |
1176 |
|
* before pDst. If pDst is 0, pSrc will be placed |
1177 |
|
* last in list. |
1178 |
|
*/ |
1179 |
|
void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) { |
1180 |
|
if (!pSubChunks) LoadSubChunks(); |
1181 |
|
pSubChunks->remove(pSrc); |
1182 |
|
ChunkList::iterator iter = find(pSubChunks->begin(), pSubChunks->end(), pDst); |
1183 |
|
pSubChunks->insert(iter, pSrc); |
1184 |
|
} |
1185 |
|
|
1186 |
/** @brief Creates a new list sub chunk. |
/** @brief Creates a new list sub chunk. |
1187 |
* |
* |
1188 |
* Creates and adds a new list sub chunk to this list chunk. Note that |
* Creates and adds a new list sub chunk to this list chunk. Note that |
1197 |
List* pNewListChunk = new List(pFile, this, uiListType); |
List* pNewListChunk = new List(pFile, this, uiListType); |
1198 |
pSubChunks->push_back(pNewListChunk); |
pSubChunks->push_back(pNewListChunk); |
1199 |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1200 |
|
NewChunkSize += LIST_HEADER_SIZE; |
1201 |
|
pFile->LogAsResized(this); |
1202 |
return pNewListChunk; |
return pNewListChunk; |
1203 |
} |
} |
1204 |
|
|
1206 |
* |
* |
1207 |
* Removes the sub chunk given by \a pSubChunk from this list and frees |
* Removes the sub chunk given by \a pSubChunk from this list and frees |
1208 |
* it completely from RAM. The given chunk can either be a normal sub |
* it completely from RAM. The given chunk can either be a normal sub |
1209 |
* chunk or a list sub chunk. You should call File::Save() to make this |
* chunk or a list sub chunk. In case the given chunk is a list chunk, |
1210 |
* change persistent at any time. |
* all its subchunks (if any) will be removed recursively as well. You |
1211 |
|
* should call File::Save() to make this change persistent at any time. |
1212 |
* |
* |
1213 |
* @param pSubChunk - sub chunk or sub list chunk to be removed |
* @param pSubChunk - sub chunk or sub list chunk to be removed |
1214 |
*/ |
*/ |
1235 |
std::cout << "List::Readheader(ulong) "; |
std::cout << "List::Readheader(ulong) "; |
1236 |
#endif // DEBUG |
#endif // DEBUG |
1237 |
Chunk::ReadHeader(fPos); |
Chunk::ReadHeader(fPos); |
1238 |
|
if (CurrentChunkSize < 8) return; |
1239 |
NewChunkSize = CurrentChunkSize -= 4; |
NewChunkSize = CurrentChunkSize -= 4; |
1240 |
#if POSIX |
#if POSIX |
1241 |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1242 |
read(pFile->hFileRead, &ListType, 4); |
read(pFile->hFileRead, &ListType, 4); |
1243 |
|
#elif defined(WIN32) |
1244 |
|
SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1245 |
|
DWORD dwBytesRead; |
1246 |
|
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
1247 |
#else |
#else |
1248 |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1249 |
fread(&ListType, 4, 1, pFile->hFileRead); |
fread(&ListType, 4, 1, pFile->hFileRead); |
1264 |
#if POSIX |
#if POSIX |
1265 |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1266 |
write(pFile->hFileWrite, &ListType, 4); |
write(pFile->hFileWrite, &ListType, 4); |
1267 |
|
#elif defined(WIN32) |
1268 |
|
SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1269 |
|
DWORD dwBytesWritten; |
1270 |
|
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
1271 |
#else |
#else |
1272 |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1273 |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
1281 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1282 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1283 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1284 |
|
#if defined(WIN32) |
1285 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) return; |
1286 |
|
#else |
1287 |
if (!pFile->hFileRead) return; |
if (!pFile->hFileRead) return; |
1288 |
|
#endif |
1289 |
unsigned long uiOriginalPos = GetPos(); |
unsigned long uiOriginalPos = GetPos(); |
1290 |
SetPos(0); // jump to beginning of list chunk body |
SetPos(0); // jump to beginning of list chunk body |
1291 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
1381 |
* "from scratch". Note: there must be no empty chunks or empty list |
* "from scratch". Note: there must be no empty chunks or empty list |
1382 |
* chunks when trying to make the new RIFF file persistent with Save()! |
* chunks when trying to make the new RIFF file persistent with Save()! |
1383 |
* |
* |
1384 |
|
* Note: by default, the RIFF file will be saved in native endian |
1385 |
|
* format; that is, as a RIFF file on little-endian machines and |
1386 |
|
* as a RIFX file on big-endian. To change this behaviour, call |
1387 |
|
* SetByteOrder() before calling Save(). |
1388 |
|
* |
1389 |
* @param FileType - four-byte identifier of the RIFF file type |
* @param FileType - four-byte identifier of the RIFF file type |
1390 |
* @see AddSubChunk(), AddSubList() |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1391 |
*/ |
*/ |
1392 |
File::File(uint32_t FileType) : List(this) { |
File::File(uint32_t FileType) : List(this) { |
1393 |
|
#if defined(WIN32) |
1394 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1395 |
|
#else |
1396 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1397 |
|
#endif |
1398 |
Mode = stream_mode_closed; |
Mode = stream_mode_closed; |
1399 |
bEndianNative = true; |
bEndianNative = true; |
1400 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1420 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1421 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1422 |
} |
} |
1423 |
|
#elif defined(WIN32) |
1424 |
|
hFileRead = hFileWrite = CreateFile( |
1425 |
|
path.c_str(), GENERIC_READ, |
1426 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1427 |
|
NULL, OPEN_EXISTING, |
1428 |
|
FILE_ATTRIBUTE_NORMAL | |
1429 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1430 |
|
); |
1431 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1432 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1433 |
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
1434 |
|
} |
1435 |
#else |
#else |
1436 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1437 |
if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1438 |
#endif // POSIX |
#endif // POSIX |
1439 |
Mode = stream_mode_read; |
Mode = stream_mode_read; |
1440 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1441 |
ReadHeader(0); |
ReadHeader(0); |
1442 |
if (ChunkID != CHUNK_ID_RIFF) { |
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
1443 |
throw RIFF::Exception("Not a RIFF file"); |
throw RIFF::Exception("Not a RIFF file"); |
1444 |
} |
} |
1445 |
} |
} |
1473 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1474 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1475 |
} |
} |
1476 |
|
#elif defined(WIN32) |
1477 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1478 |
|
hFileRead = hFileWrite = CreateFile( |
1479 |
|
Filename.c_str(), GENERIC_READ, |
1480 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1481 |
|
NULL, OPEN_EXISTING, |
1482 |
|
FILE_ATTRIBUTE_NORMAL | |
1483 |
|
FILE_FLAG_RANDOM_ACCESS, |
1484 |
|
NULL |
1485 |
|
); |
1486 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1487 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1488 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1489 |
|
} |
1490 |
#else |
#else |
1491 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1492 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1493 |
if (!hFileRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
if (!hFileRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1494 |
#endif |
#endif |
1495 |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1502 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1503 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1504 |
} |
} |
1505 |
|
#elif defined(WIN32) |
1506 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1507 |
|
hFileRead = hFileWrite = CreateFile( |
1508 |
|
Filename.c_str(), |
1509 |
|
GENERIC_READ | GENERIC_WRITE, |
1510 |
|
FILE_SHARE_READ, |
1511 |
|
NULL, OPEN_ALWAYS, |
1512 |
|
FILE_ATTRIBUTE_NORMAL | |
1513 |
|
FILE_FLAG_RANDOM_ACCESS, |
1514 |
|
NULL |
1515 |
|
); |
1516 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1517 |
|
hFileRead = hFileWrite = CreateFile( |
1518 |
|
Filename.c_str(), GENERIC_READ, |
1519 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1520 |
|
NULL, OPEN_EXISTING, |
1521 |
|
FILE_ATTRIBUTE_NORMAL | |
1522 |
|
FILE_FLAG_RANDOM_ACCESS, |
1523 |
|
NULL |
1524 |
|
); |
1525 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); |
1526 |
|
} |
1527 |
#else |
#else |
1528 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1529 |
hFileRead = hFileWrite = fopen(path.c_str(), "r+b"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b"); |
1530 |
if (!hFileRead) { |
if (!hFileRead) { |
1531 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1532 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1533 |
} |
} |
1534 |
#endif |
#endif |
1538 |
#if POSIX |
#if POSIX |
1539 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1540 |
if (hFileWrite) close(hFileWrite); |
if (hFileWrite) close(hFileWrite); |
1541 |
|
#elif defined(WIN32) |
1542 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1543 |
|
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1544 |
#else |
#else |
1545 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1546 |
if (hFileWrite) fclose(hFileWrite); |
if (hFileWrite) fclose(hFileWrite); |
1556 |
return false; |
return false; |
1557 |
} |
} |
1558 |
|
|
1559 |
|
/** @brief Set the byte order to be used when saving. |
1560 |
|
* |
1561 |
|
* Set the byte order to be used in the file. A value of |
1562 |
|
* endian_little will create a RIFF file, endian_big a RIFX file |
1563 |
|
* and endian_native will create a RIFF file on little-endian |
1564 |
|
* machines and RIFX on big-endian machines. |
1565 |
|
* |
1566 |
|
* @param Endian - endianess to use when file is saved. |
1567 |
|
*/ |
1568 |
|
void File::SetByteOrder(endian_t Endian) { |
1569 |
|
#if WORDS_BIGENDIAN |
1570 |
|
bEndianNative = Endian != endian_little; |
1571 |
|
#else |
1572 |
|
bEndianNative = Endian != endian_big; |
1573 |
|
#endif |
1574 |
|
} |
1575 |
|
|
1576 |
/** @brief Save changes to same file. |
/** @brief Save changes to same file. |
1577 |
* |
* |
1578 |
* Make all changes of all chunks persistent by writing them to the |
* Make all changes of all chunks persistent by writing them to the |
1599 |
|
|
1600 |
// first we sum up all positive chunk size changes (and skip all negative ones) |
// first we sum up all positive chunk size changes (and skip all negative ones) |
1601 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1602 |
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
for (std::set<Chunk*>::const_iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
1603 |
if ((*iter)->GetNewSize() == 0) throw Exception("There is at least one empty chunk (zero size)"); |
if ((*iter)->GetNewSize() == 0) { |
1604 |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
|
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
|
|
ulPositiveSizeDiff += ulDiff; |
|
1605 |
} |
} |
1606 |
|
unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2; |
1607 |
|
unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2; |
1608 |
|
if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded; |
1609 |
} |
} |
1610 |
|
|
1611 |
unsigned long ulWorkingFileSize = GetFileSize(); |
unsigned long ulWorkingFileSize = GetFileSize(); |
1618 |
// ... and move current data by the same amount towards end of file. |
// ... and move current data by the same amount towards end of file. |
1619 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
1620 |
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
1621 |
|
#if defined(WIN32) |
1622 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1623 |
|
#else |
1624 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1625 |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
#endif |
1626 |
const unsigned long ulToMove = ulFileSize - ulPos; |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
1627 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
1628 |
|
ulPos -= iBytesMoved; |
1629 |
#if POSIX |
#if POSIX |
1630 |
lseek(hFileRead, ulPos, SEEK_SET); |
lseek(hFileRead, ulPos, SEEK_SET); |
1631 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1632 |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1633 |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
1634 |
|
#elif defined(WIN32) |
1635 |
|
SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN); |
1636 |
|
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1637 |
|
SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN); |
1638 |
|
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1639 |
#else |
#else |
1640 |
fseek(hFileRead, ulPos, SEEK_SET); |
fseek(hFileRead, ulPos, SEEK_SET); |
1641 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
1685 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
1686 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
throw Exception("Could not open file \"" + path + "\" for writing"); |
1687 |
} |
} |
1688 |
|
#elif defined(WIN32) |
1689 |
|
hFileWrite = CreateFile( |
1690 |
|
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1691 |
|
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | |
1692 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1693 |
|
); |
1694 |
|
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1695 |
|
hFileWrite = hFileRead; |
1696 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1697 |
|
} |
1698 |
#else |
#else |
1699 |
hFileWrite = fopen(path.c_str(), "w+b"); |
hFileWrite = fopen(path.c_str(), "w+b"); |
1700 |
if (!hFileWrite) { |
if (!hFileWrite) { |
1714 |
// forget all resized chunks |
// forget all resized chunks |
1715 |
ResizedChunks.clear(); |
ResizedChunks.clear(); |
1716 |
|
|
1717 |
if (Filename.length() > 0) { |
#if POSIX |
1718 |
#if POSIX |
if (hFileWrite) close(hFileWrite); |
1719 |
close(hFileWrite); |
#elif defined(WIN32) |
1720 |
#else |
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1721 |
fclose(hFileWrite); |
#else |
1722 |
#endif |
if (hFileWrite) fclose(hFileWrite); |
1723 |
hFileWrite = hFileRead; |
#endif |
1724 |
} |
hFileWrite = hFileRead; |
1725 |
|
|
1726 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
1727 |
Filename = path; |
Filename = path; |
1733 |
#if POSIX |
#if POSIX |
1734 |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
1735 |
throw Exception("Could not resize file \"" + Filename + "\""); |
throw Exception("Could not resize file \"" + Filename + "\""); |
1736 |
|
#elif defined(WIN32) |
1737 |
|
if ( |
1738 |
|
SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER || |
1739 |
|
!SetEndOfFile(hFileWrite) |
1740 |
|
) throw Exception("Could not resize file \"" + Filename + "\""); |
1741 |
#else |
#else |
1742 |
# error Sorry, this version of libgig only supports POSIX systems yet. |
# error Sorry, this version of libgig only supports POSIX and Windows systems yet. |
1743 |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
1744 |
#endif |
#endif |
1745 |
} |
} |
1750 |
#endif // DEBUG |
#endif // DEBUG |
1751 |
#if POSIX |
#if POSIX |
1752 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1753 |
|
#elif defined(WIN32) |
1754 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1755 |
#else |
#else |
1756 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1757 |
#endif // POSIX |
#endif // POSIX |
1758 |
} |
} |
1759 |
|
|
1760 |
void File::LogAsResized(Chunk* pResizedChunk) { |
void File::LogAsResized(Chunk* pResizedChunk) { |
1761 |
ResizedChunks.push_back(pResizedChunk); |
ResizedChunks.insert(pResizedChunk); |
1762 |
|
} |
1763 |
|
|
1764 |
|
void File::UnlogResized(Chunk* pResizedChunk) { |
1765 |
|
ResizedChunks.erase(pResizedChunk); |
1766 |
} |
} |
1767 |
|
|
1768 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |
1776 |
long size = filestat.st_size; |
long size = filestat.st_size; |
1777 |
return size; |
return size; |
1778 |
} |
} |
1779 |
|
#elif defined(WIN32) |
1780 |
|
unsigned long File::__GetFileSize(HANDLE hFile) { |
1781 |
|
DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/); |
1782 |
|
if (dwSize == INVALID_FILE_SIZE) |
1783 |
|
throw Exception("Windows FS error: could not determine file size"); |
1784 |
|
return dwSize; |
1785 |
|
} |
1786 |
#else // standard C functions |
#else // standard C functions |
1787 |
unsigned long File::__GetFileSize(FILE* hFile) { |
unsigned long File::__GetFileSize(FILE* hFile) { |
1788 |
long curpos = ftell(hFile); |
long curpos = ftell(hFile); |