c - 在 gzip 的 zlib 上读取错误时访问冲突

标签 c gzip zlib

我正在尝试使用 zlib 解压缩 c 中的二进制缓冲区

BOOL gzipInflate(char* lpFileIn, DWORD lpFileInSize) {
    z_stream strm;
    char* uncomp, * uncomp2;
    unsigned int uncompLength, half_length;
    int err;
    bool done = false;

    uncompLength = lpFileInSize;
    half_length = lpFileInSize / 2;

    uncomp = (char*)calloc(sizeof(char), uncompLength);

    memset(&strm, 0, sizeof(strm));

    strm.next_in = (Bytef*)lpFileIn;
    strm.avail_in = (uInt)lpFileInSize;
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    strm.total_out = Z_NULL;


    if (inflateInit2(&strm, 16 + MAX_WBITS) != Z_OK) {
        free(uncomp);
        return FALSE;
    }

    while (!done)
    {

        if (strm.total_out >= uncompLength) {
            uncomp2 = (char*)calloc(sizeof(char), uncompLength + half_length);
            memcpy(uncomp2, uncomp, uncompLength);
            uncompLength += half_length;
            free(uncomp);
            uncomp = uncomp2;
        }

        strm.next_out = (Bytef*)(uncomp + strm.total_out);
        strm.avail_out = uncompLength - strm.total_out;


        err = inflate(&strm, Z_SYNC_FLUSH);

        if (err == Z_STREAM_END) {
            done = true;
        }
        else if (err != Z_OK) {
            break;
        }
    }

    if (inflateEnd(&strm) != Z_OK) {
        free(uncomp);
        return FALSE;
    }

    free(uncomp);

    return TRUE;
}

使用随机地址读取时出现访问冲突 我还使用 .NET 中的“GZipStream”来压缩缓冲区并使用 C 对其进行解压缩

原始代码:https://windrealm.org/tutorials/decompress-gzip-stream.php

最佳答案

事实证明,在 Windows 的 Visual Studio 上编译 zlib 时使用“ReleaseWithoutAsm”而不是“Release”,因为 inflate_fast() 在 asm 中存在错误

关于c - 在 gzip 的 zlib 上读取错误时访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59642910/

相关文章:

wget - 如何查看wget源代码?

java - java中字符串数据的压缩和解压

python - 在保持串行读取的同时压缩一系列 JSON 对象?

c - 使用 libsox 对 wav 文件进行下采样

调用内存

apache-spark - 如何使用 spark DF 或 DS 读取 ".gz"压缩文件?

javascript - 错误 : incorrect header check when running post

c++ - 在windows上使用zlib编译boost 1.62/1.63

c++ - 条件运算符的返回值

c - PIC32 - PWM 和正弦波