Hi Stefan

I've posted my algorithm to use the zlib some time age, cause i didn't
find it I'm posting it again.
If youre having some problems with it, just ask...

#define SH(p) ((unsigned short int)(unsigned char)((p)[0]) | ((unsigned short int)(unsigned char)((p)[1]) << 8))

#define LG(p) ((unsigned long)(SH(p)) | ((unsigned long)(SH((p)+2)) << 16))

#define GZPMAG 0xDA78 /* two-byte zlib lead-in */ #define BSIZ 2000 /*maxsize of the read-buffer for compression and output-buffer for decompression*/ #define ZBSIZ (BSIZ/4) /*maxsize of the compression output-buffer and decompression read-buffer*/
#define CRCVAL_INITIAL  0L            /*init value for the crc-generation*/
#define    VN_GRUND_LEN        20

/*****a little function to read 1 Character from a file*******/
int FileGetC(FileHand file)
{
 unsigned char readchar;
 if (1 != FileRead(file, &readchar, 1, 1, NULL))
   return -1;
 return readchar;
}


/*************************** zipping ********************/

char gzhead[10] = {0x1f , 0x8b, 8 , 0,0,0,0,0,0,3 };//needed header for the zipped file

int gzip(FileHand inf, FileHand outf)
{
 z_stream                 gzipit;
 int                         readgot, isav;
 unsigned long     crc32val, itotal, total = 0;
 unsigned char     ibuf[BSIZ], obuf[ZBSIZ];


/*to use the deflate function the gzipit structure has to be initialised*/
 memset(&gzipit, 0, sizeof(gzipit));

 FileTell(inf,(long *)&itotal, NULL);

 crc32val = CRCVAL_INITIAL;

 gzipit.next_out = ibuf;
 gzipit.avail_out = BSIZ;

/*initialise all needed structures*/
 ZLSetup;
if(deflateInit2(&gzipit, /* level */ 8, 8,/* bits */ 13, /* memlevel */ 6, 0) != Z_OK)return -1;
 readgot = ZBSIZ;


/*read the file as long as ZBSIZ long pieces can be read*/
 while (!FileEOF(inf) && readgot == ZBSIZ)
 {
   obuf[0]=chrNull;
   if (0 >= (readgot = FileRead(inf, obuf,1 , ZBSIZ, NULL)))
     break;

   crc32val = crc32(crc32val, obuf, readgot);
   total += readgot;

/*set the next-in next-out pointer, compress the data and write it into the outf file*/
   gzipit.next_in = obuf;
   gzipit.avail_in =readgot;

   while (gzipit.avail_in || readgot !=ZBSIZ)
   {
isav = deflate(&gzipit, readgot == ZBSIZ ? 0 : Z_FINISH); if(isav<0)
     {
         deflateEnd(&gzipit);
         return -1;
     }
if( gzipit.avail_out == BSIZ ) /* no bytes added */
                                   break;
     FileWrite(outf, ibuf, 1, BSIZ - gzipit.avail_out, NULL);

     gzipit.next_out = ibuf;
     gzipit.avail_out = BSIZ;
}

 }

/*the rest of the data that is smaller as ZBSIZ has to be compressed*/
 isav = deflate(&gzipit, Z_FINISH);
FileWrite(outf, ibuf, 1, BSIZ - gzipit.avail_out, NULL); if(isav<0)
 {
     deflateEnd(&gzipit);
     return -1;
 }

/*if any error occured the function exits immediately*/
 isav = deflateEnd(&gzipit);
 if(isav<0)
     return -1;

 memcpy(ibuf,&crc32val,4);
 itotal = LG(ibuf);
 FileWrite(outf, &itotal, 1, 4, NULL);
 memcpy(ibuf,&total,4);
 itotal = LG(ibuf);
 FileWrite(outf, &itotal, 1, 4, NULL);

 return 0;
}


/*******************unzipping***************************/

int unzip(FileHand ifd,FileHand ofd)
{
 int                                 k;
 UInt16                         cardno = 0;
 UInt32                         got;
 z_stream                     pgpz;
 unsigned char         ibuf[ZBSIZ], obuf[BSIZ];

 FileRewind(ifd);

/*initialise the z_stream structure*/
 memset(&pgpz, 0, sizeof(pgpz));
 ZLSetup;
 k = inflateInit2(&pgpz, 15);

 if(k!=0)
return -1;
 while (!FileEOF(ifd) )
 {
/*reset the whole input and output buffer*/ if(k<0)break;
     memset(ibuf, 0, ZBSIZ);
   memset(obuf, 0, BSIZ - pgpz.avail_out);
got = FileRead(ifd, ibuf, 1, ZBSIZ/4, NULL); /*show the uncompressing algorithm where to start and where to write the data*/
   pgpz.next_in = ibuf;
   pgpz.avail_in = got;

   while (pgpz.avail_in || FileEOF(ifd))
   {
     pgpz.next_out = obuf;
pgpz.avail_out = BSIZ;
/*uncompress the dataand write the output into the ofd-File*/
     k = inflate(&pgpz, FileEOF(ifd) ? Z_FULL_FLUSH : 0);

     FileWrite(ofd, obuf, 1, BSIZ - pgpz.avail_out, NULL);

/*if the outputbuffer wasn't big enough or the output buffer wasn touched or the file is at an end - then continue*/
     if (k == Z_BUF_ERROR && pgpz.avail_out != BSIZ && FileEOF(ifd))
       continue;

/*if the inflate didn't work the break the while-cycle*/ if (k != Z_OK)
       break;
   }
 }
 if(k<0)
 {
   k = inflateEnd(&pgpz);
     return -1;
 }

/*tell the decompress algorithm that it's time has come*/
 k = inflateEnd(&pgpz);

/* if the ending failed return an errorcode  */
 if(k<0)
 {
   k = inflateEnd(&pgpz);
     return -1;
 }
 else
     return  0;
}


/**********************************************************************************/

Stefan Kolb wrote:
Hello,

my application receives data via tcp/IP over gsm. bacause of the low
transmission rate i would like to uses zip compression. Where can I fing any
examples how to use zlib?


Thanks



Stefan





______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email ______________________________________________________________________

--
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/

Reply via email to