Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
1- Use different locks for reading and writing, slightly improving speed.
2- Check for compression by only using the CLST.
  • Loading branch information
lingeringwillx authored Mar 20, 2024
1 parent fe5e367 commit 2ec5eea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
17 changes: 14 additions & 3 deletions dbpf-recompress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 16 additions & 14 deletions dbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -575,4 +577,4 @@ namespace dbpf {

}

#endif
#endif

0 comments on commit 2ec5eea

Please sign in to comment.