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

OSGeo / gdal / 15797738239

21 Jun 2025 04:51PM UTC coverage: 71.076% (+0.001%) from 71.075%
15797738239

Pull #12622

github

web-flow
Merge 19559fd15 into 645a00347
Pull Request #12622: VSI Win32: implement OpenDir()

574181 of 807840 relevant lines covered (71.08%)

249831.39 hits per line

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

66.62
/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,
18,727✔
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;
18,727✔
61
#endif
62
    tmsize_t already_read = 0;
18,727✔
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)
37,429✔
86
    {
87
        tmsize_t bytes_read;
88
        tmsize_t to_read = size - already_read;
18,727✔
89
#if SIZEOF_SIZE_T == 8
90
        if (to_read >= threshold && threshold < MAX_THRESHOLD &&
18,727✔
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)
18,727✔
98
        {
99
            uint8_t *new_rawdata;
100
            assert((tif->tif_flags & TIFF_MYBUFFER) != 0);
1,857✔
101
            tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64(
1,857✔
102
                (uint64_t)already_read + to_read + rawdata_offset, 1024);
103
            if (tif->tif_rawdatasize == 0)
1,857✔
104
            {
105
                TIFFErrorExtR(tif, module, "Invalid buffer size");
×
106
                return 0;
×
107
            }
108
            new_rawdata = (uint8_t *)_TIFFreallocExt(tif, tif->tif_rawdata,
1,857✔
109
                                                     tif->tif_rawdatasize);
110
            if (new_rawdata == 0)
1,859✔
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,859✔
121
        }
122
        if (tif->tif_rawdata == NULL)
18,729✔
123
        {
124
            /* should not happen in practice but helps CoverityScan */
125
            return 0;
×
126
        }
127

128
        bytes_read = TIFFReadFile(
18,729✔
129
            tif, tif->tif_rawdata + rawdata_offset + already_read, to_read);
130
        already_read += bytes_read;
18,729✔
131
        if (bytes_read != to_read)
18,729✔
132
        {
133
            memset(tif->tif_rawdata + rawdata_offset + already_read, 0,
27✔
134
                   tif->tif_rawdatasize - rawdata_offset - already_read);
27✔
135
            if (is_strip)
27✔
136
            {
137
                TIFFErrorExtR(tif, module,
4✔
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,
23✔
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;
27✔
154
        }
155
    }
156
    return 1;
18,702✔
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

355
    if (!whole_strip)
153,234✔
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
        if (tif->tif_scanlinesize < TIFF_TMSIZE_T_MAX / 16 &&
153,233✔
362
            tif->tif_scanlinesize * 16 < TIFF_TMSIZE_T_MAX - 5000)
153,233✔
363
        {
364
            read_ahead = tif->tif_scanlinesize * 16 + 5000;
153,233✔
365
        }
366
        else
367
        {
368
            read_ahead = tif->tif_scanlinesize;
×
369
        }
370
    }
371
#else
372
    whole_strip = 1;
373
#endif
374

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

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

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

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

424
        if (tif->tif_rawdataoff != 0)
25✔
425
        {
426
            if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))
×
427
                return 0;
×
428
        }
429
        else
430
        {
431
            if (!TIFFStartStrip(tif, strip))
25✔
432
                return (0);
×
433
        }
434
    }
435

436
    if (row != tif->tif_row)
153,231✔
437
    {
438
        /*
439
         * Seek forward to the desired row.
440
         */
441

442
        /* TODO: Will this really work with partial buffers? */
443

444
        if (!(*tif->tif_seek)(tif, row - tif->tif_row))
×
445
            return (0);
×
446
        tif->tif_row = row;
×
447
    }
448

449
    return (1);
153,231✔
450
}
451

452
int TIFFReadScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample)
153,234✔
453
{
454
    int e;
455

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

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

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

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

506
    rowsperstrip = td->td_rowsperstrip;
2,095,120✔
507
    if (rowsperstrip > td->td_imagelength)
2,095,120✔
508
        rowsperstrip = td->td_imagelength;
16✔
509
    if (rowsperstrip == 0)
2,095,120✔
510
    {
511
        TIFFErrorExtR(tif, module, "rowsperstrip is zero");
×
512
        return ((tmsize_t)(-1));
×
513
    }
514
    stripsperplane =
2,095,120✔
515
        TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
2,095,120✔
516
    stripinplane = (strip % stripsperplane);
2,095,120✔
517
    if (pplane)
2,095,120✔
518
        *pplane = (uint16_t)(strip / stripsperplane);
2,095,110✔
519
    rows = td->td_imagelength - stripinplane * rowsperstrip;
2,095,120✔
520
    if (rows > rowsperstrip)
2,095,120✔
521
        rows = rowsperstrip;
2,078,820✔
522
    stripsize = TIFFVStripSize(tif, rows);
2,095,120✔
523
    if (stripsize == 0)
2,095,140✔
524
        return ((tmsize_t)(-1));
×
525
    return stripsize;
2,095,140✔
526
}
527

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

540
    stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
2,095,100✔
541
    if (stripsize == ((tmsize_t)(-1)))
2,095,080✔
542
        return ((tmsize_t)(-1));
×
543

544
    /* shortcut to avoid an extra memcpy() */
545
    if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
2,095,080✔
546
        size >= stripsize && !isMapped(tif) &&
2,083,040✔
547
        ((tif->tif_flags & TIFF_NOREADRAW) == 0))
2,083,040✔
548
    {
549
        if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize)
2,083,030✔
550
            return ((tmsize_t)(-1));
57✔
551

552
        if (!isFillOrder(tif, td->td_fillorder) &&
2,082,990✔
553
            (tif->tif_flags & TIFF_NOBITREV) == 0)
×
554
            TIFFReverseBits(buf, stripsize);
×
555

556
        (*tif->tif_postdecode)(tif, buf, stripsize);
2,082,990✔
557
        return (stripsize);
2,082,990✔
558
    }
559

560
    if ((size != (tmsize_t)(-1)) && (size < stripsize))
12,046✔
561
        stripsize = size;
×
562
    if (!TIFFFillStrip(tif, strip))
12,046✔
563
    {
564
        /* The output buf may be NULL, in particular if TIFFTAG_FAXFILLFUNC
565
           is being used. Thus, memset must be conditional on buf not NULL. */
566
        if (buf)
7✔
567
            memset(buf, 0, (size_t)stripsize);
7✔
568
        return ((tmsize_t)(-1));
7✔
569
    }
570
    if ((*tif->tif_decodestrip)(tif, buf, stripsize, plane) <= 0)
12,024✔
571
        return ((tmsize_t)(-1));
5✔
572
    (*tif->tif_postdecode)(tif, buf, stripsize);
12,019✔
573
    return (stripsize);
12,019✔
574
}
575

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

590
    if (*buf != NULL)
53✔
591
    {
592
        return TIFFReadEncodedStrip(tif, strip, *buf, size_to_read);
×
593
    }
594

595
    this_stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
53✔
596
    if (this_stripsize == ((tmsize_t)(-1)))
53✔
597
        return ((tmsize_t)(-1));
×
598

599
    if ((size_to_read != (tmsize_t)(-1)) && (size_to_read < this_stripsize))
53✔
600
        this_stripsize = size_to_read;
×
601
    if (!TIFFFillStrip(tif, strip))
53✔
602
        return ((tmsize_t)(-1));
×
603

604
    *buf = _TIFFmallocExt(tif, bufsizetoalloc);
53✔
605
    if (*buf == NULL)
53✔
606
    {
607
        TIFFErrorExtR(tif, TIFFFileName(tif), "No space for strip buffer");
×
608
        return ((tmsize_t)(-1));
×
609
    }
610
    _TIFFmemset(*buf, 0, bufsizetoalloc);
53✔
611

612
    if ((*tif->tif_decodestrip)(tif, *buf, this_stripsize, plane) <= 0)
53✔
613
        return ((tmsize_t)(-1));
×
614
    (*tif->tif_postdecode)(tif, *buf, this_stripsize);
53✔
615
    return (this_stripsize);
53✔
616
}
617

618
static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf,
2,083,060✔
619
                                  tmsize_t size, const char *module)
620
{
621
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
2,083,060✔
622
    if (!isMapped(tif))
2,083,060✔
623
    {
624
        tmsize_t cc;
625

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

679
static tmsize_t TIFFReadRawStripOrTile2(TIFF *tif, uint32_t strip_or_tile,
18,668✔
680
                                        int is_strip, tmsize_t size,
681
                                        const char *module)
682
{
683
    assert(!isMapped(tif));
18,668✔
684
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
18,668✔
685

686
    if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip_or_tile)))
18,668✔
687
    {
688
        if (is_strip)
×
689
        {
690
            TIFFErrorExtR(tif, module,
×
691
                          "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
692
                          tif->tif_row, strip_or_tile);
693
        }
694
        else
695
        {
696
            TIFFErrorExtR(tif, module,
×
697
                          "Seek error at row %" PRIu32 ", col %" PRIu32
698
                          ", tile %" PRIu32,
699
                          tif->tif_row, tif->tif_col, strip_or_tile);
700
        }
701
        return ((tmsize_t)(-1));
×
702
    }
703

704
    if (!TIFFReadAndRealloc(tif, size, 0, is_strip, strip_or_tile, module))
18,666✔
705
    {
706
        return ((tmsize_t)(-1));
27✔
707
    }
708

709
    return (size);
18,641✔
710
}
711

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

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

750
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
751
static uint64_t NoSanitizeSubUInt64(uint64_t a, uint64_t b) { return a - b; }
2✔
752

753
/*
754
 * Read the specified strip and setup for decoding. The data buffer is
755
 * expanded, as necessary, to hold the strip's data.
756
 */
757
int TIFFFillStrip(TIFF *tif, uint32_t strip)
12,087✔
758
{
759
    static const char module[] = "TIFFFillStrip";
760
    TIFFDirectory *td = &tif->tif_dir;
12,087✔
761

762
    if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
12,087✔
763
    {
764
        uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
12,087✔
765
        if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
12,087✔
766
        {
767
            TIFFErrorExtR(tif, module,
1✔
768
                          "Invalid strip byte count %" PRIu64
769
                          ", strip %" PRIu32,
770
                          bytecount, strip);
771
            return (0);
1✔
772
        }
773

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

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

816
                    "Read error on strip %" PRIu32 "; "
817
                    "got %" PRIu64 " bytes, expected %" PRIu64,
818
                    strip,
819
                    NoSanitizeSubUInt64(tif->tif_size,
2✔
820
                                        TIFFGetStrileOffset(tif, strip)),
821
                    bytecount);
822
                tif->tif_curstrip = NOSTRIP;
2✔
823
                return (0);
2✔
824
            }
825
        }
826

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

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

896
            if (isMapped(tif))
12,082✔
897
            {
898
                if (bytecountm > tif->tif_rawdatasize &&
×
899
                    !TIFFReadBufferSetup(tif, 0, bytecountm))
×
900
                {
901
                    return (0);
×
902
                }
903
                if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata, bytecountm,
×
904
                                      module) != bytecountm)
905
                {
906
                    return (0);
×
907
                }
908
            }
909
            else
910
            {
911
                if (TIFFReadRawStripOrTile2(tif, strip, 1, bytecountm,
12,082✔
912
                                            module) != bytecountm)
913
                {
914
                    return (0);
4✔
915
                }
916
            }
