tif_zip.c 23 KB

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