cmprzlib.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. * int *
  25. * G_zlib_compress (src, srz_sz, dst, dst_sz) *
  26. * int src_sz, dst_sz; *
  27. * unsigned char *src, *dst; *
  28. * ---------------------------------------------------------------- *
  29. * This function is a wrapper around the zlib deflate() function. *
  30. * It uses an all or nothing call to deflate(). If you need a *
  31. * continuous compression scheme, you'll have to code your own. *
  32. * In order to do a single pass compression, the input src must be *
  33. * copied to a buffer 1% + 12 bytes larger than the data. This may *
  34. * cause performance degradation. *
  35. * *
  36. * The function either returns the number of bytes of compressed *
  37. * data in dst, or an error code. *
  38. * *
  39. * Errors include: *
  40. * -1 -- Compression failed. *
  41. * -2 -- dst is too small. *
  42. * *
  43. * ================================================================ *
  44. * int *
  45. * G_zlib_expand (src, src_sz, dst, dst_sz) *
  46. * int src_sz, dst_sz; *
  47. * unsigned char *src, *dst; *
  48. * ---------------------------------------------------------------- *
  49. * This function is a wrapper around the zlib inflate() function. *
  50. * It uses a single pass call to inflate(). If you need a contin- *
  51. * uous expansion scheme, you'll have to code your own. *
  52. * *
  53. * The function returns the number of bytes expanded into 'dst' or *
  54. * and error code. *
  55. * *
  56. * Errors include: *
  57. * -1 -- Expansion failed. *
  58. * *
  59. ********************************************************************
  60. */
  61. #include <grass/config.h>
  62. #ifndef HAVE_ZLIB_H
  63. #error "GRASS requires libz to compile"
  64. #else
  65. #include <zlib.h>
  66. #include <grass/gis.h>
  67. #include <grass/glocale.h>
  68. #include "G.h"
  69. int
  70. G_zlib_compress_bound(int src_sz)
  71. {
  72. /* from zlib.h:
  73. * "when using compress or compress2,
  74. * destLen must be at least the value returned by
  75. * compressBound(sourceLen)"
  76. * no explanation for the "must be"
  77. */
  78. return compressBound(src_sz);
  79. }
  80. int
  81. G_zlib_compress(unsigned char *src, int src_sz, unsigned char *dst,
  82. int dst_sz)
  83. {
  84. uLong err, nbytes, buf_sz;
  85. unsigned char *buf;
  86. /* Catch errors early */
  87. if (src == NULL || dst == NULL) {
  88. if (src == NULL)
  89. G_warning(_("No source buffer"));
  90. if (dst == NULL)
  91. G_warning(_("No destination buffer"));
  92. return -1;
  93. }
  94. /* Don't do anything if either of these are true */
  95. if (src_sz <= 0 || dst_sz <= 0) {
  96. if (src_sz <= 0)
  97. G_warning(_("Invalid source buffer size %d"), src_sz);
  98. if (dst_sz <= 0)
  99. G_warning(_("Invalid destination buffer size %d"), dst_sz);
  100. return 0;
  101. }
  102. /* Output buffer has to be 1% + 12 bytes bigger for single pass deflate */
  103. /* buf_sz = (int)((double)dst_sz * 1.01 + (double)12); */
  104. /* Output buffer should be large enough for single pass compression */
  105. buf = dst;
  106. buf_sz = G_zlib_compress_bound(src_sz);
  107. if (buf_sz > dst_sz) {
  108. G_warning("G_zlib_compress(): programmer error, destination is too small");
  109. if (NULL == (buf = (unsigned char *)
  110. G_calloc(buf_sz, sizeof(unsigned char))))
  111. return -1;
  112. }
  113. else
  114. buf_sz = dst_sz;
  115. /* Valid zlib compression levels -1 - 9 */
  116. /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
  117. * as used here, 1 gives the best compromise between speed and compression */
  118. /* Do single pass compression */
  119. nbytes = buf_sz;
  120. err = compress2((Bytef *)buf, &nbytes, /* destination */
  121. (const Bytef *)src, src_sz, /* source */
  122. G__.compression_level); /* level */
  123. if (err != Z_OK) {
  124. G_warning(_("ZLIB compression error %d: %s"),
  125. (int)err, zError(err));
  126. if (buf != dst)
  127. G_free(buf);
  128. return -1;
  129. }
  130. /* updated buf_sz is bytes of compressed data */
  131. if (nbytes >= src_sz) {
  132. /* compression not possible */
  133. if (buf != dst)
  134. G_free(buf);
  135. return -2;
  136. }
  137. if (buf != dst) {
  138. /* Copy the data from buf to dst */
  139. for (err = 0; err < nbytes; err++)
  140. dst[err] = buf[err];
  141. G_free(buf);
  142. }
  143. return nbytes;
  144. } /* G_zlib_compress() */
  145. int
  146. G_zlib_expand(unsigned char *src, int src_sz, unsigned char *dst,
  147. int dst_sz)
  148. {
  149. int err;
  150. uLong ss, nbytes;
  151. /* Catch error condition */
  152. if (src == NULL || dst == NULL) {
  153. if (src == NULL)
  154. G_warning(_("No source buffer"));
  155. if (dst == NULL)
  156. G_warning(_("No destination buffer"));
  157. return -2;
  158. }
  159. /* Don't do anything if either of these are true */
  160. if (src_sz <= 0 || dst_sz <= 0) {
  161. if (src_sz <= 0)
  162. G_warning(_("Invalid source buffer size %d"), src_sz);
  163. if (dst_sz <= 0)
  164. G_warning(_("Invalid destination buffer size %d"), dst_sz);
  165. return 0;
  166. }
  167. ss = src_sz;
  168. /* Do single pass decompression */
  169. nbytes = dst_sz;
  170. err = uncompress((Bytef *)dst, &nbytes, /* destination */
  171. (const Bytef *)src, ss); /* source */
  172. /* If not Z_OK return error -1 */
  173. if (err != Z_OK) {
  174. G_warning(_("ZLIB decompression error %d: %s"),
  175. err, zError(err));
  176. return -1;
  177. }
  178. /* Number of bytes inflated to output stream is
  179. * updated buffer size
  180. */
  181. if (nbytes != dst_sz) {
  182. /* TODO: it is not an error if destination is larger than needed */
  183. G_warning(_("Got uncompressed size %d, expected %d"), (int)nbytes, dst_sz);
  184. return -1;
  185. }
  186. return nbytes;
  187. } /* G_zlib_expand() */
  188. #endif /* HAVE_ZLIB_H */
  189. /* vim: set softtabstop=4 shiftwidth=4 expandtab: */