917

918
            tif->tif_rawdataoff = 0;
12,078✔
919
            tif->tif_rawdataloaded = bytecountm;
12,078✔
920

921
            if (!isFillOrder(tif, td->td_fillorder) &&
12,078✔
922
                (tif->tif_flags & TIFF_NOBITREV) == 0)
×
923
                TIFFReverseBits(tif->tif_rawdata, bytecountm);
×
924
        }
925
    }
926
    return (TIFFStartStrip(tif, strip));
12,080✔
927
}
928

929
/*
930
 * Tile-oriented Read Support
931
 * Contributed by Nancy Cam (Silicon Graphics).
932
 */
933

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

947
/*
948
 * Read a tile of data and decompress the specified
949
 * amount into the user-supplied buffer.
950
 */
951
tmsize_t TIFFReadEncodedTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
29,885✔
952
{
953
    static const char module[] = "TIFFReadEncodedTile";
954
    TIFFDirectory *td = &tif->tif_dir;
29,885✔
955
    tmsize_t tilesize = tif->tif_tilesize;
29,885✔
956

957
    if (!TIFFCheckRead(tif, 1))
29,885✔
958
        return ((tmsize_t)(-1));
×
959
    if (tile >= td->td_nstrips)
29,886✔
960
    {
961
        TIFFErrorExtR(tif, module,
×
962
                      "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
963
                      td->td_nstrips);
964
        return ((tmsize_t)(-1));
×
965
    }
966

967
    /* shortcut to avoid an extra memcpy() */
968
    if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
29,886✔
969
        size >= tilesize && !isMapped(tif) &&
23,298✔
970
        ((tif->tif_flags & TIFF_NOREADRAW) == 0))
23,298✔
971
    {
972
        if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize)
23,298✔
973
            return ((tmsize_t)(-1));
4✔
974

975
        if (!isFillOrder(tif, td->td_fillorder) &&
23,294✔
976
            (tif->tif_flags & TIFF_NOBITREV) == 0)
×
977
            TIFFReverseBits(buf, tilesize);
×
978

979
        (*tif->tif_postdecode)(tif, buf, tilesize);
23,294✔
980
        return (tilesize);
23,294✔
981
    }
982

983
    if (size == (tmsize_t)(-1))
6,588✔
984
        size = tilesize;
2✔
985
    else if (size > tilesize)
6,586✔
986
        size = tilesize;
×
987
    if (!TIFFFillTile(tif, tile))
6,588✔
988
    {
989
        /* See TIFFReadEncodedStrip comment regarding TIFFTAG_FAXFILLFUNC. */
990
        if (buf)
26✔
991
            memset(buf, 0, (size_t)size);
26✔
992
        return ((tmsize_t)(-1));
26✔
993
    }
994
    else if ((*tif->tif_decodetile)(tif, (uint8_t *)buf, size,
6,558✔
995
                                    (uint16_t)(tile / td->td_stripsperimage)))
6,561✔
996
    {
997
        (*tif->tif_postdecode)(tif, (uint8_t *)buf, size);
6,558✔
998
        return (size);
6,562✔
999
    }
1000
    else
1001
        return ((tmsize_t)(-1));
×
1002
}
1003

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

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

1035
    if (*buf != NULL)
4✔
1036
    {
1037
        return TIFFReadEncodedTile(tif, tile, *buf, size_to_read);
×
1038
    }
1039

1040
    if (!TIFFCheckRead(tif, 1))
4✔
1041
        return ((tmsize_t)(-1));
×
1042
    if (tile >= td->td_nstrips)
4✔
1043
    {
1044
        TIFFErrorExtR(tif, module,
×
1045
                      "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
1046
                      td->td_nstrips);
1047
        return ((tmsize_t)(-1));
×
1048
    }
1049

1050
    if (!TIFFFillTile(tif, tile))
4✔
1051
        return ((tmsize_t)(-1));
