在zlib中解压缩消息的问题

时间:2015-09-05 17:44:52

标签: c linux zlib compression

我有以下问题。我需要解压缩从这里生成的linux内核消息:https://lkml.org/lkml/2014/3/17/525

内核中消息的压缩如下:

static int __init qr_compr_init(void)
{
    size_t size = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
            zlib_inflate_workspacesize());  
    stream.workspace = vmalloc(size);
    if (!stream.workspace)
        return -ENOMEM;
    return 0;
}

static void qr_compr_exit(void)
{
    vfree(stream.workspace);
}

static int qr_compress(void *in, void *out, size_t inlen, size_t outlen)
{
    int err, ret;
    int i;
    z_stream infstream;

ret = -EIO;

err = qr_compr_init();
if (err != 0)
    goto error;
mutex_lock(&compr_mutex);
err = zlib_deflateInit(&stream, COMPR_LEVEL);
if (err != Z_OK)
    goto error;

stream.avail_in = inlen;
stream.total_in = 0;
stream.next_out = out;
stream.avail_out = outlen;
stream.total_out = 0;

err = zlib_deflate(&stream, Z_FINISH);
if (err != Z_STREAM_END)
    goto error;

err = zlib_deflateEnd(&stream);
if (err != Z_OK)
    goto error;

if (stream.total_out >= stream.total_in)
    goto error;

ret = stream.total_out;

error:
    mutex_unlock(&compr_mutex);
    return ret;
}

它使用内核中的zlib库。

我的解压缩代码如下:

void uncompress_init()
{
    stream.zalloc = Z_NULL;
    stream.zfree = Z_NULL;
    stream.opaque = Z_NULL;
    stream.next_in = NULL;
    stream.avail_in = 0;
}

int uncompress_block(void *dst, int dstlen, void *src, int srclen)
{
    int err;

    stream.next_in = src;
    stream.avail_in = srclen;

    stream.next_out = dst;
    stream.avail_out = dstlen;

    inflateInit(&stream);

    err = inflate(&stream, Z_FINISH);
    if (err != Z_STREAM_END)
        goto err;
    return stream.total_out;

err:
    printf("Error %d while decompressing!\n", err);
    printf("%p(%d)->%p(%d)\n", src, srclen, dst, dstlen);
    return -1;
}

void uncompress_exit()
{
    inflateEnd(&stream);
}

不幸的是,它不起作用。我收到以下错误消息:

compressed string size: 8
compressed string: x廳L#
Error -3 while decompressing!
0x7ffe9be69f68(8)->0x7ffe9be6a360(1024)
Uncompressed size is: 0
Uncompressed string is:

我在内核代码中找到了这个解压缩示例。 我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

-3是数据错误,表示提供了无效的zlib或deflate输入以进行膨胀。因此,你正在喂食膨胀的不是放气的结果。在某些地方,数据被破坏或被错误识别。