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 2262 : static int ZIPFixupTags(TIFF *tif)
96 : {
97 : (void)tif;
98 2262 : return (1);
99 : }
100 :
101 498 : static int ZIPSetupDecode(TIFF *tif)
102 : {
103 : static const char module[] = "ZIPSetupDecode";
104 498 : ZIPState *sp = ZIPDecoderState(tif);
105 :
106 498 : assert(sp != NULL);
107 :
108 : /* if we were last encoding, terminate this mode */
109 498 : 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 996 : if ((sp->state & ZSTATE_INIT_DECODE) == 0 &&
119 498 : inflateInit(&sp->stream) != Z_OK)
120 : {
121 0 : TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp));
122 0 : return (0);
123 : }
124 : else
125 : {
126 498 : sp->state |= ZSTATE_INIT_DECODE;
127 498 : return (1);
128 : }
129 : }
130 :
131 : /*
132 : * Setup state for decoding a strip.
133 : */
134 4676 : static int ZIPPreDecode(TIFF *tif, uint16_t s)
135 : {
136 4676 : ZIPState *sp = ZIPDecoderState(tif);
137 :
138 : (void)s;
139 4676 : assert(sp != NULL);
140 :
141 4676 : if ((sp->state & ZSTATE_INIT_DECODE) == 0)
142 15 : tif->tif_setupdecode(tif);
143 :
144 : #if LIBDEFLATE_SUPPORT
145 4676 : sp->libdeflate_state = -1;
146 : #endif
147 4676 : sp->stream.next_in = tif->tif_rawdata;
148 : assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
149 : we need to simplify this code to reflect a ZLib that is likely updated
150 : to deal with 8byte memory sizes, though this code will respond
151 : appropriately even before we simplify it */
152 9352 : sp->stream.avail_in = (uint64_t)tif->tif_rawcc < 0xFFFFFFFFU
153 4676 : ? (uInt)tif->tif_rawcc
154 4676 : : 0xFFFFFFFFU;
155 4676 : if (inflateReset(&sp->stream) == Z_OK)
156 : {
157 4676 : sp->read_error = 0;
158 4676 : return 1;
159 : }
160 0 : return 0;
161 : }
162 :
163 6676 : static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
164 : {
165 : static const char module[] = "ZIPDecode";
166 6676 : ZIPState *sp = ZIPDecoderState(tif);
167 :
168 : (void)s;
169 6676 : assert(sp != NULL);
170 6676 : assert(sp->state == ZSTATE_INIT_DECODE);
171 :
172 6676 : if (sp->read_error)
173 : {
174 0 : memset(op, 0, (size_t)occ);
175 0 : TIFFErrorExtR(tif, module,
176 : "ZIPDecode: Scanline %" PRIu32 " cannot be read due to "
177 : "previous error",
178 : tif->tif_row);
179 0 : return 0;
180 : }
181 :
182 : #if LIBDEFLATE_SUPPORT
183 6676 : if (sp->libdeflate_state == 1)
184 0 : return 0;
185 :
186 : /* If we have libdeflate support and we are asked to read a whole */
187 : /* strip/tile, then go for using it */
188 : do
189 : {
190 6676 : TIFFDirectory *td = &tif->tif_dir;
191 :
192 6676 : if (sp->libdeflate_state == 0)
193 2000 : break;
194 4676 : if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB)
195 0 : break;
196 :
197 : /* Check if we are in the situation where we can use libdeflate */
198 4676 : if (isTiled(tif))
199 : {
200 1045 : if (TIFFTileSize64(tif) != (uint64_t)occ)
201 171 : break;
202 : }
203 : else
204 : {
205 3631 : uint32_t strip_height = td->td_imagelength - tif->tif_row;
206 3631 : if (strip_height > td->td_rowsperstrip)
207 2903 : strip_height = td->td_rowsperstrip;
208 3631 : if (TIFFVStripSize64(tif, strip_height) != (uint64_t)occ)
209 2 : break;
210 : }
211 :
212 : /* Check for overflow */
213 : if ((size_t)tif->tif_rawcc != (uint64_t)tif->tif_rawcc)
214 : break;
215 : if ((size_t)occ != (uint64_t)occ)
216 : break;
217 :
218 : /* Go for decompression using libdeflate */
219 : {
220 : enum libdeflate_result res;
221 4503 : if (sp->libdeflate_dec == NULL)
222 : {
223 437 : sp->libdeflate_dec = libdeflate_alloc_decompressor();
224 437 : if (sp->libdeflate_dec == NULL)
225 : {
226 0 : break;
227 : }
228 : }
229 :
230 4503 : sp->libdeflate_state = 1;
231 :
232 4503 : res = libdeflate_zlib_decompress(sp->libdeflate_dec, tif->tif_rawcp,
233 4503 : (size_t)tif->tif_rawcc, op,
234 : (size_t)occ, NULL);
235 :
236 4503 : tif->tif_rawcp += tif->tif_rawcc;
237 4503 : tif->tif_rawcc = 0;
238 :
239 : /* We accept LIBDEFLATE_INSUFFICIENT_SPACE has a return */
240 : /* There are odd files in the wild where the last strip, when */
241 : /* it is smaller in height than td_rowsperstrip, actually contains
242 : */
243 : /* data for td_rowsperstrip lines. Just ignore that silently. */
244 4503 : if (res != LIBDEFLATE_SUCCESS &&
245 : res != LIBDEFLATE_INSUFFICIENT_SPACE)
246 : {
247 0 : memset(op, 0, (size_t)occ);
248 0 : TIFFErrorExtR(tif, module, "Decoding error at scanline %lu",
249 0 : (unsigned long)tif->tif_row);
250 0 : sp->read_error = 1;
251 0 : return 0;
252 : }
253 :
254 4503 : return 1;
255 : }
256 : } while (0);
257 2173 : sp->libdeflate_state = 0;
258 : #endif /* LIBDEFLATE_SUPPORT */
259 :
260 2173 : sp->stream.next_in = tif->tif_rawcp;
261 :
262 2173 : sp->stream.next_out = op;
263 : assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
264 : we need to simplify this code to reflect a ZLib that is likely updated
265 : to deal with 8byte memory sizes, though this code will respond
266 : appropriately even before we simplify it */
267 : do
268 : {
269 : int state;
270 4346 : uInt avail_in_before = (uint64_t)tif->tif_rawcc <= 0xFFFFFFFFU
271 2173 : ? (uInt)tif->tif_rawcc
272 2173 : : 0xFFFFFFFFU;
273 2173 : uInt avail_out_before =
274 2173 : (uint64_t)occ < 0xFFFFFFFFU ? (uInt)occ : 0xFFFFFFFFU;
275 2173 : sp->stream.avail_in = avail_in_before;
276 2173 : sp->stream.avail_out = avail_out_before;
277 : /* coverity[overrun-buffer-arg] */
278 2173 : state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
279 2173 : tif->tif_rawcc -= (avail_in_before - sp->stream.avail_in);
280 2173 : occ -= (avail_out_before - sp->stream.avail_out);
281 2173 : if (state == Z_STREAM_END)
282 1 : break;
283 2172 : if (state == Z_DATA_ERROR)
284 : {
285 : /* coverity[overrun-buffer-arg:SUPPRESS] */
286 1 : memset(sp->stream.next_out, 0, sp->stream.avail_out);
287 1 : TIFFErrorExtR(tif, module, "Decoding error at scanline %lu, %s",
288 2 : (unsigned long)tif->tif_row, SAFE_MSG(sp));
289 1 : sp->read_error = 1;
290 1 : return (0);
291 : }
292 2171 : if (state != Z_OK)
293 : {
294 0 : memset(sp->stream.next_out, 0, sp->stream.avail_out);
295 0 : TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
296 0 : sp->read_error = 1;
297 0 : return (0);
298 : }
299 2171 : } while (occ > 0);
300 2172 : if (occ != 0)
301 : {
302 0 : TIFFErrorExtR(tif, module,
303 : "Not enough data at scanline %lu (short %" PRIu64
304 : " bytes)",
305 0 : (unsigned long)tif->tif_row, (uint64_t)occ);
306 0 : memset(sp->stream.next_out, 0, sp->stream.avail_out);
307 0 : sp->read_error = 1;
308 0 : return (0);
309 : }
310 :
311 2172 : tif->tif_rawcp = sp->stream.next_in;
312 :
313 2172 : return (1);
314 : }
315 :
316 5744 : static int ZIPSetupEncode(TIFF *tif)
317 : {
318 : static const char module[] = "ZIPSetupEncode";
319 5744 : ZIPState *sp = ZIPEncoderState(tif);
320 : int cappedQuality;
321 :
322 5744 : assert(sp != NULL);
323 5744 : if (sp->state & ZSTATE_INIT_DECODE)
324 : {
325 8 : inflateEnd(&sp->stream);
326 8 : sp->state = 0;
327 : }
328 :
329 5744 : cappedQuality = sp->zipquality;
330 5744 : if (cappedQuality > Z_BEST_COMPRESSION)
331 0 : cappedQuality = Z_BEST_COMPRESSION;
332 :
333 5744 : if (deflateInit(&sp->stream, cappedQuality) != Z_OK)
334 : {
335 0 : TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp));
336 0 : return (0);
337 : }
338 : else
339 : {
340 5744 : sp->state |= ZSTATE_INIT_ENCODE;
341 5744 : return (1);
342 : }
343 : }
344 :
345 : /*
346 : * Reset encoding state at the start of a strip.
347 : */
348 14512 : static int ZIPPreEncode(TIFF *tif, uint16_t s)
349 : {
350 14512 : ZIPState *sp = ZIPEncoderState(tif);
351 :
352 : (void)s;
353 14512 : assert(sp != NULL);
354 14512 : if (sp->state != ZSTATE_INIT_ENCODE)
355 8 : tif->tif_setupencode(tif);
356 :
357 : #if LIBDEFLATE_SUPPORT
358 14504 : sp->libdeflate_state = -1;
359 : #endif
360 14504 : sp->stream.next_out = tif->tif_rawdata;
361 : assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
362 : we need to simplify this code to reflect a ZLib that is likely updated
363 : to deal with 8byte memory sizes, though this code will respond
364 : appropriately even before we simplify it */
365 29008 : sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
366 14508 : ? (uInt)tif->tif_rawdatasize
367 14504 : : 0xFFFFFFFFU;
368 14504 : return (deflateReset(&sp->stream) == Z_OK);
369 : }
370 :
371 : /*
372 : * Encode a chunk of pixels.
373 : */
374 16513 : static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
375 : {
376 : static const char module[] = "ZIPEncode";
377 16513 : ZIPState *sp = ZIPEncoderState(tif);
378 :
379 16513 : assert(sp != NULL);
380 16513 : assert(sp->state == ZSTATE_INIT_ENCODE);
381 :
382 : (void)s;
383 :
384 : #if LIBDEFLATE_SUPPORT
385 16513 : if (sp->libdeflate_state == 1)
386 0 : return 0;
387 :
388 : /* If we have libdeflate support and we are asked to write a whole */
389 : /* strip/tile, then go for using it */
390 : do
391 : {
392 16513 : TIFFDirectory *td = &tif->tif_dir;
393 :
394 16513 : if (sp->libdeflate_state == 0)
395 2000 : break;
396 14513 : if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB)
397 4097 : break;
398 :
399 : /* Libdeflate does not support the 0-compression level */
400 10416 : if (sp->zipquality == Z_NO_COMPRESSION)
401 0 : break;
402 :
403 : /* Check if we are in the situation where we can use libdeflate */
404 10416 : if (isTiled(tif))
405 : {
406 5727 : if (TIFFTileSize64(tif) != (uint64_t)cc)
407 0 : break;
408 : }
409 : else
410 : {
411 4689 : uint32_t strip_height = td->td_imagelength - tif->tif_row;
412 4689 : if (strip_height > td->td_rowsperstrip)
413 3227 : strip_height = td->td_rowsperstrip;
414 4689 : if (TIFFVStripSize64(tif, strip_height) != (uint64_t)cc)
415 1 : break;
416 : }
417 :
418 : /* Check for overflow */
419 : if ((size_t)tif->tif_rawdatasize != (uint64_t)tif->tif_rawdatasize)
420 : break;
421 : if ((size_t)cc != (uint64_t)cc)
422 : break;
423 :
424 : /* Go for compression using libdeflate */
425 : {
426 : size_t nCompressedBytes;
427 10417 : if (sp->libdeflate_enc == NULL)
428 : {
429 : /* To get results as good as zlib, we asked for an extra */
430 : /* level of compression */
431 1641 : sp->libdeflate_enc = libdeflate_alloc_compressor(
432 1641 : sp->zipquality == Z_DEFAULT_COMPRESSION ? 7
433 12 : : sp->zipquality >= 6 && sp->zipquality <= 9
434 12 : ? sp->zipquality + 1
435 32 : : sp->zipquality);
436 1641 : if (sp->libdeflate_enc == NULL)
437 : {
438 0 : TIFFErrorExtR(tif, module, "Cannot allocate compressor");
439 0 : break;
440 : }
441 : }
442 :
443 : /* Make sure the output buffer is large enough for the worse case.
444 : */
445 : /* In TIFFWriteBufferSetup(), when libtiff allocates the buffer */
446 : /* we've taken a 10% margin over the uncompressed size, which should
447 : */
448 : /* be large enough even for the the worse case scenario. */
449 10417 : if (libdeflate_zlib_compress_bound(sp->libdeflate_enc, (size_t)cc) >
450 10417 : (size_t)tif->tif_rawdatasize)
451 : {
452 0 : break;
453 : }
454 :
455 10417 : sp->libdeflate_state = 1;
456 10417 : nCompressedBytes = libdeflate_zlib_compress(
457 10417 : sp->libdeflate_enc, bp, (size_t)cc, tif->tif_rawdata,
458 10417 : (size_t)tif->tif_rawdatasize);
459 :
460 10398 : if (nCompressedBytes == 0)
461 : {
462 0 : TIFFErrorExtR(tif, module, "Encoder error at scanline %lu",
463 0 : (unsigned long)tif->tif_row);
464 0 : return 0;
465 : }
466 :
467 10398 : tif->tif_rawcc = nCompressedBytes;
468 :
469 10398 : if (!TIFFFlushData1(tif))
470 0 : return 0;
471 :
472 10421 : return 1;
473 : }
474 : } while (0);
475 6098 : sp->libdeflate_state = 0;
476 : #endif /* LIBDEFLATE_SUPPORT */
477 :
478 6098 : sp->stream.next_in = bp;
479 : assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
480 : we need to simplify this code to reflect a ZLib that is likely updated
481 : to deal with 8byte memory sizes, though this code will respond
482 : appropriately even before we simplify it */
483 : do
484 : {
485 6098 : uInt avail_in_before =
486 6098 : (uint64_t)cc <= 0xFFFFFFFFU ? (uInt)cc : 0xFFFFFFFFU;
487 6098 : sp->stream.avail_in = avail_in_before;
488 : /* coverity[overrun-buffer-arg] */
489 6098 : if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK)
490 : {
491 0 : TIFFErrorExtR(tif, module, "Encoder error: %s", SAFE_MSG(sp));
492 0 : return (0);
493 : }
494 6098 : if (sp->stream.avail_out == 0)
495 : {
496 0 : tif->tif_rawcc = tif->tif_rawdatasize;
497 0 : if (!TIFFFlushData1(tif))
498 0 : return 0;
499 0 : sp->stream.next_out = tif->tif_rawdata;
500 0 : sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
501 0 : ? (uInt)tif->tif_rawdatasize
502 0 : : 0xFFFFFFFFU;
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 14515 : static int ZIPPostEncode(TIFF *tif)
514 : {
515 : static const char module[] = "ZIPPostEncode";
516 14515 : ZIPState *sp = ZIPEncoderState(tif);
517 : int state;
518 :
519 : #if LIBDEFLATE_SUPPORT
520 14515 : if (sp->libdeflate_state == 1)
521 10417 : return 1;
522 : #endif
523 :
524 4098 : sp->stream.avail_in = 0;
525 : do
526 : {
527 4098 : 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 8112 : static void ZIPCleanup(TIFF *tif)
554 : {
555 8112 : ZIPState *sp = GetZIPState(tif);
556 :
557 8112 : assert(sp != 0);
558 :
559 8112 : (void)TIFFPredictorCleanup(tif);
560 :
561 8111 : tif->tif_tagmethods.vgetfield = sp->vgetparent;
562 8111 : tif->tif_tagmethods.vsetfield = sp->vsetparent;
563 :
564 8111 : if (sp->state & ZSTATE_INIT_ENCODE)
565 : {
566 5727 : deflateEnd(&sp->stream);
567 5728 : sp->state = 0;
568 : }
569 2384 : else if (sp->state & ZSTATE_INIT_DECODE)
570 : {
571 490 : inflateEnd(&sp->stream);
572 490 : sp->state = 0;
573 : }
574 :
575 : #if LIBDEFLATE_SUPPORT
576 8112 : if (sp->libdeflate_dec)
577 437 : libdeflate_free_decompressor(sp->libdeflate_dec);
578 8112 : if (sp->libdeflate_enc)
579 1640 : libdeflate_free_compressor(sp->libdeflate_enc);
580 : #endif
581 :
582 8111 : _TIFFfreeExt(tif, sp);
583 8111 : tif->tif_data = NULL;
584 :
585 8111 : _TIFFSetDefaultCompressionState(tif);
586 8112 : }
587 :
588 54637 : static int ZIPVSetField(TIFF *tif, uint32_t tag, va_list ap)
589 : {
590 : static const char module[] = "ZIPVSetField";
591 54637 : ZIPState *sp = GetZIPState(tif);
592 :
593 54637 : 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 50492 : default:
650 50492 : return (*sp->vsetparent)(tif, tag, ap);
651 : }
652 : /*NOTREACHED*/
653 : }
654 :
655 53114 : static int ZIPVGetField(TIFF *tif, uint32_t tag, va_list ap)
656 : {
657 53114 : ZIPState *sp = GetZIPState(tif);
658 :
659 53114 : 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 53114 : default:
670 53114 : 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 29297 : static void *TIFF_zalloc(void *opaque, unsigned int items, unsigned int size)
683 : {
684 : static const char module[] = "TIFF_zalloc";
685 29297 : TIFF *tif = opaque;
686 :
687 29297 : if (items > ~(size_t)0 / size)
688 : {
689 0 : TIFFErrorExtR(tif, module, "Overflow");
690 0 : return NULL;
691 : }
692 :
693 29297 : return _TIFFmallocExt(tif, items * size);
694 : }
695 :
696 29293 : static void TIFF_zfree(void *opaque, void *ptr)
697 : {
698 29293 : _TIFFfreeExt((TIFF *)opaque, ptr);
699 29296 : }
700 :
701 8113 : int TIFFInitZIP(TIFF *tif, int scheme)
702 : {
703 : static const char module[] = "TIFFInitZIP";
704 : ZIPState *sp;
705 :
706 8113 : 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 8113 : 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 8113 : tif->tif_data = (uint8_t *)_TIFFcallocExt(tif, sizeof(ZIPState), 1);
726 8113 : if (tif->tif_data == NULL)
727 0 : goto bad;
728 8113 : sp = GetZIPState(tif);
729 8113 : sp->stream.zalloc = TIFF_zalloc;
730 8113 : sp->stream.zfree = TIFF_zfree;
731 8113 : sp->stream.opaque = tif;
732 8113 : sp->stream.data_type = Z_BINARY;
733 :
734 : /*
735 : * Override parent get/set field methods.
736 : */
737 8113 : sp->vgetparent = tif->tif_tagmethods.vgetfield;
738 8113 : tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
739 8113 : sp->vsetparent = tif->tif_tagmethods.vsetfield;
740 8113 : tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
741 :
742 : /* Default values for codec-specific fields */
743 8113 : sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
744 8113 : sp->state = 0;
745 : #if LIBDEFLATE_SUPPORT
746 8113 : sp->subcodec = DEFLATE_SUBCODEC_LIBDEFLATE;
747 : #else
748 : sp->subcodec = DEFLATE_SUBCODEC_ZLIB;
749 : #endif
750 :
751 : /*
752 : * Install codec methods.
753 : */
754 8113 : tif->tif_fixuptags = ZIPFixupTags;
755 8113 : tif->tif_setupdecode = ZIPSetupDecode;
756 8113 : tif->tif_predecode = ZIPPreDecode;
757 8113 : tif->tif_decoderow = ZIPDecode;
758 8113 : tif->tif_decodestrip = ZIPDecode;
759 8113 : tif->tif_decodetile = ZIPDecode;
760 8113 : tif->tif_setupencode = ZIPSetupEncode;
761 8113 : tif->tif_preencode = ZIPPreEncode;
762 8113 : tif->tif_postencode = ZIPPostEncode;
763 8113 : tif->tif_encoderow = ZIPEncode;
764 8113 : tif->tif_encodestrip = ZIPEncode;
765 8113 : tif->tif_encodetile = ZIPEncode;
766 8113 : tif->tif_cleanup = ZIPCleanup;
767 : /*
768 : * Setup predictor setup.
769 : */
770 8113 : (void)TIFFPredictorInit(tif);
771 8113 : 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 */
|