3 * Copyright 2009 Daniel Borkmann <dborkma@tik.ee.ethz.ch>
6 * Faculty of Computer Science, Mathematics and Natural Sciences,
7 * Leipzig University of Applied Sciences (HTWK Leipzig)
19 /* Not thread safe! */
20 static z_stream inf
, def
;
22 /* Maximum of a jumbo frame and some overhead */
23 static unsigned char *z_buf
= NULL
;
24 static int z_buf_size
= 9200;
26 int z_alloc_or_maybe_die(int z_level
)
30 /* Usually can be Z_DEFAULT_COMPRESSION */
31 if (z_level
< -1 || z_level
> 9)
42 ret
= deflateInit(&def
, z_level
);
44 panic("Can't initialize zLibs compressor!\n");
46 ret
= inflateInit(&inf
);
48 panic("Can't initialize zLibs decompressor!\n");
50 z_buf
= xmalloc(z_buf_size
);
63 char *z_get_version(void)
68 static void z_buf_expansion_or_die(z_stream
*stream
, size_t size
)
70 z_buf
= xrealloc(z_buf
, 1, z_buf_size
+ size
);
72 stream
->next_out
= z_buf
+ z_buf_size
;
73 stream
->avail_out
= size
;
78 ssize_t
z_deflate(char *src
, size_t size
, char **dst
)
81 size_t todo
, done
= 0;
83 def
.next_in
= (void *) src
;
85 def
.next_out
= (void *) z_buf
;
86 def
.avail_out
= z_buf_size
;
91 ret
= deflate(&def
, Z_SYNC_FLUSH
);
93 whine("Deflate error %d!\n", ret
);
97 done
+= (todo
- def
.avail_out
);
98 if (def
.avail_in
== 0)
101 z_buf_expansion_or_die(&def
, 100);
104 *dst
= (void *) z_buf
;
108 ssize_t
z_inflate(char *src
, size_t size
, char **dst
)
113 inf
.next_in
= (void *) src
;
115 inf
.next_out
= (void *) z_buf
;
116 inf
.avail_out
= z_buf_size
;
119 todo
= inf
.avail_out
;
121 ret
= inflate(&inf
, Z_SYNC_FLUSH
);
123 whine("Inflate error %d!\n", ret
);
127 done
+= (todo
- inf
.avail_out
);
128 if (inf
.avail_in
== 0)
131 z_buf_expansion_or_die(&inf
, 100);
134 *dst
= (void *) z_buf
;