/src/freeimage-svn/FreeImage/trunk/Source/LibTIFF4/tif_read.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright (c) 1988-1997 Sam Leffler  | 
3  |  |  * Copyright (c) 1991-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  |  | /*  | 
26  |  |  * TIFF Library.  | 
27  |  |  * Scanline-oriented Read Support  | 
28  |  |  */  | 
29  |  | #include "tiffiop.h"  | 
30  |  | #include <stdio.h>  | 
31  |  |  | 
32  |  | int TIFFFillStrip(TIFF *tif, uint32_t strip);  | 
33  |  | int TIFFFillTile(TIFF *tif, uint32_t tile);  | 
34  |  | static int TIFFStartStrip(TIFF *tif, uint32_t strip);  | 
35  |  | static int TIFFStartTile(TIFF *tif, uint32_t tile);  | 
36  |  | static int TIFFCheckRead(TIFF *, int);  | 
37  |  | static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf,  | 
38  |  |                                   tmsize_t size, const char *module);  | 
39  |  | static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf,  | 
40  |  |                                  tmsize_t size, const char *module);  | 
41  |  |  | 
42  | 0  | #define NOSTRIP ((uint32_t)(-1)) /* undefined state */  | 
43  | 0  | #define NOTILE ((uint32_t)(-1))  /* undefined state */  | 
44  |  |  | 
45  | 0  | #define INITIAL_THRESHOLD (1024 * 1024)  | 
46  | 0  | #define THRESHOLD_MULTIPLIER 10  | 
47  |  | #define MAX_THRESHOLD                                                          \  | 
48  | 0  |     (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER *      \  | 
49  | 0  |      INITIAL_THRESHOLD)  | 
50  |  |  | 
51  | 0  | #define TIFF_INT64_MAX ((((int64_t)0x7FFFFFFF) << 32) | 0xFFFFFFFF)  | 
52  |  |  | 
53  |  | /* Read 'size' bytes in tif_rawdata buffer starting at offset 'rawdata_offset'  | 
54  |  |  * Returns 1 in case of success, 0 otherwise. */  | 
55  |  | static int TIFFReadAndRealloc(TIFF *tif, tmsize_t size, tmsize_t rawdata_offset,  | 
56  |  |                               int is_strip, uint32_t strip_or_tile,  | 
57  |  |                               const char *module)  | 
58  | 0  | { | 
59  | 0  | #if SIZEOF_SIZE_T == 8  | 
60  | 0  |     tmsize_t threshold = INITIAL_THRESHOLD;  | 
61  | 0  | #endif  | 
62  | 0  |     tmsize_t already_read = 0;  | 
63  |  | 
  | 
64  |  | #if SIZEOF_SIZE_T != 8  | 
65  |  |     /* On 32 bit processes, if the request is large enough, check against */  | 
66  |  |     /* file size */  | 
67  |  |     if (size > 1000 * 1000 * 1000)  | 
68  |  |     { | 
69  |  |         uint64_t filesize = TIFFGetFileSize(tif);  | 
70  |  |         if ((uint64_t)size >= filesize)  | 
71  |  |         { | 
72  |  |             TIFFErrorExtR(tif, module,  | 
73  |  |                           "Chunk size requested is larger than file size.");  | 
74  |  |             return 0;  | 
75  |  |         }  | 
76  |  |     }  | 
77  |  | #endif  | 
78  |  |  | 
79  |  |     /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */  | 
80  |  |     /* so as to avoid allocating too much memory in case the file is too */  | 
81  |  |     /* short. We could ask for the file size, but this might be */  | 
82  |  |     /* expensive with some I/O layers (think of reading a gzipped file) */  | 
83  |  |     /* Restrict to 64 bit processes, so as to avoid reallocs() */  | 
84  |  |     /* on 32 bit processes where virtual memory is scarce.  */  | 
85  | 0  |     while (already_read < size)  | 
86  | 0  |     { | 
87  | 0  |         tmsize_t bytes_read;  | 
88  | 0  |         tmsize_t to_read = size - already_read;  | 
89  | 0  | #if SIZEOF_SIZE_T == 8  | 
90  | 0  |         if (to_read >= threshold && threshold < MAX_THRESHOLD &&  | 
91  | 0  |             already_read + to_read + rawdata_offset > tif->tif_rawdatasize)  | 
92  | 0  |         { | 
93  | 0  |             to_read = threshold;  | 
94  | 0  |             threshold *= THRESHOLD_MULTIPLIER;  | 
95  | 0  |         }  | 
96  | 0  | #endif  | 
97  | 0  |         if (already_read + to_read + rawdata_offset > tif->tif_rawdatasize)  | 
98  | 0  |         { | 
99  | 0  |             uint8_t *new_rawdata;  | 
100  | 0  |             assert((tif->tif_flags & TIFF_MYBUFFER) != 0);  | 
101  | 0  |             tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64(  | 
102  | 0  |                 (uint64_t)already_read + to_read + rawdata_offset, 1024);  | 
103  | 0  |             if (tif->tif_rawdatasize == 0)  | 
104  | 0  |             { | 
105  | 0  |                 TIFFErrorExtR(tif, module, "Invalid buffer size");  | 
106  | 0  |                 return 0;  | 
107  | 0  |             }  | 
108  | 0  |             new_rawdata = (uint8_t *)_TIFFreallocExt(tif, tif->tif_rawdata,  | 
109  | 0  |                                                      tif->tif_rawdatasize);  | 
110  | 0  |             if (new_rawdata == 0)  | 
111  | 0  |             { | 
112  | 0  |                 TIFFErrorExtR(tif, module,  | 
113  | 0  |                               "No space for data buffer at scanline %" PRIu32,  | 
114  | 0  |                               tif->tif_row);  | 
115  | 0  |                 _TIFFfreeExt(tif, tif->tif_rawdata);  | 
116  | 0  |                 tif->tif_rawdata = 0;  | 
117  | 0  |                 tif->tif_rawdatasize = 0;  | 
118  | 0  |                 return 0;  | 
119  | 0  |             }  | 
120  | 0  |             tif->tif_rawdata = new_rawdata;  | 
121  | 0  |         }  | 
122  | 0  |         if (tif->tif_rawdata == NULL)  | 
123  | 0  |         { | 
124  |  |             /* should not happen in practice but helps CoverityScan */  | 
125  | 0  |             return 0;  | 
126  | 0  |         }  | 
127  |  |  | 
128  | 0  |         bytes_read = TIFFReadFile(  | 
129  | 0  |             tif, tif->tif_rawdata + rawdata_offset + already_read, to_read);  | 
130  | 0  |         already_read += bytes_read;  | 
131  | 0  |         if (bytes_read != to_read)  | 
132  | 0  |         { | 
133  | 0  |             memset(tif->tif_rawdata + rawdata_offset + already_read, 0,  | 
134  | 0  |                    tif->tif_rawdatasize - rawdata_offset - already_read);  | 
135  | 0  |             if (is_strip)  | 
136  | 0  |             { | 
137  | 0  |                 TIFFErrorExtR(tif, module,  | 
138  | 0  |                               "Read error at scanline %" PRIu32  | 
139  | 0  |                               "; got %" TIFF_SSIZE_FORMAT " bytes, "  | 
140  | 0  |                               "expected %" TIFF_SSIZE_FORMAT,  | 
141  | 0  |                               tif->tif_row, already_read, size);  | 
142  | 0  |             }  | 
143  | 0  |             else  | 
144  | 0  |             { | 
145  | 0  |                 TIFFErrorExtR(tif, module,  | 
146  | 0  |                               "Read error at row %" PRIu32 ", col %" PRIu32  | 
147  | 0  |                               ", tile %" PRIu32 "; "  | 
148  | 0  |                               "got %" TIFF_SSIZE_FORMAT  | 
149  | 0  |                               " bytes, expected %" TIFF_SSIZE_FORMAT "",  | 
150  | 0  |                               tif->tif_row, tif->tif_col, strip_or_tile,  | 
151  | 0  |                               already_read, size);  | 
152  | 0  |             }  | 
153  | 0  |             return 0;  | 
154  | 0  |         }  | 
155  | 0  |     }  | 
156  | 0  |     return 1;  | 
157  | 0  | }  | 
158  |  |  | 
159  |  | static int TIFFFillStripPartial(TIFF *tif, int strip, tmsize_t read_ahead,  | 
160  |  |                                 int restart)  | 
161  | 0  | { | 
162  | 0  |     static const char module[] = "TIFFFillStripPartial";  | 
163  | 0  |     register TIFFDirectory *td = &tif->tif_dir;  | 
164  | 0  |     tmsize_t unused_data;  | 
165  | 0  |     uint64_t read_offset;  | 
166  | 0  |     tmsize_t to_read;  | 
167  | 0  |     tmsize_t read_ahead_mod;  | 
168  |  |     /* tmsize_t bytecountm; */  | 
169  |  |  | 
170  |  |     /*  | 
171  |  |      * Expand raw data buffer, if needed, to hold data  | 
172  |  |      * strip coming from file (perhaps should set upper  | 
173  |  |      * bound on the size of a buffer we'll use?).  | 
174  |  |      */  | 
175  |  |  | 
176  |  |     /* bytecountm=(tmsize_t) TIFFGetStrileByteCount(tif, strip); */  | 
177  |  |  | 
178  |  |     /* Not completely sure where the * 2 comes from, but probably for */  | 
179  |  |     /* an exponentional growth strategy of tif_rawdatasize */  | 
180  | 0  |     if (read_ahead < TIFF_TMSIZE_T_MAX / 2)  | 
181  | 0  |         read_ahead_mod = read_ahead * 2;  | 
182  | 0  |     else  | 
183  | 0  |         read_ahead_mod = read_ahead;  | 
184  | 0  |     if (read_ahead_mod > tif->tif_rawdatasize)  | 
185  | 0  |     { | 
186  | 0  |         assert(restart);  | 
187  |  | 
  | 
188  | 0  |         tif->tif_curstrip = NOSTRIP;  | 
189  | 0  |         if ((tif->tif_flags & TIFF_MYBUFFER) == 0)  | 
190  | 0  |         { | 
191  | 0  |             TIFFErrorExtR(tif, module,  | 
192  | 0  |                           "Data buffer too small to hold part of strip %d",  | 
193  | 0  |                           strip);  | 
194  | 0  |             return (0);  | 
195  | 0  |         }  | 
196  | 0  |     }  | 
197  |  |  | 
198  | 0  |     if (restart)  | 
199  | 0  |     { | 
200  | 0  |         tif->tif_rawdataloaded = 0;  | 
201  | 0  |         tif->tif_rawdataoff = 0;  | 
202  | 0  |     }  | 
203  |  |  | 
204  |  |     /*  | 
205  |  |     ** If we are reading more data, move any unused data to the  | 
206  |  |     ** start of the buffer.  | 
207  |  |     */  | 
208  | 0  |     if (tif->tif_rawdataloaded > 0)  | 
209  | 0  |         unused_data =  | 
210  | 0  |             tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);  | 
211  | 0  |     else  | 
212  | 0  |         unused_data = 0;  | 
213  |  | 
  | 
214  | 0  |     if (unused_data > 0)  | 
215  | 0  |     { | 
216  | 0  |         assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);  | 
217  | 0  |         memmove(tif->tif_rawdata, tif->tif_rawcp, unused_data);  | 
218  | 0  |     }  | 
219  |  |  | 
220  |  |     /*  | 
221  |  |     ** Seek to the point in the file where more data should be read.  | 
222  |  |     */  | 
223  | 0  |     read_offset = TIFFGetStrileOffset(tif, strip) + tif->tif_rawdataoff +  | 
224  | 0  |                   tif->tif_rawdataloaded;  | 
225  |  | 
  | 
226  | 0  |     if (!SeekOK(tif, read_offset))  | 
227  | 0  |     { | 
228  | 0  |         TIFFErrorExtR(tif, module,  | 
229  | 0  |                       "Seek error at scanline %" PRIu32 ", strip %d",  | 
230  | 0  |                       tif->tif_row, strip);  | 
231  | 0  |         return 0;  | 
232  | 0  |     }  | 
233  |  |  | 
234  |  |     /*  | 
235  |  |     ** How much do we want to read?  | 
236  |  |     */  | 
237  | 0  |     if (read_ahead_mod > tif->tif_rawdatasize)  | 
238  | 0  |         to_read = read_ahead_mod - unused_data;  | 
239  | 0  |     else  | 
240  | 0  |         to_read = tif->tif_rawdatasize - unused_data;  | 
241  | 0  |     if ((uint64_t)to_read > TIFFGetStrileByteCount(tif, strip) -  | 
242  | 0  |                                 tif->tif_rawdataoff - tif->tif_rawdataloaded)  | 
243  | 0  |     { | 
244  | 0  |         to_read = (tmsize_t)TIFFGetStrileByteCount(tif, strip) -  | 
245  | 0  |                   tif->tif_rawdataoff - tif->tif_rawdataloaded;  | 
246  | 0  |     }  | 
247  |  | 
  | 
248  | 0  |     assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);  | 
249  | 0  |     if (!TIFFReadAndRealloc(tif, to_read, unused_data, 1, /* is_strip */  | 
250  | 0  |                             0,                            /* strip_or_tile */  | 
251  | 0  |                             module))  | 
252  | 0  |     { | 
253  | 0  |         return 0;  | 
254  | 0  |     }  | 
255  |  |  | 
256  | 0  |     tif->tif_rawdataoff =  | 
257  | 0  |         tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data;  | 
258  | 0  |     tif->tif_rawdataloaded = unused_data + to_read;  | 
259  |  | 
  | 
260  | 0  |     tif->tif_rawcc = tif->tif_rawdataloaded;  | 
261  | 0  |     tif->tif_rawcp = tif->tif_rawdata;  | 
262  |  | 
  | 
263  | 0  |     if (!isFillOrder(tif, td->td_fillorder) &&  | 
264  | 0  |         (tif->tif_flags & TIFF_NOBITREV) == 0)  | 
265  | 0  |     { | 
266  | 0  |         assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);  | 
267  | 0  |         TIFFReverseBits(tif->tif_rawdata + unused_data, to_read);  | 
268  | 0  |     }  | 
269  |  |  | 
270  |  |     /*  | 
271  |  |     ** When starting a strip from the beginning we need to  | 
272  |  |     ** restart the decoder.  | 
273  |  |     */  | 
274  | 0  |     if (restart)  | 
275  | 0  |     { | 
276  |  | 
  | 
277  | 0  | #ifdef JPEG_SUPPORT  | 
278  |  |         /* A bit messy since breaks the codec abstraction. Ultimately */  | 
279  |  |         /* there should be a function pointer for that, but it seems */  | 
280  |  |         /* only JPEG is affected. */  | 
281  |  |         /* For JPEG, if there are multiple scans (can generally be known */  | 
282  |  |         /* with the  read_ahead used), we need to read the whole strip */  | 
283  | 0  |         if (tif->tif_dir.td_compression == COMPRESSION_JPEG &&  | 
284  | 0  |             (uint64_t)tif->tif_rawcc < TIFFGetStrileByteCount(tif, strip))  | 
285  | 0  |         { | 
286  | 0  |             if (TIFFJPEGIsFullStripRequired(tif))  | 
287  | 0  |             { | 
288  | 0  |                 return TIFFFillStrip(tif, strip);  | 
289  | 0  |             }  | 
290  | 0  |         }  | 
291  | 0  | #endif  | 
292  |  |  | 
293  | 0  |         return TIFFStartStrip(tif, strip);  | 
294  | 0  |     }  | 
295  | 0  |     else  | 
296  | 0  |     { | 
297  | 0  |         return 1;  | 
298  | 0  |     }  | 
299  | 0  | }  | 
300  |  |  | 
301  |  | /*  | 
302  |  |  * Seek to a random row+sample in a file.  | 
303  |  |  *  | 
304  |  |  * Only used by TIFFReadScanline, and is only used on  | 
305  |  |  * strip organized files.  We do some tricky stuff to try  | 
306  |  |  * and avoid reading the whole compressed raw data for big  | 
307  |  |  * strips.  | 
308  |  |  */  | 
309  |  | static int TIFFSeek(TIFF *tif, uint32_t row, uint16_t sample)  | 
310  | 0  | { | 
311  | 0  |     register TIFFDirectory *td = &tif->tif_dir;  | 
312  | 0  |     uint32_t strip;  | 
313  | 0  |     int whole_strip;  | 
314  | 0  |     tmsize_t read_ahead = 0;  | 
315  |  |  | 
316  |  |     /*  | 
317  |  |     ** Establish what strip we are working from.  | 
318  |  |     */  | 
319  | 0  |     if (row >= td->td_imagelength)  | 
320  | 0  |     { /* out of range */ | 
321  | 0  |         TIFFErrorExtR(tif, tif->tif_name,  | 
322  | 0  |                       "%" PRIu32 ": Row out of range, max %" PRIu32 "", row,  | 
323  | 0  |                       td->td_imagelength);  | 
324  | 0  |         return (0);  | 
325  | 0  |     }  | 
326  | 0  |     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)  | 
327  | 0  |     { | 
328  | 0  |         if (sample >= td->td_samplesperpixel)  | 
329  | 0  |         { | 
330  | 0  |             TIFFErrorExtR(tif, tif->tif_name,  | 
331  | 0  |                           "%" PRIu16 ": Sample out of range, max %" PRIu16 "",  | 
332  | 0  |                           sample, td->td_samplesperpixel);  | 
333  | 0  |             return (0);  | 
334  | 0  |         }  | 
335  | 0  |         strip = (uint32_t)sample * td->td_stripsperimage +  | 
336  | 0  |                 row / td->td_rowsperstrip;  | 
337  | 0  |     }  | 
338  | 0  |     else  | 
339  | 0  |         strip = row / td->td_rowsperstrip;  | 
340  |  |  | 
341  |  |         /*  | 
342  |  |          * Do we want to treat this strip as one whole chunk or  | 
343  |  |          * read it a few lines at a time?  | 
344  |  |          */  | 
345  |  | #if defined(CHUNKY_STRIP_READ_SUPPORT)  | 
346  |  |     whole_strip = TIFFGetStrileByteCount(tif, strip) < 10 || isMapped(tif);  | 
347  |  |     if (td->td_compression == COMPRESSION_LERC ||  | 
348  |  |         td->td_compression == COMPRESSION_JBIG)  | 
349  |  |     { | 
350  |  |         /* Ideally plugins should have a way to declare they don't support  | 
351  |  |          * chunk strip */  | 
352  |  |         whole_strip = 1;  | 
353  |  |     }  | 
354  |  | #else  | 
355  | 0  |     whole_strip = 1;  | 
356  | 0  | #endif  | 
357  |  | 
  | 
358  | 0  |     if (!whole_strip)  | 
359  | 0  |     { | 
360  |  |         /* 16 is for YCbCr mode where we may need to read 16 */  | 
361  |  |         /* lines at a time to get a decompressed line, and 5000 */  | 
362  |  |         /* is some constant value, for example for JPEG tables */  | 
363  | 0  |         if (tif->tif_scanlinesize < TIFF_TMSIZE_T_MAX / 16 &&  | 
364  | 0  |             tif->tif_scanlinesize * 16 < TIFF_TMSIZE_T_MAX - 5000)  | 
365  | 0  |         { | 
366  | 0  |             read_ahead = tif->tif_scanlinesize * 16 + 5000;  | 
367  | 0  |         }  | 
368  | 0  |         else  | 
369  | 0  |         { | 
370  | 0  |             read_ahead = tif->tif_scanlinesize;  | 
371  | 0  |         }  | 
372  | 0  |     }  | 
373  |  |  | 
374  |  |     /*  | 
375  |  |      * If we haven't loaded this strip, do so now, possibly  | 
376  |  |      * only reading the first part.  | 
377  |  |      */  | 
378  | 0  |     if (strip != tif->tif_curstrip)  | 
379  | 0  |     { /* different strip, refill */ | 
380  |  | 
  | 
381  | 0  |         if (whole_strip)  | 
382  | 0  |         { | 
383  | 0  |             if (!TIFFFillStrip(tif, strip))  | 
384  | 0  |                 return (0);  | 
385  | 0  |         }  | 
386  | 0  |         else  | 
387  | 0  |         { | 
388  | 0  |             if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))  | 
389  | 0  |                 return 0;  | 
390  | 0  |         }  | 
391  | 0  |     }  | 
392  |  |  | 
393  |  |     /*  | 
394  |  |     ** If we already have some data loaded, do we need to read some more?  | 
395  |  |     */  | 
396  | 0  |     else if (!whole_strip)  | 
397  | 0  |     { | 
398  | 0  |         if (((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) <  | 
399  | 0  |                 read_ahead &&  | 
400  | 0  |             (uint64_t)tif->tif_rawdataoff + tif->tif_rawdataloaded <  | 
401  | 0  |                 TIFFGetStrileByteCount(tif, strip))  | 
402  | 0  |         { | 
403  | 0  |             if (!TIFFFillStripPartial(tif, strip, read_ahead, 0))  | 
404  | 0  |                 return 0;  | 
405  | 0  |         }  | 
406  | 0  |     }  | 
407  |  |  | 
408  | 0  |     if (row < tif->tif_row)  | 
409  | 0  |     { | 
410  |  |         /*  | 
411  |  |          * Moving backwards within the same strip: backup  | 
412  |  |          * to the start and then decode forward (below).  | 
413  |  |          *  | 
414  |  |          * NB: If you're planning on lots of random access within a  | 
415  |  |          * strip, it's better to just read and decode the entire  | 
416  |  |          * strip, and then access the decoded data in a random fashion.  | 
417  |  |          */  | 
418  |  | 
  | 
419  | 0  |         if (tif->tif_rawdataoff != 0)  | 
420  | 0  |         { | 
421  | 0  |             if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))  | 
422  | 0  |                 return 0;  | 
423  | 0  |         }  | 
424  | 0  |         else  | 
425  | 0  |         { | 
426  | 0  |             if (!TIFFStartStrip(tif, strip))  | 
427  | 0  |                 return (0);  | 
428  | 0  |         }  | 
429  | 0  |     }  | 
430  |  |  | 
431  | 0  |     if (row != tif->tif_row)  | 
432  | 0  |     { | 
433  |  |         /*  | 
434  |  |          * Seek forward to the desired row.  | 
435  |  |          */  | 
436  |  |  | 
437  |  |         /* TODO: Will this really work with partial buffers? */  | 
438  |  | 
  | 
439  | 0  |         if (!(*tif->tif_seek)(tif, row - tif->tif_row))  | 
440  | 0  |             return (0);  | 
441  | 0  |         tif->tif_row = row;  | 
442  | 0  |     }  | 
443  |  |  | 
444  | 0  |     return (1);  | 
445  | 0  | }  | 
446  |  |  | 
447  |  | int TIFFReadScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample)  | 
448  | 0  | { | 
449  | 0  |     int e;  | 
450  |  | 
  | 
451  | 0  |     if (!TIFFCheckRead(tif, 0))  | 
452  | 0  |         return (-1);  | 
453  | 0  |     if ((e = TIFFSeek(tif, row, sample)) != 0)  | 
454  | 0  |     { | 
455  |  |         /*  | 
456  |  |          * Decompress desired row into user buffer.  | 
457  |  |          */  | 
458  | 0  |         e = (*tif->tif_decoderow)(tif, (uint8_t *)buf, tif->tif_scanlinesize,  | 
459  | 0  |                                   sample);  | 
460  |  |  | 
461  |  |         /* we are now poised at the beginning of the next row */  | 
462  | 0  |         tif->tif_row = row + 1;  | 
463  |  | 
  | 
464  | 0  |         if (e)  | 
465  | 0  |             (*tif->tif_postdecode)(tif, (uint8_t *)buf, tif->tif_scanlinesize);  | 
466  | 0  |     }  | 
467  | 0  |     return (e > 0 ? 1 : -1);  | 
468  | 0  | }  | 
469  |  |  | 
470  |  | /*  | 
471  |  |  * Calculate the strip size according to the number of  | 
472  |  |  * rows in the strip (check for truncated last strip on any  | 
473  |  |  * of the separations).  | 
474  |  |  */  | 
475  |  | static tmsize_t TIFFReadEncodedStripGetStripSize(TIFF *tif, uint32_t strip,  | 
476  |  |                                                  uint16_t *pplane)  | 
477  | 0  | { | 
478  | 0  |     static const char module[] = "TIFFReadEncodedStrip";  | 
479  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
480  | 0  |     uint32_t rowsperstrip;  | 
481  | 0  |     uint32_t stripsperplane;  | 
482  | 0  |     uint32_t stripinplane;  | 
483  | 0  |     uint32_t rows;  | 
484  | 0  |     tmsize_t stripsize;  | 
485  | 0  |     if (!TIFFCheckRead(tif, 0))  | 
486  | 0  |         return ((tmsize_t)(-1));  | 
487  | 0  |     if (strip >= td->td_nstrips)  | 
488  | 0  |     { | 
489  | 0  |         TIFFErrorExtR(tif, module,  | 
490  | 0  |                       "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,  | 
491  | 0  |                       td->td_nstrips);  | 
492  | 0  |         return ((tmsize_t)(-1));  | 
493  | 0  |     }  | 
494  |  |  | 
495  | 0  |     rowsperstrip = td->td_rowsperstrip;  | 
496  | 0  |     if (rowsperstrip > td->td_imagelength)  | 
497  | 0  |         rowsperstrip = td->td_imagelength;  | 
498  | 0  |     if (rowsperstrip == 0)  | 
499  | 0  |     { | 
500  | 0  |         TIFFErrorExtR(tif, module, "rowsperstrip is zero");  | 
501  | 0  |         return ((tmsize_t)(-1));  | 
502  | 0  |     }  | 
503  | 0  |     stripsperplane =  | 
504  | 0  |         TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);  | 
505  | 0  |     stripinplane = (strip % stripsperplane);  | 
506  | 0  |     if (pplane)  | 
507  | 0  |         *pplane = (uint16_t)(strip / stripsperplane);  | 
508  | 0  |     rows = td->td_imagelength - stripinplane * rowsperstrip;  | 
509  | 0  |     if (rows > rowsperstrip)  | 
510  | 0  |         rows = rowsperstrip;  | 
511  | 0  |     stripsize = TIFFVStripSize(tif, rows);  | 
512  | 0  |     if (stripsize == 0)  | 
513  | 0  |         return ((tmsize_t)(-1));  | 
514  | 0  |     return stripsize;  | 
515  | 0  | }  | 
516  |  |  | 
517  |  | /*  | 
518  |  |  * Read a strip of data and decompress the specified  | 
519  |  |  * amount into the user-supplied buffer.  | 
520  |  |  */  | 
521  |  | tmsize_t TIFFReadEncodedStrip(TIFF *tif, uint32_t strip, void *buf,  | 
522  |  |                               tmsize_t size)  | 
523  | 0  | { | 
524  | 0  |     static const char module[] = "TIFFReadEncodedStrip";  | 
525  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
526  | 0  |     tmsize_t stripsize;  | 
527  | 0  |     uint16_t plane;  | 
528  |  | 
  | 
529  | 0  |     stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);  | 
530  | 0  |     if (stripsize == ((tmsize_t)(-1)))  | 
531  | 0  |         return ((tmsize_t)(-1));  | 
532  |  |  | 
533  |  |     /* shortcut to avoid an extra memcpy() */  | 
534  | 0  |     if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&  | 
535  | 0  |         size >= stripsize && !isMapped(tif) &&  | 
536  | 0  |         ((tif->tif_flags & TIFF_NOREADRAW) == 0))  | 
537  | 0  |     { | 
538  | 0  |         if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize)  | 
539  | 0  |             return ((tmsize_t)(-1));  | 
540  |  |  | 
541  | 0  |         if (!isFillOrder(tif, td->td_fillorder) &&  | 
542  | 0  |             (tif->tif_flags & TIFF_NOBITREV) == 0)  | 
543  | 0  |             TIFFReverseBits(buf, stripsize);  | 
544  |  | 
  | 
545  | 0  |         (*tif->tif_postdecode)(tif, buf, stripsize);  | 
546  | 0  |         return (stripsize);  | 
547  | 0  |     }  | 
548  |  |  | 
549  | 0  |     if ((size != (tmsize_t)(-1)) && (size < stripsize))  | 
550  | 0  |         stripsize = size;  | 
551  | 0  |     if (!TIFFFillStrip(tif, strip))  | 
552  | 0  |         return ((tmsize_t)(-1));  | 
553  | 0  |     if ((*tif->tif_decodestrip)(tif, buf, stripsize, plane) <= 0)  | 
554  | 0  |         return ((tmsize_t)(-1));  | 
555  | 0  |     (*tif->tif_postdecode)(tif, buf, stripsize);  | 
556  | 0  |     return (stripsize);  | 
557  | 0  | }  | 
558  |  |  | 
559  |  | /* Variant of TIFFReadEncodedStrip() that does  | 
560  |  |  * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after  | 
561  |  |  * TIFFFillStrip() has succeeded. This avoid excessive memory allocation in case  | 
562  |  |  * of truncated file.  | 
563  |  |  * * calls regular TIFFReadEncodedStrip() if *buf != NULL  | 
564  |  |  */  | 
565  |  | tmsize_t _TIFFReadEncodedStripAndAllocBuffer(TIFF *tif, uint32_t strip,  | 
566  |  |                                              void **buf,  | 
567  |  |                                              tmsize_t bufsizetoalloc,  | 
568  |  |                                              tmsize_t size_to_read)  | 
569  | 0  | { | 
570  | 0  |     tmsize_t this_stripsize;  | 
571  | 0  |     uint16_t plane;  | 
572  |  | 
  | 
573  | 0  |     if (*buf != NULL)  | 
574  | 0  |     { | 
575  | 0  |         return TIFFReadEncodedStrip(tif, strip, *buf, size_to_read);  | 
576  | 0  |     }  | 
577  |  |  | 
578  | 0  |     this_stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);  | 
579  | 0  |     if (this_stripsize == ((tmsize_t)(-1)))  | 
580  | 0  |         return ((tmsize_t)(-1));  | 
581  |  |  | 
582  | 0  |     if ((size_to_read != (tmsize_t)(-1)) && (size_to_read < this_stripsize))  | 
583  | 0  |         this_stripsize = size_to_read;  | 
584  | 0  |     if (!TIFFFillStrip(tif, strip))  | 
585  | 0  |         return ((tmsize_t)(-1));  | 
586  |  |  | 
587  | 0  |     *buf = _TIFFmallocExt(tif, bufsizetoalloc);  | 
588  | 0  |     if (*buf == NULL)  | 
589  | 0  |     { | 
590  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif), "No space for strip buffer");  | 
591  | 0  |         return ((tmsize_t)(-1));  | 
592  | 0  |     }  | 
593  | 0  |     _TIFFmemset(*buf, 0, bufsizetoalloc);  | 
594  |  | 
  | 
595  | 0  |     if ((*tif->tif_decodestrip)(tif, *buf, this_stripsize, plane) <= 0)  | 
596  | 0  |         return ((tmsize_t)(-1));  | 
597  | 0  |     (*tif->tif_postdecode)(tif, *buf, this_stripsize);  | 
598  | 0  |     return (this_stripsize);  | 
599  | 0  | }  | 
600  |  |  | 
601  |  | static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf,  | 
602  |  |                                   tmsize_t size, const char *module)  | 
603  | 0  | { | 
604  | 0  |     assert((tif->tif_flags & TIFF_NOREADRAW) == 0);  | 
605  | 0  |     if (!isMapped(tif))  | 
606  | 0  |     { | 
607  | 0  |         tmsize_t cc;  | 
608  |  | 
  | 
609  | 0  |         if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip)))  | 
610  | 0  |         { | 
611  | 0  |             TIFFErrorExtR(tif, module,  | 
612  | 0  |                           "Seek error at scanline %" PRIu32 ", strip %" PRIu32,  | 
613  | 0  |                           tif->tif_row, strip);  | 
614  | 0  |             return ((tmsize_t)(-1));  | 
615  | 0  |         }  | 
616  | 0  |         cc = TIFFReadFile(tif, buf, size);  | 
617  | 0  |         if (cc != size)  | 
618  | 0  |         { | 
619  | 0  |             TIFFErrorExtR(tif, module,  | 
620  | 0  |                           "Read error at scanline %" PRIu32  | 
621  | 0  |                           "; got %" TIFF_SSIZE_FORMAT  | 
622  | 0  |                           " bytes, expected %" TIFF_SSIZE_FORMAT,  | 
623  | 0  |                           tif->tif_row, cc, size);  | 
624  | 0  |             return ((tmsize_t)(-1));  | 
625  | 0  |         }  | 
626  | 0  |     }  | 
627  | 0  |     else  | 
628  | 0  |     { | 
629  | 0  |         tmsize_t ma = 0;  | 
630  | 0  |         tmsize_t n;  | 
631  | 0  |         if ((TIFFGetStrileOffset(tif, strip) > (uint64_t)TIFF_TMSIZE_T_MAX) ||  | 
632  | 0  |             ((ma = (tmsize_t)TIFFGetStrileOffset(tif, strip)) > tif->tif_size))  | 
633  | 0  |         { | 
634  | 0  |             n = 0;  | 
635  | 0  |         }  | 
636  | 0  |         else if (ma > TIFF_TMSIZE_T_MAX - size)  | 
637  | 0  |         { | 
638  | 0  |             n = 0;  | 
639  | 0  |         }  | 
640  | 0  |         else  | 
641  | 0  |         { | 
642  | 0  |             tmsize_t mb = ma + size;  | 
643  | 0  |             if (mb > tif->tif_size)  | 
644  | 0  |                 n = tif->tif_size - ma;  | 
645  | 0  |             else  | 
646  | 0  |                 n = size;  | 
647  | 0  |         }  | 
648  | 0  |         if (n != size)  | 
649  | 0  |         { | 
650  | 0  |             TIFFErrorExtR(tif, module,  | 
651  | 0  |                           "Read error at scanline %" PRIu32 ", strip %" PRIu32  | 
652  | 0  |                           "; got %" TIFF_SSIZE_FORMAT  | 
653  | 0  |                           " bytes, expected %" TIFF_SSIZE_FORMAT,  | 
654  | 0  |                           tif->tif_row, strip, n, size);  | 
655  | 0  |             return ((tmsize_t)(-1));  | 
656  | 0  |         }  | 
657  | 0  |         _TIFFmemcpy(buf, tif->tif_base + ma, size);  | 
658  | 0  |     }  | 
659  | 0  |     return (size);  | 
660  | 0  | }  | 
661  |  |  | 
662  |  | static tmsize_t TIFFReadRawStripOrTile2(TIFF *tif, uint32_t strip_or_tile,  | 
663  |  |                                         int is_strip, tmsize_t size,  | 
664  |  |                                         const char *module)  | 
665  | 0  | { | 
666  | 0  |     assert(!isMapped(tif));  | 
667  | 0  |     assert((tif->tif_flags & TIFF_NOREADRAW) == 0);  | 
668  |  | 
  | 
669  | 0  |     if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip_or_tile)))  | 
670  | 0  |     { | 
671  | 0  |         if (is_strip)  | 
672  | 0  |         { | 
673  | 0  |             TIFFErrorExtR(tif, module,  | 
674  | 0  |                           "Seek error at scanline %" PRIu32 ", strip %" PRIu32,  | 
675  | 0  |                           tif->tif_row, strip_or_tile);  | 
676  | 0  |         }  | 
677  | 0  |         else  | 
678  | 0  |         { | 
679  | 0  |             TIFFErrorExtR(tif, module,  | 
680  | 0  |                           "Seek error at row %" PRIu32 ", col %" PRIu32  | 
681  | 0  |                           ", tile %" PRIu32,  | 
682  | 0  |                           tif->tif_row, tif->tif_col, strip_or_tile);  | 
683  | 0  |         }  | 
684  | 0  |         return ((tmsize_t)(-1));  | 
685  | 0  |     }  | 
686  |  |  | 
687  | 0  |     if (!TIFFReadAndRealloc(tif, size, 0, is_strip, strip_or_tile, module))  | 
688  | 0  |     { | 
689  | 0  |         return ((tmsize_t)(-1));  | 
690  | 0  |     }  | 
691  |  |  | 
692  | 0  |     return (size);  | 
693  | 0  | }  | 
694  |  |  | 
695  |  | /*  | 
696  |  |  * Read a strip of data from the file.  | 
697  |  |  */  | 
698  |  | tmsize_t TIFFReadRawStrip(TIFF *tif, uint32_t strip, void *buf, tmsize_t size)  | 
699  | 0  | { | 
700  | 0  |     static const char module[] = "TIFFReadRawStrip";  | 
701  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
702  | 0  |     uint64_t bytecount64;  | 
703  | 0  |     tmsize_t bytecountm;  | 
704  |  | 
  | 
705  | 0  |     if (!TIFFCheckRead(tif, 0))  | 
706  | 0  |         return ((tmsize_t)(-1));  | 
707  | 0  |     if (strip >= td->td_nstrips)  | 
708  | 0  |     { | 
709  | 0  |         TIFFErrorExtR(tif, module,  | 
710  | 0  |                       "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,  | 
711  | 0  |                       td->td_nstrips);  | 
712  | 0  |         return ((tmsize_t)(-1));  | 
713  | 0  |     }  | 
714  | 0  |     if (tif->tif_flags & TIFF_NOREADRAW)  | 
715  | 0  |     { | 
716  | 0  |         TIFFErrorExtR(tif, module,  | 
717  | 0  |                       "Compression scheme does not support access to raw "  | 
718  | 0  |                       "uncompressed data");  | 
719  | 0  |         return ((tmsize_t)(-1));  | 
720  | 0  |     }  | 
721  | 0  |     bytecount64 = TIFFGetStrileByteCount(tif, strip);  | 
722  | 0  |     if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)  | 
723  | 0  |         bytecountm = size;  | 
724  | 0  |     else  | 
725  | 0  |         bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);  | 
726  | 0  |     if (bytecountm == 0)  | 
727  | 0  |     { | 
728  | 0  |         return ((tmsize_t)(-1));  | 
729  | 0  |     }  | 
730  | 0  |     return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));  | 
731  | 0  | }  | 
732  |  |  | 
733  |  | TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW  | 
734  | 0  | static uint64_t NoSanitizeSubUInt64(uint64_t a, uint64_t b) { return a - b; } | 
735  |  |  | 
736  |  | /*  | 
737  |  |  * Read the specified strip and setup for decoding. The data buffer is  | 
738  |  |  * expanded, as necessary, to hold the strip's data.  | 
739  |  |  */  | 
740  |  | int TIFFFillStrip(TIFF *tif, uint32_t strip)  | 
741  | 0  | { | 
742  | 0  |     static const char module[] = "TIFFFillStrip";  | 
743  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
744  |  | 
  | 
745  | 0  |     if ((tif->tif_flags & TIFF_NOREADRAW) == 0)  | 
746  | 0  |     { | 
747  | 0  |         uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);  | 
748  | 0  |         if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)  | 
749  | 0  |         { | 
750  | 0  |             TIFFErrorExtR(tif, module,  | 
751  | 0  |                           "Invalid strip byte count %" PRIu64  | 
752  | 0  |                           ", strip %" PRIu32,  | 
753  | 0  |                           bytecount, strip);  | 
754  | 0  |             return (0);  | 
755  | 0  |         }  | 
756  |  |  | 
757  |  |         /* To avoid excessive memory allocations: */  | 
758  |  |         /* Byte count should normally not be larger than a number of */  | 
759  |  |         /* times the uncompressed size plus some margin */  | 
760  | 0  |         if (bytecount > 1024 * 1024)  | 
761  | 0  |         { | 
762  |  |             /* 10 and 4096 are just values that could be adjusted. */  | 
763  |  |             /* Hopefully they are safe enough for all codecs */  | 
764  | 0  |             tmsize_t stripsize = TIFFStripSize(tif);  | 
765  | 0  |             if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)  | 
766  | 0  |             { | 
767  | 0  |                 uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;  | 
768  | 0  |                 TIFFErrorExtR(tif, module,  | 
769  | 0  |                               "Too large strip byte count %" PRIu64  | 
770  | 0  |                               ", strip %" PRIu32 ". Limiting to %" PRIu64,  | 
771  | 0  |                               bytecount, strip, newbytecount);  | 
772  | 0  |                 bytecount = newbytecount;  | 
773  | 0  |             }  | 
774  | 0  |         }  | 
775  |  | 
  | 
776  | 0  |         if (isMapped(tif))  | 
777  | 0  |         { | 
778  |  |             /*  | 
779  |  |              * We must check for overflow, potentially causing  | 
780  |  |              * an OOB read. Instead of simple  | 
781  |  |              *  | 
782  |  |              *  TIFFGetStrileOffset(tif, strip)+bytecount > tif->tif_size  | 
783  |  |              *  | 
784  |  |              * comparison (which can overflow) we do the following  | 
785  |  |              * two comparisons:  | 
786  |  |              */  | 
787  | 0  |             if (bytecount > (uint64_t)tif->tif_size ||  | 
788  | 0  |                 TIFFGetStrileOffset(tif, strip) >  | 
789  | 0  |                     (uint64_t)tif->tif_size - bytecount)  | 
790  | 0  |             { | 
791  |  |                 /*  | 
792  |  |                  * This error message might seem strange, but  | 
793  |  |                  * it's what would happen if a read were done  | 
794  |  |                  * instead.  | 
795  |  |                  */  | 
796  | 0  |                 TIFFErrorExtR(  | 
797  | 0  |                     tif, module,  | 
798  |  | 
  | 
799  | 0  |                     "Read error on strip %" PRIu32 "; "  | 
800  | 0  |                     "got %" PRIu64 " bytes, expected %" PRIu64,  | 
801  | 0  |                     strip,  | 
802  | 0  |                     NoSanitizeSubUInt64(tif->tif_size,  | 
803  | 0  |                                         TIFFGetStrileOffset(tif, strip)),  | 
804  | 0  |                     bytecount);  | 
805  | 0  |                 tif->tif_curstrip = NOSTRIP;  | 
806  | 0  |                 return (0);  | 
807  | 0  |             }  | 
808  | 0  |         }  | 
809  |  |  | 
810  | 0  |         if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||  | 
811  | 0  |                               (tif->tif_flags & TIFF_NOBITREV)))  | 
812  | 0  |         { | 
813  |  |             /*  | 
814  |  |              * The image is mapped into memory and we either don't  | 
815  |  |              * need to flip bits or the compression routine is  | 
816  |  |              * going to handle this operation itself.  In this  | 
817  |  |              * case, avoid copying the raw data and instead just  | 
818  |  |              * reference the data from the memory mapped file  | 
819  |  |              * image.  This assumes that the decompression  | 
820  |  |              * routines do not modify the contents of the raw data  | 
821  |  |              * buffer (if they try to, the application will get a  | 
822  |  |              * fault since the file is mapped read-only).  | 
823  |  |              */  | 
824  | 0  |             if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)  | 
825  | 0  |             { | 
826  | 0  |                 _TIFFfreeExt(tif, tif->tif_rawdata);  | 
827  | 0  |                 tif->tif_rawdata = NULL;  | 
828  | 0  |                 tif->tif_rawdatasize = 0;  | 
829  | 0  |             }  | 
830  | 0  |             tif->tif_flags &= ~TIFF_MYBUFFER;  | 
831  | 0  |             tif->tif_rawdatasize = (tmsize_t)bytecount;  | 
832  | 0  |             tif->tif_rawdata =  | 
833  | 0  |                 tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, strip);  | 
834  | 0  |             tif->tif_rawdataoff = 0;  | 
835  | 0  |             tif->tif_rawdataloaded = (tmsize_t)bytecount;  | 
836  |  |  | 
837  |  |             /*  | 
838  |  |              * When we have tif_rawdata reference directly into the memory  | 
839  |  |              * mapped file we need to be pretty careful about how we use the  | 
840  |  |              * rawdata.  It is not a general purpose working buffer as it  | 
841  |  |              * normally otherwise is.  So we keep track of this fact to avoid  | 
842  |  |              * using it improperly.  | 
843  |  |              */  | 
844  | 0  |             tif->tif_flags |= TIFF_BUFFERMMAP;  | 
845  | 0  |         }  | 
846  | 0  |         else  | 
847  | 0  |         { | 
848  |  |             /*  | 
849  |  |              * Expand raw data buffer, if needed, to hold data  | 
850  |  |              * strip coming from file (perhaps should set upper  | 
851  |  |              * bound on the size of a buffer we'll use?).  | 
852  |  |              */  | 
853  | 0  |             tmsize_t bytecountm;  | 
854  | 0  |             bytecountm = (tmsize_t)bytecount;  | 
855  | 0  |             if ((uint64_t)bytecountm != bytecount)  | 
856  | 0  |             { | 
857  | 0  |                 TIFFErrorExtR(tif, module, "Integer overflow");  | 
858  | 0  |                 return (0);  | 
859  | 0  |             }  | 
860  | 0  |             if (bytecountm > tif->tif_rawdatasize)  | 
861  | 0  |             { | 
862  | 0  |                 tif->tif_curstrip = NOSTRIP;  | 
863  | 0  |                 if ((tif->tif_flags & TIFF_MYBUFFER) == 0)  | 
864  | 0  |                 { | 
865  | 0  |                     TIFFErrorExtR(  | 
866  | 0  |                         tif, module,  | 
867  | 0  |                         "Data buffer too small to hold strip %" PRIu32, strip);  | 
868  | 0  |                     return (0);  | 
869  | 0  |                 }  | 
870  | 0  |             }  | 
871  | 0  |             if (tif->tif_flags & TIFF_BUFFERMMAP)  | 
872  | 0  |             { | 
873  | 0  |                 tif->tif_curstrip = NOSTRIP;  | 
874  | 0  |                 tif->tif_rawdata = NULL;  | 
875  | 0  |                 tif->tif_rawdatasize = 0;  | 
876  | 0  |                 tif->tif_flags &= ~TIFF_BUFFERMMAP;  | 
877  | 0  |             }  | 
878  |  | 
  | 
879  | 0  |             if (isMapped(tif))  | 
880  | 0  |             { | 
881  | 0  |                 if (bytecountm > tif->tif_rawdatasize &&  | 
882  | 0  |                     !TIFFReadBufferSetup(tif, 0, bytecountm))  | 
883  | 0  |                 { | 
884  | 0  |                     return (0);  | 
885  | 0  |                 }  | 
886  | 0  |                 if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata, bytecountm,  | 
887  | 0  |                                       module) != bytecountm)  | 
888  | 0  |                 { | 
889  | 0  |                     return (0);  | 
890  | 0  |                 }  | 
891  | 0  |             }  | 
892  | 0  |             else  | 
893  | 0  |             { | 
894  | 0  |                 if (TIFFReadRawStripOrTile2(tif, strip, 1, bytecountm,  | 
895  | 0  |                                             module) != bytecountm)  | 
896  | 0  |                 { | 
897  | 0  |                     return (0);  | 
898  | 0  |                 }  | 
899  | 0  |             }  | 
900  |  |  | 
901  | 0  |             tif->tif_rawdataoff = 0;  | 
902  | 0  |             tif->tif_rawdataloaded = bytecountm;  | 
903  |  | 
  | 
904  | 0  |             if (!isFillOrder(tif, td->td_fillorder) &&  | 
905  | 0  |                 (tif->tif_flags & TIFF_NOBITREV) == 0)  | 
906  | 0  |                 TIFFReverseBits(tif->tif_rawdata, bytecountm);  | 
907  | 0  |         }  | 
908  | 0  |     }  | 
909  | 0  |     return (TIFFStartStrip(tif, strip));  | 
910  | 0  | }  | 
911  |  |  | 
912  |  | /*  | 
913  |  |  * Tile-oriented Read Support  | 
914  |  |  * Contributed by Nancy Cam (Silicon Graphics).  | 
915  |  |  */  | 
916  |  |  | 
917  |  | /*  | 
918  |  |  * Read and decompress a tile of data.  The  | 
919  |  |  * tile is selected by the (x,y,z,s) coordinates.  | 
920  |  |  */  | 
921  |  | tmsize_t TIFFReadTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z,  | 
922  |  |                       uint16_t s)  | 
923  | 0  | { | 
924  | 0  |     if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))  | 
925  | 0  |         return ((tmsize_t)(-1));  | 
926  | 0  |     return (TIFFReadEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf,  | 
927  | 0  |                                 (tmsize_t)(-1)));  | 
928  | 0  | }  | 
929  |  |  | 
930  |  | /*  | 
931  |  |  * Read a tile of data and decompress the specified  | 
932  |  |  * amount into the user-supplied buffer.  | 
933  |  |  */  | 
934  |  | tmsize_t TIFFReadEncodedTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)  | 
935  | 0  | { | 
936  | 0  |     static const char module[] = "TIFFReadEncodedTile";  | 
937  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
938  | 0  |     tmsize_t tilesize = tif->tif_tilesize;  | 
939  |  | 
  | 
940  | 0  |     if (!TIFFCheckRead(tif, 1))  | 
941  | 0  |         return ((tmsize_t)(-1));  | 
942  | 0  |     if (tile >= td->td_nstrips)  | 
943  | 0  |     { | 
944  | 0  |         TIFFErrorExtR(tif, module,  | 
945  | 0  |                       "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,  | 
946  | 0  |                       td->td_nstrips);  | 
947  | 0  |         return ((tmsize_t)(-1));  | 
948  | 0  |     }  | 
949  |  |  | 
950  |  |     /* shortcut to avoid an extra memcpy() */  | 
951  | 0  |     if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&  | 
952  | 0  |         size >= tilesize && !isMapped(tif) &&  | 
953  | 0  |         ((tif->tif_flags & TIFF_NOREADRAW) == 0))  | 
954  | 0  |     { | 
955  | 0  |         if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize)  | 
956  | 0  |             return ((tmsize_t)(-1));  | 
957  |  |  | 
958  | 0  |         if (!isFillOrder(tif, td->td_fillorder) &&  | 
959  | 0  |             (tif->tif_flags & TIFF_NOBITREV) == 0)  | 
960  | 0  |             TIFFReverseBits(buf, tilesize);  | 
961  |  | 
  | 
962  | 0  |         (*tif->tif_postdecode)(tif, buf, tilesize);  | 
963  | 0  |         return (tilesize);  | 
964  | 0  |     }  | 
965  |  |  | 
966  | 0  |     if (size == (tmsize_t)(-1))  | 
967  | 0  |         size = tilesize;  | 
968  | 0  |     else if (size > tilesize)  | 
969  | 0  |         size = tilesize;  | 
970  | 0  |     if (TIFFFillTile(tif, tile) &&  | 
971  | 0  |         (*tif->tif_decodetile)(tif, (uint8_t *)buf, size,  | 
972  | 0  |                                (uint16_t)(tile / td->td_stripsperimage)))  | 
973  | 0  |     { | 
974  | 0  |         (*tif->tif_postdecode)(tif, (uint8_t *)buf, size);  | 
975  | 0  |         return (size);  | 
976  | 0  |     }  | 
977  | 0  |     else  | 
978  | 0  |         return ((tmsize_t)(-1));  | 
979  | 0  | }  | 
980  |  |  | 
981  |  | /* Variant of TIFFReadTile() that does  | 
982  |  |  * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after  | 
983  |  |  * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case  | 
984  |  |  * of truncated file.  | 
985  |  |  * * calls regular TIFFReadEncodedTile() if *buf != NULL  | 
986  |  |  */  | 
987  |  | tmsize_t _TIFFReadTileAndAllocBuffer(TIFF *tif, void **buf,  | 
988  |  |                                      tmsize_t bufsizetoalloc, uint32_t x,  | 
989  |  |                                      uint32_t y, uint32_t z, uint16_t s)  | 
990  | 0  | { | 
991  | 0  |     if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))  | 
992  | 0  |         return ((tmsize_t)(-1));  | 
993  | 0  |     return (_TIFFReadEncodedTileAndAllocBuffer(  | 
994  | 0  |         tif, TIFFComputeTile(tif, x, y, z, s), buf, bufsizetoalloc,  | 
995  | 0  |         (tmsize_t)(-1)));  | 
996  | 0  | }  | 
997  |  |  | 
998  |  | /* Variant of TIFFReadEncodedTile() that does  | 
999  |  |  * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after  | 
1000  |  |  * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case  | 
1001  |  |  * of truncated file.  | 
1002  |  |  * * calls regular TIFFReadEncodedTile() if *buf != NULL  | 
1003  |  |  */  | 
1004  |  | tmsize_t _TIFFReadEncodedTileAndAllocBuffer(TIFF *tif, uint32_t tile,  | 
1005  |  |                                             void **buf, tmsize_t bufsizetoalloc,  | 
1006  |  |                                             tmsize_t size_to_read)  | 
1007  | 0  | { | 
1008  | 0  |     static const char module[] = "_TIFFReadEncodedTileAndAllocBuffer";  | 
1009  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
1010  | 0  |     tmsize_t tilesize = tif->tif_tilesize;  | 
1011  |  | 
  | 
1012  | 0  |     if (*buf != NULL)  | 
1013  | 0  |     { | 
1014  | 0  |         return TIFFReadEncodedTile(tif, tile, *buf, size_to_read);  | 
1015  | 0  |     }  | 
1016  |  |  | 
1017  | 0  |     if (!TIFFCheckRead(tif, 1))  | 
1018  | 0  |         return ((tmsize_t)(-1));  | 
1019  | 0  |     if (tile >= td->td_nstrips)  | 
1020  | 0  |     { | 
1021  | 0  |         TIFFErrorExtR(tif, module,  | 
1022  | 0  |                       "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,  | 
1023  | 0  |                       td->td_nstrips);  | 
1024  | 0  |         return ((tmsize_t)(-1));  | 
1025  | 0  |     }  | 
1026  |  |  | 
1027  | 0  |     if (!TIFFFillTile(tif, tile))  | 
1028  | 0  |         return ((tmsize_t)(-1));  | 
1029  |  |  | 
1030  |  |     /* Sanity checks to avoid excessive memory allocation */  | 
1031  |  |     /* Cf https://gitlab.com/libtiff/libtiff/-/issues/479 */  | 
1032  | 0  |     if (td->td_compression == COMPRESSION_NONE)  | 
1033  | 0  |     { | 
1034  | 0  |         if (tif->tif_rawdatasize != tilesize)  | 
1035  | 0  |         { | 
1036  | 0  |             TIFFErrorExtR(tif, TIFFFileName(tif),  | 
1037  | 0  |                           "Invalid tile byte count for tile %u. "  | 
1038  | 0  |                           "Expected %" PRIu64 ", got %" PRIu64,  | 
1039  | 0  |                           tile, (uint64_t)tilesize,  | 
1040  | 0  |                           (uint64_t)tif->tif_rawdatasize);  | 
1041  | 0  |             return ((tmsize_t)(-1));  | 
1042  | 0  |         }  | 
1043  | 0  |     }  | 
1044  | 0  |     else  | 
1045  | 0  |     { | 
1046  |  |         /* Max compression ratio experimentally determined. Might be fragile...  | 
1047  |  |          * Only apply this heuristics to situations where the memory allocation  | 
1048  |  |          * would be big, to avoid breaking nominal use cases.  | 
1049  |  |          */  | 
1050  | 0  |         const int maxCompressionRatio =  | 
1051  | 0  |             td->td_compression == COMPRESSION_ZSTD ? 33000  | 
1052  | 0  |             : td->td_compression == COMPRESSION_JXL  | 
1053  | 0  |                 ?  | 
1054  |  |                 /* Evaluated on a 8000x8000 tile */  | 
1055  | 0  |                 25000 * (td->td_planarconfig == PLANARCONFIG_CONTIG  | 
1056  | 0  |                              ? td->td_samplesperpixel  | 
1057  | 0  |                              : 1)  | 
1058  | 0  |                 : td->td_compression == COMPRESSION_LZMA ? 7000 : 1000;  | 
1059  | 0  |         if (bufsizetoalloc > 100 * 1000 * 1000 &&  | 
1060  | 0  |             tif->tif_rawdatasize < tilesize / maxCompressionRatio)  | 
1061  | 0  |         { | 
1062  | 0  |             TIFFErrorExtR(tif, TIFFFileName(tif),  | 
1063  | 0  |                           "Likely invalid tile byte count for tile %u. "  | 
1064  | 0  |                           "Uncompressed tile size is %" PRIu64 ", "  | 
1065  | 0  |                           "compressed one is %" PRIu64,  | 
1066  | 0  |                           tile, (uint64_t)tilesize,  | 
1067  | 0  |                           (uint64_t)tif->tif_rawdatasize);  | 
1068  | 0  |             return ((tmsize_t)(-1));  | 
1069  | 0  |         }  | 
1070  | 0  |     }  | 
1071  |  |  | 
1072  | 0  |     *buf = _TIFFmallocExt(tif, bufsizetoalloc);  | 
1073  | 0  |     if (*buf == NULL)  | 
1074  | 0  |     { | 
1075  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer");  | 
1076  | 0  |         return ((tmsize_t)(-1));  | 
1077  | 0  |     }  | 
1078  | 0  |     _TIFFmemset(*buf, 0, bufsizetoalloc);  | 
1079  |  | 
  | 
1080  | 0  |     if (size_to_read == (tmsize_t)(-1))  | 
1081  | 0  |         size_to_read = tilesize;  | 
1082  | 0  |     else if (size_to_read > tilesize)  | 
1083  | 0  |         size_to_read = tilesize;  | 
1084  | 0  |     if ((*tif->tif_decodetile)(tif, (uint8_t *)*buf, size_to_read,  | 
1085  | 0  |                                (uint16_t)(tile / td->td_stripsperimage)))  | 
1086  | 0  |     { | 
1087  | 0  |         (*tif->tif_postdecode)(tif, (uint8_t *)*buf, size_to_read);  | 
1088  | 0  |         return (size_to_read);  | 
1089  | 0  |     }  | 
1090  | 0  |     else  | 
1091  | 0  |         return ((tmsize_t)(-1));  | 
1092  | 0  | }  | 
1093  |  |  | 
1094  |  | static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf,  | 
1095  |  |                                  tmsize_t size, const char *module)  | 
1096  | 0  | { | 
1097  | 0  |     assert((tif->tif_flags & TIFF_NOREADRAW) == 0);  | 
1098  | 0  |     if (!isMapped(tif))  | 
1099  | 0  |     { | 
1100  | 0  |         tmsize_t cc;  | 
1101  |  | 
  | 
1102  | 0  |         if (!SeekOK(tif, TIFFGetStrileOffset(tif, tile)))  | 
1103  | 0  |         { | 
1104  | 0  |             TIFFErrorExtR(tif, module,  | 
1105  | 0  |                           "Seek error at row %" PRIu32 ", col %" PRIu32  | 
1106  | 0  |                           ", tile %" PRIu32,  | 
1107  | 0  |                           tif->tif_row, tif->tif_col, tile);  | 
1108  | 0  |             return ((tmsize_t)(-1));  | 
1109  | 0  |         }  | 
1110  | 0  |         cc = TIFFReadFile(tif, buf, size);  | 
1111  | 0  |         if (cc != size)  | 
1112  | 0  |         { | 
1113  | 0  |             TIFFErrorExtR(tif, module,  | 
1114  | 0  |                           "Read error at row %" PRIu32 ", col %" PRIu32  | 
1115  | 0  |                           "; got %" TIFF_SSIZE_FORMAT  | 
1116  | 0  |                           " bytes, expected %" TIFF_SSIZE_FORMAT,  | 
1117  | 0  |                           tif->tif_row, tif->tif_col, cc, size);  | 
1118  | 0  |             return ((tmsize_t)(-1));  | 
1119  | 0  |         }  | 
1120  | 0  |     }  | 
1121  | 0  |     else  | 
1122  | 0  |     { | 
1123  | 0  |         tmsize_t ma, mb;  | 
1124  | 0  |         tmsize_t n;  | 
1125  | 0  |         ma = (tmsize_t)TIFFGetStrileOffset(tif, tile);  | 
1126  | 0  |         mb = ma + size;  | 
1127  | 0  |         if ((TIFFGetStrileOffset(tif, tile) > (uint64_t)TIFF_TMSIZE_T_MAX) ||  | 
1128  | 0  |             (ma > tif->tif_size))  | 
1129  | 0  |             n = 0;  | 
1130  | 0  |         else if ((mb < ma) || (mb < size) || (mb > tif->tif_size))  | 
1131  | 0  |             n = tif->tif_size - ma;  | 
1132  | 0  |         else  | 
1133  | 0  |             n = size;  | 
1134  | 0  |         if (n != size)  | 
1135  | 0  |         { | 
1136  | 0  |             TIFFErrorExtR(tif, module,  | 
1137  | 0  |                           "Read error at row %" PRIu32 ", col %" PRIu32  | 
1138  | 0  |                           ", tile %" PRIu32 "; got %" TIFF_SSIZE_FORMAT  | 
1139  | 0  |                           " bytes, expected %" TIFF_SSIZE_FORMAT,  | 
1140  | 0  |                           tif->tif_row, tif->tif_col, tile, n, size);  | 
1141  | 0  |             return ((tmsize_t)(-1));  | 
1142  | 0  |         }  | 
1143  | 0  |         _TIFFmemcpy(buf, tif->tif_base + ma, size);  | 
1144  | 0  |     }  | 
1145  | 0  |     return (size);  | 
1146  | 0  | }  | 
1147  |  |  | 
1148  |  | /*  | 
1149  |  |  * Read a tile of data from the file.  | 
1150  |  |  */  | 
1151  |  | tmsize_t TIFFReadRawTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)  | 
1152  | 0  | { | 
1153  | 0  |     static const char module[] = "TIFFReadRawTile";  | 
1154  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
1155  | 0  |     uint64_t bytecount64;  | 
1156  | 0  |     tmsize_t bytecountm;  | 
1157  |  | 
  | 
1158  | 0  |     if (!TIFFCheckRead(tif, 1))  | 
1159  | 0  |         return ((tmsize_t)(-1));  | 
1160  | 0  |     if (tile >= td->td_nstrips)  | 
1161  | 0  |     { | 
1162  | 0  |         TIFFErrorExtR(tif, module,  | 
1163  | 0  |                       "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,  | 
1164  | 0  |                       td->td_nstrips);  | 
1165  | 0  |         return ((tmsize_t)(-1));  | 
1166  | 0  |     }  | 
1167  | 0  |     if (tif->tif_flags & TIFF_NOREADRAW)  | 
1168  | 0  |     { | 
1169  | 0  |         TIFFErrorExtR(tif, module,  | 
1170  | 0  |                       "Compression scheme does not support access to raw "  | 
1171  | 0  |                       "uncompressed data");  | 
1172  | 0  |         return ((tmsize_t)(-1));  | 
1173  | 0  |     }  | 
1174  | 0  |     bytecount64 = TIFFGetStrileByteCount(tif, tile);  | 
1175  | 0  |     if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)  | 
1176  | 0  |         bytecountm = size;  | 
1177  | 0  |     else  | 
1178  | 0  |         bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);  | 
1179  | 0  |     if (bytecountm == 0)  | 
1180  | 0  |     { | 
1181  | 0  |         return ((tmsize_t)(-1));  | 
1182  | 0  |     }  | 
1183  | 0  |     return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));  | 
1184  | 0  | }  | 
1185  |  |  | 
1186  |  | /*  | 
1187  |  |  * Read the specified tile and setup for decoding. The data buffer is  | 
1188  |  |  * expanded, as necessary, to hold the tile's data.  | 
1189  |  |  */  | 
1190  |  | int TIFFFillTile(TIFF *tif, uint32_t tile)  | 
1191  | 0  | { | 
1192  | 0  |     static const char module[] = "TIFFFillTile";  | 
1193  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
1194  |  | 
  | 
1195  | 0  |     if ((tif->tif_flags & TIFF_NOREADRAW) == 0)  | 
1196  | 0  |     { | 
1197  | 0  |         uint64_t bytecount = TIFFGetStrileByteCount(tif, tile);  | 
1198  | 0  |         if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)  | 
1199  | 0  |         { | 
1200  | 0  |             TIFFErrorExtR(tif, module,  | 
1201  | 0  |                           "%" PRIu64 ": Invalid tile byte count, tile %" PRIu32,  | 
1202  | 0  |                           bytecount, tile);  | 
1203  | 0  |             return (0);  | 
1204  | 0  |         }  | 
1205  |  |  | 
1206  |  |         /* To avoid excessive memory allocations: */  | 
1207  |  |         /* Byte count should normally not be larger than a number of */  | 
1208  |  |         /* times the uncompressed size plus some margin */  | 
1209  | 0  |         if (bytecount > 1024 * 1024)  | 
1210  | 0  |         { | 
1211  |  |             /* 10 and 4096 are just values that could be adjusted. */  | 
1212  |  |             /* Hopefully they are safe enough for all codecs */  | 
1213  | 0  |             tmsize_t stripsize = TIFFTileSize(tif);  | 
1214  | 0  |             if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)  | 
1215  | 0  |             { | 
1216  | 0  |                 uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;  | 
1217  | 0  |                 TIFFErrorExtR(tif, module,  | 
1218  | 0  |                               "Too large tile byte count %" PRIu64  | 
1219  | 0  |                               ", tile %" PRIu32 ". Limiting to %" PRIu64,  | 
1220  | 0  |                               bytecount, tile, newbytecount);  | 
1221  | 0  |                 bytecount = newbytecount;  | 
1222  | 0  |             }  | 
1223  | 0  |         }  | 
1224  |  | 
  | 
1225  | 0  |         if (isMapped(tif))  | 
1226  | 0  |         { | 
1227  |  |             /*  | 
1228  |  |              * We must check for overflow, potentially causing  | 
1229  |  |              * an OOB read. Instead of simple  | 
1230  |  |              *  | 
1231  |  |              *  TIFFGetStrileOffset(tif, tile)+bytecount > tif->tif_size  | 
1232  |  |              *  | 
1233  |  |              * comparison (which can overflow) we do the following  | 
1234  |  |              * two comparisons:  | 
1235  |  |              */  | 
1236  | 0  |             if (bytecount > (uint64_t)tif->tif_size ||  | 
1237  | 0  |                 TIFFGetStrileOffset(tif, tile) >  | 
1238  | 0  |                     (uint64_t)tif->tif_size - bytecount)  | 
1239  | 0  |             { | 
1240  | 0  |                 tif->tif_curtile = NOTILE;  | 
1241  | 0  |                 return (0);  | 
1242  | 0  |             }  | 
1243  | 0  |         }  | 
1244  |  |  | 
1245  | 0  |         if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||  | 
1246  | 0  |                               (tif->tif_flags & TIFF_NOBITREV)))  | 
1247  | 0  |         { | 
1248  |  |             /*  | 
1249  |  |              * The image is mapped into memory and we either don't  | 
1250  |  |              * need to flip bits or the compression routine is  | 
1251  |  |              * going to handle this operation itself.  In this  | 
1252  |  |              * case, avoid copying the raw data and instead just  | 
1253  |  |              * reference the data from the memory mapped file  | 
1254  |  |              * image.  This assumes that the decompression  | 
1255  |  |              * routines do not modify the contents of the raw data  | 
1256  |  |              * buffer (if they try to, the application will get a  | 
1257  |  |              * fault since the file is mapped read-only).  | 
1258  |  |              */  | 
1259  | 0  |             if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)  | 
1260  | 0  |             { | 
1261  | 0  |                 _TIFFfreeExt(tif, tif->tif_rawdata);  | 
1262  | 0  |                 tif->tif_rawdata = NULL;  | 
1263  | 0  |                 tif->tif_rawdatasize = 0;  | 
1264  | 0  |             }  | 
1265  | 0  |             tif->tif_flags &= ~TIFF_MYBUFFER;  | 
1266  |  | 
  | 
1267  | 0  |             tif->tif_rawdatasize = (tmsize_t)bytecount;  | 
1268  | 0  |             tif->tif_rawdata =  | 
1269  | 0  |                 tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, tile);  | 
1270  | 0  |             tif->tif_rawdataoff = 0;  | 
1271  | 0  |             tif->tif_rawdataloaded = (tmsize_t)bytecount;  | 
1272  | 0  |             tif->tif_flags |= TIFF_BUFFERMMAP;  | 
1273  | 0  |         }  | 
1274  | 0  |         else  | 
1275  | 0  |         { | 
1276  |  |             /*  | 
1277  |  |              * Expand raw data buffer, if needed, to hold data  | 
1278  |  |              * tile coming from file (perhaps should set upper  | 
1279  |  |              * bound on the size of a buffer we'll use?).  | 
1280  |  |              */  | 
1281  | 0  |             tmsize_t bytecountm;  | 
1282  | 0  |             bytecountm = (tmsize_t)bytecount;  | 
1283  | 0  |             if ((uint64_t)bytecountm != bytecount)  | 
1284  | 0  |             { | 
1285  | 0  |                 TIFFErrorExtR(tif, module, "Integer overflow");  | 
1286  | 0  |                 return (0);  | 
1287  | 0  |             }  | 
1288  | 0  |             if (bytecountm > tif->tif_rawdatasize)  | 
1289  | 0  |             { | 
1290  | 0  |                 tif->tif_curtile = NOTILE;  | 
1291  | 0  |                 if ((tif->tif_flags & TIFF_MYBUFFER) == 0)  | 
1292  | 0  |                 { | 
1293  | 0  |                     TIFFErrorExtR(tif, module,  | 
1294  | 0  |                                   "Data buffer too small to hold tile %" PRIu32,  | 
1295  | 0  |                                   tile);  | 
1296  | 0  |                     return (0);  | 
1297  | 0  |                 }  | 
1298  | 0  |             }  | 
1299  | 0  |             if (tif->tif_flags & TIFF_BUFFERMMAP)  | 
1300  | 0  |             { | 
1301  | 0  |                 tif->tif_curtile = NOTILE;  | 
1302  | 0  |                 tif->tif_rawdata = NULL;  | 
1303  | 0  |                 tif->tif_rawdatasize = 0;  | 
1304  | 0  |                 tif->tif_flags &= ~TIFF_BUFFERMMAP;  | 
1305  | 0  |             }  | 
1306  |  | 
  | 
1307  | 0  |             if (isMapped(tif))  | 
1308  | 0  |             { | 
1309  | 0  |                 if (bytecountm > tif->tif_rawdatasize &&  | 
1310  | 0  |                     !TIFFReadBufferSetup(tif, 0, bytecountm))  | 
1311  | 0  |                 { | 
1312  | 0  |                     return (0);  | 
1313  | 0  |                 }  | 
1314  | 0  |                 if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata, bytecountm,  | 
1315  | 0  |                                      module) != bytecountm)  | 
1316  | 0  |                 { | 
1317  | 0  |                     return (0);  | 
1318  | 0  |                 }  | 
1319  | 0  |             }  | 
1320  | 0  |             else  | 
1321  | 0  |             { | 
1322  | 0  |                 if (TIFFReadRawStripOrTile2(tif, tile, 0, bytecountm, module) !=  | 
1323  | 0  |                     bytecountm)  | 
1324  | 0  |                 { | 
1325  | 0  |                     return (0);  | 
1326  | 0  |                 }  | 
1327  | 0  |             }  | 
1328  |  |  | 
1329  | 0  |             tif->tif_rawdataoff = 0;  | 
1330  | 0  |             tif->tif_rawdataloaded = bytecountm;  | 
1331  |  | 
  | 
