Question

I've written a program, which, supposedly, should compress and then uncompress the string using zlib functions compress() and uncompress(), it compiles just fine, but for some reason, when I start it, some symbols from the uncompressed string are missing - what I get is "some " and then some system symbols. Could anyone help me in finding the mistake here?

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


const char *i2stream = ostream;
char* o2stream = malloc(4 * strlen(istream));
ulong destLen2 = strlen(i2stream);
int des = uncompress(o2stream, &destLen2, i2stream, destLen2);
printf("%s", o2stream);
return 0;
}
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top