zlib: process inflate Z_STREAM_END as success if no remaining bytes

This commit is contained in:
Jean-Francois Dockes 2020-02-28 11:14:39 +01:00
parent 1f0296a873
commit 5353a22ce0

View File

@ -169,6 +169,18 @@ private:
#if defined(READFILE_ENABLE_ZLIB) #if defined(READFILE_ENABLE_ZLIB)
#include <zlib.h> #include <zlib.h>
#include <vector>
static const vector<CharFlags> inflateErrors{
CHARFLAGENTRY(Z_OK),
CHARFLAGENTRY(Z_STREAM_END),
CHARFLAGENTRY(Z_NEED_DICT),
CHARFLAGENTRY(Z_ERRNO),
CHARFLAGENTRY(Z_STREAM_ERROR),
CHARFLAGENTRY(Z_DATA_ERROR),
CHARFLAGENTRY(Z_MEM_ERROR),
CHARFLAGENTRY(Z_BUF_ERROR),
CHARFLAGENTRY(Z_VERSION_ERROR),
};
class GzFilter : public FileScanFilter { class GzFilter : public FileScanFilter {
public: public:
@ -229,11 +241,13 @@ public:
while (m_stream.avail_in != 0) { while (m_stream.avail_in != 0) {
m_stream.next_out = (Bytef*)m_obuf; m_stream.next_out = (Bytef*)m_obuf;
m_stream.avail_out = m_obs; m_stream.avail_out = m_obs;
if ((error = inflate(&m_stream, Z_SYNC_FLUSH)) != Z_OK) { error = inflate(&m_stream, Z_SYNC_FLUSH);
// Note that Z_STREAM_END is also an error here, if (error != Z_OK &&
// because we still have data: something is wrong with !(error == Z_STREAM_END && m_stream.avail_in == 0)) {
// the file. // Note that Z_STREAM_END with avail_in!=0 is an error,
LOGERR("inflate error: " << error << endl); // we still have data: something is wrong with the file.
LOGERR("inflate error: " << valToString(inflateErrors, error)
<< " remaining bytes: " << m_stream.avail_in << endl);
if (reason) { if (reason) {
*reason += " Zlib inflate failed"; *reason += " Zlib inflate failed";
if (m_stream.msg && *m_stream.msg) { if (m_stream.msg && *m_stream.msg) {
@ -502,7 +516,7 @@ bool file_scan(const std::string& filename, const std::string& membername,
if (membername.empty()) { if (membername.empty()) {
return file_scan(filename, doer, 0, -1, reason return file_scan(filename, doer, 0, -1, reason
#ifdef READFILE_ENABLE_MD5 #ifdef READFILE_ENABLE_MD5
, nullptr , nullptr
#endif #endif
); );
} else { } else {
@ -517,7 +531,7 @@ bool string_scan(const char *data, size_t cnt, const std::string& membername,
if (membername.empty()) { if (membername.empty()) {
return string_scan(data, cnt, doer, reason return string_scan(data, cnt, doer, reason
#ifdef READFILE_ENABLE_MD5 #ifdef READFILE_ENABLE_MD5
, nullptr , nullptr
#endif #endif
); );
} else { } else {
@ -581,7 +595,7 @@ bool file_scan(const string& fn, FileScanDo* doer, string *reason)
{ {
return file_scan(fn, doer, 0, -1, reason return file_scan(fn, doer, 0, -1, reason
#ifdef READFILE_ENABLE_MD5 #ifdef READFILE_ENABLE_MD5
, nullptr , nullptr
#endif #endif
); );
} }