1✔
1052

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

1095
    *buf = _TIFFmallocExt(tif, bufsizetoalloc);
3✔
1096
    if (*buf == NULL)
3✔
1097
    {
1098
        TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer");
×
1099
        return ((tmsize_t)(-1));
×
1100
    }
1101
    _TIFFmemset(*buf, 0, bufsizetoalloc);
3✔
1102

1103
    if (size_to_read == (tmsize_t)(-1))
3✔
1104
        size_to_read = tilesize;
3✔
1105
    else if (size_to_read > tilesize)
×
1106
        size_to_read = tilesize;
×
1107
    if ((*tif->tif_decodetile)(tif, (uint8_t *)*buf, size_to_read,
3✔
1108
                               (uint16_t)(tile / td->td_stripsperimage)))
3✔
1109
    {
1110
        (*tif->tif_postdecode)(tif, (uint8_t *)*buf, size_to_read);
3✔
1111
        return (size_to_read);
3✔
1112
    }
1113
    else
1114
        return ((tmsize_t)(-1));
×
1115
}
1116

1117
static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf,
23,301✔
1118
                                 tmsize_t size, const char *module)
1119
{
1120
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
23,301✔
1121
    if (!isMapped(tif))
23,301✔
1122
    {
1123
        tmsize_t cc;
1124

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

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

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

1209
/*
1210
 * Read the specified tile and setup for decoding. The data buffer is
1211
 * expanded, as necessary, to hold the tile's data.
1212
 */
1213
int TIFFFillTile(TIFF *tif, uint32_t tile)
6,591✔
1214
{
1215
    static const char module[] = "TIFFFillTile";
1216
    TIFFDirectory *td = &tif->tif_dir;
6,591✔
1217

1218
    if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
6,591✔
1219
    {
1220
        uint64_t bytecount = TIFFGetStrileByteCount(tif, tile);
6,590✔
1221
        if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
6,588✔
1222
        {
1223
            TIFFErrorExtR(tif, module,
×
1224
                          "%" PRIu64 ": Invalid tile byte count, tile %" PRIu32,
1225
                          bytecount, tile);
1226
            return (0);
×
1227
        }
1228

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

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

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

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

1330
            if (isMapped(tif))
6,585✔
1331
            {
1332
                if (bytecountm > tif->tif_rawdatasize &&
×
1333
                    !TIFFReadBufferSetup(tif, 0, bytecountm))
×
1334
                {
1335
                    return (0);
×
1336
                }
1337
                if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata, bytecountm,
×
1338
                                     module) != bytecountm)
1339
                {
1340
                    return (0);
×
1341
                }
1342
            }
1343
            else
1344
            {
1345
                if (TIFFReadRawStripOrTile2(tif, tile, 0, bytecountm, module) !=
6,585✔
1346
                    bytecountm)
1347
                {
1348
                    return (0);
23✔
1349
                }
1350
            }
1351

1352
            tif->tif_rawdataoff = 0;
6,562✔
1353
            tif->tif_rawdataloaded = bytecountm;
6,562✔
1354

1355
            if (tif->tif_rawdata != NULL &&
6,562✔
1356
                !isFillOrder(tif, td->td_fillorder) &&
6,563✔
1357
                (tif->tif_flags & TIFF_NOBITREV) == 0)
×
1358
                TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdataloaded);
×
1359
        }
1360
    }
1361
    return (TIFFStartTile(tif, tile));
6,565✔
1362
}
1363

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

1377
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
×
1378
    tif->tif_flags &= ~TIFF_BUFFERMMAP;
×
1379

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

1418
/*
1419
 * Set state to appear as if a
1420
 * strip has just been read in.
1421
 */
