RIFF.cpp
/***************************************************************************
* *
* libgig - C++ cross-platform Gigasampler format file access library *
* *
* Copyright (C) 2003-2025 by Christian Schoenebeck *
* <cuse@users.sourceforge.net> *
* *
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
* MA 02111-1307 USA *
***************************************************************************/
#include <algorithm>
#include <set>
#include <string.h>
#include "RIFF.h"
#include "helper.h"
#if POSIX
# include <errno.h>
#endif
#if POSIX
# define RIFF_INVALID_FILE_HANDLE -1 /* not 0 as 0 refers to stdin on POSIX */
#elif defined(WIN32)
# define RIFF_INVALID_FILE_HANDLE INVALID_HANDLE_VALUE
#else
# define RIFF_INVALID_FILE_HANDLE NULL
#endif
namespace RIFF {
// *************** Internal functions **************
// *
static String __resolveChunkPath(Chunk* pCk) {
String sPath;
for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) {
if (pChunk->GetChunkID() == CHUNK_ID_LIST) {
List* pList = (List*) pChunk;
sPath = "->'" + pList->GetListTypeString() + "'" + sPath;
} else {
sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath;
}
}
return sPath;
}
inline static bool _isValidHandle(File::Handle handle) {
return handle != RIFF_INVALID_FILE_HANDLE;
}
inline static void _close(File::Handle handle) {
if (!_isValidHandle(handle)) return;
#if POSIX
close(handle);
#elif defined(WIN32)
CloseHandle(handle);
#else
fclose(handle);
#endif
}
// *************** progress_t ***************
// *
progress_t::progress_t() {
callback = NULL;
custom = NULL;
activity = "";
__range_min = 0.0f;
__range_max = 1.0f;
__private = NULL;
}
progress_t::~progress_t() {
}
std::vector<progress_t> progress_t::subdivide(int iSubtasks) const {
std::vector<progress_t> v;
for (int i = 0; i < iSubtasks; ++i) {
progress_t p;
__divide_progress(this, &p, iSubtasks, i);
v.push_back(p);
}
return v;
}
std::vector<progress_t> progress_t::subdivide(std::vector<float> vSubTaskPortions) const {
float fTotal = 0.f; // usually 1.0, but we sum the portions up below to be sure
for (int i = 0; i < vSubTaskPortions.size(); ++i)
fTotal += vSubTaskPortions[i];
float fLow = 0.f, fHigh = 0.f;
std::vector<progress_t> v;
for (int i = 0; i < vSubTaskPortions.size(); ++i) {
fLow = fHigh;
fHigh = vSubTaskPortions[i];
progress_t p;
__divide_progress(this, &p, fTotal, fLow, fHigh);
v.push_back(p);
}
return v;
}
// *************** _FileProgress **************
// *
struct _FileProgress {
file_offset_t current;
file_offset_t total;
void advance(progress_t* progress, file_offset_t bytes) {
current += bytes;
__notify_progress(progress, float(current) / float(total));
}
};
// *************** Chunk **************
// *
Chunk::Chunk(File* pFile) {
#if DEBUG_RIFF
std::cout << "Chunk::Chunk(File* pFile)" << std::endl;
#endif // DEBUG_RIFF
chunkPos.ullPos = 0;
pParent = NULL;
pChunkData = NULL;
ullCurrentChunkSize = 0;
ullNewChunkSize = 0;
ullChunkDataSize = 0;
ChunkID = CHUNK_ID_RIFF;
this->pFile = pFile;
}
Chunk::Chunk(File* pFile, file_offset_t StartPos, List* Parent) {
#if DEBUG_RIFF
std::cout << "Chunk::Chunk(File*,file_offset_t,List*),StartPos=" << StartPos << std::endl;
#endif // DEBUG_RIFF
this->pFile = pFile;
ullStartPos = StartPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
pParent = Parent;
chunkPos.ullPos = 0;
pChunkData = NULL;
ullCurrentChunkSize = 0;
ullNewChunkSize = 0;
ullChunkDataSize = 0;
ReadHeader(StartPos);
}
Chunk::Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize) {
this->pFile = pFile;
ullStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk
this->pParent = pParent;
chunkPos.ullPos = 0;
pChunkData = NULL;
ChunkID = uiChunkID;
ullChunkDataSize = 0;
ullCurrentChunkSize = 0;
ullNewChunkSize = ullBodySize;
}
Chunk::~Chunk() {
if (pChunkData) delete[] pChunkData;
}
void Chunk::ReadHeader(file_offset_t filePos) {
#if DEBUG_RIFF
std::cout << "Chunk::Readheader(" << filePos << ") ";
#endif // DEBUG_RIFF
ChunkID = 0;
ullNewChunkSize = ullCurrentChunkSize = 0;
const File::Handle hRead = pFile->FileHandle();
#if POSIX
if (lseek(hRead, filePos, SEEK_SET) != -1) {
read(hRead, &ChunkID, 4);
read(hRead, &ullCurrentChunkSize, pFile->FileOffsetSize);
#elif defined(WIN32)
LARGE_INTEGER liFilePos;
liFilePos.QuadPart = filePos;
if (SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) {
DWORD dwBytesRead;
ReadFile(hRead, &ChunkID, 4, &dwBytesRead, NULL);
ReadFile(hRead, &ullCurrentChunkSize, pFile->FileOffsetSize, &dwBytesRead, NULL);
#else
if (!fseeko(hRead, filePos, SEEK_SET)) {
fread(&ChunkID, 4, 1, hRead);
fread(&ullCurrentChunkSize, pFile->FileOffsetSize, 1, hRead);
#endif // POSIX
#if WORDS_BIGENDIAN
if (ChunkID == CHUNK_ID_RIFF) {
pFile->bEndianNative = false;
}
#else // little endian
if (ChunkID == CHUNK_ID_RIFX) {
pFile->bEndianNative = false;
ChunkID = CHUNK_ID_RIFF;
}
#endif // WORDS_BIGENDIAN
if (!pFile->bEndianNative) {
//swapBytes_32(&ChunkID);
if (pFile->FileOffsetSize == 4)
swapBytes_32(&ullCurrentChunkSize);
else
swapBytes_64(&ullCurrentChunkSize);
}
#if DEBUG_RIFF
std::cout << "ckID=" << convertToString(ChunkID) << " ";
std::cout << "ckSize=" << ullCurrentChunkSize << " ";
std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl;
#endif // DEBUG_RIFF
ullNewChunkSize = ullCurrentChunkSize;
}
}
void Chunk::WriteHeader(file_offset_t filePos) {
uint32_t uiNewChunkID = ChunkID;
if (ChunkID == CHUNK_ID_RIFF) {
#if WORDS_BIGENDIAN
if (pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX;
#else // little endian
if (!pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX;
#endif // WORDS_BIGENDIAN
}
uint64_t ullNewChunkSize = this->ullNewChunkSize;
if (!pFile->bEndianNative) {
if (pFile->FileOffsetSize == 4)
swapBytes_32(&ullNewChunkSize);
else
swapBytes_64(&ullNewChunkSize);
}
const File::Handle hWrite = pFile->FileWriteHandle();
#if POSIX
if (lseek(hWrite, filePos, SEEK_SET) != -1) {
write(hWrite, &uiNewChunkID, 4);
write(hWrite, &ullNewChunkSize, pFile->FileOffsetSize);
}
#elif defined(WIN32)
LARGE_INTEGER liFilePos;
liFilePos.QuadPart = filePos;
if (SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) {
DWORD dwBytesWritten;
WriteFile(hWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL);
WriteFile(hWrite, &ullNewChunkSize, pFile->FileOffsetSize, &dwBytesWritten, NULL);
}
#else
if (!fseeko(hWrite, filePos, SEEK_SET)) {
fwrite(&uiNewChunkID, 4, 1, hWrite);
fwrite(&ullNewChunkSize, pFile->FileOffsetSize, 1, hWrite);
}
#endif // POSIX
}
String Chunk::GetChunkIDString() const {
return convertToString(ChunkID);
}
file_offset_t& Chunk::GetPosUnsafeRef() {
if (!pFile->IsIOPerThread()) return chunkPos.ullPos;
const std::thread::id tid = std::this_thread::get_id();
return chunkPos.byThread[tid];
}
file_offset_t Chunk::GetPos() const {
if (!pFile->IsIOPerThread()) return chunkPos.ullPos;
const std::thread::id tid = std::this_thread::get_id();
std::lock_guard<std::mutex> lock(chunkPos.mutex);
return chunkPos.byThread[tid];
}
file_offset_t Chunk::GetFilePos() const {
return ullStartPos + GetPos();
}
file_offset_t Chunk::SetPos(file_offset_t Where, stream_whence_t Whence) {
#if DEBUG_RIFF
std::cout << "Chunk::SetPos(file_offset_t,stream_whence_t)" << std::endl;
#endif // DEBUG_RIFF
std::lock_guard<std::mutex> lock(chunkPos.mutex);
file_offset_t& pos = GetPosUnsafeRef();
switch (Whence) {
case stream_curpos:
pos += Where;
break;
case stream_end:
pos = ullCurrentChunkSize - 1 - Where;
break;
case stream_backward:
pos -= Where;
break;
case stream_start: default:
pos = Where;
break;
}
if (pos > ullCurrentChunkSize) pos = ullCurrentChunkSize;
return pos;
}
file_offset_t Chunk::RemainingBytes() const {
#if DEBUG_RIFF
std::cout << "Chunk::Remainingbytes()=" << ullCurrentChunkSize - ullPos << std::endl;
#endif // DEBUG_RIFF
const file_offset_t pos = GetPos();
return (ullCurrentChunkSize > pos) ? ullCurrentChunkSize - pos : 0;
}
file_offset_t Chunk::RequiredPhysicalSize(int fileOffsetSize) {
return CHUNK_HEADER_SIZE(fileOffsetSize) + // RIFF chunk header
ullNewChunkSize + // chunks's actual data body
ullNewChunkSize % 2; // optional pad byte
}
stream_state_t Chunk::GetState() const {
#if DEBUG_RIFF
std::cout << "Chunk::GetState()" << std::endl;
#endif // DEBUG_RIFF
const File::Handle hRead = pFile->FileHandle();
if (!_isValidHandle(hRead))
return stream_closed;
const file_offset_t pos = GetPos();
if (pos < ullCurrentChunkSize) return stream_ready;
else return stream_end_reached;
}
file_offset_t Chunk::Read(void* pData, file_offset_t WordCount, file_offset_t WordSize) {
#if DEBUG_RIFF
std::cout << "Chunk::Read(void*,file_offset_t,file_offset_t)" << std::endl;
#endif // DEBUG_RIFF
//if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet)
const file_offset_t pos = GetPos();
if (pos >= ullCurrentChunkSize) return 0;
if (pos + WordCount * WordSize >= ullCurrentChunkSize)
WordCount = (ullCurrentChunkSize - pos) / WordSize;
const File::Handle hRead = pFile->FileHandle();
#if POSIX
if (lseek(hRead, ullStartPos + pos, SEEK_SET) < 0) return 0;
ssize_t readWords = read(hRead, pData, WordCount * WordSize);
if (readWords < 1) {
#if DEBUG_RIFF
std::cerr << "POSIX read() failed: " << strerror(errno) << std::endl << std::flush;
#endif // DEBUG_RIFF
return 0;
}
readWords /= WordSize;
#elif defined(WIN32)
LARGE_INTEGER liFilePos;
liFilePos.QuadPart = ullStartPos + pos;
if (!SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN))
return 0;
DWORD readWords;
ReadFile(hRead, pData, WordCount * WordSize, &readWords, NULL); //FIXME: does not work for reading buffers larger than 2GB (even though this should rarely be the case in practice)
if (readWords < 1) return 0;
readWords /= WordSize;
#else // standard C functions
if (fseeko(hRead, ullStartPos + pos, SEEK_SET)) return 0;
file_offset_t readWords = fread(pData, WordSize, WordCount, hRead);
#endif // POSIX
if (!pFile->bEndianNative && WordSize != 1) {
switch (WordSize) {
case 2:
for (file_offset_t iWord = 0; iWord < readWords; iWord++)
swapBytes_16((uint16_t*) pData + iWord);
break;
case 4:
for (file_offset_t iWord = 0; iWord < readWords; iWord++)
swapBytes_32((uint32_t*) pData + iWord);
break;
case 8:
for (file_offset_t iWord = 0; iWord < readWords; iWord++)
swapBytes_64((uint64_t*) pData + iWord);
break;
default:
for (file_offset_t iWord = 0; iWord < readWords; iWord++)
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize);
break;
}
}
SetPos(readWords * WordSize, stream_curpos);
return readWords;
}
file_offset_t Chunk::Write(void* pData, file_offset_t WordCount, file_offset_t WordSize) {
const File::HandlePair io = pFile->FileHandlePair();
if (io.Mode != stream_mode_read_write)
throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first");
const file_offset_t pos = GetPos();
if (pos >= ullCurrentChunkSize || pos + WordCount * WordSize > ullCurrentChunkSize)
throw Exception("End of chunk reached while trying to write data");
if (!pFile->bEndianNative && WordSize != 1) {
switch (WordSize) {
case 2:
for (file_offset_t iWord = 0; iWord < WordCount; iWord++)
swapBytes_16((uint16_t*) pData + iWord);
break;
case 4:
for (file_offset_t iWord = 0; iWord < WordCount; iWord++)
swapBytes_32((uint32_t*) pData + iWord);
break;
case 8:
for (file_offset_t iWord = 0; iWord < WordCount; iWord++)
swapBytes_64((uint64_t*) pData + iWord);
break;
default:
for (file_offset_t iWord = 0; iWord < WordCount; iWord++)
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize);
break;
}
}
#if POSIX
if (lseek(io.hWrite, ullStartPos + pos, SEEK_SET) < 0) {
throw Exception("Could not seek to position " + ToString(pos) +
" in chunk (" + ToString(ullStartPos + pos) + " in file)");
}
ssize_t writtenWords = write(io.hWrite, pData, WordCount * WordSize);
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data");
writtenWords /= WordSize;
#elif defined(WIN32)
LARGE_INTEGER liFilePos;
liFilePos.QuadPart = ullStartPos + pos;
if (!SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) {
throw Exception("Could not seek to position " + ToString(pos) +
" in chunk (" + ToString(ullStartPos + pos) + " in file)");
}
DWORD writtenWords;
WriteFile(io.hWrite, pData, WordCount * WordSize, &writtenWords, NULL); //FIXME: does not work for writing buffers larger than 2GB (even though this should rarely be the case in practice)
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data");
writtenWords /= WordSize;
#else // standard C functions
if (fseeko(io.hWrite, ullStartPos + pos, SEEK_SET)) {
throw Exception("Could not seek to position " + ToString(pos) +
" in chunk (" + ToString(ullStartPos + pos) + " in file)");
}
file_offset_t writtenWords = fwrite(pData, WordSize, WordCount, io.hWrite);
#endif // POSIX
SetPos(writtenWords * WordSize, stream_curpos);
return writtenWords;
}
file_offset_t Chunk::ReadSceptical(void* pData, file_offset_t WordCount, file_offset_t WordSize) {
file_offset_t readWords = Read(pData, WordCount, WordSize);
if (readWords != WordCount) throw RIFF::Exception("End of chunk data reached.");
return readWords;
}
file_offset_t Chunk::ReadInt8(int8_t* pData, file_offset_t WordCount) {
#if DEBUG_RIFF
std::cout << "Chunk::ReadInt8(int8_t*,file_offset_t)" << std::endl;
#endif // DEBUG_RIFF
return ReadSceptical(pData, WordCount, 1);
}
file_offset_t Chunk::WriteInt8(int8_t* pData, file_offset_t WordCount) {
return Write(pData, WordCount, 1);
}
file_offset_t Chunk::ReadUint8(uint8_t* pData, file_offset_t WordCount) {
#if DEBUG_RIFF
std::cout << "Chunk::ReadUint8(uint8_t*,file_offset_t)" << std::endl;
#endif // DEBUG_RIFF
return ReadSceptical(pData, WordCount, 1);
}
file_offset_t Chunk::WriteUint8(uint8_t* pData, file_offset_t WordCount) {
return Write(pData, WordCount, 1);
}
file_offset_t Chunk::ReadInt16(int16_t* pData, file_offset_t WordCount) {
#if DEBUG_RIFF
std::cout << "Chunk::ReadInt16(int16_t*,file_offset_t)" << std::endl;
#endif // DEBUG_RIFF
return ReadSceptical(pData, WordCount, 2);
}
file_offset_t Chunk::WriteInt16(int16_t* pData, file_offset_t WordCount) {
return Write(pData, WordCount, 2);
}
file_offset_t Chunk::ReadUint16(uint16_t* pData, file_offset_t WordCount) {
#if DEBUG_RIFF
std::cout << "Chunk::ReadUint16(uint16_t*,file_offset_t)" << std::endl;
#endif // DEBUG_RIFF
return ReadSceptical(pData, WordCount, 2);
}
file_offset_t Chunk::WriteUint16(uint16_t* pData, file_offset_t WordCount) {
return Write(pData, WordCount, 2);
}
file_offset_t Chunk::ReadInt32(int32_t* pData, file_offset_t WordCount) {
#if DEBUG_RIFF
std::cout << "Chunk::ReadInt32(int32_t*,file_offset_t)" << std::endl;
#endif // DEBUG_RIFF
return ReadSceptical(pData, WordCount, 4);
}
file_offset_t Chunk::WriteInt32(int32_t* pData, file_offset_t WordCount) {
return Write(pData, WordCount, 4);
}
file_offset_t Chunk::ReadUint32(uint32_t* pData, file_offset_t WordCount) {
#if DEBUG_RIFF
std::cout << "Chunk::ReadUint32(uint32_t*,file_offset_t)" << std::endl;
#endif // DEBUG_RIFF
return ReadSceptical(pData, WordCount, 4);
}
void Chunk::ReadString(String& s, int size) {
char* buf = new char[size];
ReadSceptical(buf, 1, size);
s.assign(buf, std::find(buf, buf + size, '\0'));
delete[] buf;
}
file_offset_t Chunk::WriteUint32(uint32_t* pData, file_offset_t WordCount) {
return Write(pData, WordCount, 4);
}
int8_t Chunk::ReadInt8() {
#if DEBUG_RIFF
std::cout << "Chunk::ReadInt8()" << std::endl;
#endif // DEBUG_RIFF
int8_t word;
ReadSceptical(&word,1,1);
return word;
}
uint8_t Chunk::ReadUint8() {
#if DEBUG_RIFF
std::cout << "Chunk::ReadUint8()" << std::endl;
#endif // DEBUG_RIFF
uint8_t word;
ReadSceptical(&word,1,1);
return word;
}
int16_t Chunk::ReadInt16() {
#if DEBUG_RIFF
std::cout << "Chunk::ReadInt16()" << std::endl;
#endif // DEBUG_RIFF
int16_t word;
ReadSceptical(&word,1,2);
return word;
}
uint16_t Chunk::ReadUint16() {
#if DEBUG_RIFF
std::cout << "Chunk::ReadUint16()" << std::endl;
#endif // DEBUG_RIFF
uint16_t word;
ReadSceptical(&word,1,2);
return word;
}
int32_t Chunk::ReadInt32() {
#if DEBUG_RIFF
std::cout << "Chunk::ReadInt32()" << std::endl;
#endif // DEBUG_RIFF
int32_t word;
ReadSceptical(&word,1,4);
return word;
}
uint32_t Chunk::ReadUint32() {
#if DEBUG_RIFF
std::cout << "Chunk::ReadUint32()" << std::endl;
#endif // DEBUG_RIFF
uint32_t word;
ReadSceptical(&word,1,4);
return word;
}
void* Chunk::LoadChunkData() {
if (!pChunkData && pFile->Filename != "" /*&& ulStartPos != 0*/) {
File::Handle hRead = pFile->FileHandle();
#if POSIX
if (lseek(hRead, ullStartPos, SEEK_SET) == -1) return NULL;
#elif defined(WIN32)
LARGE_INTEGER liFilePos;
liFilePos.QuadPart = ullStartPos;
if (!SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) return NULL;
#else
if (fseeko(hRead, ullStartPos, SEEK_SET)) return NULL;
#endif // POSIX
file_offset_t ullBufferSize = (ullCurrentChunkSize > ullNewChunkSize) ? ullCurrentChunkSize : ullNewChunkSize;
pChunkData = new uint8_t[ullBufferSize];
if (!pChunkData) return NULL;
memset(pChunkData, 0, ullBufferSize);
#if POSIX
file_offset_t readWords = read(hRead, pChunkData, GetSize());
#elif defined(WIN32)
DWORD readWords;
ReadFile(hRead, pChunkData, GetSize(), &readWords, NULL); //FIXME: won't load chunks larger than 2GB !
#else
file_offset_t readWords = fread(pChunkData, 1, GetSize(), hRead);
#endif // POSIX
if (readWords != GetSize()) {
delete[] pChunkData;
return (pChunkData = NULL);
}
ullChunkDataSize = ullBufferSize;
} else if (ullNewChunkSize > ullChunkDataSize) {
uint8_t* pNewBuffer = new uint8_t[ullNewChunkSize];
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(ullNewChunkSize) + " bytes");
memset(pNewBuffer, 0 , ullNewChunkSize);
if (pChunkData) {
memcpy(pNewBuffer, pChunkData, ullChunkDataSize);
delete[] pChunkData;
}
pChunkData = pNewBuffer;
ullChunkDataSize = ullNewChunkSize;
}
return pChunkData;
}
void Chunk::ReleaseChunkData() {
if (pChunkData) {
delete[] pChunkData;
pChunkData = NULL;
}
}
void Chunk::Resize(file_offset_t NewSize) {
if (NewSize == 0)
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this));
if ((NewSize >> 48) != 0)
throw Exception("Unrealistic high chunk size detected: " + __resolveChunkPath(this));
if (ullNewChunkSize == NewSize) return;
ullNewChunkSize = NewSize;
}
file_offset_t Chunk::WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress) {
const file_offset_t ullOriginalPos = ullWritePos;
ullWritePos += CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
const File::HandlePair io = pFile->FileHandlePair();
if (io.Mode != stream_mode_read_write)
throw Exception("Cannot write list chunk, file has to be opened in read+write mode");
// if the whole chunk body was loaded into RAM
if (pChunkData) {
// make sure chunk data buffer in RAM is at least as large as the new chunk size
LoadChunkData();
// write chunk data from RAM persistently to the file
#if POSIX
lseek(io.hWrite, ullWritePos, SEEK_SET);
if (write(io.hWrite, pChunkData, ullNewChunkSize) != ullNewChunkSize) {
throw Exception("Writing Chunk data (from RAM) failed");
}
#elif defined(WIN32)
LARGE_INTEGER liFilePos;
liFilePos.QuadPart = ullWritePos;
SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
DWORD dwBytesWritten;
WriteFile(io.hWrite, pChunkData, ullNewChunkSize, &dwBytesWritten, NULL); //FIXME: won't save chunks larger than 2GB !
if (dwBytesWritten != ullNewChunkSize) {
throw Exception("Writing Chunk data (from RAM) failed");
}
#else
fseeko(io.hWrite, ullWritePos, SEEK_SET);
if (fwrite(pChunkData, 1, ullNewChunkSize, io.hWrite) != ullNewChunkSize) {
throw Exception("Writing Chunk data (from RAM) failed");
}
#endif // POSIX
} else {
// move chunk data from the end of the file to the appropriate position
int8_t* pCopyBuffer = new int8_t[4096];
file_offset_t ullToMove = (ullNewChunkSize < ullCurrentChunkSize) ? ullNewChunkSize : ullCurrentChunkSize;
#if defined(WIN32)
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured
#else
int iBytesMoved = 1;
#endif
for (file_offset_t ullOffset = 0; ullToMove > 0 && iBytesMoved > 0; ullOffset += iBytesMoved, ullToMove -= iBytesMoved) {
iBytesMoved = (ullToMove < 4096) ? int(ullToMove) : 4096;
#if POSIX
lseek(io.hRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET);
iBytesMoved = (int) read(io.hRead, pCopyBuffer, (size_t) iBytesMoved);
lseek(io.hWrite, ullWritePos + ullOffset, SEEK_SET);
iBytesMoved = (int) write(io.hWrite, pCopyBuffer, (size_t) iBytesMoved);
#elif defined(WIN32)
LARGE_INTEGER liFilePos;
liFilePos.QuadPart = ullStartPos + ullCurrentDataOffset + ullOffset;
SetFilePointerEx(io.hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
ReadFile(io.hRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
liFilePos.QuadPart = ullWritePos + ullOffset;
SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
WriteFile(io.hWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
#else
fseeko(io.hRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET);
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, io.hRead);
fseeko(io.hWrite, ullWritePos + ullOffset, SEEK_SET);
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, io.hWrite);
#endif
}
delete[] pCopyBuffer;
if (iBytesMoved < 0) throw Exception("Writing Chunk data (from file) failed");
}
// update this chunk's header
ullCurrentChunkSize = ullNewChunkSize;
WriteHeader(ullOriginalPos);
// update chunk's position pointers
ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
Chunk::__resetPos();
if (pProgress && pProgress->__private) {
_FileProgress* fileProgress = (_FileProgress*) pProgress->__private;
fileProgress->advance(pProgress, ullNewChunkSize +
CHUNK_HEADER_SIZE(pFile->FileOffsetSize));
}
// add pad byte if needed
if ((ullStartPos + ullNewChunkSize) % 2 != 0) {
const char cPadByte = 0;
#if POSIX
lseek(io.hWrite, ullStartPos + ullNewChunkSize, SEEK_SET);
write(io.hWrite, &cPadByte, 1);
#elif defined(WIN32)
LARGE_INTEGER liFilePos;
liFilePos.QuadPart = ullStartPos + ullNewChunkSize;
SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
DWORD dwBytesWritten;
WriteFile(io.hWrite, &cPadByte, 1, &dwBytesWritten, NULL);
#else
fseeko(io.hWrite, ullStartPos + ullNewChunkSize, SEEK_SET);
fwrite(&cPadByte, 1, 1, io.hWrite);
#endif
return ullStartPos + ullNewChunkSize + 1;
}
return ullStartPos + ullNewChunkSize;
}
void Chunk::__resetPos() {
std::lock_guard<std::mutex> lock(chunkPos.mutex);
chunkPos.ullPos = 0;
chunkPos.byThread.clear();
}
// *************** List ***************
// *
List::List(File* pFile) : Chunk(pFile) {
#if DEBUG_RIFF
std::cout << "List::List(File* pFile)" << std::endl;
#endif // DEBUG_RIFF
pSubChunks = NULL;
pSubChunksMap = NULL;
}
List::List(File* pFile, file_offset_t StartPos, List* Parent)
: Chunk(pFile, StartPos, Parent) {
#if DEBUG_RIFF
std::cout << "List::List(File*,file_offset_t,List*)" << std::endl;
#endif // DEBUG_RIFF
pSubChunks = NULL;
pSubChunksMap = NULL;
ReadHeader(StartPos);
ullStartPos = StartPos + LIST_HEADER_SIZE(pFile->FileOffsetSize);
}
List::List(File* pFile, List* pParent, uint32_t uiListID)
: Chunk(pFile, pParent, CHUNK_ID_LIST, 0) {
pSubChunks = NULL;
pSubChunksMap = NULL;
ListType = uiListID;
}
List::~List() {
#if DEBUG_RIFF
std::cout << "List::~List()" << std::endl;
#endif // DEBUG_RIFF
DeleteChunkList();
}
void List::DeleteChunkList() {
if (pSubChunks) {
ChunkList::iterator iter = pSubChunks->begin();
ChunkList::iterator end = pSubChunks->end();
while (iter != end) {
delete *iter;
iter++;
}
delete pSubChunks;
pSubChunks = NULL;
}
if (pSubChunksMap) {
delete pSubChunksMap;
pSubChunksMap = NULL;
}
}
Chunk* List::GetSubChunkAt(size_t pos) {
if (!pSubChunks) LoadSubChunks();
if (pos >= pSubChunks->size()) return NULL;
return (*pSubChunks)[pos];
}
Chunk* List::GetSubChunk(uint32_t ChunkID) {
#if DEBUG_RIFF
std::cout << "List::GetSubChunk(uint32_t)" << std::endl;
#endif // DEBUG_RIFF
if (!pSubChunksMap) LoadSubChunks();
return (*pSubChunksMap)[ChunkID];
}
List* List::GetSubListAt(size_t pos) {
if (!pSubChunks) LoadSubChunks();
if (pos >= pSubChunks->size()) return NULL;
for (size_t iCk = 0, iLst = 0; iCk < pSubChunks->size(); ++iCk) {
Chunk* pChunk = (*pSubChunks)[iCk];
if (pChunk->GetChunkID() != CHUNK_ID_LIST) continue;
if (iLst == pos) return (List*) pChunk;
++iLst;
}
return NULL;
}
List* List::GetSubList(uint32_t ListType) {
#if DEBUG_RIFF
std::cout << "List::GetSubList(uint32_t)" << std::endl;
#endif // DEBUG_RIFF
if (!pSubChunks) LoadSubChunks();
ChunkList::iterator iter = pSubChunks->begin();
ChunkList::iterator end = pSubChunks->end();
while (iter != end) {
if ((*iter)->GetChunkID() == CHUNK_ID_LIST) {
List* l = (List*) *iter;
if (l->GetListType() == ListType) return l;
}
iter++;
}
return NULL;
}
Chunk* List::GetFirstSubChunk() {
#if DEBUG_RIFF
std::cout << "List::GetFirstSubChunk()" << std::endl;
#endif // DEBUG_RIFF
if (!pSubChunks) LoadSubChunks();
ChunksIterator = pSubChunks->begin();
return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
}
Chunk* List::GetNextSubChunk() {
#if DEBUG_RIFF
std::cout << "List::GetNextSubChunk()" << std::endl;
#endif // DEBUG_RIFF
if (!pSubChunks) return NULL;
ChunksIterator++;
return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
}
List* List::GetFirstSubList() {
#if DEBUG_RIFF
std::cout << "List::GetFirstSubList()" << std::endl;
#endif // DEBUG_RIFF
if (!pSubChunks) LoadSubChunks();
ListIterator = pSubChunks->begin();
ChunkList::iterator end = pSubChunks->end();
while (ListIterator != end) {
if ((*ListIterator)->GetChunkID() == CHUNK_ID_LIST) return (List*) *ListIterator;
ListIterator++;
}
return NULL;
}
List* List::GetNextSubList() {
#if DEBUG_RIFF
std::cout << "List::GetNextSubList()" << std::endl;
#endif // DEBUG_RIFF
if (!pSubChunks) return NULL;
if (ListIterator == pSubChunks->end()) return NULL;
ListIterator++;
ChunkList::iterator end = pSubChunks->end();
while (ListIterator != end) {
if ((*ListIterator)->GetChunkID() == CHUNK_ID_LIST) return (List*) *ListIterator;
ListIterator++;
}
return NULL;
}
size_t List::CountSubChunks() {
if (!pSubChunks) LoadSubChunks();
return pSubChunks->size();
}
size_t List::CountSubChunks(uint32_t ChunkID) {
size_t result = 0;
if (!pSubChunks) LoadSubChunks();
ChunkList::iterator iter = pSubChunks->begin();
ChunkList::iterator end = pSubChunks->end();
while (iter != end) {
if ((*iter)->GetChunkID() == ChunkID) {
result++;
}
iter++;
}
return result;
}
size_t List::CountSubLists() {
return CountSubChunks(CHUNK_ID_LIST);
}
size_t List::CountSubLists(uint32_t ListType) {
size_t result = 0;
if (!pSubChunks) LoadSubChunks();
ChunkList::iterator iter = pSubChunks->begin();
ChunkList::iterator end = pSubChunks->end();
while (iter != end) {
if ((*iter)->GetChunkID() == CHUNK_ID_LIST) {
List* l = (List*) *iter;
if (l->GetListType() == ListType) result++;
}
iter++;
}
return result;
}
Chunk* List::AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize) {
if (ullBodySize == 0) throw Exception("Chunk body size must be at least 1 byte");
if (!pSubChunks) LoadSubChunks();
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0);
pSubChunks->push_back(pNewChunk);
(*pSubChunksMap)[uiChunkID] = pNewChunk;
pNewChunk->Resize(ullBodySize);
ullNewChunkSize += CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
pFile->nDataChunkCount++;
return pNewChunk;
}
void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) {
if (!pSubChunks) LoadSubChunks();
for (size_t i = 0; i < pSubChunks->size(); ++i) {
if ((*pSubChunks)[i] == pSrc) {
pSubChunks->erase(pSubChunks->begin() + i);
ChunkList::iterator iter =
find(pSubChunks->begin(), pSubChunks->end(), pDst);
pSubChunks->insert(iter, pSrc);
return;
}
}
}
void List::MoveSubChunk(Chunk* pSrc, List* pNewParent) {
if (pNewParent == this || !pNewParent) return;
if (!pSubChunks) LoadSubChunks();
if (!pNewParent->pSubChunks) pNewParent->LoadSubChunks();
ChunkList::iterator iter =
find(pSubChunks->begin(), pSubChunks->end(), pSrc);
if (iter == pSubChunks->end()) return;
pSubChunks->erase(iter);
pNewParent->pSubChunks->push_back(pSrc);
// update chunk id map of this List
if ((*pSubChunksMap)[pSrc->GetChunkID()] == pSrc) {
pSubChunksMap->erase(pSrc->GetChunkID());
// try to find another chunk of the same chunk ID
ChunkList::iterator iter = pSubChunks->begin();
ChunkList::iterator end = pSubChunks->end();
for (; iter != end; ++iter) {
if ((*iter)->GetChunkID() == pSrc->GetChunkID()) {
(*pSubChunksMap)[pSrc->GetChunkID()] = *iter;
break; // we're done, stop search
}
}
}
// update chunk id map of other list
if (!(*pNewParent->pSubChunksMap)[pSrc->GetChunkID()])
(*pNewParent->pSubChunksMap)[pSrc->GetChunkID()] = pSrc;
}
List* List::AddSubList(uint32_t uiListType) {
if (!pSubChunks) LoadSubChunks();
List* pNewListChunk = new List(pFile, this, uiListType);
pSubChunks->push_back(pNewListChunk);
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk;
ullNewChunkSize += LIST_HEADER_SIZE(pFile->FileOffsetSize);
pFile->nListChunkCount++;
return pNewListChunk;
}
void List::DeleteSubChunk(Chunk* pSubChunk) {
if (!pSubChunks) LoadSubChunks();
ChunkList::iterator iter =
find(pSubChunks->begin(), pSubChunks->end(), pSubChunk);
if (iter == pSubChunks->end()) return;
pSubChunks->erase(iter);
if (dynamic_cast<List*>(pSubChunk))
pFile->nListChunkCount--;
else
pFile->nDataChunkCount--;
if ((*pSubChunksMap)[pSubChunk->GetChunkID()] == pSubChunk) {
pSubChunksMap->erase(pSubChunk->GetChunkID());
// try to find another chunk of the same chunk ID
ChunkList::iterator iter = pSubChunks->begin();
ChunkList::iterator end = pSubChunks->end();
for (; iter != end; ++iter) {
if ((*iter)->GetChunkID() == pSubChunk->GetChunkID()) {
(*pSubChunksMap)[pSubChunk->GetChunkID()] = *iter;
break; // we're done, stop search
}
}
}
delete pSubChunk;
}
file_offset_t List::RequiredPhysicalSize(int fileOffsetSize) {
if (!pSubChunks) LoadSubChunks();
file_offset_t size = LIST_HEADER_SIZE(fileOffsetSize);
ChunkList::iterator iter = pSubChunks->begin();
ChunkList::iterator end = pSubChunks->end();
for (; iter != end; ++iter)
size += (*iter)->RequiredPhysicalSize(fileOffsetSize);
return size;
}
void List::ReadHeader(file_offset_t filePos) {
#if DEBUG_RIFF
std::cout << "List::Readheader(file_offset_t) ";
#endif // DEBUG_RIFF
Chunk::ReadHeader(filePos);
if (ullCurrentChunkSize < 4) return;
ullNewChunkSize = ullCurrentChunkSize -= 4;
const File::Handle hRead = pFile->FileHandle();
#if POSIX
lseek(hRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET);
read(hRead, &ListType, 4);
#elif defined(WIN32)
LARGE_INTEGER liFilePos;
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
DWORD dwBytesRead;
ReadFile(hRead, &ListType, 4, &dwBytesRead, NULL);
#else
fseeko(hRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET);
fread(&ListType, 4, 1, hRead);
#endif // POSIX
#if DEBUG_RIFF
std::cout << "listType=" << convertToString(ListType) << std::endl;
#endif // DEBUG_RIFF
if (!pFile->bEndianNative) {
//swapBytes_32(&ListType);
}
}
void List::WriteHeader(file_offset_t filePos) {
// the four list type bytes officially belong the chunk's body in the RIFF format
ullNewChunkSize += 4;
Chunk::WriteHeader(filePos);
ullNewChunkSize -= 4; // just revert the +4 incrementation
const File::Handle hWrite = pFile->FileWriteHandle();
#if POSIX
lseek(hWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET);
write(hWrite, &ListType, 4);
#elif defined(WIN32)
LARGE_INTEGER liFilePos;
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
DWORD dwBytesWritten;
WriteFile(hWrite, &ListType, 4, &dwBytesWritten, NULL);
#else
fseeko(hWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET);
fwrite(&ListType, 4, 1, hWrite);
#endif // POSIX
}
void List::LoadSubChunks(progress_t* pProgress) {
#if DEBUG_RIFF
std::cout << "List::LoadSubChunks()";
#endif // DEBUG_RIFF
if (!pSubChunks) {
pSubChunks = new ChunkList();
pSubChunksMap = new ChunkMap();
const File::Handle hRead = pFile->FileHandle();
if (!_isValidHandle(hRead)) return;
const file_offset_t ullOriginalPos = GetPos();
SetPos(0); // jump to beginning of list chunk body
while (RemainingBytes() >= CHUNK_HEADER_SIZE(pFile->FileOffsetSize)) {
Chunk* ck;
uint32_t ckid;
// return value check is required here to prevent a potential
// garbage data use of 'ckid' below in case Read() failed
if (Read(&ckid, 4, 1) != 4)
throw Exception("LoadSubChunks(): Failed reading RIFF chunk ID");
#if DEBUG_RIFF
std::cout << " ckid=" << convertToString(ckid) << std::endl;
#endif // DEBUG_RIFF
const file_offset_t pos = GetPos();
if (ckid == CHUNK_ID_LIST) {
ck = new RIFF::List(pFile, ullStartPos + pos - 4, this);
SetPos(ck->GetSize() + LIST_HEADER_SIZE(pFile->FileOffsetSize) - 4, RIFF::stream_curpos);
pFile->nListChunkCount++;
}
else { // simple chunk
ck = new RIFF::Chunk(pFile, ullStartPos + pos - 4, this);
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE(pFile->FileOffsetSize) - 4, RIFF::stream_curpos);
pFile->nDataChunkCount++;
}
pSubChunks->push_back(ck);
(*pSubChunksMap)[ckid] = ck;
if (GetPos() % 2 != 0) SetPos(1, RIFF::stream_curpos); // jump over pad byte
}
SetPos(ullOriginalPos); // restore position before this call
}
if (pProgress)
__notify_progress(pProgress, 1.0); // notify done
}
void List::LoadSubChunksRecursively(progress_t* pProgress) {
const bool alreadyLoaded = pSubChunks;
const int n = (int) CountSubLists();
int i = 0;
for (List* pList = GetSubListAt(i); pList; pList = GetSubListAt(++i)) {
if (pProgress) {
// divide local progress into subprogress
progress_t subprogress;
__divide_progress(pProgress, &subprogress, n, i);
// do the actual work
pList->LoadSubChunksRecursively(&subprogress);
} else
pList->LoadSubChunksRecursively(NULL);
}
if (pProgress && !alreadyLoaded)
__notify_progress(pProgress, 1.0); // notify done
}
file_offset_t List::WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress) {
const file_offset_t ullOriginalPos = ullWritePos;
ullWritePos += LIST_HEADER_SIZE(pFile->FileOffsetSize);
if (pFile->GetMode() != stream_mode_read_write)
throw Exception("Cannot write list chunk, file has to be opened in read+write mode");
// write all subchunks (including sub list chunks) recursively
if (pSubChunks) {
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) {
ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, pProgress);
}
}
// update this list chunk's header
ullCurrentChunkSize = ullNewChunkSize = ullWritePos - ullOriginalPos - LIST_HEADER_SIZE(pFile->FileOffsetSize);
WriteHeader(ullOriginalPos);
// offset of this list chunk in new written file may have changed
ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize);
if (pProgress && pProgress->__private) {
_FileProgress* fileProgress = (_FileProgress*) pProgress->__private;
fileProgress->advance(pProgress,
LIST_HEADER_SIZE(pFile->FileOffsetSize));
}
return ullWritePos;
}
void List::__resetPos() {
Chunk::__resetPos();
if (pSubChunks) {
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) {
(*iter)->__resetPos();
}
}
}
String List::GetListTypeString() const {
return convertToString(ListType);
}
// *************** File ***************
// *
File::File(uint32_t FileType)
: List(this), bIsNewFile(true), Layout(layout_standard),
FileOffsetPreference(offset_size_auto)
{
io.isPerThread = false;
io.hRead = io.hWrite = RIFF_INVALID_FILE_HANDLE;
io.Mode = stream_mode_closed;
bEndianNative = true;
ListType = FileType;
FileOffsetSize = 4;
nDataChunkCount = 0;
nListChunkCount = 0;
ullStartPos = RIFF_HEADER_SIZE(FileOffsetSize);
}
File::File(const String& path)
: List(this), Filename(path), bIsNewFile(false), Layout(layout_standard),
FileOffsetPreference(offset_size_auto)
{
#if DEBUG_RIFF
std::cout << "File::File("<<path<<")" << std::endl;
#endif // DEBUG_RIFF
bEndianNative = true;
FileOffsetSize = 4;
nDataChunkCount = 0;
nListChunkCount = 0;
try {
__openExistingFile(path);
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) {
throw RIFF::Exception("Not a RIFF file");
}
}
catch (...) {
Cleanup();
throw;
}
}
File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize)
: List(this), Filename(path), bIsNewFile(false), Layout(layout),
FileOffsetPreference(fileOffsetSize)
{
SetByteOrder(Endian);
if (fileOffsetSize < offset_size_auto || fileOffsetSize > offset_size_64bit)
throw Exception("Invalid RIFF::offset_size_t");
FileOffsetSize = 4;
nDataChunkCount = 0;
nListChunkCount = 0;
try {
__openExistingFile(path, &FileType);
}
catch (...) {
Cleanup();
throw;
}
}
void File::__openExistingFile(const String& path, uint32_t* FileType) {
io.isPerThread = false;
#if POSIX
io.hRead = io.hWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK);
if (!_isValidHandle(io.hRead)) {
String sError = strerror(errno);
throw RIFF::Exception("Can't open \"" + path + "\": " + sError);
}
#elif defined(WIN32)
io.hRead = io.hWrite = CreateFile(
path.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_RANDOM_ACCESS, NULL
);
if (!_isValidHandle(io.hRead)) {
throw RIFF::Exception("Can't open \"" + path + "\"");
}
#else
io.hRead = io.hWrite = fopen(path.c_str(), "rb");
if (!_isValidHandle(io.hRead))
throw RIFF::Exception("Can't open \"" + path + "\"");
#endif // POSIX
io.Mode = stream_mode_read;
// determine RIFF file offset size to be used (in RIFF chunk headers)
// according to the current file offset preference
FileOffsetSize = FileOffsetSizeFor(GetCurrentFileSize());
switch (Layout) {
case layout_standard: // this is a normal RIFF file
ullStartPos = RIFF_HEADER_SIZE(FileOffsetSize);
ReadHeader(0);
if (FileType && ChunkID != *FileType)
throw RIFF::Exception("Invalid file container ID");
break;
case layout_flat: // non-standard RIFF-alike file
ullStartPos = 0;
ullNewChunkSize = ullCurrentChunkSize = GetCurrentFileSize();
if (FileType) {
uint32_t ckid;
if (Read(&ckid, 4, 1) != 4) {
throw RIFF::Exception("Invalid file header ID (premature end of header)");
} else if (ckid != *FileType) {
String s = " (expected '" + convertToString(*FileType) + "' but got '" + convertToString(ckid) + "')";
throw RIFF::Exception("Invalid file header ID" + s);
}
SetPos(0); // reset to first byte of file
}
LoadSubChunks();
break;
}
}
String File::GetFileName() const {
return Filename;
}
void File::SetFileName(const String& path) {
Filename = path;
}
File::HandlePair& File::FileHandlePairUnsafeRef() {
if (io.byThread.empty()) return io;
const std::thread::id tid = std::this_thread::get_id();
const auto it = io.byThread.find(tid);
return (it != io.byThread.end()) ?
it->second :
io.byThread[tid] = {
// designated initializers require C++20, so commented for now
/* .hRead = */ RIFF_INVALID_FILE_HANDLE,
/* .hWrite = */ RIFF_INVALID_FILE_HANDLE,
/* .Mode = */ stream_mode_closed
};
}
File::HandlePair File::FileHandlePair() const {
std::lock_guard<std::mutex> lock(io.mutex);
if (io.byThread.empty()) return io;
const std::thread::id tid = std::this_thread::get_id();
const auto it = io.byThread.find(tid);
return (it != io.byThread.end()) ?
it->second :
io.byThread[tid] = {
// designated initializers require C++20, so commented for now
/* .hRead = */ RIFF_INVALID_FILE_HANDLE,
/* .hWrite = */ RIFF_INVALID_FILE_HANDLE,
/* .Mode = */ stream_mode_closed
};
}
File::Handle File::FileHandle() const {
return FileHandlePair().hRead;
}
File::Handle File::FileWriteHandle() const {
return FileHandlePair().hWrite;
}
stream_mode_t File::GetMode() const {
return FileHandlePair().Mode;
}
layout_t File::GetLayout() const {
return Layout;
}
bool File::SetMode(stream_mode_t NewMode) {
bool bResetPos = false;
bool res = SetModeInternal(NewMode, &bResetPos);
// resetting position must be handled outside above's call to avoid any
// potential dead lock, as SetModeInternal() acquires a lock and
// __resetPos() acquires a lock by itself (not a theoretical issue!)
if (bResetPos)
__resetPos(); // reset read/write position of ALL 'Chunk' objects
return res;
}
bool File::SetModeInternal(stream_mode_t NewMode, bool* pResetPos) {
std::lock_guard<std::mutex> lock(io.mutex);
HandlePair& io = FileHandlePairUnsafeRef();
if (NewMode != io.Mode) {
switch (NewMode) {
case stream_mode_read:
if (_isValidHandle(io.hRead)) _close(io.hRead);
#if POSIX
io.hRead = io.hWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK);
if (!_isValidHandle(io.hRead)) {
String sError = strerror(errno);
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode: " + sError);
}
#elif defined(WIN32)
io.hRead = io.hWrite = CreateFile(
Filename.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_RANDOM_ACCESS,
NULL
);
if (!_isValidHandle(io.hRead)) {
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
}
#else
io.hRead = io.hWrite = fopen(Filename.c_str(), "rb");
if (!_isValidHandle(io.hRead))
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
#endif
*pResetPos = true;
break;
case stream_mode_read_write:
if (_isValidHandle(io.hRead)) _close(io.hRead);
#if POSIX
io.hRead = io.hWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK);
if (!_isValidHandle(io.hRead)) {
io.hRead = io.hWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK);
String sError = strerror(errno);
throw Exception("Could not open file \"" + Filename + "\" in read+write mode: " + sError);
}
#elif defined(WIN32)
io.hRead = io.hWrite = CreateFile(
Filename.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL, OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_RANDOM_ACCESS,
NULL
);
if (!_isValidHandle(io.hRead)) {
io.hRead = io.hWrite = CreateFile(
Filename.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_RANDOM_ACCESS,
NULL
);
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode");
}
#else
io.hRead = io.hWrite = fopen(Filename.c_str(), "r+b");
if (!_isValidHandle(io.hRead)) {
io.hRead = io.hWrite = fopen(Filename.c_str(), "rb");
throw Exception("Could not open file \"" + Filename + "\" in read+write mode");
}
#endif
*pResetPos = true;
break;
case stream_mode_closed:
if (_isValidHandle(io.hRead)) _close(io.hRead);
if (_isValidHandle(io.hWrite)) _close(io.hWrite);
io.hRead = io.hWrite = RIFF_INVALID_FILE_HANDLE;
break;
default:
throw Exception("Unknown file access mode");
}
io.Mode = NewMode;
return true;
}
return false;
}
void File::SetByteOrder(endian_t Endian) {
#if WORDS_BIGENDIAN
bEndianNative = Endian != endian_little;
#else
bEndianNative = Endian != endian_big;
#endif
}
void File::Save(progress_t* pProgress) {
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files)
if (Layout == layout_flat)
throw Exception("Saving a RIFF file with layout_flat is not implemented yet");
const bool bNeedLoadSubChunks = !pSubChunks;
// make sure the RIFF tree is built (from the original file)
if (pProgress) {
// divide progress into subprogress
progress_t subprogress;
__divide_progress(pProgress, &subprogress, 1.f, 0.f, 0.2f); // assume 20% of time
// do the actual work
LoadSubChunksRecursively(&subprogress);
// notify subprogress done
__notify_progress(&subprogress, 1.f);
} else
LoadSubChunksRecursively(NULL);
// reopen file in write mode
SetMode(stream_mode_read_write);
// get the current file size as it is now still physically stored on disk
const file_offset_t workingFileSize = GetCurrentFileSize();
// get the overall file size required to save this file
const file_offset_t newFileSize = GetRequiredFileSize(FileOffsetPreference);
// determine whether this file will yield in a large file (>=4GB) and
// the RIFF file offset size to be used accordingly for all chunks
FileOffsetSize = FileOffsetSizeFor(newFileSize);
const HandlePair io = FileHandlePair();
const Handle hRead = io.hRead;
const Handle hWrite = io.hWrite;
// to be able to save the whole file without loading everything into
// RAM and without having to store the data in a temporary file, we
// enlarge the file with the overall positive file size change,
// then move current data towards the end of the file by the calculated
// positive file size difference and finally update / rewrite the file
// by copying the old data back to the right position at the beginning
// of the file
// if there are positive size changes...
file_offset_t positiveSizeDiff = 0;
if (newFileSize > workingFileSize) {
positiveSizeDiff = newFileSize - workingFileSize;
// divide progress into subprogress
progress_t subprogress;
if (pProgress)
__divide_progress(pProgress, &subprogress, 1.f,
bNeedLoadSubChunks ? 0.2f : 0.f,
bNeedLoadSubChunks ? 0.7f : 0.6f);
// ... we enlarge this file first ...
ResizeFile(newFileSize);
// ... and move current data by the same amount towards end of file.
int8_t* pCopyBuffer = new int8_t[4096];
#if defined(WIN32)
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured
#else
ssize_t iBytesMoved = 1;
#endif
for (file_offset_t ullPos = workingFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) {
iBytesMoved = (ullPos < 4096) ? ullPos : 4096;
ullPos -= iBytesMoved;
#if POSIX
lseek(hRead, ullPos, SEEK_SET);
iBytesMoved = read(hRead, pCopyBuffer, iBytesMoved);
lseek(hWrite, ullPos + positiveSizeDiff, SEEK_SET);
iBytesMoved = write(hWrite, pCopyBuffer, iBytesMoved);
#elif defined(WIN32)
LARGE_INTEGER liFilePos;
liFilePos.QuadPart = ullPos;
SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
ReadFile(hRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
liFilePos.QuadPart = ullPos + positiveSizeDiff;
SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
WriteFile(hWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
#else
fseeko(hRead, ullPos, SEEK_SET);
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hRead);
fseeko(hWrite, ullPos + positiveSizeDiff, SEEK_SET);
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hWrite);
#endif
if (pProgress && !(iNotif % 8) && iBytesMoved > 0)
__notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize));
}
delete[] pCopyBuffer;
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it");
if (pProgress)
__notify_progress(&subprogress, 1.f); // notify subprogress done
}
// rebuild / rewrite complete RIFF tree ...
// divide progress into subprogress
progress_t subprogress;
_FileProgress fileProgress = { 0, newFileSize };
if (pProgress) {
__divide_progress(pProgress, &subprogress, 1.f,
bNeedLoadSubChunks ? 0.7f : 0.6f, 1.f);
subprogress.__private = &fileProgress;
}
// do the actual work
const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, pProgress ? &subprogress : NULL);
const file_offset_t finalActualSize = __GetFileSize(hWrite);
// notify subprogress done
if (pProgress)
__notify_progress(&subprogress, 1.f);
// resize file to the final size
if (finalSize < finalActualSize) ResizeFile(finalSize);
if (pProgress)
__notify_progress(pProgress, 1.0); // notify done
}
void File::Save(const String& path, progress_t* pProgress) {
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files)
if (Layout == layout_flat)
throw Exception("Saving a RIFF file with layout_flat is not implemented yet");
const bool bNeedLoadSubChunks = !pSubChunks;
// make sure the RIFF tree is built (from the original file)
if (pProgress) {
// divide progress into subprogress
progress_t subprogress;
__divide_progress(pProgress, &subprogress, 1.f, 0.f, 0.3f); // assume 30% of time
// do the actual work
LoadSubChunksRecursively(&subprogress);
// notify subprogress done
if (bNeedLoadSubChunks)
__notify_progress(&subprogress, 1.f);
} else
LoadSubChunksRecursively(NULL);
if (!bIsNewFile) SetMode(stream_mode_read);
{
std::lock_guard<std::mutex> lock(io.mutex);
HandlePair& io = FileHandlePairUnsafeRef();
// open the other (new) file for writing and truncate it to zero size
#if POSIX
io.hWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
if (!_isValidHandle(io.hWrite)) {
io.hWrite = io.hRead;
String sError = strerror(errno);
throw Exception("Could not open file \"" + path + "\" for writing: " + sError);
}
#elif defined(WIN32)
io.hWrite = CreateFile(
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_RANDOM_ACCESS, NULL
);
if (!_isValidHandle(io.hWrite)) {
io.hWrite = io.hRead;
throw Exception("Could not open file \"" + path + "\" for writing");
}
#else
io.hWrite = fopen(path.c_str(), "w+b");
if (!_isValidHandle(io.hWrite)) {
io.hWrite = io.hRead;
throw Exception("Could not open file \"" + path + "\" for writing");
}
#endif // POSIX
io.Mode = stream_mode_read_write;
}
// get the overall file size required to save this file
const file_offset_t newFileSize = GetRequiredFileSize(FileOffsetPreference);
// determine whether this file will yield in a large file (>=4GB) and
// the RIFF file offset size to be used accordingly for all chunks
FileOffsetSize = FileOffsetSizeFor(newFileSize);
// write complete RIFF tree to the other (new) file
file_offset_t ullTotalSize;
if (pProgress) {
// divide progress into subprogress
progress_t subprogress;
__divide_progress(pProgress, &subprogress, 1.f,
bNeedLoadSubChunks ? 0.3f : 0.f, 1.f);
_FileProgress fileProgress = { 0, newFileSize };
subprogress.__private = &fileProgress;
// do the actual work
ullTotalSize = WriteChunk(0, 0, &subprogress);
// notify subprogress done
__notify_progress(&subprogress, 1.f);
} else
ullTotalSize = WriteChunk(0, 0, NULL);
const file_offset_t ullActualSize = __GetFileSize(FileWriteHandle());
// resize file to the final size (if the file was originally larger)
if (ullActualSize > ullTotalSize) ResizeFile(ullTotalSize);
{
std::lock_guard<std::mutex> lock(io.mutex);
HandlePair& io = FileHandlePairUnsafeRef();
if (_isValidHandle(io.hWrite)) _close(io.hWrite);
io.hWrite = io.hRead;
// associate new file with this File object from now on
Filename = path;
bIsNewFile = false;
io.Mode = (stream_mode_t) -1; // Just set it to an undefined mode ...
}
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles.
if (pProgress)
__notify_progress(pProgress, 1.0); // notify done
}
void File::ResizeFile(file_offset_t ullNewSize) {
const Handle hWrite = FileWriteHandle();
#if POSIX
if (ftruncate(hWrite, ullNewSize) < 0)
throw Exception("Could not resize file \"" + Filename + "\"");
#elif defined(WIN32)
LARGE_INTEGER liFilePos;
liFilePos.QuadPart = ullNewSize;
if (
!SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN) ||
!SetEndOfFile(hWrite)
) throw Exception("Could not resize file \"" + Filename + "\"");
#else
# error Sorry, this version of libgig only supports POSIX and Windows systems yet.
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)!
#endif
}
File::~File() {
#if DEBUG_RIFF
std::cout << "File::~File()" << std::endl;
#endif // DEBUG_RIFF
Cleanup();
}
bool File::IsNew() const {
return bIsNewFile;
}
void File::Cleanup() {
if (IsIOPerThread()) {
for (auto it = io.byThread.begin(); it != io.byThread.end(); ++it) {
_close(it->second.hRead);
}
} else {
_close(io.hRead);
}
DeleteChunkList();
pFile = NULL;
}
file_offset_t File::GetCurrentFileSize() const {
file_offset_t size = 0;
const Handle hRead = FileHandle();
try {
size = __GetFileSize(hRead);
} catch (...) {
size = 0;
}
return size;
}
file_offset_t File::GetRequiredFileSize() {
return GetRequiredFileSize(FileOffsetPreference);
}
file_offset_t File::GetRequiredFileSize(offset_size_t fileOffsetSize) {
switch (fileOffsetSize) {
case offset_size_auto: {
file_offset_t fileSize = GetRequiredFileSize(offset_size_32bit);
if (fileSize >> 32)
return GetRequiredFileSize(offset_size_64bit);
else
return fileSize;
}
case offset_size_32bit: break;
case offset_size_64bit: break;
default: throw Exception("Internal error: Invalid RIFF::offset_size_t");
}
return RequiredPhysicalSize(FileOffsetSize);
}
int File::FileOffsetSizeFor(file_offset_t fileSize) const {
switch (FileOffsetPreference) {
case offset_size_auto:
return (fileSize >> 32) ? 8 : 4;
case offset_size_32bit:
return 4;
case offset_size_64bit:
return 8;
default:
throw Exception("Internal error: Invalid RIFF::offset_size_t");
}
}
int File::GetFileOffsetSize() const {
return FileOffsetSize;
}
int File::GetRequiredFileOffsetSize() {
return FileOffsetSizeFor(GetCurrentFileSize());
}
bool File::IsIOPerThread() const {
//NOTE: Not caring about atomicity here at all, for three reasons:
// 1. SetIOPerThread() is assumed to be called only once for the entire
// life time of a RIFF::File, usually very early at its lifetime, and
// hence a change to isPerThread should already safely be propagated
// before any other thread would actually read this boolean flag.
// 2. This method is called very frequently, and therefore any
// synchronization techique would hurt runtime efficiency.
// 3. Using even a mutex lock here might easily cause a deadlock due to
// other locks been taken in this .cpp file, i.e. at a higher call
// stack level (and this is the main reason why I removed it here).
return io.isPerThread;
}
void File::SetIOPerThread(bool enable) {
std::lock_guard<std::mutex> lock(io.mutex);
if (!io.byThread.empty() == enable) return;
io.isPerThread = enable;
if (enable) {
const std::thread::id tid = std::this_thread::get_id();
io.byThread[tid] = io;
} else {
// retain an arbitrary handle pair, close all other handle pairs
for (auto it = io.byThread.begin(); it != io.byThread.end(); ++it) {
if (it == io.byThread.begin()) {
io.hRead = it->second.hRead;
io.hWrite = it->second.hWrite;
} else {
_close(it->second.hRead);
_close(it->second.hWrite);
}
}
io.byThread.clear();
}
}
size_t File::totalDataChunkCount() const {
return nDataChunkCount;
}
size_t File::totalListChunkCount() const {
return nListChunkCount;
}
size_t File::totalChunkCount() const {
return nDataChunkCount + nListChunkCount;
}
#if POSIX
file_offset_t File::__GetFileSize(int hFile) const {
struct stat filestat;
if (fstat(hFile, &filestat) == -1)
throw Exception("POSIX FS error: could not determine file size");
return filestat.st_size;
}
#elif defined(WIN32)
file_offset_t File::__GetFileSize(HANDLE hFile) const {
LARGE_INTEGER size;
if (!GetFileSizeEx(hFile, &size))
throw Exception("Windows FS error: could not determine file size");
return size.QuadPart;
}
#else // standard C functions
file_offset_t File::__GetFileSize(FILE* hFile) const {
off_t curpos = ftello(hFile);
if (fseeko(hFile, 0, SEEK_END) == -1)
throw Exception("FS error: could not determine file size");
off_t size = ftello(hFile);
fseeko(hFile, curpos, SEEK_SET);
return size;
}
#endif
// *************** Exception ***************
// *
Exception::Exception() {
}
Exception::Exception(String format, ...) {
va_list arg;
va_start(arg, format);
Message = assemble(format, arg);
va_end(arg);
}
Exception::Exception(String format, va_list arg) {
Message = assemble(format, arg);
}
Exception::~Exception() {
}
void Exception::PrintMessage() const {
std::cout << "RIFF::Exception: " << Message << std::endl;
}
String Exception::assemble(String format, va_list arg) {
char* buf = NULL;
vasprintf(&buf, format.c_str(), arg);
String s = buf;
free(buf);
return s;
}
// *************** functions ***************
// *
String libraryName() {
return PACKAGE;
}
String libraryVersion() {
return VERSION;
}
} // namespace RIFF
RIFF::Chunk
Ordinary RIFF Chunk.
Definition RIFF.h:186
RIFF::Chunk::GetParent
List * GetParent() const
Returns pointer to the chunk's parent list chunk.
Definition RIFF.h:192
RIFF::Chunk::WriteInt16
file_offset_t WriteInt16(int16_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 16 Bit signed integer words from the buffer pointed by pData to the chunk'...
Definition RIFF.cpp:717
RIFF::Chunk::__resetPos
virtual void __resetPos()
Sets Chunk's read/write position to zero.
Definition RIFF.cpp:1186
RIFF::Chunk::Resize
void Resize(file_offset_t NewSize)
Resize chunk.
Definition RIFF.cpp:1058
RIFF::Chunk::ReadInt8
int8_t ReadInt8()
Reads one 8 Bit signed integer word and increments the position within the chunk.
Definition RIFF.cpp:864
RIFF::Chunk::ReadUint32
uint32_t ReadUint32()
Reads one 32 Bit unsigned integer word and increments the position within the chunk.
Definition RIFF.cpp:953
RIFF::Chunk::GetState
stream_state_t GetState() const
Returns the current state of the chunk object.
Definition RIFF.cpp:434
RIFF::Chunk::SetPos
file_offset_t SetPos(file_offset_t Where, stream_whence_t Whence=stream_start)
Sets the position within the chunk body, thus within the data portion of the chunk (in bytes).
Definition RIFF.cpp:365
RIFF::Chunk::GetFilePos
file_offset_t GetFilePos() const
Current, actual offset in file of current chunk data body read/write position.
Definition RIFF.cpp:348
RIFF::Chunk::ReleaseChunkData
void ReleaseChunkData()
Free loaded chunk body from RAM.
Definition RIFF.cpp:1033
RIFF::Chunk::Write
file_offset_t Write(void *pData, file_offset_t WordCount, file_offset_t WordSize)
Writes WordCount number of data words with given WordSize from the buffer pointed by pData.
Definition RIFF.cpp:541
RIFF::Chunk::RemainingBytes
file_offset_t RemainingBytes() const
Returns the number of bytes left to read in the chunk body.
Definition RIFF.cpp:400
RIFF::Chunk::ReadInt16
int16_t ReadInt16()
Reads one 16 Bit signed integer word and increments the position within the chunk.
Definition RIFF.cpp:899
RIFF::Chunk::WriteChunk
virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t *pProgress=NULL)
Write chunk persistently e.g.
Definition RIFF.cpp:1081
RIFF::Chunk::GetPos
file_offset_t GetPos() const
Current read/write position within the chunk data body (starting with 0).
Definition RIFF.cpp:335
RIFF::Chunk::WriteInt32
file_offset_t WriteInt32(int32_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 32 Bit signed integer words from the buffer pointed by pData to the chunk'...
Definition RIFF.cpp:795
RIFF::Chunk::ReadSceptical
file_offset_t ReadSceptical(void *pData, file_offset_t WordCount, file_offset_t WordSize)
Just an internal wrapper for the main Read() method with additional Exception throwing on errors.
Definition RIFF.cpp:599
RIFF::Chunk::RequiredPhysicalSize
virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize)
Returns the actual total size in bytes (including header) of this Chunk if being stored to a file.
Definition RIFF.cpp:415
RIFF::Chunk::Read
file_offset_t Read(void *pData, file_offset_t WordCount, file_offset_t WordSize)
Reads WordCount number of data words with given WordSize and copies it into a buffer pointed by pData...
Definition RIFF.cpp:465
RIFF::Chunk::GetChunkID
uint32_t GetChunkID() const
Chunk ID in unsigned integer representation.
Definition RIFF.h:190
RIFF::Chunk::LoadChunkData
void * LoadChunkData()
Load chunk body into RAM.
Definition RIFF.cpp:984
RIFF::Chunk::GetChunkIDString
String GetChunkIDString() const
Returns the String representation of the chunk's ID (e.g.
Definition RIFF.cpp:312
RIFF::Chunk::ReadUint8
uint8_t ReadUint8()
Reads one 8 Bit unsigned integer word and increments the position within the chunk.
Definition RIFF.cpp:881
RIFF::Chunk::WriteInt8
file_offset_t WriteInt8(int8_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 8 Bit signed integer words from the buffer pointed by pData to the chunk's...
Definition RIFF.cpp:639
RIFF::Chunk::WriteUint16
file_offset_t WriteUint16(uint16_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 16 Bit unsigned integer words from the buffer pointed by pData to the chun...
Definition RIFF.cpp:756
RIFF::Chunk::WriteUint32
file_offset_t WriteUint32(uint32_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 32 Bit unsigned integer words from the buffer pointed by pData to the chun...
Definition RIFF.cpp:852
RIFF::Chunk::ReadString
void ReadString(String &s, int size)
Reads a null-padded string of size characters and copies it into the string s.
Definition RIFF.cpp:830
RIFF::Chunk::ReadUint16
uint16_t ReadUint16()
Reads one 16 Bit unsigned integer word and increments the position within the chunk.
Definition RIFF.cpp:917
RIFF::Chunk::GetSize
file_offset_t GetSize() const
Chunk size in bytes (without header, thus the chunk data body).
Definition RIFF.h:193
RIFF::Chunk::ReadInt32
int32_t ReadInt32()
Reads one 32 Bit signed integer word and increments the position within the chunk.
Definition RIFF.cpp:935
RIFF::Chunk::WriteUint8
file_offset_t WriteUint8(uint8_t *pData, file_offset_t WordCount=1)
Writes WordCount number of 8 Bit unsigned integer words from the buffer pointed by pData to the chunk...
Definition RIFF.cpp:678
RIFF::Exception
Will be thrown whenever an error occurs while handling a RIFF file.
Definition RIFF.h:397
RIFF::File
RIFF File.
Definition RIFF.h:314
RIFF::File::FileWriteHandle
Handle FileWriteHandle() const
Returns the OS dependent file I/O write handle intended to be used by the calling thread.
Definition RIFF.cpp:2056
RIFF::File::FileHandlePair
HandlePair FileHandlePair() const
Returns the OS dependent file I/O read and write handles intended to be used by the calling thread.
Definition RIFF.cpp:2025
RIFF::File::SetMode
bool SetMode(stream_mode_t NewMode)
Change file access mode.
Definition RIFF.cpp:2086
RIFF::File::GetRequiredFileOffsetSize
int GetRequiredFileOffsetSize()
Returns the required size (in bytes) of file offsets stored in the headers of all chunks of this file...
Definition RIFF.cpp:2607
RIFF::File::GetFileOffsetSize
int GetFileOffsetSize() const
Returns the current size (in bytes) of file offsets stored in the headers of all chunks of this file.
Definition RIFF.cpp:2593
RIFF::File::File
File(uint32_t FileType)
Create new RIFF file.
Definition RIFF.cpp:1833
RIFF::File::GetCurrentFileSize
file_offset_t GetCurrentFileSize() const
Returns the current size of this file (in bytes) as it is currently yet stored on disk.
Definition RIFF.cpp:2499
RIFF::File::SetByteOrder
void SetByteOrder(endian_t Endian)
Set the byte order to be used when saving.
Definition RIFF.cpp:2191
RIFF::File::IsNew
bool IsNew() const
Returns true if this file has been created new from scratch and has not been stored to disk yet.
Definition RIFF.cpp:2477
RIFF::File::GetRequiredFileSize
file_offset_t GetRequiredFileSize()
Returns the required size (in bytes) for this RIFF File to be saved to disk.
Definition RIFF.cpp:2529
RIFF::File::totalDataChunkCount
size_t totalDataChunkCount() const
File's total amount of ordinary (data) chunks.
Definition RIFF.cpp:2696
RIFF::File::FileHandle
Handle FileHandle() const
Returns the OS dependent file I/O read handle intended to be used by the calling thread.
Definition RIFF.cpp:2046
RIFF::File::IsIOPerThread
bool IsIOPerThread() const
Whether file streams are independent for each thread.
Definition RIFF.cpp:2637
RIFF::File::totalChunkCount
size_t totalChunkCount() const
File's total amount of data chunks and list chunks.
Definition RIFF.cpp:2724
RIFF::File::GetMode
stream_mode_t GetMode() const
Returns the file I/O mode currently being available for the calling thread for this RIFF file (either...
Definition RIFF.cpp:2066
RIFF::File::totalListChunkCount
size_t totalListChunkCount() const
File's total amount of list chunks.
Definition RIFF.cpp:2708
RIFF::File::Save
virtual void Save(progress_t *pProgress=NULL)
Save changes to same file.
Definition RIFF.cpp:2209
RIFF::File::Layout
layout_t Layout
An ordinary RIFF file is always set to layout_standard.
Definition RIFF.h:367
RIFF::File::Handle
int Handle
OS dependent type serving as file handle / descriptor for OS dependent file I/O operations.
Definition RIFF.h:321
RIFF::File::FileOffsetSize
int FileOffsetSize
Size of file offsets (in bytes) when this file was opened (or saved the last time).
Definition RIFF.h:369
RIFF::File::SetIOPerThread
void SetIOPerThread(bool enable)
Enable/disable file streams being independent for each thread.
Definition RIFF.cpp:2666
RIFF::List
RIFF List Chunk.
Definition RIFF.h:262
RIFF::List::CountSubLists
size_t CountSubLists()
Returns number of sublists within the list.
Definition RIFF.cpp:1449
RIFF::List::GetSubChunk
Chunk * GetSubChunk(uint32_t ChunkID)
Returns subchunk with chunk ID ChunkID within this chunk list.
Definition RIFF.cpp:1273
RIFF::List::RequiredPhysicalSize
virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize)
Returns the actual total size in bytes (including List chunk header and all subchunks) of this List C...
Definition RIFF.cpp:1625
RIFF::List::MoveSubChunk
void MoveSubChunk(Chunk *pSrc, Chunk *pDst)
Moves a sub chunk witin this list.
Definition RIFF.cpp:1511
RIFF::List::GetFirstSubList
List * GetFirstSubList()
Returns the first sublist within the list (that is a subchunk with chunk ID "LIST").
Definition RIFF.cpp:1381
RIFF::List::AddSubList
List * AddSubList(uint32_t uiListType)
Creates a new list sub chunk.
Definition RIFF.cpp:1568
RIFF::List::GetFirstSubChunk
Chunk * GetFirstSubChunk()
Returns the first subchunk within the list (which may be an ordinary chunk as well as a list chunk).
Definition RIFF.cpp:1342
RIFF::List::WriteChunk
virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t *pProgress=NULL)
Write list chunk persistently e.g.
Definition RIFF.cpp:1768
RIFF::List::DeleteSubChunk
void DeleteSubChunk(Chunk *pSubChunk)
Removes a sub chunk.
Definition RIFF.cpp:1591
RIFF::List::GetSubListAt
List * GetSubListAt(size_t pos)
Returns sublist chunk with list type ListType at supplied pos position among all subchunks of type Li...
Definition RIFF.cpp:1291
RIFF::List::CountSubChunks
size_t CountSubChunks()
Returns number of subchunks within the list (including list chunks).
Definition RIFF.cpp:1423
RIFF::List::GetSubList
List * GetSubList(uint32_t ListType)
Returns sublist chunk with list type ListType within this chunk list.
Definition RIFF.cpp:1314
RIFF::List::GetNextSubChunk
Chunk * GetNextSubChunk()
Returns the next subchunk within the list (which may be an ordinary chunk as well as a list chunk).
Definition RIFF.cpp:1361
RIFF::List::GetListType
uint32_t GetListType() const
Returns unsigned integer representation of the list's ID.
Definition RIFF.h:266
RIFF::List::GetListTypeString
String GetListTypeString() const
Returns string representation of the lists's id.
Definition RIFF.cpp:1810
RIFF::List::GetNextSubList
List * GetNextSubList()
Returns the next sublist (that is a subchunk with chunk ID "LIST") within the list.
Definition RIFF.cpp:1405
RIFF::List::AddSubChunk
Chunk * AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize)
Creates a new sub chunk.
Definition RIFF.cpp:1485
RIFF::List::__resetPos
virtual void __resetPos()
Sets List Chunk's read/write position to zero and causes all sub chunks to do the same.
Definition RIFF.cpp:1798
RIFF::List::GetSubChunkAt
Chunk * GetSubChunkAt(size_t pos)
Returns subchunk at supplied pos position within this chunk list.
Definition RIFF.cpp:1256
RIFF
RIFF specific classes and definitions.
Definition RIFF.h:101
RIFF::libraryVersion
String libraryVersion()
Returns version of this C++ library.
Definition RIFF.cpp:2803
RIFF::stream_whence_t
stream_whence_t
File stream position dependent to these relations.
Definition RIFF.h:128
RIFF::stream_state_t
stream_state_t
Current state of the file stream.
Definition RIFF.h:121
RIFF::libraryName
String libraryName()
Returns the name of this C++ library.
Definition RIFF.cpp:2795
RIFF::offset_size_t
offset_size_t
Size of RIFF file offsets used in all RIFF chunks' headers.
Definition RIFF.h:149
RIFF::offset_size_64bit
@ offset_size_64bit
Always use 64 bit offsets (even for files smaller than 4 GB).
Definition RIFF.h:152
RIFF::offset_size_auto
@ offset_size_auto
Use 32 bit offsets for files smaller than 4 GB, use 64 bit offsets for files equal or larger than 4 G...
Definition RIFF.h:150
RIFF::offset_size_32bit
@ offset_size_32bit
Always use 32 bit offsets (even for files larger than 4 GB).
Definition RIFF.h:151
RIFF::stream_mode_t
stream_mode_t
Whether file stream is open in read or in read/write mode.
Definition RIFF.h:114
RIFF::layout_t
layout_t
General RIFF chunk structure of a RIFF file.
Definition RIFF.h:143
RIFF::layout_standard
@ layout_standard
Standard RIFF file layout: First chunk in file is a List chunk which contains all other chunks and th...
Definition RIFF.h:144
RIFF::layout_flat
@ layout_flat
Not a "real" RIFF file: First chunk in file is an ordinary data chunk, not a List chunk,...
Definition RIFF.h:145
RIFF::endian_t
endian_t
Alignment of data bytes in memory (system dependant).
Definition RIFF.h:136
RIFF::file_offset_t
uint64_t file_offset_t
Type used by libgig for handling file positioning during file I/O tasks.
Definition RIFF.h:111
RIFF::progress_t
Used for indicating the progress of a certain task.
Definition RIFF.h:167
RIFF::progress_t::__range_min
float __range_min
Only for internal usage, do not modify!
Definition RIFF.h:172
RIFF::progress_t::__private
void * __private
Only for internal usage, do not modify!
Definition RIFF.h:174
RIFF::progress_t::callback
void(* callback)(progress_t *)
Callback function pointer which has to be assigned to a function for progress notification.
Definition RIFF.h:168
RIFF::progress_t::custom
void * custom
This pointer can be used for arbitrary data.
Definition RIFF.h:170
RIFF::progress_t::activity
const char * activity
Text which describes current ongoing action (e.g. to be displayed along a progress bar).
Definition RIFF.h:171
RIFF::progress_t::subdivide
std::vector< progress_t > subdivide(int iSubtasks) const
Divides this progress task into the requested amount of equal weighted sub-progress tasks and returns...
Definition RIFF.cpp:102
RIFF::progress_t::__range_max
float __range_max
Only for internal usage, do not modify!
Definition RIFF.h:173