简体   繁体   中英

ZLIB uncompress

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

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!

I post here the code:

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!
}

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)

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

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() will only decompress zlib-wrapped deflate data. It will not decompress gzip-wrapped data or raw deflate data.

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.

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.

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

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.

Why in your GetProcAddress use 'uncompress', but trying decompress? You obtain address of 'uncompress' routine!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM