1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-29 13:31:59 +00:00
cc65/libsrc/zlib/uncompress.c
cuz f2967f26fc Update from Piotr
git-svn-id: svn://svn.cc65.org/cc65/trunk@1124 b7a2c559-68d2-44c3-8de9-860c34a00d81
2001-11-18 20:05:57 +00:00

37 lines
1.1 KiB
C

/*
* uncompress.c
*
* Piotr Fusik, 18.11.2001
*/
#include <zlib.h>
int uncompress (char* dest, unsigned* destLen,
const char* source, unsigned sourceLen)
{
unsigned len;
unsigned char* ptr;
unsigned long csum;
/* source[0]: Compression method and flags
bits 0 to 3: Compression method (must be Z_DEFLATED)
bits 4 to 7: Compression info (must be <= 7)
source[1]: Flags
bits 0 to 4: Check bits
bit 5: Preset dictionary (not supported, sorry)
bits 6 to 7: Compression level
*/
if ((source[0] & 0x8f) != Z_DEFLATED || source[1] & 0x20)
return Z_DATA_ERROR;
if ((((unsigned) source[0] << 8) | (unsigned char) source[1]) % 31)
return Z_DATA_ERROR;
*destLen = len = inflatemem(dest, source + 2);
ptr = (unsigned char*) source + sourceLen - 4;
csum = adler32(adler32(0L, Z_NULL, 0), dest, len);
if ((unsigned char) csum != ptr[3]
|| (unsigned char) (csum >> 8) != ptr[2]
|| (unsigned char) (csum >> 16) != ptr[1]
|| (unsigned char) (csum >> 24) != ptr[0])
return Z_DATA_ERROR;
return Z_OK;
}