首页 > 解决方案 > 读取 zlib/miniz 压缩数据时出现 DATA_ERROR

问题描述

我正在为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;
}

我的资料

我已经在调试器中验证了我读取的数据是正确的: 在此处输入图像描述 您可以看到zStreamwhich is a mz_streamhas data in next_inwhich is valid zlib-encoded data。至少它以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-

错误

无论出于何种原因,mz_inflate模拟 zlib 的调用inflate返回DATA_ERROR (-3)。get 中的total_in字段zStream设置为 13,因此看起来在错误发生之前只读取了 13 个字节。

总结一下:如果压缩后的数据没问题,可以用 提取zlib-flate,那为什么不能 miniz 读取这些数据呢?它实际上是自己写的。如果前 13 个字节有问题,我看不出它可能是什么。

作为参考,这里是Inflator和测试的完整代码。

标签: c++iocompressionzlibminiz

解决方案


这是一个虎头蛇尾的解决方案,但事实证明我应该使用实际miniz而不是单头镜像,即miniz-cpp. 这个单头库使用的是 2017 年严重过时的库版本,根本无法正确读取数据。

使用实际miniz时,测试通过并且一切正常。我的代码 100% 正确,只是使用了错误的库。


推荐阅读