GRASS GIS 7 Programmer's Manual  7.4.0(2018)-exported
cmprzlib.c
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  * -- GRASS Development Team --
4  *
5  * MODULE: GRASS gis library
6  * FILENAME: cmprzlib.c
7  * AUTHOR(S): Eric G. Miller <egm2@jps.net>
8  * Markus Metz
9  * PURPOSE: To provide an interface to libz for compressing and
10  * decompressing data using DEFLATE. It's primary use is in
11  * the storage and reading of GRASS floating point rasters.
12  * It replaces the patented LZW compression interface.
13  *
14  * ALGORITHM: http://www.gzip.org/zlib/feldspar.html
15  * DATE CREATED: Dec 17 2015
16  * COPYRIGHT: (C) 2015 by the GRASS Development Team
17  *
18  * This program is free software under the GNU General Public
19  * License (version 2 or greater). Read the file COPYING that
20  * comes with GRASS for details.
21  *
22  *****************************************************************************/
23 
24 /********************************************************************
25  * int *
26  * G_zlib_compress (src, srz_sz, dst, dst_sz) *
27  * int src_sz, dst_sz; *
28  * unsigned char *src, *dst; *
29  * ---------------------------------------------------------------- *
30  * This function is a wrapper around the zlib deflate() function. *
31  * It uses an all or nothing call to deflate(). If you need a *
32  * continuous compression scheme, you'll have to code your own. *
33  * In order to do a single pass compression, the input src must be *
34  * copied to a buffer 1% + 12 bytes larger than the data. This may *
35  * cause performance degradation. *
36  * *
37  * The function either returns the number of bytes of compressed *
38  * data in dst, or an error code. *
39  * *
40  * Errors include: *
41  * -1 -- Compression failed. *
42  * -2 -- dst is too small. *
43  * *
44  * ================================================================ *
45  * int *
46  * G_zlib_expand (src, src_sz, dst, dst_sz) *
47  * int src_sz, dst_sz; *
48  * unsigned char *src, *dst; *
49  * ---------------------------------------------------------------- *
50  * This function is a wrapper around the zlib inflate() function. *
51  * It uses a single pass call to inflate(). If you need a contin- *
52  * uous expansion scheme, you'll have to code your own. *
53  * *
54  * The function returns the number of bytes expanded into 'dst' or *
55  * and error code. *
56  * *
57  * Errors include: *
58  * -1 -- Expansion failed. *
59  * *
60  ********************************************************************
61  */
62 
63 #include <grass/config.h>
64 
65 #ifndef HAVE_ZLIB_H
66 
67 #error "GRASS requires libz to compile"
68 
69 #else
70 
71 #include <zlib.h>
72 #include <grass/gis.h>
73 #include <grass/glocale.h>
74 
75 #include "G.h"
76 
77 static void _init_zstruct(z_stream * z)
78 {
79  /* The types are defined in zlib.h, we set to NULL so zlib uses
80  * its default functions.
81  */
82  z->zalloc = (alloc_func) 0;
83  z->zfree = (free_func) 0;
84  z->opaque = (voidpf) 0;
85 }
86 
87 
88 int
89 G_zlib_compress(unsigned char *src, int src_sz, unsigned char *dst,
90  int dst_sz)
91 {
92  int err, nbytes, buf_sz;
93  unsigned char *buf;
94  z_stream c_stream;
95 
96  /* Catch errors early */
97  if (src == NULL || dst == NULL)
98  return -1;
99 
100  /* Don't do anything if either of these are true */
101  if (src_sz <= 0 || dst_sz <= 0)
102  return 0;
103 
104  /* Output buffer has to be 1% + 12 bytes bigger for single pass deflate */
105  /* buf_sz = (int)((double)dst_sz * 1.01 + (double)12); */
106  buf_sz = compressBound(src_sz);
107  if (NULL == (buf = (unsigned char *)
108  G_calloc(buf_sz, sizeof(unsigned char))))
109  return -1;
110 
111  /* Set-up for default zlib memory handling */
112  _init_zstruct(&c_stream);
113 
114  /* Set-up the stream */
115  c_stream.avail_in = src_sz;
116  c_stream.next_in = (unsigned char *) src;
117  c_stream.avail_out = buf_sz;
118  c_stream.next_out = buf;
119 
120  /* Initialize */
121  /* Valid zlib compression levels -1 - 9 */
122  /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
123  * as used here, 1 gives the best compromise between speed and compression */
124  err = deflateInit(&c_stream, G__.compression_level);
125 
126  /* If there was an error initializing, return -1 */
127  if (err != Z_OK) {
128  G_free(buf);
129  return -1;
130  }
131 
132  /* Do single pass compression */
133  err = deflate(&c_stream, Z_FINISH);
134  if (err != Z_STREAM_END) {
135  switch (err) {
136  case Z_OK: /* Destination too small */
137  G_free(buf);
138  deflateEnd(&c_stream);
139  return -2;
140  break;
141  default: /* Give other error */
142  G_free(buf);
143  deflateEnd(&c_stream);
144  return -1;
145  break;
146  }
147  }
148 
149  /* avail_out is updated to bytes remaining in buf, so bytes of compressed
150  * data is the original size minus that
151  */
152  nbytes = buf_sz - c_stream.avail_out;
153  if (nbytes >= src_sz) {
154  /* compression not possible */
155  G_free(buf);
156  deflateEnd(&c_stream);
157  return -2;
158  }
159  /* Copy the data from buf to dst */
160  for (err = 0; err < nbytes; err++)
161  dst[err] = buf[err];
162 
163  G_free(buf);
164  deflateEnd(&c_stream);
165 
166  return nbytes;
167 } /* G_zlib_compress() */
168 
169 
170 int
171 G_zlib_expand(unsigned char *src, int src_sz, unsigned char *dst,
172  int dst_sz)
173 {
174  int err, nbytes;
175  z_stream c_stream;
176 
177  /* Catch error condition */
178  if (src == NULL || dst == NULL)
179  return -2;
180 
181  /* Don't do anything if either of these are true */
182  if (src_sz <= 0 || dst_sz <= 0)
183  return 0;
184 
185  /* Set-up default zlib memory handling */
186  _init_zstruct(&c_stream);
187 
188  /* Set-up I/O streams */
189  c_stream.avail_in = src_sz;
190  c_stream.next_in = (unsigned char *)src;
191  c_stream.avail_out = dst_sz;
192  c_stream.next_out = dst;
193 
194  /* Call zlib initialization function */
195  err = inflateInit(&c_stream);
196 
197  /* If not Z_OK return error -1 */
198  if (err != Z_OK)
199  return -1;
200 
201  /* Do single pass inflate */
202  err = inflate(&c_stream, Z_FINISH);
203 
204  /* Number of bytes inflated to output stream is
205  * original bytes available minus what avail_out now says
206  */
207  nbytes = dst_sz - c_stream.avail_out;
208 
209  /* Z_STREAM_END means all input was consumed,
210  * Z_OK means only some was processed (not enough room in dst)
211  */
212  if (!(err == Z_STREAM_END || err == Z_OK)) {
213  if (!(err == Z_BUF_ERROR && nbytes == dst_sz)) {
214  inflateEnd(&c_stream);
215  return -1;
216  }
217  /* Else, there was extra input, but requested output size was
218  * decompressed successfully.
219  */
220  }
221 
222  inflateEnd(&c_stream);
223 
224  return nbytes;
225 } /* G_zlib_expand() */
226 
227 
228 #endif /* HAVE_ZLIB_H */
229 
230 
231 /* vim: set softtabstop=4 shiftwidth=4 expandtab: */
char * dst
Definition: lz4.h:354
#define NULL
Definition: ccmath.h:32
int compression_level
Definition: G.h:9
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
Definition: symbol/read.c:220
Definition: G.h:4
void G_free(void *buf)
Free allocated memory.
Definition: alloc.c:149