cmprzlib.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. return -1;
  89. /* Don't do anything if either of these are true */
  90. if (src_sz <= 0 || dst_sz <= 0)
  91. return 0;
  92. /* Output buffer has to be 1% + 12 bytes bigger for single pass deflate */
  93. /* buf_sz = (int)((double)dst_sz * 1.01 + (double)12); */
  94. /* Output buffer should be large enough for single pass compression */
  95. buf = dst;
  96. buf_sz = G_zlib_compress_bound(src_sz);
  97. if (buf_sz > dst_sz) {
  98. G_warning("G_zlib_compress(): programmer error, destination is too small");
  99. if (NULL == (buf = (unsigned char *)
  100. G_calloc(buf_sz, sizeof(unsigned char))))
  101. return -1;
  102. }
  103. else
  104. buf_sz = dst_sz;
  105. /* Valid zlib compression levels -1 - 9 */
  106. /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
  107. * as used here, 1 gives the best compromise between speed and compression */
  108. /* Do single pass compression */
  109. nbytes = buf_sz;
  110. err = compress2((Bytef *)buf, &nbytes, /* destination */
  111. (const Bytef *)src, src_sz, /* source */
  112. G__.compression_level); /* level */
  113. if (err != Z_OK) {
  114. if (buf != dst)
  115. G_free(buf);
  116. return -1;
  117. }
  118. /* updated buf_sz is bytes of compressed data */
  119. if (nbytes >= src_sz) {
  120. /* compression not possible */
  121. if (buf != dst)
  122. G_free(buf);
  123. return -2;
  124. }
  125. if (buf != dst) {
  126. /* Copy the data from buf to dst */
  127. for (err = 0; err < nbytes; err++)
  128. dst[err] = buf[err];
  129. G_free(buf);
  130. }
  131. return nbytes;
  132. } /* G_zlib_compress() */
  133. int
  134. G_zlib_expand(unsigned char *src, int src_sz, unsigned char *dst,
  135. int dst_sz)
  136. {
  137. int err;
  138. uLong ss, nbytes;
  139. /* Catch error condition */
  140. if (src == NULL || dst == NULL)
  141. return -2;
  142. /* Don't do anything if either of these are true */
  143. if (src_sz <= 0 || dst_sz <= 0)
  144. return 0;
  145. ss = src_sz;
  146. /* Do single pass decompression */
  147. nbytes = dst_sz;
  148. err = uncompress((Bytef *)dst, &nbytes, /* destination */
  149. (const Bytef *)src, ss); /* source */
  150. /* If not Z_OK return error -1 */
  151. if (err != Z_OK)
  152. return -1;
  153. /* Number of bytes inflated to output stream is
  154. * updated buffer size
  155. */
  156. if (nbytes != dst_sz) {
  157. return -1;
  158. }
  159. return nbytes;
  160. } /* G_zlib_expand() */
  161. #endif /* HAVE_ZLIB_H */
  162. /* vim: set softtabstop=4 shiftwidth=4 expandtab: */