libgig  4.4.1
RIFF.cpp
1 /***************************************************************************
2  * *
3  * libgig - C++ cross-platform Gigasampler format file access library *
4  * *
5  * Copyright (C) 2003-2024 by Christian Schoenebeck *
6  * <cuse@users.sourceforge.net> *
7  * *
8  * This library is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  ***************************************************************************/
23 
24 #include <algorithm>
25 #include <set>
26 #include <string.h>
27 
28 #include "RIFF.h"
29 
30 #include "helper.h"
31 
32 #if POSIX
33 # include <errno.h>
34 #endif
35 
36 namespace RIFF {
37 
38 // *************** Internal functions **************
39 // *
40 
42  static String __resolveChunkPath(Chunk* pCk) {
43  String sPath;
44  for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) {
45  if (pChunk->GetChunkID() == CHUNK_ID_LIST) {
46  List* pList = (List*) pChunk;
47  sPath = "->'" + pList->GetListTypeString() + "'" + sPath;
48  } else {
49  sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath;
50  }
51  }
52  return sPath;
53  }
54 
55  inline static bool _isValidHandle(File::Handle handle) {
56  #if defined(WIN32)
57  return handle != INVALID_HANDLE_VALUE;
58  #else
59  return handle;
60  #endif
61  }
62 
63  inline static void _close(File::Handle handle) {
64  if (!_isValidHandle(handle)) return;
65  #if POSIX
66  close(handle);
67  #elif defined(WIN32)
68  CloseHandle(handle);
69  #else
70  fclose(handle);
71  #endif
72  }
73 
74 
75 
76 // *************** progress_t ***************
77 // *
78 
79  progress_t::progress_t() {
80  callback = NULL;
81  custom = NULL;
82  __range_min = 0.0f;
83  __range_max = 1.0f;
84  }
85 
93  std::vector<progress_t> progress_t::subdivide(int iSubtasks) {
94  std::vector<progress_t> v;
95  for (int i = 0; i < iSubtasks; ++i) {
96  progress_t p;
97  __divide_progress(this, &p, iSubtasks, i);
98  v.push_back(p);
99  }
100  return v;
101  }
102 
125  std::vector<progress_t> progress_t::subdivide(std::vector<float> vSubTaskPortions) {
126  float fTotal = 0.f; // usually 1.0, but we sum the portions up below to be sure
127  for (int i = 0; i < vSubTaskPortions.size(); ++i)
128  fTotal += vSubTaskPortions[i];
129 
130  float fLow = 0.f, fHigh = 0.f;
131  std::vector<progress_t> v;
132  for (int i = 0; i < vSubTaskPortions.size(); ++i) {
133  fLow = fHigh;
134  fHigh = vSubTaskPortions[i];
135  progress_t p;
136  __divide_progress(this, &p, fTotal, fLow, fHigh);
137  v.push_back(p);
138  }
139  return v;
140  }
141 
142 
143 
144 // *************** Chunk **************
145 // *
146 
147  Chunk::Chunk(File* pFile) {
148  #if DEBUG_RIFF
149  std::cout << "Chunk::Chunk(File* pFile)" << std::endl;
150  #endif // DEBUG_RIFF
151  chunkPos.ullPos = 0;
152  pParent = NULL;
153  pChunkData = NULL;
154  ullCurrentChunkSize = 0;
155  ullNewChunkSize = 0;
156  ullChunkDataSize = 0;
157  ChunkID = CHUNK_ID_RIFF;
158  this->pFile = pFile;
159  }
160 
161  Chunk::Chunk(File* pFile, file_offset_t StartPos, List* Parent) {
162  #if DEBUG_RIFF
163  std::cout << "Chunk::Chunk(File*,file_offset_t,List*),StartPos=" << StartPos << std::endl;
164  #endif // DEBUG_RIFF
165  this->pFile = pFile;
166  ullStartPos = StartPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
167  pParent = Parent;
168  chunkPos.ullPos = 0;
169  pChunkData = NULL;
170  ullCurrentChunkSize = 0;
171  ullNewChunkSize = 0;
172  ullChunkDataSize = 0;
173  ReadHeader(StartPos);
174  }
175 
176  Chunk::Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize) {
177  this->pFile = pFile;
178  ullStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk
179  this->pParent = pParent;
180  chunkPos.ullPos = 0;
181  pChunkData = NULL;
182  ChunkID = uiChunkID;
183  ullChunkDataSize = 0;
184  ullCurrentChunkSize = 0;
185  ullNewChunkSize = ullBodySize;
186  }
187 
188  Chunk::~Chunk() {
189  if (pChunkData) delete[] pChunkData;
190  }
191 
192  void Chunk::ReadHeader(file_offset_t filePos) {
193  #if DEBUG_RIFF
194  std::cout << "Chunk::Readheader(" << filePos << ") ";
195  #endif // DEBUG_RIFF
196  ChunkID = 0;
197  ullNewChunkSize = ullCurrentChunkSize = 0;
198 
199  const File::Handle hRead = pFile->FileHandle();
200 
201  #if POSIX
202  if (lseek(hRead, filePos, SEEK_SET) != -1) {
203  read(hRead, &ChunkID, 4);
204  read(hRead, &ullCurrentChunkSize, pFile->FileOffsetSize);
205  #elif defined(WIN32)
206  LARGE_INTEGER liFilePos;
207  liFilePos.QuadPart = filePos;
208  if (SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) {
209  DWORD dwBytesRead;
210  ReadFile(hRead, &ChunkID, 4, &dwBytesRead, NULL);
211  ReadFile(hRead, &ullCurrentChunkSize, pFile->FileOffsetSize, &dwBytesRead, NULL);
212  #else
213  if (!fseeko(hRead, filePos, SEEK_SET)) {
214  fread(&ChunkID, 4, 1, hRead);
215  fread(&ullCurrentChunkSize, pFile->FileOffsetSize, 1, hRead);
216  #endif // POSIX
217  #if WORDS_BIGENDIAN
218  if (ChunkID == CHUNK_ID_RIFF) {
219  pFile->bEndianNative = false;
220  }
221  #else // little endian
222  if (ChunkID == CHUNK_ID_RIFX) {
223  pFile->bEndianNative = false;
224  ChunkID = CHUNK_ID_RIFF;
225  }
226  #endif // WORDS_BIGENDIAN
227  if (!pFile->bEndianNative) {
228  //swapBytes_32(&ChunkID);
229  if (pFile->FileOffsetSize == 4)
230  swapBytes_32(&ullCurrentChunkSize);
231  else
232  swapBytes_64(&ullCurrentChunkSize);
233  }
234  #if DEBUG_RIFF
235  std::cout << "ckID=" << convertToString(ChunkID) << " ";
236  std::cout << "ckSize=" << ullCurrentChunkSize << " ";
237  std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl;
238  #endif // DEBUG_RIFF
239  ullNewChunkSize = ullCurrentChunkSize;
240  }
241  }
242 
243  void Chunk::WriteHeader(file_offset_t filePos) {
244  uint32_t uiNewChunkID = ChunkID;
245  if (ChunkID == CHUNK_ID_RIFF) {
246  #if WORDS_BIGENDIAN
247  if (pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX;
248  #else // little endian
249  if (!pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX;
250  #endif // WORDS_BIGENDIAN
251  }
252 
253  uint64_t ullNewChunkSize = this->ullNewChunkSize;
254  if (!pFile->bEndianNative) {
255  if (pFile->FileOffsetSize == 4)
256  swapBytes_32(&ullNewChunkSize);
257  else
258  swapBytes_64(&ullNewChunkSize);
259  }
260 
261  const File::Handle hWrite = pFile->FileWriteHandle();
262 
263  #if POSIX
264  if (lseek(hWrite, filePos, SEEK_SET) != -1) {
265  write(hWrite, &uiNewChunkID, 4);
266  write(hWrite, &ullNewChunkSize, pFile->FileOffsetSize);
267  }
268  #elif defined(WIN32)
269  LARGE_INTEGER liFilePos;
270  liFilePos.QuadPart = filePos;
271  if (SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) {
272  DWORD dwBytesWritten;
273  WriteFile(hWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL);
274  WriteFile(hWrite, &ullNewChunkSize, pFile->FileOffsetSize, &dwBytesWritten, NULL);
275  }
276  #else
277  if (!fseeko(hWrite, filePos, SEEK_SET)) {
278  fwrite(&uiNewChunkID, 4, 1, hWrite);
279  fwrite(&ullNewChunkSize, pFile->FileOffsetSize, 1, hWrite);
280  }
281  #endif // POSIX
282  }
283 
288  String Chunk::GetChunkIDString() const {
289  return convertToString(ChunkID);
290  }
291 
300  file_offset_t& Chunk::GetPosUnsafeRef() {
301  if (!pFile->IsIOPerThread()) return chunkPos.ullPos;
302  const std::thread::id tid = std::this_thread::get_id();
303  return chunkPos.byThread[tid];
304  }
305 
312  if (!pFile->IsIOPerThread()) return chunkPos.ullPos;
313  const std::thread::id tid = std::this_thread::get_id();
314  std::lock_guard<std::mutex> lock(chunkPos.mutex);
315  return chunkPos.byThread[tid];
316  }
317 
325  return ullStartPos + GetPos();
326  }
327 
342  #if DEBUG_RIFF
343  std::cout << "Chunk::SetPos(file_offset_t,stream_whence_t)" << std::endl;
344  #endif // DEBUG_RIFF
345  std::lock_guard<std::mutex> lock(chunkPos.mutex);
346  file_offset_t& pos = GetPosUnsafeRef();
347  switch (Whence) {
348  case stream_curpos:
349  pos += Where;
350  break;
351  case stream_end:
352  pos = ullCurrentChunkSize - 1 - Where;
353  break;
354  case stream_backward:
355  pos -= Where;
356  break;
357  case stream_start: default:
358  pos = Where;
359  break;
360  }
361  if (pos > ullCurrentChunkSize) pos = ullCurrentChunkSize;
362  return pos;
363  }
364 
377  #if DEBUG_RIFF
378  std::cout << "Chunk::Remainingbytes()=" << ullCurrentChunkSize - ullPos << std::endl;
379  #endif // DEBUG_RIFF
380  const file_offset_t pos = GetPos();
381  return (ullCurrentChunkSize > pos) ? ullCurrentChunkSize - pos : 0;
382  }
383 
392  return CHUNK_HEADER_SIZE(fileOffsetSize) + // RIFF chunk header
393  ullNewChunkSize + // chunks's actual data body
394  ullNewChunkSize % 2; // optional pad byte
395  }
396 
411  #if DEBUG_RIFF
412  std::cout << "Chunk::GetState()" << std::endl;
413  #endif // DEBUG_RIFF
414 
415  const File::Handle hRead = pFile->FileHandle();
416 
417  if (!_isValidHandle(hRead))
418  return stream_closed;
419 
420  const file_offset_t pos = GetPos();
421  if (pos < ullCurrentChunkSize) return stream_ready;
422  else return stream_end_reached;
423  }
424 
441  file_offset_t Chunk::Read(void* pData, file_offset_t WordCount, file_offset_t WordSize) {
442  #if DEBUG_RIFF
443  std::cout << "Chunk::Read(void*,file_offset_t,file_offset_t)" << std::endl;
444  #endif // DEBUG_RIFF
445  //if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet)
446  const file_offset_t pos = GetPos();
447  if (pos >= ullCurrentChunkSize) return 0;
448  if (pos + WordCount * WordSize >= ullCurrentChunkSize)
449  WordCount = (ullCurrentChunkSize - pos) / WordSize;
450 
451  const File::Handle hRead = pFile->FileHandle();
452 
453  #if POSIX
454  if (lseek(hRead, ullStartPos + pos, SEEK_SET) < 0) return 0;
455  ssize_t readWords = read(hRead, pData, WordCount * WordSize);
456  if (readWords < 1) {
457  #if DEBUG_RIFF
458  std::cerr << "POSIX read() failed: " << strerror(errno) << std::endl << std::flush;
459  #endif // DEBUG_RIFF
460  return 0;
461  }
462  readWords /= WordSize;
463  #elif defined(WIN32)
464  LARGE_INTEGER liFilePos;
465  liFilePos.QuadPart = ullStartPos + pos;
466  if (!SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN))
467  return 0;
468  DWORD readWords;
469  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)
470  if (readWords < 1) return 0;
471  readWords /= WordSize;
472  #else // standard C functions
473  if (fseeko(hRead, ullStartPos + pos, SEEK_SET)) return 0;
474  file_offset_t readWords = fread(pData, WordSize, WordCount, hRead);
475  #endif // POSIX
476  if (!pFile->bEndianNative && WordSize != 1) {
477  switch (WordSize) {
478  case 2:
479  for (file_offset_t iWord = 0; iWord < readWords; iWord++)
480  swapBytes_16((uint16_t*) pData + iWord);
481  break;
482  case 4:
483  for (file_offset_t iWord = 0; iWord < readWords; iWord++)
484  swapBytes_32((uint32_t*) pData + iWord);
485  break;
486  case 8:
487  for (file_offset_t iWord = 0; iWord < readWords; iWord++)
488  swapBytes_64((uint64_t*) pData + iWord);
489  break;
490  default:
491  for (file_offset_t iWord = 0; iWord < readWords; iWord++)
492  swapBytes((uint8_t*) pData + iWord * WordSize, WordSize);
493  break;
494  }
495  }
496  SetPos(readWords * WordSize, stream_curpos);
497  return readWords;
498  }
499 
517  file_offset_t Chunk::Write(void* pData, file_offset_t WordCount, file_offset_t WordSize) {
518  const File::HandlePair io = pFile->FileHandlePair();
519  if (io.Mode != stream_mode_read_write)
520  throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first");
521  const file_offset_t pos = GetPos();
522  if (pos >= ullCurrentChunkSize || pos + WordCount * WordSize > ullCurrentChunkSize)
523  throw Exception("End of chunk reached while trying to write data");
524  if (!pFile->bEndianNative && WordSize != 1) {
525  switch (WordSize) {
526  case 2:
527  for (file_offset_t iWord = 0; iWord < WordCount; iWord++)
528  swapBytes_16((uint16_t*) pData + iWord);
529  break;
530  case 4:
531  for (file_offset_t iWord = 0; iWord < WordCount; iWord++)
532  swapBytes_32((uint32_t*) pData + iWord);
533  break;
534  case 8:
535  for (file_offset_t iWord = 0; iWord < WordCount; iWord++)
536  swapBytes_64((uint64_t*) pData + iWord);
537  break;
538  default:
539  for (file_offset_t iWord = 0; iWord < WordCount; iWord++)
540  swapBytes((uint8_t*) pData + iWord * WordSize, WordSize);
541  break;
542  }
543  }
544  #if POSIX
545  if (lseek(io.hWrite, ullStartPos + pos, SEEK_SET) < 0) {
546  throw Exception("Could not seek to position " + ToString(pos) +
547  " in chunk (" + ToString(ullStartPos + pos) + " in file)");
548  }
549  ssize_t writtenWords = write(io.hWrite, pData, WordCount * WordSize);
550  if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data");
551  writtenWords /= WordSize;
552  #elif defined(WIN32)
553  LARGE_INTEGER liFilePos;
554  liFilePos.QuadPart = ullStartPos + pos;
555  if (!SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) {
556  throw Exception("Could not seek to position " + ToString(pos) +
557  " in chunk (" + ToString(ullStartPos + pos) + " in file)");
558  }
559  DWORD writtenWords;
560  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)
561  if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data");
562  writtenWords /= WordSize;
563  #else // standard C functions
564  if (fseeko(io.hWrite, ullStartPos + pos, SEEK_SET)) {
565  throw Exception("Could not seek to position " + ToString(pos) +
566  " in chunk (" + ToString(ullStartPos + pos) + " in file)");
567  }
568  file_offset_t writtenWords = fwrite(pData, WordSize, WordCount, io.hWrite);
569  #endif // POSIX
570  SetPos(writtenWords * WordSize, stream_curpos);
571  return writtenWords;
572  }
573 
575  file_offset_t Chunk::ReadSceptical(void* pData, file_offset_t WordCount, file_offset_t WordSize) {
576  file_offset_t readWords = Read(pData, WordCount, WordSize);
577  if (readWords != WordCount) throw RIFF::Exception("End of chunk data reached.");
578  return readWords;
579  }
580 
593  file_offset_t Chunk::ReadInt8(int8_t* pData, file_offset_t WordCount) {
594  #if DEBUG_RIFF
595  std::cout << "Chunk::ReadInt8(int8_t*,file_offset_t)" << std::endl;
596  #endif // DEBUG_RIFF
597  return ReadSceptical(pData, WordCount, 1);
598  }
599 
615  file_offset_t Chunk::WriteInt8(int8_t* pData, file_offset_t WordCount) {
616  return Write(pData, WordCount, 1);
617  }
618 
632  file_offset_t Chunk::ReadUint8(uint8_t* pData, file_offset_t WordCount) {
633  #if DEBUG_RIFF
634  std::cout << "Chunk::ReadUint8(uint8_t*,file_offset_t)" << std::endl;
635  #endif // DEBUG_RIFF
636  return ReadSceptical(pData, WordCount, 1);
637  }
638 
654  file_offset_t Chunk::WriteUint8(uint8_t* pData, file_offset_t WordCount) {
655  return Write(pData, WordCount, 1);
656  }
657 
671  file_offset_t Chunk::ReadInt16(int16_t* pData, file_offset_t WordCount) {
672  #if DEBUG_RIFF
673  std::cout << "Chunk::ReadInt16(int16_t*,file_offset_t)" << std::endl;
674  #endif // DEBUG_RIFF
675  return ReadSceptical(pData, WordCount, 2);
676  }
677 
693  file_offset_t Chunk::WriteInt16(int16_t* pData, file_offset_t WordCount) {
694  return Write(pData, WordCount, 2);
695  }
696 
710  file_offset_t Chunk::ReadUint16(uint16_t* pData, file_offset_t WordCount) {
711  #if DEBUG_RIFF
712  std::cout << "Chunk::ReadUint16(uint16_t*,file_offset_t)" << std::endl;
713  #endif // DEBUG_RIFF
714  return ReadSceptical(pData, WordCount, 2);
715  }
716 
732  file_offset_t Chunk::WriteUint16(uint16_t* pData, file_offset_t WordCount) {
733  return Write(pData, WordCount, 2);
734  }
735 
749  file_offset_t Chunk::ReadInt32(int32_t* pData, file_offset_t WordCount) {
750  #if DEBUG_RIFF
751  std::cout << "Chunk::ReadInt32(int32_t*,file_offset_t)" << std::endl;
752  #endif // DEBUG_RIFF
753  return ReadSceptical(pData, WordCount, 4);
754  }
755 
771  file_offset_t Chunk::WriteInt32(int32_t* pData, file_offset_t WordCount) {
772  return Write(pData, WordCount, 4);
773  }
774 
788  file_offset_t Chunk::ReadUint32(uint32_t* pData, file_offset_t WordCount) {
789  #if DEBUG_RIFF
790  std::cout << "Chunk::ReadUint32(uint32_t*,file_offset_t)" << std::endl;
791  #endif // DEBUG_RIFF
792  return ReadSceptical(pData, WordCount, 4);
793  }
794 
806  void Chunk::ReadString(String& s, int size) {
807  char* buf = new char[size];
808  ReadSceptical(buf, 1, size);
809  s.assign(buf, std::find(buf, buf + size, '\0'));
810  delete[] buf;
811  }
812 
828  file_offset_t Chunk::WriteUint32(uint32_t* pData, file_offset_t WordCount) {
829  return Write(pData, WordCount, 4);
830  }
831 
840  int8_t Chunk::ReadInt8() {
841  #if DEBUG_RIFF
842  std::cout << "Chunk::ReadInt8()" << std::endl;
843  #endif // DEBUG_RIFF
844  int8_t word;
845  ReadSceptical(&word,1,1);
846  return word;
847  }
848 
857  uint8_t Chunk::ReadUint8() {
858  #if DEBUG_RIFF
859  std::cout << "Chunk::ReadUint8()" << std::endl;
860  #endif // DEBUG_RIFF
861  uint8_t word;
862  ReadSceptical(&word,1,1);
863  return word;
864  }
865 
875  int16_t Chunk::ReadInt16() {
876  #if DEBUG_RIFF
877  std::cout << "Chunk::ReadInt16()" << std::endl;
878  #endif // DEBUG_RIFF
879  int16_t word;
880  ReadSceptical(&word,1,2);
881  return word;
882  }
883 
893  uint16_t Chunk::ReadUint16() {
894  #if DEBUG_RIFF
895  std::cout << "Chunk::ReadUint16()" << std::endl;
896  #endif // DEBUG_RIFF
897  uint16_t word;
898  ReadSceptical(&word,1,2);
899  return word;
900  }
901 
911  int32_t Chunk::ReadInt32() {
912  #if DEBUG_RIFF
913  std::cout << "Chunk::ReadInt32()" << std::endl;
914  #endif // DEBUG_RIFF
915  int32_t word;
916  ReadSceptical(&word,1,4);
917  return word;
918  }
919 
929  uint32_t Chunk::ReadUint32() {
930  #if DEBUG_RIFF
931  std::cout << "Chunk::ReadUint32()" << std::endl;
932  #endif // DEBUG_RIFF
933  uint32_t word;
934  ReadSceptical(&word,1,4);
935  return word;
936  }
937 
961  if (!pChunkData && pFile->Filename != "" /*&& ulStartPos != 0*/) {
962  File::Handle hRead = pFile->FileHandle();
963  #if POSIX
964  if (lseek(hRead, ullStartPos, SEEK_SET) == -1) return NULL;
965  #elif defined(WIN32)
966  LARGE_INTEGER liFilePos;
967  liFilePos.QuadPart = ullStartPos;
968  if (!SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) return NULL;
969  #else
970  if (fseeko(hRead, ullStartPos, SEEK_SET)) return NULL;
971  #endif // POSIX
972  file_offset_t ullBufferSize = (ullCurrentChunkSize > ullNewChunkSize) ? ullCurrentChunkSize : ullNewChunkSize;
973  pChunkData = new uint8_t[ullBufferSize];
974  if (!pChunkData) return NULL;
975  memset(pChunkData, 0, ullBufferSize);
976  #if POSIX
977  file_offset_t readWords = read(hRead, pChunkData, GetSize());
978  #elif defined(WIN32)
979  DWORD readWords;
980  ReadFile(hRead, pChunkData, GetSize(), &readWords, NULL); //FIXME: won't load chunks larger than 2GB !
981  #else
982  file_offset_t readWords = fread(pChunkData, 1, GetSize(), hRead);
983  #endif // POSIX
984  if (readWords != GetSize()) {
985  delete[] pChunkData;
986  return (pChunkData = NULL);
987  }
988  ullChunkDataSize = ullBufferSize;
989  } else if (ullNewChunkSize > ullChunkDataSize) {
990  uint8_t* pNewBuffer = new uint8_t[ullNewChunkSize];
991  if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(ullNewChunkSize) + " bytes");
992  memset(pNewBuffer, 0 , ullNewChunkSize);
993  if (pChunkData) {
994  memcpy(pNewBuffer, pChunkData, ullChunkDataSize);
995  delete[] pChunkData;
996  }
997  pChunkData = pNewBuffer;
998  ullChunkDataSize = ullNewChunkSize;
999  }
1000  return pChunkData;
1001  }
1002 
1010  if (pChunkData) {
1011  delete[] pChunkData;
1012  pChunkData = NULL;
1013  }
1014  }
1015 
1035  if (NewSize == 0)
1036  throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this));
1037  if ((NewSize >> 48) != 0)
1038  throw Exception("Unrealistic high chunk size detected: " + __resolveChunkPath(this));
1039  if (ullNewChunkSize == NewSize) return;
1040  ullNewChunkSize = NewSize;
1041  }
1042 
1057  file_offset_t Chunk::WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress) {
1058  const file_offset_t ullOriginalPos = ullWritePos;
1059  ullWritePos += CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
1060 
1061  const File::HandlePair io = pFile->FileHandlePair();
1062 
1063  if (io.Mode != stream_mode_read_write)
1064  throw Exception("Cannot write list chunk, file has to be opened in read+write mode");
1065 
1066  // if the whole chunk body was loaded into RAM
1067  if (pChunkData) {
1068  // make sure chunk data buffer in RAM is at least as large as the new chunk size
1069  LoadChunkData();
1070  // write chunk data from RAM persistently to the file
1071  #if POSIX
1072  lseek(io.hWrite, ullWritePos, SEEK_SET);
1073  if (write(io.hWrite, pChunkData, ullNewChunkSize) != ullNewChunkSize) {
1074  throw Exception("Writing Chunk data (from RAM) failed");
1075  }
1076  #elif defined(WIN32)
1077  LARGE_INTEGER liFilePos;
1078  liFilePos.QuadPart = ullWritePos;
1079  SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
1080  DWORD dwBytesWritten;
1081  WriteFile(io.hWrite, pChunkData, ullNewChunkSize, &dwBytesWritten, NULL); //FIXME: won't save chunks larger than 2GB !
1082  if (dwBytesWritten != ullNewChunkSize) {
1083  throw Exception("Writing Chunk data (from RAM) failed");
1084  }
1085  #else
1086  fseeko(io.hWrite, ullWritePos, SEEK_SET);
1087  if (fwrite(pChunkData, 1, ullNewChunkSize, io.hWrite) != ullNewChunkSize) {
1088  throw Exception("Writing Chunk data (from RAM) failed");
1089  }
1090  #endif // POSIX
1091  } else {
1092  // move chunk data from the end of the file to the appropriate position
1093  int8_t* pCopyBuffer = new int8_t[4096];
1094  file_offset_t ullToMove = (ullNewChunkSize < ullCurrentChunkSize) ? ullNewChunkSize : ullCurrentChunkSize;
1095  #if defined(WIN32)
1096  DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured
1097  #else
1098  int iBytesMoved = 1;
1099  #endif
1100  for (file_offset_t ullOffset = 0; ullToMove > 0 && iBytesMoved > 0; ullOffset += iBytesMoved, ullToMove -= iBytesMoved) {
1101  iBytesMoved = (ullToMove < 4096) ? int(ullToMove) : 4096;
1102  #if POSIX
1103  lseek(io.hRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET);
1104  iBytesMoved = (int) read(io.hRead, pCopyBuffer, (size_t) iBytesMoved);
1105  lseek(io.hWrite, ullWritePos + ullOffset, SEEK_SET);
1106  iBytesMoved = (int) write(io.hWrite, pCopyBuffer, (size_t) iBytesMoved);
1107  #elif defined(WIN32)
1108  LARGE_INTEGER liFilePos;
1109  liFilePos.QuadPart = ullStartPos + ullCurrentDataOffset + ullOffset;
1110  SetFilePointerEx(io.hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
1111  ReadFile(io.hRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
1112  liFilePos.QuadPart = ullWritePos + ullOffset;
1113  SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
1114  WriteFile(io.hWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
1115  #else
1116  fseeko(io.hRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET);
1117  iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, io.hRead);
1118  fseeko(io.hWrite, ullWritePos + ullOffset, SEEK_SET);
1119  iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, io.hWrite);
1120  #endif
1121  }
1122  delete[] pCopyBuffer;
1123  if (iBytesMoved < 0) throw Exception("Writing Chunk data (from file) failed");
1124  }
1125 
1126  // update this chunk's header
1127  ullCurrentChunkSize = ullNewChunkSize;
1128  WriteHeader(ullOriginalPos);
1129 
1130  if (pProgress)
1131  __notify_progress(pProgress, 1.0); // notify done
1132 
1133  // update chunk's position pointers
1134  ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
1136 
1137  // add pad byte if needed
1138  if ((ullStartPos + ullNewChunkSize) % 2 != 0) {
1139  const char cPadByte = 0;
1140  #if POSIX
1141  lseek(io.hWrite, ullStartPos + ullNewChunkSize, SEEK_SET);
1142  write(io.hWrite, &cPadByte, 1);
1143  #elif defined(WIN32)
1144  LARGE_INTEGER liFilePos;
1145  liFilePos.QuadPart = ullStartPos + ullNewChunkSize;
1146  SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
1147  DWORD dwBytesWritten;
1148  WriteFile(io.hWrite, &cPadByte, 1, &dwBytesWritten, NULL);
1149  #else
1150  fseeko(io.hWrite, ullStartPos + ullNewChunkSize, SEEK_SET);
1151  fwrite(&cPadByte, 1, 1, io.hWrite);
1152  #endif
1153  return ullStartPos + ullNewChunkSize + 1;
1154  }
1155 
1156  return ullStartPos + ullNewChunkSize;
1157  }
1158 
1160  std::lock_guard<std::mutex> lock(chunkPos.mutex);
1161  chunkPos.ullPos = 0;
1162  chunkPos.byThread.clear();
1163  }
1164 
1165 
1166 
1167 // *************** List ***************
1168 // *
1169 
1170  List::List(File* pFile) : Chunk(pFile) {
1171  #if DEBUG_RIFF
1172  std::cout << "List::List(File* pFile)" << std::endl;
1173  #endif // DEBUG_RIFF
1174  pSubChunks = NULL;
1175  pSubChunksMap = NULL;
1176  }
1177 
1178  List::List(File* pFile, file_offset_t StartPos, List* Parent)
1179  : Chunk(pFile, StartPos, Parent) {
1180  #if DEBUG_RIFF
1181  std::cout << "List::List(File*,file_offset_t,List*)" << std::endl;
1182  #endif // DEBUG_RIFF
1183  pSubChunks = NULL;
1184  pSubChunksMap = NULL;
1185  ReadHeader(StartPos);
1186  ullStartPos = StartPos + LIST_HEADER_SIZE(pFile->FileOffsetSize);
1187  }
1188 
1189  List::List(File* pFile, List* pParent, uint32_t uiListID)
1190  : Chunk(pFile, pParent, CHUNK_ID_LIST, 0) {
1191  pSubChunks = NULL;
1192  pSubChunksMap = NULL;
1193  ListType = uiListID;
1194  }
1195 
1196  List::~List() {
1197  #if DEBUG_RIFF
1198  std::cout << "List::~List()" << std::endl;
1199  #endif // DEBUG_RIFF
1200  DeleteChunkList();
1201  }
1202 
1203  void List::DeleteChunkList() {
1204  if (pSubChunks) {
1205  ChunkList::iterator iter = pSubChunks->begin();
1206  ChunkList::iterator end = pSubChunks->end();
1207  while (iter != end) {
1208  delete *iter;
1209  iter++;
1210  }
1211  delete pSubChunks;
1212  pSubChunks = NULL;
1213  }
1214  if (pSubChunksMap) {
1215  delete pSubChunksMap;
1216  pSubChunksMap = NULL;
1217  }
1218  }
1219 
1230  if (!pSubChunks) LoadSubChunks();
1231  if (pos >= pSubChunks->size()) return NULL;
1232  return (*pSubChunks)[pos];
1233  }
1234 
1246  Chunk* List::GetSubChunk(uint32_t ChunkID) {
1247  #if DEBUG_RIFF
1248  std::cout << "List::GetSubChunk(uint32_t)" << std::endl;
1249  #endif // DEBUG_RIFF
1250  if (!pSubChunksMap) LoadSubChunks();
1251  return (*pSubChunksMap)[ChunkID];
1252  }
1253 
1264  List* List::GetSubListAt(size_t pos) {
1265  if (!pSubChunks) LoadSubChunks();
1266  if (pos >= pSubChunks->size()) return NULL;
1267  for (size_t iCk = 0, iLst = 0; iCk < pSubChunks->size(); ++iCk) {
1268  Chunk* pChunk = (*pSubChunks)[iCk];
1269  if (pChunk->GetChunkID() != CHUNK_ID_LIST) continue;
1270  if (iLst == pos) return (List*) pChunk;
1271  ++iLst;
1272  }
1273  return NULL;
1274  }
1275 
1287  List* List::GetSubList(uint32_t ListType) {
1288  #if DEBUG_RIFF
1289  std::cout << "List::GetSubList(uint32_t)" << std::endl;
1290  #endif // DEBUG_RIFF
1291  if (!pSubChunks) LoadSubChunks();
1292  ChunkList::iterator iter = pSubChunks->begin();
1293  ChunkList::iterator end = pSubChunks->end();
1294  while (iter != end) {
1295  if ((*iter)->GetChunkID() == CHUNK_ID_LIST) {
1296  List* l = (List*) *iter;
1297  if (l->GetListType() == ListType) return l;
1298  }
1299  iter++;
1300  }
1301  return NULL;
1302  }
1303 
1316  #if DEBUG_RIFF
1317  std::cout << "List::GetFirstSubChunk()" << std::endl;
1318  #endif // DEBUG_RIFF
1319  if (!pSubChunks) LoadSubChunks();
1320  ChunksIterator = pSubChunks->begin();
1321  return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
1322  }
1323 
1335  #if DEBUG_RIFF
1336  std::cout << "List::GetNextSubChunk()" << std::endl;
1337  #endif // DEBUG_RIFF
1338  if (!pSubChunks) return NULL;
1339  ChunksIterator++;
1340  return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
1341  }
1342 
1355  #if DEBUG_RIFF
1356  std::cout << "List::GetFirstSubList()" << std::endl;
1357  #endif // DEBUG_RIFF
1358  if (!pSubChunks) LoadSubChunks();
1359  ListIterator = pSubChunks->begin();
1360  ChunkList::iterator end = pSubChunks->end();
1361  while (ListIterator != end) {
1362  if ((*ListIterator)->GetChunkID() == CHUNK_ID_LIST) return (List*) *ListIterator;
1363  ListIterator++;
1364  }
1365  return NULL;
1366  }
1367 
1379  #if DEBUG_RIFF
1380  std::cout << "List::GetNextSubList()" << std::endl;
1381  #endif // DEBUG_RIFF
1382  if (!pSubChunks) return NULL;
1383  if (ListIterator == pSubChunks->end()) return NULL;
1384  ListIterator++;
1385  ChunkList::iterator end = pSubChunks->end();
1386  while (ListIterator != end) {
1387  if ((*ListIterator)->GetChunkID() == CHUNK_ID_LIST) return (List*) *ListIterator;
1388  ListIterator++;
1389  }
1390  return NULL;
1391  }
1392 
1397  if (!pSubChunks) LoadSubChunks();
1398  return pSubChunks->size();
1399  }
1400 
1405  size_t List::CountSubChunks(uint32_t ChunkID) {
1406  size_t result = 0;
1407  if (!pSubChunks) LoadSubChunks();
1408  ChunkList::iterator iter = pSubChunks->begin();
1409  ChunkList::iterator end = pSubChunks->end();
1410  while (iter != end) {
1411  if ((*iter)->GetChunkID() == ChunkID) {
1412  result++;
1413  }
1414  iter++;
1415  }
1416  return result;
1417  }
1418 
1423  return CountSubChunks(CHUNK_ID_LIST);
1424  }
1425 
1430  size_t List::CountSubLists(uint32_t ListType) {
1431  size_t result = 0;
1432  if (!pSubChunks) LoadSubChunks();
1433  ChunkList::iterator iter = pSubChunks->begin();
1434  ChunkList::iterator end = pSubChunks->end();
1435  while (iter != end) {
1436  if ((*iter)->GetChunkID() == CHUNK_ID_LIST) {
1437  List* l = (List*) *iter;
1438  if (l->GetListType() == ListType) result++;
1439  }
1440  iter++;
1441  }
1442  return result;
1443  }
1444 
1458  Chunk* List::AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize) {
1459  if (ullBodySize == 0) throw Exception("Chunk body size must be at least 1 byte");
1460  if (!pSubChunks) LoadSubChunks();
1461  Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0);
1462  pSubChunks->push_back(pNewChunk);
1463  (*pSubChunksMap)[uiChunkID] = pNewChunk;
1464  pNewChunk->Resize(ullBodySize);
1465  ullNewChunkSize += CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
1466  return pNewChunk;
1467  }
1468 
1480  void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) {
1481  if (!pSubChunks) LoadSubChunks();
1482  for (size_t i = 0; i < pSubChunks->size(); ++i) {
1483  if ((*pSubChunks)[i] == pSrc) {
1484  pSubChunks->erase(pSubChunks->begin() + i);
1485  ChunkList::iterator iter =
1486  find(pSubChunks->begin(), pSubChunks->end(), pDst);
1487  pSubChunks->insert(iter, pSrc);
1488  return;
1489  }
1490  }
1491  }
1492 
1501  void List::MoveSubChunk(Chunk* pSrc, List* pNewParent) {
1502  if (pNewParent == this || !pNewParent) return;
1503  if (!pSubChunks) LoadSubChunks();
1504  if (!pNewParent->pSubChunks) pNewParent->LoadSubChunks();
1505  ChunkList::iterator iter =
1506  find(pSubChunks->begin(), pSubChunks->end(), pSrc);
1507  if (iter == pSubChunks->end()) return;
1508  pSubChunks->erase(iter);
1509  pNewParent->pSubChunks->push_back(pSrc);
1510  // update chunk id map of this List
1511  if ((*pSubChunksMap)[pSrc->GetChunkID()] == pSrc) {
1512  pSubChunksMap->erase(pSrc->GetChunkID());
1513  // try to find another chunk of the same chunk ID
1514  ChunkList::iterator iter = pSubChunks->begin();
1515  ChunkList::iterator end = pSubChunks->end();
1516  for (; iter != end; ++iter) {
1517  if ((*iter)->GetChunkID() == pSrc->GetChunkID()) {
1518  (*pSubChunksMap)[pSrc->GetChunkID()] = *iter;
1519  break; // we're done, stop search
1520  }
1521  }
1522  }
1523  // update chunk id map of other list
1524  if (!(*pNewParent->pSubChunksMap)[pSrc->GetChunkID()])
1525  (*pNewParent->pSubChunksMap)[pSrc->GetChunkID()] = pSrc;
1526  }
1527 
1537  List* List::AddSubList(uint32_t uiListType) {
1538  if (!pSubChunks) LoadSubChunks();
1539  List* pNewListChunk = new List(pFile, this, uiListType);
1540  pSubChunks->push_back(pNewListChunk);
1541  (*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk;
1542  ullNewChunkSize += LIST_HEADER_SIZE(pFile->FileOffsetSize);
1543  return pNewListChunk;
1544  }
1545 
1556  void List::DeleteSubChunk(Chunk* pSubChunk) {
1557  if (!pSubChunks) LoadSubChunks();
1558  ChunkList::iterator iter =
1559  find(pSubChunks->begin(), pSubChunks->end(), pSubChunk);
1560  if (iter == pSubChunks->end()) return;
1561  pSubChunks->erase(iter);
1562  if ((*pSubChunksMap)[pSubChunk->GetChunkID()] == pSubChunk) {
1563  pSubChunksMap->erase(pSubChunk->GetChunkID());
1564  // try to find another chunk of the same chunk ID
1565  ChunkList::iterator iter = pSubChunks->begin();
1566  ChunkList::iterator end = pSubChunks->end();
1567  for (; iter != end; ++iter) {
1568  if ((*iter)->GetChunkID() == pSubChunk->GetChunkID()) {
1569  (*pSubChunksMap)[pSubChunk->GetChunkID()] = *iter;
1570  break; // we're done, stop search
1571  }
1572  }
1573  }
1574  delete pSubChunk;
1575  }
1576 
1585  if (!pSubChunks) LoadSubChunks();
1586  file_offset_t size = LIST_HEADER_SIZE(fileOffsetSize);
1587  ChunkList::iterator iter = pSubChunks->begin();
1588  ChunkList::iterator end = pSubChunks->end();
1589  for (; iter != end; ++iter)
1590  size += (*iter)->RequiredPhysicalSize(fileOffsetSize);
1591  return size;
1592  }
1593 
1594  void List::ReadHeader(file_offset_t filePos) {
1595  #if DEBUG_RIFF
1596  std::cout << "List::Readheader(file_offset_t) ";
1597  #endif // DEBUG_RIFF
1598  Chunk::ReadHeader(filePos);
1599  if (ullCurrentChunkSize < 4) return;
1600  ullNewChunkSize = ullCurrentChunkSize -= 4;
1601 
1602  const File::Handle hRead = pFile->FileHandle();
1603 
1604  #if POSIX
1605  lseek(hRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET);
1606  read(hRead, &ListType, 4);
1607  #elif defined(WIN32)
1608  LARGE_INTEGER liFilePos;
1609  liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
1610  SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
1611  DWORD dwBytesRead;
1612  ReadFile(hRead, &ListType, 4, &dwBytesRead, NULL);
1613  #else
1614  fseeko(hRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET);
1615  fread(&ListType, 4, 1, hRead);
1616  #endif // POSIX
1617  #if DEBUG_RIFF
1618  std::cout << "listType=" << convertToString(ListType) << std::endl;
1619  #endif // DEBUG_RIFF
1620  if (!pFile->bEndianNative) {
1621  //swapBytes_32(&ListType);
1622  }
1623  }
1624 
1625  void List::WriteHeader(file_offset_t filePos) {
1626  // the four list type bytes officially belong the chunk's body in the RIFF format
1627  ullNewChunkSize += 4;
1628  Chunk::WriteHeader(filePos);
1629  ullNewChunkSize -= 4; // just revert the +4 incrementation
1630 
1631  const File::Handle hWrite = pFile->FileWriteHandle();
1632 
1633  #if POSIX
1634  lseek(hWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET);
1635  write(hWrite, &ListType, 4);
1636  #elif defined(WIN32)
1637  LARGE_INTEGER liFilePos;
1638  liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
1639  SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
1640  DWORD dwBytesWritten;
1641  WriteFile(hWrite, &ListType, 4, &dwBytesWritten, NULL);
1642  #else
1643  fseeko(hWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET);
1644  fwrite(&ListType, 4, 1, hWrite);
1645  #endif // POSIX
1646  }
1647 
1648  void List::LoadSubChunks(progress_t* pProgress) {
1649  #if DEBUG_RIFF
1650  std::cout << "List::LoadSubChunks()";
1651  #endif // DEBUG_RIFF
1652  if (!pSubChunks) {
1653  pSubChunks = new ChunkList();
1654  pSubChunksMap = new ChunkMap();
1655 
1656  const File::Handle hRead = pFile->FileHandle();
1657  if (!_isValidHandle(hRead)) return;
1658 
1659  const file_offset_t ullOriginalPos = GetPos();
1660  SetPos(0); // jump to beginning of list chunk body
1661  while (RemainingBytes() >= CHUNK_HEADER_SIZE(pFile->FileOffsetSize)) {
1662  Chunk* ck;
1663  uint32_t ckid;
1664  // return value check is required here to prevent a potential
1665  // garbage data use of 'ckid' below in case Read() failed
1666  if (Read(&ckid, 4, 1) != 4)
1667  throw Exception("LoadSubChunks(): Failed reading RIFF chunk ID");
1668  #if DEBUG_RIFF
1669  std::cout << " ckid=" << convertToString(ckid) << std::endl;
1670  #endif // DEBUG_RIFF
1671  const file_offset_t pos = GetPos();
1672  if (ckid == CHUNK_ID_LIST) {
1673  ck = new RIFF::List(pFile, ullStartPos + pos - 4, this);
1674  SetPos(ck->GetSize() + LIST_HEADER_SIZE(pFile->FileOffsetSize) - 4, RIFF::stream_curpos);
1675  }
1676  else { // simple chunk
1677  ck = new RIFF::Chunk(pFile, ullStartPos + pos - 4, this);
1678  SetPos(ck->GetSize() + CHUNK_HEADER_SIZE(pFile->FileOffsetSize) - 4, RIFF::stream_curpos);
1679  }
1680  pSubChunks->push_back(ck);
1681  (*pSubChunksMap)[ckid] = ck;
1682  if (GetPos() % 2 != 0) SetPos(1, RIFF::stream_curpos); // jump over pad byte
1683  }
1684  SetPos(ullOriginalPos); // restore position before this call
1685  }
1686  if (pProgress)
1687  __notify_progress(pProgress, 1.0); // notify done
1688  }
1689 
1690  void List::LoadSubChunksRecursively(progress_t* pProgress) {
1691  const int n = (int) CountSubLists();
1692  int i = 0;
1693  for (List* pList = GetSubListAt(i); pList; pList = GetSubListAt(++i)) {
1694  if (pProgress) {
1695  // divide local progress into subprogress
1696  progress_t subprogress;
1697  __divide_progress(pProgress, &subprogress, n, i);
1698  // do the actual work
1699  pList->LoadSubChunksRecursively(&subprogress);
1700  } else
1701  pList->LoadSubChunksRecursively(NULL);
1702  }
1703  if (pProgress)
1704  __notify_progress(pProgress, 1.0); // notify done
1705  }
1706 
1722  file_offset_t List::WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress) {
1723  const file_offset_t ullOriginalPos = ullWritePos;
1724  ullWritePos += LIST_HEADER_SIZE(pFile->FileOffsetSize);
1725 
1726  if (pFile->GetMode() != stream_mode_read_write)
1727  throw Exception("Cannot write list chunk, file has to be opened in read+write mode");
1728 
1729  // write all subchunks (including sub list chunks) recursively
1730  if (pSubChunks) {
1731  size_t i = 0;
1732  const size_t n = pSubChunks->size();
1733  for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) {
1734  if (pProgress) {
1735  // divide local progress into subprogress for loading current Instrument
1736  progress_t subprogress;
1737  __divide_progress(pProgress, &subprogress, n, i);
1738  // do the actual work
1739  ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, &subprogress);
1740  } else
1741  ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, NULL);
1742  }
1743  }
1744 
1745  // update this list chunk's header
1746  ullCurrentChunkSize = ullNewChunkSize = ullWritePos - ullOriginalPos - LIST_HEADER_SIZE(pFile->FileOffsetSize);
1747  WriteHeader(ullOriginalPos);
1748 
1749  // offset of this list chunk in new written file may have changed
1750  ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize);
1751 
1752  if (pProgress)
1753  __notify_progress(pProgress, 1.0); // notify done
1754 
1755  return ullWritePos;
1756  }
1757 
1760  if (pSubChunks) {
1761  for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) {
1762  (*iter)->__resetPos();
1763  }
1764  }
1765  }
1766 
1770  String List::GetListTypeString() const {
1771  return convertToString(ListType);
1772  }
1773 
1774 
1775 
1776 // *************** File ***************
1777 // *
1778 
1793  File::File(uint32_t FileType)
1794  : List(this), bIsNewFile(true), Layout(layout_standard),
1795  FileOffsetPreference(offset_size_auto)
1796  {
1797  io.isPerThread = false;
1798  #if defined(WIN32)
1799  io.hRead = io.hWrite = INVALID_HANDLE_VALUE;
1800  #else
1801  io.hRead = io.hWrite = 0;
1802  #endif
1803  io.Mode = stream_mode_closed;
1804  bEndianNative = true;
1805  ListType = FileType;
1806  FileOffsetSize = 4;
1807  ullStartPos = RIFF_HEADER_SIZE(FileOffsetSize);
1808  }
1809 
1818  File::File(const String& path)
1819  : List(this), Filename(path), bIsNewFile(false), Layout(layout_standard),
1820  FileOffsetPreference(offset_size_auto)
1821  {
1822  #if DEBUG_RIFF
1823  std::cout << "File::File("<<path<<")" << std::endl;
1824  #endif // DEBUG_RIFF
1825  bEndianNative = true;
1826  FileOffsetSize = 4;
1827  try {
1828  __openExistingFile(path);
1829  if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) {
1830  throw RIFF::Exception("Not a RIFF file");
1831  }
1832  }
1833  catch (...) {
1834  Cleanup();
1835  throw;
1836  }
1837  }
1838 
1865  File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize)
1866  : List(this), Filename(path), bIsNewFile(false), Layout(layout),
1867  FileOffsetPreference(fileOffsetSize)
1868  {
1869  SetByteOrder(Endian);
1870  if (fileOffsetSize < offset_size_auto || fileOffsetSize > offset_size_64bit)
1871  throw Exception("Invalid RIFF::offset_size_t");
1872  FileOffsetSize = 4;
1873  try {
1874  __openExistingFile(path, &FileType);
1875  }
1876  catch (...) {
1877  Cleanup();
1878  throw;
1879  }
1880  }
1881 
1892  void File::__openExistingFile(const String& path, uint32_t* FileType) {
1893  io.isPerThread = false;
1894  #if POSIX
1895  io.hRead = io.hWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK);
1896  if (io.hRead == -1) {
1897  io.hRead = io.hWrite = 0;
1898  String sError = strerror(errno);
1899  throw RIFF::Exception("Can't open \"" + path + "\": " + sError);
1900  }
1901  #elif defined(WIN32)
1902  io.hRead = io.hWrite = CreateFile(
1903  path.c_str(), GENERIC_READ,
1904  FILE_SHARE_READ | FILE_SHARE_WRITE,
1905  NULL, OPEN_EXISTING,
1906  FILE_ATTRIBUTE_NORMAL |
1907  FILE_FLAG_RANDOM_ACCESS, NULL
1908  );
1909  if (io.hRead == INVALID_HANDLE_VALUE) {
1910  io.hRead = io.hWrite = INVALID_HANDLE_VALUE;
1911  throw RIFF::Exception("Can't open \"" + path + "\"");
1912  }
1913  #else
1914  io.hRead = io.hWrite = fopen(path.c_str(), "rb");
1915  if (!io.hRead) throw RIFF::Exception("Can't open \"" + path + "\"");
1916  #endif // POSIX
1917  io.Mode = stream_mode_read;
1918 
1919  // determine RIFF file offset size to be used (in RIFF chunk headers)
1920  // according to the current file offset preference
1921  FileOffsetSize = FileOffsetSizeFor(GetCurrentFileSize());
1922 
1923  switch (Layout) {
1924  case layout_standard: // this is a normal RIFF file
1925  ullStartPos = RIFF_HEADER_SIZE(FileOffsetSize);
1926  ReadHeader(0);
1927  if (FileType && ChunkID != *FileType)
1928  throw RIFF::Exception("Invalid file container ID");
1929  break;
1930  case layout_flat: // non-standard RIFF-alike file
1931  ullStartPos = 0;
1932  ullNewChunkSize = ullCurrentChunkSize = GetCurrentFileSize();
1933  if (FileType) {
1934  uint32_t ckid;
1935  if (Read(&ckid, 4, 1) != 4) {
1936  throw RIFF::Exception("Invalid file header ID (premature end of header)");
1937  } else if (ckid != *FileType) {
1938  String s = " (expected '" + convertToString(*FileType) + "' but got '" + convertToString(ckid) + "')";
1939  throw RIFF::Exception("Invalid file header ID" + s);
1940  }
1941  SetPos(0); // reset to first byte of file
1942  }
1943  LoadSubChunks();
1944  break;
1945  }
1946  }
1947 
1948  String File::GetFileName() const {
1949  return Filename;
1950  }
1951 
1952  void File::SetFileName(const String& path) {
1953  Filename = path;
1954  }
1955 
1964  File::HandlePair& File::FileHandlePairUnsafeRef() {
1965  if (io.byThread.empty()) return io;
1966  const std::thread::id tid = std::this_thread::get_id();
1967  const auto it = io.byThread.find(tid);
1968  return (it != io.byThread.end()) ?
1969  it->second :
1970  io.byThread[tid] = {
1971  // designated initializers require C++20, so commented for now
1972  #if defined(WIN32)
1973  /* .hRead = */ INVALID_HANDLE_VALUE,
1974  /* .hWrite = */ INVALID_HANDLE_VALUE,
1975  #else
1976  /* .hRead = */ 0,
1977  /* .hWrite = */ 0,
1978  #endif
1979  /* .Mode = */ stream_mode_closed
1980  };
1981  }
1982 
1989  File::HandlePair File::FileHandlePair() const {
1990  std::lock_guard<std::mutex> lock(io.mutex);
1991  if (io.byThread.empty()) return io;
1992  const std::thread::id tid = std::this_thread::get_id();
1993  const auto it = io.byThread.find(tid);
1994  return (it != io.byThread.end()) ?
1995  it->second :
1996  io.byThread[tid] = {
1997  // designated initializers require C++20, so commented for now
1998  #if defined(WIN32)
1999  /* .hRead = */ INVALID_HANDLE_VALUE,
2000  /* .hWrite = */ INVALID_HANDLE_VALUE,
2001  #else
2002  /* .hRead = */ 0,
2003  /* .hWrite = */ 0,
2004  #endif
2005  /* .Mode = */ stream_mode_closed
2006  };
2007  }
2008 
2016  return FileHandlePair().hRead;
2017  }
2018 
2026  return FileHandlePair().hWrite;
2027  }
2028 
2036  return FileHandlePair().Mode;
2037  }
2038 
2039  layout_t File::GetLayout() const {
2040  return Layout;
2041  }
2042 
2056  bool bResetPos = false;
2057  bool res = SetModeInternal(NewMode, &bResetPos);
2058  // resetting position must be handled outside above's call to avoid any
2059  // potential dead lock, as SetModeInternal() acquires a lock and
2060  // __resetPos() acquires a lock by itself (not a theoretical issue!)
2061  if (bResetPos)
2062  __resetPos(); // reset read/write position of ALL 'Chunk' objects
2063  return res;
2064  }
2065 
2066  bool File::SetModeInternal(stream_mode_t NewMode, bool* pResetPos) {
2067  std::lock_guard<std::mutex> lock(io.mutex);
2068  HandlePair& io = FileHandlePairUnsafeRef();
2069  if (NewMode != io.Mode) {
2070  switch (NewMode) {
2071  case stream_mode_read:
2072  if (_isValidHandle(io.hRead)) _close(io.hRead);
2073  #if POSIX
2074  io.hRead = io.hWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK);
2075  if (io.hRead == -1) {
2076  io.hRead = io.hWrite = 0;
2077  String sError = strerror(errno);
2078  throw Exception("Could not (re)open file \"" + Filename + "\" in read mode: " + sError);
2079  }
2080  #elif defined(WIN32)
2081  io.hRead = io.hWrite = CreateFile(
2082  Filename.c_str(), GENERIC_READ,
2083  FILE_SHARE_READ | FILE_SHARE_WRITE,
2084  NULL, OPEN_EXISTING,
2085  FILE_ATTRIBUTE_NORMAL |
2086  FILE_FLAG_RANDOM_ACCESS,
2087  NULL
2088  );
2089  if (io.hRead == INVALID_HANDLE_VALUE) {
2090  io.hRead = io.hWrite = INVALID_HANDLE_VALUE;
2091  throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
2092  }
2093  #else
2094  io.hRead = io.hWrite = fopen(Filename.c_str(), "rb");
2095  if (!io.hRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
2096  #endif
2097  *pResetPos = true;
2098  break;
2099  case stream_mode_read_write:
2100  if (_isValidHandle(io.hRead)) _close(io.hRead);
2101  #if POSIX
2102  io.hRead = io.hWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK);
2103  if (io.hRead == -1) {
2104  io.hRead = io.hWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK);
2105  String sError = strerror(errno);
2106  throw Exception("Could not open file \"" + Filename + "\" in read+write mode: " + sError);
2107  }
2108  #elif defined(WIN32)
2109  io.hRead = io.hWrite = CreateFile(
2110  Filename.c_str(),
2111  GENERIC_READ | GENERIC_WRITE,
2112  FILE_SHARE_READ,
2113  NULL, OPEN_ALWAYS,
2114  FILE_ATTRIBUTE_NORMAL |
2115  FILE_FLAG_RANDOM_ACCESS,
2116  NULL
2117  );
2118  if (io.hRead == INVALID_HANDLE_VALUE) {
2119  io.hRead = io.hWrite = CreateFile(
2120  Filename.c_str(), GENERIC_READ,
2121  FILE_SHARE_READ | FILE_SHARE_WRITE,
2122  NULL, OPEN_EXISTING,
2123  FILE_ATTRIBUTE_NORMAL |
2124  FILE_FLAG_RANDOM_ACCESS,
2125  NULL
2126  );
2127  throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode");
2128  }
2129  #else
2130  io.hRead = io.hWrite = fopen(Filename.c_str(), "r+b");
2131  if (!io.hRead) {
2132  io.hRead = io.hWrite = fopen(Filename.c_str(), "rb");
2133  throw Exception("Could not open file \"" + Filename + "\" in read+write mode");
2134  }
2135  #endif
2136  *pResetPos = true;
2137  break;
2138  case stream_mode_closed:
2139  if (_isValidHandle(io.hRead)) _close(io.hRead);
2140  if (_isValidHandle(io.hWrite)) _close(io.hWrite);
2141  #if POSIX
2142  io.hRead = io.hWrite = 0;
2143  #elif defined(WIN32)
2144  io.hRead = io.hWrite = INVALID_HANDLE_VALUE;
2145  #else
2146  io.hRead = io.hWrite = NULL;
2147  #endif
2148  break;
2149  default:
2150  throw Exception("Unknown file access mode");
2151  }
2152  io.Mode = NewMode;
2153  return true;
2154  }
2155  return false;
2156  }
2157 
2168  #if WORDS_BIGENDIAN
2169  bEndianNative = Endian != endian_little;
2170  #else
2171  bEndianNative = Endian != endian_big;
2172  #endif
2173  }
2174 
2185  void File::Save(progress_t* pProgress) {
2186  //TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files)
2187  if (Layout == layout_flat)
2188  throw Exception("Saving a RIFF file with layout_flat is not implemented yet");
2189 
2190  // make sure the RIFF tree is built (from the original file)
2191  if (pProgress) {
2192  // divide progress into subprogress
2193  progress_t subprogress;
2194  __divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress
2195  // do the actual work
2196  LoadSubChunksRecursively(&subprogress);
2197  // notify subprogress done
2198  __notify_progress(&subprogress, 1.f);
2199  } else
2200  LoadSubChunksRecursively(NULL);
2201 
2202  // reopen file in write mode
2203  SetMode(stream_mode_read_write);
2204 
2205  // get the current file size as it is now still physically stored on disk
2206  const file_offset_t workingFileSize = GetCurrentFileSize();
2207 
2208  // get the overall file size required to save this file
2209  const file_offset_t newFileSize = GetRequiredFileSize(FileOffsetPreference);
2210 
2211  // determine whether this file will yield in a large file (>=4GB) and
2212  // the RIFF file offset size to be used accordingly for all chunks
2213  FileOffsetSize = FileOffsetSizeFor(newFileSize);
2214 
2215  const HandlePair io = FileHandlePair();
2216  const Handle hRead = io.hRead;
2217  const Handle hWrite = io.hWrite;
2218 
2219  // to be able to save the whole file without loading everything into
2220  // RAM and without having to store the data in a temporary file, we
2221  // enlarge the file with the overall positive file size change,
2222  // then move current data towards the end of the file by the calculated
2223  // positive file size difference and finally update / rewrite the file
2224  // by copying the old data back to the right position at the beginning
2225  // of the file
2226 
2227  // if there are positive size changes...
2228  file_offset_t positiveSizeDiff = 0;
2229  if (newFileSize > workingFileSize) {
2230  positiveSizeDiff = newFileSize - workingFileSize;
2231 
2232  // divide progress into subprogress
2233  progress_t subprogress;
2234  if (pProgress)
2235  __divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress
2236 
2237  // ... we enlarge this file first ...
2238  ResizeFile(newFileSize);
2239 
2240  // ... and move current data by the same amount towards end of file.
2241  int8_t* pCopyBuffer = new int8_t[4096];
2242  #if defined(WIN32)
2243  DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured
2244  #else
2245  ssize_t iBytesMoved = 1;
2246  #endif
2247  for (file_offset_t ullPos = workingFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) {
2248  iBytesMoved = (ullPos < 4096) ? ullPos : 4096;
2249  ullPos -= iBytesMoved;
2250  #if POSIX
2251  lseek(hRead, ullPos, SEEK_SET);
2252  iBytesMoved = read(hRead, pCopyBuffer, iBytesMoved);
2253  lseek(hWrite, ullPos + positiveSizeDiff, SEEK_SET);
2254  iBytesMoved = write(hWrite, pCopyBuffer, iBytesMoved);
2255  #elif defined(WIN32)
2256  LARGE_INTEGER liFilePos;
2257  liFilePos.QuadPart = ullPos;
2258  SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
2259  ReadFile(hRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
2260  liFilePos.QuadPart = ullPos + positiveSizeDiff;
2261  SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN);
2262  WriteFile(hWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
2263  #else
2264  fseeko(hRead, ullPos, SEEK_SET);
2265  iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hRead);
2266  fseeko(hWrite, ullPos + positiveSizeDiff, SEEK_SET);
2267  iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hWrite);
2268  #endif
2269  if (pProgress && !(iNotif % 8) && iBytesMoved > 0)
2270  __notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize));
2271  }
2272  delete[] pCopyBuffer;
2273  if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it");
2274 
2275  if (pProgress)
2276  __notify_progress(&subprogress, 1.f); // notify subprogress done
2277  }
2278 
2279  // rebuild / rewrite complete RIFF tree ...
2280 
2281  // divide progress into subprogress
2282  progress_t subprogress;
2283  if (pProgress)
2284  __divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress
2285  // do the actual work
2286  const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, pProgress ? &subprogress : NULL);
2287  const file_offset_t finalActualSize = __GetFileSize(hWrite);
2288  // notify subprogress done
2289  if (pProgress)
2290  __notify_progress(&subprogress, 1.f);
2291 
2292  // resize file to the final size
2293  if (finalSize < finalActualSize) ResizeFile(finalSize);
2294 
2295  if (pProgress)
2296  __notify_progress(pProgress, 1.0); // notify done
2297  }
2298 
2314  void File::Save(const String& path, progress_t* pProgress) {
2315  //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
2316 
2317  //TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files)
2318  if (Layout == layout_flat)
2319  throw Exception("Saving a RIFF file with layout_flat is not implemented yet");
2320 
2321  // make sure the RIFF tree is built (from the original file)
2322  if (pProgress) {
2323  // divide progress into subprogress
2324  progress_t subprogress;
2325  __divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress
2326  // do the actual work
2327  LoadSubChunksRecursively(&subprogress);
2328  // notify subprogress done
2329  __notify_progress(&subprogress, 1.f);
2330  } else
2331  LoadSubChunksRecursively(NULL);
2332 
2333  if (!bIsNewFile) SetMode(stream_mode_read);
2334 
2335  {
2336  std::lock_guard<std::mutex> lock(io.mutex);
2337  HandlePair& io = FileHandlePairUnsafeRef();
2338 
2339  // open the other (new) file for writing and truncate it to zero size
2340  #if POSIX
2341  io.hWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
2342  if (io.hWrite == -1) {
2343  io.hWrite = io.hRead;
2344  String sError = strerror(errno);
2345  throw Exception("Could not open file \"" + path + "\" for writing: " + sError);
2346  }
2347  #elif defined(WIN32)
2348  io.hWrite = CreateFile(
2349  path.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
2350  NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL |
2351  FILE_FLAG_RANDOM_ACCESS, NULL
2352  );
2353  if (io.hWrite == INVALID_HANDLE_VALUE) {
2354  io.hWrite = io.hRead;
2355  throw Exception("Could not open file \"" + path + "\" for writing");
2356  }
2357  #else
2358  io.hWrite = fopen(path.c_str(), "w+b");
2359  if (!io.hWrite) {
2360  io.hWrite = io.hRead;
2361  throw Exception("Could not open file \"" + path + "\" for writing");
2362  }
2363  #endif // POSIX
2364  io.Mode = stream_mode_read_write;
2365  }
2366 
2367  // get the overall file size required to save this file
2368  const file_offset_t newFileSize = GetRequiredFileSize(FileOffsetPreference);
2369 
2370  // determine whether this file will yield in a large file (>=4GB) and
2371  // the RIFF file offset size to be used accordingly for all chunks
2372  FileOffsetSize = FileOffsetSizeFor(newFileSize);
2373 
2374  // write complete RIFF tree to the other (new) file
2375  file_offset_t ullTotalSize;
2376  if (pProgress) {
2377  // divide progress into subprogress
2378  progress_t subprogress;
2379  __divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress
2380  // do the actual work
2381  ullTotalSize = WriteChunk(0, 0, &subprogress);
2382  // notify subprogress done
2383  __notify_progress(&subprogress, 1.f);
2384  } else
2385  ullTotalSize = WriteChunk(0, 0, NULL);
2386 
2387  const file_offset_t ullActualSize = __GetFileSize(FileWriteHandle());
2388 
2389  // resize file to the final size (if the file was originally larger)
2390  if (ullActualSize > ullTotalSize) ResizeFile(ullTotalSize);
2391 
2392  {
2393  std::lock_guard<std::mutex> lock(io.mutex);
2394  HandlePair& io = FileHandlePairUnsafeRef();
2395 
2396  if (_isValidHandle(io.hWrite)) _close(io.hWrite);
2397  io.hWrite = io.hRead;
2398 
2399  // associate new file with this File object from now on
2400  Filename = path;
2401  bIsNewFile = false;
2402  io.Mode = (stream_mode_t) -1; // Just set it to an undefined mode ...
2403  }
2404  SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles.
2405 
2406  if (pProgress)
2407  __notify_progress(pProgress, 1.0); // notify done
2408  }
2409 
2410  void File::ResizeFile(file_offset_t ullNewSize) {
2411  const Handle hWrite = FileWriteHandle();
2412  #if POSIX
2413  if (ftruncate(hWrite, ullNewSize) < 0)
2414  throw Exception("Could not resize file \"" + Filename + "\"");
2415  #elif defined(WIN32)
2416  LARGE_INTEGER liFilePos;
2417  liFilePos.QuadPart = ullNewSize;
2418  if (
2419  !SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN) ||
2420  !SetEndOfFile(hWrite)
2421  ) throw Exception("Could not resize file \"" + Filename + "\"");
2422  #else
2423  # error Sorry, this version of libgig only supports POSIX and Windows systems yet.
2424  # error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)!
2425  #endif
2426  }
2427 
2428  File::~File() {
2429  #if DEBUG_RIFF
2430  std::cout << "File::~File()" << std::endl;
2431  #endif // DEBUG_RIFF
2432  Cleanup();
2433  }
2434 
2439  bool File::IsNew() const {
2440  return bIsNewFile;
2441  }
2442 
2443  void File::Cleanup() {
2444  if (IsIOPerThread()) {
2445  for (auto it = io.byThread.begin(); it != io.byThread.end(); ++it) {
2446  _close(it->second.hRead);
2447  }
2448  } else {
2449  _close(io.hRead);
2450  }
2451  DeleteChunkList();
2452  pFile = NULL;
2453  }
2454 
2462  file_offset_t size = 0;
2463  const Handle hRead = FileHandle();
2464  try {
2465  size = __GetFileSize(hRead);
2466  } catch (...) {
2467  size = 0;
2468  }
2469  return size;
2470  }
2471 
2492  return GetRequiredFileSize(FileOffsetPreference);
2493  }
2494 
2507  switch (fileOffsetSize) {
2508  case offset_size_auto: {
2510  if (fileSize >> 32)
2512  else
2513  return fileSize;
2514  }
2515  case offset_size_32bit: break;
2516  case offset_size_64bit: break;
2517  default: throw Exception("Internal error: Invalid RIFF::offset_size_t");
2518  }
2520  }
2521 
2522  int File::FileOffsetSizeFor(file_offset_t fileSize) const {
2523  switch (FileOffsetPreference) {
2524  case offset_size_auto:
2525  return (fileSize >> 32) ? 8 : 4;
2526  case offset_size_32bit:
2527  return 4;
2528  case offset_size_64bit:
2529  return 8;
2530  default:
2531  throw Exception("Internal error: Invalid RIFF::offset_size_t");
2532  }
2533  }
2534 
2556  return FileOffsetSize;
2557  }
2558 
2570  return FileOffsetSizeFor(GetCurrentFileSize());
2571  }
2572 
2599  bool File::IsIOPerThread() const {
2600  //NOTE: Not caring about atomicity here at all, for three reasons:
2601  // 1. SetIOPerThread() is assumed to be called only once for the entire
2602  // life time of a RIFF::File, usually very early at its lifetime, and
2603  // hence a change to isPerThread should already safely be propagated
2604  // before any other thread would actually read this boolean flag.
2605  // 2. This method is called very frequently, and therefore any
2606  // synchronization techique would hurt runtime efficiency.
2607  // 3. Using even a mutex lock here might easily cause a deadlock due to
2608  // other locks been taken in this .cpp file, i.e. at a higher call
2609  // stack level (and this is the main reason why I removed it here).
2610  return io.isPerThread;
2611  }
2612 
2628  void File::SetIOPerThread(bool enable) {
2629  std::lock_guard<std::mutex> lock(io.mutex);
2630  if (!io.byThread.empty() == enable) return;
2631  io.isPerThread = enable;
2632  if (enable) {
2633  const std::thread::id tid = std::this_thread::get_id();
2634  io.byThread[tid] = io;
2635  } else {
2636  // retain an arbitrary handle pair, close all other handle pairs
2637  for (auto it = io.byThread.begin(); it != io.byThread.end(); ++it) {
2638  if (it == io.byThread.begin()) {
2639  io.hRead = it->second.hRead;
2640  io.hWrite = it->second.hWrite;
2641  } else {
2642  _close(it->second.hRead);
2643  _close(it->second.hWrite);
2644  }
2645  }
2646  io.byThread.clear();
2647  }
2648  }
2649 
2650  #if POSIX
2651  file_offset_t File::__GetFileSize(int hFile) const {
2652  struct stat filestat;
2653  if (fstat(hFile, &filestat) == -1)
2654  throw Exception("POSIX FS error: could not determine file size");
2655  return filestat.st_size;
2656  }
2657  #elif defined(WIN32)
2658  file_offset_t File::__GetFileSize(HANDLE hFile) const {
2659  LARGE_INTEGER size;
2660  if (!GetFileSizeEx(hFile, &size))
2661  throw Exception("Windows FS error: could not determine file size");
2662  return size.QuadPart;
2663  }
2664  #else // standard C functions
2665  file_offset_t File::__GetFileSize(FILE* hFile) const {
2666  off_t curpos = ftello(hFile);
2667  if (fseeko(hFile, 0, SEEK_END) == -1)
2668  throw Exception("FS error: could not determine file size");
2669  off_t size = ftello(hFile);
2670  fseeko(hFile, curpos, SEEK_SET);
2671  return size;
2672  }
2673  #endif
2674 
2675 
2676 // *************** Exception ***************
2677 // *
2678 
2679  Exception::Exception() {
2680  }
2681 
2682  Exception::Exception(String format, ...) {
2683  va_list arg;
2684  va_start(arg, format);
2685  Message = assemble(format, arg);
2686  va_end(arg);
2687  }
2688 
2689  Exception::Exception(String format, va_list arg) {
2690  Message = assemble(format, arg);
2691  }
2692 
2693  void Exception::PrintMessage() {
2694  std::cout << "RIFF::Exception: " << Message << std::endl;
2695  }
2696 
2697  String Exception::assemble(String format, va_list arg) {
2698  char* buf = NULL;
2699  vasprintf(&buf, format.c_str(), arg);
2700  String s = buf;
2701  free(buf);
2702  return s;
2703  }
2704 
2705 
2706 // *************** functions ***************
2707 // *
2708 
2714  String libraryName() {
2715  return PACKAGE;
2716  }
2717 
2722  String libraryVersion() {
2723  return VERSION;
2724  }
2725 
2726 } // namespace RIFF
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:828
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:2439
int16_t ReadInt16()
Reads one 16 Bit signed integer word and increments the position within the chunk.
Definition: RIFF.cpp:875
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:1584
int FileOffsetSize
Size of file offsets (in bytes) when this file was opened (or saved the last time).
Definition: RIFF.h:365
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:732
size_t CountSubLists()
Returns number of sublists within the list.
Definition: RIFF.cpp:1422
stream_whence_t
File stream position dependent to these relations.
Definition: RIFF.h:124
Chunk * GetFirstSubChunk()
Returns the first subchunk within the list (which may be an ordinary chunk as well as a list chunk)...
Definition: RIFF.cpp:1315
String libraryName()
Returns the name of this C++ library.
Definition: RIFF.cpp:2714
File(uint32_t FileType)
Create new RIFF file.
Definition: RIFF.cpp:1793
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&#39;...
Definition: RIFF.cpp:771
file_offset_t GetSize() const
Chunk size in bytes (without header, thus the chunk data body)
Definition: RIFF.h:186
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:1264
layout_t Layout
An ordinary RIFF file is always set to layout_standard.
Definition: RIFF.h:363
void(* callback)(progress_t *)
Callback function pointer which has to be assigned to a function for progress notification.
Definition: RIFF.h:164
stream_state_t
Current state of the file stream.
Definition: RIFF.h:117
void ReadString(String &s, int size)
Reads a null-padded string of size characters and copies it into the string s.
Definition: RIFF.cpp:806
String libraryVersion()
Returns version of this C++ library.
Definition: RIFF.cpp:2722
std::vector< progress_t > subdivide(int iSubtasks)
Divides this progress task into the requested amount of equal weighted sub-progress tasks and returns...
Definition: RIFF.cpp:93
List * GetSubList(uint32_t ListType)
Returns sublist chunk with list type ListType within this chunk list.
Definition: RIFF.cpp:1287
void DeleteSubChunk(Chunk *pSubChunk)
Removes a sub chunk.
Definition: RIFF.cpp:1556
Handle FileHandle() const
Returns the OS dependent file I/O read handle intended to be used by the calling thread.
Definition: RIFF.cpp:2015
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:146
List * GetFirstSubList()
Returns the first sublist within the list (that is a subchunk with chunk ID "LIST").
Definition: RIFF.cpp:1354
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:1057
stream_mode_t
Whether file stream is open in read or in read/write mode.
Definition: RIFF.h:110
String GetListTypeString() const
Returns string representation of the lists&#39;s id.
Definition: RIFF.cpp:1770
void SetByteOrder(endian_t Endian)
Set the byte order to be used when saving.
Definition: RIFF.cpp:2167
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:1989
RIFF List Chunk.
Definition: RIFF.h:261
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&#39;...
Definition: RIFF.cpp:693
Handle FileWriteHandle() const
Returns the OS dependent file I/O write handle intended to be used by the calling thread...
Definition: RIFF.cpp:2025
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:575
int Handle
OS dependent type serving as file handle / descriptor for OS dependent file I/O operations.
Definition: RIFF.h:320
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:441
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&#39;s...
Definition: RIFF.cpp:615
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:341
int8_t ReadInt8()
Reads one 8 Bit signed integer word and increments the position within the chunk. ...
Definition: RIFF.cpp:840
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:1722
offset_size_t
Size of RIFF file offsets used in all RIFF chunks&#39; headers.
Definition: RIFF.h:145
uint64_t file_offset_t
Type used by libgig for handling file positioning during file I/O tasks.
Definition: RIFF.h:107
float __range_min
Only for internal usage, do not modify!
Definition: RIFF.h:167
Chunk * GetSubChunk(uint32_t ChunkID)
Returns subchunk with chunk ID ChunkID within this chunk list.
Definition: RIFF.cpp:1246
Chunk * GetNextSubChunk()
Returns the next subchunk within the list (which may be an ordinary chunk as well as a list chunk)...
Definition: RIFF.cpp:1334
int32_t ReadInt32()
Reads one 32 Bit signed integer word and increments the position within the chunk.
Definition: RIFF.cpp:911
virtual void Save(progress_t *pProgress=NULL)
Save changes to same file.
Definition: RIFF.cpp:2185
layout_t
General RIFF chunk structure of a RIFF file.
Definition: RIFF.h:139
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:391
Ordinary RIFF Chunk.
Definition: RIFF.h:179
file_offset_t GetRequiredFileSize()
Returns the required size (in bytes) for this RIFF File to be saved to disk.
Definition: RIFF.cpp:2491
uint32_t ReadUint32()
Reads one 32 Bit unsigned integer word and increments the position within the chunk.
Definition: RIFF.cpp:929
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:2555
uint32_t GetChunkID() const
Chunk ID in unsigned integer representation.
Definition: RIFF.h:183
void * custom
This pointer can be used for arbitrary data.
Definition: RIFF.h:166
file_offset_t RemainingBytes() const
Returns the number of bytes left to read in the chunk body.
Definition: RIFF.cpp:376
Used for indicating the progress of a certain task.
Definition: RIFF.h:163
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:517
uint32_t GetListType() const
Returns unsigned integer representation of the list&#39;s ID.
Definition: RIFF.h:265
Not a "real" RIFF file: First chunk in file is an ordinary data chunk, not a List chunk...
Definition: RIFF.h:141
bool IsIOPerThread() const
Whether file streams are independent for each thread.
Definition: RIFF.cpp:2599
Chunk * GetSubChunkAt(size_t pos)
Returns subchunk at supplied pos position within this chunk list.
Definition: RIFF.cpp:1229
virtual void __resetPos()
Sets Chunk&#39;s read/write position to zero.
Definition: RIFF.cpp:1159
uint16_t ReadUint16()
Reads one 16 Bit unsigned integer word and increments the position within the chunk.
Definition: RIFF.cpp:893
virtual void __resetPos()
Sets List Chunk&#39;s read/write position to zero and causes all sub chunks to do the same...
Definition: RIFF.cpp:1758
endian_t
Alignment of data bytes in memory (system dependant).
Definition: RIFF.h:132
void * LoadChunkData()
Load chunk body into RAM.
Definition: RIFF.cpp:960
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:654
Standard RIFF file layout: First chunk in file is a List chunk which contains all other chunks and th...
Definition: RIFF.h:140
void Resize(file_offset_t NewSize)
Resize chunk.
Definition: RIFF.cpp:1034
RIFF File.
Definition: RIFF.h:313
List * AddSubList(uint32_t uiListType)
Creates a new list sub chunk.
Definition: RIFF.cpp:1537
RIFF specific classes and definitions.
Definition: RIFF.h:97
void MoveSubChunk(Chunk *pSrc, Chunk *pDst)
Moves a sub chunk witin this list.
Definition: RIFF.cpp:1480
Always use 32 bit offsets (even for files larger than 4 GB).
Definition: RIFF.h:147
size_t CountSubChunks()
Returns number of subchunks within the list (including list chunks).
Definition: RIFF.cpp:1396
stream_state_t GetState() const
Returns the current state of the chunk object.
Definition: RIFF.cpp:410
bool SetMode(stream_mode_t NewMode)
Change file access mode.
Definition: RIFF.cpp:2055
float __range_max
Only for internal usage, do not modify!
Definition: RIFF.h:168
String GetChunkIDString() const
Returns the String representation of the chunk&#39;s ID (e.g.
Definition: RIFF.cpp:288
int GetRequiredFileOffsetSize()
Returns the required size (in bytes) of file offsets stored in the headers of all chunks of this file...
Definition: RIFF.cpp:2569
Will be thrown whenever an error occurs while handling a RIFF file.
Definition: RIFF.h:391
file_offset_t GetFilePos() const
Current, actual offset in file of current chunk data body read/write position.
Definition: RIFF.cpp:324
Always use 64 bit offsets (even for files smaller than 4 GB).
Definition: RIFF.h:148
void ReleaseChunkData()
Free loaded chunk body from RAM.
Definition: RIFF.cpp:1009
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:2461
file_offset_t GetPos() const
Current read/write position within the chunk data body (starting with 0).
Definition: RIFF.cpp:311
void SetIOPerThread(bool enable)
Enable/disable file streams being independent for each thread.
Definition: RIFF.cpp:2628
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:2035
List * GetNextSubList()
Returns the next sublist (that is a subchunk with chunk ID "LIST") within the list.
Definition: RIFF.cpp:1378
Chunk * AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize)
Creates a new sub chunk.
Definition: RIFF.cpp:1458
uint8_t ReadUint8()
Reads one 8 Bit unsigned integer word and increments the position within the chunk.
Definition: RIFF.cpp:857