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