Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving PMC delta, reducing the blast radius of compression bombs #79

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Decompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ size_t Decompressor::getImageOffset() const noexcept
return 0;
}

// 1G should be enough for everyone (this is retro!)
// 128M should be enough for everyone (this is retro!)
size_t Decompressor::getMaxPackedSize() noexcept
{
return 0x4000'0000U;
return 0x800'0000U;
}

size_t Decompressor::getMaxRawSize() noexcept
{
return 0x4000'0000U;
return 0x800'0000U;
}

}
10 changes: 8 additions & 2 deletions src/LHDecompressor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* Copyright (C) Teemu Suutari */

#include <cstring>

#include "LHDecompressor.hpp"

#include "InputStream.hpp"
Expand Down Expand Up @@ -36,7 +38,7 @@ const std::string &LHDecompressor::getSubName() const noexcept
}

// lh.library decompress
void LHDecompressor::decompressLhLib(Buffer &rawData,const Buffer &packedData)
size_t LHDecompressor::decompressLhLib(Buffer &rawData,const Buffer &packedData)
{
ForwardInputStream inputStream{packedData,0,packedData.size()};
MSBBitReader<ForwardInputStream> bitReader{inputStream};
Expand Down Expand Up @@ -79,10 +81,14 @@ void LHDecompressor::decompressLhLib(Buffer &rawData,const Buffer &packedData)
else for (uint32_t i=0;i<count;i++) outputStream.writeByte(0);
}
}
return outputStream.getOffset();
}

void LHDecompressor::decompressImpl(Buffer &rawData,const Buffer &previousData,bool verify)
{
decompressLhLib(rawData,_packedData);
size_t length{decompressLhLib(rawData,_packedData)};
if (length!=rawData.size())
std::memset(rawData.data()+length,0,rawData.size()-length);
}

}
2 changes: 1 addition & 1 deletion src/LHDecompressor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LHDecompressor : public XPKDecompressor
static bool detectHeaderXPK(uint32_t hdr) noexcept;
static std::shared_ptr<XPKDecompressor> create(uint32_t hdr,uint32_t recursionLevel,const Buffer &packedData,std::shared_ptr<XPKDecompressor::State> &state,bool verify);

static void decompressLhLib(Buffer &rawData,const Buffer &packedData);
static size_t decompressLhLib(Buffer &rawData,const Buffer &packedData);

private:
const Buffer &_packedData;
Expand Down
20 changes: 17 additions & 3 deletions src/PMCDecompressor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* Copyright (C) Teemu Suutari */

#include <cstring>

#include "PMCDecompressor.hpp"
#include "InputStream.hpp"
#include "OutputStream.hpp"
Expand Down Expand Up @@ -60,11 +62,23 @@ size_t PMCDecompressor::getRawSize() const noexcept

void PMCDecompressor::decompressImpl(Buffer &rawData,bool verify)
{
// thats all folks!
if (rawData.size()<_rawSize)
throw DecompressionError();
ConstSubBuffer subPackedData(_packedData,12,_packedSize-12);

LHDecompressor::decompressLhLib(rawData,subPackedData);
if (_ver) DLTADecode::decode(rawData,rawData,0,_rawSize);
size_t length{LHDecompressor::decompressLhLib(rawData,subPackedData)};
if (!length)
throw DecompressionError();
// thats all folks!
if (_ver)
{
DLTADecode::decode(rawData,rawData,0,_rawSize);
if (length!=_rawSize)
std::memset(rawData.data()+length,rawData[length-1],_rawSize-length);
} else {
if (length!=_rawSize)
std::memset(rawData.data()+length,0,_rawSize-length);
}
}

}
Loading