flate.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. #define G_ZLIB_COMPRESSED_NO (unsigned char)'0'
  120. #define G_ZLIB_COMPRESSED_YES (unsigned char)'1'
  121. static void _init_zstruct(z_stream * z)
  122. {
  123. /* The types are defined in zlib.h, we set to NULL so zlib uses
  124. * its default functions.
  125. */
  126. z->zalloc = (alloc_func) 0;
  127. z->zfree = (free_func) 0;
  128. z->opaque = (voidpf) 0;
  129. }
  130. int G_zlib_read(int fd, int rbytes, unsigned char *dst, int nbytes)
  131. {
  132. int bsize, nread, err;
  133. unsigned char *b;
  134. if (dst == NULL || nbytes < 0)
  135. return -2;
  136. bsize = rbytes;
  137. /* Our temporary input buffer for read */
  138. if (NULL == (b = (unsigned char *)
  139. G_calloc(bsize, sizeof(unsigned char))))
  140. return -1;
  141. /* Read from the file until we get our bsize or an error */
  142. nread = 0;
  143. do {
  144. err = read(fd, b + nread, bsize - nread);
  145. if (err >= 0)
  146. nread += err;
  147. } while (err > 0 && nread < bsize);
  148. /* If the bsize if less than rbytes and we didn't get an error.. */
  149. if (nread < rbytes && err > 0) {
  150. G_free(b);
  151. return -1;
  152. }
  153. /* Test if row is compressed */
  154. if (b[0] == G_ZLIB_COMPRESSED_NO) {
  155. /* Then just copy it to dst */
  156. for (err = 0; err < nread - 1 && err < nbytes; err++)
  157. dst[err] = b[err + 1];
  158. G_free(b);
  159. return (nread - 1);
  160. }
  161. else if (b[0] != G_ZLIB_COMPRESSED_YES) {
  162. /* We're not at the start of a row */
  163. G_free(b);
  164. return -1;
  165. }
  166. /* Okay it's a compressed row */
  167. /* Just call G_zlib_expand() with the buffer we read,
  168. * Account for first byte being a flag
  169. */
  170. err = G_zlib_expand(b + 1, bsize - 1, dst, nbytes);
  171. /* We're done with b */
  172. G_free(b);
  173. /* Return whatever G_zlib_expand() returned */
  174. return err;
  175. } /* G_zlib_read() */
  176. int G_zlib_write(int fd, const unsigned char *src, int nbytes)
  177. {
  178. int dst_sz, nwritten, err;
  179. unsigned char *dst, compressed;
  180. /* Catch errors */
  181. if (src == NULL || nbytes < 0)
  182. return -1;
  183. dst_sz = nbytes;
  184. if (NULL == (dst = (unsigned char *)
  185. G_calloc(dst_sz, sizeof(unsigned char))))
  186. return -1;
  187. /* Now just call G_zlib_compress() */
  188. err = G_zlib_compress(src, nbytes, dst, dst_sz);
  189. /* If compression succeeded write compressed row,
  190. * otherwise write uncompressed row. Compression will fail
  191. * if dst is too small (i.e. compressed data is larger)
  192. */
  193. if (err > 0 && err <= dst_sz) {
  194. dst_sz = err;
  195. /* Write the compression flag */
  196. compressed = G_ZLIB_COMPRESSED_YES;
  197. if (write(fd, &compressed, 1) != 1) {
  198. G_free(dst);
  199. return -1;
  200. }
  201. nwritten = 0;
  202. do {
  203. err = write(fd, dst + nwritten, dst_sz - nwritten);
  204. if (err >= 0)
  205. nwritten += err;
  206. } while (err > 0 && nwritten < dst_sz);
  207. /* Account for extra byte */
  208. nwritten++;
  209. }
  210. else {
  211. /* Write compression flag */
  212. compressed = G_ZLIB_COMPRESSED_NO;
  213. if (write(fd, &compressed, 1) != 1) {
  214. G_free(dst);
  215. return -1;
  216. }
  217. nwritten = 0;
  218. do {
  219. err = write(fd, src + nwritten, nbytes - nwritten);
  220. if (err >= 0)
  221. nwritten += err;
  222. } while (err > 0 && nwritten < nbytes);
  223. /* Account for extra byte */
  224. nwritten++;
  225. } /* if (err > 0) */
  226. /* Done with the dst buffer */
  227. G_free(dst);
  228. /* If we didn't write all the data return an error */
  229. if (err < 0)
  230. return -2;
  231. return nwritten;
  232. } /* G_zlib_write() */
  233. int G_zlib_write_noCompress(int fd, const unsigned char *src, int nbytes)
  234. {
  235. int err, nwritten;
  236. unsigned char compressed;
  237. /* Catch errors */
  238. if (src == NULL || nbytes < 0)
  239. return -1;
  240. /* Write the compression flag */
  241. compressed = G_ZLIB_COMPRESSED_NO;
  242. if (write(fd, &compressed, 1) != 1)
  243. return -1;
  244. /* Now write the data */
  245. nwritten = 0;
  246. do {
  247. err = write(fd, src + nwritten, nbytes - nwritten);
  248. if (err > 0)
  249. nwritten += err;
  250. } while (err > 0 && nwritten < nbytes);
  251. if (err < 0 || nwritten != nbytes)
  252. return -1;
  253. /* Account for extra compressed flag */
  254. nwritten++;
  255. /* That's all */
  256. return nwritten;
  257. } /* G_zlib_write_noCompress() */
  258. int
  259. G_zlib_compress(const unsigned char *src, int src_sz, unsigned char *dst,
  260. int dst_sz)
  261. {
  262. int err, nbytes, buf_sz;
  263. unsigned char *buf;
  264. z_stream c_stream;
  265. /* Catch errors early */
  266. if (src == NULL || dst == NULL)
  267. return -1;
  268. /* Don't do anything if either of these are true */
  269. if (src_sz <= 0 || dst_sz <= 0)
  270. return 0;
  271. /* Output buffer has to be 1% + 12 bytes bigger for single pass deflate */
  272. buf_sz = (int)((double)dst_sz * 1.01 + (double)12);
  273. if (NULL == (buf = (unsigned char *)
  274. G_calloc(buf_sz, sizeof(unsigned char))))
  275. return -1;
  276. /* Set-up for default zlib memory handling */
  277. _init_zstruct(&c_stream);
  278. /* Set-up the stream */
  279. c_stream.avail_in = src_sz;
  280. c_stream.next_in = (unsigned char *) src;
  281. c_stream.avail_out = buf_sz;
  282. c_stream.next_out = buf;
  283. /* Initialize using default compression (usually 6) */
  284. err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
  285. /* If there was an error initializing, return -1 */
  286. if (err != Z_OK) {
  287. G_free(buf);
  288. return -1;
  289. }
  290. /* Do single pass compression */
  291. err = deflate(&c_stream, Z_FINISH);
  292. if (err != Z_STREAM_END) {
  293. switch (err) {
  294. case Z_OK: /* Destination too small */
  295. G_free(buf);
  296. deflateEnd(&c_stream);
  297. return -2;
  298. break;
  299. default: /* Give other error */
  300. G_free(buf);
  301. deflateEnd(&c_stream);
  302. return -1;
  303. break;
  304. }
  305. }
  306. /* avail_out is updated to bytes remaining in buf, so bytes of compressed
  307. * data is the original size minus that
  308. */
  309. nbytes = buf_sz - c_stream.avail_out;
  310. if (nbytes > dst_sz) { /* Not enough room to copy output */
  311. G_free(buf);
  312. return -2;
  313. }
  314. /* Copy the data from buf to dst */
  315. for (err = 0; err < nbytes; err++)
  316. dst[err] = buf[err];
  317. G_free(buf);
  318. deflateEnd(&c_stream);
  319. return nbytes;
  320. } /* G_zlib_compress() */
  321. int
  322. G_zlib_expand(const unsigned char *src, int src_sz, unsigned char *dst,
  323. int dst_sz)
  324. {
  325. int err, nbytes;
  326. z_stream c_stream;
  327. /* Catch error condition */
  328. if (src == NULL || dst == NULL)
  329. return -2;
  330. /* Don't do anything if either of these are true */
  331. if (src_sz <= 0 || dst_sz <= 0)
  332. return 0;
  333. /* Set-up default zlib memory handling */
  334. _init_zstruct(&c_stream);
  335. /* Set-up I/O streams */
  336. c_stream.avail_in = src_sz;
  337. c_stream.next_in = (unsigned char *)src;
  338. c_stream.avail_out = dst_sz;
  339. c_stream.next_out = dst;
  340. /* Call zlib initilization function */
  341. err = inflateInit(&c_stream);
  342. /* If not Z_OK return error -1 */
  343. if (err != Z_OK)
  344. return -1;
  345. /* Do single pass inflate */
  346. err = inflate(&c_stream, Z_FINISH);
  347. /* Number of bytes inflated to output stream is
  348. * original bytes available minus what avail_out now says
  349. */
  350. nbytes = dst_sz - c_stream.avail_out;
  351. /* Z_STREAM_END means all input was consumed,
  352. * Z_OK means only some was processed (not enough room in dst)
  353. */
  354. if (!(err == Z_STREAM_END || err == Z_OK)) {
  355. if (!(err == Z_BUF_ERROR && nbytes == dst_sz)) {
  356. inflateEnd(&c_stream);
  357. return -1;
  358. }
  359. /* Else, there was extra input, but requested output size was
  360. * decompressed successfully.
  361. */
  362. }
  363. inflateEnd(&c_stream);
  364. return nbytes;
  365. } /* G_zlib_expand() */
  366. #endif /* HAVE_ZLIB_H */
  367. /* vim: set softtabstop=4 shiftwidth=4 expandtab: */