• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OSGeo / gdal / 13813968540

12 Mar 2025 02:33PM UTC coverage: 70.43% (-0.02%) from 70.446%
13813968540

Pull #11951

github

web-flow
Merge 0560ed8f8 into 5ab779ac6
Pull Request #11951: Doc: Build docs using CMake

553276 of 785573 relevant lines covered (70.43%)

222076.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.47
/frmts/gtiff/libtiff/tif_read.c
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
#define NOSTRIP ((uint32_t)(-1)) /* undefined state */
43
#define NOTILE ((uint32_t)(-1))  /* undefined state */
44

45
#define INITIAL_THRESHOLD (1024 * 1024)
46
#define THRESHOLD_MULTIPLIER 10
47
#define MAX_THRESHOLD                                                          \
48
    (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER *      \
49
     INITIAL_THRESHOLD)
50

51
#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,
17,468✔
56
                              int is_strip, uint32_t strip_or_tile,
57
                              const char *module)
58
{
59
#if SIZEOF_SIZE_T == 8
60
    tmsize_t threshold = INITIAL_THRESHOLD;
17,468✔
61
#endif
62
    tmsize_t already_read = 0;
17,468✔
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
    while (already_read < size)
34,909✔
86
    {
87
        tmsize_t bytes_read;
88
        tmsize_t to_read = size - already_read;
17,467✔
89
#if SIZEOF_SIZE_T == 8
90
        if (to_read >= threshold && threshold < MAX_THRESHOLD &&
17,467✔
91
            already_read + to_read + rawdata_offset > tif->tif_rawdatasize)
3✔
92
        {
93
            to_read = threshold;
3✔
94
            threshold *= THRESHOLD_MULTIPLIER;
3✔
95
        }
96
#endif
97
        if (already_read + to_read + rawdata_offset > tif->tif_rawdatasize)
17,467✔
98
        {
99
            uint8_t *new_rawdata;
100
            assert((tif->tif_flags & TIFF_MYBUFFER) != 0);
1,653✔
101
            tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64(
1,653✔
102
                (uint64_t)already_read + to_read + rawdata_offset, 1024);
103
            if (tif->tif_rawdatasize == 0)
1,653✔
104
            {
105
                TIFFErrorExtR(tif, module, "Invalid buffer size");
×
106
                return 0;
×
107
            }
108
            new_rawdata = (uint8_t *)_TIFFreallocExt(tif, tif->tif_rawdata,
1,653✔
109
                                                     tif->tif_rawdatasize);
110
            if (new_rawdata == 0)
1,656✔
111
            {
112
                TIFFErrorExtR(tif, module,
×
113
                              "No space for data buffer at scanline %" PRIu32,
114
                              tif->tif_row);
115
                _TIFFfreeExt(tif, tif->tif_rawdata);
×
116
                tif->tif_rawdata = 0;
×
117
                tif->tif_rawdatasize = 0;
×
118
                return 0;
×
119
            }
120
            tif->tif_rawdata = new_rawdata;
1,656✔
121
        }
122
        if (tif->tif_rawdata == NULL)
17,470✔
123
        {
124
            /* should not happen in practice but helps CoverityScan */
125
            return 0;
×
126
        }
127

128
        bytes_read = TIFFReadFile(
17,470✔
129
            tif, tif->tif_rawdata + rawdata_offset + already_read, to_read);
130
        already_read += bytes_read;
17,470✔
131
        if (bytes_read != to_read)
17,470✔
132
        {
133
            memset(tif->tif_rawdata + rawdata_offset + already_read, 0,
29✔
134
                   tif->tif_rawdatasize - rawdata_offset - already_read);
29✔
135
            if (is_strip)
29✔
136
            {
137
                TIFFErrorExtR(tif, module,
2✔
138
                              "Read error at scanline %" PRIu32
139
                              "; got %" TIFF_SSIZE_FORMAT " bytes, "
140
                              "expected %" TIFF_SSIZE_FORMAT,
141
                              tif->tif_row, already_read, size);
142
            }
143
            else
144
            {
145
                TIFFErrorExtR(tif, module,
27✔
146
                              "Read error at row %" PRIu32 ", col %" PRIu32
147
                              ", tile %" PRIu32 "; "
148
                              "got %" TIFF_SSIZE_FORMAT
149
                              " bytes, expected %" TIFF_SSIZE_FORMAT "",
150
                              tif->tif_row, tif->tif_col, strip_or_tile,
151
                              already_read, size);
152
            }
153
            return 0;
29✔
154
        }
155
    }
156
    return 1;
17,442✔
157
}
158

159
static int TIFFFillStripPartial(TIFF *tif, int strip, tmsize_t read_ahead,
59✔
160
                                int restart)
161
{
162
    static const char module[] = "TIFFFillStripPartial";
163
    register TIFFDirectory *td = &tif->tif_dir;
59✔
164
    tmsize_t unused_data;
165
    uint64_t read_offset;
166
    tmsize_t to_read;
167
    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
    if (read_ahead < TIFF_TMSIZE_T_MAX / 2)
59✔
181
        read_ahead_mod = read_ahead * 2;
59✔
182
    else
183
        read_ahead_mod = read_ahead;
×
184
    if (read_ahead_mod > tif->tif_rawdatasize)
59✔
185
    {
186
        assert(restart);
34✔
187

188
        tif->tif_curstrip = NOSTRIP;
34✔
189
        if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
34✔
190
        {
191
            TIFFErrorExtR(tif, module,
×
192
                          "Data buffer too small to hold part of strip %d",
193
                          strip);
194
            return (0);
×
195
        }
196
    }
197

198
    if (restart)
59✔
199
    {
200
        tif->tif_rawdataloaded = 0;
34✔
201
        tif->tif_rawdataoff = 0;
34✔
202
    }
203

204
    /*
205
    ** If we are reading more data, move any unused data to the
206
    ** start of the buffer.
207
    */
208
    if (tif->tif_rawdataloaded > 0)
59✔
209
        unused_data =
23✔
210
            tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);
23✔
211
    else
212
        unused_data = 0;
36✔
213

214
    if (unused_data > 0)
59✔
215
    {
216
        assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);
23✔
217
        memmove(tif->tif_rawdata, tif->tif_rawcp, unused_data);
23✔
218
    }
219

220
    /*
221
    ** Seek to the point in the file where more data should be read.
222
    */
223
    read_offset = TIFFGetStrileOffset(tif, strip) + tif->tif_rawdataoff +
59✔
224
                  tif->tif_rawdataloaded;
59✔
225

226
    if (!SeekOK(tif, read_offset))
59✔
227
    {
228
        TIFFErrorExtR(tif, module,
×
229
                      "Seek error at scanline %" PRIu32 ", strip %d",
230
                      tif->tif_row, strip);
231
        return 0;
×
232
    }
233

234
    /*
235
    ** How much do we want to read?
236
    */
237
    if (read_ahead_mod > tif->tif_rawdatasize)
59✔
238
        to_read = read_ahead_mod - unused_data;
34✔
239
    else
240
        to_read = tif->tif_rawdatasize - unused_data;
25✔
241
    if ((uint64_t)to_read > TIFFGetStrileByteCount(tif, strip) -
59✔
242
                                tif->tif_rawdataoff - tif->tif_rawdataloaded)
59✔
243
    {
244
        to_read = (tmsize_t)TIFFGetStrileByteCount(tif, strip) -
33✔
245
                  tif->tif_rawdataoff - tif->tif_rawdataloaded;
33✔
246
    }
247

248
    assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);
59✔
249
    if (!TIFFReadAndRealloc(tif, to_read, unused_data, 1, /* is_strip */
59✔
250
                            0,                            /* strip_or_tile */
251
                            module))
252
    {
253
        return 0;
×
254
    }
255

256
    tif->tif_rawdataoff =
59✔
257
        tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data;
59✔
258
    tif->tif_rawdataloaded = unused_data + to_read;
59✔
259

260
    tif->tif_rawcc = tif->tif_rawdataloaded;
59✔
261
    tif->tif_rawcp = tif->tif_rawdata;
59✔
262

263
    if (!isFillOrder(tif, td->td_fillorder) &&
59✔
264
        (tif->tif_flags & TIFF_NOBITREV) == 0)
