LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 11-14-2013, 11:24 AM   #1
mlugov
LQ Newbie
 
Registered: Jul 2013
Posts: 4

Rep: Reputation: Disabled
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
 
  


Reply

Tags
linux +


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use functions defined inside the kernel source rico1505 Linux - Kernel 2 02-10-2010 01:20 PM
How to use kernel's crypto API inside the kernel to compute an MD5 hash ? linuxabhinav Linux - Kernel 2 05-28-2008 11:35 AM
Need help finding linux equivalent to windows API functions: SHBrowseForFolder....etc mike1 Programming 2 11-11-2004 06:48 AM
Linux Equivalent for Windows API functions saravanan1979 Linux - General 10 06-18-2002 06:39 AM
Does Linux have API-like functions solartear Programming 8 04-03-2002 12:40 PM


All times are GMT -5. The time now is 04:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration