112 #include <grass/config.h>
116 #error "GRASS requires libz to compile"
124 #include <grass/gis.h>
126 #define G_ZLIB_COMPRESSED_NO (unsigned char)'0'
127 #define G_ZLIB_COMPRESSED_YES (unsigned char)'1'
129 static void _init_zstruct(z_stream * z)
134 z->zalloc = (alloc_func) 0;
135 z->zfree = (free_func) 0;
136 z->opaque = (voidpf) 0;
139 int G_zlib_read(
int fd,
int rbytes,
unsigned char *dst,
int nbytes)
141 int bsize, nread,
err;
144 if (dst ==
NULL || nbytes < 0)
150 if (
NULL == (b = (
unsigned char *)
151 G_calloc(bsize,
sizeof(
unsigned char))))
157 err = read(fd, b + nread, bsize - nread);
160 }
while (err > 0 && nread < bsize);
163 if (nread < rbytes && err > 0) {
169 if (b[0] == G_ZLIB_COMPRESSED_NO) {
171 for (err = 0; err < nread - 1 && err < nbytes; err++)
172 dst[err] = b[err + 1];
177 else if (b[0] != G_ZLIB_COMPRESSED_YES) {
187 err = G_zlib_expand(b + 1, bsize - 1, dst, nbytes);
198 int G_zlib_write(
int fd,
const unsigned char *src,
int nbytes)
200 int dst_sz, nwritten,
err;
201 unsigned char *dst, compressed;
204 if (src ==
NULL || nbytes < 0)
208 if (
NULL == (dst = (
unsigned char *)
209 G_calloc(dst_sz,
sizeof(
unsigned char))))
213 err = G_zlib_compress(src, nbytes, dst, dst_sz);
219 if (err > 0 && err <= dst_sz) {
222 compressed = G_ZLIB_COMPRESSED_YES;
223 if (write(fd, &compressed, 1) != 1) {
229 err = write(fd, dst + nwritten, dst_sz - nwritten);
232 }
while (err > 0 && nwritten < dst_sz);
238 compressed = G_ZLIB_COMPRESSED_NO;
239 if (write(fd, &compressed, 1) != 1) {
245 err = write(fd, src + nwritten, nbytes - nwritten);
248 }
while (err > 0 && nwritten < nbytes);
264 int G_zlib_write_noCompress(
int fd,
const unsigned char *src,
int nbytes)
267 unsigned char compressed;
270 if (src ==
NULL || nbytes < 0)
274 compressed = G_ZLIB_COMPRESSED_NO;
275 if (write(fd, &compressed, 1) != 1)
281 err = write(fd, src + nwritten, nbytes - nwritten);
284 }
while (err > 0 && nwritten < nbytes);
286 if (err < 0 || nwritten != nbytes)
299 G_zlib_compress(
const unsigned char *src,
int src_sz,
unsigned char *dst,
302 int err, nbytes, buf_sz;
311 if (src_sz <= 0 || dst_sz <= 0)
315 buf_sz = (
int)((
double)dst_sz * 1.01 + (double)12);
316 if (
NULL == (buf = (
unsigned char *)
317 G_calloc(buf_sz,
sizeof(
unsigned char))))
321 _init_zstruct(&c_stream);
324 c_stream.avail_in = src_sz;
325 c_stream.next_in = (
char *)src;
326 c_stream.avail_out = buf_sz;
327 c_stream.next_out =
buf;
330 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
339 err = deflate(&c_stream, Z_FINISH);
340 if (err != Z_STREAM_END) {
344 deflateEnd(&c_stream);
349 deflateEnd(&c_stream);
358 nbytes = buf_sz - c_stream.avail_out;
359 if (nbytes > dst_sz) {
364 for (err = 0; err < nbytes; err++)
368 deflateEnd(&c_stream);
374 G_zlib_expand(
const unsigned char *src,
int src_sz,
unsigned char *dst,
385 if (src_sz <= 0 || dst_sz <= 0)
389 _init_zstruct(&c_stream);
392 c_stream.avail_in = src_sz;
393 c_stream.next_in = (
char *)src;
394 c_stream.avail_out = dst_sz;
395 c_stream.next_out = dst;
398 err = inflateInit(&c_stream);
405 err = inflate(&c_stream, Z_FINISH);
410 nbytes = dst_sz - c_stream.avail_out;
415 if (!(err == Z_STREAM_END || err == Z_OK)) {
416 if (!(err == Z_BUF_ERROR && nbytes == dst_sz)) {
417 inflateEnd(&c_stream);
425 inflateEnd(&c_stream);
void G_free(void *buf)
Free allocated memory.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]