compress.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. ****************************************************************************
  3. * -- GRASS Development Team --
  4. *
  5. * MODULE: GRASS gis library
  6. * FILENAME: compress.c
  7. * AUTHOR(S): Markus Metz
  8. * PURPOSE: To provide an interface for compressing and
  9. * decompressing data using various methods. Its primary
  10. * use is in the storage and reading of GRASS rasters.
  11. *
  12. * DATE CREATED: Dec 17 2015
  13. * COPYRIGHT: (C) 2015 by the GRASS Development Team
  14. *
  15. * This program is free software under the GNU General Public
  16. * License (version 2 or greater). Read the file COPYING that
  17. * comes with GRASS for details.
  18. *
  19. *****************************************************************************/
  20. /********************************************************************
  21. * Compression methods: *
  22. * 1 : RLE (generic Run-Length Encoding of single bytes) *
  23. * 2 : ZLIB's DEFLATE (good speed and compression) *
  24. * 3 : LZ4 (fastest, low compression) *
  25. * 4 : BZIP2 (slowest, high compression) *
  26. * *
  27. * int *
  28. * G_read_compressed (fd, rbytes, dst, nbytes, compression_type) *
  29. * int fd, rbytes, nbytes; *
  30. * unsigned char *dst; *
  31. * ---------------------------------------------------------------- *
  32. * This is the basic function for reading a compressed chunk of a *
  33. * data file. The file descriptor should be in the proper location *
  34. * and the 'dst' array should have enough space for the data. *
  35. * 'nbytes' is the size of 'dst'. The 'rbytes' parameter is the *
  36. * number of bytes to read (knowable from the offsets index). For *
  37. * best results, 'nbytes' should be the exact amount of space *
  38. * needed for the expansion. Too large a value of nbytes may cause *
  39. * more data to be expanded than is desired. *
  40. * Returns: The number of bytes decompressed into dst, or an error. *
  41. * *
  42. * Errors include: *
  43. * -1 -- Error Reading or Decompressing data. *
  44. * -2 -- Not enough space in dst. You must make dst larger *
  45. * and then call the function again (remembering to *
  46. * reset the file descriptor to it's proper location. *
  47. * *
  48. * ================================================================ *
  49. * int *
  50. * G_write_compressed (fd, src, nbytes, compression_type) *
  51. * int fd, nbytes; *
  52. * unsigned char *src; *
  53. * ---------------------------------------------------------------- *
  54. * This is the basic function for writing and compressing a data *
  55. * chunk to a file. The file descriptor should be in the correct *
  56. * location prior to this call. The function will compress 'nbytes' *
  57. * of 'src' and write it to the file 'fd'. Returns the number of *
  58. * bytes written or an error code: *
  59. * *
  60. * Errors include: *
  61. * -1 -- Compression Failed. *
  62. * -2 -- Unable to write to file. *
  63. * *
  64. * ================================================================ *
  65. * int *
  66. * G_write_uncompressed (fd, src, nbytes) *
  67. * int fd, nbytes; *
  68. * unsigned char *src; *
  69. * ---------------------------------------------------------------- *
  70. * Works similar to G_write_compressed() except no attempt at *
  71. * compression is made. This is quicker, but may result in larger *
  72. * files. *
  73. * Returns the number of bytes written, or -1 for an error. It will *
  74. * return an error if it fails to write nbytes. Otherwise, the *
  75. * return value will always be nbytes + 1 (for compression flag). *
  76. * *
  77. ********************************************************************
  78. */
  79. #include <grass/config.h>
  80. #include <stdio.h>
  81. #include <stdlib.h>
  82. #include <string.h>
  83. #include <unistd.h>
  84. #include <grass/gis.h>
  85. #include <grass/glocale.h>
  86. #include "compress.h"
  87. #define G_COMPRESSED_NO (unsigned char)'0'
  88. #define G_COMPRESSED_YES (unsigned char)'1'
  89. int G_get_compressor(char *compressor)
  90. {
  91. /* 0: NONE (no compressor)
  92. * 1: RLE
  93. * 2: ZLIB's DEFLATE (default)
  94. * 3: LZ4
  95. * 4: BZIP2
  96. */
  97. if (!compressor)
  98. return 2;
  99. if (G_strncasecmp(compressor, "NONE", 4) == 0)
  100. return 0;
  101. if (G_strncasecmp(compressor, "RLE", 3) == 0)
  102. return 1;
  103. if (G_strncasecmp(compressor, "ZLIB", 4) == 0)
  104. return 2;
  105. if (G_strncasecmp(compressor, "LZ4", 3) == 0)
  106. return 3;
  107. if (G_strncasecmp(compressor, "BZ", 2) == 0)
  108. return 4;
  109. G_warning(_("Unknown compressor <%s>, using default ZLIB compressor"),
  110. compressor);
  111. return 2;
  112. }
  113. int
  114. G_no_compress(unsigned char *src, int src_sz, unsigned char *dst,
  115. int dst_sz)
  116. {
  117. /* Catch errors early */
  118. if (src == NULL || dst == NULL)
  119. return -1;
  120. /* Don't do anything if src is empty */
  121. if (src_sz <= 0)
  122. return 0;
  123. /* dst too small */
  124. if (dst_sz < src_sz)
  125. return -2;
  126. /* Copy the data from src to dst */
  127. memcpy(dst, src, src_sz);
  128. return src_sz;
  129. }
  130. int
  131. G_no_expand(unsigned char *src, int src_sz, unsigned char *dst,
  132. int dst_sz)
  133. {
  134. /* Catch errors early */
  135. if (src == NULL || dst == NULL)
  136. return -1;
  137. /* Don't do anything if src is empty */
  138. if (src_sz <= 0)
  139. return 0;
  140. /* dst too small */
  141. if (dst_sz < src_sz)
  142. return -2;
  143. /* Copy the data from src to dst */
  144. memcpy(dst, src, src_sz);
  145. return src_sz;
  146. }
  147. /* G_*_compress() returns
  148. * > 0: number of bytes in dst
  149. * 0: nothing done
  150. * -1: error
  151. * -2: dst too small
  152. */
  153. int
  154. G_compress(unsigned char *src, int src_sz, unsigned char *dst,
  155. int dst_sz, int compressor)
  156. {
  157. if (compressor == 0)
  158. return G_no_compress(src, src_sz, dst, dst_sz);
  159. if (compressor == 1)
  160. return G_rle_compress(src, src_sz, dst, dst_sz);
  161. if (compressor == 2)
  162. return G_zlib_compress(src, src_sz, dst, dst_sz);
  163. if (compressor == 3)
  164. return G_lz4_compress(src, src_sz, dst, dst_sz);
  165. if (compressor == 4)
  166. return G_bz2_compress(src, src_sz, dst, dst_sz);
  167. G_fatal_error(_("Request for unsupported compressor"));
  168. return -1;
  169. }
  170. /* G_*_expand() returns
  171. * > 0: number of bytes in dst
  172. * -1: error
  173. */
  174. int
  175. G_expand(unsigned char *src, int src_sz, unsigned char *dst,
  176. int dst_sz, int compressor)
  177. {
  178. if (compressor == 0)
  179. return G_no_expand(src, src_sz, dst, dst_sz);
  180. if (compressor == 1)
  181. return G_rle_expand(src, src_sz, dst, dst_sz);
  182. if (compressor == 2)
  183. return G_zlib_expand(src, src_sz, dst, dst_sz);
  184. if (compressor == 3)
  185. return G_lz4_expand(src, src_sz, dst, dst_sz);
  186. if (compressor == 4)
  187. return G_bz2_expand(src, src_sz, dst, dst_sz);
  188. G_fatal_error(_("Request for unsupported compressor"));
  189. return -1;
  190. }
  191. int G_read_compressed(int fd, int rbytes, unsigned char *dst, int nbytes,
  192. int compressor)
  193. {
  194. int bsize, nread, err;
  195. unsigned char *b;
  196. if (dst == NULL || nbytes < 0)
  197. return -2;
  198. bsize = rbytes;
  199. /* Our temporary input buffer for read */
  200. if (NULL == (b = (unsigned char *)
  201. G_calloc(bsize, sizeof(unsigned char))))
  202. return -1;
  203. /* Read from the file until we get our bsize or an error */
  204. nread = 0;
  205. do {
  206. err = read(fd, b + nread, bsize - nread);
  207. if (err >= 0)
  208. nread += err;
  209. } while (err > 0 && nread < bsize);
  210. /* If the bsize if less than rbytes and we didn't get an error.. */
  211. if (nread < rbytes && err > 0) {
  212. G_free(b);
  213. return -1;
  214. }
  215. /* Test if row is compressed */
  216. if (b[0] == G_COMPRESSED_NO) {
  217. /* Then just copy it to dst */
  218. for (err = 0; err < nread - 1 && err < nbytes; err++)
  219. dst[err] = b[err + 1];
  220. G_free(b);
  221. return (nread - 1);
  222. }
  223. else if (b[0] != G_COMPRESSED_YES) {
  224. /* We're not at the start of a row */
  225. G_free(b);
  226. return -1;
  227. }
  228. /* Okay it's a compressed row */
  229. /* Just call G_expand() with the buffer we read,
  230. * Account for first byte being a flag
  231. */
  232. err = G_expand(b + 1, bsize - 1, dst, nbytes, compressor);
  233. /* We're done with b */
  234. G_free(b);
  235. /* Return whatever G_expand() returned */
  236. return err;
  237. } /* G_read_compressed() */
  238. int G_write_compressed(int fd, unsigned char *src, int nbytes,
  239. int compressor)
  240. {
  241. int dst_sz, nwritten, err;
  242. unsigned char *dst, compressed;
  243. /* Catch errors */
  244. if (src == NULL || nbytes < 0)
  245. return -1;
  246. dst_sz = nbytes;
  247. if (NULL == (dst = (unsigned char *)
  248. G_calloc(dst_sz, sizeof(unsigned char))))
  249. return -1;
  250. /* Now just call G_compress() */
  251. err = G_compress(src, nbytes, dst, dst_sz, compressor);
  252. /* If compression succeeded write compressed row,
  253. * otherwise write uncompressed row. Compression will fail
  254. * if dst is too small (i.e. compressed data is larger)
  255. */
  256. if (err > 0 && err <= dst_sz) {
  257. dst_sz = err;
  258. /* Write the compression flag */
  259. compressed = G_COMPRESSED_YES;
  260. if (write(fd, &compressed, 1) != 1) {
  261. G_free(dst);
  262. return -1;
  263. }
  264. nwritten = 0;
  265. do {
  266. err = write(fd, dst + nwritten, dst_sz - nwritten);
  267. if (err >= 0)
  268. nwritten += err;
  269. } while (err > 0 && nwritten < dst_sz);
  270. /* Account for extra byte */
  271. nwritten++;
  272. }
  273. else {
  274. /* Write compression flag */
  275. compressed = G_COMPRESSED_NO;
  276. if (write(fd, &compressed, 1) != 1) {
  277. G_free(dst);
  278. return -1;
  279. }
  280. nwritten = 0;
  281. do {
  282. err = write(fd, src + nwritten, nbytes - nwritten);
  283. if (err >= 0)
  284. nwritten += err;
  285. } while (err > 0 && nwritten < nbytes);
  286. /* Account for extra byte */
  287. nwritten++;
  288. } /* if (err > 0) */
  289. /* Done with the dst buffer */
  290. G_free(dst);
  291. /* If we didn't write all the data return an error */
  292. if (err < 0)
  293. return -2;
  294. return nwritten;
  295. } /* G_write_compressed() */
  296. int G_write_uncompressed(int fd, const unsigned char *src, int nbytes)
  297. {
  298. int err, nwritten;
  299. unsigned char compressed;
  300. /* Catch errors */
  301. if (src == NULL || nbytes < 0)
  302. return -1;
  303. /* Write the compression flag */
  304. compressed = G_COMPRESSED_NO;
  305. if (write(fd, &compressed, 1) != 1)
  306. return -1;
  307. /* Now write the data */
  308. nwritten = 0;
  309. do {
  310. err = write(fd, src + nwritten, nbytes - nwritten);
  311. if (err > 0)
  312. nwritten += err;
  313. } while (err > 0 && nwritten < nbytes);
  314. if (err < 0 || nwritten != nbytes)
  315. return -1;
  316. /* Account for extra compressed flag */
  317. nwritten++;
  318. /* That's all */
  319. return nwritten;
  320. } /* G_write_uncompressed() */
  321. /* vim: set softtabstop=4 shiftwidth=4 expandtab: */