1341 |
return pGroup; |
return pGroup; |
1342 |
} |
} |
1343 |
|
|
1344 |
|
/** |
1345 |
|
* Checks the integrity of this sample's raw audio wave data. Whenever a |
1346 |
|
* Sample's raw wave data is intentionally modified (i.e. by calling |
1347 |
|
* Write() and supplying the new raw audio wave form data) a CRC32 checksum |
1348 |
|
* is calculated and stored/updated for this sample, along to the sample's |
1349 |
|
* meta informations. |
1350 |
|
* |
1351 |
|
* Now by calling this method the current raw audio wave data is checked |
1352 |
|
* against the already stored CRC32 check sum in order to check whether the |
1353 |
|
* sample data had been damaged unintentionally for some reason. Since by |
1354 |
|
* calling this method always the entire raw audio wave data has to be |
1355 |
|
* read, verifying all samples this way may take a long time accordingly. |
1356 |
|
* And that's also the reason why the sample integrity is not checked by |
1357 |
|
* default whenever a gig file is loaded. So this method must be called |
1358 |
|
* explicitly to fulfill this task. |
1359 |
|
* |
1360 |
|
* @returns true if sample is OK or false if the sample is damaged |
1361 |
|
* @throws Exception if no checksum had been stored to disk for this |
1362 |
|
* sample yet, or on I/O issues |
1363 |
|
*/ |
1364 |
|
bool Sample::VerifyWaveData() { |
1365 |
|
File* pFile = static_cast<File*>(GetParent()); |
1366 |
|
const uint32_t referenceCRC = pFile->GetSampleChecksum(this); |
1367 |
|
uint32_t crc = CalculateWaveDataChecksum(); |
1368 |
|
return crc == referenceCRC; |
1369 |
|
} |
1370 |
|
|
1371 |
|
uint32_t Sample::CalculateWaveDataChecksum() { |
1372 |
|
const size_t sz = 20*1024; // 20kB buffer size |
1373 |
|
std::vector<uint8_t> buffer(sz); |
1374 |
|
buffer.resize(sz); |
1375 |
|
|
1376 |
|
const size_t n = sz / FrameSize; |
1377 |
|
SetPos(0); |
1378 |
|
uint32_t crc = 0; |
1379 |
|
__resetCRC(crc); |
1380 |
|
while (true) { |
1381 |
|
file_offset_t nRead = Read(&buffer[0], n); |
1382 |
|
if (nRead <= 0) break; |
1383 |
|
__calculateCRC(&buffer[0], nRead * FrameSize, crc); |
1384 |
|
} |
1385 |
|
__encodeCRC(crc); |
1386 |
|
return crc; |
1387 |
|
} |
1388 |
|
|
1389 |
Sample::~Sample() { |
Sample::~Sample() { |
1390 |
Instances--; |
Instances--; |
1391 |
if (!Instances && InternalDecompressionBuffer.Size) { |
if (!Instances && InternalDecompressionBuffer.Size) { |
5660 |
if (!_3crc) return; |
if (!_3crc) return; |
5661 |
|
|
5662 |
// get the index of the sample |
// get the index of the sample |
5663 |
int iWaveIndex = -1; |
int iWaveIndex = GetWaveTableIndexOf(pSample); |
|
File::SampleList::iterator iter = pSamples->begin(); |
|
|
File::SampleList::iterator end = pSamples->end(); |
|
|
for (int index = 0; iter != end; ++iter, ++index) { |
|
|
if (*iter == pSample) { |
|
|
iWaveIndex = index; |
|
|
break; |
|
|
} |
|
|
} |
|
5664 |
if (iWaveIndex < 0) throw gig::Exception("Could not update crc, could not find sample"); |
if (iWaveIndex < 0) throw gig::Exception("Could not update crc, could not find sample"); |
5665 |
|
|
5666 |
// write the CRC-32 checksum to disk |
// write the CRC-32 checksum to disk |
5667 |
_3crc->SetPos(iWaveIndex * 8); |
_3crc->SetPos(iWaveIndex * 8); |
5668 |
uint32_t tmp = 1; |
uint32_t one = 1; |
5669 |
_3crc->WriteUint32(&tmp); // unknown, always 1? |
_3crc->WriteUint32(&one); // always 1 |
5670 |
_3crc->WriteUint32(&crc); |
_3crc->WriteUint32(&crc); |
5671 |
|
|
5672 |
|
// reload CRC table to RAM to keep it persistent over several subsequent save operations |
5673 |
|
_3crc->ReleaseChunkData(); |
5674 |
|
_3crc->LoadChunkData(); |
5675 |
|
} |
5676 |
|
|
5677 |
|
uint32_t File::GetSampleChecksum(Sample* pSample) { |
5678 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
5679 |
|
if (!_3crc) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); |
5680 |
|
uint8_t* pData = (uint8_t*) _3crc->LoadChunkData(); |
5681 |
|
if (!pData) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); |
5682 |
|
|
5683 |
|
// get the index of the sample |
5684 |
|
int iWaveIndex = GetWaveTableIndexOf(pSample); |
5685 |
|
if (iWaveIndex < 0) throw gig::Exception("Could not retrieve reference crc of sample, could not resolve sample's wave table index"); |
5686 |
|
|
5687 |
|
// read the CRC-32 checksum directly from disk |
5688 |
|
size_t pos = iWaveIndex * 8; |
5689 |
|
if (pos + 8 > _3crc->GetNewSize()) |
5690 |
|
throw gig::Exception("Could not retrieve reference crc of sample, could not seek to required position in crc chunk"); |
5691 |
|
|
5692 |
|
uint32_t one = load32(&pData[pos]); // always 1 |
5693 |
|
if (one != 1) |
5694 |
|
throw gig::Exception("Could not verify sample, because reference checksum table is damaged"); |
5695 |
|
|
5696 |
|
return load32(&pData[pos+4]); |
5697 |
|
} |
5698 |
|
|
5699 |
|
int File::GetWaveTableIndexOf(gig::Sample* pSample) { |
5700 |
|
if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned |
5701 |
|
File::SampleList::iterator iter = pSamples->begin(); |
5702 |
|
File::SampleList::iterator end = pSamples->end(); |
5703 |
|
for (int index = 0; iter != end; ++iter, ++index) |
5704 |
|
if (*iter == pSample) |
5705 |
|
return index; |
5706 |
|
return -1; |
5707 |
|
} |
5708 |
|
|
5709 |
|
/** |
5710 |
|
* Checks whether the file's "3CRC" chunk was damaged. This chunk contains |
5711 |
|
* the CRC32 check sums of all samples' raw wave data. |
5712 |
|
* |
5713 |
|
* @return true if 3CRC chunk is OK, or false if 3CRC chunk is damaged |
5714 |
|
*/ |
5715 |
|
bool File::VerifySampleChecksumTable() { |
5716 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
5717 |
|
if (!_3crc) return false; |
5718 |
|
if (_3crc->GetNewSize() <= 0) return false; |
5719 |
|
if (_3crc->GetNewSize() % 8) return false; |
5720 |
|
if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned |
5721 |
|
if (_3crc->GetNewSize() != pSamples->size() * 8) return false; |
5722 |
|
|
5723 |
|
const int n = _3crc->GetNewSize() / 8; |
5724 |
|
|
5725 |
|
uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); |
5726 |
|
if (!pData) return false; |
5727 |
|
|
5728 |
|
for (int i = 0; i < n; ++i) { |
5729 |
|
uint32_t one = pData[i*2]; |
5730 |
|
if (one != 1) return false; |
5731 |
|
} |
5732 |
|
|
5733 |
|
return true; |
5734 |
|
} |
5735 |
|
|
5736 |
|
/** |
5737 |
|
* Recalculates CRC32 checksums for all samples and rebuilds this gig |
5738 |
|
* file's checksum table with those new checksums. This might usually |
5739 |
|
* just be necessary if the checksum table was damaged. |
5740 |
|
* |
5741 |
|
* @e IMPORTANT: The current implementation of this method only works |
5742 |
|
* with files that have not been modified since it was loaded, because |
5743 |
|
* it expects that no externally caused file structure changes are |
5744 |
|
* required! |
5745 |
|
* |
5746 |
|
* Due to the expectation above, this method is currently protected |
5747 |
|
* and actually only used by the command line tool "gigdump" yet. |
5748 |
|
* |
5749 |
|
* @returns true if Save() is required to be called after this call, |
5750 |
|
* false if no further action is required |
5751 |
|
*/ |
5752 |
|
bool File::RebuildSampleChecksumTable() { |
5753 |
|
// make sure sample chunks were scanned |
5754 |
|
if (!pSamples) GetFirstSample(); |
5755 |
|
|
5756 |
|
bool bRequiresSave = false; |
5757 |
|
|
5758 |
|
// make sure "3CRC" chunk exists with required size |
5759 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
5760 |
|
if (!_3crc) { |
5761 |
|
_3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); |
5762 |
|
bRequiresSave = true; |
5763 |
|
} else if (_3crc->GetNewSize() != pSamples->size() * 8) { |
5764 |
|
_3crc->Resize(pSamples->size() * 8); |
5765 |
|
bRequiresSave = true; |
5766 |
|
} |
5767 |
|
|
5768 |
|
if (bRequiresSave) { // refill CRC table for all samples in RAM ... |
5769 |
|
uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); |
5770 |
|
{ |
5771 |
|
File::SampleList::iterator iter = pSamples->begin(); |
5772 |
|
File::SampleList::iterator end = pSamples->end(); |
5773 |
|
for (; iter != end; ++iter) { |
5774 |
|
gig::Sample* pSample = (gig::Sample*) *iter; |
5775 |
|
int index = GetWaveTableIndexOf(pSample); |
5776 |
|
if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved"); |
5777 |
|
pData[index*2] = 1; // always 1 |
5778 |
|
pData[index*2+1] = pSample->CalculateWaveDataChecksum(); |
5779 |
|
} |
5780 |
|
} |
5781 |
|
} else { // no file structure changes necessary, so directly write to disk and we are done ... |
5782 |
|
// make sure file is in write mode |
5783 |
|
pRIFF->SetMode(RIFF::stream_mode_read_write); |
5784 |
|
{ |
5785 |
|
File::SampleList::iterator iter = pSamples->begin(); |
5786 |
|
File::SampleList::iterator end = pSamples->end(); |
5787 |
|
for (; iter != end; ++iter) { |
5788 |
|
gig::Sample* pSample = (gig::Sample*) *iter; |
5789 |
|
int index = GetWaveTableIndexOf(pSample); |
5790 |
|
if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved"); |
5791 |
|
uint32_t crc = pSample->CalculateWaveDataChecksum(); |
5792 |
|
SetSampleChecksum(pSample, crc); |
5793 |
|
} |
5794 |
|
} |
5795 |
|
} |
5796 |
|
|
5797 |
|
return bRequiresSave; |
5798 |
} |
} |
5799 |
|
|
5800 |
Group* File::GetFirstGroup() { |
Group* File::GetFirstGroup() { |
6056 |
lst3LS = NULL; |
lst3LS = NULL; |
6057 |
} |
} |
6058 |
|
|
6059 |
|
// if there is a 3CRC chunk, make sure it is loaded into RAM, otherwise |
6060 |
|
// its data might get lost or damaged on file structure changes |
6061 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
6062 |
|
if (_3crc) _3crc->LoadChunkData(); |
6063 |
|
|
6064 |
// first update base class's chunks |
// first update base class's chunks |
6065 |
DLS::File::UpdateChunks(pProgress); |
DLS::File::UpdateChunks(pProgress); |
6066 |
|
|
6220 |
// samples. The actual checksum values will be filled in |
// samples. The actual checksum values will be filled in |
6221 |
// later, by Sample::Write. |
// later, by Sample::Write. |
6222 |
|
|
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
|
6223 |
if (_3crc) { |
if (_3crc) { |
6224 |
_3crc->Resize(pSamples->size() * 8); |
_3crc->Resize(pSamples->size() * 8); |
6225 |
} else if (newFile) { |
} else if (newFile) { |