tif_zip.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /* $Id: tif_zip.c,v 1.31 2011-01-06 16:00:23 fwarmerdam Exp $ */
  2. /*
  3. * Copyright (c) 1995-1997 Sam Leffler
  4. * Copyright (c) 1995-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. #include "tiffiop.h"
  26. #ifdef ZIP_SUPPORT
  27. /*
  28. * TIFF Library.
  29. *
  30. * ZIP (aka Deflate) Compression Support
  31. *
  32. * This file is simply an interface to the zlib library written by
  33. * Jean-loup Gailly and Mark Adler. You must use version 1.0 or later
  34. * of the library: this code assumes the 1.0 API and also depends on
  35. * the ability to write the zlib header multiple times (one per strip)
  36. * which was not possible with versions prior to 0.95. Note also that
  37. * older versions of this codec avoided this bug by supressing the header
  38. * entirely. This means that files written with the old library cannot
  39. * be read; they should be converted to a different compression scheme
  40. * and then reconverted.
  41. *
  42. * The data format used by the zlib library is described in the files
  43. * zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available in the
  44. * directory ftp://ftp.uu.net/pub/archiving/zip/doc. The library was
  45. * last found at ftp://ftp.uu.net/pub/archiving/zip/zlib/zlib-0.99.tar.gz.
  46. */
  47. #include "tif_predict.h"
  48. #include "zlib.h"
  49. #include <stdio.h>
  50. /*
  51. * Sigh, ZLIB_VERSION is defined as a string so there's no
  52. * way to do a proper check here. Instead we guess based
  53. * on the presence of #defines that were added between the
  54. * 0.95 and 1.0 distributions.
  55. */
  56. #if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
  57. #error "Antiquated ZLIB software; you must use version 1.0 or later"
  58. #endif
  59. /*
  60. * State block for each open TIFF
  61. * file using ZIP compression/decompression.
  62. */
  63. typedef struct {
  64. TIFFPredictorState predict;
  65. z_stream stream;
  66. int zipquality; /* compression level */
  67. int state; /* state flags */
  68. #define ZSTATE_INIT_DECODE 0x01
  69. #define ZSTATE_INIT_ENCODE 0x02
  70. TIFFVGetMethod vgetparent; /* super-class method */
  71. TIFFVSetMethod vsetparent; /* super-class method */
  72. } ZIPState;
  73. #define ZState(tif) ((ZIPState*) (tif)->tif_data)
  74. #define DecoderState(tif) ZState(tif)
  75. #define EncoderState(tif) ZState(tif)
  76. static int ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
  77. static int ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
  78. static int
  79. ZIPFixupTags(TIFF* tif)
  80. {
  81. (void) tif;
  82. return (1);
  83. }
  84. static int
  85. ZIPSetupDecode(TIFF* tif)
  86. {
  87. static const char module[] = "ZIPSetupDecode";
  88. ZIPState* sp = DecoderState(tif);
  89. assert(sp != NULL);
  90. /* if we were last encoding, terminate this mode */
  91. if (sp->state & ZSTATE_INIT_ENCODE) {
  92. deflateEnd(&sp->stream);
  93. sp->state = 0;
  94. }
  95. if (inflateInit(&sp->stream) != Z_OK) {
  96. TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
  97. return (0);
  98. } else {
  99. sp->state |= ZSTATE_INIT_DECODE;
  100. return (1);
  101. }
  102. }
  103. /*
  104. * Setup state for decoding a strip.
  105. */
  106. static int
  107. ZIPPreDecode(TIFF* tif, uint16 s)
  108. {
  109. static const char module[] = "ZIPPreDecode";
  110. ZIPState* sp = DecoderState(tif);
  111. (void) s;
  112. assert(sp != NULL);
  113. if( (sp->state & ZSTATE_INIT_DECODE) == 0 )
  114. tif->tif_setupdecode( tif );
  115. sp->stream.next_in = tif->tif_rawdata;
  116. assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
  117. we need to simplify this code to reflect a ZLib that is likely updated
  118. to deal with 8byte memory sizes, though this code will respond
  119. apropriately even before we simplify it */
  120. sp->stream.avail_in = (uInt) tif->tif_rawcc;
  121. if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
  122. {
  123. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  124. return (0);
  125. }
  126. return (inflateReset(&sp->stream) == Z_OK);
  127. }
  128. static int
  129. ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  130. {
  131. static const char module[] = "ZIPDecode";
  132. ZIPState* sp = DecoderState(tif);
  133. (void) s;
  134. assert(sp != NULL);
  135. assert(sp->state == ZSTATE_INIT_DECODE);
  136. sp->stream.next_in = tif->tif_rawcp;
  137. sp->stream.avail_in = (uInt) tif->tif_rawcc;
  138. sp->stream.next_out = op;
  139. assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
  140. we need to simplify this code to reflect a ZLib that is likely updated
  141. to deal with 8byte memory sizes, though this code will respond
  142. apropriately even before we simplify it */
  143. sp->stream.avail_out = (uInt) occ;
  144. if ((tmsize_t)sp->stream.avail_out != occ)
  145. {
  146. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  147. return (0);
  148. }
  149. do {
  150. int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
  151. if (state == Z_STREAM_END)
  152. break;
  153. if (state == Z_DATA_ERROR) {
  154. TIFFErrorExt(tif->tif_clientdata, module,
  155. "Decoding error at scanline %lu, %s",
  156. (unsigned long) tif->tif_row, sp->stream.msg);
  157. if (inflateSync(&sp->stream) != Z_OK)
  158. return (0);
  159. continue;
  160. }
  161. if (state != Z_OK) {
  162. TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
  163. sp->stream.msg);
  164. return (0);
  165. }
  166. } while (sp->stream.avail_out > 0);
  167. if (sp->stream.avail_out != 0) {
  168. TIFFErrorExt(tif->tif_clientdata, module,
  169. "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
  170. (unsigned long) tif->tif_row, (TIFF_UINT64_T) sp->stream.avail_out);
  171. return (0);
  172. }
  173. tif->tif_rawcp = sp->stream.next_in;
  174. tif->tif_rawcc = sp->stream.avail_in;
  175. return (1);
  176. }
  177. static int
  178. ZIPSetupEncode(TIFF* tif)
  179. {
  180. static const char module[] = "ZIPSetupEncode";
  181. ZIPState* sp = EncoderState(tif);
  182. assert(sp != NULL);
  183. if (sp->state & ZSTATE_INIT_DECODE) {
  184. inflateEnd(&sp->stream);
  185. sp->state = 0;
  186. }
  187. if (deflateInit(&sp->stream, sp->zipquality) != Z_OK) {
  188. TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
  189. return (0);
  190. } else {
  191. sp->state |= ZSTATE_INIT_ENCODE;
  192. return (1);
  193. }
  194. }
  195. /*
  196. * Reset encoding state at the start of a strip.
  197. */
  198. static int
  199. ZIPPreEncode(TIFF* tif, uint16 s)
  200. {
  201. static const char module[] = "ZIPPreEncode";
  202. ZIPState *sp = EncoderState(tif);
  203. (void) s;
  204. assert(sp != NULL);
  205. if( sp->state != ZSTATE_INIT_ENCODE )
  206. tif->tif_setupencode( tif );
  207. sp->stream.next_out = tif->tif_rawdata;
  208. assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
  209. we need to simplify this code to reflect a ZLib that is likely updated
  210. to deal with 8byte memory sizes, though this code will respond
  211. apropriately even before we simplify it */
  212. sp->stream.avail_out = tif->tif_rawdatasize;
  213. if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
  214. {
  215. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  216. return (0);
  217. }
  218. return (deflateReset(&sp->stream) == Z_OK);
  219. }
  220. /*
  221. * Encode a chunk of pixels.
  222. */
  223. static int
  224. ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  225. {
  226. static const char module[] = "ZIPEncode";
  227. ZIPState *sp = EncoderState(tif);
  228. assert(sp != NULL);
  229. assert(sp->state == ZSTATE_INIT_ENCODE);
  230. (void) s;
  231. sp->stream.next_in = bp;
  232. assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
  233. we need to simplify this code to reflect a ZLib that is likely updated
  234. to deal with 8byte memory sizes, though this code will respond
  235. apropriately even before we simplify it */
  236. sp->stream.avail_in = (uInt) cc;
  237. if ((tmsize_t)sp->stream.avail_in != cc)
  238. {
  239. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  240. return (0);
  241. }
  242. do {
  243. if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
  244. TIFFErrorExt(tif->tif_clientdata, module, "Encoder error: %s",
  245. sp->stream.msg);
  246. return (0);
  247. }
  248. if (sp->stream.avail_out == 0) {
  249. tif->tif_rawcc = tif->tif_rawdatasize;
  250. TIFFFlushData1(tif);
  251. sp->stream.next_out = tif->tif_rawdata;
  252. sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
  253. }
  254. } while (sp->stream.avail_in > 0);
  255. return (1);
  256. }
  257. /*
  258. * Finish off an encoded strip by flushing the last
  259. * string and tacking on an End Of Information code.
  260. */
  261. static int
  262. ZIPPostEncode(TIFF* tif)
  263. {
  264. static const char module[] = "ZIPPostEncode";
  265. ZIPState *sp = EncoderState(tif);
  266. int state;
  267. sp->stream.avail_in = 0;
  268. do {
  269. state = deflate(&sp->stream, Z_FINISH);
  270. switch (state) {
  271. case Z_STREAM_END:
  272. case Z_OK:
  273. if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
  274. {
  275. tif->tif_rawcc = tif->tif_rawdatasize - sp->stream.avail_out;
  276. TIFFFlushData1(tif);
  277. sp->stream.next_out = tif->tif_rawdata;
  278. sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
  279. }
  280. break;
  281. default:
  282. TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
  283. sp->stream.msg);
  284. return (0);
  285. }
  286. } while (state != Z_STREAM_END);
  287. return (1);
  288. }
  289. static void
  290. ZIPCleanup(TIFF* tif)
  291. {
  292. ZIPState* sp = ZState(tif);
  293. assert(sp != 0);
  294. (void)TIFFPredictorCleanup(tif);
  295. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  296. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  297. if (sp->state & ZSTATE_INIT_ENCODE) {
  298. deflateEnd(&sp->stream);
  299. sp->state = 0;
  300. } else if( sp->state & ZSTATE_INIT_DECODE) {
  301. inflateEnd(&sp->stream);
  302. sp->state = 0;
  303. }
  304. _TIFFfree(sp);
  305. tif->tif_data = NULL;
  306. _TIFFSetDefaultCompressionState(tif);
  307. }
  308. static int
  309. ZIPVSetField(TIFF* tif, uint32 tag, va_list ap)
  310. {
  311. static const char module[] = "ZIPVSetField";
  312. ZIPState* sp = ZState(tif);
  313. switch (tag) {
  314. case TIFFTAG_ZIPQUALITY:
  315. sp->zipquality = (int) va_arg(ap, int);
  316. if ( sp->state&ZSTATE_INIT_ENCODE ) {
  317. if (deflateParams(&sp->stream,
  318. sp->zipquality, Z_DEFAULT_STRATEGY) != Z_OK) {
  319. TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
  320. sp->stream.msg);
  321. return (0);
  322. }
  323. }
  324. return (1);
  325. default:
  326. return (*sp->vsetparent)(tif, tag, ap);
  327. }
  328. /*NOTREACHED*/
  329. }
  330. static int
  331. ZIPVGetField(TIFF* tif, uint32 tag, va_list ap)
  332. {
  333. ZIPState* sp = ZState(tif);
  334. switch (tag) {
  335. case TIFFTAG_ZIPQUALITY:
  336. *va_arg(ap, int*) = sp->zipquality;
  337. break;
  338. default:
  339. return (*sp->vgetparent)(tif, tag, ap);
  340. }
  341. return (1);
  342. }
  343. static const TIFFField zipFields[] = {
  344. { TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
  345. };
  346. int
  347. TIFFInitZIP(TIFF* tif, int scheme)
  348. {
  349. static const char module[] = "TIFFInitZIP";
  350. ZIPState* sp;
  351. assert( (scheme == COMPRESSION_DEFLATE)
  352. || (scheme == COMPRESSION_ADOBE_DEFLATE));
  353. /*
  354. * Merge codec-specific tag information.
  355. */
  356. if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields))) {
  357. TIFFErrorExt(tif->tif_clientdata, module,
  358. "Merging Deflate codec-specific tags failed");
  359. return 0;
  360. }
  361. /*
  362. * Allocate state block so tag methods have storage to record values.
  363. */
  364. tif->tif_data = (uint8*) _TIFFmalloc(sizeof (ZIPState));
  365. if (tif->tif_data == NULL)
  366. goto bad;
  367. sp = ZState(tif);
  368. sp->stream.zalloc = NULL;
  369. sp->stream.zfree = NULL;
  370. sp->stream.opaque = NULL;
  371. sp->stream.data_type = Z_BINARY;
  372. /*
  373. * Override parent get/set field methods.
  374. */
  375. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  376. tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
  377. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  378. tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
  379. /* Default values for codec-specific fields */
  380. sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
  381. sp->state = 0;
  382. /*
  383. * Install codec methods.
  384. */
  385. tif->tif_fixuptags = ZIPFixupTags;
  386. tif->tif_setupdecode = ZIPSetupDecode;
  387. tif->tif_predecode = ZIPPreDecode;
  388. tif->tif_decoderow = ZIPDecode;
  389. tif->tif_decodestrip = ZIPDecode;
  390. tif->tif_decodetile = ZIPDecode;
  391. tif->tif_setupencode = ZIPSetupEncode;
  392. tif->tif_preencode = ZIPPreEncode;
  393. tif->tif_postencode = ZIPPostEncode;
  394. tif->tif_encoderow = ZIPEncode;
  395. tif->tif_encodestrip = ZIPEncode;
  396. tif->tif_encodetile = ZIPEncode;
  397. tif->tif_cleanup = ZIPCleanup;
  398. /*
  399. * Setup predictor setup.
  400. */
  401. (void) TIFFPredictorInit(tif);
  402. return (1);
  403. bad:
  404. TIFFErrorExt(tif->tif_clientdata, module,
  405. "No space for ZIP state block");
  406. return (0);
  407. }
  408. #endif /* ZIP_SUPORT */
  409. /* vim: set ts=8 sts=8 sw=8 noet: */
  410. /*
  411. * Local Variables:
  412. * mode: c
  413. * c-basic-offset: 8
  414. * fill-column: 78
  415. * End:
  416. */