tif_zip.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* $Id: tif_zip.c,v 1.37 2017-05-10 15:21:16 erouault 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 suppressing 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/zlib.h" // ESENTHEL CHANGED
  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. #define SAFE_MSG(sp) ((sp)->stream.msg == NULL ? "" : (sp)->stream.msg)
  60. /*
  61. * State block for each open TIFF
  62. * file using ZIP compression/decompression.
  63. */
  64. typedef struct {
  65. TIFFPredictorState predict;
  66. z_stream stream;
  67. int zipquality; /* compression level */
  68. int state; /* state flags */
  69. #define ZSTATE_INIT_DECODE 0x01
  70. #define ZSTATE_INIT_ENCODE 0x02
  71. TIFFVGetMethod vgetparent; /* super-class method */
  72. TIFFVSetMethod vsetparent; /* super-class method */
  73. } ZIPState;
  74. #define ZState(tif) ((ZIPState*) (tif)->tif_data)
  75. #define DecoderState(tif) ZState(tif)
  76. #define EncoderState(tif) ZState(tif)
  77. static int ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
  78. static int ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
  79. static int
  80. ZIPFixupTags(TIFF* tif)
  81. {
  82. (void) tif;
  83. return (1);
  84. }
  85. static int
  86. ZIPSetupDecode(TIFF* tif)
  87. {
  88. static const char module[] = "ZIPSetupDecode";
  89. ZIPState* sp = DecoderState(tif);
  90. assert(sp != NULL);
  91. /* if we were last encoding, terminate this mode */
  92. if (sp->state & ZSTATE_INIT_ENCODE) {
  93. deflateEnd(&sp->stream);
  94. sp->state = 0;
  95. }
  96. /* This function can possibly be called several times by */
  97. /* PredictorSetupDecode() if this function succeeds but */
  98. /* PredictorSetup() fails */
  99. if ((sp->state & ZSTATE_INIT_DECODE) == 0 &&
  100. inflateInit(&sp->stream) != Z_OK) {
  101. TIFFErrorExt(tif->tif_clientdata, module, "%s", SAFE_MSG(sp));
  102. return (0);
  103. } else {
  104. sp->state |= ZSTATE_INIT_DECODE;
  105. return (1);
  106. }
  107. }
  108. /*
  109. * Setup state for decoding a strip.
  110. */
  111. static int
  112. ZIPPreDecode(TIFF* tif, uint16 s)
  113. {
  114. static const char module[] = "ZIPPreDecode";
  115. ZIPState* sp = DecoderState(tif);
  116. (void) s;
  117. assert(sp != NULL);
  118. if( (sp->state & ZSTATE_INIT_DECODE) == 0 )
  119. tif->tif_setupdecode( tif );
  120. sp->stream.next_in = tif->tif_rawdata;
  121. assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
  122. we need to simplify this code to reflect a ZLib that is likely updated
  123. to deal with 8byte memory sizes, though this code will respond
  124. appropriately even before we simplify it */
  125. sp->stream.avail_in = (uInt) tif->tif_rawcc;
  126. if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
  127. {
  128. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  129. return (0);
  130. }
  131. return (inflateReset(&sp->stream) == Z_OK);
  132. }
  133. static int
  134. ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  135. {
  136. static const char module[] = "ZIPDecode";
  137. ZIPState* sp = DecoderState(tif);
  138. (void) s;
  139. assert(sp != NULL);
  140. assert(sp->state == ZSTATE_INIT_DECODE);
  141. sp->stream.next_in = tif->tif_rawcp;
  142. sp->stream.avail_in = (uInt) tif->tif_rawcc;
  143. sp->stream.next_out = op;
  144. assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
  145. we need to simplify this code to reflect a ZLib that is likely updated
  146. to deal with 8byte memory sizes, though this code will respond
  147. appropriately even before we simplify it */
  148. sp->stream.avail_out = (uInt) occ;
  149. if ((tmsize_t)sp->stream.avail_out != occ)
  150. {
  151. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  152. return (0);
  153. }
  154. do {
  155. int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
  156. if (state == Z_STREAM_END)
  157. break;
  158. if (state == Z_DATA_ERROR) {
  159. TIFFErrorExt(tif->tif_clientdata, module,
  160. "Decoding error at scanline %lu, %s",
  161. (unsigned long) tif->tif_row, SAFE_MSG(sp));
  162. if (inflateSync(&sp->stream) != Z_OK)
  163. return (0);
  164. continue;
  165. }
  166. if (state != Z_OK) {
  167. TIFFErrorExt(tif->tif_clientdata, module,
  168. "ZLib error: %s", SAFE_MSG(sp));
  169. return (0);
  170. }
  171. } while (sp->stream.avail_out > 0);
  172. if (sp->stream.avail_out != 0) {
  173. TIFFErrorExt(tif->tif_clientdata, module,
  174. "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
  175. (unsigned long) tif->tif_row, (TIFF_UINT64_T) sp->stream.avail_out);
  176. return (0);
  177. }
  178. tif->tif_rawcp = sp->stream.next_in;
  179. tif->tif_rawcc = sp->stream.avail_in;
  180. return (1);
  181. }
  182. static int
  183. ZIPSetupEncode(TIFF* tif)
  184. {
  185. static const char module[] = "ZIPSetupEncode";
  186. ZIPState* sp = EncoderState(tif);
  187. assert(sp != NULL);
  188. if (sp->state & ZSTATE_INIT_DECODE) {
  189. inflateEnd(&sp->stream);
  190. sp->state = 0;
  191. }
  192. if (deflateInit(&sp->stream, sp->zipquality) != Z_OK) {
  193. TIFFErrorExt(tif->tif_clientdata, module, "%s", SAFE_MSG(sp));
  194. return (0);
  195. } else {
  196. sp->state |= ZSTATE_INIT_ENCODE;
  197. return (1);
  198. }
  199. }
  200. /*
  201. * Reset encoding state at the start of a strip.
  202. */
  203. static int
  204. ZIPPreEncode(TIFF* tif, uint16 s)
  205. {
  206. static const char module[] = "ZIPPreEncode";
  207. ZIPState *sp = EncoderState(tif);
  208. (void) s;
  209. assert(sp != NULL);
  210. if( sp->state != ZSTATE_INIT_ENCODE )
  211. tif->tif_setupencode( tif );
  212. sp->stream.next_out = tif->tif_rawdata;
  213. assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
  214. we need to simplify this code to reflect a ZLib that is likely updated
  215. to deal with 8byte memory sizes, though this code will respond
  216. appropriately even before we simplify it */
  217. sp->stream.avail_out = (uInt)tif->tif_rawdatasize;
  218. if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
  219. {
  220. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  221. return (0);
  222. }
  223. return (deflateReset(&sp->stream) == Z_OK);
  224. }
  225. /*
  226. * Encode a chunk of pixels.
  227. */
  228. static int
  229. ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  230. {
  231. static const char module[] = "ZIPEncode";
  232. ZIPState *sp = EncoderState(tif);
  233. assert(sp != NULL);
  234. assert(sp->state == ZSTATE_INIT_ENCODE);
  235. (void) s;
  236. sp->stream.next_in = bp;
  237. assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
  238. we need to simplify this code to reflect a ZLib that is likely updated
  239. to deal with 8byte memory sizes, though this code will respond
  240. appropriately even before we simplify it */
  241. sp->stream.avail_in = (uInt) cc;
  242. if ((tmsize_t)sp->stream.avail_in != cc)
  243. {
  244. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  245. return (0);
  246. }
  247. do {
  248. if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
  249. TIFFErrorExt(tif->tif_clientdata, module,
  250. "Encoder error: %s",
  251. SAFE_MSG(sp));
  252. return (0);
  253. }
  254. if (sp->stream.avail_out == 0) {
  255. tif->tif_rawcc = tif->tif_rawdatasize;
  256. TIFFFlushData1(tif);
  257. sp->stream.next_out = tif->tif_rawdata;
  258. sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
  259. }
  260. } while (sp->stream.avail_in > 0);
  261. return (1);
  262. }
  263. /*
  264. * Finish off an encoded strip by flushing the last
  265. * string and tacking on an End Of Information code.
  266. */
  267. static int
  268. ZIPPostEncode(TIFF* tif)
  269. {
  270. static const char module[] = "ZIPPostEncode";
  271. ZIPState *sp = EncoderState(tif);
  272. int state;
  273. sp->stream.avail_in = 0;
  274. do {
  275. state = deflate(&sp->stream, Z_FINISH);
  276. switch (state) {
  277. case Z_STREAM_END:
  278. case Z_OK:
  279. if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
  280. {
  281. tif->tif_rawcc = tif->tif_rawdatasize - sp->stream.avail_out;
  282. TIFFFlushData1(tif);
  283. sp->stream.next_out = tif->tif_rawdata;
  284. sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
  285. }
  286. break;
  287. default:
  288. TIFFErrorExt(tif->tif_clientdata, module,
  289. "ZLib error: %s", SAFE_MSG(sp));
  290. return (0);
  291. }
  292. } while (state != Z_STREAM_END);
  293. return (1);
  294. }
  295. static void
  296. ZIPCleanup(TIFF* tif)
  297. {
  298. ZIPState* sp = ZState(tif);
  299. assert(sp != 0);
  300. (void)TIFFPredictorCleanup(tif);
  301. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  302. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  303. if (sp->state & ZSTATE_INIT_ENCODE) {
  304. deflateEnd(&sp->stream);
  305. sp->state = 0;
  306. } else if( sp->state & ZSTATE_INIT_DECODE) {
  307. inflateEnd(&sp->stream);
  308. sp->state = 0;
  309. }
  310. _TIFFfree(sp);
  311. tif->tif_data = NULL;
  312. _TIFFSetDefaultCompressionState(tif);
  313. }
  314. static int
  315. ZIPVSetField(TIFF* tif, uint32 tag, va_list ap)
  316. {
  317. static const char module[] = "ZIPVSetField";
  318. ZIPState* sp = ZState(tif);
  319. switch (tag) {
  320. case TIFFTAG_ZIPQUALITY:
  321. sp->zipquality = (int) va_arg(ap, int);
  322. if ( sp->state&ZSTATE_INIT_ENCODE ) {
  323. if (deflateParams(&sp->stream,
  324. sp->zipquality, Z_DEFAULT_STRATEGY) != Z_OK) {
  325. TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
  326. SAFE_MSG(sp));
  327. return (0);
  328. }
  329. }
  330. return (1);
  331. default:
  332. return (*sp->vsetparent)(tif, tag, ap);
  333. }
  334. /*NOTREACHED*/
  335. }
  336. static int
  337. ZIPVGetField(TIFF* tif, uint32 tag, va_list ap)
  338. {
  339. ZIPState* sp = ZState(tif);
  340. switch (tag) {
  341. case TIFFTAG_ZIPQUALITY:
  342. *va_arg(ap, int*) = sp->zipquality;
  343. break;
  344. default:
  345. return (*sp->vgetparent)(tif, tag, ap);
  346. }
  347. return (1);
  348. }
  349. static const TIFFField zipFields[] = {
  350. { TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
  351. };
  352. int
  353. TIFFInitZIP(TIFF* tif, int scheme)
  354. {
  355. static const char module[] = "TIFFInitZIP";
  356. ZIPState* sp;
  357. assert( (scheme == COMPRESSION_DEFLATE)
  358. || (scheme == COMPRESSION_ADOBE_DEFLATE));
  359. /*
  360. * Merge codec-specific tag information.
  361. */
  362. if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields))) {
  363. TIFFErrorExt(tif->tif_clientdata, module,
  364. "Merging Deflate codec-specific tags failed");
  365. return 0;
  366. }
  367. /*
  368. * Allocate state block so tag methods have storage to record values.
  369. */
  370. tif->tif_data = (uint8*) _TIFFmalloc(sizeof (ZIPState));
  371. if (tif->tif_data == NULL)
  372. goto bad;
  373. sp = ZState(tif);
  374. sp->stream.zalloc = NULL;
  375. sp->stream.zfree = NULL;
  376. sp->stream.opaque = NULL;
  377. sp->stream.data_type = Z_BINARY;
  378. /*
  379. * Override parent get/set field methods.
  380. */
  381. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  382. tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
  383. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  384. tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
  385. /* Default values for codec-specific fields */
  386. sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
  387. sp->state = 0;
  388. /*
  389. * Install codec methods.
  390. */
  391. tif->tif_fixuptags = ZIPFixupTags;
  392. tif->tif_setupdecode = ZIPSetupDecode;
  393. tif->tif_predecode = ZIPPreDecode;
  394. tif->tif_decoderow = ZIPDecode;
  395. tif->tif_decodestrip = ZIPDecode;
  396. tif->tif_decodetile = ZIPDecode;
  397. tif->tif_setupencode = ZIPSetupEncode;
  398. tif->tif_preencode = ZIPPreEncode;
  399. tif->tif_postencode = ZIPPostEncode;
  400. tif->tif_encoderow = ZIPEncode;
  401. tif->tif_encodestrip = ZIPEncode;
  402. tif->tif_encodetile = ZIPEncode;
  403. tif->tif_cleanup = ZIPCleanup;
  404. /*
  405. * Setup predictor setup.
  406. */
  407. (void) TIFFPredictorInit(tif);
  408. return (1);
  409. bad:
  410. TIFFErrorExt(tif->tif_clientdata, module,
  411. "No space for ZIP state block");
  412. return (0);
  413. }
  414. #endif /* ZIP_SUPPORT */
  415. /* vim: set ts=8 sts=8 sw=8 noet: */
  416. /*
  417. * Local Variables:
  418. * mode: c
  419. * c-basic-offset: 8
  420. * fill-column: 78
  421. * End:
  422. */