Coverage Report

Created: 2025-06-16 07:00

/src/libtiff/libtiff/tif_read.c
Line
Count
Source (jump to first uncovered line)
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
43.4k
#define NOSTRIP ((uint32_t)(-1)) /* undefined state */
43
41.3k
#define NOTILE ((uint32_t)(-1))  /* undefined state */
44
45
2.51k
#define INITIAL_THRESHOLD (1024 * 1024)
46
0
#define THRESHOLD_MULTIPLIER 10
47
#define MAX_THRESHOLD                                                          \
48
2.51k
    (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER *      \
49
2.51k
     INITIAL_THRESHOLD)
50
51
2.64M
#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
2.51k
{
59
2.51k
#if SIZEOF_SIZE_T == 8
60
2.51k
    tmsize_t threshold = INITIAL_THRESHOLD;
61
2.51k
#endif
62
2.51k
    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
5.03k
    while (already_read < size)
86
2.51k
    {
87
2.51k
        tmsize_t bytes_read;
88
2.51k
        tmsize_t to_read = size - already_read;
89
2.51k
#if SIZEOF_SIZE_T == 8
90
2.51k
        if (to_read >= threshold && threshold < MAX_THRESHOLD &&
91
2.51k
            already_read + to_read + rawdata_offset > tif->tif_rawdatasize)
92
0
        {
93
0
            to_read = threshold;
94
0
            threshold *= THRESHOLD_MULTIPLIER;
95
0
        }
96
2.51k
#endif
97
2.51k
        if (already_read + to_read + rawdata_offset > tif->tif_rawdatasize)
98
2.51k
        {
99
2.51k
            uint8_t *new_rawdata;
100
2.51k
            assert((tif->tif_flags & TIFF_MYBUFFER) != 0);
101
2.51k
            tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64(
102
2.51k
                (uint64_t)already_read + to_read + rawdata_offset, 1024);
103
2.51k
            if (tif->tif_rawdatasize == 0)
104
0
            {
105
0
                TIFFErrorExtR(tif, module, "Invalid buffer size");
106
0
                return 0;
107
0
            }
108
2.51k
            new_rawdata = (uint8_t *)_TIFFreallocExt(tif, tif->tif_rawdata,
109
2.51k
                                                     tif->tif_rawdatasize);
110
2.51k
            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
2.51k
            tif->tif_rawdata = new_rawdata;
121
2.51k
        }
122
2.51k
        if (tif->tif_rawdata == NULL)
123
0
        {
124
            /* should not happen in practice but helps CoverityScan */
125
0
            return 0;
126
0
        }
127
128
2.51k
        bytes_read = TIFFReadFile(
129
2.51k
            tif, tif->tif_rawdata + rawdata_offset + already_read, to_read);
130
2.51k
        already_read += bytes_read;
131
2.51k
        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
2.51k
    }
156
2.51k
    return 1;
157
2.51k
}
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
355
    if (!whole_strip)
356
    {
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
        if (tif->tif_scanlinesize < TIFF_TMSIZE_T_MAX / 16 &&
363
            tif->tif_scanlinesize * 16 < TIFF_TMSIZE_T_MAX - 5000)
364
        {
365
            read_ahead = tif->tif_scanlinesize * 16 + 5000;
366
        }
367
        else
368
        {
369
            read_ahead = tif->tif_scanlinesize;
370
        }
371
    }
372
#else
373
0
    whole_strip = 1;
374
0
#endif
375
376
    /*
377
     * If we haven't loaded this strip, do so now, possibly
378
     * only reading the first part.
379
     */
380
0
    if (strip != tif->tif_curstrip)
381
0
    { /* different strip, refill */
382
383
0
        if (whole_strip)
384
0
        {
385
0
            if (!TIFFFillStrip(tif, strip))
386
0
                return (0);
387
0
        }
388
#if defined(CHUNKY_STRIP_READ_SUPPORT)
389
        else
390
        {
391
            if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))
392
                return 0;
393
        }
394
#endif
395
0
    }
396
397
#if defined(CHUNKY_STRIP_READ_SUPPORT)
398
    /*
399
    ** If we already have some data loaded, do we need to read some more?
400
    */
401
    else if (!whole_strip)
402
    {
403
        /* coverity[dead_error_line:SUPPRESS] */
404
        if (((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) <
405
                read_ahead &&
406
            (uint64_t)tif->tif_rawdataoff + tif->tif_rawdataloaded <
407
                TIFFGetStrileByteCount(tif, strip))
408
        {
409
            if (!TIFFFillStripPartial(tif, strip, read_ahead, 0))
410
                return 0;
411
        }
412
    }
413
#endif
414
415
0
    if (row < tif->tif_row)
416
0
    {
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
0
        if (tif->tif_rawdataoff != 0)
427
0
        {
428
0
            if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))
429
0
                return 0;
430
0
        }
431
0
        else
432
0
        {
433
0
            if (!TIFFStartStrip(tif, strip))
434
0
                return (0);
435
0
        }
