compress.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. * 5 : ZSTD (faster than ZLIB, higher compression than ZLIB) *
  27. * *
  28. * int *
  29. * G_read_compressed (fd, rbytes, dst, nbytes, compression_type) *
  30. * int fd, rbytes, nbytes; *
  31. * unsigned char *dst; *
  32. * ---------------------------------------------------------------- *
  33. * This is the basic function for reading a compressed chunk of a *
  34. * data file. The file descriptor should be in the proper location *
  35. * and the 'dst' array should have enough space for the data. *
  36. * 'nbytes' is the size of 'dst'. The 'rbytes' parameter is the *
  37. * number of bytes to read (knowable from the offsets index). For *
  38. * best results, 'nbytes' should be the exact amount of space *
  39. * needed for the expansion. Too large a value of nbytes may cause *
  40. * more data to be expanded than is desired. *
  41. * Returns: The number of bytes decompressed into dst, or an error. *
  42. * *
  43. * Errors include: *
  44. * -1 -- Error Reading or Decompressing data. *
  45. * -2 -- Not enough space in dst. You must make dst larger *
  46. * and then call the function again (remembering to *
  47. * reset the file descriptor to it's proper location. *
  48. * *
  49. * ================================================================ *
  50. * int *
  51. * G_write_compressed (fd, src, nbytes, compression_type) *
  52. * int fd, nbytes; *
  53. * unsigned char *src; *
  54. * ---------------------------------------------------------------- *
  55. * This is the basic function for writing and compressing a data *
  56. * chunk to a file. The file descriptor should be in the correct *
  57. * location prior to this call. The function will compress 'nbytes' *
  58. * of 'src' and write it to the file 'fd'. Returns the number of *
  59. * bytes written or an error code: *
  60. * *
  61. * Errors include: *
  62. * -1 -- Compression Failed. *
  63. * -2 -- Unable to write to file. *
  64. * *
  65. * ================================================================ *
  66. * int *
  67. * G_write_uncompressed (fd, src, nbytes) *
  68. * int fd, nbytes; *
  69. * unsigned char *src; *
  70. * ---------------------------------------------------------------- *
  71. * Works similar to G_write_compressed() except no attempt at *
  72. * compression is made. This is quicker, but may result in larger *
  73. * files. *
  74. * Returns the number of bytes written, or -1 for an error. It will *
  75. * return an error if it fails to write nbytes. Otherwise, the *
  76. * return value will always be nbytes + 1 (for compression flag). *
  77. * *
  78. ********************************************************************
  79. */
  80. #include <grass/config.h>
  81. #include <stdio.h>
  82. #include <stdlib.h>
  83. #include <string.h>
  84. #include <errno.h>
  85. #include <unistd.h>
  86. #include <grass/gis.h>
  87. #include <grass/glocale.h>
  88. #include "compress.h"
  89. #define G_COMPRESSED_NO (unsigned char)'0'
  90. #define G_COMPRESSED_YES (unsigned char)'1'
  91. /* get compressor number
  92. * return -1 on error
  93. * return number >= 0 for known processor */
  94. int G_compressor_number(char *name)
  95. {
  96. int i;
  97. if (!name)
  98. return -1;
  99. for (i = 0; compressor[i].name ; i++) {
  100. if (G_strcasecmp(name, compressor[i].name) == 0)
  101. return i;
  102. }
  103. return -1;
  104. }
  105. /* get compressor name
  106. * return NULL on error
  107. * return string (name) of known processor */
  108. char *G_compressor_name(int number)
  109. {
  110. if (number < 0 || number >= n_compressors)
  111. return NULL;
  112. return compressor[number].name;
  113. }
  114. int G_default_compressor(void)
  115. {
  116. #ifdef HAVE_ZSTD_H
  117. /* ZSTD */
  118. return 5;
  119. #endif
  120. /* ZLIB */
  121. return 2;
  122. }
  123. /* check compressor number
  124. * return -1 on error
  125. * return 0 known but not available
  126. * return 1 known and available */
  127. int G_check_compressor(int number)
  128. {
  129. if (number < 0 || number >= n_compressors) {
  130. G_warning(_("Request for unsupported compressor"));
  131. return -1;
  132. }
  133. return compressor[number].available;
  134. }
  135. int G_no_compress_bound(int src_sz)
  136. {
  137. return src_sz;
  138. }
  139. int
  140. G_no_compress(unsigned char *src, int src_sz, unsigned char *dst,
  141. int dst_sz)
  142. {
  143. /* Catch errors early */
  144. if (src == NULL || dst == NULL)
  145. return -1;
  146. /* Don't do anything if src is empty */
  147. if (src_sz <= 0)
  148. return 0;
  149. /* dst too small */
  150. if (dst_sz < src_sz)
  151. return -2;
  152. /* Copy the data from src to dst */
  153. memcpy(dst, src, src_sz);
  154. return src_sz;
  155. }
  156. int
  157. G_no_expand(unsigned char *src, int src_sz, unsigned char *dst,
  158. int dst_sz)
  159. {
  160. /* Catch errors early */
  161. if (src == NULL || dst == NULL)
  162. return -1;
  163. /* Don't do anything if src is empty */
  164. if (src_sz <= 0)
  165. return 0;
  166. /* dst too small */
  167. if (dst_sz < src_sz)
  168. return -2;
  169. /* Copy the data from src to dst */
  170. memcpy(dst, src, src_sz);
  171. return src_sz;
  172. }
  173. /* G_*_compress_bound() returns an upper bound on the compressed size
  174. * which can be larger than the input size
  175. * some compressors are a bit faster if the size of the destination
  176. * is at least the upper bound (no need to test for buffer overlflow)
  177. * read comments on the specific compressor interfaces
  178. */
  179. int G_compress_bound(int src_sz, int number)
  180. {
  181. if (number < 0 || number >= n_compressors) {
  182. G_fatal_error(_("Request for unsupported compressor"));
  183. return -1;
  184. }
  185. return compressor[number].bound(src_sz);
  186. }
  187. /* G_*_compress() returns
  188. * > 0: number of bytes in dst
  189. * 0: nothing done
  190. * -1: error
  191. * -2: dst too small
  192. */
  193. int G_compress(unsigned char *src, int src_sz, unsigned char *dst,
  194. int dst_sz, int number)
  195. {
  196. if (number < 0 || number >= n_compressors) {
  197. G_fatal_error(_("Request for unsupported compressor"));
  198. return -1;
  199. }
  200. return compressor[number].compress(src, src_sz, dst, dst_sz);
  201. }
  202. /* G_*_expand() returns
  203. * > 0: number of bytes in dst
  204. * -1: error
  205. */
  206. int G_expand(unsigned char *src, int src_sz, unsigned char *dst,
  207. int dst_sz, int number)
  208. {
  209. if (number < 0 || number >= n_compressors) {
  210. G_fatal_error(_("Request for unsupported compressor"));
  211. return -1;
  212. }
  213. return compressor[number].expand(src, src_sz, dst, dst_sz);
  214. }
  215. int G_read_compressed(int fd, int rbytes, unsigned char *dst, int nbytes,
  216. int number)
  217. {
  218. int bsize, nread, err;
  219. unsigned char *b;
  220. if (dst == NULL || nbytes <= 0) {
  221. if (dst == NULL)
  222. G_warning(_("No destination buffer allocated"));
  223. if (nbytes <= 0)
  224. G_warning(_("Invalid destination buffer size %d"), nbytes);
  225. return -2;
  226. }
  227. if (rbytes <= 0) {
  228. G_warning(_("Invalid read size %d"), nbytes);
  229. return -2;
  230. }
  231. bsize = rbytes;
  232. /* Our temporary input buffer for read */
  233. if (NULL == (b = (unsigned char *)
  234. G_calloc(bsize, sizeof(unsigned char))))
  235. return -1;
  236. /* Read from the file until we get our bsize or an error */
  237. nread = 0;
  238. do {
  239. err = read(fd, b + nread, bsize - nread);
  240. if (err >= 0)
  241. nread += err;
  242. } while (err > 0 && nread < bsize);
  243. if (err <= 0) {
  244. if (err == 0)
  245. G_warning(_("Unable to read %d bytes: end of file"), rbytes);
  246. else
  247. G_warning(_("Unable to read %d bytes: %s"), rbytes, strerror(errno));
  248. return -1;
  249. }
  250. /* If the bsize if less than rbytes and we didn't get an error.. */
  251. if (nread < rbytes) {
  252. G_free(b);
  253. G_warning("Unable to read %d bytes, got %d bytes", rbytes, nread);
  254. return -1;
  255. }
  256. /* Test if row is compressed */
  257. if (b[0] == G_COMPRESSED_NO) {
  258. /* Then just copy it to dst */
  259. for (err = 0; err < nread - 1 && err < nbytes; err++)
  260. dst[err] = b[err + 1];
  261. G_free(b);
  262. return (nread - 1);
  263. }
  264. else if (b[0] != G_COMPRESSED_YES) {
  265. /* We're not at the start of a row */
  266. G_free(b);
  267. G_warning("Read error: We're not at the start of a row");
  268. return -1;
  269. }
  270. /* Okay it's a compressed row */
  271. /* Just call G_expand() with the buffer we read,
  272. * Account for first byte being a flag
  273. */
  274. err = G_expand(b + 1, bsize - 1, dst, nbytes, number);
  275. /* We're done with b */
  276. G_free(b);
  277. /* Return whatever G_expand() returned */
  278. return err;
  279. } /* G_read_compressed() */
  280. int G_write_compressed(int fd, unsigned char *src, int nbytes,
  281. int number)
  282. {
  283. int dst_sz, nwritten, err;
  284. unsigned char *dst, compressed;
  285. /* Catch errors */
  286. if (src == NULL || nbytes < 0)
  287. return -1;
  288. /* get upper bound of compressed size */
  289. dst_sz = G_compress_bound(nbytes, number);
  290. if (NULL == (dst = (unsigned char *)
  291. G_calloc(dst_sz, sizeof(unsigned char))))
  292. return -1;
  293. /* Now just call G_compress() */
  294. err = G_compress(src, nbytes, dst, dst_sz, number);
  295. /* If compression succeeded write compressed row,
  296. * otherwise write uncompressed row. Compression will fail
  297. * if dst is too small (i.e. compressed data is larger)
  298. */
  299. if (err > 0 && err < nbytes) {
  300. dst_sz = err;
  301. /* Write the compression flag */
  302. compressed = G_COMPRESSED_YES;
  303. if (write(fd, &compressed, 1) != 1) {
  304. G_free(dst);
  305. return -1;
  306. }
  307. nwritten = 0;
  308. do {
  309. err = write(fd, dst + nwritten, dst_sz - nwritten);
  310. if (err >= 0)
  311. nwritten += err;
  312. } while (err > 0 && nwritten < dst_sz);
  313. /* Account for extra byte */
  314. nwritten++;
  315. }
  316. else {
  317. /* Write compression flag */
  318. compressed = G_COMPRESSED_NO;
  319. if (write(fd, &compressed, 1) != 1) {
  320. G_free(dst);
  321. return -1;
  322. }
  323. nwritten = 0;
  324. do {
  325. err = write(fd, src + nwritten, nbytes - nwritten);
  326. if (err >= 0)
  327. nwritten += err;
  328. } while (err > 0 && nwritten < nbytes);
  329. /* Account for extra byte */
  330. nwritten++;
  331. } /* if (err > 0) */
  332. /* Done with the dst buffer */
  333. G_free(dst);
  334. /* If we didn't write all the data return an error */
  335. if (err < 0)
  336. return -2;
  337. return nwritten;
  338. } /* G_write_compressed() */
  339. int G_write_uncompressed(int fd, const unsigned char *src, int nbytes)
  340. {
  341. int err, nwritten;
  342. unsigned char compressed;
  343. /* Catch errors */
  344. if (src == NULL || nbytes < 0)
  345. return -1;
  346. /* Write the compression flag */
  347. compressed = G_COMPRESSED_NO;
  348. if (write(fd, &compressed, 1) != 1)
  349. return -1;
  350. /* Now write the data */
  351. nwritten = 0;
  352. do {
  353. err = write(fd, src + nwritten, nbytes - nwritten);
  354. if (err > 0)
  355. nwritten += err;
  356. } while (err > 0 && nwritten < nbytes);
  357. if (err < 0 || nwritten != nbytes)
  358. return -1;
  359. /* Account for extra compressed flag */
  360. nwritten++;
  361. /* That's all */
  362. return nwritten;
  363. } /* G_write_uncompressed() */
  364. /* vim: set softtabstop=4 shiftwidth=4 expandtab: */