cmprbzip.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. ****************************************************************************
  3. * -- GRASS Development Team --
  4. *
  5. * MODULE: GRASS gis library
  6. * FILENAME: cmprbzip.c
  7. * AUTHOR(S): Markus Metz
  8. * PURPOSE: To provide an interface to libbzip2 for compressing and
  9. * decompressing data. Its primary use is in
  10. * the storage and reading of GRASS rasters.
  11. *
  12. * ALGORITHM: http://www.bzip.org
  13. * DATE CREATED: Nov 19 2015
  14. * COPYRIGHT: (C) 2015 by the GRASS Development Team
  15. *
  16. * This program is free software under the GNU General Public
  17. * License (version 2 or greater). Read the file COPYING that
  18. * comes with GRASS for details.
  19. *
  20. *****************************************************************************/
  21. /********************************************************************
  22. * int *
  23. * G_bz2_compress (src, srz_sz, dst, dst_sz) *
  24. * int src_sz, dst_sz; *
  25. * unsigned char *src, *dst; *
  26. * ---------------------------------------------------------------- *
  27. * This function is a wrapper around the bzip2 compression *
  28. * function. It uses an all or nothing call. *
  29. * If you need a continuous compression scheme, you'll have to code *
  30. * your own. *
  31. * In order to do a single pass compression, the input src must be *
  32. * copied to a buffer 1% + 600 bytes larger than the data. This *
  33. * may cause performance degradation. *
  34. * *
  35. * The function either returns the number of bytes of compressed *
  36. * data in dst, or an error code. *
  37. * *
  38. * Errors include: *
  39. * -1 -- Compression failed. *
  40. * -2 -- dst is too small. *
  41. * *
  42. * ================================================================ *
  43. * int *
  44. * G_bz2_expand (src, src_sz, dst, dst_sz) *
  45. * int src_sz, dst_sz; *
  46. * unsigned char *src, *dst; *
  47. * ---------------------------------------------------------------- *
  48. * This function is a wrapper around the bzip2 decompression *
  49. * function. It uses a single pass call to inflate(). *
  50. * If you need a continuous expansion scheme, you'll have to code *
  51. * 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. #ifdef HAVE_BZLIB_H
  63. #include <bzlib.h>
  64. #endif
  65. #include <grass/gis.h>
  66. #include <grass/glocale.h>
  67. int
  68. G_bz2_compress_bound(int src_sz)
  69. {
  70. /* from the documentation:
  71. * To guarantee that the compressed data will fit in its buffer,
  72. * allocate an output buffer of size 1% larger than the uncompressed data,
  73. * plus six hundred extra bytes.
  74. * bzip2 does not provide a compressbound fn
  75. * and apparently does not have a fast version if destLen is
  76. * large enough to hold a worst case result
  77. */
  78. return src_sz;
  79. }
  80. int
  81. G_bz2_compress(unsigned char *src, int src_sz, unsigned char *dst,
  82. int dst_sz)
  83. {
  84. int err;
  85. int i, buf_sz;
  86. unsigned int nbytes;
  87. unsigned char *buf;
  88. #ifndef HAVE_BZLIB_H
  89. G_fatal_error(_("GRASS needs to be compiled with BZIP2 for BZIP2 compression"));
  90. return -1;
  91. #else
  92. /* Catch errors early */
  93. if (src == NULL || dst == NULL) {
  94. if (src == NULL)
  95. G_warning(_("No source buffer"));
  96. if (dst == NULL)
  97. G_warning(_("No destination buffer"));
  98. return -1;
  99. }
  100. /* Don't do anything if either of these are true */
  101. if (src_sz <= 0 || dst_sz <= 0) {
  102. if (src_sz <= 0)
  103. G_warning(_("Invalid source buffer size %d"), src_sz);
  104. if (dst_sz <= 0)
  105. G_warning(_("Invalid destination buffer size %d"), dst_sz);
  106. return 0;
  107. }
  108. /* Output buffer has to be 1% + 600 bytes bigger for single pass compression */
  109. buf = dst;
  110. buf_sz = G_bz2_compress_bound(src_sz);
  111. if (buf_sz > dst_sz) {
  112. G_warning("G_bz2_compress(): programmer error, destination is too small");
  113. if (NULL == (buf = (unsigned char *)
  114. G_calloc(buf_sz, sizeof(unsigned char))))
  115. return -1;
  116. }
  117. else
  118. buf_sz = dst_sz;
  119. /* Do single pass compression */
  120. nbytes = buf_sz;
  121. err = BZ2_bzBuffToBuffCompress((char *)buf, &nbytes, /* destination */
  122. (char *)src, src_sz, /* source */
  123. 9, /* blockSize100k */
  124. 0, /* verbosity */
  125. 100); /* workFactor */
  126. if (err != BZ_OK) {
  127. G_warning(_("BZIP2 version %s compression error %d"),
  128. BZ2_bzlibVersion(), err);
  129. if (buf != dst)
  130. G_free(buf);
  131. return -1;
  132. }
  133. /* updated buf_sz is bytes of compressed data */
  134. if (nbytes >= (unsigned int)src_sz) {
  135. /* compression not possible */
  136. if (buf != dst)
  137. G_free(buf);
  138. return -2;
  139. }
  140. if (buf != dst) {
  141. /* Copy the data from buf to dst */
  142. for (i = 0; i < nbytes; i++)
  143. dst[i] = buf[i];
  144. G_free(buf);
  145. }
  146. return nbytes;
  147. #endif
  148. } /* G_bz2_compress() */
  149. int
  150. G_bz2_expand(unsigned char *src, int src_sz, unsigned char *dst,
  151. int dst_sz)
  152. {
  153. int err;
  154. unsigned int nbytes;
  155. #ifndef HAVE_BZLIB_H
  156. G_fatal_error(_("GRASS needs to be compiled with BZIP2 for BZIP2 compression"));
  157. return -2;
  158. #else
  159. /* Catch error condition */
  160. if (src == NULL || dst == NULL) {
  161. if (src == NULL)
  162. G_warning(_("No source buffer"));
  163. if (dst == NULL)
  164. G_warning(_("No destination buffer"));
  165. return -2;
  166. }
  167. /* Don't do anything if either of these are true */
  168. if (src_sz <= 0 || dst_sz <= 0) {
  169. if (src_sz <= 0)
  170. G_warning(_("Invalid source buffer size %d"), src_sz);
  171. if (dst_sz <= 0)
  172. G_warning(_("Invalid destination buffer size %d"), dst_sz);
  173. return 0;
  174. }
  175. /* Do single pass decompression */
  176. nbytes = dst_sz;
  177. err = BZ2_bzBuffToBuffDecompress((char *)dst, &nbytes, /* destination */
  178. (char *)src, src_sz, /* source */
  179. 0, /* small */
  180. 0); /* verbosity */
  181. if (err != BZ_OK) {
  182. G_warning(_("BZIP2 version %s decompression error %d"),
  183. BZ2_bzlibVersion(), err);
  184. return -1;
  185. }
  186. /* Number of bytes inflated to output stream is
  187. * updated buffer size
  188. */
  189. if (nbytes != dst_sz) {
  190. /* TODO: it is not an error if destination is larger than needed */
  191. G_warning(_("Got uncompressed size %d, expected %d"), (int)nbytes, dst_sz);
  192. return -1;
  193. }
  194. return nbytes;
  195. #endif
  196. }
  197. /* vim: set softtabstop=4 shiftwidth=4 expandtab: */