cmprlz4.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 LZ4. 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_lz4_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 LZ4 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_lz4_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 lz4 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. #include <grass/gis.h>
  63. #include <grass/glocale.h>
  64. #include "lz4.h"
  65. int
  66. G_lz4_compress_bound(int src_sz)
  67. {
  68. /* LZ4 has a fast version if destLen is large enough
  69. * to hold a worst case result
  70. */
  71. return LZ4_compressBound(src_sz);
  72. }
  73. int
  74. G_lz4_compress(unsigned char *src, int src_sz, unsigned char *dst,
  75. int dst_sz)
  76. {
  77. int err, nbytes, buf_sz;
  78. unsigned char *buf;
  79. /* Catch errors early */
  80. if (src == NULL || dst == NULL) {
  81. if (src == NULL)
  82. G_warning(_("No source buffer"));
  83. if (dst == NULL)
  84. G_warning(_("No destination buffer"));
  85. return -1;
  86. }
  87. /* Don't do anything if either of these are true */
  88. if (src_sz <= 0 || dst_sz <= 0) {
  89. if (src_sz <= 0)
  90. G_warning(_("Invalid source buffer size %d"), src_sz);
  91. if (dst_sz <= 0)
  92. G_warning(_("Invalid destination buffer size %d"), dst_sz);
  93. return 0;
  94. }
  95. /* Output buffer should be large enough for single pass compression */
  96. buf = dst;
  97. buf_sz = G_lz4_compress_bound(src_sz);
  98. if (buf_sz > dst_sz) {
  99. G_warning("G_lz4_compress(): programmer error, destination is too small");
  100. if (NULL == (buf = (unsigned char *)
  101. G_calloc(buf_sz, sizeof(unsigned char))))
  102. return -1;
  103. }
  104. else
  105. buf_sz = dst_sz;
  106. /* Do single pass compression */
  107. err = LZ4_compress_default((char *)src, (char *)buf, src_sz, buf_sz);
  108. if (err <= 0) {
  109. G_warning(_("LZ4 compression error"));
  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. }
  130. int
  131. G_lz4_expand(unsigned char *src, int src_sz, unsigned char *dst,
  132. int dst_sz)
  133. {
  134. int err, nbytes;
  135. /* Catch error condition */
  136. if (src == NULL || dst == NULL) {
  137. if (src == NULL)
  138. G_warning(_("No source buffer"));
  139. if (dst == NULL)
  140. G_warning(_("No destination buffer"));
  141. return -2;
  142. }
  143. /* Don't do anything if either of these are true */
  144. if (src_sz <= 0 || dst_sz <= 0) {
  145. if (src_sz <= 0)
  146. G_warning(_("Invalid source buffer size %d"), src_sz);
  147. if (dst_sz <= 0)
  148. G_warning(_("Invalid destination buffer size %d"), dst_sz);
  149. return 0;
  150. }
  151. /* Do single pass decompress */
  152. err = LZ4_decompress_safe((char *)src, (char *)dst, src_sz, dst_sz);
  153. /* err = LZ4_decompress_fast(src, dst, src_sz); */
  154. if (err <= 0) {
  155. G_warning(_("LZ4 decompression error"));
  156. return -1;
  157. }
  158. /* Number of bytes inflated to output stream is return value */
  159. nbytes = err;
  160. if (nbytes != dst_sz) {
  161. /* TODO: it is not an error if destination is larger than needed */
  162. G_warning(_("Got uncompressed size %d, expected %d"), (int)nbytes, dst_sz);
  163. return -1;
  164. }
  165. return nbytes;
  166. }
  167. /* vim: set softtabstop=4 shiftwidth=4 expandtab: */