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