GRASS GIS 7 Programmer's Manual  7.4.2(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  if (src == NULL)
99  G_warning(_("No source buffer"));
100 
101  if (dst == NULL)
102  G_warning(_("No destination buffer"));
103  return -1;
104  }
105 
106  /* Don't do anything if either of these are true */
107  if (src_sz <= 0 || dst_sz <= 0) {
108  if (src_sz <= 0)
109  G_warning(_("Invalid source buffer size %d"), src_sz);
110  if (dst_sz <= 0)
111  G_warning(_("Invalid destination buffer size %d"), dst_sz);
112  return 0;
113  }
114 
115  /* Output buffer has to be 1% + 12 bytes bigger for single pass deflate */
116  /* buf_sz = (int)((double)dst_sz * 1.01 + (double)12); */
117  buf_sz = compressBound(src_sz);
118  if (NULL == (buf = (unsigned char *)
119  G_calloc(buf_sz, sizeof(unsigned char))))
120  return -1;
121 
122  /* Set-up for default zlib memory handling */
123  _init_zstruct(&c_stream);
124 
125  /* Set-up the 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;
130 
131  /* Initialize */
132  /* Valid zlib compression levels -1 - 9 */
133  /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
134  * as used here, 1 gives the best compromise between speed and compression */
135  err = deflateInit(&c_stream, G__.compression_level);
136 
137  /* If there was an error initializing, return -1 */
138  if (err != Z_OK) {
139  G_warning(_("ZLIB compression error %d: %s"),
140  (int)err, zError(err));
141  G_free(buf);
142  return -1;
143  }
144 
145  /* Do single pass compression */
146  err = deflate(&c_stream, Z_FINISH);
147  if (err != Z_STREAM_END) {
148  switch (err) {
149  case Z_OK: /* Destination too small */
150  G_free(buf);
151  deflateEnd(&c_stream);
152  return -2;
153  break;
154  default: /* Give other error */
155  G_free(buf);
156  deflateEnd(&c_stream);
157  G_warning(_("ZLIB compression error %d: %s"),
158  (int)err, zError(err));
159  return -1;
160  break;
161  }
162  }
163 
164  /* avail_out is updated to bytes remaining in buf, so bytes of compressed
165  * data is the original size minus that
166  */
167  nbytes = buf_sz - c_stream.avail_out;
168  if (nbytes >= src_sz) {
169  /* compression not possible */
170  G_free(buf);
171  deflateEnd(&c_stream);
172  return -2;
173  }
174  /* Copy the data from buf to dst */
175  for (err = 0; err < nbytes; err++)
176  dst[err] = buf[err];
177 
178  G_free(buf);
179  deflateEnd(&c_stream);
180 
181  return nbytes;
182 } /* G_zlib_compress() */
183 
184 
185 int
186 G_zlib_expand(unsigned char *src, int src_sz, unsigned char *dst,
187  int dst_sz)
188 {
189  int err, nbytes;
190  z_stream c_stream;
191 
192  /* Catch error condition */
193  if (src == NULL || dst == NULL) {
194  if (src == NULL)
195  G_warning(_("No source buffer"));
196 
197  if (dst == NULL)
198  G_warning(_("No destination buffer"));
199  return -2;
200  }
201 
202  /* Don't do anything if either of these are true */
203  if (src_sz <= 0 || dst_sz <= 0) {
204  if (src_sz <= 0)
205  G_warning(_("Invalid source buffer size %d"), src_sz);
206  if (dst_sz <= 0)
207  G_warning(_("Invalid destination buffer size %d"), dst_sz);
208  return 0;
209  }
210 
211  /* Set-up default zlib memory handling */
212  _init_zstruct(&c_stream);
213 
214  /* Set-up I/O streams */
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;
219 
220  /* Call zlib initialization function */
221  err = inflateInit(&c_stream);
222 
223  /* If not Z_OK return error -1 */
224  if (err != Z_OK) {
225  G_warning(_("ZLIB decompression error %d: %s"),
226  err, zError(err));
227  return -1;
228  }
229 
230  /* Do single pass inflate */
231  err = inflate(&c_stream, Z_FINISH);
232 
233  /* Number of bytes inflated to output stream is
234  * original bytes available minus what avail_out now says
235  */
236  nbytes = dst_sz - c_stream.avail_out;
237 
238  /* Z_STREAM_END means all input was consumed,
239  * Z_OK means only some was processed (not enough room in dst)
240  */
241  if (!(err == Z_STREAM_END || err == Z_OK)) {
242  G_warning(_("ZLIB decompression error %d: %s"),
243  err, zError(err));
244  if (!(err == Z_BUF_ERROR && nbytes == dst_sz)) {
245  inflateEnd(&c_stream);
246  return -1;
247  }
248  /* Else, there was extra input, but requested output size was
249  * decompressed successfully.
250  */
251  }
252 
253  inflateEnd(&c_stream);
254 
255  return nbytes;
256 } /* G_zlib_expand() */
257 
258 
259 #endif /* HAVE_ZLIB_H */
260 
261 
262 /* 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
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204