A
Andrea Crotti
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.
I tried to understand the zpipe example and modified for me and I got
something like
(where the struct is)
--8<---------------cut here---------------start------------->8---
typdef struct {
unsigned char *stream;
int len;
} payload_t;
--8<---------------cut here---------------end--------------->8---
--8<---------------cut here---------------start------------->8---
// compress original data in the given result
int payload_compress(payload_t data, payload_t *result) {
int ret, flush;
unsigned have;
z_stream strm;
/* allocate deflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit(&strm, LEVEL);
if (ret != Z_OK)
return ret;
// is this thing enough for it?
strm.next_in = data.stream;
ret = deflate(&strm, Z_FINISH); /* no bad return value */
// the initialization was successful
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
deflateEnd(&strm);
return Z_OK;
}
--8<---------------cut here---------------end--------------->8---
what I don't understand is:
- is it just setting "next_in" enough to give the data to the compression?
- how do write to my "result" object?
- why does it say (here http://www.zlib.net/zlib_how.html)
" CHUNK is simply the buffer size for feeding data to and pulling data
from the zlib routines."
does that mean that it should not be the dimension of the packet to
compress but just the max pool where it should work??
And last thing, where do I get how much is the actual data compressed?
I was used too well , in python it was just
data = zlib.compress(data)
Thanks a lot!
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.
I tried to understand the zpipe example and modified for me and I got
something like
(where the struct is)
--8<---------------cut here---------------start------------->8---
typdef struct {
unsigned char *stream;
int len;
} payload_t;
--8<---------------cut here---------------end--------------->8---
--8<---------------cut here---------------start------------->8---
// compress original data in the given result
int payload_compress(payload_t data, payload_t *result) {
int ret, flush;
unsigned have;
z_stream strm;
/* allocate deflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit(&strm, LEVEL);
if (ret != Z_OK)
return ret;
// is this thing enough for it?
strm.next_in = data.stream;
ret = deflate(&strm, Z_FINISH); /* no bad return value */
// the initialization was successful
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
deflateEnd(&strm);
return Z_OK;
}
--8<---------------cut here---------------end--------------->8---
what I don't understand is:
- is it just setting "next_in" enough to give the data to the compression?
- how do write to my "result" object?
- why does it say (here http://www.zlib.net/zlib_how.html)
" CHUNK is simply the buffer size for feeding data to and pulling data
from the zlib routines."
does that mean that it should not be the dimension of the packet to
compress but just the max pool where it should work??
And last thing, where do I get how much is the actual data compressed?
I was used too well , in python it was just
data = zlib.compress(data)
Thanks a lot!