Init the value of z_stream.avail_out when using decompressing package which is sent from server in client by using inflate in zlib.h

StackOverflow https://stackoverflow.com/questions/18082774

  •  23-06-2022
  •  | 
  •  

문제

I want to decompress the package,which is sent from server, in client.

When the size of package is small, the below code is ok.

However when the package's size is bigger, the value of below out_length variable has to be bigger than 1024. I don't want to do that, I want to has a dynamically way. It means when the package's size is bigger, I don't have to change the out_length! Please help me.

int Decompress(BYTE *src, int srcLen, BYTE *dst)
{
static char dummy_head[2] = 
{
    0x8 + 0x7 * 0x10,
    (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
};

int out_length = 1024;  
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = (Bytef *)src;
strm.avail_in = srcLen;    
strm.next_out = (Bytef *)dst;
strm.avail_out = out_length;

/** 15 window bits, and the +32 tells zlib to to detect if using gzip or zlib **/
int ret = inflateInit2(&strm, 15 + 32);
if (ret != Z_OK) 
{
    return -1;
}
while (strm.total_out < out_length && strm.total_in < srcLen) 
{
/* force small buffers */
    strm.avail_in = strm.avail_out = 1; 
    if((ret = inflate(&strm, Z_NO_FLUSH)) == Z_STREAM_END) 
    {
        break;
    }           
    if(ret != Z_OK )
    {
        if(ret == Z_DATA_ERROR)
        {
            strm.next_in = (Bytef*) dummy_head;
            strm.avail_in = sizeof(dummy_head);
            if((ret = inflate(&strm, Z_NO_FLUSH)) != Z_OK) 
            {
                return -1;
            }
        }
        else 
        {
            return -1;
        }
    }
}
// realease the memory for z_stream
if (inflateEnd(&strm) != Z_OK) 
{
    return -1;
}

return strm.total_out;
}
도움이 되었습니까?

해결책

Look at this example for how to use inflate() with fixed-size buffers. If I understand what you're asking, that example reuses the same output buffer until all of the data is decompressed. You then have to do something with the uncompressed data in the output buffer each time, since it will be overwritten the next time. Your Decompress() function doesn't do anything with the uncompressed data.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top