Tag Archives: deflate

zlib

Download zlib-1.2.8.tar.gz

nmake -f win32/Makefile.msc

copy zlib.h zconf.h and zlib.lib

 

int  uncompress3 (int type,Bytef *dest, uLongf *destLen,const Bytef *source,uLong sourceLen) {  int nWindowBites;  if(1==type)  {   nWindowBites=15;  }  else if(2==type)  {   nWindowBites=-15;  }  else  {   nWindowBites=15+16;  }     z_stream stream;     int err;

stream.next_in = (z_const Bytef *)source;     stream.avail_in = (uInt)sourceLen;     /* Check for source > 64K on 16-bit machine: */     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;

stream.next_out = dest;     stream.avail_out = (uInt)*destLen;     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;

stream.zalloc = (alloc_func)0;     stream.zfree = (free_func)0;

//err = inflateInit(&stream);  err = inflateInit2 (&stream,nWindowBites);     if (err != Z_OK) return err;

err = inflate(&stream, Z_FINISH);     if (err != Z_STREAM_END) {         inflateEnd(&stream);         if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))             return Z_DATA_ERROR;         return err;     }     *destLen = stream.total_out;

err = inflateEnd(&stream);     return err; }

int  compress3 (int type,     Bytef *dest,     uLongf *destLen,     const Bytef *source,     uLong sourceLen) {  //type 1 = Deflate, 2= raw, 3 = Gzip  int nWindowBites;  if(1==type)  {   nWindowBites=15;  }  else if(2==type)  {   nWindowBites=-15;  }  else  {   nWindowBites=15+16;  }  z_stream stream;     int err;

stream.next_in = (z_const Bytef *)source;     stream.avail_in = (uInt)sourceLen; #ifdef MAXSEG_64K     /* Check for source > 64K on 16-bit machine: */     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; #endif     stream.next_out = dest;     stream.avail_out = (uInt)*destLen;     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;

stream.zalloc = (alloc_func)0;     stream.zfree = (free_func)0;     stream.opaque = (voidpf)0;

//err = deflateInit(&stream, level);    err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED ,nWindowBites,8,Z_DEFAULT_STRATEGY);

if (err != Z_OK) return err;

err = deflate(&stream, Z_FINISH);     if (err != Z_STREAM_END) {         deflateEnd(&stream);         return err == Z_OK ? Z_BUF_ERROR : err;     }     *destLen = stream.total_out;

err = deflateEnd(&stream);     return err; }

int _tmain(int argc, _TCHAR* argv[]) {  unsigned char arr1[255];  unsigned char arr2[255];  unsigned char arr3[255];  unsigned long nTargetLen=255;  unsigned long nTargetLen2=255;  memset(arr1,0,255);  memset(arr2,0,255);  memcpy(arr1,”hello”,sizeof(“hello”));  int ret=compress3(1,arr2,&nTargetLen,arr1,sizeof(“hello”));

ret=uncompress3(1,arr3,&nTargetLen2,arr2,nTargetLen);

memset(arr3,0,255);  ret=compress3(2,arr2,&nTargetLen,arr1,sizeof(“hello”));

while(Z_BUF_ERROR==uncompress3(2,arr3,&nTargetLen2,arr2,nTargetLen))  {   nTargetLen2*=2;  }  memset(arr3,0,255);

while(Z_BUF_ERROR==compress3(3,arr2,&nTargetLen,arr1,sizeof(“hello”)))  {   nTargetLen*=2;  }  while(Z_BUF_ERROR==uncompress3(3,arr3,&nTargetLen2,arr2,nTargetLen))  {   nTargetLen2*=2;  }

return 0; }