@@ -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 }
0 commit comments