ZLIB解压缩 [英] ZLIB uncompress

查看:304
本文介绍了ZLIB解压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个小型应用程序,该应用程序应解压缩以gzip / deflate格式编码的数据。

I've coded a small application that should uncompress data encoded in the gzip/deflate format.

为了实现这一点,我使用ZLIB库,使用uncompress函数。

In order to accomplish this,I'm using the ZLIB library, using the uncompress function.

问题是该函数不起作用!换句话说,数据不是未压缩的!

The problem is that the function doesn't work! In orher words, data is not uncompressed!

我在这里发布代码:

int (*decompress)(PBYTE,PULONG,PBYTE,ULONG);

void DecodeData(PBYTE data,ULONG dataSize){
  LoadLibrary("C:\\zlib1.dll");

  decompress=(int(*)(PBYTE,PULONG,PBYTE,ULONG))GetProcAddress(
    GetModuleHandle("zlib1.dll"),"uncompress");

  // Yeah I know the size is hardcoded and it's not right, but it's just a test,
  // so nevermind

  PBYTE decompressedData=(PBYTE)VirtualAlloc(NULL,300,MEM_COMMIT|MEM_RESERVE,
    PAGE_EXECUTE_READWRITE);
  ULONG maxSize=250;

  decompress(decompressedData,&maxSize,data,dataSize);

  MessageBox(0,(char*)decompressedData,0,MB_OK);//MessageBox shows no data, it's blank!
}

GetProcAddress成功获取了该函数的指针,问题是函数返回NULL(甚至没有zlib文档中列出的错误)

The pointer to the function is successfully obtained by GetProcAddress, the problem is that the function return NULL(and not even the errors listed in zlib documentation)

推荐答案

uncompress()函数不会解压缩gzip数据,并且取决于您 deflate的含义,它也可能不会解压缩。

The uncompress() function will not decompress gzip data, and depending on what you mean by "deflate", it may not decompress that either.

有三种可能您所指的格式,即实际的压缩数据,可能带有一些短的标题和尾部,以标识流并在末尾提供一些检查数据。如 RFC 1951 所述,存在原始的放气数据。 RFC 1950 定义了zlib包装的压缩数据。有gzip包裹的deflate数据,由 RFC 1952 定义。

There are three possible formats you are referring to, which is the actual compressed data possibly with some short headers and trailers to identify the stream and provide some check data at the end. There is raw deflate data, as described by RFC 1951. There is zlib-wrapped deflate data defined by RFC 1950. There is gzip-wrapped deflate data, defined by RFC 1952.

uncompress()仅解压缩zlib包装的deflate数据。它不会解压缩gzip包装的数据或原始的deflate数据。

uncompress() will only decompress zlib-wrapped deflate data. It will not decompress gzip-wrapped data or raw deflate data.

您未提供要执行的操作的上下文。但是,当您说 gzip / deflate时,我可能会猜测这种组合是指HTTP内容编码选项的名称。在那种情况下,由于名称的不幸选择, deflate内容编码是指zlib包装的deflate数据,而不是原始数据。 uncompress()将解压缩正确交付的HTTP压缩内容编码的数据。

You did not provide the context for what you're trying to do. However when you say "gzip/deflate", I might guess by that combination you are referring to the names of the HTTP content encoding options. In that case, due to an unfortunate choice of name, the "deflate" content encoding refers to zlib-wrapped deflate data, not raw data. uncompress() will decompress properly-delivered HTTP deflate content encoded data.

我说正确交付,因为可能不是。同样,由于不幸地选择了名称,并且Microsoft程序员无法实际读取HTTP规范,当客户端接受deflate内容编码时,IIS服务器将错误地传递原始deflate数据,而不是zlib包装的数据。这导致客户端必须要么尝试尝试对deflate内容编码进行两种解码,然后查看其中一种方法,要么更好的方法就是一开始就不接受deflate编码。如果客户端仅接受gzip内容编码,则没有问题。

I say "properly-delivered", because it may not be. Again due to the unfortunate choice of name, and the inability of Microsoft programmers to actually read the HTTP specification, IIS servers would incorrectly deliver raw deflate data instead of zlib-wrapped data when the client accepted deflate content encoding. This has resulted in clients that must either try to decode deflate content encoding both ways and see if one of them works, or the better approach which is to simply not accept deflate encoding in the first place. If the client only accepts gzip content encoding, then there's no problem.

您可以使用 inflateInit2(),< zlib的code> inflate()和 inflateEnd()函数可解码上述任何格式,即gzip包装的zlib包好,原始的。请阅读 zlib.h 查看操作方法。

You can use inflateInit2(), inflate(), and inflateEnd() functions of zlib to decode any of the formats mentioned, i.e. gzip-wrapped, zlib-wrapped, and raw. Please read the documentation in zlib.h to see how.

顺便说一下, uncompress()函数返回一个整数,而不是指针。因此,当您说该函数返回了 NULL 而不是它应该返回的内容时,我只能假定该 zlib 函数未正确定义。

By the way, the uncompress() function returns an integer, not a pointer. So when you say that the function returned NULL instead of what it was supposed to, I can only assume that the interface to that zlib function was not properly defined.

这篇关于ZLIB解压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