cmprzstd.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. ****************************************************************************
  3. * -- GRASS Development Team --
  4. *
  5. * MODULE: GRASS gis library
  6. * FILENAME: cmprlz4.c
  7. * AUTHOR(S): Eric G. Miller <egm2@jps.net>
  8. * Markus Metz
  9. * PURPOSE: To provide an interface to lz4 for compressing and
  10. * decompressing data using LZ$. It's primary use is in
  11. * the storage and reading of GRASS floating point rasters.
  12. *
  13. * ALGORITHM: https://code.google.com/p/lz4/
  14. * DATE CREATED: Dec 18 2015
  15. * COPYRIGHT: (C) 2015 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. return -1;
  93. /* Don't do anything if either of these are true */
  94. if (src_sz <= 0 || dst_sz <= 0)
  95. return 0;
  96. /* Output buffer has to be larger for single pass compression */
  97. buf = dst;
  98. buf_sz = G_zstd_compress_bound(src_sz);
  99. if (buf_sz > dst_sz) {
  100. G_warning("G_zstd_compress(): programmer error, destination is too small");
  101. if (NULL == (buf = (unsigned char *)
  102. G_calloc(buf_sz, sizeof(unsigned char))))
  103. return -1;
  104. }
  105. else
  106. buf_sz = dst_sz;
  107. /* Do single pass compression */
  108. err = ZSTD_compress((char *)buf, buf_sz, (char *)src, src_sz, 3);
  109. if (err <= 0 || ZSTD_isError(err)) {
  110. if (buf != dst)
  111. G_free(buf);
  112. return -1;
  113. }
  114. if (err >= src_sz) {
  115. /* compression not possible */
  116. if (buf != dst)
  117. G_free(buf);
  118. return -2;
  119. }
  120. /* bytes of compressed data is return value */
  121. nbytes = err;
  122. if (buf != dst) {
  123. /* Copy the data from buf to dst */
  124. for (err = 0; err < nbytes; err++)
  125. dst[err] = buf[err];
  126. G_free(buf);
  127. }
  128. return nbytes;
  129. #endif
  130. }
  131. int
  132. G_zstd_expand(unsigned char *src, int src_sz, unsigned char *dst,
  133. int dst_sz)
  134. {
  135. int err, nbytes;
  136. #ifndef HAVE_ZSTD_H
  137. G_fatal_error(_("GRASS needs to be compiled with ZSTD for ZSTD compression"));
  138. return -1;
  139. #else
  140. /* Catch error condition */
  141. if (src == NULL || dst == NULL)
  142. return -2;
  143. /* Don't do anything if either of these are true */
  144. if (src_sz <= 0 || dst_sz <= 0)
  145. return 0;
  146. /* Do single pass decompress */
  147. err = ZSTD_decompress((char *)dst, dst_sz, (char *)src, src_sz);
  148. /* err = LZ4_decompress_fast(src, dst, src_sz); */
  149. /* Number of bytes inflated to output stream is return value */
  150. nbytes = err;
  151. if (nbytes != dst_sz) {
  152. return -1;
  153. }
  154. return nbytes;
  155. #endif
  156. }
  157. /* vim: set softtabstop=4 shiftwidth=4 expandtab: */