首页 > 解决方案 > 在 zlib 上为 gzip 读取错误时访问冲突

问题描述

我正在尝试使用 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

标签: cgzipzlib

解决方案


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


推荐阅读