On Tue, 27 Jul 2010, Andrea Crotti wrote:
> Hi everyone, I would like to compress some data, in practice some ip
> packets that have to be compressed and then sent chunked and sent over
> the network.
>
> I've seen zlib.h and it looks nice, but I have some trouble
> understanding how ti works.
See the manual [0].
The basic idea is this: you provide "input data" in
*next_in .. *(next_in + avail_in - 1)
(inclusive), and provide "output space" in
*next_out .. *(next_out + avail_out - 1)
(inclusive). You call the corresponding (compression or decompression)
routine. In the most common case, the routine returns due to one of these
(normal) conditions:
- output space ran out,
- input data ran out,
- (theoretically possible) both of these at the same time.
You need to "fix" all these conditions before calling the function again
(ie. provide more input if needed, *and* provide more output space if
needed).
The whole loop resembles (to me at least) how iconv() is used, and to some
extent, select(). In some sense, you don't call the (de)compression
function so that it serves you. You rather set an automaton in motion and
care for its needs. It just happens to spit out (de)compressed data. It
resembles "event driven programming", ie. the automaton is a black box and
returns "events" for you to handle ("add more input", "provide more output
space"). You need to look at the input and the output independently.
Disclaimer: I wrote the above mostly from memory, based on what I remember
from the libbzip2 (not zlib) API. Sorry. Still, "[t]he structure of
libbzip2's interfaces is similar to that of Jean-loup Gailly's and Mark
Adler's excellent zlib library" [1].
If you need an upper bound on the compressed size of the plaintext data
before actually doing a single-shot compression (you mentioned Z_FINISH),
see deflateBound() [2] in the zlib docs.
For "packet compression", you might want to consider LZO [3] [4]. For
example, OpenVPN employs "fast LZO compression" -- search [5] for
"comp-lzo".
I'll also mention QuickLZ [6] (their words: "QuickLZ is the world's
fastest compression library (really)"). I didn't test QuickLZ itself, but
tamp [7] is based on it, and tamp was actually faster than anything else
I've seen (for the compression efficiency it provided).
lacos
[0]
http://zlib.net/manual.html
[1]
http://bzip.org/1.0.5/bzip2-manual-1.0.5.html#top-level
[2]
http://zlib.net/manual.html#Advanced
[3]
http://www.oberhumer.com/opensource/lzo/#abstract
[4]
http://www.oberhumer.com/opensource/lzo/#lzop
[5]
http://www.openvpn.net/index.php/ope...penvpn-21.html
[6]
http://www.quicklz.com/
[7]
http://blogs.sun.com/timc/entry/tamp...multi_threaded