/src/PROJ/libtiff/libtiff/tif_zip.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | #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 | 0 | #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 | 0 | #define ZSTATE_INIT_DECODE 0x01 |
82 | 0 | #define ZSTATE_INIT_ENCODE 0x02 |
83 | | |
84 | | TIFFVGetMethod vgetparent; /* super-class method */ |
85 | | TIFFVSetMethod vsetparent; /* super-class method */ |
86 | | } ZIPState; |
87 | | |
88 | 0 | #define GetZIPState(tif) ((ZIPState *)(tif)->tif_data) |
89 | 0 | #define ZIPDecoderState(tif) GetZIPState(tif) |
90 | 0 | #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 | | static int ZIPFixupTags(TIFF *tif) |
96 | 0 | { |
97 | 0 | (void)tif; |
98 | 0 | return (1); |
99 | 0 | } |
100 | | |
101 | | static int ZIPSetupDecode(TIFF *tif) |
102 | 0 | { |
103 | 0 | static const char module[] = "ZIPSetupDecode"; |
104 | 0 | ZIPState *sp = ZIPDecoderState(tif); |
105 | |
|
106 | 0 | assert(sp != NULL); |
107 | | |
108 | | /* if we were last encoding, terminate this mode */ |
109 | 0 | if (sp->state & ZSTATE_INIT_ENCODE) |
110 | 0 | { |
111 | 0 | deflateEnd(&sp->stream); |
112 | 0 | sp->state = 0; |
113 | 0 | } |
114 | | |
115 | | /* This function can possibly be called several times by */ |
116 | | /* PredictorSetupDecode() if this function succeeds but */ |
117 | | /* PredictorSetup() fails */ |
118 | 0 | if ((sp->state & ZSTATE_INIT_DECODE) == 0 && |
119 | 0 | inflateInit(&sp->stream) != Z_OK) |
120 | 0 | { |
121 | 0 | TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp)); |
122 | 0 | return (0); |
123 | 0 | } |
124 | 0 | else |
125 | 0 | { |
126 | 0 | sp->state |= ZSTATE_INIT_DECODE; |
127 | 0 | return (1); |
128 | 0 | } |
129 | 0 | } |
130 | | |
131 | | static inline uint64_t TIFF_MIN_UINT64(uint64_t a, uint64_t b) |
132 | 0 | { |
133 | 0 | return a < b ? a : b; |
134 | 0 | } |
135 | | |
136 | | static inline uInt TIFF_CLAMP_UINT64_TO_INT32_MAX(uint64_t v) |
137 | 0 | { |
138 | 0 | return (uInt)TIFF_MIN_UINT64(v, INT32_MAX); |
139 | 0 | } |
140 | | |
141 | | /* |
142 | | * Setup state for decoding a strip. |
143 | | */ |
144 | | static int ZIPPreDecode(TIFF *tif, uint16_t s) |
145 | 0 | { |
146 | 0 | ZIPState *sp = ZIPDecoderState(tif); |
147 | |
|
148 | 0 | (void)s; |
149 | 0 | assert(sp != NULL); |
150 | | |
151 | 0 | if ((sp->state & ZSTATE_INIT_DECODE) == 0) |
152 | 0 | tif->tif_setupdecode(tif); |
153 | |
|
154 | | #if LIBDEFLATE_SUPPORT |
155 | | sp->libdeflate_state = -1; |
156 | | #endif |
157 | 0 | sp->stream.next_in = tif->tif_rawdata; |
158 | 0 | 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 | 0 | sp->stream.avail_in = TIFF_CLAMP_UINT64_TO_INT32_MAX(tif->tif_rawcc); |
163 | 0 | if (inflateReset(&sp->stream) == Z_OK) |
164 | 0 | { |
165 | 0 | sp->read_error = 0; |
166 | 0 | return 1; |
167 | 0 | } |
168 | 0 | return 0; |
169 | 0 | } |
170 | | |
171 | | static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) |
172 | 0 | { |
173 | 0 | static const char module[] = "ZIPDecode"; |
174 | 0 | ZIPState *sp = ZIPDecoderState(tif); |
175 | |
|
176 | 0 | (void)s; |
177 | 0 | assert(sp != NULL); |
178 | 0 | assert(sp->state == ZSTATE_INIT_DECODE); |
179 | | |
180 | 0 | if (sp->read_error) |
181 | 0 | { |
182 | 0 | memset(op, 0, (size_t)occ); |
183 | 0 | TIFFErrorExtR(tif, module, |
184 | 0 | "ZIPDecode: Scanline %" PRIu32 " cannot be read due to " |
185 | 0 | "previous error", |
186 | 0 | tif->tif_row); |
187 | 0 | return 0; |
188 | 0 | } |
189 | | |
190 | | #if LIBDEFLATE_SUPPORT |
191 | | if (sp->libdeflate_state == 1) |
192 | | 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 | | TIFFDirectory *td = &tif->tif_dir; |
199 | | |
200 | | if (sp->libdeflate_state == 0) |
201 | | break; |
202 | | if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB) |
203 | | break; |
204 | | |
205 | | /* Check if we are in the situation where we can use libdeflate */ |
206 | | if (isTiled(tif)) |
207 | | { |
208 | | if (TIFFTileSize64(tif) != (uint64_t)occ) |
209 | | break; |
210 | | } |
211 | | else |
212 | | { |
213 | | uint32_t strip_height = td->td_imagelength - tif->tif_row; |
214 | | if (strip_height > td->td_rowsperstrip) |
215 | | strip_height = td->td_rowsperstrip; |
216 | | if (TIFFVStripSize64(tif, strip_height) != (uint64_t)occ) |
217 | | 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 | | if (sp->libdeflate_dec == NULL) |
230 | | { |
231 | | sp->libdeflate_dec = libdeflate_alloc_decompressor(); |
232 | | if (sp->libdeflate_dec == NULL) |
233 | | { |
234 | | break; |
235 | | } |
236 | | } |
237 | | |
238 | | sp->libdeflate_state = 1; |
239 | | |
240 | | res = libdeflate_zlib_decompress(sp->libdeflate_dec, tif->tif_rawcp, |
241 | | (size_t)tif->tif_rawcc, op, |
242 | | (size_t)occ, NULL); |
243 | | |
244 | | tif->tif_rawcp += tif->tif_rawcc; |
245 | | 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 | | if (res != LIBDEFLATE_SUCCESS && |
253 | | res != LIBDEFLATE_INSUFFICIENT_SPACE) |
254 | | { |
255 | | memset(op, 0, (size_t)occ); |
256 | | TIFFErrorExtR(tif, module, "Decoding error at scanline %lu", |
257 | | (unsigned long)tif->tif_row); |
258 | | sp->read_error = 1; |
259 | | return 0; |
260 | | } |
261 | | |
262 | | return 1; |
263 | | } |
264 | | } while (0); |
265 | | sp->libdeflate_state = 0; |
266 | | #endif /* LIBDEFLATE_SUPPORT */ |
267 | | |
268 | 0 | sp->stream.next_in = tif->tif_rawcp; |
269 | |
|
270 | 0 | sp->stream.next_out = op; |
271 | 0 | 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 | 0 | do |
276 | 0 | { |
277 | 0 | int state; |
278 | 0 | uInt avail_in_before = TIFF_CLAMP_UINT64_TO_INT32_MAX(tif->tif_rawcc); |
279 | 0 | uInt avail_out_before = TIFF_CLAMP_UINT64_TO_INT32_MAX(occ); |
280 | 0 | sp->stream.avail_in = avail_in_before; |
281 | 0 | sp->stream.avail_out = avail_out_before; |
282 | 0 | state = inflate(&sp->stream, Z_PARTIAL_FLUSH); |
283 | 0 | tif->tif_rawcc -= (avail_in_before - sp->stream.avail_in); |
284 | 0 | occ -= (avail_out_before - sp->stream.avail_out); |
285 | 0 | if (state == Z_STREAM_END) |
286 | 0 | break; |
287 | 0 | if (state == Z_DATA_ERROR) |
288 | 0 | { |
289 | 0 | memset(sp->stream.next_out, 0, (size_t)occ); |
290 | 0 | TIFFErrorExtR(tif, module, "Decoding error at scanline %lu, %s", |
291 | 0 | (unsigned long)tif->tif_row, SAFE_MSG(sp)); |
292 | 0 | sp->read_error = 1; |
293 | 0 | return (0); |
294 | 0 | } |
295 | 0 | if (state != Z_OK) |
296 | 0 | { |
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 | 0 | } |
302 | 0 | } while (occ > 0); |
303 | 0 | if (occ != 0) |
304 | 0 | { |
305 | 0 | TIFFErrorExtR(tif, module, |
306 | 0 | "Not enough data at scanline %lu (short %" PRIu64 |
307 | 0 | " 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 | 0 | } |
313 | | |
314 | 0 | tif->tif_rawcp = sp->stream.next_in; |
315 | |
|
316 | 0 | return (1); |
317 | 0 | } |
318 | | |
319 | | static int ZIPSetupEncode(TIFF *tif) |
320 | 0 | { |
321 | 0 | static const char module[] = "ZIPSetupEncode"; |
322 | 0 | ZIPState *sp = ZIPEncoderState(tif); |
323 | 0 | int cappedQuality; |
324 | |
|
325 | 0 | assert(sp != NULL); |
326 | 0 | if (sp->state & ZSTATE_INIT_DECODE) |
327 | 0 | { |
328 | 0 | inflateEnd(&sp->stream); |
329 | 0 | sp->state = 0; |
330 | 0 | } |
331 | |
|
332 | 0 | cappedQuality = sp->zipquality; |
333 | 0 | if (cappedQuality > Z_BEST_COMPRESSION) |
334 | 0 | cappedQuality = Z_BEST_COMPRESSION; |
335 | |
|
336 | 0 | if (deflateInit(&sp->stream, cappedQuality) != Z_OK) |
337 | 0 | { |
338 | 0 | TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp)); |
339 | 0 | return (0); |
340 | 0 | } |
341 | 0 | else |
342 | 0 | { |
343 | 0 | sp->state |= ZSTATE_INIT_ENCODE; |
344 | 0 | return (1); |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | | /* |
349 | | * Reset encoding state at the start of a strip. |
350 | | */ |
351 | | static int ZIPPreEncode(TIFF *tif, uint16_t s) |
352 | 0 | { |
353 | 0 | ZIPState *sp = ZIPEncoderState(tif); |
354 | |
|
355 | 0 | (void)s; |
356 | 0 | assert(sp != NULL); |
357 | 0 | if (sp->state != ZSTATE_INIT_ENCODE) |
358 | 0 | tif->tif_setupencode(tif); |
359 | |
|
360 | | #if LIBDEFLATE_SUPPORT |
361 | | sp->libdeflate_state = -1; |
362 | | #endif |
363 | 0 | sp->stream.next_out = tif->tif_rawdata; |
364 | 0 | 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 | 0 | sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU |
369 | 0 | ? (uInt)tif->tif_rawdatasize |
370 | 0 | : 0xFFFFFFFFU; |
371 | 0 | return (deflateReset(&sp->stream) == Z_OK); |
372 | 0 | } |
373 | | |
374 | | /* |
375 | | * Encode a chunk of pixels. |
376 | | */ |
377 | | static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) |
378 | 0 | { |
379 | 0 | static const char module[] = "ZIPEncode"; |
380 | 0 | ZIPState *sp = ZIPEncoderState(tif); |
381 | |
|
382 | 0 | assert(sp != NULL); |
383 | 0 | assert(sp->state == ZSTATE_INIT_ENCODE); |
384 | | |
385 | 0 | (void)s; |
386 | |
|
387 | | #if LIBDEFLATE_SUPPORT |
388 | | if (sp->libdeflate_state == 1) |
389 | | 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 | | TIFFDirectory *td = &tif->tif_dir; |
396 | | |
397 | | if (sp->libdeflate_state == 0) |
398 | | break; |
399 | | if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB) |
400 | | break; |
401 | | |
402 | | /* Libdeflate does not support the 0-compression level */ |
403 | | if (sp->zipquality == Z_NO_COMPRESSION) |
404 | | break; |
405 | | |
406 | | /* Check if we are in the situation where we can use libdeflate */ |
407 | | if (isTiled(tif)) |
408 | | { |
409 | | if (TIFFTileSize64(tif) != (uint64_t)cc) |
410 | | break; |
411 | | } |
412 | | else |
413 | | { |
414 | | uint32_t strip_height = td->td_imagelength - tif->tif_row; |
415 | | if (strip_height > td->td_rowsperstrip) |
416 | | strip_height = td->td_rowsperstrip; |
417 | | if (TIFFVStripSize64(tif, strip_height) != (uint64_t)cc) |
418 | | 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 | | if (sp->libdeflate_enc == NULL) |
431 | | { |
432 | | /* To get results as good as zlib, we asked for an extra */ |
433 | | /* level of compression */ |
434 | | sp->libdeflate_enc = libdeflate_alloc_compressor( |
435 | | sp->zipquality == Z_DEFAULT_COMPRESSION ? 7 |
436 | | : sp->zipquality >= 6 && sp->zipquality <= 9 |
437 | | ? sp->zipquality + 1 |
438 | | : sp->zipquality); |
439 | | if (sp->libdeflate_enc == NULL) |
440 | | { |
441 | | TIFFErrorExtR(tif, module, "Cannot allocate compressor"); |
442 | | 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 | | if (libdeflate_zlib_compress_bound(sp->libdeflate_enc, (size_t)cc) > |
453 | | (size_t)tif->tif_rawdatasize) |
454 | | { |
455 | | break; |
456 | | } |
457 | | |
458 | | sp->libdeflate_state = 1; |
459 | | nCompressedBytes = libdeflate_zlib_compress( |
460 | | sp->libdeflate_enc, bp, (size_t)cc, tif->tif_rawdata, |
461 | | (size_t)tif->tif_rawdatasize); |
462 | | |
463 | | if (nCompressedBytes == 0) |
464 | | { |
465 | | TIFFErrorExtR(tif, module, "Encoder error at scanline %lu", |
466 | | (unsigned long)tif->tif_row); |
467 | | return 0; |
468 | | } |
469 | | |
470 | | tif->tif_rawcc = nCompressedBytes; |
471 | | |
472 | | if (!TIFFFlushData1(tif)) |
473 | | return 0; |
474 | | |
475 | | return 1; |
476 | | } |
477 | | } while (0); |
478 | | sp->libdeflate_state = 0; |
479 | | #endif /* LIBDEFLATE_SUPPORT */ |
480 | |
|
481 | 0 | sp->stream.next_in = bp; |
482 | 0 | 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 | 0 | do |
487 | 0 | { |
488 | 0 | uInt avail_in_before = TIFF_CLAMP_UINT64_TO_INT32_MAX(cc); |
489 | 0 | sp->stream.avail_in = avail_in_before; |
490 | 0 | if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) |
491 | 0 | { |
492 | 0 | TIFFErrorExtR(tif, module, "Encoder error: %s", SAFE_MSG(sp)); |
493 | 0 | return (0); |
494 | 0 | } |
495 | 0 | if (sp->stream.avail_out == 0) |
496 | 0 | { |
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 | 0 | } |
504 | 0 | cc -= (avail_in_before - sp->stream.avail_in); |
505 | 0 | } while (cc > 0); |
506 | 0 | return (1); |
507 | 0 | } |
508 | | |
509 | | /* |
510 | | * Finish off an encoded strip by flushing the last |
511 | | * string and tacking on an End Of Information code. |
512 | | */ |
513 | | static int ZIPPostEncode(TIFF *tif) |
514 | 0 | { |
515 | 0 | static const char module[] = "ZIPPostEncode"; |
516 | 0 | ZIPState *sp = ZIPEncoderState(tif); |
517 | 0 | int state; |
518 | |
|
519 | | #if LIBDEFLATE_SUPPORT |
520 | | if (sp->libdeflate_state == 1) |
521 | | return 1; |
522 | | #endif |
523 | |
|
524 | 0 | sp->stream.avail_in = 0; |
525 | 0 | do |
526 | 0 | { |
527 | 0 | state = deflate(&sp->stream, Z_FINISH); |
528 | 0 | switch (state) |
529 | 0 | { |
530 | 0 | case Z_STREAM_END: |
531 | 0 | case Z_OK: |
532 | 0 | if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) |
533 | 0 | { |
534 | 0 | tif->tif_rawcc = |
535 | 0 | tif->tif_rawdatasize - sp->stream.avail_out; |
536 | 0 | if (!TIFFFlushData1(tif)) |
537 | 0 | return 0; |
538 | 0 | sp->stream.next_out = tif->tif_rawdata; |
539 | 0 | sp->stream.avail_out = |
540 | 0 | (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU |
541 | 0 | ? (uInt)tif->tif_rawdatasize |
542 | 0 | : 0xFFFFFFFFU; |
543 | 0 | } |
544 | 0 | break; |
545 | 0 | default: |
546 | 0 | TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp)); |
547 | 0 | return (0); |
548 | 0 | } |
549 | 0 | } while (state != Z_STREAM_END); |
550 | 0 | return (1); |
551 | 0 | } |
552 | | |
553 | | static void ZIPCleanup(TIFF *tif) |
554 | 0 | { |
555 | 0 | ZIPState *sp = GetZIPState(tif); |
556 | |
|
557 | 0 | assert(sp != 0); |
558 | | |
559 | 0 | (void)TIFFPredictorCleanup(tif); |
560 | |
|
561 | 0 | tif->tif_tagmethods.vgetfield = sp->vgetparent; |
562 | 0 | tif->tif_tagmethods.vsetfield = sp->vsetparent; |
563 | |
|
564 | 0 | if (sp->state & ZSTATE_INIT_ENCODE) |
565 | 0 | { |
566 | 0 | deflateEnd(&sp->stream); |
567 | 0 | sp->state = 0; |
568 | 0 | } |
569 | 0 | else if (sp->state & ZSTATE_INIT_DECODE) |
570 | 0 | { |
571 | 0 | inflateEnd(&sp->stream); |
572 | 0 | sp->state = 0; |
573 | 0 | } |
574 | |
|
575 | | #if LIBDEFLATE_SUPPORT |
576 | | if (sp->libdeflate_dec) |
577 | | libdeflate_free_decompressor(sp->libdeflate_dec); |
578 | | if (sp->libdeflate_enc) |
579 | | libdeflate_free_compressor(sp->libdeflate_enc); |
580 | | #endif |
581 | |
|
582 | 0 | _TIFFfreeExt(tif, sp); |
583 | 0 | tif->tif_data = NULL; |
584 | |
|
585 | 0 | _TIFFSetDefaultCompressionState(tif); |
586 | 0 | } |
587 | | |
588 | | static int ZIPVSetField(TIFF *tif, uint32_t tag, va_list ap) |
589 | 0 | { |
590 | 0 | static const char module[] = "ZIPVSetField"; |
591 | 0 | ZIPState *sp = GetZIPState(tif); |
592 | |
|
593 | 0 | switch (tag) |
594 | 0 | { |
595 | 0 | case TIFFTAG_ZIPQUALITY: |
596 | 0 | sp->zipquality = (int)va_arg(ap, int); |
597 | 0 | if (sp->zipquality < Z_DEFAULT_COMPRESSION || |
598 | 0 | sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL) |
599 | 0 | { |
600 | 0 | TIFFErrorExtR( |
601 | 0 | tif, module, |
602 | 0 | "Invalid ZipQuality value. Should be in [-1,%d] range", |
603 | 0 | LIBDEFLATE_MAX_COMPRESSION_LEVEL); |
604 | 0 | return 0; |
605 | 0 | } |
606 | | |
607 | 0 | if (sp->state & ZSTATE_INIT_ENCODE) |
608 | 0 | { |
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 | 0 | Z_DEFAULT_STRATEGY) != Z_OK) |
614 | 0 | { |
615 | 0 | TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp)); |
616 | 0 | return (0); |
617 | 0 | } |
618 | 0 | } |
619 | | |
620 | | #if LIBDEFLATE_SUPPORT |
621 | | if (sp->libdeflate_enc) |
622 | | { |
623 | | libdeflate_free_compressor(sp->libdeflate_enc); |
624 | | sp->libdeflate_enc = NULL; |
625 | | } |
626 | | #endif |
627 | | |
628 | 0 | return (1); |
629 | | |
630 | 0 | case TIFFTAG_DEFLATE_SUBCODEC: |
631 | 0 | sp->subcodec = (int)va_arg(ap, int); |
632 | 0 | if (sp->subcodec != DEFLATE_SUBCODEC_ZLIB && |
633 | 0 | sp->subcodec != DEFLATE_SUBCODEC_LIBDEFLATE) |
634 | 0 | { |
635 | 0 | TIFFErrorExtR(tif, module, "Invalid DeflateCodec value."); |
636 | 0 | return 0; |
637 | 0 | } |
638 | 0 | #if !LIBDEFLATE_SUPPORT |
639 | 0 | if (sp->subcodec == DEFLATE_SUBCODEC_LIBDEFLATE) |
640 | 0 | { |
641 | 0 | TIFFErrorExtR(tif, module, |
642 | 0 | "DeflateCodec = DEFLATE_SUBCODEC_LIBDEFLATE " |
643 | 0 | "unsupported in this build"); |
644 | 0 | return 0; |
645 | 0 | } |
646 | 0 | #endif |
647 | 0 | return 1; |
648 | | |
649 | 0 | default: |
650 | 0 | return (*sp->vsetparent)(tif, tag, ap); |
651 | 0 | } |
652 | | /*NOTREACHED*/ |
653 | 0 | } |
654 | | |
655 | | static int ZIPVGetField(TIFF *tif, uint32_t tag, va_list ap) |
656 | 0 | { |
657 | 0 | ZIPState *sp = GetZIPState(tif); |
658 | |
|
659 | 0 | switch (tag) |
660 | 0 | { |
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 | 0 | default: |
670 | 0 | return (*sp->vgetparent)(tif, tag, ap); |
671 | 0 | } |
672 | 0 | return (1); |
673 | 0 | } |
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 | | static void *TIFF_zalloc(void *opaque, unsigned int items, unsigned int size) |
683 | 0 | { |
684 | 0 | static const char module[] = "TIFF_zalloc"; |
685 | 0 | TIFF *tif = opaque; |
686 | |
|
687 | 0 | if (items > ~(size_t)0 / size) |
688 | 0 | { |
689 | 0 | TIFFErrorExtR(tif, module, "Overflow"); |
690 | 0 | return NULL; |
691 | 0 | } |
692 | | |
693 | 0 | return _TIFFmallocExt(tif, items * size); |
694 | 0 | } |
695 | | |
696 | | static void TIFF_zfree(void *opaque, void *ptr) |
697 | 0 | { |
698 | 0 | _TIFFfreeExt((TIFF *)opaque, ptr); |
699 | 0 | } |
700 | | |
701 | | int TIFFInitZIP(TIFF *tif, int scheme) |
702 | 0 | { |
703 | 0 | static const char module[] = "TIFFInitZIP"; |
704 | 0 | ZIPState *sp; |
705 | |
|
706 | 0 | assert((scheme == COMPRESSION_DEFLATE) || |
707 | 0 | (scheme == COMPRESSION_ADOBE_DEFLATE)); |
708 | | #ifdef NDEBUG |
709 | | (void)scheme; |
710 | | #endif |
711 | | |
712 | | /* |
713 | | * Merge codec-specific tag information. |
714 | | */ |
715 | 0 | if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields))) |
716 | 0 | { |
717 | 0 | TIFFErrorExtR(tif, module, |
718 | 0 | "Merging Deflate codec-specific tags failed"); |
719 | 0 | return 0; |
720 | 0 | } |
721 | | |
722 | | /* |
723 | | * Allocate state block so tag methods have storage to record values. |
724 | | */ |
725 | 0 | tif->tif_data = (uint8_t *)_TIFFcallocExt(tif, sizeof(ZIPState), 1); |
726 | 0 | if (tif->tif_data == NULL) |
727 | 0 | goto bad; |
728 | 0 | sp = GetZIPState(tif); |
729 | 0 | sp->stream.zalloc = TIFF_zalloc; |
730 | 0 | sp->stream.zfree = TIFF_zfree; |
731 | 0 | sp->stream.opaque = tif; |
732 | 0 | sp->stream.data_type = Z_BINARY; |
733 | | |
734 | | /* |
735 | | * Override parent get/set field methods. |
736 | | */ |
737 | 0 | sp->vgetparent = tif->tif_tagmethods.vgetfield; |
738 | 0 | tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */ |
739 | 0 | sp->vsetparent = tif->tif_tagmethods.vsetfield; |
740 | 0 | tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */ |
741 | | |
742 | | /* Default values for codec-specific fields */ |
743 | 0 | sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */ |
744 | 0 | sp->state = 0; |
745 | | #if LIBDEFLATE_SUPPORT |
746 | | sp->subcodec = DEFLATE_SUBCODEC_LIBDEFLATE; |
747 | | #else |
748 | 0 | sp->subcodec = DEFLATE_SUBCODEC_ZLIB; |
749 | 0 | #endif |
750 | | |
751 | | /* |
752 | | * Install codec methods. |
753 | | */ |
754 | 0 | tif->tif_fixuptags = ZIPFixupTags; |
755 | 0 | tif->tif_setupdecode = ZIPSetupDecode; |
756 | 0 | tif->tif_predecode = ZIPPreDecode; |
757 | 0 | tif->tif_decoderow = ZIPDecode; |
758 | 0 | tif->tif_decodestrip = ZIPDecode; |
759 | 0 | tif->tif_decodetile = ZIPDecode; |
760 | 0 | tif->tif_setupencode = ZIPSetupEncode; |
761 | 0 | tif->tif_preencode = ZIPPreEncode; |
762 | 0 | tif->tif_postencode = ZIPPostEncode; |
763 | 0 | tif->tif_encoderow = ZIPEncode; |
764 | 0 | tif->tif_encodestrip = ZIPEncode; |
765 | 0 | tif->tif_encodetile = ZIPEncode; |
766 | 0 | tif->tif_cleanup = ZIPCleanup; |
767 | | /* |
768 | | * Setup predictor setup. |
769 | | */ |
770 | 0 | (void)TIFFPredictorInit(tif); |
771 | 0 | return (1); |
772 | 0 | bad: |
773 | 0 | TIFFErrorExtR(tif, module, "No space for ZIP state block"); |
774 | 0 | return (0); |
775 | 0 | } |
776 | | #endif /* ZIP_SUPPORT */ |