c++ - 读取zlib/miniz压缩数据时出现DATA_ERROR

标签 c++ io compression zlib miniz

我正在为miniz-cpp的zlib压缩实现编写一个简单的C++包装。通货紧缩可以解决,但现在又出现了数据膨胀问题。

我有一个测试用例(大致简化)归结为:

ByteArray randomData = createRandomData(1024 * 1024);
ByteArray deflatedBytes = deflate(randomData);
writeToTmpFile(deflatedBytes); // for manual review
ByteArray inflatedBytes = inflate(deflatedBytes);

assert(randomData == inflatedBytes);
我被卡在DATA_ERROR (-3)上,当我再次为数据充气时会返回它。
这是发生问题的函数:
// inflates the next <size> bytes and stores them in <out[]>
// stores the actually written amount in <written>
ResultCode Inflator::inflate(uint8_t out[], size_t size, size_t& written)
{
    zStream.next_out = out;
    zStream.avail_out = static_cast<unsigned int>(size);

    // loop until output buffer is completely filled
    while (zStream.avail_out != 0) {
        if (zStream.avail_in == 0) {
            // our Inflator stores a ByteArrayInputStream from which
            // we request more data
            size_t read = iStream.read(in, BUFFER_SIZE);
            if (iStream.err()) {
                return ResultCode::STREAM_ERROR;
            }
            zStream.next_in = in;
            zStream.avail_in = static_cast<unsigned int>(read);
        }

        // THIS IS WHERE WE ACTUALLY CALL INFLATE.
        // RESULT CODE -3 (DATA_ERROR) IS RETURNED AFTER READING
        // ONLY 13 BYTES.
        ResultCode result{mz_inflate(&zStream, Flushing::NONE)};

        if (result == ResultCode::STREAM_END) {
            written = size - zStream.avail_out;
            this->eof_ = true;
            return ResultCode::OK;
        }
        else if (result != ResultCode::OK) {
            return result;
        }
    }

    written = size - zStream.avail_out;
    return ResultCode::OK;
}
我的资料
我已经在调试器中验证了我读取的数据是正确的:
enter image description here
您会看到zStream(即mz_stream)在next_in中具有数据,该数据是有效的zlib编码数据。至少它以0x78开头。
正如我在伪代码中提到的,我还将数据转储到磁盘。使用以下命令可以很好地读取此数据:
# this command is included in the qpdf package and uncompresses zlib streams
zlib-flate -uncompress < 'mve_deflOutput.zlib' > 'mve_deflOutput.bin'
这也是前几个字节的十六进制转储:
00000000: 7801 a4dd fb7f cfe5 1b07 7072 c8a9 9632  x.........pr...2
00000010: 49ac 9043 9a4c 392c 462c 4d88 8ab0 4a7c  I..C.L9,F,M...J|
00000020: 55d6 2c6d 6921 0931 34ad 4db5 8898 1c26  U.,mi!.14.M....&
00000030: 3a88 ce69 51d9 6a52 94a8 302d d252 6ba6  :..iQ.jR..0-.Rk.
00000040: 84a2 b2ef 9ff0 fce1 be7f dd63 dbe7 f37e  ...........c...~
00000050: dff7 75bd aed7 eb75 5df7 11ac 4358 3763  ..u....u]...CX7c
00000060: b5c0 ea88 550f ab33 d62e aca7 b132 b116  ....U..3.....2..
00000070: 611d c73a 8935 096b 0d56 05d6 8758 a3b1  a..:.5.k.V...X..
00000080: f0ef d7b4 c5c2 bfff f027 2c3c be93 b5b1  .........',<....
00000090: cec2 1a80 7531 5612 d68d 583b b0d6 622d  ....u1V...X;..b-
错误
无论出于何种原因,对模仿zlib的mz_inflateinflate的调用都会返回DATA_ERROR (-3)total_in中的zStream字段设置为13,因此看起来在错误发生之前仅读取了13个字节。
总结一下:如果放气后的数据正常,可以使用zlib-flate提取,那么为什么不能最小化读取此数据?它是按字面意思写的。如果前13个字节有问题,我看不到可能是什么。
For reference, here is the full code of the Inflator and the test.

最佳答案

这是一个防反跳的解决方案,但事实证明,我应该使用实际的 miniz 而不是 miniz-cpp 的单头镜像。这个单头库使用的是自2017年以来库的严重过时版本,根本无法正确读取数据。
当使用实际的miniz时,测试通过了,一切正常。我的代码100%正确,只是使用了错误的库。

关于c++ - 读取zlib/miniz压缩数据时出现DATA_ERROR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63539054/

相关文章:

C++ 如何实现:myInstance << "abc"<< someInt << std::endl

windows - Haskell:立即从控制台读取输入字符,而不是换行后

python - 如何创建内存文件对象

java - 使用Java无法保存到文本文件

c++ - 来自多行文件 C++ 的 vector 矩阵

c++ - Linux Mint x64 : Qt 5. 3.1 插件部署:不兼容的 qt 库

Powershell 压缩文件夹和文件,然后删除旧文件

android - 减小图像的文件大小

C++ 常量和变量的连接名称

asp.net - IIS URL重写模块和动态压缩