GRASS GIS 7 Programmer's Manual  7.0.3(2016)-r00000
flate.c
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  * -- GRASS Development Team --
4  *
5  * MODULE: GRASS gis library
6  * FILENAME: flate.c
7  * AUTHOR(S): Eric G. Miller <egm2@jps.net>
8  * PURPOSE: To provide an interface to libz for compressing and
9  * decompressing data using DEFLATE. It's primary use is in
10  * the storage and reading of GRASS floating point rasters.
11  * It replaces the patented LZW compression interface.
12  *
13  * ALGORITHM: http://www.gzip.org/zlib/feldspar.html
14  * DATE CREATED: Nov 19 2000
15  * COPYRIGHT: (C) 2000 by the GRASS Development Team
16  *
17  * This program is free software under the GNU General Public
18  * License (version 2 or greater). Read the file COPYING that
19  * comes with GRASS for details.
20  *
21  *****************************************************************************/
22 
23 /********************************************************************
24  * int *
25  * G_zlib_read (fd, rbytes, dst, nbytes) *
26  * int fd, rbytes, nbytes; *
27  * unsigned char *dst; *
28  * ---------------------------------------------------------------- *
29  * This is the basic function for reading a compressed chunk of a *
30  * data file. The file descriptor should be in the proper location *
31  * and the 'dst' array should have enough space for the data. *
32  * 'nbytes' is the size of 'dst'. The 'rbytes' parameter is the *
33  * number of bytes to read (knowable from the offsets index). For *
34  * best results, 'nbytes' should be the exact amount of space *
35  * needed for the expansion. Too large a value of nbytes may cause *
36  * more data to be expanded than is desired. *
37  * Returns: The number of bytes decompressed into dst, or an error. *
38  * *
39  * Errors include: *
40  * -1 -- Error Reading or Decompressing data. *
41  * -2 -- Not enough space in dst. You must make dst larger *
42  * and then call the function again (remembering to *
43  * reset the file descriptor to it's proper location. *
44  * *
45  * ================================================================ *
46  * int *
47  * G_zlib_write (fd, src, nbytes) *
48  * int fd, nbytes; *
49  * unsigned char *src; *
50  * ---------------------------------------------------------------- *
51  * This is the basic function for writing and compressing a data *
52  * chunk to a file. The file descriptor should be in the correct *
53  * location prior to this call. The function will compress 'nbytes' *
54  * of 'src' and write it to the file 'fd'. Returns the number of *
55  * bytes written or an error code: *
56  * *
57  * Errors include: *
58  * -1 -- Compression Failed. *
59  * -2 -- Unable to write to file. *
60  * *
61  * ================================================================ *
62  * int *
63  * G_zlib_write_noCompress (fd, src, nbytes) *
64  * int fd, nbytes; *
65  * unsigned char *src; *
66  * ---------------------------------------------------------------- *
67  * Works similar to G_zlib_write() except no attempt at compression *
68  * is made. This is quicker, but may result in larger files. *
69  * Returns the number of bytes written, or -1 for an error. It will *
70  * return an error if it fails to write nbytes. Otherwise, the *
71  * return value will always be nbytes + 1 (for compression flag). *
72  * *
73  * ================================================================ *
74  * int *
75  * G_zlib_compress (src, srz_sz, dst, dst_sz) *
76  * int src_sz, dst_sz; *
77  * unsigned char *src, *dst; *
78  * ---------------------------------------------------------------- *
79  * This function is a wrapper around the zlib deflate() function. *
80  * It uses an all or nothing call to deflate(). If you need a *
81  * continuous compression scheme, you'll have to code your own. *
82  * In order to do a single pass compression, the input src must be *
83  * copied to a buffer 1% + 12 bytes larger than the data. This may *
84  * cause performance degradation. *
85  * *
86  * The function either returns the number of bytes of compressed *
87  * data in dst, or an error code. *
88  * *
89  * Errors include: *
90  * -1 -- Compression failed. *
91  * -2 -- dst is too small. *
92  * *
93  * ================================================================ *
94  * int *
95  * G_zlib_expand (src, src_sz, dst, dst_sz) *
96  * int src_sz, dst_sz; *
97  * unsigned char *src, *dst; *
98  * ---------------------------------------------------------------- *
99  * This function is a wrapper around the zlib inflate() function. *
100  * It uses a single pass call to inflate(). If you need a contin- *
101  * uous expansion scheme, you'll have to code your own. *
102  * *
103  * The function returns the number of bytes expanded into 'dst' or *
104  * and error code. *
105  * *
106  * Errors include: *
107  * -1 -- Expansion failed. *
108  * *
109  ********************************************************************
110  */
111 
112 #include <grass/config.h>
113 
114 #ifndef HAVE_ZLIB_H
115 
116 #error "GRASS requires libz to compile"
117 
118 #else
119 
120 #include <zlib.h>
121 #include <stdio.h>
122 #include <stdlib.h>
123 #include <unistd.h>
124 #include <grass/gis.h>
125 
126 #include "G.h"
127 
128 #define G_ZLIB_COMPRESSED_NO (unsigned char)'0'
129 #define G_ZLIB_COMPRESSED_YES (unsigned char)'1'
130 
131 static void _init_zstruct(z_stream * z)
132 {
133  /* The types are defined in zlib.h, we set to NULL so zlib uses
134  * its default functions.
135  */
136  z->zalloc = (alloc_func) 0;
137  z->zfree = (free_func) 0;
138  z->opaque = (voidpf) 0;
139 }
140 
141 int G_zlib_read(int fd, int rbytes, unsigned char *dst, int nbytes)
142 {
143  int bsize, nread, err;
144  unsigned char *b;
145 
146  if (dst == NULL || nbytes < 0)
147  return -2;
148 
149  bsize = rbytes;
150 
151  /* Our temporary input buffer for read */
152  if (NULL == (b = (unsigned char *)
153  G_calloc(bsize, sizeof(unsigned char))))
154  return -1;
155 
156  /* Read from the file until we get our bsize or an error */
157  nread = 0;
158  do {
159  err = read(fd, b + nread, bsize - nread);
160  if (err >= 0)
161  nread += err;
162  } while (err > 0 && nread < bsize);
163 
164  /* If the bsize if less than rbytes and we didn't get an error.. */
165  if (nread < rbytes && err > 0) {
166  G_free(b);
167  return -1;
168  }
169 
170  /* Test if row is compressed */
171  if (b[0] == G_ZLIB_COMPRESSED_NO) {
172  /* Then just copy it to dst */
173  for (err = 0; err < nread - 1 && err < nbytes; err++)
174  dst[err] = b[err + 1];
175 
176  G_free(b);
177  return (nread - 1);
178  }
179  else if (b[0] != G_ZLIB_COMPRESSED_YES) {
180  /* We're not at the start of a row */
181  G_free(b);
182  return -1;
183  }
184  /* Okay it's a compressed row */
185 
186  /* Just call G_zlib_expand() with the buffer we read,
187  * Account for first byte being a flag
188  */
189  err = G_zlib_expand(b + 1, bsize - 1, dst, nbytes);
190 
191  /* We're done with b */
192  G_free(b);
193 
194  /* Return whatever G_zlib_expand() returned */
195  return err;
196 
197 } /* G_zlib_read() */
198 
199 
200 int G_zlib_write(int fd, const unsigned char *src, int nbytes)
201 {
202  int dst_sz, nwritten, err;
203  unsigned char *dst, compressed;
204 
205  /* Catch errors */
206  if (src == NULL || nbytes < 0)
207  return -1;
208 
209  dst_sz = nbytes;
210  if (NULL == (dst = (unsigned char *)
211  G_calloc(dst_sz, sizeof(unsigned char))))
212  return -1;
213 
214  /* Now just call G_zlib_compress() */
215  err = G_zlib_compress(src, nbytes, dst, dst_sz);
216 
217  /* If compression succeeded write compressed row,
218  * otherwise write uncompressed row. Compression will fail
219  * if dst is too small (i.e. compressed data is larger)
220  */
221  if (err > 0 && err <= dst_sz) {
222  dst_sz = err;
223  /* Write the compression flag */
224  compressed = G_ZLIB_COMPRESSED_YES;
225  if (write(fd, &compressed, 1) != 1) {
226  G_free(dst);
227  return -1;
228  }
229  nwritten = 0;
230  do {
231  err = write(fd, dst + nwritten, dst_sz - nwritten);
232  if (err >= 0)
233  nwritten += err;
234  } while (err > 0 && nwritten < dst_sz);
235  /* Account for extra byte */
236  nwritten++;
237  }
238  else {
239  /* Write compression flag */
240  compressed = G_ZLIB_COMPRESSED_NO;
241  if (write(fd, &compressed, 1) != 1) {
242  G_free(dst);
243  return -1;
244  }
245  nwritten = 0;
246  do {
247  err = write(fd, src + nwritten, nbytes - nwritten);
248  if (err >= 0)
249  nwritten += err;
250  } while (err > 0 && nwritten < nbytes);
251  /* Account for extra byte */
252  nwritten++;
253  } /* if (err > 0) */
254 
255  /* Done with the dst buffer */
256  G_free(dst);
257 
258  /* If we didn't write all the data return an error */
259  if (err < 0)
260  return -2;
261 
262  return nwritten;
263 } /* G_zlib_write() */
264 
265 
266 int G_zlib_write_noCompress(int fd, const unsigned char *src, int nbytes)
267 {
268  int err, nwritten;
269  unsigned char compressed;
270 
271  /* Catch errors */
272  if (src == NULL || nbytes < 0)
273  return -1;
274 
275  /* Write the compression flag */
276  compressed = G_ZLIB_COMPRESSED_NO;
277  if (write(fd, &compressed, 1) != 1)
278  return -1;
279 
280  /* Now write the data */
281  nwritten = 0;
282  do {
283  err = write(fd, src + nwritten, nbytes - nwritten);
284  if (err > 0)
285  nwritten += err;
286  } while (err > 0 && nwritten < nbytes);
287 
288  if (err < 0 || nwritten != nbytes)
289  return -1;
290 
291  /* Account for extra compressed flag */
292  nwritten++;
293 
294  /* That's all */
295  return nwritten;
296 
297 } /* G_zlib_write_noCompress() */
298 
299 
300 int
301 G_zlib_compress(const unsigned char *src, int src_sz, unsigned char *dst,
302  int dst_sz)
303 {
304  int err, nbytes, buf_sz;
305  unsigned char *buf;
306  z_stream c_stream;
307 
308  /* Catch errors early */
309  if (src == NULL || dst == NULL)
310  return -1;
311 
312  /* Don't do anything if either of these are true */
313  if (src_sz <= 0 || dst_sz <= 0)
314  return 0;
315 
316  /* Output buffer has to be 1% + 12 bytes bigger for single pass deflate */
317  buf_sz = (int)((double)dst_sz * 1.01 + (double)12);
318  if (NULL == (buf = (unsigned char *)
319  G_calloc(buf_sz, sizeof(unsigned char))))
320  return -1;
321 
322  /* Set-up for default zlib memory handling */
323  _init_zstruct(&c_stream);
324 
325  /* Set-up the stream */
326  c_stream.avail_in = src_sz;
327  c_stream.next_in = (unsigned char *) src;
328  c_stream.avail_out = buf_sz;
329  c_stream.next_out = buf;
330 
331  /* Initialize */
332  /* Valid zlib compression levels -1 - 9 */
333  /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
334  * as used here, 1 gives the best compromise between speed and compression */
335  err = deflateInit(&c_stream,
337  ? 1 : G__.compression_level);
338 
339  /* If there was an error initializing, return -1 */
340  if (err != Z_OK) {
341  G_free(buf);
342  return -1;
343  }
344 
345  /* Do single pass compression */
346  err = deflate(&c_stream, Z_FINISH);
347  if (err != Z_STREAM_END) {
348  switch (err) {
349  case Z_OK: /* Destination too small */
350  G_free(buf);
351  deflateEnd(&c_stream);
352  return -2;
353  break;
354  default: /* Give other error */
355  G_free(buf);
356  deflateEnd(&c_stream);
357  return -1;
358  break;
359  }
360  }
361 
362  /* avail_out is updated to bytes remaining in buf, so bytes of compressed
363  * data is the original size minus that
364  */
365  nbytes = buf_sz - c_stream.avail_out;
366  if (nbytes > dst_sz) { /* Not enough room to copy output */
367  G_free(buf);
368  deflateEnd(&c_stream);
369  return -2;
370  }
371  /* Copy the data from buf to dst */
372  for (err = 0; err < nbytes; err++)
373  dst[err] = buf[err];
374 
375  G_free(buf);
376  deflateEnd(&c_stream);
377 
378  return nbytes;
379 } /* G_zlib_compress() */
380 
381 int
382 G_zlib_expand(const unsigned char *src, int src_sz, unsigned char *dst,
383  int dst_sz)
384 {
385  int err, nbytes;
386  z_stream c_stream;
387 
388  /* Catch error condition */
389  if (src == NULL || dst == NULL)
390  return -2;
391 
392  /* Don't do anything if either of these are true */
393  if (src_sz <= 0 || dst_sz <= 0)
394  return 0;
395 
396  /* Set-up default zlib memory handling */
397  _init_zstruct(&c_stream);
398 
399  /* Set-up I/O streams */
400  c_stream.avail_in = src_sz;
401  c_stream.next_in = (unsigned char *)src;
402  c_stream.avail_out = dst_sz;
403  c_stream.next_out = dst;
404 
405  /* Call zlib initilization function */
406  err = inflateInit(&c_stream);
407 
408  /* If not Z_OK return error -1 */
409  if (err != Z_OK)
410  return -1;
411 
412  /* Do single pass inflate */
413  err = inflate(&c_stream, Z_FINISH);
414 
415  /* Number of bytes inflated to output stream is
416  * original bytes available minus what avail_out now says
417  */
418  nbytes = dst_sz - c_stream.avail_out;
419 
420  /* Z_STREAM_END means all input was consumed,
421  * Z_OK means only some was processed (not enough room in dst)
422  */
423  if (!(err == Z_STREAM_END || err == Z_OK)) {
424  if (!(err == Z_BUF_ERROR && nbytes == dst_sz)) {
425  inflateEnd(&c_stream);
426  return -1;
427  }
428  /* Else, there was extra input, but requested output size was
429  * decompressed successfully.
430  */
431  }
432 
433  inflateEnd(&c_stream);
434 
435  return nbytes;
436 } /* G_zlib_expand() */
437 
438 #endif /* HAVE_ZLIB_H */
439 
440 
441 /* vim: set softtabstop=4 shiftwidth=4 expandtab: */
#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
double b
Definition: G.h:4
void G_free(void *buf)
Free allocated memory.
Definition: alloc.c:149