63 #include <grass/config.h> 67 #error "GRASS requires libz to compile" 72 #include <grass/gis.h> 73 #include <grass/glocale.h> 77 static void _init_zstruct(z_stream * z)
82 z->zalloc = (alloc_func) 0;
83 z->zfree = (free_func) 0;
84 z->opaque = (voidpf) 0;
89 G_zlib_compress(
unsigned char *src,
int src_sz,
unsigned char *
dst,
92 int err, nbytes, buf_sz;
107 if (src_sz <= 0 || dst_sz <= 0) {
109 G_warning(_(
"Invalid source buffer size %d"), src_sz);
111 G_warning(_(
"Invalid destination buffer size %d"), dst_sz);
117 buf_sz = compressBound(src_sz);
118 if (
NULL == (buf = (
unsigned char *)
119 G_calloc(buf_sz,
sizeof(
unsigned char))))
123 _init_zstruct(&c_stream);
126 c_stream.avail_in = src_sz;
127 c_stream.next_in = (
unsigned char *) src;
128 c_stream.avail_out = buf_sz;
129 c_stream.next_out = buf;
139 G_warning(_(
"ZLIB compression error %d: %s"),
140 (
int)err, zError(err));
146 err = deflate(&c_stream, Z_FINISH);
147 if (err != Z_STREAM_END) {
151 deflateEnd(&c_stream);
156 deflateEnd(&c_stream);
157 G_warning(_(
"ZLIB compression error %d: %s"),
158 (
int)err, zError(err));
167 nbytes = buf_sz - c_stream.avail_out;
168 if (nbytes >= src_sz) {
171 deflateEnd(&c_stream);
175 for (err = 0; err < nbytes; err++)
179 deflateEnd(&c_stream);
186 G_zlib_expand(
unsigned char *src,
int src_sz,
unsigned char *dst,
203 if (src_sz <= 0 || dst_sz <= 0) {
205 G_warning(_(
"Invalid source buffer size %d"), src_sz);
207 G_warning(_(
"Invalid destination buffer size %d"), dst_sz);
212 _init_zstruct(&c_stream);
215 c_stream.avail_in = src_sz;
216 c_stream.next_in = (
unsigned char *)src;
217 c_stream.avail_out = dst_sz;
218 c_stream.next_out =
dst;
221 err = inflateInit(&c_stream);
225 G_warning(_(
"ZLIB decompression error %d: %s"),
231 err = inflate(&c_stream, Z_FINISH);
236 nbytes = dst_sz - c_stream.avail_out;
241 if (!(err == Z_STREAM_END || err == Z_OK)) {
242 G_warning(_(
"ZLIB decompression error %d: %s"),
244 if (!(err == Z_BUF_ERROR && nbytes == dst_sz)) {
245 inflateEnd(&c_stream);
253 inflateEnd(&c_stream);
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
void G_free(void *buf)
Free allocated memory.
void G_warning(const char *msg,...)
Print a warning message to stderr.