flate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. ****************************************************************************
  3. * -- GRASS Development Team --
  4. *
  5. * MODULE: GRASS gis library
  6. * FILENAME: flate.c
  7. * AUTHOR(S): Eric G. Miller <egm2@jps.net>
  8. * PURPOSE: To provide an interface to libz for compressing and
  9. * decompressing data using DEFLATE. It's primary use is in
  10. * the storage and reading of GRASS floating point rasters.
  11. * It replaces the patented LZW compression interface.
  12. *
  13. * ALGORITHM: http://www.gzip.org/zlib/feldspar.html
  14. * DATE CREATED: Nov 19 2000
  15. * COPYRIGHT: (C) 2000 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_zlib_read (fd, rbytes, dst, nbytes) *
  25. * int fd, rbytes, nbytes; *
  26. * unsigned char *dst; *
  27. * ---------------------------------------------------------------- *
  28. * This is the basic function for reading a compressed chunk of a *
  29. * data file. The file descriptor should be in the proper location *
  30. * and the 'dst' array should have enough space for the data. *
  31. * 'nbytes' is the size of 'dst'. The 'rbytes' parameter is the *
  32. * number of bytes to read (knowable from the offsets index). For *
  33. * best results, 'nbytes' should be the exact amount of space *
  34. * needed for the expansion. Too large a value of nbytes may cause *
  35. * more data to be expanded than is desired. *
  36. * Returns: The number of bytes decompressed into dst, or an error. *
  37. * *
  38. * Errors include: *
  39. * -1 -- Error Reading or Decompressing data. *
  40. * -2 -- Not enough space in dst. You must make dst larger *
  41. * and then call the function again (remembering to *
  42. * reset the file descriptor to it's proper location. *
  43. * *
  44. * ================================================================ *
  45. * int *
  46. * G_zlib_write (fd, src, nbytes) *
  47. * int fd, nbytes; *
  48. * unsigned char *src; *
  49. * ---------------------------------------------------------------- *
  50. * This is the basic function for writing and compressing a data *
  51. * chunk to a file. The file descriptor should be in the correct *
  52. * location prior to this call. The function will compress 'nbytes' *
  53. * of 'src' and write it to the file 'fd'. Returns the number of *
  54. * bytes written or an error code: *
  55. * *
  56. * Errors include: *
  57. * -1 -- Compression Failed. *
  58. * -2 -- Unable to write to file. *
  59. * *
  60. * ================================================================ *
  61. * int *
  62. * G_zlib_write_noCompress (fd, src, nbytes) *
  63. * int fd, nbytes; *
  64. * unsigned char *src; *
  65. * ---------------------------------------------------------------- *
  66. * Works similar to G_zlib_write() except no attempt at compression *
  67. * is made. This is quicker, but may result in larger files. *
  68. * Returns the number of bytes written, or -1 for an error. It will *
  69. * return an error if it fails to write nbytes. Otherwise, the *
  70. * return value will always be nbytes + 1 (for compression flag). *
  71. * *
  72. * ================================================================ *
  73. * int *
  74. * G_zlib_compress (src, srz_sz, dst, dst_sz) *
  75. * int src_sz, dst_sz; *
  76. * unsigned char *src, *dst; *
  77. * ---------------------------------------------------------------- *
  78. * This function is a wrapper around the zlib deflate() function. *
  79. * It uses an all or nothing call to deflate(). If you need a *
  80. * continuous compression scheme, you'll have to code your own. *
  81. * In order to do a single pass compression, the input src must be *
  82. * copied to a buffer 1% + 12 bytes larger than the data. This may *
  83. * cause performance degradation. *
  84. * *
  85. * The function either returns the number of bytes of compressed *
  86. * data in dst, or an error code. *
  87. * *
  88. * Errors include: *
  89. * -1 -- Compression failed. *
  90. * -2 -- dst is too small. *
  91. * *
  92. * ================================================================ *
  93. * int *
  94. * G_zlib_expand (src, src_sz, dst, dst_sz) *
  95. * int src_sz, dst_sz; *
  96. * unsigned char *src, *dst; *
  97. * ---------------------------------------------------------------- *
  98. * This function is a wrapper around the zlib inflate() function. *
  99. * It uses a single pass call to inflate(). If you need a contin- *
  100. * uous expansion scheme, you'll have to code your own. *
  101. * *
  102. * The function returns the number of bytes expanded into 'dst' or *
  103. * and error code. *
  104. * *
  105. * Errors include: *
  106. * -1 -- Expansion failed. *
  107. * *
  108. ********************************************************************
  109. */
  110. #include <grass/config.h>
  111. #ifndef HAVE_ZLIB_H
  112. #error "GRASS requires libz to compile"
  113. #else
  114. #include <zlib.h>
  115. #include <stdio.h>
  116. #include <stdlib.h>
  117. #include <unistd.h>
  118. #include <grass/gis.h>
  119. #include "G.h"
  120. #define G_ZLIB_COMPRESSED_NO (unsigned char)'0'
  121. #define G_ZLIB_COMPRESSED_YES (unsigned char)'1'
  122. static void _init_zstruct(z_stream * z)
  123. {
  124. /* The types are defined in zlib.h, we set to NULL so zlib uses
  125. * its default functions.
  126. */
  127. z->zalloc = (alloc_func) 0;
  128. z->zfree = (free_func) 0;
  129. z->opaque = (voidpf) 0;
  130. }
  131. int G_zlib_read(int fd, int rbytes, unsigned char *dst, int nbytes)
  132. {
  133. int bsize, nread, err;
  134. unsigned char *b;
  135. if (dst == NULL || nbytes < 0)
  136. return -2;
  137. bsize = rbytes;
  138. /* Our temporary input buffer for read */
  139. if (NULL == (b = (unsigned char *)
  140. G_calloc(bsize, sizeof(unsigned char))))
  141. return -1;
  142. /* Read from the file until we get our bsize or an error */
  143. nread = 0;
  144. do {
  145. err = read(fd, b + nread, bsize - nread);
  146. if (err >= 0)
  147. nread += err;
  148. } while (err > 0 && nread < bsize);
  149. /* If the bsize if less than rbytes and we didn't get an error.. */
  150. if (nread < rbytes && err > 0) {
  151. G_free(b);
  152. return -1;
  153. }
  154. /* Test if row is compressed */
  155. if (b[0] == G_ZLIB_COMPRESSED_NO) {
  156. /* Then just copy it to dst */
  157. for (err = 0; err < nread - 1 && err < nbytes; err++)
  158. dst[err] = b[err + 1];
  159. G_free(b);
  160. return (nread - 1);
  161. }
  162. else if (b[0] != G_ZLIB_COMPRESSED_YES) {
  163. /* We're not at the start of a row */
  164. G_free(b);
  165. return -1;
  166. }
  167. /* Okay it's a compressed row */
  168. /* Just call G_zlib_expand() with the buffer we read,
  169. * Account for first byte being a flag
  170. */
  171. err = G_zlib_expand(b + 1, bsize - 1, dst, nbytes);
  172. /* We're done with b */
  173. G_free(b);
  174. /* Return whatever G_zlib_expand() returned */
  175. return err;
  176. } /* G_zlib_read() */
  177. int G_zlib_write(int fd, const unsigned char *src, int nbytes)
  178. {
  179. int dst_sz, nwritten, err;
  180. unsigned char *dst, compressed;
  181. /* Catch errors */
  182. if (src == NULL || nbytes < 0)
  183. return -1;
  184. dst_sz = nbytes;
  185. if (NULL == (dst = (unsigned char *)
  186. G_calloc(dst_sz, sizeof(unsigned char))))
  187. return -1;
  188. /* Now just call G_zlib_compress() */
  189. err = G_zlib_compress(src, nbytes, dst, dst_sz);
  190. /* If compression succeeded write compressed row,
  191. * otherwise write uncompressed row. Compression will fail
  192. * if dst is too small (i.e. compressed data is larger)
  193. */
  194. if (err > 0 && err <= dst_sz) {
  195. dst_sz = err;
  196. /* Write the compression flag */
  197. compressed = G_ZLIB_COMPRESSED_YES;
  198. if (write(fd, &compressed, 1) != 1) {
  199. G_free(dst);
  200. return -1;
  201. }
  202. nwritten = 0;
  203. do {
  204. err = write(fd, dst + nwritten, dst_sz - nwritten);
  205. if (err >= 0)
  206. nwritten += err;
  207. } while (err > 0 && nwritten < dst_sz);
  208. /* Account for extra byte */
  209. nwritten++;
  210. }
  211. else {
  212. /* Write compression flag */
  213. compressed = G_ZLIB_COMPRESSED_NO;
  214. if (write(fd, &compressed, 1) != 1) {
  215. G_free(dst);
  216. return -1;
  217. }
  218. nwritten = 0;
  219. do {
  220. err = write(fd, src + nwritten, nbytes - nwritten);
  221. if (err >= 0)
  222. nwritten += err;
  223. } while (err > 0 && nwritten < nbytes);
  224. /* Account for extra byte */
  225. nwritten++;
  226. } /* if (err > 0) */
  227. /* Done with the dst buffer */
  228. G_free(dst);
  229. /* If we didn't write all the data return an error */
  230. if (err < 0)
  231. return -2;
  232. return nwritten;
  233. } /* G_zlib_write() */
  234. int G_zlib_write_noCompress(int fd, const unsigned char *src, int nbytes)
  235. {
  236. int err, nwritten;
  237. unsigned char compressed;
  238. /* Catch errors */
  239. if (src == NULL || nbytes < 0)
  240. return -1;
  241. /* Write the compression flag */
  242. compressed = G_ZLIB_COMPRESSED_NO;
  243. if (write(fd, &compressed, 1) != 1)
  244. return -1;
  245. /* Now write the data */
  246. nwritten = 0;
  247. do {
  248. err = write(fd, src + nwritten, nbytes - nwritten);
  249. if (err > 0)
  250. nwritten += err;
  251. } while (err > 0 && nwritten < nbytes);
  252. if (err < 0 || nwritten != nbytes)
  253. return -1;
  254. /* Account for extra compressed flag */
  255. nwritten++;
  256. /* That's all */
  257. return nwritten;
  258. } /* G_zlib_write_noCompress() */
  259. int
  260. G_zlib_compress(const unsigned char *src, int src_sz, unsigned char *dst,
  261. int dst_sz)
  262. {
  263. int err, nbytes, buf_sz;
  264. unsigned char *buf;
  265. z_stream c_stream;
  266. /* Catch errors early */
  267. if (src == NULL || dst == NULL)
  268. return -1;
  269. /* Don't do anything if either of these are true */
  270. if (src_sz <= 0 || dst_sz <= 0)
  271. return 0;
  272. /* Output buffer has to be 1% + 12 bytes bigger for single pass deflate */
  273. buf_sz = (int)((double)dst_sz * 1.01 + (double)12);
  274. if (NULL == (buf = (unsigned char *)
  275. G_calloc(buf_sz, sizeof(unsigned char))))
  276. return -1;
  277. /* Set-up for default zlib memory handling */
  278. _init_zstruct(&c_stream);
  279. /* Set-up the stream */
  280. c_stream.avail_in = src_sz;
  281. c_stream.next_in = (unsigned char *) src;
  282. c_stream.avail_out = buf_sz;
  283. c_stream.next_out = buf;
  284. /* Initialize */
  285. /* Valid zlib compression levels -1 - 9 */
  286. /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
  287. * as used here, 1 gives the best compromise between speed and compression */
  288. err = deflateInit(&c_stream,
  289. (G__.compression_level < -1 || G__.compression_level > 9)
  290. ? 1 : G__.compression_level);
  291. /* If there was an error initializing, return -1 */
  292. if (err != Z_OK) {
  293. G_free(buf);
  294. return -1;
  295. }
  296. /* Do single pass compression */
  297. err = deflate(&c_stream, Z_FINISH);
  298. if (err != Z_STREAM_END) {
  299. switch (err) {
  300. case Z_OK: /* Destination too small */
  301. G_free(buf);
  302. deflateEnd(&c_stream);
  303. return -2;
  304. break;
  305. default: /* Give other error */
  306. G_free(buf);
  307. deflateEnd(&c_stream);
  308. return -1;
  309. break;
  310. }
  311. }
  312. /* avail_out is updated to bytes remaining in buf, so bytes of compressed
  313. * data is the original size minus that
  314. */
  315. nbytes = buf_sz - c_stream.avail_out;
  316. if (nbytes > dst_sz) { /* Not enough room to copy output */
  317. G_free(buf);
  318. deflateEnd(&c_stream);
  319. return -2;
  320. }
  321. /* Copy the data from buf to dst */
  322. for (err = 0; err < nbytes; err++)
  323. dst[err] = buf[err];
  324. G_free(buf);
  325. deflateEnd(&c_stream);
  326. return nbytes;
  327. } /* G_zlib_compress() */
  328. int
  329. G_zlib_expand(const unsigned char *src, int src_sz, unsigned char *dst,
  330. int dst_sz)
  331. {
  332. int err, nbytes;
  333. z_stream c_stream;
  334. /* Catch error condition */
  335. if (src == NULL || dst == NULL)
  336. return -2;
  337. /* Don't do anything if either of these are true */
  338. if (src_sz <= 0 || dst_sz <= 0)
  339. return 0;
  340. /* Set-up default zlib memory handling */
  341. _init_zstruct(&c_stream);
  342. /* Set-up I/O streams */
  343. c_stream.avail_in = src_sz;
  344. c_stream.next_in = (unsigned char *)src;
  345. c_stream.avail_out = dst_sz;
  346. c_stream.next_out = dst;
  347. /* Call zlib initilization function */
  348. err = inflateInit(&c_stream);
  349. /* If not Z_OK return error -1 */
  350. if (err != Z_OK)
  351. return -1;
  352. /* Do single pass inflate */
  353. err = inflate(&c_stream, Z_FINISH);
  354. /* Number of bytes inflated to output stream is
  355. * original bytes available minus what avail_out now says
  356. */
  357. nbytes = dst_sz - c_stream.avail_out;
  358. /* Z_STREAM_END means all input was consumed,
  359. * Z_OK means only some was processed (not enough room in dst)
  360. */
  361. if (!(err == Z_STREAM_END || err == Z_OK)) {
  362. if (!(err == Z_BUF_ERROR && nbytes == dst_sz)) {
  363. inflateEnd(&c_stream);
  364. return -1;
  365. }
  366. /* Else, there was extra input, but requested output size was
  367. * decompressed successfully.
  368. */
  369. }
  370. inflateEnd(&c_stream);
  371. return nbytes;
  372. } /* G_zlib_expand() */
  373. #endif /* HAVE_ZLIB_H */
  374. /* vim: set softtabstop=4 shiftwidth=4 expandtab: */