×
265
    {
266
        assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);
×
267
        TIFFReverseBits(tif->tif_rawdata + unused_data, to_read);
×
268
    }
269

270
    /*
271
    ** When starting a strip from the beginning we need to
272
    ** restart the decoder.
273
    */
274
    if (restart)
59✔
275
    {
276

277
#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
        if (tif->tif_dir.td_compression == COMPRESSION_JPEG &&
36✔
284
            (uint64_t)tif->tif_rawcc < TIFFGetStrileByteCount(tif, strip))
2✔
285
        {
286
            if (TIFFJPEGIsFullStripRequired(tif))
2✔
287
            {
288
                return TIFFFillStrip(tif, strip);
2✔
289
            }
290
        }
291
#endif
292

293
        return TIFFStartStrip(tif, strip);
32✔
294
    }
295
    else
296
    {
297
        return 1;
25✔
298
    }
299
}
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)
153,234✔
310
{
311
    register TIFFDirectory *td = &tif->tif_dir;
153,234✔
312
    uint32_t strip;
313
    int whole_strip;
314
    tmsize_t read_ahead = 0;
153,234✔
315

316
    /*
317
    ** Establish what strip we are working from.
318
    */
319
    if (row >= td->td_imagelength)
153,234✔
320
    { /* out of range */
321
        TIFFErrorExtR(tif, tif->tif_name,
×
322
                      "%" PRIu32 ": Row out of range, max %" PRIu32 "", row,
323
                      td->td_imagelength);
324
        return (0);
×
325
    }
326
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
153,234✔
327
    {
328
        if (sample >= td->td_samplesperpixel)
64,244✔
329
        {
330
            TIFFErrorExtR(tif, tif->tif_name,
×
331
                          "%" PRIu16 ": Sample out of range, max %" PRIu16 "",
332
                          sample, td->td_samplesperpixel);
×
333
            return (0);
×
334
        }
335
        strip = (uint32_t)sample * td->td_stripsperimage +
64,244✔
336
                row / td->td_rowsperstrip;
64,244✔
337
    }
338
    else
339
        strip = row / td->td_rowsperstrip;
88,990✔
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);
153,234✔
347
    if (td->td_compression == COMPRESSION_LERC ||
153,234✔
348
        td->td_compression == COMPRESSION_JBIG)
153,234✔
349
    {
350
        /* Ideally plugins should have a way to declare they don't support
351
         * chunk strip */
352
        whole_strip = 1;
×
353
    }
354
#else
355
    whole_strip = 1;
356
#endif
357

358
    if (!whole_strip)
153,234✔
359
    {
360
        /* 16 is for YCbCr mode where we may need to read 16 */
361
        /* lines at a time to get a decompressed line, and 5000 */
362
        /* is some constant value, for example for JPEG tables */
363
        if (tif->tif_scanlinesize < TIFF_TMSIZE_T_MAX / 16 &&
153,233✔
364
            tif->tif_scanlinesize * 16 < TIFF_TMSIZE_T_MAX - 5000)
153,233✔
365
        {
366
            read_ahead = tif->tif_scanlinesize * 16 + 5000;
153,233✔
367
        }
368
        else
369
        {
370
            read_ahead = tif->tif_scanlinesize;
×
371
        }
372
    }
373

374
    /*
375
     * If we haven't loaded this strip, do so now, possibly
376
     * only reading the first part.
377
     */
378
    if (strip != tif->tif_curstrip)
153,234✔
379
    { /* different strip, refill */
380

381
        if (whole_strip)
35✔
382
        {
383
            if (!TIFFFillStrip(tif, strip))
1✔
384
                return (0);
1✔
385
        }
386
        else
387
        {
388
            if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))
34✔
389
                return 0;
2✔
390
        }
391
    }
392

393
    /*
394
    ** If we already have some data loaded, do we need to read some more?
395
    */
396
    else if (!whole_strip)
153,199✔
397
    {
398
        if (((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) <
153,199✔
399
                read_ahead &&
148,298✔
400
            (uint64_t)tif->tif_rawdataoff + tif->tif_rawdataloaded <
148,298✔
401
                TIFFGetStrileByteCount(tif, strip))
148,298✔
402
        {
403
            if (!TIFFFillStripPartial(tif, strip, read_ahead, 0))
25✔
404
                return 0;
×
405
        }
406
    }
407

408
    if (row < tif->tif_row)
153,231✔
409
    {
410
        /*
411
         * Moving backwards within the same strip: backup
412
         * to the start and then decode forward (below).
413
         *
414
         * NB: If you're planning on lots of random access within a
415
         * strip, it's better to just read and decode the entire
416
         * strip, and then access the decoded data in a random fashion.
417
         */
418

419
        if (tif->tif_rawdataoff != 0)
25✔
420
        {
421
            if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))
×
422
                return 0;
×
423
        }
424
        else
425
        {
426
            if (!TIFFStartStrip(tif, strip))
25✔
427
                return (0);
×
428
        }
429
    }
430

431
    if (row != tif->tif_row)
153,231✔
432
    {
433
        /*
434
         * Seek forward to the desired row.
435
         */
436

437
        /* TODO: Will this really work with partial buffers? */
438

439
        if (!(*tif->tif_seek)(tif, row - tif->tif_row))
×
440
            return (0);
×
441
        tif->tif_row = row;
×
442
    }
443

444
    return (1);
153,231✔
445
}
446

447
int TIFFReadScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample)
153,234✔
448
{
449
    int e;
450

451
    if (!TIFFCheckRead(tif, 0))
153,234✔
452
        return (-1);
×
453
    if ((e = TIFFSeek(tif, row, sample)) != 0)
153,234✔
454
    {
455
        /*
456
         * Decompress desired row into user buffer.
457
         */
458
        e = (*tif->tif_decoderow)(tif, (uint8_t *)buf, tif->tif_scanlinesize,
153,231✔
459
                                  sample);
460

461
        /* we are now poised at the beginning of the next row */
462
        tif->tif_row = row + 1;
153,231✔
463

464
        if (e)
153,231✔
465
            (*tif->tif_postdecode)(tif, (uint8_t *)buf, tif->tif_scanlinesize);
153,229✔
466
    }
467
    else
468
    {
469
        /* See TIFFReadEncodedStrip comment regarding TIFFTAG_FAXFILLFUNC. */
470
        if (buf)
3✔
471
            memset(buf, 0, (size_t)tif->tif_scanlinesize);
3✔
472
    }
473
    return (e > 0 ? 1 : -1);
153,234✔
474
}
475

476
/*
477
 * Calculate the strip size according to the number of
478
 * rows in the strip (check for truncated last strip on any
479
 * of the separations).
480
 */
481
static tmsize_t TIFFReadEncodedStripGetStripSize(TIFF *tif, uint32_t strip,
2,090,420✔
482
                                                 uint16_t *pplane)
483
{
484
    static const char module[] = "TIFFReadEncodedStrip";
485
    TIFFDirectory *td = &tif->tif_dir;
2,090,420✔
486
    uint32_t rowsperstrip;
487
    uint32_t stripsperplane;
488
    uint32_t stripinplane;
489
    uint32_t rows;
490
    tmsize_t stripsize;
491
    if (!TIFFCheckRead(tif, 0))
2,090,420✔
492
        return ((tmsize_t)(-1));
×
493
    if (strip >= td->td_nstrips)
2,090,260✔
494
    {
495
        TIFFErrorExtR(tif, module,
×
496
                      "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,
497
                      td->td_nstrips);
498
        return ((tmsize_t)(-1));
×
499
    }
500

501
    rowsperstrip = td->td_rowsperstrip;
2,090,260✔
502
    if (rowsperstrip > td->td_imagelength)
2,090,260✔
503
        rowsperstrip = td->td_imagelength;
16✔
504
    if (rowsperstrip == 0)
2,090,260✔
505
    {
506
        TIFFErrorExtR(tif, module, "rowsperstrip is zero");
×
507
        return ((tmsize_t)(-1));
×
508
    }
509
    stripsperplane =
2,090,260✔
510
        TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
2,090,260✔
511
    stripinplane = (strip % stripsperplane);
2,090,260✔
512
    if (pplane)
2,090,260✔
513
        *pplane = (uint16_t)(strip / stripsperplane);
2,090,320✔
514
    rows = td->td_imagelength - stripinplane * rowsperstrip;
2,090,260✔
515
    if (rows > rowsperstrip)
2,090,260✔
516
        rows = rowsperstrip;
2,077,440✔
517
    stripsize = TIFFVStripSize(tif, rows);
2,090,260✔
518
    if (stripsize == 0)
2,090,360✔
519
        return ((tmsize_t)(-1));
×
520
    return stripsize;
2,090,360✔
521
}
522

523
/*
524
 * Read a strip of data and decompress the specified
525
 * amount into the user-supplied buffer.
526
 */
527
tmsize_t TIFFReadEncodedStrip(TIFF *tif, uint32_t strip, void *buf,
2,090,400✔
528
                              tmsize_t size)
529
{
530
    static const char module[] = "TIFFReadEncodedStrip";
531
    TIFFDirectory *td = &tif->tif_dir;
2,090,400✔
532
    tmsize_t stripsize;
533
    uint16_t plane;
534

535
    stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
2,090,400✔
536
    if (stripsize == ((tmsize_t)(-1)))
2,090,290✔
537
        return ((tmsize_t)(-1));
×
538

539
    /* shortcut to avoid an extra memcpy() */
540
    if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
2,090,290✔
541
        size >= stripsize && !isMapped(tif) &&
2,078,670✔
542
        ((tif->tif_flags & TIFF_NOREADRAW) == 0))
2,078,680✔
543
    {
544
        if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize)
2,078,690✔
545
            return ((tmsize_t)(-1));
61✔
546

547
        if (!isFillOrder(tif, td->td_fillorder) &&
2,078,710✔
548
            (tif->tif_flags & TIFF_NOBITREV) == 0)
×
549
            TIFFReverseBits(buf, stripsize);
×
550

551
        (*tif->tif_postdecode)(tif, buf, stripsize);
2,078,710✔
552
        return (stripsize);
2,078,730✔
553
    }
554

555
    if ((size != (tmsize_t)(-1)) && (size < stripsize))
11,604✔
556
        stripsize = size;
×
557
    if (!TIFFFillStrip(tif, strip))
11,604✔
558
    {
559
        /* The output buf may be NULL, in particular if TIFFTAG_FAXFILLFUNC
560
           is being used. Thus, memset must be conditional on buf not NULL. */
561
        if (buf)
5✔
562
            memset(buf, 0, (size_t)stripsize);
5✔
563
        return ((tmsize_t)(-1));
5✔
564
    }
565
    if ((*tif->tif_decodestrip)(tif, buf, stripsize, plane) <= 0)
11,547✔
566
        return ((tmsize_t)(-1));
5✔
567
    (*tif->tif_postdecode)(tif, buf, stripsize);
11,542✔
568
    return (stripsize);
11,542✔
569
}
570

571
/* Variant of TIFFReadEncodedStrip() that does
572
 * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
573
 * TIFFFillStrip() has succeeded. This avoid excessive memory allocation in case
574
 * of truncated file.
575
 * * calls regular TIFFReadEncodedStrip() if *buf != NULL
576
 */
577
tmsize_t _TIFFReadEncodedStripAndAllocBuffer(TIFF *tif, uint32_t strip,
53✔
578
                                             void **buf,
579
                                             tmsize_t bufsizetoalloc,
580
                                             tmsize_t size_to_read)
581
{
582
    tmsize_t this_stripsize;
583
    uint16_t plane;
584

585
    if (*buf != NULL)
53✔
586
    {
587
        return TIFFReadEncodedStrip(tif, strip, *buf, size_to_read);
×
588
    }
589

590
    this_stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
53✔
591
    if (this_stripsize == ((tmsize_t)(-1)))
53✔
592
        return ((tmsize_t)(-1));
×
593

594
    if ((size_to_read != (tmsize_t)(-1)) && (size_to_read < this_stripsize))
53✔
595
        this_stripsize = size_to_read;
×
596
    if (!TIFFFillStrip(tif, strip))
53✔
597
        return ((tmsize_t)(-1));
×
598

599
    *buf = _TIFFmallocExt(tif, bufsizetoalloc);
53✔
600
    if (*buf == NULL)
53✔
601
    {
602
        TIFFErrorExtR(tif, TIFFFileName(tif), "No space for strip buffer");
×
603
        return ((tmsize_t)(-1));
×
604
    }
605
    _TIFFmemset(*buf, 0, bufsizetoalloc);
53✔
606

607
    if ((*tif->tif_decodestrip)(tif, *buf, this_stripsize, plane) <= 0)
53✔
608
        return ((tmsize_t)(-1));
×
609
    (*tif->tif_postdecode)(tif, *buf, this_stripsize);
53✔
610
    return (this_stripsize);
53✔
611
}
612

613
static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf,
2,078,840✔
614
                                  tmsize_t size, const char *module)
615
{
616
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
2,078,840✔
617
    if (!isMapped(tif))
2,078,840✔
618
    {
619
        tmsize_t cc;
620

621
        if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip)))
2,078,690✔
622
        {
623
            TIFFErrorExtR(tif, module,
×
624
                          "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
625
                          tif->tif_row, strip);
626
            return ((tmsize_t)(-1));
×
627
        }
628
        cc = TIFFReadFile(tif, buf, size);
2,078,780✔
629
        if (cc != size)
2,078,800✔
630
        {
631
            TIFFErrorExtR(tif, module,
61✔
632
                          "Read error at scanline %" PRIu32
633
                          "; got %" TIFF_SSIZE_FORMAT
634
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
635
                          tif->tif_row, cc, size);
636
            return ((tmsize_t)(-1));
61✔
637
        }
638
    }
639
    else
640
    {
641
        tmsize_t ma = 0;
154✔
642
        tmsize_t n;
643
        if ((TIFFGetStrileOffset(tif, strip) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
154✔
644
            ((ma = (tmsize_t)TIFFGetStrileOffset(tif, strip)) > tif->tif_size))
×
645
        {
646
            n = 0;
×
647
        }
648
        else if (ma > TIFF_TMSIZE_T_MAX - size)
×
649
        {
650
            n = 0;
×
651
        }
652
        else
653
        {
654
            tmsize_t mb = ma + size;
×
655
            if (mb > tif->tif_size)
×
656
                n = tif->tif_size - ma;
×
657
            else
658
                n = size;
×
659
        }
660
        if (n != size)
×
661
        {
662
            TIFFErrorExtR(tif, module,
×
663
                          "Read error at scanline %" PRIu32 ", strip %" PRIu32
664
                          "; got %" TIFF_SSIZE_FORMAT
665
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
666
                          tif->tif_row, strip, n, size);
667
            return ((tmsize_t)(-1));
×
668
        }
669
        _TIFFmemcpy(buf, tif->tif_base + ma, size);
×
670
    }
671
    return (size);
2,078,760✔
672
}
673

674
static tmsize_t TIFFReadRawStripOrTile2(TIFF *tif, uint32_t strip_or_tile,
17,409✔
675
                                        int is_strip, tmsize_t size,
676
                                        const char *module)
677
{
678
    assert(!isMapped(tif));
17,409✔
679
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
17,409✔
680

681
    if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip_or_tile)))
17,409✔
682
    {
683
        if (is_strip)
×
684
        {
685
            TIFFErrorExtR(tif, module,
×
686
                          "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
687
                          tif->tif_row, strip_or_tile);
688
        }
689
        else
690
        {
691
            TIFFErrorExtR(tif, module,
×
692
                          "Seek error at row %" PRIu32 ", col %" PRIu32
693
                          ", tile %" PRIu32,
694
                          tif->tif_row, tif->tif_col, strip_or_tile);
695
        }
696
        return ((tmsize_t)(-1));
×
697
    }
698

699
    if (!TIFFReadAndRealloc(tif, size, 0, is_strip, strip_or_tile, module))
17,409✔
700
    {
701
        return ((tmsize_t)(-1));
29✔
702
    }
703

704
    return (size);
17,379✔
705
}
706

707
/*
708
 * Read a strip of data from the file.
709
 */
710
tmsize_t TIFFReadRawStrip(TIFF *tif, uint32_t strip, void *buf, tmsize_t size)
4✔
711
{
712
    static const char module[] = "TIFFReadRawStrip";
713
    TIFFDirectory *td = &tif->tif_dir;
4✔
714
    uint64_t bytecount64;
715
    tmsize_t bytecountm;
716

717
    if (!TIFFCheckRead(tif, 0))
4✔
718
        return ((tmsize_t)(-1));
×
719
    if (strip >= td->td_nstrips)
4✔
720
    {
721
        TIFFErrorExtR(tif, module,
×
722
                      "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,
723
                      td->td_nstrips);
724
        return ((tmsize_t)(-1));
×
725
    }
726
    if (tif->tif_flags & TIFF_NOREADRAW)
4✔
727
    {
728
        TIFFErrorExtR(tif, module,
×
729
                      "Compression scheme does not support access to raw "
730
                      "uncompressed data");
731
        return ((tmsize_t)(-1));
×
732
    }
733
    bytecount64 = TIFFGetStrileByteCount(tif, strip);
4✔
734
    if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)
4✔
735
        bytecountm = size;
4✔
736
    else
737
        bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
×
738
    if (bytecountm == 0)
4✔
739
    {
740
        return ((tmsize_t)(-1));
×
741
    }
742
    return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
4✔
743
}
744

745
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
746
static uint64_t NoSanitizeSubUInt64(uint64_t a, uint64_t b) { return a - b; }
2✔
747

748
/*
749
 * Read the specified strip and setup for decoding. The data buffer is
750
 * expanded, as necessary, to hold the strip's data.
751
 */
752
int TIFFFillStrip(TIFF *tif, uint32_t strip)
11,608✔
753
{
754
    static const char module[] = "TIFFFillStrip";
755
    TIFFDirectory *td = &tif->tif_dir;
11,608✔
756

757
    if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
11,608✔
758
    {
759
        uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
11,608✔
760
        if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
11,608✔
761
        {
762
            TIFFErrorExtR(tif, module,
1✔
763
                          "Invalid strip byte count %" PRIu64
764
                          ", strip %" PRIu32,
765
                          bytecount, strip);
766
            return (0);
1✔
767
        }
768

769
        /* To avoid excessive memory allocations: */
770
        /* Byte count should normally not be larger than a number of */
771
        /* times the uncompressed size plus some margin */
772
        if (bytecount > 1024 * 1024)
11,607✔
773
        {
774
            /* 10 and 4096 are just values that could be adjusted. */
775
            /* Hopefully they are safe enough for all codecs */
776
            tmsize_t stripsize = TIFFStripSize(tif);
3✔
777
            if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
3✔
778
            {
779
                uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
1✔
780
                TIFFErrorExtR(tif, module,
1✔
781
                              "Too large strip byte count %" PRIu64
782
                              ", strip %" PRIu32 ". Limiting to %" PRIu64,
783
                              bytecount, strip, newbytecount);
784
                bytecount = newbytecount;
1✔
785
            }
786
        }
787

788
        if (isMapped(tif))
11,607✔
789
        {
790
            /*
791
             * We must check for overflow, potentially causing
792
             * an OOB read. Instead of simple
793
             *
794
             *  TIFFGetStrileOffset(tif, strip)+bytecount > tif->tif_size
795
             *
796
             * comparison (which can overflow) we do the following
797
             * two comparisons:
798
             */
799
            if (bytecount > (uint64_t)tif->tif_size ||
4✔
800
                TIFFGetStrileOffset(tif, strip) >
4✔
801
                    (uint64_t)tif->tif_size - bytecount)
4✔
802
            {
803
                /*
804
                 * This error message might seem strange, but
805
                 * it's what would happen if a read were done
806
                 * instead.
807
                 */
808
                TIFFErrorExtR(
4✔
809
                    tif, module,
810

811
                    "Read error on strip %" PRIu32 "; "
812
                    "got %" PRIu64 " bytes, expected %" PRIu64,
813
                    strip,
814
                    NoSanitizeSubUInt64(tif->tif_size,
2✔
815
                                        TIFFGetStrileOffset(tif, strip)),
816
                    bytecount);
817
                tif->tif_curstrip = NOSTRIP;
2✔
818
                return (0);
2✔
819
            }
820
        }
821

822
        if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
11,605✔
823
                              (tif->tif_flags & TIFF_NOBITREV)))
×
824
        {
825
            /*
826
             * The image is mapped into memory and we either don't
827
             * need to flip bits or the compression routine is
828
             * going to handle this operation itself.  In this
829
             * case, avoid copying the raw data and instead just
830
             * reference the data from the memory mapped file
831
             * image.  This assumes that the decompression
832
             * routines do not modify the contents of the raw data
833
             * buffer (if they try to, the application will get a
834
             * fault since the file is mapped read-only).
835
             */
836
            if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
2✔
837
            {
838
                _TIFFfreeExt(tif, tif->tif_rawdata);
×
839
                tif->tif_rawdata = NULL;
×
840
                tif->tif_rawdatasize = 0;
×
841
            }
842
            tif->tif_flags &= ~TIFF_MYBUFFER;
2✔
843
            tif->tif_rawdatasize = (tmsize_t)bytecount;
2✔
844
            tif->tif_rawdata =
2✔
845
                tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, strip);
2✔
846
            tif->tif_rawdataoff = 0;
2✔
847
            tif->tif_rawdataloaded = (tmsize_t)bytecount;
2✔
848

849
            /*
850
             * When we have tif_rawdata reference directly into the memory
851
             * mapped file we need to be pretty careful about how we use the
852
             * rawdata.  It is not a general purpose working buffer as it
853
             * normally otherwise is.  So we keep track of this fact to avoid
854
             * using it improperly.
855
             */
856
            tif->tif_flags |= TIFF_BUFFERMMAP;
2✔
857
        }
858
        else
859
        {
860
            /*
861
             * Expand raw data buffer, if needed, to hold data
862
             * strip coming from file (perhaps should set upper
863
             * bound on the size of a buffer we'll use?).
864
             */
865
            tmsize_t bytecountm;
866
            bytecountm = (tmsize_t)bytecount;
11,603✔
867
            if ((uint64_t)bytecountm != bytecount)
11,603✔
868
            {
869
                TIFFErrorExtR(tif, module, "Integer overflow");
×
870
                return (0);
×
871
            }
872
            if (bytecountm > tif->tif_rawdatasize)
11,603✔
873
            {
874
                tif->tif_curstrip = NOSTRIP;
807✔
875
                if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
807✔
876
                {
877
                    TIFFErrorExtR(
×
878
                        tif, module,
879
                        "Data buffer too small to hold strip %" PRIu32, strip);
880
                    return (0);
×
881
                }
882
            }
883
            if (tif->tif_flags & TIFF_BUFFERMMAP)
11,603✔
884
            {
885
                tif->tif_curstrip = NOSTRIP;
×
886
                tif->tif_rawdata = NULL;
×
887
                tif->tif_rawdatasize = 0;
×
888
                tif->tif_flags &= ~TIFF_BUFFERMMAP;
×
889
            }
890

891
            if (isMapped(tif))
11,603✔
892
            {
893
                if (bytecountm > tif->tif_rawdatasize &&
×
894
                    !TIFFReadBufferSetup(tif, 0, bytecountm))
×
895
                {
896
                    return (0);
×
897
                }
898
                if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata, bytecountm,
×
899
                                      module) != bytecountm)
900
                {
901
                    return (0);
×
902
                }
903
            }
904
            else
905
            {
906
                if (TIFFReadRawStripOrTile2(tif, strip, 1, bytecountm,
11,603✔
907
                                            module) != bytecountm)
908
                {
909
                    return (0);
2✔
910
                }
911
            }
912

913
            tif->tif_rawdataoff = 0;
11,601✔
914
            tif->tif_rawdataloaded = bytecountm;
11,601✔
915

916
            if (!isFillOrder(tif, td->td_fillorder) &&
11,601✔
917
                (tif->tif_flags & TIFF_NOBITREV) == 0)
×
918
                TIFFReverseBits(tif->tif_rawdata, bytecountm);
×
919
        }
920
    }
921
    return (TIFFStartStrip(tif, strip));
11,603✔
922
}
923

924
/*
925
 * Tile-oriented Read Support
926
 * Contributed by Nancy Cam (Silicon Graphics).
927
 */
928

929
/*
930
 * Read and decompress a tile of data.  The
931
 * tile is selected by the (x,y,z,s) coordinates.
932
 */
933
tmsize_t TIFFReadTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z,
2✔
934
                      uint16_t s)
935
{
936
    if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
2✔
937
        return ((tmsize_t)(-1));
×
938
    return (TIFFReadEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf,
2✔
939
                                (tmsize_t)(-1)));
940
}
941

942
/*
943
 * Read a tile of data and decompress the specified
944
 * amount into the user-supplied buffer.
945
 */
946
tmsize_t TIFFReadEncodedTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
27,500✔
947
{
948
    static const char module[] = "TIFFReadEncodedTile";
949
    TIFFDirectory *td = &tif->tif_dir;
27,500✔
950
    tmsize_t tilesize = tif->tif_tilesize;
27,500✔
951

952
    if (!TIFFCheckRead(tif, 1))
27,500✔
953
        return ((tmsize_t)(-1));
×
954
    if (tile >= td->td_nstrips)
27,498✔
955
    {
956
        TIFFErrorExtR(tif, module,
×
957
                      "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
958
                      td->td_nstrips);
959
        return ((tmsize_t)(-1));
×
960
    }
961

962
    /* shortcut to avoid an extra memcpy() */
963
    if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
27,498✔
964
        size >= tilesize && !isMapped(tif) &&
21,693✔
965
        ((tif->tif_flags & TIFF_NOREADRAW) == 0))
21,693✔
966
    {
967
        if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize)
21,693✔
968
            return ((tmsize_t)(-1));
4✔
969

970
        if (!isFillOrder(tif, td->td_fillorder) &&
21,689✔
971
            (tif->tif_flags & TIFF_NOBITREV) == 0)
×
972
            TIFFReverseBits(buf, tilesize);
×
973

974
        (*tif->tif_postdecode)(tif, buf, tilesize);
21,689✔
975
        return (tilesize);
21,689✔
976
    }
977

978
    if (size == (tmsize_t)(-1))
5,805✔
979
        size = tilesize;
2✔
980
    else if (size > tilesize)
5,803✔
981
        size = tilesize;
×
982
    if (!TIFFFillTile(tif, tile))
5,805✔
983
    {
984
        /* See TIFFReadEncodedStrip comment regarding TIFFTAG_FAXFILLFUNC. */
985
        if (buf)
30✔
986
            memset(buf, 0, (size_t)size);
30✔
987
        return ((tmsize_t)(-1));
30✔
988
    }
989
    else if ((*tif->tif_decodetile)(tif, (uint8_t *)buf, size,
5,776✔
990
                                    (uint16_t)(tile / td->td_stripsperimage)))
5,776✔
991
    {
992
        (*tif->tif_postdecode)(tif, (uint8_t *)buf, size);
5,776✔
993
        return (size);
5,776✔
994
    }
995
    else
996
        return ((tmsize_t)(-1));
×
997
}
998

999
/* Variant of TIFFReadTile() that does
1000
 * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
1001
 * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case
1002
 * of truncated file.
1003
 * * calls regular TIFFReadEncodedTile() if *buf != NULL
1004
 */
1005
tmsize_t _TIFFReadTileAndAllocBuffer(TIFF *tif, void **buf,
4✔
1006
                                     tmsize_t bufsizetoalloc, uint32_t x,
1007
                                     uint32_t y, uint32_t z, uint16_t s)
1008
{
1009
    if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
4✔
1010
        return ((tmsize_t)(-1));
×
1011
    return (_TIFFReadEncodedTileAndAllocBuffer(
4✔
1012
        tif, TIFFComputeTile(tif, x, y, z, s), buf, bufsizetoalloc,
1013
        (tmsize_t)(-1)));
1014
}
1015

1016
/* Variant of TIFFReadEncodedTile() that does
1017
 * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
1018
 * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case
1019
 * of truncated file.
1020
 * * calls regular TIFFReadEncodedTile() if *buf != NULL
1021
 */
1022
tmsize_t _TIFFReadEncodedTileAndAllocBuffer(TIFF *tif, uint32_t tile,
4✔
1023
                                            void **buf, tmsize_t bufsizetoalloc,
1024
                                            tmsize_t size_to_read)
1025
{
1026
    static const char module[] = "_TIFFReadEncodedTileAndAllocBuffer";
1027
    TIFFDirectory *td = &tif->tif_dir;
4✔
1028
    tmsize_t tilesize = tif->tif_tilesize;
4✔
1029

1030
    if (*buf != NULL)
4✔
1031
    {
1032
        return TIFFReadEncodedTile(tif, tile, *buf, size_to_read);
×
1033
    }
1034

1035
    if (!TIFFCheckRead(tif, 1))
4✔
1036
        return ((tmsize_t)(-1));
×
1037
    if (tile >= td->td_nstrips)
4✔
1038
    {
1039
        TIFFErrorExtR(tif, module,
×
1040
                      "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
1041
                      td->td_nstrips);
1042
        return ((tmsize_t)(-1));
×
1043
    }
1044

1045
    if (!TIFFFillTile(tif, tile))
4✔
1046
        return ((tmsize_t)(-1));
1✔
1047

1048
    /* Sanity checks to avoid excessive memory allocation */
1049
    /* Cf https://gitlab.com/libtiff/libtiff/-/issues/479 */
1050
    if (td->td_compression == COMPRESSION_NONE)
3✔
1051
    {
1052
        if (tif->tif_rawdatasize != tilesize)
2✔
1053
        {
1054
            TIFFErrorExtR(tif, TIFFFileName(tif),
×
1055
                          "Invalid tile byte count for tile %u. "
1056
                          "Expected %" PRIu64 ", got %" PRIu64,
1057
                          tile, (uint64_t)tilesize,
1058
                          (uint64_t)tif->tif_rawdatasize);
×
1059
            return ((tmsize_t)(-1));
×
1060
        }
1061
    }
1062
    else
1063
    {
1064
        /* Max compression ratio experimentally determined. Might be fragile...
1065
         * Only apply this heuristics to situations where the memory allocation
1066
         * would be big, to avoid breaking nominal use cases.
1067
         */
1068
        const int maxCompressionRatio =
1✔
1069
            td->td_compression == COMPRESSION_ZSTD ? 33000
1✔
1070
            : td->td_compression == COMPRESSION_JXL
2✔
1071
                ?
1072
                /* Evaluated on a 8000x8000 tile */
1073
                25000 * (td->td_planarconfig == PLANARCONFIG_CONTIG
×
1074
                             ? td->td_samplesperpixel
×
1075
                             : 1)
×
1076
                : td->td_compression == COMPRESSION_LZMA ? 7000 : 1000;
1✔
1077
        if (bufsizetoalloc > 100 * 1000 * 1000 &&
1✔
1078
            tif->tif_rawdatasize < tilesize / maxCompressionRatio)
×
1079
        {
1080
            TIFFErrorExtR(tif, TIFFFileName(tif),
×
1081
                          "Likely invalid tile byte count for tile %u. "
1082
                          "Uncompressed tile size is %" PRIu64 ", "
1083
                          "compressed one is %" PRIu64,
1084
                          tile, (uint64_t)tilesize,
1085
                          (uint64_t)tif->tif_rawdatasize);
×
1086
            return ((tmsize_t)(-1));
×
1087
        }
1088
    }
1089

1090
    *buf = _TIFFmallocExt(tif, bufsizetoalloc);
3✔
1091
    if (*buf == NULL)
3✔
1092
    {
1093
        TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer");
×
1094
        return ((tmsize_t)(-1));
×
1095
    }
1096
    _TIFFmemset(*buf, 0, bufsizetoalloc);
3✔
1097

1098
    if (size_to_read == (tmsize_t)(-1))
3✔
1099
        size_to_read = tilesize;
3✔
1100
    else if (size_to_read > tilesize)
×
1101
        size_to_read = tilesize;
×
1102
    if ((*tif->tif_decodetile)(tif, (uint8_t *)*buf, size_to_read,
3✔
1103
                               (uint16_t)(tile / td->td_stripsperimage)))
3✔
1104
    {
1105
        (*tif->tif_postdecode)(tif, (uint8_t *)*buf, size_to_read);
3✔
1106
        return (size_to_read);
3✔
1107
    }
1108
    else
1109
        return ((tmsize_t)(-1));
×
1110
}
1111

1112
static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf,
21,696✔
1113
                                 tmsize_t size, const char *module)
1114
{
1115
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
21,696✔
1116
    if (!isMapped(tif))
21,696✔
1117
    {
1118
        tmsize_t cc;
1119

1120
        if (!SeekOK(tif, TIFFGetStrileOffset(tif, tile)))
21,696✔
1121
        {
1122
            TIFFErrorExtR(tif, module,
×
1123
                          "Seek error at row %" PRIu32 ", col %" PRIu32
1124
                          ", tile %" PRIu32,
1125
                          tif->tif_row, tif->tif_col, tile);
1126
            return ((tmsize_t)(-1));
×
1127
        }
1128
        cc = TIFFReadFile(tif, buf, size);
21,696✔
1129
        if (cc != size)
21,696✔
1130
        {
1131
            TIFFErrorExtR(tif, module,
4✔
1132
                          "Read error at row %" PRIu32 ", col %" PRIu32
1133
                          "; got %" TIFF_SSIZE_FORMAT
1134
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
1135
                          tif->tif_row, tif->tif_col, cc, size);
1136
            return ((tmsize_t)(-1));
4✔
1137
        }
1138
    }
1139
    else
1140
    {
1141
        tmsize_t ma, mb;
1142
        tmsize_t n;
1143
        ma = (tmsize_t)TIFFGetStrileOffset(tif, tile);
×
1144
        mb = ma + size;
×
1145
        if ((TIFFGetStrileOffset(tif, tile) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
×
1146
            (ma > tif->tif_size))
×
1147
            n = 0;
×
1148
        else if ((mb < ma) || (mb < size) || (mb > tif->tif_size))
×
1149
            n = tif->tif_size - ma;
×
1150
        else
1151
            n = size;
×
1152
        if (n != size)
×
1153
        {
1154
            TIFFErrorExtR(tif, module,
×
1155
                          "Read error at row %" PRIu32 ", col %" PRIu32
1156
                          ", tile %" PRIu32 "; got %" TIFF_SSIZE_FORMAT
1157
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
1158
                          tif->tif_row, tif->tif_col, tile, n, size);
1159
            return ((tmsize_t)(-1));
×
1160
        }
1161
        _TIFFmemcpy(buf, tif->tif_base + ma, size);
×
1162
    }
1163
    return (size);
21,692✔
1164
}
1165

1166
/*
1167
 * Read a tile of data from the file.
1168
 */
1169
tmsize_t TIFFReadRawTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
3✔
1170
{
1171
    static const char module[] = "TIFFReadRawTile";
1172
    TIFFDirectory *td = &tif->tif_dir;
3✔
1173
    uint64_t bytecount64;
1174
    tmsize_t bytecountm;
1175

1176
    if (!TIFFCheckRead(tif, 1))
3✔
1177
        return ((tmsize_t)(-1));
×
1178
    if (tile >= td->td_nstrips)
3✔
1179
    {
1180
        TIFFErrorExtR(tif, module,
×
1181
                      "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
1182
                      td->td_nstrips);
1183
        return ((tmsize_t)(-1));
×
1184
    }
1185
    if (tif->tif_flags & TIFF_NOREADRAW)
3✔
1186
    {
1187
        TIFFErrorExtR(tif, module,
×
1188
                      "Compression scheme does not support access to raw "
1189
                      "uncompressed data");
1190
        return ((tmsize_t)(-1));
×
1191
    }
1192
    bytecount64 = TIFFGetStrileByteCount(tif, tile);
3✔
1193
    if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)
3✔
1194
        bytecountm = size;
3✔
1195
    else
1196
        bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
×
1197
    if (bytecountm == 0)
3✔
1198
    {
1199
        return ((tmsize_t)(-1));
×
1200
    }
1201
    return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
3✔
1202
}
1203

1204
/*
1205
 * Read the specified tile and setup for decoding. The data buffer is
1206
 * expanded, as necessary, to hold the tile's data.
1207
 */
1208
int TIFFFillTile(TIFF *tif, uint32_t tile)
5,811✔
1209
{
1210
    static const char module[] = "TIFFFillTile";
1211
    TIFFDirectory *td = &tif->tif_dir;
5,811✔
1212

1213
    if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
5,811✔
1214
    {
1215
        uint64_t bytecount = TIFFGetStrileByteCount(tif, tile);
5,807✔
1216
        if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
5,809✔
1217
        {
1218
            TIFFErrorExtR(tif, module,
×
1219
                          "%" PRIu64 ": Invalid tile byte count, tile %" PRIu32,
1220
                          bytecount, tile);
1221
            return (0);
×
1222
        }
1223

1224
        /* To avoid excessive memory allocations: */
1225
        /* Byte count should normally not be larger than a number of */
1226
        /* times the uncompressed size plus some margin */
1227
        if (bytecount > 1024 * 1024)
5,809✔
1228
        {
1229
            /* 10 and 4096 are just values that could be adjusted. */
1230
            /* Hopefully they are safe enough for all codecs */
1231
            tmsize_t stripsize = TIFFTileSize(tif);
2✔
1232
            if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
2✔
1233
            {
1234
                uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
1✔
1235
                TIFFErrorExtR(tif, module,
1✔
1236
                              "Too large tile byte count %" PRIu64
1237
                              ", tile %" PRIu32 ". Limiting to %" PRIu64,
1238
                              bytecount, tile, newbytecount);
1239
                bytecount = newbytecount;
1✔
1240
            }
1241
        }
1242

1243
        if (isMapped(tif))
5,809✔
1244
        {
1245
            /*
1246
             * We must check for overflow, potentially causing
1247
             * an OOB read. Instead of simple
1248
             *
1249
             *  TIFFGetStrileOffset(tif, tile)+bytecount > tif->tif_size
1250
             *
1251
             * comparison (which can overflow) we do the following
1252
             * two comparisons:
1253
             */
1254
            if (bytecount > (uint64_t)tif->tif_size ||
4✔
1255
                TIFFGetStrileOffset(tif, tile) >
4✔
1256
                    (uint64_t)tif->tif_size - bytecount)
4✔
1257
            {
1258
                tif->tif_curtile = NOTILE;
2✔
1259
                return (0);
2✔
1260
            }
1261
        }
1262

1263
        if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
5,807✔
1264
                              (tif->tif_flags & TIFF_NOBITREV)))
×
1265
        {
1266
            /*
1267
             * The image is mapped into memory and we either don't
1268
             * need to flip bits or the compression routine is
1269
             * going to handle this operation itself.  In this
1270
             * case, avoid copying the raw data and instead just
1271
             * reference the data from the memory mapped file
1272
             * image.  This assumes that the decompression
1273
             * routines do not modify the contents of the raw data
1274
             * buffer (if they try to, the application will get a
1275
             * fault since the file is mapped read-only).
1276
             */
1277
            if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
4✔
1278
            {
1279
                _TIFFfreeExt(tif, tif->tif_rawdata);
×
1280
                tif->tif_rawdata = NULL;
×
1281
                tif->tif_rawdatasize = 0;
×
1282
            }
1283
            tif->tif_flags &= ~TIFF_MYBUFFER;
4✔
1284

1285
            tif->tif_rawdatasize = (tmsize_t)bytecount;
4✔
1286
            tif->tif_rawdata =
2✔
1287
                tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, tile);
4✔
1288
            tif->tif_rawdataoff = 0;
2✔
1289
            tif->tif_rawdataloaded = (tmsize_t)bytecount;
2✔
1290
            tif->tif_flags |= TIFF_BUFFERMMAP;
2✔
1291
        }
1292
        else
1293
        {
1294
            /*
1295
             * Expand raw data buffer, if needed, to hold data
1296
             * tile coming from file (perhaps should set upper
1297
             * bound on the size of a buffer we'll use?).
1298
             */
1299
            tmsize_t bytecountm;
1300
            bytecountm = (tmsize_t)bytecount;
5,803✔
1301
            if ((uint64_t)bytecountm != bytecount)
5,803✔
1302
            {
1303
                TIFFErrorExtR(tif, module, "Integer overflow");
×
1304
                return (0);
×
1305
            }
1306
            if (bytecountm > tif->tif_rawdatasize)
5,803✔
1307
            {
1308
                tif->tif_curtile = NOTILE;
829✔
1309
                if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
829✔
1310
                {
1311
                    TIFFErrorExtR(tif, module,
×
1312
                                  "Data buffer too small to hold tile %" PRIu32,
1313
                                  tile);
1314
                    return (0);
×
1315
                }
1316
            }
1317
            if (tif->tif_flags & TIFF_BUFFERMMAP)
5,803✔
1318
            {
1319
                tif->tif_curtile = NOTILE;
×
1320
                tif->tif_rawdata = NULL;
×
1321
                tif->tif_rawdatasize = 0;
×
1322
                tif->tif_flags &= ~TIFF_BUFFERMMAP;
×
1323
            }
1324

1325
            if (isMapped(tif))
5,803✔
1326
            {
1327
                if (bytecountm > tif->tif_rawdatasize &&
×
1328
                    !TIFFReadBufferSetup(tif, 0, bytecountm))
×
1329
                {
1330
                    return (0);
×
1331
                }
1332
                if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata, bytecountm,
×
1333
                                     module) != bytecountm)
1334
                {
1335
                    return (0);
×
1336
                }
1337
            }
1338
            else
1339
            {
1340
                if (TIFFReadRawStripOrTile2(tif, tile, 0, bytecountm, module) !=
5,803✔
1341
                    bytecountm)
1342
                {
1343
                    return (0);
27✔
1344
                }
1345
            }
1346

1347
            tif->tif_rawdataoff = 0;
5,778✔
1348
            tif->tif_rawdataloaded = bytecountm;
5,778✔
1349

1350
            if (tif->tif_rawdata != NULL &&
5,778✔
1351
                !isFillOrder(tif, td->td_fillorder) &&
5,778✔
1352
                (tif->tif_flags & TIFF_NOBITREV) == 0)
×
1353
                TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdataloaded);
×
1354
        }
1355
    }
1356
    return (TIFFStartTile(tif, tile));
5,784✔
1357
}
1358

1359
/*
1360
 * Setup the raw data buffer in preparation for
1361
 * reading a strip of raw data.  If the buffer
1362
 * is specified as zero, then a buffer of appropriate
1363
 * size is allocated by the library.  Otherwise,
1364
 * the client must guarantee that the buffer is
1365
 * large enough to hold any individual strip of
1366
 * raw data.
1367
 */
1368
int TIFFReadBufferSetup(TIFF *tif, void *bp, tmsize_t size)
×
1369
{
1370
    static const char module[] = "TIFFReadBufferSetup";
1371

1372
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
×
1373
    tif->tif_flags &= ~TIFF_BUFFERMMAP;
×
1374

1375
    if (tif->tif_rawdata)
×
1376
    {
1377
        if (tif->tif_flags & TIFF_MYBUFFER)
×
1378
            _TIFFfreeExt(tif, tif->tif_rawdata);
×
1379
        tif->tif_rawdata = NULL;
×
1380
        tif->tif_rawdatasize = 0;
×
1381
    }
1382
    if (bp)
×
1383
    {
1384
        tif->tif_rawdatasize = size;
×
1385
        tif->tif_rawdata = (uint8_t *)bp;
×
1386
        tif->tif_flags &= ~TIFF_MYBUFFER;
×
1387
    }
1388
    else
1389
    {
1390
        tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64_t)size, 1024);
×
1391
        if (tif->tif_rawdatasize == 0)
×
1392
        {
1393
            TIFFErrorExtR(tif, module, "Invalid buffer size");
×
1394
            return (0);
×
1395
        }
1396
        /* Initialize to zero to avoid uninitialized buffers in case of */
1397
        /* short reads (http://bugzilla.maptools.org/show_bug.cgi?id=2651) */
1398
        tif->tif_rawdata =
×
1399
            (uint8_t *)_TIFFcallocExt(tif, 1, tif->tif_rawdatasize);
×
1400
        tif->tif_flags |= TIFF_MYBUFFER;
×
1401
    }
1402
    if (tif->tif_rawdata == NULL)
×
1403
    {
1404
        TIFFErrorExtR(tif, module,
×
1405
                      "No space for data buffer at scanline %" PRIu32,
1406
                      tif->tif_row);
1407
        tif->tif_rawdatasize = 0;
×
1408
        return (0);
×
1409
    }
1410
    return (1);
×
1411
}
1412

1413
/*
1414
 * Set state to appear as if a
1415
 * strip has just been read in.
1416
 */
1417
static int TIFFStartStrip(TIFF *tif, uint32_t strip)
14,020✔
1418
{
1419
    TIFFDirectory *td = &tif->tif_dir;
14,020✔
1420

1421
    if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
14,020✔
1422
    {
1423
        if (!(*tif->tif_setupdecode)(tif))
3,095✔
1424
            return (0);
1✔
1425
        tif->tif_flags |= TIFF_CODERSETUP;
3,094✔
1426
    }
1427
    tif->tif_curstrip = strip;
14,019✔
1428
    tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
14,019✔
1429
    tif->tif_flags &= ~TIFF_BUF4WRITE;
14,019✔
1430

1431
    if (tif->tif_flags & TIFF_NOREADRAW)
14,019✔
1432
    {
1433
        tif->tif_rawcp = NULL;
×
1434
        tif->tif_rawcc = 0;
×
1435
    }
1436
    else
1437
    {
1438
        tif->tif_rawcp = tif->tif_rawdata;
14,019✔
1439
        if (tif->tif_rawdataloaded > 0)
14,019✔
1440
            tif->tif_rawcc = tif->tif_rawdataloaded;
14,019✔
1441
        else
1442
            tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, strip);
×
1443
    }
1444
    if ((*tif->tif_predecode)(tif, (uint16_t)(strip / td->td_stripsperimage)) ==
14,019✔
1445
        0)
1446
    {
1447
        /* Needed for example for scanline access, if tif_predecode */
1448
        /* fails, and we try to read the same strip again. Without invalidating
1449
         */
1450
        /* tif_curstrip, we'd call tif_decoderow() on a possibly invalid */
1451
        /* codec state. */
1452
        tif->tif_curstrip = NOSTRIP;
2✔
1453
        return 0;
2✔
1454
    }
1455
    return 1;
14,016✔
1456
}
1457

1458
/*
1459
 * Set state to appear as if a
1460
 * tile has just been read in.
1461
 */
1462
static int TIFFStartTile(TIFF *tif, uint32_t tile)
6,020✔
1463
{
1464
    static const char module[] = "TIFFStartTile";
1465
    TIFFDirectory *td = &tif->tif_dir;
6,020✔
1466
    uint32_t howmany32;
1467

1468
    if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
6,020✔
1469
    {
1470
        if (!(*tif->tif_setupdecode)(tif))
857✔
1471
            return (0);
×
1472
        tif->tif_flags |= TIFF_CODERSETUP;
856✔
1473
    }
1474
    tif->tif_curtile = tile;
6,019✔
1475
    if (td->td_tilewidth == 0)
6,019✔
1476
    {
1477
        TIFFErrorExtR(tif, module, "Zero tilewidth");
×
1478
        return 0;
×
1479
    }
1480
    howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
6,019✔
1481
    if (howmany32 == 0)
6,019✔
1482
    {
1483
        TIFFErrorExtR(tif, module, "Zero tiles");
×
1484
        return 0;
×
1485
    }
1486
    tif->tif_row = (tile % howmany32) * td->td_tilelength;
6,019✔
1487
    howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
6,019✔
1488
    if (howmany32 == 0)
6,019✔
1489
    {
1490
        TIFFErrorExtR(tif, module, "Zero tiles");
×
1491
        return 0;
×
1492
    }
1493
    tif->tif_col = (tile % howmany32) * td->td_tilewidth;
6,019✔
1494
    tif->tif_flags &= ~TIFF_BUF4WRITE;
6,019✔
1495
    if (tif->tif_flags & TIFF_NOREADRAW)
6,019✔
1496
    {
1497
        tif->tif_rawcp = NULL;
2✔
1498
        tif->tif_rawcc = 0;
2✔
1499
    }
1500
    else
1501
    {
1502
        tif->tif_rawcp = tif->tif_rawdata;
6,017✔
1503
        if (tif->tif_rawdataloaded > 0)
6,017✔
1504
            tif->tif_rawcc = tif->tif_rawdataloaded;
6,016✔
1505
        else
1506
            tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, tile);
1✔
1507
    }
1508
    return (
1509
        (*tif->tif_predecode)(tif, (uint16_t)(tile / td->td_stripsperimage)));
6,018✔
1510
}
1511

1512
static int TIFFCheckRead(TIFF *tif, int tiles)
2,271,200✔
1513
{
1514
    if (tif->tif_mode == O_WRONLY)
2,271,200✔
1515
    {
1516
        TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
×
1517
        return (0);
×
1518
    }
1519
    if (tiles ^ isTiled(tif))
2,271,200✔
1520
    {
1521
        TIFFErrorExtR(tif, tif->tif_name,
×
1522
                      tiles ? "Can not read tiles from a striped image"
1523
                            : "Can not read scanlines from a tiled image");
1524
        return (0);
×
1525
    }
1526
    return (1);
2,271,200✔
1527
}
1528

1529
/* Use the provided input buffer (inbuf, insize) and decompress it into
1530
 * (outbuf, outsize).
1531
 * This function replaces the use of
1532
 * TIFFReadEncodedStrip()/TIFFReadEncodedTile() when the user can provide the
1533
 * buffer for the input data, for example when he wants to avoid libtiff to read
1534
 * the strile offset/count values from the [Strip|Tile][Offsets/ByteCounts]
1535
 * array. inbuf content must be writable (if bit reversal is needed) Returns 1
1536
 * in case of success, 0 otherwise.
1537
 */
1538
int TIFFReadFromUserBuffer(TIFF *tif, uint32_t strile, void *inbuf,
2,598✔
1539
                           tmsize_t insize, void *outbuf, tmsize_t outsize)
1540
{
1541
    static const char module[] = "TIFFReadFromUserBuffer";
1542
    TIFFDirectory *td = &tif->tif_dir;
2,598✔
1543
    int ret = 1;
2,598✔
1544
    uint32_t old_tif_flags = tif->tif_flags;
2,598✔
1545
    tmsize_t old_rawdatasize = tif->tif_rawdatasize;
2,598✔
1546
    void *old_rawdata = tif->tif_rawdata;
2,598✔
1547

1548
    if (tif->tif_mode == O_WRONLY)
2,598✔
1549
    {
1550
        TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
×
1551
        return 0;
×
1552
    }
1553
    if (tif->tif_flags & TIFF_NOREADRAW)
2,598✔
1554
    {
1555
        TIFFErrorExtR(tif, module,
×
1556
                      "Compression scheme does not support access to raw "
1557
                      "uncompressed data");
1558
        return 0;
×
1559
    }
1560

1561
    tif->tif_flags &= ~TIFF_MYBUFFER;
2,598✔
1562
    tif->tif_flags |= TIFF_BUFFERMMAP;
2,598✔
1563
    tif->tif_rawdatasize = insize;
2,598✔
1564
    tif->tif_rawdata = inbuf;
2,598✔
1565
    tif->tif_rawdataoff = 0;
2,598✔
1566
    tif->tif_rawdataloaded = insize;
2,598✔
1567

1568
    if (!isFillOrder(tif, td->td_fillorder) &&
2,598✔
1569
        (tif->tif_flags & TIFF_NOBITREV) == 0)
×
1570
    {
1571
        TIFFReverseBits(inbuf, insize);
×
1572
    }
1573

1574
    if (TIFFIsTiled(tif))
2,598✔
1575
    {
1576
        if (!TIFFStartTile(tif, strile))
238✔
1577
        {
1578
            ret = 0;
×
1579
            /* See related TIFFReadEncodedStrip comment. */
1580
            if (outbuf)
×
1581
                memset(outbuf, 0, (size_t)outsize);
×
1582
        }
1583
        else if (!(*tif->tif_decodetile)(
238✔
1584
                     tif, (uint8_t *)outbuf, outsize,
1585
                     (uint16_t)(strile / td->td_stripsperimage)))
238✔
1586
        {
1587
            ret = 0;
×
1588
        }
1589
    }
1590
    else
1591
    {
1592
        uint32_t rowsperstrip = td->td_rowsperstrip;
2,360✔
1593
        uint32_t stripsperplane;
1594
        if (rowsperstrip > td->td_imagelength)
2,360✔
1595
            rowsperstrip = td->td_imagelength;
×
1596
        if (rowsperstrip == 0)
2,360✔
1597
        {
1598
            TIFFErrorExtR(tif, module, "rowsperstrip is zero");
×
1599
            ret = 0;
×
1600
        }
1601
        else
1602
        {
1603
            stripsperplane =
2,360✔
1604
                TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
2,360✔
1605
            if (!TIFFStartStrip(tif, strile))
2,360✔
1606
            {
1607
                ret = 0;
×
1608
                /* See related TIFFReadEncodedStrip comment. */
1609
                if (outbuf)
×
1610
                    memset(outbuf, 0, (size_t)outsize);
×
1611
            }
1612
            else if (!(*tif->tif_decodestrip)(
2,360✔
1613
                         tif, (uint8_t *)outbuf, outsize,
1614
                         (uint16_t)(strile / stripsperplane)))
2,359✔
1615
            {
1616
                ret = 0;
×
1617
            }
1618
        }
1619
    }
1620
    if (ret)
2,598✔
1621
    {
1622
        (*tif->tif_postdecode)(tif, (uint8_t *)outbuf, outsize);
2,598✔
1623
    }
1624

1625
    if (!isFillOrder(tif, td->td_fillorder) &&
2,598✔
1626
        (tif->tif_flags & TIFF_NOBITREV) == 0)
×
1627
    {
1628
        TIFFReverseBits(inbuf, insize);
×
1629
    }
1630

1631
    tif->tif_flags = (old_tif_flags & (TIFF_MYBUFFER | TIFF_BUFFERMMAP)) |
2,598✔
1632
                     (tif->tif_flags & ~(TIFF_MYBUFFER | TIFF_BUFFERMMAP));
2,598✔
1633
    tif->tif_rawdatasize = old_rawdatasize;
2,598✔
1634
    tif->tif_rawdata = old_rawdata;
2,598✔
1635
    tif->tif_rawdataoff = 0;
2,598✔
1636
    tif->tif_rawdataloaded = 0;
2,598✔
1637

1638
    return ret;
2,598✔
1639
}
1640

1641
void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
2,513,030✔
1642
{
1643
    (void)tif;
1644
    (void)buf;
1645
    (void)cc;
1646
}
2,513,030✔
1647

1648
void _TIFFSwab16BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
1,943✔
1649
{
1650
    (void)tif;
1651
    assert((cc & 1) == 0);
1,943✔
1652
    TIFFSwabArrayOfShort((uint16_t *)buf, cc / 2);
1,943✔
1653
}
1,943✔
1654

1655
void _TIFFSwab24BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
×
1656
{
1657
    (void)tif;
1658
    assert((cc % 3) == 0);
×
1659
    TIFFSwabArrayOfTriples((uint8_t *)buf, cc / 3);
×
1660
}
×
1661

1662
void _TIFFSwab32BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
188✔
1663
{
1664
    (void)tif;
1665
    assert((cc & 3) == 0);
188✔
1666
    TIFFSwabArrayOfLong((uint32_t *)buf, cc / 4);
188✔
1667
}
188✔
1668

1669
void _TIFFSwab64BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
4✔
1670
{
1671
    (void)tif;
1672
    assert((cc & 7) == 0);
4✔
1673
    TIFFSwabArrayOfDouble((double *)buf, cc / 8);
4✔
1674
}
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc