Line data Source code
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 :
25 : #include "tiffiop.h"
26 : #ifdef ZIP_SUPPORT
27 : /*
28 : * TIFF Library.
29 : *
30 : * ZIP (aka Deflate) Compression Support
31 : *
32 : * This file is an interface to the zlib library written by
33 : * Jean-loup Gailly and Mark Adler. You must use version 1.0 or later
34 : * of the library.
35 : *
36 : * Optionally, libdeflate (https://github.com/ebiggers/libdeflate) may be used
37 : * to do the compression and decompression, but only for whole strips and tiles.
38 : * For scanline access, zlib will be sued as a fallback.
39 : */
40 : #include "tif_predict.h"
41 : #include "zlib.h"
42 :
43 : #if LIBDEFLATE_SUPPORT
44 : #include "libdeflate.h"
45 : #endif
46 : #define LIBDEFLATE_MAX_COMPRESSION_LEVEL 12
47 :
48 : #include <stdio.h>
49 :
50 : /*
51 : * Sigh, ZLIB_VERSION is defined as a string so there's no
52 : * way to do a proper check here. Instead we guess based
53 : * on the presence of #defines that were added between the
54 : * 0.95 and 1.0 distributions.
55 : */
56 : #if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
57 : #error "Antiquated ZLIB software; you must use version 1.0 or later"
58 : #endif
59 :
60 : #define SAFE_MSG(sp) ((sp)->stream.msg == NULL ? "" : (sp)->stream.msg)
61 :
62 : /*
63 : * State block for each open TIFF
64 : * file using ZIP compression/decompression.
65 : */
66 : typedef struct
67 : {
68 : TIFFPredictorState predict;
69 : z_stream stream;
70 : int read_error; /* whether a read error has occurred, and which should cause
71 : further reads in the same strip/tile to be aborted */
72 : int zipquality; /* compression level */
73 : int state; /* state flags */
74 : int subcodec; /* DEFLATE_SUBCODEC_ZLIB or DEFLATE_SUBCODEC_LIBDEFLATE */
75 : #if LIBDEFLATE_SUPPORT
76 : int libdeflate_state; /* -1 = until first time ZIPEncode() / ZIPDecode() is
77 : called, 0 = use zlib, 1 = use libdeflate */
78 : struct libdeflate_decompressor *libdeflate_dec;
79 : struct libdeflate_compressor *libdeflate_enc;
80 : #endif
81 : #define ZSTATE_INIT_DECODE 0x01
82 : #define ZSTATE_INIT_ENCODE 0x02
83 :
84 : TIFFVGetMethod vgetparent; /* super-class method */
85 : TIFFVSetMethod vsetparent; /* super-class method */
86 : } ZIPState;
87 :
88 : #define GetZIPState(tif) ((ZIPState *)(tif)->tif_data)
89 : #define ZIPDecoderState(tif) GetZIPState(tif)
90 : #define ZIPEncoderState(tif) GetZIPState(tif)
91 :
92 : static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
93 : static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
94 :
95 2337 : static int ZIPFixupTags(TIFF *tif)
96 : {
97 : (void)tif;
98 2337 : return (1);
99 : }
100 :
101 539 : static int ZIPSetupDecode(TIFF *tif)
102 : {
103 : static const char module[] = "ZIPSetupDecode";
104 539 : ZIPState *sp = ZIPDecoderState(tif);
105 :
106 539 : assert(sp != NULL);
107 :
108 : /* if we were last encoding, terminate this mode */
109 539 : if (sp->state & ZSTATE_INIT_ENCODE)
110 : {
111 15 : deflateEnd(&sp->stream);
112 15 : sp->state = 0;
113 : }
114 :
115 : /* This function can possibly be called several times by */
116 : /* PredictorSetupDecode() if this function succeeds but */
117 : /* PredictorSetup() fails */
118 1078 : if ((sp->state & ZSTATE_INIT_DECODE) == 0 &&
119 539 : inflateInit(&sp->stream) != Z_OK)
120 : {
121 0 : TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp));
122 0 : return (0);
123 : }
124 : else
125 : {
126 539 : sp->state |= ZSTATE_INIT_DECODE;
127 539 : return (1);
128 : }
129 : }
130 :
131 15175 : static inline uint64_t TIFF_MIN_UINT64(uint64_t a, uint64_t b)
132 : {
133 15175 : return a < b ? a : b;
134 : }
135 :
136 15175 : static inline uInt TIFF_CLAMP_UINT64_TO_INT32_MAX(uint64_t v)
137 : {
138 15175 : return (uInt)TIFF_MIN_UINT64(v, INT32_MAX);
139 : }
140 :
141 : /*
142 : * Setup state for decoding a strip.
143 : */
144 4731 : static int ZIPPreDecode(TIFF *tif, uint16_t s)
145 : {
146 4731 : ZIPState *sp = ZIPDecoderState(tif);
147 :
148 : (void)s;
149 4731 : assert(sp != NULL);
150 :
151 4731 : if ((sp->state & ZSTATE_INIT_DECODE) == 0)
152 15 : tif->tif_setupdecode(tif);
153 :
154 : #if LIBDEFLATE_SUPPORT
155 4731 : sp->libdeflate_state = -1;
156 : #endif
157 4731 : sp->stream.next_in = tif->tif_rawdata;
158 : assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
159 : we need to simplify this code to reflect a ZLib that is likely updated
160 : to deal with 8byte memory sizes, though this code will respond
161 : appropriately even before we simplify it */
162 4731 : sp->stream.avail_in = TIFF_CLAMP_UINT64_TO_INT32_MAX(tif->tif_rawcc);
163 4731 : if (inflateReset(&sp->stream) == Z_OK)
164 : {
165 4731 : sp->read_error = 0;
166 4731 : return 1;
167 : }
168 0 : return 0;
169 : }
170 :
171 6731 : static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
172 : {
173 : static const char module[] = "ZIPDecode";
174 6731 : ZIPState *sp = ZIPDecoderState(tif);
175 :
176 : (void)s;
177 6731 : assert(sp != NULL);
178 6731 : assert(sp->state == ZSTATE_INIT_DECODE);
179 :
180 6731 : if (sp->read_error)
181 : {
182 0 : memset(op, 0, (size_t)occ);
183 0 : TIFFErrorExtR(tif, module,
184 : "ZIPDecode: Scanline %" PRIu32 " cannot be read due to "
185 : "previous error",
186 : tif->tif_row);
187 0 : return 0;
188 : }
189 :
190 : #if LIBDEFLATE_SUPPORT
191 6731 : if (sp->libdeflate_state == 1)
192 0 : return 0;
193 :
194 : /* If we have libdeflate support and we are asked to read a whole */
195 : /* strip/tile, then go for using it */
196 : do
197 : {
198 6731 : TIFFDirectory *td = &tif->tif_dir;
199 :
200 6731 : if (sp->libdeflate_state == 0)
201 2000 : break;
202 4731 : if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB)
203 0 : break;
204 :
205 : /* Check if we are in the situation where we can use libdeflate */
206 4731 : if (isTiled(tif))
207 : {
208 1045 : if (TIFFTileSize64(tif) != (uint64_t)occ)
209 171 : break;
210 : }
211 : else
212 : {
213 3686 : uint32_t strip_height = td->td_imagelength - tif->tif_row;
214 3686 : if (strip_height > td->td_rowsperstrip)
215 2903 : strip_height = td->td_rowsperstrip;
216 3686 : if (TIFFVStripSize64(tif, strip_height) != (uint64_t)occ)
217 2 : break;
218 : }
219 :
220 : /* Check for overflow */
221 : if ((size_t)tif->tif_rawcc != (uint64_t)tif->tif_rawcc)
222 : break;
223 : if ((size_t)occ != (uint64_t)occ)
224 : break;
225 :
226 : /* Go for decompression using libdeflate */
227 : {
228 : enum libdeflate_result res;
229 4558 : if (sp->libdeflate_dec == NULL)
230 : {
231 478 : sp->libdeflate_dec = libdeflate_alloc_decompressor();
232 478 : if (sp->libdeflate_dec == NULL)
233 : {
234 0 : break;
235 : }
236 : }
237 :
238 4558 : sp->libdeflate_state = 1;
239 :
240 4558 : res = libdeflate_zlib_decompress(sp->libdeflate_dec, tif->tif_rawcp,
241 4558 : (size_t)tif->tif_rawcc, op,
242 : (size_t)occ, NULL);
243 :
244 4558 : tif->tif_rawcp += tif->tif_rawcc;
245 4558 : tif->tif_rawcc = 0;
246 :
247 : /* We accept LIBDEFLATE_INSUFFICIENT_SPACE has a return */
248 : /* There are odd files in the wild where the last strip, when */
249 : /* it is smaller in height than td_rowsperstrip, actually contains
250 : */
251 : /* data for td_rowsperstrip lines. Just ignore that silently. */
252 4558 : if (res != LIBDEFLATE_SUCCESS &&
253 : res != LIBDEFLATE_INSUFFICIENT_SPACE)
254 : {
255 0 : memset(op, 0, (size_t)occ);
256 0 : TIFFErrorExtR(tif, module, "Decoding error at scanline %lu",
257 0 : (unsigned long)tif->tif_row);
258 0 : sp->read_error = 1;
259 0 : return 0;
260 : }
261 :
262 4558 : return 1;
263 : }
264 : } while (0);
265 2173 : sp->libdeflate_state = 0;
266 : #endif /* LIBDEFLATE_SUPPORT */
267 :
268 2173 : sp->stream.next_in = tif->tif_rawcp;
269 :
270 2173 : sp->stream.next_out = op;
271 : assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
272 : we need to simplify this code to reflect a ZLib that is likely updated
273 : to deal with 8byte memory sizes, though this code will respond
274 : appropriately even before we simplify it */
275 : do
276 : {
277 : int state;
278 2173 : uInt avail_in_before = TIFF_CLAMP_UINT64_TO_INT32_MAX(tif->tif_rawcc);
279 2173 : uInt avail_out_before = TIFF_CLAMP_UINT64_TO_INT32_MAX(occ);
280 2173 : sp->stream.avail_in = avail_in_before;
281 2173 : sp->stream.avail_out = avail_out_before;
282 2173 : state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
283 2173 : tif->tif_rawcc -= (avail_in_before - sp->stream.avail_in);
284 2173 : occ -= (avail_out_before - sp->stream.avail_out);
285 2173 : if (state == Z_STREAM_END)
286 1 : break;
287 2172 : if (state == Z_DATA_ERROR)
288 : {
289 1 : memset(sp->stream.next_out, 0, (size_t)occ);
290 1 : TIFFErrorExtR(tif, module, "Decoding error at scanline %lu, %s",
291 2 : (unsigned long)tif->tif_row, SAFE_MSG(sp));
292 1 : sp->read_error = 1;
293 1 : return (0);
294 : }
295 2171 : if (state != Z_OK)
296 : {
297 0 : memset(sp->stream.next_out, 0, (size_t)occ);
298 0 : TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
299 0 : sp->read_error = 1;
300 0 : return (0);
301 : }
302 2171 : } while (occ > 0);
303 2172 : if (occ != 0)
304 : {
305 0 : TIFFErrorExtR(tif, module,
306 : "Not enough data at scanline %lu (short %" PRIu64
307 : " bytes)",
308 0 : (unsigned long)tif->tif_row, (uint64_t)occ);
309 0 : memset(sp->stream.next_out, 0, (size_t)occ);
310 0 : sp->read_error = 1;
311 0 : return (0);
312 : }
313 :
314 2172 : tif->tif_rawcp = sp->stream.next_in;
315 :
316 2172 : return (1);
317 : }
318 :
319 5744 : static int ZIPSetupEncode(TIFF *tif)
320 : {
321 : static const char module[] = "ZIPSetupEncode";
322 5744 : ZIPState *sp = ZIPEncoderState(tif);
323 : int cappedQuality;
324 :
325 5744 : assert(sp != NULL);
326 5744 : if (sp->state & ZSTATE_INIT_DECODE)
327 : {
328 8 : inflateEnd(&sp->stream);
329 8 : sp->state = 0;
330 : }
331 :
332 5744 : cappedQuality = sp->zipquality;
333 5744 : if (cappedQuality > Z_BEST_COMPRESSION)
334 0 : cappedQuality = Z_BEST_COMPRESSION;
335 :
336 5744 : if (deflateInit(&sp->stream, cappedQuality) != Z_OK)
337 : {
338 0 : TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp));
339 0 : return (0);
340 : }
341 : else
342 : {
343 5744 : sp->state |= ZSTATE_INIT_ENCODE;
344 5744 : return (1);
345 : }
346 : }
347 :
348 : /*
349 : * Reset encoding state at the start of a strip.
350 : */
351 14519 : static int ZIPPreEncode(TIFF *tif, uint16_t s)
352 : {
353 14519 : ZIPState *sp = ZIPEncoderState(tif);
354 :
355 : (void)s;
356 14519 : assert(sp != NULL);
357 14519 : if (sp->state != ZSTATE_INIT_ENCODE)
358 8 : tif->tif_setupencode(tif);
359 :
360 : #if LIBDEFLATE_SUPPORT
361 14502 : sp->libdeflate_state = -1;
362 : #endif
363 14502 : sp->stream.next_out = tif->tif_rawdata;
364 : assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
365 : we need to simplify this code to reflect a ZLib that is likely updated
366 : to deal with 8byte memory sizes, though this code will respond
367 : appropriately even before we simplify it */
368 29004 : sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
369 14506 : ? (uInt)tif->tif_rawdatasize
370 14502 : : 0xFFFFFFFFU;
371 14502 : return (deflateReset(&sp->stream) == Z_OK);
372 : }
373 :
374 : /*
375 : * Encode a chunk of pixels.
376 : */
377 16513 : static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
378 : {
379 : static const char module[] = "ZIPEncode";
380 16513 : ZIPState *sp = ZIPEncoderState(tif);
381 :
382 16513 : assert(sp != NULL);
383 16513 : assert(sp->state == ZSTATE_INIT_ENCODE);
384 :
385 : (void)s;
386 :
387 : #if LIBDEFLATE_SUPPORT
388 16513 : if (sp->libdeflate_state == 1)
389 0 : return 0;
390 :
391 : /* If we have libdeflate support and we are asked to write a whole */
392 : /* strip/tile, then go for using it */
393 : do
394 : {
395 16513 : TIFFDirectory *td = &tif->tif_dir;
396 :
397 16513 : if (sp->libdeflate_state == 0)
398 2000 : break;
399 14513 : if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB)
400 4097 : break;
401 :
402 : /* Libdeflate does not support the 0-compression level */
403 10416 : if (sp->zipquality == Z_NO_COMPRESSION)
404 0 : break;
405 :
406 : /* Check if we are in the situation where we can use libdeflate */
407 10416 : if (isTiled(tif))
408 : {
409 5726 : if (TIFFTileSize64(tif) != (uint64_t)cc)
410 0 : break;
411 : }
412 : else
413 : {
414 4690 : uint32_t strip_height = td->td_imagelength - tif->tif_row;
415 4690 : if (strip_height > td->td_rowsperstrip)
416 3227 : strip_height = td->td_rowsperstrip;
417 4690 : if (TIFFVStripSize64(tif, strip_height) != (uint64_t)cc)
418 1 : break;
419 : }
420 :
421 : /* Check for overflow */
422 : if ((size_t)tif->tif_rawdatasize != (uint64_t)tif->tif_rawdatasize)
423 : break;
424 : if ((size_t)cc != (uint64_t)cc)
425 : break;
426 :
427 : /* Go for compression using libdeflate */
428 : {
429 : size_t nCompressedBytes;
430 10421 : if (sp->libdeflate_enc == NULL)
431 : {
432 : /* To get results as good as zlib, we asked for an extra */
433 : /* level of compression */
434 1641 : sp->libdeflate_enc = libdeflate_alloc_compressor(
435 1641 : sp->zipquality == Z_DEFAULT_COMPRESSION ? 7
436 12 : : sp->zipquality >= 6 && sp->zipquality <= 9
437 12 : ? sp->zipquality + 1
438 32 : : sp->zipquality);
439 1639 : if (sp->libdeflate_enc == NULL)
440 : {
441 0 : TIFFErrorExtR(tif, module, "Cannot allocate compressor");
442 0 : break;
443 : }
444 : }
445 :
446 : /* Make sure the output buffer is large enough for the worse case.
447 : */
448 : /* In TIFFWriteBufferSetup(), when libtiff allocates the buffer */
449 : /* we've taken a 10% margin over the uncompressed size, which should
450 : */
451 : /* be large enough even for the the worse case scenario. */
452 10419 : if (libdeflate_zlib_compress_bound(sp->libdeflate_enc, (size_t)cc) >
453 10420 : (size_t)tif->tif_rawdatasize)
454 : {
455 0 : break;
456 : }
457 :
458 10420 : sp->libdeflate_state = 1;
459 10420 : nCompressedBytes = libdeflate_zlib_compress(
460 10420 : sp->libdeflate_enc, bp, (size_t)cc, tif->tif_rawdata,
461 10420 : (size_t)tif->tif_rawdatasize);
462 :
463 10406 : if (nCompressedBytes == 0)
464 : {
465 0 : TIFFErrorExtR(tif, module, "Encoder error at scanline %lu",
466 0 : (unsigned long)tif->tif_row);
467 0 : return 0;
468 : }
469 :
470 10406 : tif->tif_rawcc = nCompressedBytes;
471 :
472 10406 : if (!TIFFFlushData1(tif))
473 0 : return 0;
474 :
475 10422 : return 1;
476 : }
477 : } while (0);
478 6098 : sp->libdeflate_state = 0;
479 : #endif /* LIBDEFLATE_SUPPORT */
480 :
481 6098 : sp->stream.next_in = bp;
482 : assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
483 : we need to simplify this code to reflect a ZLib that is likely updated
484 : to deal with 8byte memory sizes, though this code will respond
485 : appropriately even before we simplify it */
486 : do
487 : {
488 6098 : uInt avail_in_before = TIFF_CLAMP_UINT64_TO_INT32_MAX(cc);
489 6098 : sp->stream.avail_in = avail_in_before;
490 6098 : if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK)
491 : {
492 0 : TIFFErrorExtR(tif, module, "Encoder error: %s", SAFE_MSG(sp));
493 0 : return (0);
494 : }
495 6098 : if (sp->stream.avail_out == 0)
496 : {
497 0 : tif->tif_rawcc = tif->tif_rawdatasize;
498 0 : if (!TIFFFlushData1(tif))
499 0 : return 0;
500 0 : sp->stream.next_out = tif->tif_rawdata;
501 0 : sp->stream.avail_out =
502 0 : TIFF_CLAMP_UINT64_TO_INT32_MAX(tif->tif_rawdatasize);
503 : }
504 6098 : cc -= (avail_in_before - sp->stream.avail_in);
505 6098 : } while (cc > 0);
506 6098 : return (1);
507 : }
508 :
509 : /*
510 : * Finish off an encoded strip by flushing the last
511 : * string and tacking on an End Of Information code.
512 : */
513 14514 : static int ZIPPostEncode(TIFF *tif)
514 : {
515 : static const char module[] = "ZIPPostEncode";
516 14514 : ZIPState *sp = ZIPEncoderState(tif);
517 : int state;
518 :
519 : #if LIBDEFLATE_SUPPORT
520 14514 : if (sp->libdeflate_state == 1)
521 10414 : return 1;
522 : #endif
523 :
524 4100 : sp->stream.avail_in = 0;
525 : do
526 : {
527 4100 : state = deflate(&sp->stream, Z_FINISH);
528 4098 : switch (state)
529 : {
530 4098 : case Z_STREAM_END:
531 : case Z_OK:
532 4098 : if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
533 : {
534 4098 : tif->tif_rawcc =
535 4098 : tif->tif_rawdatasize - sp->stream.avail_out;
536 4098 : if (!TIFFFlushData1(tif))
537 0 : return 0;
538 4098 : sp->stream.next_out = tif->tif_rawdata;
539 4098 : sp->stream.avail_out =
540 4098 : (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
541 4098 : ? (uInt)tif->tif_rawdatasize
542 4098 : : 0xFFFFFFFFU;
543 : }
544 4098 : break;
545 0 : default:
546 0 : TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
547 0 : return (0);
548 : }
549 4098 : } while (state != Z_STREAM_END);
550 4098 : return (1);
551 : }
552 :
553 8187 : static void ZIPCleanup(TIFF *tif)
554 : {
555 8187 : ZIPState *sp = GetZIPState(tif);
556 :
557 8187 : assert(sp != 0);
558 :
559 8187 : (void)TIFFPredictorCleanup(tif);
560 :
561 8187 : tif->tif_tagmethods.vgetfield = sp->vgetparent;
562 8187 : tif->tif_tagmethods.vsetfield = sp->vsetparent;
563 :
564 8187 : if (sp->state & ZSTATE_INIT_ENCODE)
565 : {
566 5729 : deflateEnd(&sp->stream);
567 5729 : sp->state = 0;
568 : }
569 2458 : else if (sp->state & ZSTATE_INIT_DECODE)
570 : {
571 531 : inflateEnd(&sp->stream);
572 531 : sp->state = 0;
573 : }
574 :
575 : #if LIBDEFLATE_SUPPORT
576 8187 : if (sp->libdeflate_dec)
577 478 : libdeflate_free_decompressor(sp->libdeflate_dec);
578 8187 : if (sp->libdeflate_enc)
579 1641 : libdeflate_free_compressor(sp->libdeflate_enc);
580 : #endif
581 :
582 8187 : _TIFFfreeExt(tif, sp);
583 8187 : tif->tif_data = NULL;
584 :
585 8187 : _TIFFSetDefaultCompressionState(tif);
586 8187 : }
587 :
588 55504 : static int ZIPVSetField(TIFF *tif, uint32_t tag, va_list ap)
589 : {
590 : static const char module[] = "ZIPVSetField";
591 55504 : ZIPState *sp = GetZIPState(tif);
592 :
593 55504 : switch (tag)
594 : {
595 48 : case TIFFTAG_ZIPQUALITY:
596 48 : sp->zipquality = (int)va_arg(ap, int);
597 48 : if (sp->zipquality < Z_DEFAULT_COMPRESSION ||
598 48 : sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL)
599 : {
600 0 : TIFFErrorExtR(
601 : tif, module,
602 : "Invalid ZipQuality value. Should be in [-1,%d] range",
603 : LIBDEFLATE_MAX_COMPRESSION_LEVEL);
604 0 : return 0;
605 : }
606 :
607 48 : if (sp->state & ZSTATE_INIT_ENCODE)
608 : {
609 0 : int cappedQuality = sp->zipquality;
610 0 : if (cappedQuality > Z_BEST_COMPRESSION)
611 0 : cappedQuality = Z_BEST_COMPRESSION;
612 0 : if (deflateParams(&sp->stream, cappedQuality,
613 : Z_DEFAULT_STRATEGY) != Z_OK)
614 : {
615 0 : TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
616 0 : return (0);
617 : }
618 : }
619 :
620 : #if LIBDEFLATE_SUPPORT
621 48 : if (sp->libdeflate_enc)
622 : {
623 0 : libdeflate_free_compressor(sp->libdeflate_enc);
624 0 : sp->libdeflate_enc = NULL;
625 : }
626 : #endif
627 :
628 48 : return (1);
629 :
630 4097 : case TIFFTAG_DEFLATE_SUBCODEC:
631 4097 : sp->subcodec = (int)va_arg(ap, int);
632 4097 : if (sp->subcodec != DEFLATE_SUBCODEC_ZLIB &&
633 0 : sp->subcodec != DEFLATE_SUBCODEC_LIBDEFLATE)
634 : {
635 0 : TIFFErrorExtR(tif, module, "Invalid DeflateCodec value.");
636 0 : return 0;
637 : }
638 : #if !LIBDEFLATE_SUPPORT
639 : if (sp->subcodec == DEFLATE_SUBCODEC_LIBDEFLATE)
640 : {
641 : TIFFErrorExtR(tif, module,
642 : "DeflateCodec = DEFLATE_SUBCODEC_LIBDEFLATE "
643 : "unsupported in this build");
644 : return 0;
645 : }
646 : #endif
647 4097 : return 1;
648 :
649 51359 : default:
650 51359 : return (*sp->vsetparent)(tif, tag, ap);
651 : }
652 : /*NOTREACHED*/
653 : }
654 :
655 55405 : static int ZIPVGetField(TIFF *tif, uint32_t tag, va_list ap)
656 : {
657 55405 : ZIPState *sp = GetZIPState(tif);
658 :
659 55405 : switch (tag)
660 : {
661 0 : case TIFFTAG_ZIPQUALITY:
662 0 : *va_arg(ap, int *) = sp->zipquality;
663 0 : break;
664 :
665 0 : case TIFFTAG_DEFLATE_SUBCODEC:
666 0 : *va_arg(ap, int *) = sp->subcodec;
667 0 : break;
668 :
669 55405 : default:
670 55405 : return (*sp->vgetparent)(tif, tag, ap);
671 : }
672 0 : return (1);
673 : }
674 :
675 : static const TIFFField zipFields[] = {
676 : {TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, TRUE,
677 : FALSE, "", NULL},
678 : {TIFFTAG_DEFLATE_SUBCODEC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO,
679 : TRUE, FALSE, "", NULL},
680 : };
681 :
682 29338 : static void *TIFF_zalloc(void *opaque, unsigned int items, unsigned int size)
683 : {
684 : static const char module[] = "TIFF_zalloc";
685 29338 : TIFF *tif = opaque;
686 :
687 29338 : if (items > ~(size_t)0 / size)
688 : {
689 0 : TIFFErrorExtR(tif, module, "Overflow");
690 0 : return NULL;
691 : }
692 :
693 29338 : return _TIFFmallocExt(tif, items * size);
694 : }
695 :
696 29333 : static void TIFF_zfree(void *opaque, void *ptr)
697 : {
698 29333 : _TIFFfreeExt((TIFF *)opaque, ptr);
699 29337 : }
700 :
701 8188 : int TIFFInitZIP(TIFF *tif, int scheme)
702 : {
703 : static const char module[] = "TIFFInitZIP";
704 : ZIPState *sp;
705 :
706 8188 : assert((scheme == COMPRESSION_DEFLATE) ||
707 : (scheme == COMPRESSION_ADOBE_DEFLATE));
708 : #ifdef NDEBUG
709 : (void)scheme;
710 : #endif
711 :
712 : /*
713 : * Merge codec-specific tag information.
714 : */
715 8188 : if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields)))
716 : {
717 0 : TIFFErrorExtR(tif, module,
718 : "Merging Deflate codec-specific tags failed");
719 0 : return 0;
720 : }
721 :
722 : /*
723 : * Allocate state block so tag methods have storage to record values.
724 : */
725 8188 : tif->tif_data = (uint8_t *)_TIFFcallocExt(tif, sizeof(ZIPState), 1);
726 8188 : if (tif->tif_data == NULL)
727 0 : goto bad;
728 8188 : sp = GetZIPState(tif);
729 8188 : sp->stream.zalloc = TIFF_zalloc;
730 8188 : sp->stream.zfree = TIFF_zfree;
731 8188 : sp->stream.opaque = tif;
732 8188 : sp->stream.data_type = Z_BINARY;
733 :
734 : /*
735 : * Override parent get/set field methods.
736 : */
737 8188 : sp->vgetparent = tif->tif_tagmethods.vgetfield;
738 8188 : tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
739 8188 : sp->vsetparent = tif->tif_tagmethods.vsetfield;
740 8188 : tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
741 :
742 : /* Default values for codec-specific fields */
743 8188 : sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
744 8188 : sp->state = 0;
745 : #if LIBDEFLATE_SUPPORT
746 8188 : sp->subcodec = DEFLATE_SUBCODEC_LIBDEFLATE;
747 : #else
748 : sp->subcodec = DEFLATE_SUBCODEC_ZLIB;
749 : #endif
750 :
751 : /*
752 : * Install codec methods.
753 : */
754 8188 : tif->tif_fixuptags = ZIPFixupTags;
755 8188 : tif->tif_setupdecode = ZIPSetupDecode;
756 8188 : tif->tif_predecode = ZIPPreDecode;
757 8188 : tif->tif_decoderow = ZIPDecode;
758 8188 : tif->tif_decodestrip = ZIPDecode;
759 8188 : tif->tif_decodetile = ZIPDecode;
760 8188 : tif->tif_setupencode = ZIPSetupEncode;
761 8188 : tif->tif_preencode = ZIPPreEncode;
762 8188 : tif->tif_postencode = ZIPPostEncode;
763 8188 : tif->tif_encoderow = ZIPEncode;
764 8188 : tif->tif_encodestrip = ZIPEncode;
765 8188 : tif->tif_encodetile = ZIPEncode;
766 8188 : tif->tif_cleanup = ZIPCleanup;
767 : /*
768 : * Setup predictor setup.
769 : */
770 8188 : (void)TIFFPredictorInit(tif);
771 8188 : return (1);
772 0 : bad:
773 0 : TIFFErrorExtR(tif, module, "No space for ZIP state block");
774 0 : return (0);
775 : }
776 : #endif /* ZIP_SUPPORT */
|