Skip to content

Commit 8116049

Browse files
committed
Improve signature check and allow numeric zlib wrapper format
1 parent 691c34b commit 8116049

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -730,10 +730,10 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
730730
}
731731
case StringEncodeFunction::ZLIB:
732732
{
733-
int compression = 9;
734-
ZLibFormat format = ZLibFormat::GZIP;
733+
int compression = 9;
734+
int format = (int)ZLibFormat::GZIP;
735735
ZLibStrategy strategy = ZLibStrategy::DEFAULT;
736-
if (!options["format"].empty() && !StringToEnum(options["format"], format))
736+
if (!options["format"].empty() && !StringToEnum(options["format"], (ZLibFormat&)format) && !StringToZLibFormat(options["format"], format))
737737
{
738738
m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'format'");
739739
lua::Push(luaVM, false);
@@ -1170,8 +1170,8 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
11701170
}
11711171
case StringEncodeFunction::ZLIB:
11721172
{
1173-
ZLibFormat format = ZLibFormat::AUTO;
1174-
if (!options["format"].empty() && !StringToEnum(options["format"], format))
1173+
int format = 0;
1174+
if (!options["format"].empty() && !StringToEnum(options["format"], (ZLibFormat&)format) && !StringToZLibFormat(options["format"], format))
11751175
{
11761176
m_pScriptDebugging->LogCustom(luaVM, "Not supported value for field 'format'");
11771177
lua::Push(luaVM, false);

Shared/sdk/SharedUtil.Crypto.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,23 @@ namespace SharedUtil
204204
return result;
205205
}
206206

207-
inline int ZLibCompress(const std::string input, std::string* output, const ZLibFormat format = ZLibFormat::GZIP, const int compression = 9,
207+
inline bool StringToZLibFormat(std::string format, int &outResult)
208+
{
209+
int value = atoi(format.c_str());
210+
if ((value >= 9 && value <= 31) || (value >= -15 && value <= -9)) // allowed values: 9..31, -9..-15
211+
{
212+
outResult = value;
213+
return true;
214+
}
215+
return false;
216+
}
217+
218+
inline int ZLibCompress(const std::string input, std::string* output, const int windowBits = (int)ZLibFormat::GZIP, const int compression = 9,
208219
const ZLibStrategy strategy = ZLibStrategy::DEFAULT)
209220
{
210221
z_stream stream{};
211222

212-
int result = deflateInit2(&stream, compression, Z_DEFLATED, (int)format, MAX_MEM_LEVEL, (int)strategy);
223+
int result = deflateInit2(&stream, compression, Z_DEFLATED, windowBits, MAX_MEM_LEVEL, (int)strategy);
213224
if (result != Z_OK)
214225
return result;
215226

@@ -230,15 +241,14 @@ namespace SharedUtil
230241
return result;
231242
}
232243

233-
inline int ZLibUncompress(const std::string& input, std::string* output, const ZLibFormat format = ZLibFormat::AUTO)
244+
inline int ZLibUncompress(const std::string& input, std::string* output, int windowBits = 0)
234245
{
235-
int windowBits = (int)format;
236-
if (format == ZLibFormat::AUTO)
246+
if (windowBits == 0 && input.size() >= 2) // try to determine format automatically
237247
{
238-
if (input[0] == 0x78)
239-
windowBits = (int)ZLibFormat::ZLIB;
240-
else if (input[0] == 0x1F)
248+
if (input[0] == '\x1F' && input[1] == '\x8B')
241249
windowBits = (int)ZLibFormat::GZIP;
250+
else if (input[0] == '\x78')
251+
windowBits = (int)ZLibFormat::ZLIB;
242252
else
243253
windowBits = (int)ZLibFormat::ZRAW;
244254
}

Shared/sdk/SharedUtil.Hash.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ enum class HmacAlgorithm
7070

7171
enum class ZLibFormat
7272
{
73-
AUTO = 0,
7473
ZRAW = -15,
7574
ZLIB = 15,
7675
GZIP = 31,

0 commit comments

Comments
 (0)