uncompressing data in memory

S

Slaanesh

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);


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 gave
to uncompress....
So if someone could help me, it would by very nice ;)
 
J

John Harrison

Slaanesh said:
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);


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 gave
to uncompress....
So if someone could help me, it would by very nice ;)

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
 
S

Slaanesh

John said:
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.
Yes I do use GNU/Linux so it isn't why my 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.
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....
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.
Yes I know I could trace over into the zlib code but If I could avoid it, I
would win some time ;)
Don't forget that it could be because the data has been incorrectly
compressed in the first place.
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 :(
 
T

tom_usenet

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);

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
 
B

Branimir Maksimovic

Slaanesh said:
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 ;(

Do you have compression code?

Greetings, Bane.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top