Example #1
0
int zlib_decomp(int len, char *in, char **out)
{
     int oavail = 0, olen = 0;     
     int err;

     zi.next_in = (void *) in;
     zi.avail_in = len;
     zi.next_out = (void *) zbuf;
     zi.avail_out = zbuf_size;

     while(1) {
        oavail = zi.avail_out;
        if( (err=inflate(&zi, Z_SYNC_FLUSH)) != Z_OK ) {
           vtun_syslog(LOG_ERR,"Inflate error %d len %d", err, len);
           return -1;
        }
        olen += oavail - zi.avail_out;
        if(!zi.avail_in)
	   break;
        if( expand_zbuf(&zi,100) ) {
	   vtun_syslog( LOG_ERR, "Can't expand compression buffer");
           return -1;
	}
     }
     *out = (void *) zbuf;
     return olen;
}
Example #2
0
/* 
 * This functions _MUST_ consume all incoming bytes in one pass,
 * That's why we expand buffer dynamically.
 * Practice shows that buffer will not grow larger that 16K.
 */  
int zlib_comp(int len, char *in, char **out)
{ 
     int oavail, olen = 0;    
     int err;
 
     zd.next_in = (void *) in;
     zd.avail_in = len;
     zd.next_out = (void *) zbuf;
     zd.avail_out = zbuf_size;
    
     while(1) {
        oavail = zd.avail_out;
        if( (err=deflate(&zd, Z_SYNC_FLUSH)) != Z_OK ){
           vlog(LOG_ERR,"Deflate error %d",err);
           return -1;
        }
        olen += oavail - zd.avail_out;
        if(!zd.avail_in)
	   break;

        if( expand_zbuf(&zd,100) ) {
	   vlog( LOG_ERR, "Can't expand compression buffer");
           return -1;
	}
     }
     *out = (void *) zbuf;
     return olen;
}