Check the error codes!!
luk32:gcc -lz ./zlib.c
luk32:~/projects/tests$ ./a.out
Buffer was too small!
Compression is often ineffective for very small inputs. Therefore your prediction for the needed buffer size being 2*strlen(istream)
was an underestimation.
"Improved" zlib.c
to check what went wrong:
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "zlib.h"
int main()
{
const char *istream = "some foo";
ulong destLen = strlen(istream);
char* ostream = malloc(2 * strlen(istream));
int res = compress(ostream, &destLen, istream, destLen + 1);
if(res == Z_BUF_ERROR){
printf("Buffer was too small!\n");
return 1;
}
if(res == Z_MEM_ERROR){
printf("Not enough memory for compression!\n");
return 2;
}
}
After careful reading of "Utility Functions" from documentation. Full correct code:
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "zlib.h"
int main()
{
const char *istream = "some foo";
ulong srcLen = strlen(istream)+1; // +1 for the trailing `\0`
ulong destLen = compressBound(srcLen); // this is how you should estimate size
// needed for the buffer
char* ostream = malloc(destLen);
int res = compress(ostream, &destLen, istream, srcLen);
// destLen is now the size of actuall buffer needed for compression
// you don't want to uncompress whole buffer later, just the used part
if(res == Z_BUF_ERROR){
printf("Buffer was too small!\n");
return 1;
}
if(res == Z_MEM_ERROR){
printf("Not enough memory for compression!\n");
return 2;
}
const char *i2stream = ostream;
char* o2stream = malloc(srcLen);
ulong destLen2 = destLen; //destLen is the actual size of the compressed buffer
int des = uncompress(o2stream, &srcLen, i2stream, destLen2);
printf("%s\n", o2stream);
return 0;
}
Test:
luk32:gcc -lz ./zlib.c
luk32:~/projects/tests$ ./a.out
some foo