Repository navigation
Replies: 3 comments 5 replies
|
There are two separate things to fix here. In the first compressor, a single The second log already says the input length is zero. Check the input file before rerunning. In both linked gist versions, I put together a complete file round-trip example with separate new output paths, streaming compression/decompression and a full byte comparison. Its 12 CLI tests pass with zlib 1.4, including empty, binary and multichunk inputs; Python also independently decodes the raw output. Huffman-only is the encoder strategy here—the output is still a DEFLATE stream, and the decoder needs no special Huffman-only mode. |
|
I ran your gist and got your output. |
|
Final (working) version turns out as follows: long zlib_compress_huff(char *inbuf, size_t insize, char *outbuf, size_t outsize, int level)
{
int err;
long complen;
z_stream strm; strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL;
err = deflateInit2(&strm, level, Z_DEFLATED, -15, 8, Z_HUFFMAN_ONLY);
if (err != Z_OK)
return -1;
strm.next_in = (Bytef *)inbuf;
strm.avail_in = (uInt)insize;
strm.next_out = (Bytef *)outbuf;
strm.avail_out = (uInt)outsize;
do {
err = deflate(&strm, Z_FINISH);
} while (err == Z_OK);
if (err != Z_STREAM_END)
{
deflateEnd(&strm);
return -1;
}
complen = (long)strm.total_out;
deflateEnd(&strm);
return complen;
}
long zlib_decompress_huff(char *inbuf, size_t insize, char *outbuf, size_t outsize, int level)
{
int err;
long result;
z_stream strm;
memset(&strm, 0, sizeof(strm));
if (inflateInit2(&strm, -15) != Z_OK)
return -1;
strm.next_in = (Bytef *)inbuf;
strm.avail_in = (uInt)insize;
strm.next_out = (Bytef *)outbuf;
strm.avail_out = (uInt)outsize;
do {
err = inflate(&strm, Z_NO_FLUSH);
} while (err == Z_OK && strm.avail_out > 0 && strm.avail_in > 0);
if (err == Z_STREAM_END)
result = (long)strm.total_out;
else
result = -2;
inflateEnd(&strm);
return result;
}Thanks for help. I wouldn't do it without you. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I tried 'regular' compression and it worked (zlib_compress()), but when switched to huffman-only I got stuck.
Apparently result is 0.
Even given in to ai, but it didn't work either.
Full program, ready to test in gist: ver.1, ver.2.
Any idea what is wrong with that code?
All reactions