1422
static int TIFFStartStrip(TIFF *tif, uint32_t strip)
14,495✔
1423
{
1424
    TIFFDirectory *td = &tif->tif_dir;
14,495✔
1425

1426
    if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
14,495✔
1427
    {
1428
        if (!(*tif->tif_setupdecode)(tif))
3,269✔
1429
            return (0);
1✔
1430
        tif->tif_flags |= TIFF_CODERSETUP;
3,271✔
1431
    }
1432
    tif->tif_curstrip = strip;
14,497✔
1433
    tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
14,497✔
1434
    tif->tif_flags &= ~TIFF_BUF4WRITE;
14,497✔
1435

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

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

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

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

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

1553
    if (tif->tif_mode == O_WRONLY)
2,597✔
1554
    {
1555
        TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
×
1556
        return 0;
×
1557
    }
1558
    if (tif->tif_flags & TIFF_NOREADRAW)
2,597✔
1559
    {
1560
        TIFFErrorExtR(tif, module,
×
1561
                      "Compression scheme does not support access to raw "
1562
                      "uncompressed data");
1563
        return 0;
×
1564
    }
1565

1566
    tif->tif_flags &= ~TIFF_MYBUFFER;
2,597✔
1567
    tif->tif_flags |= TIFF_BUFFERMMAP;
2,597✔
1568
    tif->tif_rawdatasize = insize;
2,597✔
1569
    tif->tif_rawdata = inbuf;
2,597✔
1570
    tif->tif_rawdataoff = 0;
2,597✔
1571
    tif->tif_rawdataloaded = insize;
2,597✔
1572

1573
    if (!isFillOrder(tif, td->td_fillorder) &&
2,597✔
1574
        (tif->tif_flags & TIFF_NOBITREV) == 0)
×
1575
    {
1576
        TIFFReverseBits(inbuf, insize);
×
1577
    }
1578

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

1630
    if (!isFillOrder(tif, td->td_fillorder) &&
2,597✔
1631
        (tif->tif_flags & TIFF_NOBITREV) == 0)
×
1632
    {
1633
        TIFFReverseBits(inbuf, insize);
×
1634
    }
1635

1636
    tif->tif_flags = (old_tif_flags & (TIFF_MYBUFFER | TIFF_BUFFERMMAP)) |
2,597✔
1637
                     (tif->tif_flags & ~(TIFF_MYBUFFER | TIFF_BUFFERMMAP));
2,597✔
1638
    tif->tif_rawdatasize = old_rawdatasize;
2,597✔
1639
    tif->tif_rawdata = old_rawdata;
2,597✔
1640
    tif->tif_rawdataoff = 0;
2,597✔
1641
    tif->tif_rawdataloaded = 0;
2,597✔
1642

1643
    return ret;
2,597✔
1644
}
1645

1646
void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
2,518,110✔
1647
{
1648
    (void)tif;
1649
    (void)buf;
1650
    (void)cc;
1651
}
2,518,110✔
1652

1653
void _TIFFSwab16BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
5,073✔
1654
{
1655
    (void)tif;
1656
    assert((cc & 1) == 0);
5,073✔
1657
    TIFFSwabArrayOfShort((uint16_t *)buf, cc / 2);
5,073✔
1658
}
5,073✔
1659

1660
void _TIFFSwab24BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
×
1661
{
1662
    (void)tif;
1663
    assert((cc % 3) == 0);
×
1664
    TIFFSwabArrayOfTriples((uint8_t *)buf, cc / 3);
×
1665
}
×
1666

1667
void _TIFFSwab32BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
188✔
1668
{
1669
    (void)tif;
1670
    assert((cc & 3) == 0);
188✔
1671
    TIFFSwabArrayOfLong((uint32_t *)buf, cc / 4);
188✔
1672
}
188✔
1673

1674
void _TIFFSwab64BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
4✔
1675
{
1676
    (void)tif;
1677
    assert((cc & 7) == 0);
4✔
1678
    TIFFSwabArrayOfDouble((double *)buf, cc / 8);
4✔
1679
}
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