cmprbzip.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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(unsigned char *src, int src_sz, unsigned char *dst,
  69. int dst_sz)
  70. {
  71. int err;
  72. unsigned int i, nbytes, buf_sz;
  73. unsigned char *buf;
  74. #ifndef HAVE_BZLIB_H
  75. G_fatal_error(_("GRASS needs to be compiled with BZIP2 for BZIP2 compression"));
  76. return -1;
  77. #else
  78. /* Catch errors early */
  79. if (src == NULL || dst == NULL)
  80. return -1;
  81. /* Don't do anything if src is empty */
  82. if (src_sz <= 0)
  83. return 0;
  84. /* Output buffer has to be 1% + 600 bytes bigger for single pass compression */
  85. buf_sz = (unsigned int)((double)dst_sz * 1.01 + (double)600);
  86. if (NULL == (buf = (unsigned char *)
  87. G_calloc(buf_sz, sizeof(unsigned char))))
  88. return -1;
  89. /* Do single pass compression */
  90. nbytes = buf_sz;
  91. err = BZ2_bzBuffToBuffCompress((char *)buf, &nbytes, /* destination */
  92. (char *)src, src_sz, /* source */
  93. 9, /* blockSize100k */
  94. 0, /* verbosity */
  95. 100); /* workFactor */
  96. if (err != BZ_OK) {
  97. G_free(buf);
  98. return -1;
  99. }
  100. /* updated buf_sz is bytes of compressed data */
  101. if (nbytes >= (unsigned int)src_sz) {
  102. /* compression not possible */
  103. G_free(buf);
  104. return -2;
  105. }
  106. /* dst too small */
  107. if ((unsigned int)dst_sz < nbytes)
  108. return -2;
  109. /* Copy the data from buf to dst */
  110. for (i = 0; i < nbytes; i++)
  111. dst[i] = buf[i];
  112. G_free(buf);
  113. return nbytes;
  114. #endif
  115. } /* G_bz2_compress() */
  116. int
  117. G_bz2_expand(unsigned char *src, int src_sz, unsigned char *dst,
  118. int dst_sz)
  119. {
  120. int err;
  121. unsigned int nbytes;
  122. #ifndef HAVE_BZLIB_H
  123. G_fatal_error(_("GRASS needs to be compiled with BZIP2 for BZIP2 compression"));
  124. return -2;
  125. #else
  126. /* Catch error condition */
  127. if (src == NULL || dst == NULL)
  128. return -2;
  129. /* Don't do anything if either of these are true */
  130. if (src_sz <= 0 || dst_sz <= 0)
  131. return 0;
  132. /* Do single pass decompression */
  133. nbytes = dst_sz;
  134. err = BZ2_bzBuffToBuffDecompress((char *)dst, &nbytes, /* destination */
  135. (char *)src, src_sz, /* source */
  136. 0, /* small */
  137. 0); /* verbosity */
  138. /* Number of bytes inflated to output stream is
  139. * updated buffer size
  140. */
  141. if (!(err == BZ_OK)) {
  142. return -1;
  143. }
  144. return nbytes;
  145. #endif
  146. }
  147. /* vim: set softtabstop=4 shiftwidth=4 expandtab: */