tif_zip.c 23 KB

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