Connecting Tech Pros Worldwide Forums | Help | Site Map

uncompressing data in memory

Slaanesh
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi,

I have to uncompress a buffer extracted from a file (not the entire file is
compressed, it begins after the 8th byte). The data has been compressed
using zlib library. My problem is that it doesn't work ;(
I wrote this code the map the content of the file in memory:
****/**gets*file*size**/
****file*=*fopen(src,*"r");
****if*(!file)*{
********return*(0);
****}
****fseek(file,*0,*SEEK_END);
****fileSize*=*ftell(file);
****rewind(file);

****/**maps*file*content*into*memory**/
****data*=*(char**)*mmap(0,*fileSize,*PROT_READ,*M AP_SHARED,*fileno(file),
0);

****fclose(file);


Then I call uncompress function this way:

****uncomprLen*=*fileSize***2;
****dest*=*(unsigned*char**)*calloc(uncomprLen,*si zeof*(char));
****if*(!dest)*{
********return*(0);
****}

****err*=*uncompress(dest,*&uncomprLen,*data + 8,*fileSize - 8);
****if*(err*!=*Z_OK)*{
********/**error**/
********switch*(err)*{
********case*Z_DATA_ERROR:
************fprintf(stderr,*"cannot*uncompress*fil e*(data*corrupted)\n");
************break;
********case*Z_MEM_ERROR:
************fprintf(stderr,*"cannot*uncompress*fil e*(not*enough*memory)\n");
************break;
********case*Z_BUF_ERROR:
************fprintf(stderr,*"cannot*uncompress*fil e*(not*enough*room*in*the
output buffer)\n");
************break;
********default:
************fprintf(stderr,*"cannot*uncompress*fil e,*unknown*error*code:
%d\n", err);
********}
********return*(0);
****}
****return*(1);

My problem is I always get the Z_DATA_ERROR return code whatever data I gave
to uncompress....
So if someone could help me, it would by very nice ;)

--
Slaanesh

John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: uncompressing data in memory



"Slaanesh" <slaanesh@no-log.org> wrote in message
news:cb7oq7$h20$2@news-reader1.wanadoo.fr...[color=blue]
> Hi,
>
> I have to uncompress a buffer extracted from a file (not the entire file[/color]
is[color=blue]
> compressed, it begins after the 8th byte). The data has been compressed
> using zlib library. My problem is that it doesn't work ;(
> I wrote this code the map the content of the file in memory:
> /* gets file size */
> file = fopen(src, "r");
> if (!file) {
> return (0);
> }
> fseek(file, 0, SEEK_END);
> fileSize = ftell(file);
> rewind(file);
>
> /* maps file content into memory */
> data = (char *) mmap(0, fileSize, PROT_READ, MAP_SHARED, fileno(file),
> 0);
>
> fclose(file);
>
>
> Then I call uncompress function this way:
>
> uncomprLen = fileSize * 2;
> dest = (unsigned char *) calloc(uncomprLen, sizeof (char));
> if (!dest) {
> return (0);
> }
>
> err = uncompress(dest, &uncomprLen, data + 8, fileSize - 8);
> if (err != Z_OK) {
> /* error */
> switch (err) {
> case Z_DATA_ERROR:
> fprintf(stderr, "cannot uncompress file (data corrupted)\n");
> break;
> case Z_MEM_ERROR:
> fprintf(stderr, "cannot uncompress file (not enough memory)\n");
> break;
> case Z_BUF_ERROR:
> fprintf(stderr, "cannot uncompress file (not enough room in the
> output buffer)\n");
> break;
> default:
> fprintf(stderr, "cannot uncompress file, unknown error code:
> %d\n", err);
> }
> return (0);
> }
> return (1);
>
> My problem is I always get the Z_DATA_ERROR return code whatever data I[/color]
gave[color=blue]
> to uncompress....
> So if someone could help me, it would by very nice ;)
>
> --
> Slaanesh[/color]

You should open the file in binary mode

file = fopen(src, "rb");

However I'm guess that you are working on Linux or Unix so that probably
isn't why your code is failing.

First I would simplify, remove the call to mmap and just read the file
contents into memory in the usual way. I don't know enough about mmap to
know whether you are using it correctly or not, but it is one thing to
eliminate.

If that doesn't help then the zlib source is freely available, I suggest
that you get hold of that and use your debugger to step into the zlib code
to find out why things are really failing instead of just guessing. Don't
forget that it could be because the data has been incorrectly compressed in
the first place.

john


Slaanesh
Guest
 
Posts: n/a
#3: Jul 22 '05

re: uncompressing data in memory


John Harrison wrote:[color=blue]
> You should open the file in binary mode
>
> file = fopen(src, "rb");
>
> However I'm guess that you are working on Linux or Unix so that probably
> isn't why your code is failing.[/color]
Yes I do use GNU/Linux so it isn't why my code is failing
[color=blue]
> First I would simplify, remove the call to mmap and just read the file
> contents into memory in the usual way. I don't know enough about mmap to
> know whether you are using it correctly or not, but it is one thing to
> eliminate.[/color]
You may be right, I haven't tried to replace the mmap call by another way.
In another hand, I've checked the result of the mmap call in a debugger and
it seems to work just fine....
[color=blue]
> If that doesn't help then the zlib source is freely available, I suggest
> that you get hold of that and use your debugger to step into the zlib code
> to find out why things are really failing instead of just guessing.[/color]
Yes I know I could trace over into the zlib code but If I could avoid it, I
would win some time ;)
[color=blue]
> Don't forget that it could be because the data has been incorrectly
> compressed in the first place.[/color]
I'm sure the data has been correctly compressed. By the way, it's a flash
swf file that I can read with my Firefox'x plugin...
I would appreciate the problem doesn't come from my code but it does :(

--
Slaanesh
tom_usenet
Guest
 
Posts: n/a
#4: Jul 22 '05

re: uncompressing data in memory


On Tue, 22 Jun 2004 00:51:50 +0200, Slaanesh <slaanesh@no-log.org>
wrote:
[color=blue]
>Hi,
>
>I have to uncompress a buffer extracted from a file (not the entire file is
>compressed, it begins after the 8th byte). The data has been compressed
>using zlib library. My problem is that it doesn't work ;(
>I wrote this code the map the content of the file in memory:
>****/**gets*file*size**/
>****file*=*fopen(src,*"r");
>****if*(!file)*{
>********return*(0);
>****}
>****fseek(file,*0,*SEEK_END);
>****fileSize*=*ftell(file);
>****rewind(file);
>
>****/**maps*file*content*into*memory**/
>****data*=*(char**)*mmap(0,*fileSize,*PROT_READ,* MAP_SHARED,*fileno(file),
>0);
>
>****fclose(file);[/color]

Surely closing the file is going to mess up the memory mapping? This
is off-topic in any case - try over in comp.unix.programmer.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Branimir Maksimovic
Guest
 
Posts: n/a
#5: Jul 22 '05

re: uncompressing data in memory



"Slaanesh" <slaanesh@no-log.org> wrote in message
news:cb7oq7$h20$2@news-reader1.wanadoo.fr...[color=blue]
> Hi,
>
> I have to uncompress a buffer extracted from a file (not the entire file[/color]
is[color=blue]
> compressed, it begins after the 8th byte). The data has been compressed
> using zlib library. My problem is that it doesn't work ;([/color]

Do you have compression code?

Greetings, Bane.


Closed Thread