tif_zip.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * Copyright (c) 1995-1997 Sam Leffler
  3. * Copyright (c) 1995-1997 Silicon Graphics, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10. * publicity relating to the software without the specific, prior written
  11. * permission of Sam Leffler and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include "tiffiop.h"
  25. #ifdef ZIP_SUPPORT
  26. /*
  27. * TIFF Library.
  28. *
  29. * ZIP (aka Deflate) Compression Support
  30. *
  31. * This file is an interface to the zlib library written by
  32. * Jean-loup Gailly and Mark Adler. You must use version 1.0 or later
  33. * of the library.
  34. *
  35. * Optionally, libdeflate (https://github.com/ebiggers/libdeflate) may be used
  36. * to do the compression and decompression, but only for whole strips and tiles.
  37. * For scanline access, zlib will be sued as a fallback.
  38. */
  39. #include "tif_predict.h"
  40. #include "zlib.h"
  41. #if LIBDEFLATE_SUPPORT
  42. #include "libdeflate.h"
  43. #endif
  44. #define LIBDEFLATE_MAX_COMPRESSION_LEVEL 12
  45. #include <stdio.h>
  46. /*
  47. * Sigh, ZLIB_VERSION is defined as a string so there's no
  48. * way to do a proper check here. Instead we guess based
  49. * on the presence of #defines that were added between the
  50. * 0.95 and 1.0 distributions.
  51. */
  52. #if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
  53. #error "Antiquated ZLIB software; you must use version 1.0 or later"
  54. #endif
  55. #define SAFE_MSG(sp) ((sp)->stream.msg == NULL ? "" : (sp)->stream.msg)
  56. /*
  57. * State block for each open TIFF
  58. * file using ZIP compression/decompression.
  59. */
  60. typedef struct {
  61. TIFFPredictorState predict;
  62. z_stream stream;
  63. int zipquality; /* compression level */
  64. int state; /* state flags */
  65. int subcodec; /* DEFLATE_SUBCODEC_ZLIB or DEFLATE_SUBCODEC_LIBDEFLATE */
  66. #if LIBDEFLATE_SUPPORT
  67. int libdeflate_state; /* -1 = until first time ZIPEncode() / ZIPDecode() is called, 0 = use zlib, 1 = use libdeflate */
  68. struct libdeflate_decompressor* libdeflate_dec;
  69. struct libdeflate_compressor* libdeflate_enc;
  70. #endif
  71. #define ZSTATE_INIT_DECODE 0x01
  72. #define ZSTATE_INIT_ENCODE 0x02
  73. TIFFVGetMethod vgetparent; /* super-class method */
  74. TIFFVSetMethod vsetparent; /* super-class method */
  75. } ZIPState;
  76. #define ZState(tif) ((ZIPState*) (tif)->tif_data)
  77. #define DecoderState(tif) ZState(tif)
  78. #define EncoderState(tif) ZState(tif)
  79. static int ZIPEncode(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s);
  80. static int ZIPDecode(TIFF* tif, uint8_t* op, tmsize_t occ, uint16_t s);
  81. static int
  82. ZIPFixupTags(TIFF* tif)
  83. {
  84. (void) tif;
  85. return (1);
  86. }
  87. static int
  88. ZIPSetupDecode(TIFF* tif)
  89. {
  90. static const char module[] = "ZIPSetupDecode";
  91. ZIPState* sp = DecoderState(tif);
  92. assert(sp != NULL);
  93. /* if we were last encoding, terminate this mode */
  94. if (sp->state & ZSTATE_INIT_ENCODE) {
  95. deflateEnd(&sp->stream);
  96. sp->state = 0;
  97. }
  98. /* This function can possibly be called several times by */
  99. /* PredictorSetupDecode() if this function succeeds but */
  100. /* PredictorSetup() fails */
  101. if ((sp->state & ZSTATE_INIT_DECODE) == 0 &&
  102. inflateInit(&sp->stream) != Z_OK) {
  103. TIFFErrorExt(tif->tif_clientdata, module, "%s", SAFE_MSG(sp));
  104. return (0);
  105. } else {
  106. sp->state |= ZSTATE_INIT_DECODE;
  107. return (1);
  108. }
  109. }
  110. /*
  111. * Setup state for decoding a strip.
  112. */
  113. static int
  114. ZIPPreDecode(TIFF* tif, uint16_t s)
  115. {
  116. ZIPState* sp = DecoderState(tif);
  117. (void) s;
  118. assert(sp != NULL);
  119. if( (sp->state & ZSTATE_INIT_DECODE) == 0 )
  120. tif->tif_setupdecode( tif );
  121. #if LIBDEFLATE_SUPPORT
  122. sp->libdeflate_state = -1;
  123. #endif
  124. sp->stream.next_in = tif->tif_rawdata;
  125. assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
  126. we need to simplify this code to reflect a ZLib that is likely updated
  127. to deal with 8byte memory sizes, though this code will respond
  128. appropriately even before we simplify it */
  129. sp->stream.avail_in = (uint64_t)tif->tif_rawcc < 0xFFFFFFFFU ? (uInt) tif->tif_rawcc : 0xFFFFFFFFU;
  130. return (inflateReset(&sp->stream) == Z_OK);
  131. }
  132. static int
  133. ZIPDecode(TIFF* tif, uint8_t* op, tmsize_t occ, uint16_t s)
  134. {
  135. static const char module[] = "ZIPDecode";
  136. ZIPState* sp = DecoderState(tif);
  137. (void) s;
  138. assert(sp != NULL);
  139. assert(sp->state == ZSTATE_INIT_DECODE);
  140. #if LIBDEFLATE_SUPPORT
  141. if( sp->libdeflate_state == 1 )
  142. return 0;
  143. /* If we have libdeflate support and we are asked to read a whole */
  144. /* strip/tile, then go for using it */
  145. do {
  146. TIFFDirectory *td = &tif->tif_dir;
  147. if( sp->libdeflate_state == 0 )
  148. break;
  149. if( sp->subcodec == DEFLATE_SUBCODEC_ZLIB )
  150. break;
  151. /* Check if we are in the situation where we can use libdeflate */
  152. if (isTiled(tif)) {
  153. if( TIFFTileSize64(tif) != (uint64_t)occ )
  154. break;
  155. } else {
  156. uint32_t strip_height = td->td_imagelength - tif->tif_row;
  157. if (strip_height > td->td_rowsperstrip)
  158. strip_height = td->td_rowsperstrip;
  159. if( TIFFVStripSize64(tif, strip_height) != (uint64_t)occ )
  160. break;
  161. }
  162. /* Check for overflow */
  163. if( (size_t)tif->tif_rawcc != (uint64_t)tif->tif_rawcc )
  164. break;
  165. if( (size_t)occ != (uint64_t)occ )
  166. break;
  167. /* Go for decompression using libdeflate */
  168. {
  169. enum libdeflate_result res;
  170. if( sp->libdeflate_dec == NULL )
  171. {
  172. sp->libdeflate_dec = libdeflate_alloc_decompressor();
  173. if( sp->libdeflate_dec == NULL )
  174. {
  175. break;
  176. }
  177. }
  178. sp->libdeflate_state = 1;
  179. res = libdeflate_zlib_decompress(
  180. sp->libdeflate_dec, tif->tif_rawcp, (size_t)tif->tif_rawcc, op, (size_t)occ, NULL);
  181. tif->tif_rawcp += tif->tif_rawcc;
  182. tif->tif_rawcc = 0;
  183. /* We accept LIBDEFLATE_INSUFFICIENT_SPACE has a return */
  184. /* There are odd files in the wild where the last strip, when */
  185. /* it is smaller in height than td_rowsperstrip, actually contains */
  186. /* data for td_rowsperstrip lines. Just ignore that silently. */
  187. if( res != LIBDEFLATE_SUCCESS &&
  188. res != LIBDEFLATE_INSUFFICIENT_SPACE )
  189. {
  190. TIFFErrorExt(tif->tif_clientdata, module,
  191. "Decoding error at scanline %lu",
  192. (unsigned long) tif->tif_row);
  193. return 0;
  194. }
  195. return 1;
  196. }
  197. } while(0);
  198. sp->libdeflate_state = 0;
  199. #endif /* LIBDEFLATE_SUPPORT */
  200. sp->stream.next_in = tif->tif_rawcp;
  201. sp->stream.next_out = op;
  202. assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
  203. we need to simplify this code to reflect a ZLib that is likely updated
  204. to deal with 8byte memory sizes, though this code will respond
  205. appropriately even before we simplify it */
  206. do {
  207. int state;
  208. uInt avail_in_before = (uint64_t)tif->tif_rawcc <= 0xFFFFFFFFU ? (uInt)tif->tif_rawcc : 0xFFFFFFFFU;
  209. uInt avail_out_before = (uint64_t)occ < 0xFFFFFFFFU ? (uInt) occ : 0xFFFFFFFFU;
  210. sp->stream.avail_in = avail_in_before;
  211. sp->stream.avail_out = avail_out_before;
  212. state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
  213. tif->tif_rawcc -= (avail_in_before - sp->stream.avail_in);
  214. occ -= (avail_out_before - sp->stream.avail_out);
  215. if (state == Z_STREAM_END)
  216. break;
  217. if (state == Z_DATA_ERROR) {
  218. TIFFErrorExt(tif->tif_clientdata, module,
  219. "Decoding error at scanline %lu, %s",
  220. (unsigned long) tif->tif_row, SAFE_MSG(sp));
  221. return (0);
  222. }
  223. if (state != Z_OK) {
  224. TIFFErrorExt(tif->tif_clientdata, module,
  225. "ZLib error: %s", SAFE_MSG(sp));
  226. return (0);
  227. }
  228. } while (occ > 0);
  229. if (occ != 0) {
  230. TIFFErrorExt(tif->tif_clientdata, module,
  231. "Not enough data at scanline %lu (short %" PRIu64 " bytes)",
  232. (unsigned long) tif->tif_row, (uint64_t) occ);
  233. return (0);
  234. }
  235. tif->tif_rawcp = sp->stream.next_in;
  236. return (1);
  237. }
  238. static int
  239. ZIPSetupEncode(TIFF* tif)
  240. {
  241. static const char module[] = "ZIPSetupEncode";
  242. ZIPState* sp = EncoderState(tif);
  243. int cappedQuality;
  244. assert(sp != NULL);
  245. if (sp->state & ZSTATE_INIT_DECODE) {
  246. inflateEnd(&sp->stream);
  247. sp->state = 0;
  248. }
  249. cappedQuality = sp->zipquality;
  250. if( cappedQuality > Z_BEST_COMPRESSION )
  251. cappedQuality = Z_BEST_COMPRESSION;
  252. if (deflateInit(&sp->stream, cappedQuality) != Z_OK) {
  253. TIFFErrorExt(tif->tif_clientdata, module, "%s", SAFE_MSG(sp));
  254. return (0);
  255. } else {
  256. sp->state |= ZSTATE_INIT_ENCODE;
  257. return (1);
  258. }
  259. }
  260. /*
  261. * Reset encoding state at the start of a strip.
  262. */
  263. static int
  264. ZIPPreEncode(TIFF* tif, uint16_t s)
  265. {
  266. ZIPState *sp = EncoderState(tif);
  267. (void) s;
  268. assert(sp != NULL);
  269. if( sp->state != ZSTATE_INIT_ENCODE )
  270. tif->tif_setupencode( tif );
  271. #if LIBDEFLATE_SUPPORT
  272. sp->libdeflate_state = -1;
  273. #endif
  274. sp->stream.next_out = tif->tif_rawdata;
  275. assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
  276. we need to simplify this code to reflect a ZLib that is likely updated
  277. to deal with 8byte memory sizes, though this code will respond
  278. appropriately even before we simplify it */
  279. sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU ? (uInt)tif->tif_rawdatasize : 0xFFFFFFFFU;
  280. return (deflateReset(&sp->stream) == Z_OK);
  281. }
  282. /*
  283. * Encode a chunk of pixels.
  284. */
  285. static int
  286. ZIPEncode(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s)
  287. {
  288. static const char module[] = "ZIPEncode";
  289. ZIPState *sp = EncoderState(tif);
  290. assert(sp != NULL);
  291. assert(sp->state == ZSTATE_INIT_ENCODE);
  292. (void) s;
  293. #if LIBDEFLATE_SUPPORT
  294. if( sp->libdeflate_state == 1 )
  295. return 0;
  296. /* If we have libdeflate support and we are asked to write a whole */
  297. /* strip/tile, then go for using it */
  298. do {
  299. TIFFDirectory *td = &tif->tif_dir;
  300. if( sp->libdeflate_state == 0 )
  301. break;
  302. if( sp->subcodec == DEFLATE_SUBCODEC_ZLIB )
  303. break;
  304. /* Libdeflate does not support the 0-compression level */
  305. if( sp->zipquality == Z_NO_COMPRESSION )
  306. break;
  307. /* Check if we are in the situation where we can use libdeflate */
  308. if (isTiled(tif)) {
  309. if( TIFFTileSize64(tif) != (uint64_t)cc )
  310. break;
  311. } else {
  312. uint32_t strip_height = td->td_imagelength - tif->tif_row;
  313. if (strip_height > td->td_rowsperstrip)
  314. strip_height = td->td_rowsperstrip;
  315. if( TIFFVStripSize64(tif, strip_height) != (uint64_t)cc )
  316. break;
  317. }
  318. /* Check for overflow */
  319. if( (size_t)tif->tif_rawdatasize != (uint64_t)tif->tif_rawdatasize )
  320. break;
  321. if( (size_t)cc != (uint64_t)cc )
  322. break;
  323. /* Go for compression using libdeflate */
  324. {
  325. size_t nCompressedBytes;
  326. if( sp->libdeflate_enc == NULL )
  327. {
  328. /* To get results as good as zlib, we asked for an extra */
  329. /* level of compression */
  330. sp->libdeflate_enc = libdeflate_alloc_compressor(
  331. sp->zipquality == Z_DEFAULT_COMPRESSION ? 7 :
  332. sp->zipquality >= 6 && sp->zipquality <= 9 ? sp->zipquality + 1 :
  333. sp->zipquality);
  334. if( sp->libdeflate_enc == NULL )
  335. {
  336. TIFFErrorExt(tif->tif_clientdata, module,
  337. "Cannot allocate compressor");
  338. break;
  339. }
  340. }
  341. /* Make sure the output buffer is large enough for the worse case. */
  342. /* In TIFFWriteBufferSetup(), when libtiff allocates the buffer */
  343. /* we've taken a 10% margin over the uncompressed size, which should */
  344. /* be large enough even for the the worse case scenario. */
  345. if( libdeflate_zlib_compress_bound(sp->libdeflate_enc, (size_t)cc) >
  346. (size_t)tif->tif_rawdatasize)
  347. {
  348. break;
  349. }
  350. sp->libdeflate_state = 1;
  351. nCompressedBytes = libdeflate_zlib_compress(
  352. sp->libdeflate_enc, bp, (size_t)cc, tif->tif_rawdata, (size_t)tif->tif_rawdatasize);
  353. if( nCompressedBytes == 0 )
  354. {
  355. TIFFErrorExt(tif->tif_clientdata, module,
  356. "Encoder error at scanline %lu",
  357. (unsigned long) tif->tif_row);
  358. return 0;
  359. }
  360. tif->tif_rawcc = nCompressedBytes;
  361. if( !TIFFFlushData1(tif) )
  362. return 0;
  363. return 1;
  364. }
  365. } while(0);
  366. sp->libdeflate_state = 0;
  367. #endif /* LIBDEFLATE_SUPPORT */
  368. sp->stream.next_in = bp;
  369. assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
  370. we need to simplify this code to reflect a ZLib that is likely updated
  371. to deal with 8byte memory sizes, though this code will respond
  372. appropriately even before we simplify it */
  373. do {
  374. uInt avail_in_before = (uint64_t)cc <= 0xFFFFFFFFU ? (uInt)cc : 0xFFFFFFFFU;
  375. sp->stream.avail_in = avail_in_before;
  376. if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
  377. TIFFErrorExt(tif->tif_clientdata, module,
  378. "Encoder error: %s",
  379. SAFE_MSG(sp));
  380. return (0);
  381. }
  382. if (sp->stream.avail_out == 0) {
  383. tif->tif_rawcc = tif->tif_rawdatasize;
  384. if (!TIFFFlushData1(tif))
  385. return 0;
  386. sp->stream.next_out = tif->tif_rawdata;
  387. sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU ? (uInt)tif->tif_rawdatasize : 0xFFFFFFFFU;
  388. }
  389. cc -= (avail_in_before - sp->stream.avail_in);
  390. } while (cc > 0);
  391. return (1);
  392. }
  393. /*
  394. * Finish off an encoded strip by flushing the last
  395. * string and tacking on an End Of Information code.
  396. */
  397. static int
  398. ZIPPostEncode(TIFF* tif)
  399. {
  400. static const char module[] = "ZIPPostEncode";
  401. ZIPState *sp = EncoderState(tif);
  402. int state;
  403. #if LIBDEFLATE_SUPPORT
  404. if( sp->libdeflate_state == 1 )
  405. return 1;
  406. #endif
  407. sp->stream.avail_in = 0;
  408. do {
  409. state = deflate(&sp->stream, Z_FINISH);
  410. switch (state) {
  411. case Z_STREAM_END:
  412. case Z_OK:
  413. if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
  414. {
  415. tif->tif_rawcc = tif->tif_rawdatasize - sp->stream.avail_out;
  416. if (!TIFFFlushData1(tif))
  417. return 0;
  418. sp->stream.next_out = tif->tif_rawdata;
  419. sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU ? (uInt)tif->tif_rawdatasize : 0xFFFFFFFFU;
  420. }
  421. break;
  422. default:
  423. TIFFErrorExt(tif->tif_clientdata, module,
  424. "ZLib error: %s", SAFE_MSG(sp));
  425. return (0);
  426. }
  427. } while (state != Z_STREAM_END);
  428. return (1);
  429. }
  430. static void
  431. ZIPCleanup(TIFF* tif)
  432. {
  433. ZIPState* sp = ZState(tif);
  434. assert(sp != 0);
  435. (void)TIFFPredictorCleanup(tif);
  436. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  437. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  438. if (sp->state & ZSTATE_INIT_ENCODE) {
  439. deflateEnd(&sp->stream);
  440. sp->state = 0;
  441. } else if( sp->state & ZSTATE_INIT_DECODE) {
  442. inflateEnd(&sp->stream);
  443. sp->state = 0;
  444. }
  445. #if LIBDEFLATE_SUPPORT
  446. if( sp->libdeflate_dec )
  447. libdeflate_free_decompressor(sp->libdeflate_dec);
  448. if( sp->libdeflate_enc )
  449. libdeflate_free_compressor(sp->libdeflate_enc);
  450. #endif
  451. _TIFFfree(sp);
  452. tif->tif_data = NULL;
  453. _TIFFSetDefaultCompressionState(tif);
  454. }
  455. static int
  456. ZIPVSetField(TIFF* tif, uint32_t tag, va_list ap)
  457. {
  458. static const char module[] = "ZIPVSetField";
  459. ZIPState* sp = ZState(tif);
  460. switch (tag) {
  461. case TIFFTAG_ZIPQUALITY:
  462. sp->zipquality = (int) va_arg(ap, int);
  463. if( sp->zipquality < Z_DEFAULT_COMPRESSION ||
  464. sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL ) {
  465. TIFFErrorExt(tif->tif_clientdata, module,
  466. "Invalid ZipQuality value. Should be in [-1,%d] range",
  467. LIBDEFLATE_MAX_COMPRESSION_LEVEL);
  468. return 0;
  469. }
  470. if ( sp->state&ZSTATE_INIT_ENCODE ) {
  471. int cappedQuality = sp->zipquality;
  472. if( cappedQuality > Z_BEST_COMPRESSION )
  473. cappedQuality = Z_BEST_COMPRESSION;
  474. if (deflateParams(&sp->stream,
  475. cappedQuality, Z_DEFAULT_STRATEGY) != Z_OK) {
  476. TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
  477. SAFE_MSG(sp));
  478. return (0);
  479. }
  480. }
  481. #if LIBDEFLATE_SUPPORT
  482. if( sp->libdeflate_enc )
  483. {
  484. libdeflate_free_compressor(sp->libdeflate_enc);
  485. sp->libdeflate_enc = NULL;
  486. }
  487. #endif
  488. return (1);
  489. case TIFFTAG_DEFLATE_SUBCODEC:
  490. sp->subcodec = (int) va_arg(ap, int);
  491. if( sp->subcodec != DEFLATE_SUBCODEC_ZLIB &&
  492. sp->subcodec != DEFLATE_SUBCODEC_LIBDEFLATE )
  493. {
  494. TIFFErrorExt(tif->tif_clientdata, module,
  495. "Invalid DeflateCodec value.");
  496. return 0;
  497. }
  498. #if !LIBDEFLATE_SUPPORT
  499. if( sp->subcodec == DEFLATE_SUBCODEC_LIBDEFLATE )
  500. {
  501. TIFFErrorExt(tif->tif_clientdata, module,
  502. "DeflateCodec = DEFLATE_SUBCODEC_LIBDEFLATE unsupported in this build");
  503. return 0;
  504. }
  505. #endif
  506. return 1;
  507. default:
  508. return (*sp->vsetparent)(tif, tag, ap);
  509. }
  510. /*NOTREACHED*/
  511. }
  512. static int
  513. ZIPVGetField(TIFF* tif, uint32_t tag, va_list ap)
  514. {
  515. ZIPState* sp = ZState(tif);
  516. switch (tag) {
  517. case TIFFTAG_ZIPQUALITY:
  518. *va_arg(ap, int*) = sp->zipquality;
  519. break;
  520. case TIFFTAG_DEFLATE_SUBCODEC:
  521. *va_arg(ap, int*) = sp->subcodec;
  522. break;
  523. default:
  524. return (*sp->vgetparent)(tif, tag, ap);
  525. }
  526. return (1);
  527. }
  528. static const TIFFField zipFields[] = {
  529. { TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
  530. { TIFFTAG_DEFLATE_SUBCODEC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
  531. };
  532. int
  533. TIFFInitZIP(TIFF* tif, int scheme)
  534. {
  535. static const char module[] = "TIFFInitZIP";
  536. ZIPState* sp;
  537. assert( (scheme == COMPRESSION_DEFLATE)
  538. || (scheme == COMPRESSION_ADOBE_DEFLATE));
  539. #ifdef NDEBUG
  540. (void)scheme;
  541. #endif
  542. /*
  543. * Merge codec-specific tag information.
  544. */
  545. if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields))) {
  546. TIFFErrorExt(tif->tif_clientdata, module,
  547. "Merging Deflate codec-specific tags failed");
  548. return 0;
  549. }
  550. /*
  551. * Allocate state block so tag methods have storage to record values.
  552. */
  553. tif->tif_data = (uint8_t*) _TIFFcalloc(sizeof (ZIPState), 1);
  554. if (tif->tif_data == NULL)
  555. goto bad;
  556. sp = ZState(tif);
  557. sp->stream.zalloc = NULL;
  558. sp->stream.zfree = NULL;
  559. sp->stream.opaque = NULL;
  560. sp->stream.data_type = Z_BINARY;
  561. /*
  562. * Override parent get/set field methods.
  563. */
  564. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  565. tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
  566. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  567. tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
  568. /* Default values for codec-specific fields */
  569. sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
  570. sp->state = 0;
  571. #if LIBDEFLATE_SUPPORT
  572. sp->subcodec = DEFLATE_SUBCODEC_LIBDEFLATE;
  573. #else
  574. sp->subcodec = DEFLATE_SUBCODEC_ZLIB;
  575. #endif
  576. /*
  577. * Install codec methods.
  578. */
  579. tif->tif_fixuptags = ZIPFixupTags;
  580. tif->tif_setupdecode = ZIPSetupDecode;
  581. tif->tif_predecode = ZIPPreDecode;
  582. tif->tif_decoderow = ZIPDecode;
  583. tif->tif_decodestrip = ZIPDecode;
  584. tif->tif_decodetile = ZIPDecode;
  585. tif->tif_setupencode = ZIPSetupEncode;
  586. tif->tif_preencode = ZIPPreEncode;
  587. tif->tif_postencode = ZIPPostEncode;
  588. tif->tif_encoderow = ZIPEncode;
  589. tif->tif_encodestrip = ZIPEncode;
  590. tif->tif_encodetile = ZIPEncode;
  591. tif->tif_cleanup = ZIPCleanup;
  592. /*
  593. * Setup predictor setup.
  594. */
  595. (void) TIFFPredictorInit(tif);
  596. return (1);
  597. bad:
  598. TIFFErrorExt(tif->tif_clientdata, module,
  599. "No space for ZIP state block");
  600. return (0);
  601. }
  602. #endif /* ZIP_SUPPORT */
  603. /* vim: set ts=8 sts=8 sw=8 noet: */
  604. /*
  605. * Local Variables:
  606. * mode: c
  607. * c-basic-offset: 8
  608. * fill-column: 78
  609. * End:
  610. */