zlib膨胀返回缓冲区错误 [英] zlib inflate returning a buffer error

查看:110
本文介绍了zlib膨胀返回缓冲区错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用zlib解压缩zip文件(不使用任何扩展名或第3方).最初,src_len为48756255,而dest_len为49209890.while循环中的第一遍很好:err是Z_OK,第二遍开始.在第二遍,无论我怎么做,我都会从膨胀中得到Z_BUF_ERROR.此时的stream.total_out为49034460,所以还有一点剩余,但第二遍的stream.avail_in为0.无论如何,我希望充气会给我Z_STREAM_END.我真的不知道发生了什么,有人可以帮忙吗?

I'm trying to decompress a zip file using zlib (without using any extension or 3rd party). Initially, the src_len is 48756255, and the dest_len is 49209890. The first pass in the while loop is fine: err is Z_OK and the second pass through starts. On the second pass, no matter what I do I get a Z_BUF_ERROR from inflate. stream.total_out at this point is 49034460, so there is a bit remaining but stream.avail_in on the second pass is 0. In any case, I would expect inflate to give me Z_STREAM_END. I really don't know what's going on, can anyone help?

void compression::uncompress2(char* dest, unsigned dest_len, char* src, unsigned src_len) {
    TempAllocator ta;

    z_stream_s stream = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    stream.next_in = (Bytef*)src;
    stream.avail_in = (uInt)src_len;
    stream.next_out = (Bytef*)dest;
    stream.avail_out = (uInt)dest_len;
    stream.zalloc = zalloc;
    stream.zfree = zfree;
    stream.opaque = &ta;

    // no header
    int err = inflateInit2(&stream, -MAX_WBITS);
    XENSURE(err == Z_OK);

    bool done = false;
    while (!done) {    
        stream.next_out = (Bytef*)(dest + stream.total_out);
        stream.avail_out = dest_len - stream.total_out;

        err = inflate(&stream, Z_SYNC_FLUSH);
        if (err == Z_STREAM_END)
            done = true;
        else if (err != Z_OK) {
            break;
        }
    }

    err = inflateEnd(&stream);
    XENSURE(err == Z_OK);
}

推荐答案

inflate()将使用可用输出处理尽可能多的输入.一旦它既不能处理输入也不能产生输出,它将返回 Z_BUF_ERROR .

inflate() will process as much input as possible using the available output. Once it can neither process input nor produce output, it will return Z_BUF_ERROR.

在这种情况下,将处理所有输入,并且有剩余的输出空间,但是未检测到流的末尾.您未获得 Z_STREAM_END ,因为出于某些原因您没有提供完整的放气流.

In this case, all of the input is processed and there is output room to spare, but the end of the stream is not detected. You are not getting Z_STREAM_END because for some reason you are not providing a complete deflate stream.

以下是您的代码上的其他一些注释.您的循环不执行任何操作,并且像执行操作一样在循环中设置 next_out avail_out .您在循环中没有提供任何新的输入,也没有提供新的输出空间,因此没有循环的意义.在 inflate()周围循环的唯一目的是提供更多的输入,提供更多的输出空间,或同时提供这两者.此外,当 inflate()返回时, next_out avail_out 会更新到输出缓冲区中的下一个可用位置以及那里的可用空间量.您在循环中设置 next_out avail_out 的语句会将它们设置为它们已经具有的值.

Here are some other comments on your code. Your loop does nothing, and setting next_out and avail_out in the loop as you do does nothing. You are providing no new input nor new output space in your loop, so there is no point in having a loop. The only purpose of having a loop around inflate() is to either provide more input, provide more output space, or both. Furthermore when inflate() returns, next_out and avail_out are updated to the next available location in the output buffer and the amount of available space there. Your statements that set next_out and avail_out in the loop set those to the values they already have.

代码中,围绕 inflate() (一个> Z_BUF_ERROR 没问题,并且处理可以继续.尽管如果您在下一次调用 inflate()时期望得到不同的结果,则必须提供更多的输入和/或更多的输出空间.

In code with proper loops around inflate(), a Z_BUF_ERROR is not a problem, and the processing can continue. Though if you expect a different result on the next call of inflate(), more input and/or more output space must be provided.

无需使用零结构初始化 stream .后续任务就是您所需要的.

There is no need to initialize stream with a structure of zeros. The subsequent assignments are all you need.

这篇关于zlib膨胀返回缓冲区错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