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