tif_zip.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. #error #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. {
  62. TIFFPredictorState predict;
  63. z_stream stream;
  64. int zipquality; /* compression level */
  65. int state; /* state flags */
  66. int subcodec; /* DEFLATE_SUBCODEC_ZLIB or DEFLATE_SUBCODEC_LIBDEFLATE */
  67. #if LIBDEFLATE_SUPPORT
  68. int libdeflate_state; /* -1 = until first time ZIPEncode() / ZIPDecode() is
  69. called, 0 = use zlib, 1 = use libdeflate */
  70. struct libdeflate_decompressor *libdeflate_dec;
  71. struct libdeflate_compressor *libdeflate_enc;
  72. #endif
  73. #define ZSTATE_INIT_DECODE 0x01
  74. #define ZSTATE_INIT_ENCODE 0x02
  75. TIFFVGetMethod vgetparent; /* super-class method */
  76. TIFFVSetMethod vsetparent; /* super-class method */
  77. } ZIPState;
  78. #define ZState(tif) ((ZIPState *)(tif)->tif_data)
  79. #define DecoderState(tif) ZState(tif)
  80. #define EncoderState(tif) ZState(tif)
  81. static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
  82. static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
  83. static int ZIPFixupTags(TIFF *tif)
  84. {
  85. (void)tif;
  86. return (1);
  87. }
  88. static int 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. {
  96. deflateEnd(&sp->stream);
  97. sp->state = 0;
  98. }
  99. /* This function can possibly be called several times by */
  100. /* PredictorSetupDecode() if this function succeeds but */
  101. /* PredictorSetup() fails */
  102. if ((sp->state & ZSTATE_INIT_DECODE) == 0 &&
  103. inflateInit(&sp->stream) != Z_OK)
  104. {
  105. TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp));
  106. return (0);
  107. }
  108. else
  109. {
  110. sp->state |= ZSTATE_INIT_DECODE;
  111. return (1);
  112. }
  113. }
  114. /*
  115. * Setup state for decoding a strip.
  116. */
  117. static int ZIPPreDecode(TIFF *tif, uint16_t s)
  118. {
  119. ZIPState *sp = DecoderState(tif);
  120. (void)s;
  121. assert(sp != NULL);
  122. if ((sp->state & ZSTATE_INIT_DECODE) == 0)
  123. tif->tif_setupdecode(tif);
  124. #if LIBDEFLATE_SUPPORT
  125. sp->libdeflate_state = -1;
  126. #endif
  127. sp->stream.next_in = tif->tif_rawdata;
  128. assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
  129. we need to simplify this code to reflect a ZLib that is likely updated
  130. to deal with 8byte memory sizes, though this code will respond
  131. appropriately even before we simplify it */
  132. sp->stream.avail_in = (uint64_t)tif->tif_rawcc < 0xFFFFFFFFU
  133. ? (uInt)tif->tif_rawcc
  134. : 0xFFFFFFFFU;
  135. return (inflateReset(&sp->stream) == Z_OK);
  136. }
  137. static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
  138. {
  139. static const char module[] = "ZIPDecode";
  140. ZIPState *sp = DecoderState(tif);
  141. (void)s;
  142. assert(sp != NULL);
  143. assert(sp->state == ZSTATE_INIT_DECODE);
  144. #if LIBDEFLATE_SUPPORT
  145. if (sp->libdeflate_state == 1)
  146. return 0;
  147. /* If we have libdeflate support and we are asked to read a whole */
  148. /* strip/tile, then go for using it */
  149. do
  150. {
  151. TIFFDirectory *td = &tif->tif_dir;
  152. if (sp->libdeflate_state == 0)
  153. break;
  154. if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB)
  155. break;
  156. /* Check if we are in the situation where we can use libdeflate */
  157. if (isTiled(tif))
  158. {
  159. if (TIFFTileSize64(tif) != (uint64_t)occ)
  160. break;
  161. }
  162. else
  163. {
  164. uint32_t strip_height = td->td_imagelength - tif->tif_row;
  165. if (strip_height > td->td_rowsperstrip)
  166. strip_height = td->td_rowsperstrip;
  167. if (TIFFVStripSize64(tif, strip_height) != (uint64_t)occ)
  168. break;
  169. }
  170. /* Check for overflow */
  171. if ((size_t)tif->tif_rawcc != (uint64_t)tif->tif_rawcc)
  172. break;
  173. if ((size_t)occ != (uint64_t)occ)
  174. break;
  175. /* Go for decompression using libdeflate */
  176. {
  177. enum libdeflate_result res;
  178. if (sp->libdeflate_dec == NULL)
  179. {
  180. sp->libdeflate_dec = libdeflate_alloc_decompressor();
  181. if (sp->libdeflate_dec == NULL)
  182. {
  183. break;
  184. }
  185. }
  186. sp->libdeflate_state = 1;
  187. res = libdeflate_zlib_decompress(sp->libdeflate_dec, tif->tif_rawcp,
  188. (size_t)tif->tif_rawcc, op,
  189. (size_t)occ, NULL);
  190. tif->tif_rawcp += tif->tif_rawcc;
  191. tif->tif_rawcc = 0;
  192. /* We accept LIBDEFLATE_INSUFFICIENT_SPACE has a return */
  193. /* There are odd files in the wild where the last strip, when */
  194. /* it is smaller in height than td_rowsperstrip, actually contains
  195. */
  196. /* data for td_rowsperstrip lines. Just ignore that silently. */
  197. if (res != LIBDEFLATE_SUCCESS &&
  198. res != LIBDEFLATE_INSUFFICIENT_SPACE)
  199. {
  200. TIFFErrorExtR(tif, module, "Decoding error at scanline %lu",
  201. (unsigned long)tif->tif_row);
  202. return 0;
  203. }
  204. return 1;
  205. }
  206. } while (0);
  207. sp->libdeflate_state = 0;
  208. #endif /* LIBDEFLATE_SUPPORT */
  209. sp->stream.next_in = tif->tif_rawcp;
  210. sp->stream.next_out = op;
  211. assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
  212. we need to simplify this code to reflect a ZLib that is likely updated
  213. to deal with 8byte memory sizes, though this code will respond
  214. appropriately even before we simplify it */
  215. do
  216. {
  217. int state;
  218. uInt avail_in_before = (uint64_t)tif->tif_rawcc <= 0xFFFFFFFFU
  219. ? (uInt)tif->tif_rawcc
  220. : 0xFFFFFFFFU;
  221. uInt avail_out_before =
  222. (uint64_t)occ < 0xFFFFFFFFU ? (uInt)occ : 0xFFFFFFFFU;
  223. sp->stream.avail_in = avail_in_before;
  224. sp->stream.avail_out = avail_out_before;
  225. /* coverity[overrun-buffer-arg] */
  226. state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
  227. tif->tif_rawcc -= (avail_in_before - sp->stream.avail_in);
  228. occ -= (avail_out_before - sp->stream.avail_out);
  229. if (state == Z_STREAM_END)
  230. break;
  231. if (state == Z_DATA_ERROR)
  232. {
  233. TIFFErrorExtR(tif, module, "Decoding error at scanline %lu, %s",
  234. (unsigned long)tif->tif_row, SAFE_MSG(sp));
  235. return (0);
  236. }
  237. if (state != Z_OK)
  238. {
  239. TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
  240. return (0);
  241. }
  242. } while (occ > 0);
  243. if (occ != 0)
  244. {
  245. TIFFErrorExtR(tif, module,
  246. "Not enough data at scanline %lu (short %" PRIu64
  247. " bytes)",
  248. (unsigned long)tif->tif_row, (uint64_t)occ);
  249. return (0);
  250. }
  251. tif->tif_rawcp = sp->stream.next_in;
  252. return (1);
  253. }
  254. static int ZIPSetupEncode(TIFF *tif)
  255. {
  256. static const char module[] = "ZIPSetupEncode";
  257. ZIPState *sp = EncoderState(tif);
  258. int cappedQuality;
  259. assert(sp != NULL);
  260. if (sp->state & ZSTATE_INIT_DECODE)
  261. {
  262. inflateEnd(&sp->stream);
  263. sp->state = 0;
  264. }
  265. cappedQuality = sp->zipquality;
  266. if (cappedQuality > Z_BEST_COMPRESSION)
  267. cappedQuality = Z_BEST_COMPRESSION;
  268. if (deflateInit(&sp->stream, cappedQuality) != Z_OK)
  269. {
  270. TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp));
  271. return (0);
  272. }
  273. else
  274. {
  275. sp->state |= ZSTATE_INIT_ENCODE;
  276. return (1);
  277. }
  278. }
  279. /*
  280. * Reset encoding state at the start of a strip.
  281. */
  282. static int ZIPPreEncode(TIFF *tif, uint16_t s)
  283. {
  284. ZIPState *sp = EncoderState(tif);
  285. (void)s;
  286. assert(sp != NULL);
  287. if (sp->state != ZSTATE_INIT_ENCODE)
  288. tif->tif_setupencode(tif);
  289. #if LIBDEFLATE_SUPPORT
  290. sp->libdeflate_state = -1;
  291. #endif
  292. sp->stream.next_out = tif->tif_rawdata;
  293. assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
  294. we need to simplify this code to reflect a ZLib that is likely updated
  295. to deal with 8byte memory sizes, though this code will respond
  296. appropriately even before we simplify it */
  297. sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
  298. ? (uInt)tif->tif_rawdatasize
  299. : 0xFFFFFFFFU;
  300. return (deflateReset(&sp->stream) == Z_OK);
  301. }
  302. /*
  303. * Encode a chunk of pixels.
  304. */
  305. static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  306. {
  307. static const char module[] = "ZIPEncode";
  308. ZIPState *sp = EncoderState(tif);
  309. assert(sp != NULL);
  310. assert(sp->state == ZSTATE_INIT_ENCODE);
  311. (void)s;
  312. #if LIBDEFLATE_SUPPORT
  313. if (sp->libdeflate_state == 1)
  314. return 0;
  315. /* If we have libdeflate support and we are asked to write a whole */
  316. /* strip/tile, then go for using it */
  317. do
  318. {
  319. TIFFDirectory *td = &tif->tif_dir;
  320. if (sp->libdeflate_state == 0)
  321. break;
  322. if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB)
  323. break;
  324. /* Libdeflate does not support the 0-compression level */
  325. if (sp->zipquality == Z_NO_COMPRESSION)
  326. break;
  327. /* Check if we are in the situation where we can use libdeflate */
  328. if (isTiled(tif))
  329. {
  330. if (TIFFTileSize64(tif) != (uint64_t)cc)
  331. break;
  332. }
  333. else
  334. {
  335. uint32_t strip_height = td->td_imagelength - tif->tif_row;
  336. if (strip_height > td->td_rowsperstrip)
  337. strip_height = td->td_rowsperstrip;
  338. if (TIFFVStripSize64(tif, strip_height) != (uint64_t)cc)
  339. break;
  340. }
  341. /* Check for overflow */
  342. if ((size_t)tif->tif_rawdatasize != (uint64_t)tif->tif_rawdatasize)
  343. break;
  344. if ((size_t)cc != (uint64_t)cc)
  345. break;
  346. /* Go for compression using libdeflate */
  347. {
  348. size_t nCompressedBytes;
  349. if (sp->libdeflate_enc == NULL)
  350. {
  351. /* To get results as good as zlib, we asked for an extra */
  352. /* level of compression */
  353. sp->libdeflate_enc = libdeflate_alloc_compressor(
  354. sp->zipquality == Z_DEFAULT_COMPRESSION ? 7
  355. : sp->zipquality >= 6 && sp->zipquality <= 9
  356. ? sp->zipquality + 1
  357. : sp->zipquality);
  358. if (sp->libdeflate_enc == NULL)
  359. {
  360. TIFFErrorExtR(tif, module, "Cannot allocate compressor");
  361. break;
  362. }
  363. }
  364. /* Make sure the output buffer is large enough for the worse case.
  365. */
  366. /* In TIFFWriteBufferSetup(), when libtiff allocates the buffer */
  367. /* we've taken a 10% margin over the uncompressed size, which should
  368. */
  369. /* be large enough even for the the worse case scenario. */
  370. if (libdeflate_zlib_compress_bound(sp->libdeflate_enc, (size_t)cc) >
  371. (size_t)tif->tif_rawdatasize)
  372. {
  373. break;
  374. }
  375. sp->libdeflate_state = 1;
  376. nCompressedBytes = libdeflate_zlib_compress(
  377. sp->libdeflate_enc, bp, (size_t)cc, tif->tif_rawdata,
  378. (size_t)tif->tif_rawdatasize);
  379. if (nCompressedBytes == 0)
  380. {
  381. TIFFErrorExtR(tif, module, "Encoder error at scanline %lu",
  382. (unsigned long)tif->tif_row);
  383. return 0;
  384. }
  385. tif->tif_rawcc = nCompressedBytes;
  386. if (!TIFFFlushData1(tif))
  387. return 0;
  388. return 1;
  389. }
  390. } while (0);
  391. sp->libdeflate_state = 0;
  392. #endif /* LIBDEFLATE_SUPPORT */
  393. sp->stream.next_in = bp;
  394. assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
  395. we need to simplify this code to reflect a ZLib that is likely updated
  396. to deal with 8byte memory sizes, though this code will respond
  397. appropriately even before we simplify it */
  398. do
  399. {
  400. uInt avail_in_before =
  401. (uint64_t)cc <= 0xFFFFFFFFU ? (uInt)cc : 0xFFFFFFFFU;
  402. sp->stream.avail_in = avail_in_before;
  403. /* coverity[overrun-buffer-arg] */
  404. if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK)
  405. {
  406. TIFFErrorExtR(tif, module, "Encoder error: %s", SAFE_MSG(sp));
  407. return (0);
  408. }
  409. if (sp->stream.avail_out == 0)
  410. {
  411. tif->tif_rawcc = tif->tif_rawdatasize;
  412. if (!TIFFFlushData1(tif))
  413. return 0;
  414. sp->stream.next_out = tif->tif_rawdata;
  415. sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
  416. ? (uInt)tif->tif_rawdatasize
  417. : 0xFFFFFFFFU;
  418. }
  419. cc -= (avail_in_before - sp->stream.avail_in);
  420. } while (cc > 0);
  421. return (1);
  422. }
  423. /*
  424. * Finish off an encoded strip by flushing the last
  425. * string and tacking on an End Of Information code.
  426. */
  427. static int ZIPPostEncode(TIFF *tif)
  428. {
  429. static const char module[] = "ZIPPostEncode";
  430. ZIPState *sp = EncoderState(tif);
  431. int state;
  432. #if LIBDEFLATE_SUPPORT
  433. if (sp->libdeflate_state == 1)
  434. return 1;
  435. #endif
  436. sp->stream.avail_in = 0;
  437. do
  438. {
  439. state = deflate(&sp->stream, Z_FINISH);
  440. switch (state)
  441. {
  442. case Z_STREAM_END:
  443. case Z_OK:
  444. if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
  445. {
  446. tif->tif_rawcc =
  447. tif->tif_rawdatasize - sp->stream.avail_out;
  448. if (!TIFFFlushData1(tif))
  449. return 0;
  450. sp->stream.next_out = tif->tif_rawdata;
  451. sp->stream.avail_out =
  452. (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
  453. ? (uInt)tif->tif_rawdatasize
  454. : 0xFFFFFFFFU;
  455. }
  456. break;
  457. default:
  458. TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
  459. return (0);
  460. }
  461. } while (state != Z_STREAM_END);
  462. return (1);
  463. }
  464. static void ZIPCleanup(TIFF *tif)
  465. {
  466. ZIPState *sp = ZState(tif);
  467. assert(sp != 0);
  468. (void)TIFFPredictorCleanup(tif);
  469. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  470. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  471. if (sp->state & ZSTATE_INIT_ENCODE)
  472. {
  473. deflateEnd(&sp->stream);
  474. sp->state = 0;
  475. }
  476. else if (sp->state & ZSTATE_INIT_DECODE)
  477. {
  478. inflateEnd(&sp->stream);
  479. sp->state = 0;
  480. }
  481. #if LIBDEFLATE_SUPPORT
  482. if (sp->libdeflate_dec)
  483. libdeflate_free_decompressor(sp->libdeflate_dec);
  484. if (sp->libdeflate_enc)
  485. libdeflate_free_compressor(sp->libdeflate_enc);
  486. #endif
  487. _TIFFfreeExt(tif, sp);
  488. tif->tif_data = NULL;
  489. _TIFFSetDefaultCompressionState(tif);
  490. }
  491. static int ZIPVSetField(TIFF *tif, uint32_t tag, va_list ap)
  492. {
  493. static const char module[] = "ZIPVSetField";
  494. ZIPState *sp = ZState(tif);
  495. switch (tag)
  496. {
  497. case TIFFTAG_ZIPQUALITY:
  498. sp->zipquality = (int)va_arg(ap, int);
  499. if (sp->zipquality < Z_DEFAULT_COMPRESSION ||
  500. sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL)
  501. {
  502. TIFFErrorExtR(
  503. tif, module,
  504. "Invalid ZipQuality value. Should be in [-1,%d] range",
  505. LIBDEFLATE_MAX_COMPRESSION_LEVEL);
  506. return 0;
  507. }
  508. if (sp->state & ZSTATE_INIT_ENCODE)
  509. {
  510. int cappedQuality = sp->zipquality;
  511. if (cappedQuality > Z_BEST_COMPRESSION)
  512. cappedQuality = Z_BEST_COMPRESSION;
  513. if (deflateParams(&sp->stream, cappedQuality,
  514. Z_DEFAULT_STRATEGY) != Z_OK)
  515. {
  516. TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
  517. return (0);
  518. }
  519. }
  520. #if LIBDEFLATE_SUPPORT
  521. if (sp->libdeflate_enc)
  522. {
  523. libdeflate_free_compressor(sp->libdeflate_enc);
  524. sp->libdeflate_enc = NULL;
  525. }
  526. #endif
  527. return (1);
  528. case TIFFTAG_DEFLATE_SUBCODEC:
  529. sp->subcodec = (int)va_arg(ap, int);
  530. if (sp->subcodec != DEFLATE_SUBCODEC_ZLIB &&
  531. sp->subcodec != DEFLATE_SUBCODEC_LIBDEFLATE)
  532. {
  533. TIFFErrorExtR(tif, module, "Invalid DeflateCodec value.");
  534. return 0;
  535. }
  536. #if !LIBDEFLATE_SUPPORT
  537. if (sp->subcodec == DEFLATE_SUBCODEC_LIBDEFLATE)
  538. {
  539. TIFFErrorExtR(tif, module,
  540. "DeflateCodec = DEFLATE_SUBCODEC_LIBDEFLATE "
  541. "unsupported in this build");
  542. return 0;
  543. }
  544. #endif
  545. return 1;
  546. default:
  547. return (*sp->vsetparent)(tif, tag, ap);
  548. }
  549. /*NOTREACHED*/
  550. }
  551. static int ZIPVGetField(TIFF *tif, uint32_t tag, va_list ap)
  552. {
  553. ZIPState *sp = ZState(tif);
  554. switch (tag)
  555. {
  556. case TIFFTAG_ZIPQUALITY:
  557. *va_arg(ap, int *) = sp->zipquality;
  558. break;
  559. case TIFFTAG_DEFLATE_SUBCODEC:
  560. *va_arg(ap, int *) = sp->subcodec;
  561. break;
  562. default:
  563. return (*sp->vgetparent)(tif, tag, ap);
  564. }
  565. return (1);
  566. }
  567. static const TIFFField zipFields[] = {
  568. {TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  569. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL},
  570. {TIFFTAG_DEFLATE_SUBCODEC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  571. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL},
  572. };
  573. int TIFFInitZIP(TIFF *tif, int scheme)
  574. {
  575. static const char module[] = "TIFFInitZIP";
  576. ZIPState *sp;
  577. assert((scheme == COMPRESSION_DEFLATE) ||
  578. (scheme == COMPRESSION_ADOBE_DEFLATE));
  579. #ifdef NDEBUG
  580. (void)scheme;
  581. #endif
  582. /*
  583. * Merge codec-specific tag information.
  584. */
  585. if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields)))
  586. {
  587. TIFFErrorExtR(tif, module,
  588. "Merging Deflate codec-specific tags failed");
  589. return 0;
  590. }
  591. /*
  592. * Allocate state block so tag methods have storage to record values.
  593. */
  594. tif->tif_data = (uint8_t *)_TIFFcallocExt(tif, sizeof(ZIPState), 1);
  595. if (tif->tif_data == NULL)
  596. goto bad;
  597. sp = ZState(tif);
  598. sp->stream.zalloc = NULL;
  599. sp->stream.zfree = NULL;
  600. sp->stream.opaque = NULL;
  601. sp->stream.data_type = Z_BINARY;
  602. /*
  603. * Override parent get/set field methods.
  604. */
  605. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  606. tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
  607. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  608. tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
  609. /* Default values for codec-specific fields */
  610. sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
  611. sp->state = 0;
  612. #if LIBDEFLATE_SUPPORT
  613. sp->subcodec = DEFLATE_SUBCODEC_LIBDEFLATE;
  614. #else
  615. sp->subcodec = DEFLATE_SUBCODEC_ZLIB;
  616. #endif
  617. /*
  618. * Install codec methods.
  619. */
  620. tif->tif_fixuptags = ZIPFixupTags;
  621. tif->tif_setupdecode = ZIPSetupDecode;
  622. tif->tif_predecode = ZIPPreDecode;
  623. tif->tif_decoderow = ZIPDecode;
  624. tif->tif_decodestrip = ZIPDecode;
  625. tif->tif_decodetile = ZIPDecode;
  626. tif->tif_setupencode = ZIPSetupEncode;
  627. tif->tif_preencode = ZIPPreEncode;
  628. tif->tif_postencode = ZIPPostEncode;
  629. tif->tif_encoderow = ZIPEncode;
  630. tif->tif_encodestrip = ZIPEncode;
  631. tif->tif_encodetile = ZIPEncode;
  632. tif->tif_cleanup = ZIPCleanup;
  633. /*
  634. * Setup predictor setup.
  635. */
  636. (void)TIFFPredictorInit(tif);
  637. return (1);
  638. bad:
  639. TIFFErrorExtR(tif, module, "No space for ZIP state block");
  640. return (0);
  641. }
  642. #endif /* ZIP_SUPPORT */