436
0
    }
437
438
0
    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
0
    return (1);
452
0
}
453
454
int TIFFReadScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample)
455
0
{
456
0
    int e;
457
458
0
    if (!TIFFCheckRead(tif, 0))
459
0
        return (-1);
460
0
    if ((e = TIFFSeek(tif, row, sample)) != 0)
461
0
    {
462
        /*
463
         * Decompress desired row into user buffer.
464
         */
465
0
        e = (*tif->tif_decoderow)(tif, (uint8_t *)buf, tif->tif_scanlinesize,
466
0
                                  sample);
467
468
        /* we are now poised at the beginning of the next row */
469
0
        tif->tif_row = row + 1;
470
471
0
        if (e)
472
0
            (*tif->tif_postdecode)(tif, (uint8_t *)buf, tif->tif_scanlinesize);
473
0
    }
474
0
    else
475
0
    {
476
        /* See TIFFReadEncodedStrip comment regarding TIFFTAG_FAXFILLFUNC. */
477
0
        if (buf)
478
0
            memset(buf, 0, (size_t)tif->tif_scanlinesize);
479
0
    }
480
0
    return (e > 0 ? 1 : -1);
481
0
}
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
363k
{
491
363k
    static const char module[] = "TIFFReadEncodedStrip";
492
363k
    TIFFDirectory *td = &tif->tif_dir;
493
363k
    uint32_t rowsperstrip;
494
363k
    uint32_t stripsperplane;
495
363k
    uint32_t stripinplane;
496
363k
    uint32_t rows;
497
363k
    tmsize_t stripsize;
498
363k
    if (!TIFFCheckRead(tif, 0))
499
21
        return ((tmsize_t)(-1));
500
363k
    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
363k
    rowsperstrip = td->td_rowsperstrip;
509
363k
    if (rowsperstrip > td->td_imagelength)
510
29.4k
        rowsperstrip = td->td_imagelength;
511
363k
    if (rowsperstrip == 0)
512
0
    {
513
0
        TIFFErrorExtR(tif, module, "rowsperstrip is zero");
514
0
        return ((tmsize_t)(-1));
515
0
    }
516
363k
    stripsperplane =
517
363k
        TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
518
363k
    stripinplane = (strip % stripsperplane);
519
363k
    if (pplane)
520
363k
        *pplane = (uint16_t)(strip / stripsperplane);
521
363k
    rows = td->td_imagelength - stripinplane * rowsperstrip;
522
363k
    if (rows > rowsperstrip)
523
261k
        rows = rowsperstrip;
524
363k
    stripsize = TIFFVStripSize(tif, rows);
525
363k
    if (stripsize == 0)
526
0
        return ((tmsize_t)(-1));
527
363k
    return stripsize;
528
363k
}
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
330k
{
537
330k
    static const char module[] = "TIFFReadEncodedStrip";
538
330k
    TIFFDirectory *td = &tif->tif_dir;
539
330k
    tmsize_t stripsize;
540
330k
    uint16_t plane;
541
542
330k
    stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
543
330k
    if (stripsize == ((tmsize_t)(-1)))
544
21
        return ((tmsize_t)(-1));
545
546
    /* shortcut to avoid an extra memcpy() */
547
330k
    if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
548
330k
        size >= stripsize && !isMapped(tif) &&
549
330k
        ((tif->tif_flags & TIFF_NOREADRAW) == 0))
550
0
    {
551
0
        if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize)
552
0
            return ((tmsize_t)(-1));
553
554
0
        if (!isFillOrder(tif, td->td_fillorder) &&
555
0
            (tif->tif_flags & TIFF_NOBITREV) == 0)
556
0
            TIFFReverseBits(buf, stripsize);
557
558
0
        (*tif->tif_postdecode)(tif, buf, stripsize);
559
0
        return (stripsize);
560
0
    }
561
562
330k
    if ((size != (tmsize_t)(-1)) && (size < stripsize))
563
0
        stripsize = size;
564
330k
    if (!TIFFFillStrip(tif, strip))
565
83.7k
    {
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
83.7k
        if (buf)
569
83.7k
            memset(buf, 0, (size_t)stripsize);
570
83.7k
        return ((tmsize_t)(-1));
571
83.7k
    }
572
246k
    if ((*tif->tif_decodestrip)(tif, buf, stripsize, plane) <= 0)
573
71.5k
        return ((tmsize_t)(-1));
574
174k
    (*tif->tif_postdecode)(tif, buf, stripsize);
575
174k
    return (stripsize);
576
246k
}
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
182k
{
589
182k
    tmsize_t this_stripsize;
590
182k
    uint16_t plane;
591
592
182k
    if (*buf != NULL)
593
149k
    {
594
149k
        return TIFFReadEncodedStrip(tif, strip, *buf, size_to_read);
595
149k
    }
596
597
33.1k
    this_stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
598
33.1k
    if (this_stripsize == ((tmsize_t)(-1)))
599
0
        return ((tmsize_t)(-1));
600
601
33.1k
    if ((size_to_read != (tmsize_t)(-1)) && (size_to_read < this_stripsize))
602
95
        this_stripsize = size_to_read;
603
33.1k
    if (!TIFFFillStrip(tif, strip))
604
6.59k
        return ((tmsize_t)(-1));
605
606
26.5k
    *buf = _TIFFmallocExt(tif, bufsizetoalloc);
607
26.5k
    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.5k
    _TIFFmemset(*buf, 0, bufsizetoalloc);
613
614
26.5k
    if ((*tif->tif_decodestrip)(tif, *buf, this_stripsize, plane) <= 0)
615
17.7k
        return ((tmsize_t)(-1));
616
8.73k
    (*tif->tif_postdecode)(tif, *buf, this_stripsize);
617
8.73k
    return (this_stripsize);
618
26.5k
}
619
620
static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf,
621
                                  tmsize_t size, const char *module)
622
7.25k
{
623
7.25k
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
624
7.25k
    if (!isMapped(tif))
625
0
    {
626
0
        tmsize_t cc;
627
628
0
        if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip)))
629
0
        {
630
0
            TIFFErrorExtR(tif, module,
631
0
                          "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
632
0
                          tif->tif_row, strip);
633
0
            return ((tmsize_t)(-1));
634
0
        }
635
0
        cc = TIFFReadFile(tif, buf, size);
636
0
        if (cc != size)
637
0
        {
638
0
            TIFFErrorExtR(tif, module,
639
0
                          "Read error at scanline %" PRIu32
640
0
                          "; got %" TIFF_SSIZE_FORMAT
641
0
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
642
0
                          tif->tif_row, cc, size);
643
0
            return ((tmsize_t)(-1));
644
0
        }
645
0
    }
646
7.25k
    else
647
7.25k
    {
648
7.25k
        tmsize_t ma = 0;
649
7.25k
        tmsize_t n;
650
7.25k
        if ((TIFFGetStrileOffset(tif, strip) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
651
7.25k
            ((ma = (tmsize_t)TIFFGetStrileOffset(tif, strip)) > tif->tif_size))
652
0
        {
653
0
            n = 0;
654
0
        }
655
7.25k
        else if (ma > TIFF_TMSIZE_T_MAX - size)
656
0
        {
657
0
            n = 0;
658
0
        }
659
7.25k
        else
660
7.25k
        {
661
7.25k
            tmsize_t mb = ma + size;
662
7.25k
            if (mb > tif->tif_size)
663
0
                n = tif->tif_size - ma;
664
7.25k
            else
665
7.25k
                n = size;
666
7.25k
        }
667
7.25k
        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
7.25k
        _TIFFmemcpy(buf, tif->tif_base + ma, size);
677
7.25k
    }
678
7.25k
    return (size);
679
7.25k
}
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
2.51k
{
685
2.51k
    assert(!isMapped(tif));
686
2.51k
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
687
688
2.51k
    if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip_or_tile)))
689
0
    {
690
0
        if (is_strip)
691
0
        {
692
0
            TIFFErrorExtR(tif, module,
693
0
                          "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
694
0
                          tif->tif_row, strip_or_tile);
695
0
        }
696
0
        else
697
0
        {
698
0
            TIFFErrorExtR(tif, module,
699
0
                          "Seek error at row %" PRIu32 ", col %" PRIu32
700
0
                          ", tile %" PRIu32,
701
0
                          tif->tif_row, tif->tif_col, strip_or_tile);
702
0
        }
703
0
        return ((tmsize_t)(-1));
704
0
    }
705
706
2.51k
    if (!TIFFReadAndRealloc(tif, size, 0, is_strip, strip_or_tile, module))
707
0
    {
708
0
        return ((tmsize_t)(-1));
709
0
    }
710
711
2.51k
    return (size);
712
2.51k
}
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
1.29k
{
719
1.29k
    static const char module[] = "TIFFReadRawStrip";
720
1.29k
    TIFFDirectory *td = &tif->tif_dir;
721
1.29k
    uint64_t bytecount64;
722
1.29k
    tmsize_t bytecountm;
723
724
1.29k
    if (!TIFFCheckRead(tif, 0))
725
0
        return ((tmsize_t)(-1));
726
1.29k
    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
1.29k
    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
1.29k
    bytecount64 = TIFFGetStrileByteCount(tif, strip);
741
1.29k
    if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)
742
1.29k
        bytecountm = size;
743
0
    else
744
0
        bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
745
1.29k
    if (bytecountm == 0)
746
0
    {
747
0
        return ((tmsize_t)(-1));
748
0
    }
749
1.29k
    return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
750
1.29k
}
751
752
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
753
28.7k
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
363k
{
761
363k
    static const char module[] = "TIFFFillStrip";
762
363k
    TIFFDirectory *td = &tif->tif_dir;
763
764
363k
    if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
765
357k
    {
766
357k
        uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
767
357k
        if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
768
49.5k
        {
769
49.5k
            TIFFErrorExtR(tif, module,
770
49.5k
                          "Invalid strip byte count %" PRIu64
771
49.5k
                          ", strip %" PRIu32,
772
49.5k
                          bytecount, strip);
773
49.5k
            return (0);
774
49.5k
        }
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
308k
        if (bytecount > 1024 * 1024)
780
2.84k
        {
781
            /* 10 and 4096 are just values that could be adjusted. */
782
            /* Hopefully they are safe enough for all codecs */
783
2.84k
            tmsize_t stripsize = TIFFStripSize(tif);
784
2.84k
            if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
785
2.81k
            {
786
2.81k
                uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
787
2.81k
                TIFFErrorExtR(tif, module,
788
2.81k
                              "Too large strip byte count %" PRIu64
789
2.81k
                              ", strip %" PRIu32 ". Limiting to %" PRIu64,
790
2.81k
                              bytecount, strip, newbytecount);
791
2.81k
                bytecount = newbytecount;
792
2.81k
            }
793
2.84k
        }
794
795
308k
        if (isMapped(tif))
796
305k
        {
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
305k
            if (bytecount > (uint64_t)tif->tif_size ||
807
305k
                TIFFGetStrileOffset(tif, strip) >
808
287k
                    (uint64_t)tif->tif_size - bytecount)
809
28.7k
            {
810
                /*
811
                 * This error message might seem strange, but
812
                 * it's what would happen if a read were done
813
                 * instead.
814
                 */
815
28.7k
                TIFFErrorExtR(
816
28.7k
                    tif, module,
817
818
28.7k
                    "Read error on strip %" PRIu32 "; "
819
28.7k
                    "got %" PRIu64 " bytes, expected %" PRIu64,
820
28.7k
                    strip,
821
28.7k
                    NoSanitizeSubUInt64(tif->tif_size,
822
28.7k
                                        TIFFGetStrileOffset(tif, strip)),
823
28.7k
                    bytecount);
824
28.7k
                tif->tif_curstrip = NOSTRIP;
825
28.7k
                return (0);
826
28.7k
            }
827
305k
        }
828
829
279k
        if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
830
277k
                              (tif->tif_flags & TIFF_NOBITREV)))
831
271k
        {
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
271k
            if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
844
2
            {
845
2
                _TIFFfreeExt(tif, tif->tif_rawdata);
846
2
                tif->tif_rawdata = NULL;
847
2
                tif->tif_rawdatasize = 0;
848
2
            }
849
271k
            tif->tif_flags &= ~TIFF_MYBUFFER;
850
271k
            tif->tif_rawdatasize = (tmsize_t)bytecount;
851
271k
            tif->tif_rawdata =
852
271k
                tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, strip);
853
271k
            tif->tif_rawdataoff = 0;
854
271k
            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
271k
            tif->tif_flags |= TIFF_BUFFERMMAP;
864
271k
        }
865
8.47k
        else
866
8.47k
        {
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
8.47k
            tmsize_t bytecountm;
873
8.47k
            bytecountm = (tmsize_t)bytecount;
874
8.47k
            if ((uint64_t)bytecountm != bytecount)
875
0
            {
876
0
                TIFFErrorExtR(tif, module, "Integer overflow");
877
0
                return (0);
878
0
            }
879
8.47k
            if (bytecountm > tif->tif_rawdatasize)
880
2.88k
            {
881
2.88k
                tif->tif_curstrip = NOSTRIP;
882
2.88k
                if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
883
1
                {
884
1
                    TIFFErrorExtR(
885
1
                        tif, module,
886
1
                        "Data buffer too small to hold strip %" PRIu32, strip);
887
1
                    return (0);
888
1
                }
889
2.88k
            }
890
8.47k
            if (tif->tif_flags & TIFF_BUFFERMMAP)
891
1
            {
892
1
                tif->tif_curstrip = NOSTRIP;
893
1
                tif->tif_rawdata = NULL;
894
1
                tif->tif_rawdatasize = 0;
895
1
                tif->tif_flags &= ~TIFF_BUFFERMMAP;
896
1
            }
897
898
8.47k
            if (isMapped(tif))
899
5.95k
            {
900
5.95k
                if (bytecountm > tif->tif_rawdatasize &&
901
5.95k
                    !TIFFReadBufferSetup(tif, 0, bytecountm))
902
0
                {
903
0
                    return (0);
904
0
                }
905
5.95k
                if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata, bytecountm,
906
5.95k
                                      module) != bytecountm)
907
0
                {
908
0
                    return (0);
909
0
                }
910
5.95k
            }
911
2.51k
            else
912
2.51k
            {
913
2.51k
                if (TIFFReadRawStripOrTile2(tif, strip, 1, bytecountm,
914
2.51k
                                            module) != bytecountm)
915
0
                {
916
0
                    return (0);
917
0
                }
918
2.51k
            }
919
920
8.47k
            tif->tif_rawdataoff = 0;
921
8.47k
            tif->tif_rawdataloaded = bytecountm;
922
923
8.47k
            if (!isFillOrder(tif, td->td_fillorder) &&
924
8.47k
                (tif->tif_flags & TIFF_NOBITREV) == 0)
925
5.95k
                TIFFReverseBits(tif->tif_rawdata, bytecountm);
926
8.47k
        }
927
279k
    }
928
285k
    return (TIFFStartStrip(tif, strip));
929
363k
}
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
2.18M
{
943
2.18M
    if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
944
0
        return ((tmsize_t)(-1));
945
2.18M
    return (TIFFReadEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf,
946
2.18M
                                (tmsize_t)(-1)));
947
2.18M
}
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
3.11M
{
955
3.11M
    static const char module[] = "TIFFReadEncodedTile";
956
3.11M
    TIFFDirectory *td = &tif->tif_dir;
957
3.11M
    tmsize_t tilesize = tif->tif_tilesize;
958
959
3.11M
    if (!TIFFCheckRead(tif, 1))
960
0
        return ((tmsize_t)(-1));
961
3.11M
    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
3.11M
    if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
971
3.11M
        size >= tilesize && !isMapped(tif) &&
972
3.11M
        ((tif->tif_flags & TIFF_NOREADRAW) == 0))
973
0
    {
974
0
        if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize)
975
0
            return ((tmsize_t)(-1));
976
977
0
        if (!isFillOrder(tif, td->td_fillorder) &&
978
0
            (tif->tif_flags & TIFF_NOBITREV) == 0)
979
0
            TIFFReverseBits(buf, tilesize);
980
981
0
        (*tif->tif_postdecode)(tif, buf, tilesize);
982
0
        return (tilesize);
983
0
    }
984
985
3.11M
    if (size == (tmsize_t)(-1))
986
3.11M
        size = tilesize;
987
0
    else if (size > tilesize)
988
0
        size = tilesize;
989
3.11M
    if (!TIFFFillTile(tif, tile))
990
1.12M
    {
991
        /* See TIFFReadEncodedStrip comment regarding TIFFTAG_FAXFILLFUNC. */
992
1.12M
        if (buf)
993
1.12M
            memset(buf, 0, (size_t)size);
994
1.12M
        return ((tmsize_t)(-1));
995
1.12M
    }
996
1.98M
    else if ((*tif->tif_decodetile)(tif, (uint8_t *)buf, size,
997
1.98M
                                    (uint16_t)(tile / td->td_stripsperimage)))
998
1.97M
    {
999
1.97M
        (*tif->tif_postdecode)(tif, (uint8_t *)buf, size);
1000
1.97M
        return (size);
1001
1.97M
    }
1002
14.5k
    else
1003
14.5k
        return ((tmsize_t)(-1));
1004
3.11M
}
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
940k
{
1016
940k
    if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
1017
0
        return ((tmsize_t)(-1));
1018
940k
    return (_TIFFReadEncodedTileAndAllocBuffer(
1019
940k
        tif, TIFFComputeTile(tif, x, y, z, s), buf, bufsizetoalloc,
1020
940k
        (tmsize_t)(-1)));
1021
940k
}
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
940k
{
1033
940k
    static const char module[] = "_TIFFReadEncodedTileAndAllocBuffer";
1034
940k
    TIFFDirectory *td = &tif->tif_dir;
1035
940k
    tmsize_t tilesize = tif->tif_tilesize;
1036
1037
940k
    if (*buf != NULL)
1038
928k
    {
1039
928k
        return TIFFReadEncodedTile(tif, tile, *buf, size_to_read);
1040
928k
    }
1041
1042
12.1k
    if (!TIFFCheckRead(tif, 1))
1043
0
        return ((tmsize_t)(-1));
1044
12.1k
    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
12.1k
    if (!TIFFFillTile(tif, tile))
1053
2.69k
        return ((tmsize_t)(-1));
1054
1055
    /* Sanity checks to avoid excessive memory allocation */
1056
    /* Cf https://gitlab.com/libtiff/libtiff/-/issues/479 */
1057
9.44k
    if (td->td_compression == COMPRESSION_NONE)
1058
0
    {
1059
0
        if (tif->tif_rawdatasize != tilesize)
1060
0
        {
1061
0
            TIFFErrorExtR(tif, TIFFFileName(tif),
1062
0
                          "Invalid tile byte count for tile %u. "
1063
0
                          "Expected %" PRIu64 ", got %" PRIu64,
1064
0
                          tile, (uint64_t)tilesize,
1065
0
                          (uint64_t)tif->tif_rawdatasize);
1066
0
            return ((tmsize_t)(-1));
1067
0
        }
1068
0
    }
1069
9.44k
    else
1070
9.44k
    {
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
9.44k
        const int maxCompressionRatio =
1076
9.44k
            td->td_compression == COMPRESSION_ZSTD ? 33000
1077
9.44k
            : td->td_compression == COMPRESSION_JXL
1078
9.44k
                ?
1079
                /* Evaluated on a 8000x8000 tile */
1080
0
                25000 * (td->td_planarconfig == PLANARCONFIG_CONTIG
1081
0
                             ? td->td_samplesperpixel
1082
0
                             : 1)
1083
9.44k
                : td->td_compression == COMPRESSION_LZMA ? 7000 : 1000;
1084
9.44k
        if (bufsizetoalloc > 100 * 1000 * 1000 &&
1085
9.44k
            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
9.44k
    }
1096
1097
9.44k
    *buf = _TIFFmallocExt(tif, bufsizetoalloc);
1098
9.44k
    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
9.44k
    _TIFFmemset(*buf, 0, bufsizetoalloc);
1104
1105
9.44k
    if (size_to_read == (tmsize_t)(-1))
1106
9.44k
        size_to_read = tilesize;
1107
0
    else if (size_to_read > tilesize)
1108
0
        size_to_read = tilesize;
1109
9.44k
    if ((*tif->tif_decodetile)(tif, (uint8_t *)*buf, size_to_read,
1110
9.44k
                               (uint16_t)(tile / td->td_stripsperimage)))
1111
9.15k
    {
1112
9.15k
        (*tif->tif_postdecode)(tif, (uint8_t *)*buf, size_to_read);
1113
9.15k
        return (size_to_read);
1114
9.15k
    }
1115
291
    else
1116
291
        return ((tmsize_t)(-1));
1117
9.44k
}
1118
1119
static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf,
1120
                                 tmsize_t size, const char *module)
1121
4.18k
{
1122
4.18k
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
1123
4.18k
    if (!isMapped(tif))
1124
0
    {
1125
0
        tmsize_t cc;
1126
1127
0
        if (!SeekOK(tif, TIFFGetStrileOffset(tif, tile)))
1128
0
        {
1129
0
            TIFFErrorExtR(tif, module,
1130
0
                          "Seek error at row %" PRIu32 ", col %" PRIu32
1131
0
                          ", tile %" PRIu32,
1132
0
                          tif->tif_row, tif->tif_col, tile);
1133
0
            return ((tmsize_t)(-1));
1134
0
        }
1135
0
        cc = TIFFReadFile(tif, buf, size);
1136
0
        if (cc != size)
1137
0
        {
1138
0
            TIFFErrorExtR(tif, module,
1139
0
                          "Read error at row %" PRIu32 ", col %" PRIu32
1140
0
                          "; got %" TIFF_SSIZE_FORMAT
1141
0
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
1142
0
                          tif->tif_row, tif->tif_col, cc, size);
1143
0
            return ((tmsize_t)(-1));
1144
0
        }
1145
0
    }
1146
4.18k
    else
1147
4.18k
    {
1148
4.18k
        tmsize_t ma, mb;
1149
4.18k
        tmsize_t n;
1150
4.18k
        ma = (tmsize_t)TIFFGetStrileOffset(tif, tile);
1151
4.18k
        mb = ma + size;
1152
4.18k
        if ((TIFFGetStrileOffset(tif, tile) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
1153
4.18k
            (ma > tif->tif_size))
1154
0
            n = 0;
1155
4.18k
        else if ((mb < ma) || (mb < size) || (mb > tif->tif_size))
1156
0
            n = tif->tif_size - ma;
1157
4.18k
        else
1158
4.18k
            n = size;
1159
4.18k
        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
4.18k
        _TIFFmemcpy(buf, tif->tif_base + ma, size);
1169
4.18k
    }
1170
4.18k
    return (size);
1171
4.18k
}
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
3.12M
{
1217
3.12M
    static const char module[] = "TIFFFillTile";
1218
3.12M
    TIFFDirectory *td = &tif->tif_dir;
1219
1220
3.12M
    if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
1221
3.12M
    {
1222
3.12M
        uint64_t bytecount = TIFFGetStrileByteCount(tif, tile);
1223
3.12M
        if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
1224
789k
        {
1225
789k
            TIFFErrorExtR(tif, module,
1226
789k
                          "%" PRIu64 ": Invalid tile byte count, tile %" PRIu32,
1227
789k
                          bytecount, tile);
1228
789k
            return (0);
1229
789k
        }
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
2.33M
        if (bytecount > 1024 * 1024)
1235
2.19k
        {
1236
            /* 10 and 4096 are just values that could be adjusted. */
1237
            /* Hopefully they are safe enough for all codecs */
1238
2.19k
            tmsize_t stripsize = TIFFTileSize(tif);
1239
2.19k
            if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
1240
2.14k
            {
1241
2.14k
                uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
1242
2.14k
                TIFFErrorExtR(tif, module,
1243
2.14k
                              "Too large tile byte count %" PRIu64
1244
2.14k
                              ", tile %" PRIu32 ". Limiting to %" PRIu64,
1245
2.14k
                              bytecount, tile, newbytecount);
1246
2.14k
                bytecount = newbytecount;
1247
2.14k
            }
1248
2.19k
        }
1249
1250
2.33M
        if (isMapped(tif))
1251
2.33M
        {
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
2.33M
            if (bytecount > (uint64_t)tif->tif_size ||
1262
2.33M
                TIFFGetStrileOffset(tif, tile) >
1263
2.31M
                    (uint64_t)tif->tif_size - bytecount)
1264
41.2k
            {
1265
41.2k
                tif->tif_curtile = NOTILE;
1266
41.2k
                return (0);
1267
41.2k
            }
1268
2.33M
        }
1269
1270
2.29M
        if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
1271
2.29M
                              (tif->tif_flags & TIFF_NOBITREV)))
1272
2.28M
        {
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
2.28M
            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
2.28M
            tif->tif_flags &= ~TIFF_MYBUFFER;
1291
1292
2.28M
            tif->tif_rawdatasize = (tmsize_t)bytecount;
1293
2.28M
            tif->tif_rawdata =
1294
2.28M
                tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, tile);
1295
2.28M
            tif->tif_rawdataoff = 0;
1296
2.28M
            tif->tif_rawdataloaded = (tmsize_t)bytecount;
1297
2.28M
            tif->tif_flags |= TIFF_BUFFERMMAP;
1298
2.28M
        }
1299
4.18k
        else
1300
4.18k
        {
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
4.18k
            tmsize_t bytecountm;
1307
4.18k
            bytecountm = (tmsize_t)bytecount;
1308
4.18k
            if ((uint64_t)bytecountm != bytecount)
1309
0
            {
1310
0
                TIFFErrorExtR(tif, module, "Integer overflow");
1311
0
                return (0);
1312
0
            }
1313
4.18k
            if (bytecountm > tif->tif_rawdatasize)
1314
134
            {
1315
134
                tif->tif_curtile = NOTILE;
1316
134
                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
134
            }
1324
4.18k
            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
4.18k
            if (isMapped(tif))
1333
4.18k
            {
1334
4.18k
                if (bytecountm > tif->tif_rawdatasize &&
1335
4.18k
                    !TIFFReadBufferSetup(tif, 0, bytecountm))
1336
0
                {
1337
0
                    return (0);
1338
0
                }
1339
4.18k
                if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata, bytecountm,
1340
4.18k
                                     module) != bytecountm)
1341
0
                {
1342
0
                    return (0);
1343
0
                }
1344
4.18k
            }
1345
0
            else
1346
0
            {
1347
0
                if (TIFFReadRawStripOrTile2(tif, tile, 0, bytecountm, module) !=
1348
0
                    bytecountm)
1349
0
                {
1350
0
                    return (0);
1351
0
                }
1352
0
            }
1353
1354
4.18k
            tif->tif_rawdataoff = 0;
1355
4.18k
            tif->tif_rawdataloaded = bytecountm;
1356
1357
4.18k
            if (tif->tif_rawdata != NULL &&
1358
4.18k
                !isFillOrder(tif, td->td_fillorder) &&
1359
4.18k
                (tif->tif_flags & TIFF_NOBITREV) == 0)
1360
4.18k
                TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdataloaded);
1361
4.18k
        }
1362
2.29M
    }
1363
2.29M
    return (TIFFStartTile(tif, tile));
1364
3.12M
}
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
499
{
1377
499
    static const char module[] = "TIFFReadBufferSetup";
1378
1379
499
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
1380
499
    tif->tif_flags &= ~TIFF_BUFFERMMAP;
1381
1382
499
    if (tif->tif_rawdata)
1383
55
    {
1384
55
        if (tif->tif_flags & TIFF_MYBUFFER)
1385
55
            _TIFFfreeExt(tif, tif->tif_rawdata);
1386
55
        tif->tif_rawdata = NULL;
1387
55
        tif->tif_rawdatasize = 0;
1388
55
    }
1389
499
    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
499
    else
1396
499
    {
1397
499
        tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64_t)size, 1024);
1398
499
        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
499
        tif->tif_rawdata =
1406
499
            (uint8_t *)_TIFFcallocExt(tif, 1, tif->tif_rawdatasize);
1407
499
        tif->tif_flags |= TIFF_MYBUFFER;
1408
499
    }
1409
499
    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
499
    return (1);
1418
499
}
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
285k
{
1426
285k
    TIFFDirectory *td = &tif->tif_dir;
1427
1428
285k
    if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
1429
57.8k
    {
1430
57.8k
        if (!(*tif->tif_setupdecode)(tif))
1431
236
            return (0);
1432
57.5k
        tif->tif_flags |= TIFF_CODERSETUP;
1433
57.5k
    }
1434
284k
    tif->tif_curstrip = strip;
1435
284k
    tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
1436
284k
    tif->tif_flags &= ~TIFF_BUF4WRITE;
1437
1438
284k
    if (tif->tif_flags & TIFF_NOREADRAW)
1439
5.37k
    {
1440
5.37k
        tif->tif_rawcp = NULL;
1441
5.37k
        tif->tif_rawcc = 0;
1442
5.37k
    }
1443
279k
    else
1444
279k
    {
1445
279k
        tif->tif_rawcp = tif->tif_rawdata;
1446
279k
        if (tif->tif_rawdataloaded > 0)
1447
279k
            tif->tif_rawcc = tif->tif_rawdataloaded;
1448
0
        else
1449
0
            tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, strip);
1450
279k
    }
1451
284k
    if ((*tif->tif_predecode)(tif, (uint16_t)(strip / td->td_stripsperimage)) ==
1452
284k
        0)
1453
11.8k
    {
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
11.8k
        tif->tif_curstrip = NOSTRIP;
1460
11.8k
        return 0;
1461
11.8k
    }
1462
272k
    return 1;
1463
284k
}
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
2.29M
{
1471
2.29M
    static const char module[] = "TIFFStartTile";
1472
2.29M
    TIFFDirectory *td = &tif->tif_dir;
1473
2.29M
    uint32_t howmany32;
1474
1475
2.29M
    if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
1476
16.7k
    {
1477
16.7k
        if (!(*tif->tif_setupdecode)(tif))
1478
48
            return (0);
1479
16.7k
        tif->tif_flags |= TIFF_CODERSETUP;
1480
16.7k
    }
1481
2.29M
    tif->tif_curtile = tile;
1482
2.29M
    if (td->td_tilewidth == 0)
1483
0
    {
1484
0
        TIFFErrorExtR(tif, module, "Zero tilewidth");
1485
0
        return 0;
1486
0
    }
1487
2.29M
    howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
1488
2.29M
    if (howmany32 == 0)
1489
0
    {
1490
0
        TIFFErrorExtR(tif, module, "Zero tiles");
1491
0
        return 0;
1492
0
    }
1493
2.29M
    tif->tif_row = (tile % howmany32) * td->td_tilelength;
1494
2.29M
    howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
1495
2.29M
    if (howmany32 == 0)
1496
0
    {
1497
0
        TIFFErrorExtR(tif, module, "Zero tiles");
1498
0
        return 0;
1499
0
    }
1500
2.29M
    tif->tif_col = (tile % howmany32) * td->td_tilewidth;
1501
2.29M
    tif->tif_flags &= ~TIFF_BUF4WRITE;
1502
2.29M
    if (tif->tif_flags & TIFF_NOREADRAW)
1503
396
    {
1504
396
        tif->tif_rawcp = NULL;
1505
396
        tif->tif_rawcc = 0;
1506
396
    }
1507
2.29M
    else
1508
2.29M
    {
1509
2.29M
        tif->tif_rawcp = tif->tif_rawdata;
1510
2.29M
        if (tif->tif_rawdataloaded > 0)
1511
2.29M
            tif->tif_rawcc = tif->tif_rawdataloaded;
1512
0
        else
1513
0
            tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, tile);
1514
2.29M
    }
1515
2.29M
    return (
1516
2.29M
        (*tif->tif_predecode)(tif, (uint16_t)(tile / td->td_stripsperimage)));
1517
2.29M
}
1518
1519
static int TIFFCheckRead(TIFF *tif, int tiles)
1520
6.61M
{
1521
6.61M
    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
6.61M
    if (tiles ^ isTiled(tif))
1527
21
    {
1528
21
        TIFFErrorExtR(tif, tif->tif_name,
1529
21
                      tiles ? "Can not read tiles from a striped image"
1530
21
                            : "Can not read scanlines from a tiled image");
1531
21
        return (0);
1532
21
    }
1533
6.61M
    return (1);
1534
6.61M
}
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.37M
{
1650
2.37M
    (void)tif;
1651
2.37M
    (void)buf;
1652
2.37M
    (void)cc;
1653
2.37M
}
1654
1655
void _TIFFSwab16BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
1656
7.18k
{
1657
7.18k
    (void)tif;
1658
7.18k
    assert((cc & 1) == 0);
1659
7.18k
    TIFFSwabArrayOfShort((uint16_t *)buf, cc / 2);
1660
7.18k
}
1661
1662
void _TIFFSwab24BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
1663
7.01k
{
1664
7.01k
    (void)tif;
1665
7.01k
    assert((cc % 3) == 0);
1666
7.01k
    TIFFSwabArrayOfTriples((uint8_t *)buf, cc / 3);
1667
7.01k
}
1668
1669
void _TIFFSwab32BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
1670
8.80k
{
1671
8.80k
    (void)tif;
1672
8.80k
    assert((cc & 3) == 0);
1673
8.80k
    TIFFSwabArrayOfLong((uint32_t *)buf, cc / 4);
1674
8.80k
}
1675
1676
void _TIFFSwab64BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
1677
5.79k
{
1678
5.79k
    (void)tif;
1679
5.79k
    assert((cc & 7) == 0);
1680
5.79k
    TIFFSwabArrayOfDouble((double *)buf, cc / 8);
1681
5.79k
}