Zlib compression issues - Linux

This is a discussion on Zlib compression issues - Linux ; Hello. I'm having issues with zlib's deflate and inflate functions... Generally, my code using these functions works well. However, in some small cases, either deflate or inflate chops off the last byte of the file. For instance, creating a file ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Zlib compression issues

  1. Zlib compression issues

    Hello.
    I'm having issues with zlib's deflate and inflate functions...
    Generally, my code using these functions works well. However, in some
    small cases, either deflate or inflate chops off the last byte of the
    file.

    For instance, creating a file with the character range of 0-255 like
    this:
    perl -e 'for ($i=0; $i < 255; $i++) { print "%c", $i; }' > ascii

    Within my program, the last byte is chopped off and an md5sum on the
    original ascii file and the outcome of a compression and decompression
    fails. This is because either the deflate or inflate function is
    chopping that last byte off some how.

    My main function is just simply reading in a full file (which is 255
    in length as per the above ascii file output), putting that buffer
    into compress, taking the compressed data and putting it into
    decompress, and writing the decompressed data out to a file. Once
    comparing the original ascii file and the one run through my program,
    the new output file is only 254 in length. This is consistent.

    Any ideas what the problem may be? I've been fighting this code for 2
    days now...

    Here is my compress / decompress code:

    int compress(char *src, uint slen, char **ret, uint *retlen)
    {
    z_stream c_stream;
    int err, nalloc=1;
    Bytef *dst = NULL;

    memset(&c_stream, 0, sizeof(z_stream));
    err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
    if (err != Z_OK) {
    return -1;
    }

    c_stream.next_in = (Bytef *) src;
    c_stream.avail_in = slen;
    dst = (Bytef *) xmalloc((sizeof(Bytef) * 1024);
    c_stream.next_out = dst;
    c_stream.avail_out = 1024;

    do {
    if (c_stream.total_out > 0 && (c_stream.total_out %
    1024) == 0) {
    size_t n = sizeof(Bytef) * 1024 * (nalloc+1);
    dst = (Bytef *)xrealloc(dst, n);
    c_stream.next_out = dst + (1024 * nalloc);
    c_stream.avail_out = n - c_stream.total_out;
    nalloc++;
    }
    err = deflate(&c_stream, Z_NO_FLUSH);
    if (err != Z_OK) {
    break;
    }
    } while (c_stream.avail_out == 0);

    for (; {
    if (c_stream.total_out > 0 && (c_stream.total_out %
    1024) == 0) {
    size_t n = sizeof(Bytef) * 1024 * (nalloc+1);
    dst = (Bytef *)xrealloc(dst, n);
    c_stream.next_out = dst + (1024 * nalloc);
    c_stream.avail_out = n - c_stream.total_out;
    nalloc++;
    }
    err = defalte(&c_stream, Z_FINISH);
    if (err == Z_STREAM_END) {
    break;
    } else if (err != Z_OK) {
    free(dst);
    return -1;
    }
    }

    err = deflateEnd(&c_stream);
    if (err != Z_OK) {
    free(dst);
    return -1;
    }

    *retlen = c_stream.total_out;
    *ret = (char *)dst;

    return 0;
    }

    int decompress(char *src, uint slen, char **ret, uint **retlen)
    {
    z_stream c_stream;
    int err, nalloc=1;
    Bytef *dst=NULL;

    memset(&c_stream, 0, sizeof(z_stream));
    err = inflateInit(&c_stream);
    if (err != Z_OK) {
    return -1;
    }

    c_stream.next_in = (Bytef *)src;
    c_stream.avail_in = slen;
    dst = xmalloc(sizeof(Bytef) * 1024);
    c_stream.next_out = dst;

    do {
    if (c_stream.total_out > 0 && (c_stream.total_out %
    1024) == ) {
    size_t n = sizeof(Bytef) * 1024 * (nalloc +
    1);
    dst = (Bytef *)xrealloc(dst, n);
    c_stream.next_out = dst + (1024 * nalloc);
    c_stream.avail_out = n - c_stream.total_out;
    nalloc++;
    }
    err = inflate(&c_stream, Z_NO_FLUSH);
    if (err != Z_OK && err != Z_STREAM_END) {
    free(dst);
    return -1;
    }
    } while (c_stream.avail_out == 0);

    err = inflateEnd(&c_stream);
    if (err != Z_OK) {
    free(dst);
    return -1;
    }

    *retlen = c_stream.total_out;
    *ret = dst;
    return 0;
    }


  2. Re: Zlib compression issues


    a écrit dans le message de news:
    1193162850.518539.118010@v29g2000prd.googlegroups. com...
    > Hello.
    > I'm having issues with zlib's deflate and inflate functions...
    > Generally, my code using these functions works well. However, in some
    > small cases, either deflate or inflate chops off the last byte of the
    > file.
    >
    > For instance, creating a file with the character range of 0-255 like
    > this:
    > perl -e 'for ($i=0; $i < 255; $i++) { print "%c", $i; }' > ascii
    >
    > Within my program, the last byte is chopped off and an md5sum on the
    > original ascii file and the outcome of a compression and decompression
    > fails. This is because either the deflate or inflate function is
    > chopping that last byte off some how.
    >
    > My main function is just simply reading in a full file (which is 255
    > in length as per the above ascii file output), putting that buffer
    > into compress, taking the compressed data and putting it into
    > decompress, and writing the decompressed data out to a file. Once
    > comparing the original ascii file and the one run through my program,
    > the new output file is only 254 in length. This is consistent.
    >
    > Any ideas what the problem may be? I've been fighting this code for 2
    > days now...
    >
    > Here is my compress / decompress code:
    >
    > int compress(char *src, uint slen, char **ret, uint *retlen)
    > {
    > z_stream c_stream;
    > int err, nalloc=1;
    > Bytef *dst = NULL;
    >
    > memset(&c_stream, 0, sizeof(z_stream));
    > err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
    > if (err != Z_OK) {
    > return -1;
    > }
    >
    > c_stream.next_in = (Bytef *) src;
    > c_stream.avail_in = slen;
    > dst = (Bytef *) xmalloc((sizeof(Bytef) * 1024);
    > c_stream.next_out = dst;
    > c_stream.avail_out = 1024;
    >
    > do {
    > if (c_stream.total_out > 0 && (c_stream.total_out %
    > 1024) == 0) {
    > size_t n = sizeof(Bytef) * 1024 * (nalloc+1);
    > dst = (Bytef *)xrealloc(dst, n);
    > c_stream.next_out = dst + (1024 * nalloc);
    > c_stream.avail_out = n - c_stream.total_out;
    > nalloc++;
    > }
    > err = deflate(&c_stream, Z_NO_FLUSH);
    > if (err != Z_OK) {
    > break;
    > }
    > } while (c_stream.avail_out == 0);
    >
    > for (; {
    > if (c_stream.total_out > 0 && (c_stream.total_out %
    > 1024) == 0) {
    > size_t n = sizeof(Bytef) * 1024 * (nalloc+1);
    > dst = (Bytef *)xrealloc(dst, n);
    > c_stream.next_out = dst + (1024 * nalloc);
    > c_stream.avail_out = n - c_stream.total_out;
    > nalloc++;
    > }
    > err = defalte(&c_stream, Z_FINISH);
    > if (err == Z_STREAM_END) {
    > break;
    > } else if (err != Z_OK) {
    > free(dst);
    > return -1;
    > }
    > }
    >
    > err = deflateEnd(&c_stream);
    > if (err != Z_OK) {
    > free(dst);
    > return -1;
    > }
    >
    > *retlen = c_stream.total_out;
    > *ret = (char *)dst;
    >
    > return 0;
    > }
    >
    > int decompress(char *src, uint slen, char **ret, uint **retlen)
    > {
    > z_stream c_stream;
    > int err, nalloc=1;
    > Bytef *dst=NULL;
    >
    > memset(&c_stream, 0, sizeof(z_stream));
    > err = inflateInit(&c_stream);
    > if (err != Z_OK) {
    > return -1;
    > }
    >
    > c_stream.next_in = (Bytef *)src;
    > c_stream.avail_in = slen;
    > dst = xmalloc(sizeof(Bytef) * 1024);
    > c_stream.next_out = dst;
    >
    > do {
    > if (c_stream.total_out > 0 && (c_stream.total_out %
    > 1024) == ) {
    > size_t n = sizeof(Bytef) * 1024 * (nalloc +
    > 1);
    > dst = (Bytef *)xrealloc(dst, n);
    > c_stream.next_out = dst + (1024 * nalloc);
    > c_stream.avail_out = n - c_stream.total_out;
    > nalloc++;
    > }
    > err = inflate(&c_stream, Z_NO_FLUSH);
    > if (err != Z_OK && err != Z_STREAM_END) {
    > free(dst);
    > return -1;
    > }
    > } while (c_stream.avail_out == 0);
    >
    > err = inflateEnd(&c_stream);
    > if (err != Z_OK) {
    > free(dst);
    > return -1;
    > }
    >
    > *retlen = c_stream.total_out;
    > *ret = dst;
    > return 0;
    > }


    it seems you miss the part with Z_FINISH in your decompress function,
    inflate probably held the byte in its buffer for some reason.

    Armel



+ Reply to Thread