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) { |
90 |
} |
} |
91 |
|
|
92 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
93 |
if (CurrentChunkSize != NewChunkSize) pFile->UnlogResized(this); |
pFile->UnlogResized(this); |
94 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
95 |
} |
} |
96 |
|
|
236 |
* - RIFF::stream_closed : |
* - RIFF::stream_closed : |
237 |
* the data stream was closed somehow, no more reading possible |
* the data stream was closed somehow, no more reading possible |
238 |
* - RIFF::stream_end_reached : |
* - RIFF::stream_end_reached : |
239 |
* alreaady reached the end of the chunk data, no more reading |
* already reached the end of the chunk data, no more reading |
240 |
* possible without SetPos() |
* possible without SetPos() |
241 |
*/ |
*/ |
242 |
stream_state_t Chunk::GetState() { |
stream_state_t Chunk::GetState() { |
274 |
#if DEBUG |
#if DEBUG |
275 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
276 |
#endif // DEBUG |
#endif // DEBUG |
277 |
|
if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
278 |
if (ulPos >= CurrentChunkSize) return 0; |
if (ulPos >= CurrentChunkSize) return 0; |
279 |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
280 |
#if POSIX |
#if POSIX |
360 |
#elif defined(WIN32) |
#elif defined(WIN32) |
361 |
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) { |
362 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
throw Exception("Could not seek to position " + ToString(ulPos) + |
363 |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
364 |
} |
} |
365 |
DWORD writtenWords; |
DWORD writtenWords; |
366 |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
727 |
* @see ReleaseChunkData() |
* @see ReleaseChunkData() |
728 |
*/ |
*/ |
729 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
730 |
if (!pChunkData && pFile->Filename != "") { |
if (!pChunkData && pFile->Filename != "" && ulStartPos != 0) { |
731 |
#if POSIX |
#if POSIX |
732 |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
733 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1154 |
pSubChunks->push_back(pNewChunk); |
pSubChunks->push_back(pNewChunk); |
1155 |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1156 |
pNewChunk->Resize(uiBodySize); |
pNewChunk->Resize(uiBodySize); |
1157 |
|
NewChunkSize += CHUNK_HEADER_SIZE; |
1158 |
|
pFile->LogAsResized(this); |
1159 |
return pNewChunk; |
return pNewChunk; |
1160 |
} |
} |
1161 |
|
|
1191 |
List* pNewListChunk = new List(pFile, this, uiListType); |
List* pNewListChunk = new List(pFile, this, uiListType); |
1192 |
pSubChunks->push_back(pNewListChunk); |
pSubChunks->push_back(pNewListChunk); |
1193 |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1194 |
|
NewChunkSize += LIST_HEADER_SIZE; |
1195 |
|
pFile->LogAsResized(this); |
1196 |
return pNewListChunk; |
return pNewListChunk; |
1197 |
} |
} |
1198 |
|
|
1274 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1275 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1276 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1277 |
|
#if defined(WIN32) |
1278 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) return; |
1279 |
|
#else |
1280 |
if (!pFile->hFileRead) return; |
if (!pFile->hFileRead) return; |
1281 |
|
#endif |
1282 |
unsigned long uiOriginalPos = GetPos(); |
unsigned long uiOriginalPos = GetPos(); |
1283 |
SetPos(0); // jump to beginning of list chunk body |
SetPos(0); // jump to beginning of list chunk body |
1284 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
1418 |
path.c_str(), GENERIC_READ, |
path.c_str(), GENERIC_READ, |
1419 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1420 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1421 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1422 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1423 |
); |
); |
1424 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1425 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1472 |
Filename.c_str(), GENERIC_READ, |
Filename.c_str(), GENERIC_READ, |
1473 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1474 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1475 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1476 |
|
FILE_FLAG_RANDOM_ACCESS, |
1477 |
|
NULL |
1478 |
); |
); |
1479 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1480 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1502 |
GENERIC_READ | GENERIC_WRITE, |
GENERIC_READ | GENERIC_WRITE, |
1503 |
FILE_SHARE_READ, |
FILE_SHARE_READ, |
1504 |
NULL, OPEN_ALWAYS, |
NULL, OPEN_ALWAYS, |
1505 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1506 |
|
FILE_FLAG_RANDOM_ACCESS, |
1507 |
|
NULL |
1508 |
); |
); |
1509 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1510 |
hFileRead = hFileWrite = CreateFile( |
hFileRead = hFileWrite = CreateFile( |
1511 |
Filename.c_str(), GENERIC_READ, |
Filename.c_str(), GENERIC_READ, |
1512 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1513 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1514 |
FILE_ATTRIBUTE_NORMAL, NULL |
FILE_ATTRIBUTE_NORMAL | |
1515 |
|
FILE_FLAG_RANDOM_ACCESS, |
1516 |
|
NULL |
1517 |
); |
); |
1518 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); |
1519 |
} |
} |
1520 |
#else |
#else |
1521 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1592 |
|
|
1593 |
// 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) |
1594 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1595 |
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) { |
1596 |
if ((*iter)->GetNewSize() == 0) { |
if ((*iter)->GetNewSize() == 0) { |
1597 |
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)); |
1598 |
} |
} |
1599 |
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2; |
1600 |
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; |
1601 |
ulPositiveSizeDiff += ulDiff; |
if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded; |
|
} |
|
1602 |
} |
} |
1603 |
|
|
1604 |
unsigned long ulWorkingFileSize = GetFileSize(); |
unsigned long ulWorkingFileSize = GetFileSize(); |
1614 |
#if defined(WIN32) |
#if defined(WIN32) |
1615 |
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 |
1616 |
#else |
#else |
1617 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1618 |
#endif |
#endif |
1619 |
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
1620 |
const unsigned long ulToMove = ulFileSize - ulPos; |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
1621 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
ulPos -= iBytesMoved; |
1622 |
#if POSIX |
#if POSIX |
1623 |
lseek(hFileRead, ulPos, SEEK_SET); |
lseek(hFileRead, ulPos, SEEK_SET); |
1624 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1681 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1682 |
hFileWrite = CreateFile( |
hFileWrite = CreateFile( |
1683 |
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1684 |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | |
1685 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1686 |
); |
); |
1687 |
if (hFileWrite == INVALID_HANDLE_VALUE) { |
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1688 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
1707 |
// forget all resized chunks |
// forget all resized chunks |
1708 |
ResizedChunks.clear(); |
ResizedChunks.clear(); |
1709 |
|
|
1710 |
if (Filename.length() > 0) { |
#if POSIX |
1711 |
#if POSIX |
if (hFileWrite) close(hFileWrite); |
1712 |
close(hFileWrite); |
#elif defined(WIN32) |
1713 |
#elif defined(WIN32) |
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1714 |
CloseHandle(hFileWrite); |
#else |
1715 |
#else |
if (hFileWrite) fclose(hFileWrite); |
1716 |
fclose(hFileWrite); |
#endif |
1717 |
#endif |
hFileWrite = hFileRead; |
|
hFileWrite = hFileRead; |
|
|
} |
|
1718 |
|
|
1719 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
1720 |
Filename = path; |
Filename = path; |
1751 |
} |
} |
1752 |
|
|
1753 |
void File::LogAsResized(Chunk* pResizedChunk) { |
void File::LogAsResized(Chunk* pResizedChunk) { |
1754 |
ResizedChunks.push_back(pResizedChunk); |
ResizedChunks.insert(pResizedChunk); |
1755 |
} |
} |
1756 |
|
|
1757 |
void File::UnlogResized(Chunk* pResizedChunk) { |
void File::UnlogResized(Chunk* pResizedChunk) { |
1758 |
ResizedChunks.remove(pResizedChunk); |
ResizedChunks.erase(pResizedChunk); |
1759 |
} |
} |
1760 |
|
|
1761 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |