Coverage Report

Created: 2025-11-15 08:43

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