cmprzstd.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. ****************************************************************************
  3. * -- GRASS Development Team --
  4. *
  5. * MODULE: GRASS gis library
  6. * FILENAME: cmprzstd.c
  7. * AUTHOR(S): Eric G. Miller <egm2@jps.net>
  8. * Markus Metz
  9. * PURPOSE: To provide an interface to ZSTD for compressing and
  10. * decompressing data using ZSTD. It's primary use is in
  11. * the storage and reading of GRASS floating point rasters.
  12. *
  13. * ALGORITHM: http://www.zstd.net
  14. * DATE CREATED: Dec 18 2017
  15. * COPYRIGHT: (C) 2017 by the GRASS Development Team
  16. *
  17. * This program is free software under the GNU General Public
  18. * License (version 2 or greater). Read the file COPYING that
  19. * comes with GRASS for details.
  20. *
  21. *****************************************************************************/
  22. /********************************************************************
  23. * int *
  24. * G_zstd_compress (src, srz_sz, dst, dst_sz) *
  25. * int src_sz, dst_sz; *
  26. * unsigned char *src, *dst; *
  27. * ---------------------------------------------------------------- *
  28. * This function is a wrapper around the Zstd compression function. *
  29. * It uses an all or nothing call. *
  30. * If you need a continuous compression scheme, you'll have to code *
  31. * your own. *
  32. * In order to do a single pass compression, the input src must be *
  33. * copied to a buffer larger than the data. This may cause *
  34. * 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_zstd_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 zstd decompression *
  50. * function. It uses a single pass call. If you need a continuous *
  51. * 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. #ifdef HAVE_ZSTD_H
  63. #include <zstd.h>
  64. #endif
  65. #include <grass/gis.h>
  66. #include <grass/glocale.h>
  67. int
  68. G_zstd_compress_bound(int src_sz)
  69. {
  70. /* ZSTD has a fast version if destLen is large enough
  71. * to hold a worst case result
  72. */
  73. #ifndef HAVE_ZSTD_H
  74. G_fatal_error(_("GRASS needs to be compiled with ZSTD for ZSTD compression"));
  75. return -1;
  76. #else
  77. return ZSTD_compressBound(src_sz);
  78. #endif
  79. }
  80. int
  81. G_zstd_compress(unsigned char *src, int src_sz, unsigned char *dst,
  82. int dst_sz)
  83. {
  84. int err, nbytes, buf_sz;
  85. unsigned char *buf;
  86. #ifndef HAVE_ZSTD_H
  87. G_fatal_error(_("GRASS needs to be compiled with ZSTD for ZSTD compression"));
  88. return -1;
  89. #else
  90. /* Catch errors early */
  91. if (src == NULL || dst == NULL) {
  92. if (src == NULL)
  93. G_warning(_("No source buffer"));
  94. if (dst == NULL)
  95. G_warning(_("No destination buffer"));
  96. return -1;
  97. }
  98. /* Don't do anything if either of these are true */
  99. if (src_sz <= 0 || dst_sz <= 0) {
  100. if (src_sz <= 0)
  101. G_warning(_("Invalid source buffer size %d"), src_sz);
  102. if (dst_sz <= 0)
  103. G_warning(_("Invalid destination buffer size %d"), dst_sz);
  104. return 0;
  105. }
  106. /* Output buffer has to be larger for single pass compression */
  107. buf = dst;
  108. buf_sz = G_zstd_compress_bound(src_sz);
  109. if (buf_sz > dst_sz) {
  110. G_warning("G_zstd_compress(): programmer error, destination is too small");
  111. if (NULL == (buf = (unsigned char *)
  112. G_calloc(buf_sz, sizeof(unsigned char))))
  113. return -1;
  114. }
  115. else
  116. buf_sz = dst_sz;
  117. /* Do single pass compression */
  118. err = ZSTD_compress((char *)buf, buf_sz, (char *)src, src_sz, 3);
  119. if (err <= 0 || ZSTD_isError(err)) {
  120. G_warning(_("ZSTD compression error %d: %s"),
  121. err, ZSTD_getErrorName(err));
  122. if (buf != dst)
  123. G_free(buf);
  124. return -1;
  125. }
  126. if (err >= src_sz) {
  127. /* compression not possible */
  128. if (buf != dst)
  129. G_free(buf);
  130. return -2;
  131. }
  132. /* bytes of compressed data is return value */
  133. nbytes = err;
  134. if (buf != dst) {
  135. /* Copy the data from buf to dst */
  136. for (err = 0; err < nbytes; err++)
  137. dst[err] = buf[err];
  138. G_free(buf);
  139. }
  140. return nbytes;
  141. #endif
  142. }
  143. int
  144. G_zstd_expand(unsigned char *src, int src_sz, unsigned char *dst,
  145. int dst_sz)
  146. {
  147. int err, nbytes;
  148. #ifndef HAVE_ZSTD_H
  149. G_fatal_error(_("GRASS needs to be compiled with ZSTD for ZSTD compression"));
  150. return -1;
  151. #else
  152. /* Catch error condition */
  153. if (src == NULL || dst == NULL) {
  154. if (src == NULL)
  155. G_warning(_("No source buffer"));
  156. if (dst == NULL)
  157. G_warning(_("No destination buffer"));
  158. return -2;
  159. }
  160. /* Don't do anything if either of these are true */
  161. if (src_sz <= 0 || dst_sz <= 0) {
  162. if (src_sz <= 0)
  163. G_warning(_("Invalid source buffer size %d"), src_sz);
  164. if (dst_sz <= 0)
  165. G_warning(_("Invalid destination buffer size %d"), dst_sz);
  166. return 0;
  167. }
  168. /* Do single pass decompress */
  169. err = ZSTD_decompress((char *)dst, dst_sz, (char *)src, src_sz);
  170. if (err <= 0 || ZSTD_isError(err)) {
  171. G_warning(_("ZSTD compression error %d: %s"),
  172. err, ZSTD_getErrorName(err));
  173. return -1;
  174. }
  175. /* Number of bytes inflated to output stream is return value */
  176. nbytes = err;
  177. if (nbytes != dst_sz) {
  178. /* TODO: it is not an error if destination is larger than needed */
  179. G_warning(_("Got uncompressed size %d, expected %d"), (int)nbytes, dst_sz);
  180. return -1;
  181. }
  182. return nbytes;
  183. #endif
  184. }
  185. /* vim: set softtabstop=4 shiftwidth=4 expandtab: */