cmprbzip.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. return -1;
  95. /* Don't do anything if src is empty */
  96. if (src_sz <= 0)
  97. return 0;
  98. /* Output buffer has to be 1% + 600 bytes bigger for single pass compression */
  99. buf = dst;
  100. buf_sz = G_bz2_compress_bound(src_sz);
  101. if (buf_sz > dst_sz) {
  102. G_warning("G_bz2_compress(): programmer error, destination is too small");
  103. if (NULL == (buf = (unsigned char *)
  104. G_calloc(buf_sz, sizeof(unsigned char))))
  105. return -1;
  106. }
  107. else
  108. buf_sz = dst_sz;
  109. /* Do single pass compression */
  110. nbytes = buf_sz;
  111. err = BZ2_bzBuffToBuffCompress((char *)buf, &nbytes, /* destination */
  112. (char *)src, src_sz, /* source */
  113. 9, /* blockSize100k */
  114. 0, /* verbosity */
  115. 100); /* workFactor */
  116. if (err != BZ_OK) {
  117. if (buf != dst)
  118. G_free(buf);
  119. return -1;
  120. }
  121. /* updated buf_sz is bytes of compressed data */
  122. if (nbytes >= (unsigned int)src_sz) {
  123. /* compression not possible */
  124. if (buf != dst)
  125. G_free(buf);
  126. return -2;
  127. }
  128. if (buf != dst) {
  129. /* Copy the data from buf to dst */
  130. for (i = 0; i < nbytes; i++)
  131. dst[i] = buf[i];
  132. G_free(buf);
  133. }
  134. return nbytes;
  135. #endif
  136. } /* G_bz2_compress() */
  137. int
  138. G_bz2_expand(unsigned char *src, int src_sz, unsigned char *dst,
  139. int dst_sz)
  140. {
  141. int err;
  142. unsigned int nbytes;
  143. #ifndef HAVE_BZLIB_H
  144. G_fatal_error(_("GRASS needs to be compiled with BZIP2 for BZIP2 compression"));
  145. return -2;
  146. #else
  147. /* Catch error condition */
  148. if (src == NULL || dst == NULL)
  149. return -2;
  150. /* Don't do anything if either of these are true */
  151. if (src_sz <= 0 || dst_sz <= 0)
  152. return 0;
  153. /* Do single pass decompression */
  154. nbytes = dst_sz;
  155. err = BZ2_bzBuffToBuffDecompress((char *)dst, &nbytes, /* destination */
  156. (char *)src, src_sz, /* source */
  157. 0, /* small */
  158. 0); /* verbosity */
  159. if (err != BZ_OK) {
  160. return -1;
  161. }
  162. /* Number of bytes inflated to output stream is
  163. * updated buffer size
  164. */
  165. if (nbytes != dst_sz) {
  166. return -1;
  167. }
  168. return nbytes;
  169. #endif
  170. }
  171. /* vim: set softtabstop=4 shiftwidth=4 expandtab: */