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-2007 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" |
34 |
// * |
// * |
35 |
|
|
36 |
/// Returns a human readable path of the given chunk. |
/// Returns a human readable path of the given chunk. |
37 |
String __resolveChunkPath(Chunk* pCk) { |
static String __resolveChunkPath(Chunk* pCk) { |
38 |
String sPath; |
String sPath; |
39 |
for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { |
for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { |
40 |
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
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 |
if (CurrentChunkSize != NewChunkSize) pFile->UnlogResized(this); |
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); |
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 |
} |
} |
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() { |
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 |
366 |
#elif defined(WIN32) |
#elif defined(WIN32) |
367 |
if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
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) + |
throw Exception("Could not seek to position " + ToString(ulPos) + |
369 |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
370 |
} |
} |
371 |
DWORD writtenWords; |
DWORD writtenWords; |
372 |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
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) |
#elif defined(WIN32) |
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 |
|
|
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 |
|
|
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); |
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) |
#if defined(WIN32) |
1425 |
path.c_str(), GENERIC_READ, |
path.c_str(), GENERIC_READ, |
1426 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1427 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1428 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1429 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1430 |
); |
); |
1431 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1432 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
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 |
} |
} |
1479 |
Filename.c_str(), GENERIC_READ, |
Filename.c_str(), GENERIC_READ, |
1480 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1481 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1482 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1483 |
|
FILE_FLAG_RANDOM_ACCESS, |
1484 |
|
NULL |
1485 |
); |
); |
1486 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1487 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1509 |
GENERIC_READ | GENERIC_WRITE, |
GENERIC_READ | GENERIC_WRITE, |
1510 |
FILE_SHARE_READ, |
FILE_SHARE_READ, |
1511 |
NULL, OPEN_ALWAYS, |
NULL, OPEN_ALWAYS, |
1512 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1513 |
|
FILE_FLAG_RANDOM_ACCESS, |
1514 |
|
NULL |
1515 |
); |
); |
1516 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1517 |
hFileRead = hFileWrite = CreateFile( |
hFileRead = hFileWrite = CreateFile( |
1518 |
Filename.c_str(), GENERIC_READ, |
Filename.c_str(), GENERIC_READ, |
1519 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1520 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1521 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1522 |
|
FILE_FLAG_RANDOM_ACCESS, |
1523 |
|
NULL |
1524 |
); |
); |
1525 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
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); |
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) { |
if ((*iter)->GetNewSize() == 0) { |
1604 |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
1605 |
} |
} |
1606 |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2; |
1607 |
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2; |
1608 |
ulPositiveSizeDiff += ulDiff; |
if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded; |
|
} |
|
1609 |
} |
} |
1610 |
|
|
1611 |
unsigned long ulWorkingFileSize = GetFileSize(); |
unsigned long ulWorkingFileSize = GetFileSize(); |
1621 |
#if defined(WIN32) |
#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 |
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1623 |
#else |
#else |
1624 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1625 |
#endif |
#endif |
1626 |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
1627 |
const unsigned long ulToMove = ulFileSize - ulPos; |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
1628 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
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); |
1688 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1689 |
hFileWrite = CreateFile( |
hFileWrite = CreateFile( |
1690 |
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1691 |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | |
1692 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1693 |
); |
); |
1694 |
if (hFileWrite == INVALID_HANDLE_VALUE) { |
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1695 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
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 |
#elif defined(WIN32) |
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1721 |
CloseHandle(hFileWrite); |
#else |
1722 |
#else |
if (hFileWrite) fclose(hFileWrite); |
1723 |
fclose(hFileWrite); |
#endif |
1724 |
#endif |
hFileWrite = hFileRead; |
|
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; |
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) { |
void File::UnlogResized(Chunk* pResizedChunk) { |
1765 |
ResizedChunks.remove(pResizedChunk); |
ResizedChunks.erase(pResizedChunk); |
1766 |
} |
} |
1767 |
|
|
1768 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |