Skip to content

Commit f25eb99

Browse files
authored
Improving PMC delta, reducing the blast radius of compression bombs (#79)
* Improving PMC delta, reducing the blast radius of compression bombs
1 parent 9929dc4 commit f25eb99

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

Diff for: src/Decompressor.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ size_t Decompressor::getImageOffset() const noexcept
108108
return 0;
109109
}
110110

111-
// 1G should be enough for everyone (this is retro!)
111+
// 128M should be enough for everyone (this is retro!)
112112
size_t Decompressor::getMaxPackedSize() noexcept
113113
{
114-
return 0x4000'0000U;
114+
return 0x800'0000U;
115115
}
116116

117117
size_t Decompressor::getMaxRawSize() noexcept
118118
{
119-
return 0x4000'0000U;
119+
return 0x800'0000U;
120120
}
121121

122122
}

Diff for: src/LHDecompressor.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* Copyright (C) Teemu Suutari */
22

3+
#include <cstring>
4+
35
#include "LHDecompressor.hpp"
46

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

3840
// lh.library decompress
39-
void LHDecompressor::decompressLhLib(Buffer &rawData,const Buffer &packedData)
41+
size_t LHDecompressor::decompressLhLib(Buffer &rawData,const Buffer &packedData)
4042
{
4143
ForwardInputStream inputStream{packedData,0,packedData.size()};
4244
MSBBitReader<ForwardInputStream> bitReader{inputStream};
@@ -79,10 +81,14 @@ void LHDecompressor::decompressLhLib(Buffer &rawData,const Buffer &packedData)
7981
else for (uint32_t i=0;i<count;i++) outputStream.writeByte(0);
8082
}
8183
}
84+
return outputStream.getOffset();
8285
}
86+
8387
void LHDecompressor::decompressImpl(Buffer &rawData,const Buffer &previousData,bool verify)
8488
{
85-
decompressLhLib(rawData,_packedData);
89+
size_t length{decompressLhLib(rawData,_packedData)};
90+
if (length!=rawData.size())
91+
std::memset(rawData.data()+length,0,rawData.size()-length);
8692
}
8793

8894
}

Diff for: src/LHDecompressor.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class LHDecompressor : public XPKDecompressor
2121
static bool detectHeaderXPK(uint32_t hdr) noexcept;
2222
static std::shared_ptr<XPKDecompressor> create(uint32_t hdr,uint32_t recursionLevel,const Buffer &packedData,std::shared_ptr<XPKDecompressor::State> &state,bool verify);
2323

24-
static void decompressLhLib(Buffer &rawData,const Buffer &packedData);
24+
static size_t decompressLhLib(Buffer &rawData,const Buffer &packedData);
2525

2626
private:
2727
const Buffer &_packedData;

Diff for: src/PMCDecompressor.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* Copyright (C) Teemu Suutari */
22

3+
#include <cstring>
4+
35
#include "PMCDecompressor.hpp"
46
#include "InputStream.hpp"
57
#include "OutputStream.hpp"
@@ -60,11 +62,23 @@ size_t PMCDecompressor::getRawSize() const noexcept
6062

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

66-
LHDecompressor::decompressLhLib(rawData,subPackedData);
67-
if (_ver) DLTADecode::decode(rawData,rawData,0,_rawSize);
69+
size_t length{LHDecompressor::decompressLhLib(rawData,subPackedData)};
70+
if (!length)
71+
throw DecompressionError();
72+
// thats all folks!
73+
if (_ver)
74+
{
75+
DLTADecode::decode(rawData,rawData,0,_rawSize);
76+
if (length!=_rawSize)
77+
std::memset(rawData.data()+length,rawData[length-1],_rawSize-length);
78+
} else {
79+
if (length!=_rawSize)
80+
std::memset(rawData.data()+length,0,_rawSize-length);
81+
}
6882
}
6983

7084
}

0 commit comments

Comments
 (0)