LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Need an adice on using zlib API functions inside Linux kernel (https://www.linuxquestions.org/questions/linux-software-2/need-an-adice-on-using-zlib-api-functions-inside-linux-kernel-4175484581/)

mlugov 11-14-2013 11:24 AM

Need an adice on using zlib API functions inside Linux kernel
 
Hi, all.

In my Linux driver, I have a simple array of bytes, and I want to compress it, and then de-compress the packet to get back the original string of bytes.

I wote the following code and it doesn't work. After deflate returns, in the 'out' array there's always the same set of 2 bytes.
Can somebody tell, what's missing here? I feel like I miss some tiny detail that prevents the code from working:

z_streamp strm_def,strm_inf;

strm_def = kmalloc(sizeof(struct z_stream_s), GFP_KERNEL);
strm_def->workspace = kmalloc(zlib_deflate_workspacesize(MAX_WBITS,MAX_MEM_LEVEL), GFP_KERNEL);
if (strm_def->workspace == NULL)
printk("snull: Zip workspace is NULL!!!\n");
strm_def->next_in = NULL;

strm_inf = kmalloc(sizeof(struct z_stream_s), GFP_KERNEL);
strm_inf->workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
if (strm_inf->workspace == NULL)
printk("snull: Zip workspace is NULL!!!\n");
strm_inf->next_in = NULL;

unsigned char in[CUT_PACKET_LENGTH];
unsigned char out[CUT_PACKET_LENGTH];

memset(in,0,CUT_PACKET_LENGTH);
memset(out,0,CUT_PACKET_LENGTH);

ret = zlib_deflateInit2(strm_def, Z_DEFAULT_COMPRESSION,Z_DEFLATED, MAX_WBITS,MAX_MEM_LEVEL,Z_DEFAULT_STRATEGY);
if (ret != Z_OK)
{
printk("INIT UN-SUCCESSFULL!!!\n");
return;
}
memcpy(in, pkt->cut_data, pkt->cut_datalen);
strm_def->next_in = in;
strm_def->avail_out = CUT_PACKET_LENGTH;
strm_def->next_out = out;
strm_def->avail_in = pkt->cut_datalen;

ret = zlib_deflate(strm_def, Z_NO_FLUSH);
if (ret != Z_OK)
{
printk("COMPRESSION UN-SUCCESSFULL!!!, still left %d\n", strm_def->avail_in);
return;
}

have = CUT_PACKET_LENGTH - strm_def->avail_out;
printk("len is %i\n" KERN_DEBUG "data:",have);
for (i=0 ; i<have; i++)
printk(" %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i]&0xff);
printk("\n");

ret = zlib_inflateInit2(strm_inf, MAX_WBITS);
if (ret != Z_OK)
{
printk("INIT UN-SUCCESSFULL!!!\n");
return;
}

memset(in,0,CUT_PACKET_LENGTH);
memcpy(in, out, have);
memset(out,0,CUT_PACKET_LENGTH);
strm_inf->next_in = in;
strm_inf->avail_out = CUT_PACKET_LENGTH;
strm_inf->next_out = out;
strm_inf->avail_in = have;

ret = zlib_inflate(strm_inf, Z_NO_FLUSH);

have = CUT_PACKET_LENGTH - strm_inf->avail_out;
printk("len is %i\n" KERN_DEBUG "data:",have);
for (i=0 ; i<have; i++)
printk(" %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i++]&0xff,out[i]&0xff);
printk("\n");



Thank you very much in advance,
Mark


All times are GMT -5. The time now is 08:27 PM.