diff --git a/dbpf-recompress.cpp b/dbpf-recompress.cpp index ae4934b..fc9087e 100644 --- a/dbpf-recompress.cpp +++ b/dbpf-recompress.cpp @@ -284,20 +284,31 @@ bool validatePackage(dbpf::Package& oldPackage, dbpf::Package& newPackage, fstre bytes newContent = dbpf::readFile(newFile, newEntry.location, newEntry.size); //compression info in the directory of compressed files should match the information in the compression header - bool compressed_in_header = newContent[4] == 0x10 && newContent[5] == 0xFB; - bool in_clst = newPackage.compressedEntries.find(dbpf::CompressedEntry{newEntry.type, newEntry.group, newEntry.instance, newEntry.resource}) != newPackage.compressedEntries.end(); + bool compressed_in_header = newContent.size() >= 9 && newContent[4] == 0x10 && newContent[5] == 0xFB; + auto iter = newPackage.compressedEntries.find(dbpf::CompressedEntry{newEntry.type, newEntry.group, newEntry.instance, newEntry.resource}); + bool in_clst = iter != newPackage.compressedEntries.end(); if(compressed_in_header != in_clst) { wcout << displayPath << L": Incorrect compression information" << endl; return false; } - //the compressor should only produce compressed entries that are smaller than the original decompressed entries if(newEntry.compressed) { uint tempPos = 0; uint uncompressedSize = dbpf::getUncompressedSize(newContent); uint compressedSize = dbpf::getInt(newContent, tempPos); + if(uncompressedSize != iter->uncompressedSize) { + wcout << displayPath << L": Mismatch between the uncompressed size in the compression header and the uncompressed size in the CLST" << endl; + return false; + } + + if(compressedSize != newEntry.size) { + wcout << displayPath << L": Mismatch between the compressed size in the compression header and the compressed size in the index" << endl; + return false; + } + + //the compressor should only produce compressed entries that are smaller than the original decompressed entries if(compressedSize > uncompressedSize) { wcout << displayPath << L": Compressed size is larger than the uncompressed size for one entry" << endl; return false; diff --git a/dbpf.h b/dbpf.h index 95e3c75..1fea5dc 100644 --- a/dbpf.h +++ b/dbpf.h @@ -384,12 +384,10 @@ namespace dbpf { //check if the entries are compressed for(auto& entry: package.entries) { auto iter = package.compressedEntries.find(CompressedEntry{entry.type, entry.group, entry.instance, entry.resource}); - if(entry.size >= 9 && iter != package.compressedEntries.end()) { - bytes header = readFile(file, entry.location, 9); - - if(header[4] == 0x10 && header[5] == 0xFB) { - entry.compressed = true; - } + entry.compressed = iter != package.compressedEntries.end(); + + if(entry.compressed) { + entry.uncompressedSize = iter->uncompressedSize; } } } @@ -438,16 +436,19 @@ namespace dbpf { writeFile(newFile, buffer); //compress and write entries, and save the location and size for the index - omp_lock_t lock; - omp_init_lock(&lock); + omp_lock_t r_lock; + omp_lock_t w_lock; + + omp_init_lock(&r_lock); + omp_init_lock(&w_lock); #pragma omp parallel for for(int i = 0; i < package.entries.size(); i++) { auto& entry = package.entries[i]; - omp_set_lock(&lock); + omp_set_lock(&r_lock); bytes content = readFile(oldFile, entry.location, entry.size); - omp_unset_lock(&lock); + omp_unset_lock(&r_lock); if(mode == RECOMPRESS) { content = recompressEntry(entry, content); @@ -462,15 +463,16 @@ namespace dbpf { entry.uncompressedSize = getUncompressedSize(content); } - omp_set_lock(&lock); + omp_set_lock(&w_lock); entry.location = newFile.tellp(); writeFile(newFile, content); - omp_unset_lock(&lock); + omp_unset_lock(&w_lock); } - omp_destroy_lock(&lock); + omp_destroy_lock(&r_lock); + omp_destroy_lock(&w_lock); //make and write the directory of compressed files bytes clstContent; @@ -575,4 +577,4 @@ namespace dbpf { } -#endif +#endif \ No newline at end of file