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