Skip to content

Commit

Permalink
improving the compression ratio for Lzma mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AXiX-official committed Jul 30, 2024
1 parent 30efddc commit 5d4291e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
10 changes: 7 additions & 3 deletions include/ABRecompressor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ extern "C" {
#define ABRECOMPRESSOR_H

#ifdef _WIN32
#ifdef ABRECOMPRESSOR_EXPORTS
#define ABRECOMPRESSOR_API __declspec(dllexport)
#ifdef ABRECOMPRESSOR_STATIC
#define ABRECOMPRESSOR_API
#else
#define ABRECOMPRESSOR_API __declspec(dllimport)
#ifdef ABRECOMPRESSOR_EXPORTS
#define ABRECOMPRESSOR_API __declspec(dllexport)
#else
#define ABRECOMPRESSOR_API __declspec(dllimport)
#endif
#endif
#else
#define ABRECOMPRESSOR_API
Expand Down
14 changes: 9 additions & 5 deletions src/BlocksAndDirectoryInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ BlocksAndDirectoryInfo::BlocksAndDirectoryInfo(Reader* reader, Header* header, b
}

ByteArr* BlocksAndDirectoryInfo::ToBytes(){
uint64_t size = 16 + 4 + 4 + 10 * BlocksInfoCount;
for (uint i = 0; i < DirectoryInfoCount; i++){
size += DirectoryInfos[i].GetSize();
}
ByteArr* result = new ByteArr(size);
ByteArr* result = new ByteArr(GetRawSize());
memcpy(result->GetData(), UncompressedDataHash->GetData(), 16);
Writer writer = Writer(result, 16);
writer.WriteUInt32(BlocksInfoCount);
Expand Down Expand Up @@ -95,4 +91,12 @@ uint64_t BlocksAndDirectoryInfo::DataSize(){
return size1;
}
throw std::runtime_error("Data size mismatch, for blocks is " + std::to_string(size1) + " while for cab is " + std::to_string(size2));
}

uint64_t BlocksAndDirectoryInfo::GetRawSize(){
uint64_t size = 16 + 4 + 4 + 10 * BlocksInfoCount;
for (uint i = 0; i < DirectoryInfoCount; i++){
size += DirectoryInfos[i].GetSize();
}
return size;
}
1 change: 1 addition & 0 deletions src/BlocksAndDirectoryInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class BlocksAndDirectoryInfo{
ByteArr* ToBytes();
ByteArr* ToBytes(CompressionType compressionType);
uint64_t DataSize();
uint64_t GetRawSize();
};

#endif // ABRECOMPRESSOR_BLOCKSANDDIRECTORYINFO_H
13 changes: 13 additions & 0 deletions src/BundleFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ char BundleFile::GetCompressionType(){
}

ByteArr* BundleFile::ToBytes(CompressionType compressionType){
if (compressionType == CompressionType::Lzma){
uint64_t totalSize = data->GetSize();
mDataInfo->BlocksInfoCount = (totalSize / UINT32_MAX) + ((totalSize % UINT32_MAX) ? 1 : 0);
for (int i = 0; i < mDataInfo->BlocksInfoCount; i++){
if (i == mDataInfo->BlocksInfoCount - 1){
mDataInfo->BlocksInfos[i].uncompressedSize = totalSize % UINT32_MAX;
}else{
mDataInfo->BlocksInfos[i].uncompressedSize = UINT32_MAX;
}
}
}

for (int i = 0; i < mDataInfo->BlocksInfoCount; i++){
mDataInfo->BlocksInfos[i].flags = (StorageBlockFlags)(mDataInfo->BlocksInfos[i].flags | compressionType);
}
Expand All @@ -126,6 +138,7 @@ ByteArr* BundleFile::ToBytes(CompressionType compressionType){
p += mDataInfo->BlocksInfos[i].uncompressedSize;
}
ByteArr* bDataInfo = mDataInfo->ToBytes(CompressionType::Lz4HC);
mHeader->mUncompressedBlocksInfoSize = mDataInfo->GetRawSize();
mHeader->mCompressedBlocksInfoSize = bDataInfo->GetSize();
uint64_t size = mHeader->GetSize();
if (HeaderAligned){
Expand Down
1 change: 1 addition & 0 deletions src/Compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ ByteArr* Compress(CompressionType compressionType, byte* tocompressData, uint to
lzma_ret ret;
lzma_options_lzma opt;
lzma_lzma_preset(&opt, LZMA_PRESET_EXTREME);
opt.dict_size = 0x200000;
ret = lzma_alone_encoder(&strm, &opt);
if (ret != LZMA_OK) {
throw std::runtime_error("lzma_stream_buffer_decode error: " + std::to_string(ret));
Expand Down

0 comments on commit 5d4291e

Please sign in to comment.