1332  | 0  |             if (tif->tif_rawdata != NULL &&  | 
1333  | 0  |                 !isFillOrder(tif, td->td_fillorder) &&  | 
1334  | 0  |                 (tif->tif_flags & TIFF_NOBITREV) == 0)  | 
1335  | 0  |                 TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdataloaded);  | 
1336  | 0  |         }  | 
1337  | 0  |     }  | 
1338  | 0  |     return (TIFFStartTile(tif, tile));  | 
1339  | 0  | }  | 
1340  |  |  | 
1341  |  | /*  | 
1342  |  |  * Setup the raw data buffer in preparation for  | 
1343  |  |  * reading a strip of raw data.  If the buffer  | 
1344  |  |  * is specified as zero, then a buffer of appropriate  | 
1345  |  |  * size is allocated by the library.  Otherwise,  | 
1346  |  |  * the client must guarantee that the buffer is  | 
1347  |  |  * large enough to hold any individual strip of  | 
1348  |  |  * raw data.  | 
1349  |  |  */  | 
1350  |  | int TIFFReadBufferSetup(TIFF *tif, void *bp, tmsize_t size)  | 
1351  | 0  | { | 
1352  | 0  |     static const char module[] = "TIFFReadBufferSetup";  | 
1353  |  | 
  | 
1354  | 0  |     assert((tif->tif_flags & TIFF_NOREADRAW) == 0);  | 
1355  | 0  |     tif->tif_flags &= ~TIFF_BUFFERMMAP;  | 
1356  |  | 
  | 
1357  | 0  |     if (tif->tif_rawdata)  | 
1358  | 0  |     { | 
1359  | 0  |         if (tif->tif_flags & TIFF_MYBUFFER)  | 
1360  | 0  |             _TIFFfreeExt(tif, tif->tif_rawdata);  | 
1361  | 0  |         tif->tif_rawdata = NULL;  | 
1362  | 0  |         tif->tif_rawdatasize = 0;  | 
1363  | 0  |     }  | 
1364  | 0  |     if (bp)  | 
1365  | 0  |     { | 
1366  | 0  |         tif->tif_rawdatasize = size;  | 
1367  | 0  |         tif->tif_rawdata = (uint8_t *)bp;  | 
1368  | 0  |         tif->tif_flags &= ~TIFF_MYBUFFER;  | 
1369  | 0  |     }  | 
1370  | 0  |     else  | 
1371  | 0  |     { | 
1372  | 0  |         tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64_t)size, 1024);  | 
1373  | 0  |         if (tif->tif_rawdatasize == 0)  | 
1374  | 0  |         { | 
1375  | 0  |             TIFFErrorExtR(tif, module, "Invalid buffer size");  | 
1376  | 0  |             return (0);  | 
1377  | 0  |         }  | 
1378  |  |         /* Initialize to zero to avoid uninitialized buffers in case of */  | 
1379  |  |         /* short reads (http://bugzilla.maptools.org/show_bug.cgi?id=2651) */  | 
1380  | 0  |         tif->tif_rawdata =  | 
1381  | 0  |             (uint8_t *)_TIFFcallocExt(tif, 1, tif->tif_rawdatasize);  | 
1382  | 0  |         tif->tif_flags |= TIFF_MYBUFFER;  | 
1383  | 0  |     }  | 
1384  | 0  |     if (tif->tif_rawdata == NULL)  | 
1385  | 0  |     { | 
1386  | 0  |         TIFFErrorExtR(tif, module,  | 
1387  | 0  |                       "No space for data buffer at scanline %" PRIu32,  | 
1388  | 0  |                       tif->tif_row);  | 
1389  | 0  |         tif->tif_rawdatasize = 0;  | 
1390  | 0  |         return (0);  | 
1391  | 0  |     }  | 
1392  | 0  |     return (1);  | 
1393  | 0  | }  | 
1394  |  |  | 
1395  |  | /*  | 
1396  |  |  * Set state to appear as if a  | 
1397  |  |  * strip has just been read in.  | 
1398  |  |  */  | 
1399  |  | static int TIFFStartStrip(TIFF *tif, uint32_t strip)  | 
1400  | 0  | { | 
1401  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
1402  |  | 
  | 
1403  | 0  |     if ((tif->tif_flags & TIFF_CODERSETUP) == 0)  | 
1404  | 0  |     { | 
1405  | 0  |         if (!(*tif->tif_setupdecode)(tif))  | 
1406  | 0  |             return (0);  | 
1407  | 0  |         tif->tif_flags |= TIFF_CODERSETUP;  | 
1408  | 0  |     }  | 
1409  | 0  |     tif->tif_curstrip = strip;  | 
1410  | 0  |     tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;  | 
1411  | 0  |     tif->tif_flags &= ~TIFF_BUF4WRITE;  | 
1412  |  | 
  | 
1413  | 0  |     if (tif->tif_flags & TIFF_NOREADRAW)  | 
1414  | 0  |     { | 
1415  | 0  |         tif->tif_rawcp = NULL;  | 
1416  | 0  |         tif->tif_rawcc = 0;  | 
1417  | 0  |     }  | 
1418  | 0  |     else  | 
1419  | 0  |     { | 
1420  | 0  |         tif->tif_rawcp = tif->tif_rawdata;  | 
1421  | 0  |         if (tif->tif_rawdataloaded > 0)  | 
1422  | 0  |             tif->tif_rawcc = tif->tif_rawdataloaded;  | 
1423  | 0  |         else  | 
1424  | 0  |             tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, strip);  | 
1425  | 0  |     }  | 
1426  | 0  |     if ((*tif->tif_predecode)(tif, (uint16_t)(strip / td->td_stripsperimage)) ==  | 
1427  | 0  |         0)  | 
1428  | 0  |     { | 
1429  |  |         /* Needed for example for scanline access, if tif_predecode */  | 
1430  |  |         /* fails, and we try to read the same strip again. Without invalidating  | 
1431  |  |          */  | 
1432  |  |         /* tif_curstrip, we'd call tif_decoderow() on a possibly invalid */  | 
1433  |  |         /* codec state. */  | 
1434  | 0  |         tif->tif_curstrip = NOSTRIP;  | 
1435  | 0  |         return 0;  | 
1436  | 0  |     }  | 
1437  | 0  |     return 1;  | 
1438  | 0  | }  | 
1439  |  |  | 
1440  |  | /*  | 
1441  |  |  * Set state to appear as if a  | 
1442  |  |  * tile has just been read in.  | 
1443  |  |  */  | 
1444  |  | static int TIFFStartTile(TIFF *tif, uint32_t tile)  | 
1445  | 0  | { | 
1446  | 0  |     static const char module[] = "TIFFStartTile";  | 
1447  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
1448  | 0  |     uint32_t howmany32;  | 
1449  |  | 
  | 
1450  | 0  |     if ((tif->tif_flags & TIFF_CODERSETUP) == 0)  | 
1451  | 0  |     { | 
1452  | 0  |         if (!(*tif->tif_setupdecode)(tif))  | 
1453  | 0  |             return (0);  | 
1454  | 0  |         tif->tif_flags |= TIFF_CODERSETUP;  | 
1455  | 0  |     }  | 
1456  | 0  |     tif->tif_curtile = tile;  | 
1457  | 0  |     if (td->td_tilewidth == 0)  | 
1458  | 0  |     { | 
1459  | 0  |         TIFFErrorExtR(tif, module, "Zero tilewidth");  | 
1460  | 0  |         return 0;  | 
1461  | 0  |     }  | 
1462  | 0  |     howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);  | 
1463  | 0  |     if (howmany32 == 0)  | 
1464  | 0  |     { | 
1465  | 0  |         TIFFErrorExtR(tif, module, "Zero tiles");  | 
1466  | 0  |         return 0;  | 
1467  | 0  |     }  | 
1468  | 0  |     tif->tif_row = (tile % howmany32) * td->td_tilelength;  | 
1469  | 0  |     howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength);  | 
1470  | 0  |     if (howmany32 == 0)  | 
1471  | 0  |     { | 
1472  | 0  |         TIFFErrorExtR(tif, module, "Zero tiles");  | 
1473  | 0  |         return 0;  | 
1474  | 0  |     }  | 
1475  | 0  |     tif->tif_col = (tile % howmany32) * td->td_tilewidth;  | 
1476  | 0  |     tif->tif_flags &= ~TIFF_BUF4WRITE;  | 
1477  | 0  |     if (tif->tif_flags & TIFF_NOREADRAW)  | 
1478  | 0  |     { | 
1479  | 0  |         tif->tif_rawcp = NULL;  | 
1480  | 0  |         tif->tif_rawcc = 0;  | 
1481  | 0  |     }  | 
1482  | 0  |     else  | 
1483  | 0  |     { | 
1484  | 0  |         tif->tif_rawcp = tif->tif_rawdata;  | 
1485  | 0  |         if (tif->tif_rawdataloaded > 0)  | 
1486  | 0  |             tif->tif_rawcc = tif->tif_rawdataloaded;  | 
1487  | 0  |         else  | 
1488  | 0  |             tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, tile);  | 
1489  | 0  |     }  | 
1490  | 0  |     return (  | 
1491  | 0  |         (*tif->tif_predecode)(tif, (uint16_t)(tile / td->td_stripsperimage)));  | 
1492  | 0  | }  | 
1493  |  |  | 
1494  |  | static int TIFFCheckRead(TIFF *tif, int tiles)  | 
1495  | 0  | { | 
1496  | 0  |     if (tif->tif_mode == O_WRONLY)  | 
1497  | 0  |     { | 
1498  | 0  |         TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");  | 
1499  | 0  |         return (0);  | 
1500  | 0  |     }  | 
1501  | 0  |     if (tiles ^ isTiled(tif))  | 
1502  | 0  |     { | 
1503  | 0  |         TIFFErrorExtR(tif, tif->tif_name,  | 
1504  | 0  |                       tiles ? "Can not read tiles from a striped image"  | 
1505  | 0  |                             : "Can not read scanlines from a tiled image");  | 
1506  | 0  |         return (0);  | 
1507  | 0  |     }  | 
1508  | 0  |     return (1);  | 
1509  | 0  | }  | 
1510  |  |  | 
1511  |  | /* Use the provided input buffer (inbuf, insize) and decompress it into  | 
1512  |  |  * (outbuf, outsize).  | 
1513  |  |  * This function replaces the use of  | 
1514  |  |  * TIFFReadEncodedStrip()/TIFFReadEncodedTile() when the user can provide the  | 
1515  |  |  * buffer for the input data, for example when he wants to avoid libtiff to read  | 
1516  |  |  * the strile offset/count values from the [Strip|Tile][Offsets/ByteCounts]  | 
1517  |  |  * array. inbuf content must be writable (if bit reversal is needed) Returns 1  | 
1518  |  |  * in case of success, 0 otherwise.  | 
1519  |  |  */  | 
1520  |  | int TIFFReadFromUserBuffer(TIFF *tif, uint32_t strile, void *inbuf,  | 
1521  |  |                            tmsize_t insize, void *outbuf, tmsize_t outsize)  | 
1522  | 0  | { | 
1523  | 0  |     static const char module[] = "TIFFReadFromUserBuffer";  | 
1524  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
1525  | 0  |     int ret = 1;  | 
1526  | 0  |     uint32_t old_tif_flags = tif->tif_flags;  | 
1527  | 0  |     tmsize_t old_rawdatasize = tif->tif_rawdatasize;  | 
1528  | 0  |     void *old_rawdata = tif->tif_rawdata;  | 
1529  |  | 
  | 
1530  | 0  |     if (tif->tif_mode == O_WRONLY)  | 
1531  | 0  |     { | 
1532  | 0  |         TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");  | 
1533  | 0  |         return 0;  | 
1534  | 0  |     }  | 
1535  | 0  |     if (tif->tif_flags & TIFF_NOREADRAW)  | 
1536  | 0  |     { | 
1537  | 0  |         TIFFErrorExtR(tif, module,  | 
1538  | 0  |                       "Compression scheme does not support access to raw "  | 
1539  | 0  |                       "uncompressed data");  | 
1540  | 0  |         return 0;  | 
1541  | 0  |     }  | 
1542  |  |  | 
1543  | 0  |     tif->tif_flags &= ~TIFF_MYBUFFER;  | 
1544  | 0  |     tif->tif_flags |= TIFF_BUFFERMMAP;  | 
1545  | 0  |     tif->tif_rawdatasize = insize;  | 
1546  | 0  |     tif->tif_rawdata = inbuf;  | 
1547  | 0  |     tif->tif_rawdataoff = 0;  | 
1548  | 0  |     tif->tif_rawdataloaded = insize;  | 
1549  |  | 
  | 
1550  | 0  |     if (!isFillOrder(tif, td->td_fillorder) &&  | 
1551  | 0  |         (tif->tif_flags & TIFF_NOBITREV) == 0)  | 
1552  | 0  |     { | 
1553  | 0  |         TIFFReverseBits(inbuf, insize);  | 
1554  | 0  |     }  | 
1555  |  | 
  | 
1556  | 0  |     if (TIFFIsTiled(tif))  | 
1557  | 0  |     { | 
1558  | 0  |         if (!TIFFStartTile(tif, strile) ||  | 
1559  | 0  |             !(*tif->tif_decodetile)(tif, (uint8_t *)outbuf, outsize,  | 
1560  | 0  |                                     (uint16_t)(strile / td->td_stripsperimage)))  | 
1561  | 0  |         { | 
1562  | 0  |             ret = 0;  | 
1563  | 0  |         }  | 
1564  | 0  |     }  | 
1565  | 0  |     else  | 
1566  | 0  |     { | 
1567  | 0  |         uint32_t rowsperstrip = td->td_rowsperstrip;  | 
1568  | 0  |         uint32_t stripsperplane;  | 
1569  | 0  |         if (rowsperstrip > td->td_imagelength)  | 
1570  | 0  |             rowsperstrip = td->td_imagelength;  | 
1571  | 0  |         if (rowsperstrip == 0)  | 
1572  | 0  |         { | 
1573  | 0  |             TIFFErrorExtR(tif, module, "rowsperstrip is zero");  | 
1574  | 0  |             ret = 0;  | 
1575  | 0  |         }  | 
1576  | 0  |         else  | 
1577  | 0  |         { | 
1578  | 0  |             stripsperplane =  | 
1579  | 0  |                 TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);  | 
1580  | 0  |             if (!TIFFStartStrip(tif, strile) ||  | 
1581  | 0  |                 !(*tif->tif_decodestrip)(tif, (uint8_t *)outbuf, outsize,  | 
1582  | 0  |                                          (uint16_t)(strile / stripsperplane)))  | 
1583  | 0  |             { | 
1584  | 0  |                 ret = 0;  | 
1585  | 0  |             }  | 
1586  | 0  |         }  | 
1587  | 0  |     }  | 
1588  | 0  |     if (ret)  | 
1589  | 0  |     { | 
1590  | 0  |         (*tif->tif_postdecode)(tif, (uint8_t *)outbuf, outsize);  | 
1591  | 0  |     }  | 
1592  |  | 
  | 
1593  | 0  |     if (!isFillOrder(tif, td->td_fillorder) &&  | 
1594  | 0  |         (tif->tif_flags & TIFF_NOBITREV) == 0)  | 
1595  | 0  |     { | 
1596  | 0  |         TIFFReverseBits(inbuf, insize);  | 
1597  | 0  |     }  | 
1598  |  | 
  | 
1599  | 0  |     tif->tif_flags = (old_tif_flags & (TIFF_MYBUFFER | TIFF_BUFFERMMAP)) |  | 
1600  | 0  |                      (tif->tif_flags & ~(TIFF_MYBUFFER | TIFF_BUFFERMMAP));  | 
1601  | 0  |     tif->tif_rawdatasize = old_rawdatasize;  | 
1602  | 0  |     tif->tif_rawdata = old_rawdata;  | 
1603  | 0  |     tif->tif_rawdataoff = 0;  | 
1604  | 0  |     tif->tif_rawdataloaded = 0;  | 
1605  |  | 
  | 
1606  | 0  |     return ret;  | 
1607  | 0  | }  | 
1608  |  |  | 
1609  |  | void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)  | 
1610  | 0  | { | 
1611  | 0  |     (void)tif;  | 
1612  | 0  |     (void)buf;  | 
1613  | 0  |     (void)cc;  | 
1614  | 0  | }  | 
1615  |  |  | 
1616  |  | void _TIFFSwab16BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)  | 
1617  | 0  | { | 
1618  | 0  |     (void)tif;  | 
1619  | 0  |     assert((cc & 1) == 0);  | 
1620  | 0  |     TIFFSwabArrayOfShort((uint16_t *)buf, cc / 2);  | 
1621  | 0  | }  | 
1622  |  |  | 
1623  |  | void _TIFFSwab24BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)  | 
1624  | 0  | { | 
1625  | 0  |     (void)tif;  | 
1626  | 0  |     assert((cc % 3) == 0);  | 
1627  | 0  |     TIFFSwabArrayOfTriples((uint8_t *)buf, cc / 3);  | 
1628  | 0  | }  | 
1629  |  |  | 
1630  |  | void _TIFFSwab32BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)  | 
1631  | 0  | { | 
1632  | 0  |     (void)tif;  | 
1633  | 0  |     assert((cc & 3) == 0);  | 
1634  | 0  |     TIFFSwabArrayOfLong((uint32_t *)buf, cc / 4);  | 
1635  | 0  | }  | 
1636  |  |  | 
1637  |  | void _TIFFSwab64BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)  | 
1638  | 0  | { | 
1639  | 0  |     (void)tif;  | 
1640  | 0  |     assert((cc & 7) == 0);  | 
1641  | 0  |     TIFFSwabArrayOfDouble((double *)buf, cc / 8);  | 
1642  | 0  | }  |