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

OSGeo / gdal / 8872387746

29 Apr 2024 02:16AM UTC coverage: 69.076% (+0.003%) from 69.073%
8872387746

Pull #9801

github

web-flow
Bump actions/upload-artifact from 4.3.2 to 4.3.3

Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 4.3.2 to 4.3.3.
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](https://github.com/actions/upload-artifact/compare/1746f4ab6...65462800f)

---
updated-dependencies:
- dependency-name: actions/upload-artifact
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #9801: Bump actions/upload-artifact from 4.3.2 to 4.3.3

534153 of 773282 relevant lines covered (69.08%)

205719.18 hits per line

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

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

128
        bytes_read = TIFFReadFile(
15,390✔
129
            tif, tif->tif_rawdata + rawdata_offset + already_read, to_read);
130
        already_read += bytes_read;
15,390✔
131
        if (bytes_read != to_read)
15,390✔
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;
15,359✔
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
    return (e > 0 ? 1 : -1);
153,234✔
468
}
469

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

495
    rowsperstrip = td->td_rowsperstrip;
2,070,280✔
496
    if (rowsperstrip > td->td_imagelength)
2,070,280✔
497
        rowsperstrip = td->td_imagelength;
16✔
498
    if (rowsperstrip == 0)
2,070,280✔
499
    {
500
        TIFFErrorExtR(tif, module, "rowsperstrip is zero");
×
501
        return ((tmsize_t)(-1));
×
502
    }
503
    stripsperplane =
2,070,280✔
504
        TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
2,070,280✔
505
    stripinplane = (strip % stripsperplane);
2,070,280✔
506
    if (pplane)
2,070,280✔
507
        *pplane = (uint16_t)(strip / stripsperplane);
2,070,280✔
508
    rows = td->td_imagelength - stripinplane * rowsperstrip;
2,070,280✔
509
    if (rows > rowsperstrip)
2,070,280✔
510
        rows = rowsperstrip;
2,060,170✔
511
    stripsize = TIFFVStripSize(tif, rows);
2,070,280✔
512
    if (stripsize == 0)
2,070,280✔
513
        return ((tmsize_t)(-1));
×
514
    return stripsize;
2,070,280✔
515
}
516

517
/*
518
 * Read a strip of data and decompress the specified
519
 * amount into the user-supplied buffer.
520
 */
521
tmsize_t TIFFReadEncodedStrip(TIFF *tif, uint32_t strip, void *buf,
2,070,230✔
522
                              tmsize_t size)
523
{
524
    static const char module[] = "TIFFReadEncodedStrip";
525
    TIFFDirectory *td = &tif->tif_dir;
2,070,230✔
526
    tmsize_t stripsize;
527
    uint16_t plane;
528

529
    stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
2,070,230✔
530
    if (stripsize == ((tmsize_t)(-1)))
2,070,230✔
531
        return ((tmsize_t)(-1));
×
532

533
    /* shortcut to avoid an extra memcpy() */
534
    if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
2,070,230✔
535
        size >= stripsize && !isMapped(tif) &&
2,060,290✔
536
        ((tif->tif_flags & TIFF_NOREADRAW) == 0))
2,060,290✔
537
    {
538
        if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize)
2,060,290✔
539
            return ((tmsize_t)(-1));
59✔
540

541
        if (!isFillOrder(tif, td->td_fillorder) &&
2,060,230✔
542
            (tif->tif_flags & TIFF_NOBITREV) == 0)
×
543
            TIFFReverseBits(buf, stripsize);
×
544

545
        (*tif->tif_postdecode)(tif, buf, stripsize);
2,060,230✔
546
        return (stripsize);
2,060,230✔
547
    }
548

549
    if ((size != (tmsize_t)(-1)) && (size < stripsize))
9,937✔
550
        stripsize = size;
×
551
    if (!TIFFFillStrip(tif, strip))
9,937✔
552
        return ((tmsize_t)(-1));
5✔
553
    if ((*tif->tif_decodestrip)(tif, buf, stripsize, plane) <= 0)
9,933✔
554
        return ((tmsize_t)(-1));
4✔
555
    (*tif->tif_postdecode)(tif, buf, stripsize);
9,929✔
556
    return (stripsize);
9,929✔
557
}
558

559
/* Variant of TIFFReadEncodedStrip() that does
560
 * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
561
 * TIFFFillStrip() has succeeded. This avoid excessive memory allocation in case
562
 * of truncated file.
563
 * * calls regular TIFFReadEncodedStrip() if *buf != NULL
564
 */
565
tmsize_t _TIFFReadEncodedStripAndAllocBuffer(TIFF *tif, uint32_t strip,
53✔
566
                                             void **buf,
567
                                             tmsize_t bufsizetoalloc,
568
                                             tmsize_t size_to_read)
569
{
570
    tmsize_t this_stripsize;
571
    uint16_t plane;
572

573
    if (*buf != NULL)
53✔
574
    {
575
        return TIFFReadEncodedStrip(tif, strip, *buf, size_to_read);
×
576
    }
577

578
    this_stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
53✔
579
    if (this_stripsize == ((tmsize_t)(-1)))
53✔
580
        return ((tmsize_t)(-1));
×
581

582
    if ((size_to_read != (tmsize_t)(-1)) && (size_to_read < this_stripsize))
53✔
583
        this_stripsize = size_to_read;
×
584
    if (!TIFFFillStrip(tif, strip))
53✔
585
        return ((tmsize_t)(-1));
×
586

587
    *buf = _TIFFmallocExt(tif, bufsizetoalloc);
53✔
588
    if (*buf == NULL)
53✔
589
    {
590
        TIFFErrorExtR(tif, TIFFFileName(tif), "No space for strip buffer");
×
591
        return ((tmsize_t)(-1));
×
592
    }
593
    _TIFFmemset(*buf, 0, bufsizetoalloc);
53✔
594

595
    if ((*tif->tif_decodestrip)(tif, *buf, this_stripsize, plane) <= 0)
53✔
596
        return ((tmsize_t)(-1));
×
597
    (*tif->tif_postdecode)(tif, *buf, this_stripsize);
53✔
598
    return (this_stripsize);
53✔
599
}
600

601
static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf,
2,060,290✔
602
                                  tmsize_t size, const char *module)
603
{
604
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
2,060,290✔
605
    if (!isMapped(tif))
2,060,290✔
606
    {
607
        tmsize_t cc;
608

609
        if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip)))
2,060,290✔
610
        {
611
            TIFFErrorExtR(tif, module,
×
612
                          "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
613
                          tif->tif_row, strip);
614
            return ((tmsize_t)(-1));
×
615
        }
616
        cc = TIFFReadFile(tif, buf, size);
2,060,290✔
617
        if (cc != size)
2,060,290✔
618
        {
619
            TIFFErrorExtR(tif, module,
59✔
620
                          "Read error at scanline %" PRIu32
621
                          "; got %" TIFF_SSIZE_FORMAT
622
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
623
                          tif->tif_row, cc, size);
624
            return ((tmsize_t)(-1));
59✔
625
        }
626
    }
627
    else
628
    {
629
        tmsize_t ma = 0;
1✔
630
        tmsize_t n;
631
        if ((TIFFGetStrileOffset(tif, strip) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
1✔
632
            ((ma = (tmsize_t)TIFFGetStrileOffset(tif, strip)) > tif->tif_size))
×
633
        {
634
            n = 0;
×
635
        }
636
        else if (ma > TIFF_TMSIZE_T_MAX - size)
×
637
        {
638
            n = 0;
×
639
        }
640
        else
641
        {
642
            tmsize_t mb = ma + size;
×
643
            if (mb > tif->tif_size)
×
644
                n = tif->tif_size - ma;
×
645
            else
646
                n = size;
×
647
        }
648
        if (n != size)
×
649
        {
650
            TIFFErrorExtR(tif, module,
×
651
                          "Read error at scanline %" PRIu32 ", strip %" PRIu32
652
                          "; got %" TIFF_SSIZE_FORMAT
653
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
654
                          tif->tif_row, strip, n, size);
655
            return ((tmsize_t)(-1));
×
656
        }
657
        _TIFFmemcpy(buf, tif->tif_base + ma, size);
×
658
    }
659
    return (size);
2,060,230✔
660
}
661

662
static tmsize_t TIFFReadRawStripOrTile2(TIFF *tif, uint32_t strip_or_tile,
15,329✔
663
                                        int is_strip, tmsize_t size,
664
                                        const char *module)
665
{
666
    assert(!isMapped(tif));
15,329✔
667
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
15,329✔
668

669
    if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip_or_tile)))
15,329✔
670
    {
671
        if (is_strip)
×
672
        {
673
            TIFFErrorExtR(tif, module,
×
674
                          "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
675
                          tif->tif_row, strip_or_tile);
676
        }
677
        else
678
        {
679
            TIFFErrorExtR(tif, module,
×
680
                          "Seek error at row %" PRIu32 ", col %" PRIu32
681
                          ", tile %" PRIu32,
682
                          tif->tif_row, tif->tif_col, strip_or_tile);
683
        }
684
        return ((tmsize_t)(-1));
×
685
    }
686

687
    if (!TIFFReadAndRealloc(tif, size, 0, is_strip, strip_or_tile, module))
15,329✔
688
    {
689
        return ((tmsize_t)(-1));
29✔
690
    }
691

692
    return (size);
15,300✔
693
}
694

695
/*
696
 * Read a strip of data from the file.
697
 */
698
tmsize_t TIFFReadRawStrip(TIFF *tif, uint32_t strip, void *buf, tmsize_t size)
4✔
699
{
700
    static const char module[] = "TIFFReadRawStrip";
701
    TIFFDirectory *td = &tif->tif_dir;
4✔
702
    uint64_t bytecount64;
703
    tmsize_t bytecountm;
704

705
    if (!TIFFCheckRead(tif, 0))
4✔
706
        return ((tmsize_t)(-1));
×
707
    if (strip >= td->td_nstrips)
4✔
708
    {
709
        TIFFErrorExtR(tif, module,
×
710
                      "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,
711
                      td->td_nstrips);
712
        return ((tmsize_t)(-1));
×
713
    }
714
    if (tif->tif_flags & TIFF_NOREADRAW)
4✔
715
    {
716
        TIFFErrorExtR(tif, module,
×
717
                      "Compression scheme does not support access to raw "
718
                      "uncompressed data");
719
        return ((tmsize_t)(-1));
×
720
    }
721
    bytecount64 = TIFFGetStrileByteCount(tif, strip);
4✔
722
    if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)
4✔
723
        bytecountm = size;
4✔
724
    else
725
        bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
×
726
    if (bytecountm == 0)
4✔
727
    {
728
        return ((tmsize_t)(-1));
×
729
    }
730
    return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
4✔
731
}
732

733
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
734
static uint64_t NoSanitizeSubUInt64(uint64_t a, uint64_t b) { return a - b; }
2✔
735

736
/*
737
 * Read the specified strip and setup for decoding. The data buffer is
738
 * expanded, as necessary, to hold the strip's data.
739
 */
740
int TIFFFillStrip(TIFF *tif, uint32_t strip)
9,994✔
741
{
742
    static const char module[] = "TIFFFillStrip";
743
    TIFFDirectory *td = &tif->tif_dir;
9,994✔
744

745
    if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
9,994✔
746
    {
747
        uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
9,994✔
748
        if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
9,994✔
749
        {
750
            TIFFErrorExtR(tif, module,
1✔
751
                          "Invalid strip byte count %" PRIu64
752
                          ", strip %" PRIu32,
753
                          bytecount, strip);
754
            return (0);
1✔
755
        }
756

757
        /* To avoid excessive memory allocations: */
758
        /* Byte count should normally not be larger than a number of */
759
        /* times the uncompressed size plus some margin */
760
        if (bytecount > 1024 * 1024)
9,993✔
761
        {
762
            /* 10 and 4096 are just values that could be adjusted. */
763
            /* Hopefully they are safe enough for all codecs */
764
            tmsize_t stripsize = TIFFStripSize(tif);
3✔
765
            if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
3✔
766
            {
767
                uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
1✔
768
                TIFFErrorExtR(tif, module,
1✔
769
                              "Too large strip byte count %" PRIu64
770
                              ", strip %" PRIu32 ". Limiting to %" PRIu64,
771
                              bytecount, strip, newbytecount);
772
                bytecount = newbytecount;
1✔
773
            }
774
        }
775

776
        if (isMapped(tif))
9,993✔
777
        {
778
            /*
779
             * We must check for overflow, potentially causing
780
             * an OOB read. Instead of simple
781
             *
782
             *  TIFFGetStrileOffset(tif, strip)+bytecount > tif->tif_size
783
             *
784
             * comparison (which can overflow) we do the following
785
             * two comparisons:
786
             */
787
            if (bytecount > (uint64_t)tif->tif_size ||
4✔
788
                TIFFGetStrileOffset(tif, strip) >
4✔
789
                    (uint64_t)tif->tif_size - bytecount)
4✔
790
            {
791
                /*
792
                 * This error message might seem strange, but
793
                 * it's what would happen if a read were done
794
                 * instead.
795
                 */
796
                TIFFErrorExtR(
4✔
797
                    tif, module,
798

799
                    "Read error on strip %" PRIu32 "; "
800
                    "got %" PRIu64 " bytes, expected %" PRIu64,
801
                    strip,
802
                    NoSanitizeSubUInt64(tif->tif_size,
2✔
803
                                        TIFFGetStrileOffset(tif, strip)),
804
                    bytecount);
805
                tif->tif_curstrip = NOSTRIP;
2✔
806
                return (0);
2✔
807
            }
808
        }
809

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

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

879
            if (isMapped(tif))
9,989✔
880
            {
881
                if (bytecountm > tif->tif_rawdatasize &&
×
882
                    !TIFFReadBufferSetup(tif, 0, bytecountm))
×
883
                {
884
                    return (0);
×
885
                }
886
                if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata, bytecountm,
×
887
                                      module) != bytecountm)
888
                {
889
                    return (0);
×
890
                }
891
            }
892
            else
893
            {
894
                if (TIFFReadRawStripOrTile2(tif, strip, 1, bytecountm,
9,989✔
895
                                            module) != bytecountm)
896
                {
897
                    return (0);
2✔
898
                }
899
            }
900

901
            tif->tif_rawdataoff = 0;
9,987✔
902
            tif->tif_rawdataloaded = bytecountm;
9,987✔
903

904
            if (!isFillOrder(tif, td->td_fillorder) &&
9,987✔
905
                (tif->tif_flags & TIFF_NOBITREV) == 0)
×
906
                TIFFReverseBits(tif->tif_rawdata, bytecountm);
×
907
        }
908
    }
909
    return (TIFFStartStrip(tif, strip));
9,989✔
910
}
911

912
/*
913
 * Tile-oriented Read Support
914
 * Contributed by Nancy Cam (Silicon Graphics).
915
 */
916

917
/*
918
 * Read and decompress a tile of data.  The
919
 * tile is selected by the (x,y,z,s) coordinates.
920
 */
921
tmsize_t TIFFReadTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z,
2✔
922
                      uint16_t s)
923
{
924
    if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
2✔
925
        return ((tmsize_t)(-1));
×
926
    return (TIFFReadEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf,
2✔
927
                                (tmsize_t)(-1)));
928
}
929

930
/*
931
 * Read a tile of data and decompress the specified
932
 * amount into the user-supplied buffer.
933
 */
934
tmsize_t TIFFReadEncodedTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
26,994✔
935
{
936
    static const char module[] = "TIFFReadEncodedTile";
937
    TIFFDirectory *td = &tif->tif_dir;
26,994✔
938
    tmsize_t tilesize = tif->tif_tilesize;
26,994✔
939

940
    if (!TIFFCheckRead(tif, 1))
26,994✔
941
        return ((tmsize_t)(-1));
×
942
    if (tile >= td->td_nstrips)
26,994✔
943
    {
944
        TIFFErrorExtR(tif, module,
×
945
                      "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
946
                      td->td_nstrips);
947
        return ((tmsize_t)(-1));
×
948
    }
949

950
    /* shortcut to avoid an extra memcpy() */
951
    if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
26,994✔
952
        size >= tilesize && !isMapped(tif) &&
21,652✔
953
        ((tif->tif_flags & TIFF_NOREADRAW) == 0))
21,652✔
954
    {
955
        if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize)
21,652✔
956
            return ((tmsize_t)(-1));
1✔
957

958
        if (!isFillOrder(tif, td->td_fillorder) &&
21,651✔
959
            (tif->tif_flags & TIFF_NOBITREV) == 0)
×
960
            TIFFReverseBits(buf, tilesize);
×
961

962
        (*tif->tif_postdecode)(tif, buf, tilesize);
21,651✔
963
        return (tilesize);
21,651✔
964
    }
965

966
    if (size == (tmsize_t)(-1))
5,342✔
967
        size = tilesize;
2✔
968
    else if (size > tilesize)
5,340✔
969
        size = tilesize;
×
970
    if (TIFFFillTile(tif, tile) &&
10,654✔
971
        (*tif->tif_decodetile)(tif, (uint8_t *)buf, size,
5,312✔
972
                               (uint16_t)(tile / td->td_stripsperimage)))
5,312✔
973
    {
974
        (*tif->tif_postdecode)(tif, (uint8_t *)buf, size);
5,312✔
975
        return (size);
5,312✔
976
    }
977
    else
978
        return ((tmsize_t)(-1));
30✔
979
}
980

981
/* Variant of TIFFReadTile() that does
982
 * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
983
 * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case
984
 * of truncated file.
985
 * * calls regular TIFFReadEncodedTile() if *buf != NULL
986
 */
987
tmsize_t _TIFFReadTileAndAllocBuffer(TIFF *tif, void **buf,
4✔
988
                                     tmsize_t bufsizetoalloc, uint32_t x,
989
                                     uint32_t y, uint32_t z, uint16_t s)
990
{
991
    if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
4✔
992
        return ((tmsize_t)(-1));
×
993
    return (_TIFFReadEncodedTileAndAllocBuffer(
4✔
994
        tif, TIFFComputeTile(tif, x, y, z, s), buf, bufsizetoalloc,
995
        (tmsize_t)(-1)));
996
}
997

998
/* Variant of TIFFReadEncodedTile() that does
999
 * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
1000
 * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case
1001
 * of truncated file.
1002
 * * calls regular TIFFReadEncodedTile() if *buf != NULL
1003
 */
1004
tmsize_t _TIFFReadEncodedTileAndAllocBuffer(TIFF *tif, uint32_t tile,
4✔
1005
                                            void **buf, tmsize_t bufsizetoalloc,
1006
                                            tmsize_t size_to_read)
1007
{
1008
    static const char module[] = "_TIFFReadEncodedTileAndAllocBuffer";
1009
    TIFFDirectory *td = &tif->tif_dir;
4✔
1010
    tmsize_t tilesize = tif->tif_tilesize;
4✔
1011

1012
    if (*buf != NULL)
4✔
1013
    {
1014
        return TIFFReadEncodedTile(tif, tile, *buf, size_to_read);
×
1015
    }
1016

1017
    if (!TIFFCheckRead(tif, 1))
4✔
1018
        return ((tmsize_t)(-1));
×
1019
    if (tile >= td->td_nstrips)
4✔
1020
    {
1021
        TIFFErrorExtR(tif, module,
×
1022
                      "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
1023
                      td->td_nstrips);
1024
        return ((tmsize_t)(-1));
×
1025
    }
1026

1027
    if (!TIFFFillTile(tif, tile))
4✔
1028
        return ((tmsize_t)(-1));
1✔
1029

1030
    /* Sanity checks to avoid excessive memory allocation */
1031
    /* Cf https://gitlab.com/libtiff/libtiff/-/issues/479 */
1032
    if (td->td_compression == COMPRESSION_NONE)
3✔
1033
    {
1034
        if (tif->tif_rawdatasize != tilesize)
2✔
1035
        {
1036
            TIFFErrorExtR(tif, TIFFFileName(tif),
×
1037
                          "Invalid tile byte count for tile %u. "
1038
                          "Expected %" PRIu64 ", got %" PRIu64,
1039
                          tile, (uint64_t)tilesize,
1040
                          (uint64_t)tif->tif_rawdatasize);
×
1041
            return ((tmsize_t)(-1));
×
1042
        }
1043
    }
1044
    else
1045
    {
1046
        /* Max compression ratio experimentally determined. Might be fragile...
1047
         * Only apply this heuristics to situations where the memory allocation
1048
         * would be big, to avoid breaking nominal use cases.
1049
         */
1050
        const int maxCompressionRatio =
1✔
1051
            td->td_compression == COMPRESSION_ZSTD ? 33000
1✔
1052
            : td->td_compression == COMPRESSION_JXL
2✔
1053
                ?
1054
                /* Evaluated on a 8000x8000 tile */
1055
                25000 * (td->td_planarconfig == PLANARCONFIG_CONTIG
×
1056
                             ? td->td_samplesperpixel
×
1057
                             : 1)
×
1058
                : td->td_compression == COMPRESSION_LZMA ? 7000 : 1000;
1✔
1059
        if (bufsizetoalloc > 100 * 1000 * 1000 &&
1✔
1060
            tif->tif_rawdatasize < tilesize / maxCompressionRatio)
×
1061
        {
1062
            TIFFErrorExtR(tif, TIFFFileName(tif),
×
1063
                          "Likely invalid tile byte count for tile %u. "
1064
                          "Uncompressed tile size is %" PRIu64 ", "
1065
                          "compressed one is %" PRIu64,
1066
                          tile, (uint64_t)tilesize,
1067
                          (uint64_t)tif->tif_rawdatasize);
×
1068
            return ((tmsize_t)(-1));
×
1069
        }
1070
    }
1071

1072
    *buf = _TIFFmallocExt(tif, bufsizetoalloc);
3✔
1073
    if (*buf == NULL)
3✔
1074
    {
1075
        TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer");
×
1076
        return ((tmsize_t)(-1));
×
1077
    }
1078
    _TIFFmemset(*buf, 0, bufsizetoalloc);
3✔
1079

1080
    if (size_to_read == (tmsize_t)(-1))
3✔
1081
        size_to_read = tilesize;
3✔
1082
    else if (size_to_read > tilesize)
×
1083
        size_to_read = tilesize;
×
1084
    if ((*tif->tif_decodetile)(tif, (uint8_t *)*buf, size_to_read,
3✔
1085
                               (uint16_t)(tile / td->td_stripsperimage)))
3✔
1086
    {
1087
        (*tif->tif_postdecode)(tif, (uint8_t *)*buf, size_to_read);
3✔
1088
        return (size_to_read);
3✔
1089
    }
1090
    else
1091
        return ((tmsize_t)(-1));
×
1092
}
1093

1094
static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf,
21,655✔
1095
                                 tmsize_t size, const char *module)
1096
{
1097
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
21,655✔
1098
    if (!isMapped(tif))
21,655✔
1099
    {
1100
        tmsize_t cc;
1101

1102
        if (!SeekOK(tif, TIFFGetStrileOffset(tif, tile)))
21,655✔
1103
        {
1104
            TIFFErrorExtR(tif, module,
×
1105
                          "Seek error at row %" PRIu32 ", col %" PRIu32
1106
                          ", tile %" PRIu32,
1107
                          tif->tif_row, tif->tif_col, tile);
1108
            return ((tmsize_t)(-1));
×
1109
        }
1110
        cc = TIFFReadFile(tif, buf, size);
21,655✔
1111
        if (cc != size)
21,655✔
1112
        {
1113
            TIFFErrorExtR(tif, module,
1✔
1114
                          "Read error at row %" PRIu32 ", col %" PRIu32
1115
                          "; got %" TIFF_SSIZE_FORMAT
1116
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
1117
                          tif->tif_row, tif->tif_col, cc, size);
1118
            return ((tmsize_t)(-1));
1✔
1119
        }
1120
    }
1121
    else
1122
    {
1123
        tmsize_t ma, mb;
1124
        tmsize_t n;
1125
        ma = (tmsize_t)TIFFGetStrileOffset(tif, tile);
×
1126
        mb = ma + size;
×
1127
        if ((TIFFGetStrileOffset(tif, tile) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
×
1128
            (ma > tif->tif_size))
×
1129
            n = 0;
×
1130
        else if ((mb < ma) || (mb < size) || (mb > tif->tif_size))
×
1131
            n = tif->tif_size - ma;
×
1132
        else
1133
            n = size;
×
1134
        if (n != size)
×
1135
        {
1136
            TIFFErrorExtR(tif, module,
×
1137
                          "Read error at row %" PRIu32 ", col %" PRIu32
1138
                          ", tile %" PRIu32 "; got %" TIFF_SSIZE_FORMAT
1139
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
1140
                          tif->tif_row, tif->tif_col, tile, n, size);
1141
            return ((tmsize_t)(-1));
×
1142
        }
1143
        _TIFFmemcpy(buf, tif->tif_base + ma, size);
×
1144
    }
1145
    return (size);
21,654✔
1146
}
1147

1148
/*
1149
 * Read a tile of data from the file.
1150
 */
1151
tmsize_t TIFFReadRawTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
3✔
1152
{
1153
    static const char module[] = "TIFFReadRawTile";
1154
    TIFFDirectory *td = &tif->tif_dir;
3✔
1155
    uint64_t bytecount64;
1156
    tmsize_t bytecountm;
1157

1158
    if (!TIFFCheckRead(tif, 1))
3✔
1159
        return ((tmsize_t)(-1));
×
1160
    if (tile >= td->td_nstrips)
3✔
1161
    {
1162
        TIFFErrorExtR(tif, module,
×
1163
                      "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
1164
                      td->td_nstrips);
1165
        return ((tmsize_t)(-1));
×
1166
    }
1167
    if (tif->tif_flags & TIFF_NOREADRAW)
3✔
1168
    {
1169
        TIFFErrorExtR(tif, module,
×
1170
                      "Compression scheme does not support access to raw "
1171
                      "uncompressed data");
1172
        return ((tmsize_t)(-1));
×
1173
    }
1174
    bytecount64 = TIFFGetStrileByteCount(tif, tile);
3✔
1175
    if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)
3✔
1176
        bytecountm = size;
3✔
1177
    else
1178
        bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
×
1179
    if (bytecountm == 0)
3✔
1180
    {
1181
        return ((tmsize_t)(-1));
×
1182
    }
1183
    return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
3✔
1184
}
1185

1186
/*
1187
 * Read the specified tile and setup for decoding. The data buffer is
1188
 * expanded, as necessary, to hold the tile's data.
1189
 */
1190
int TIFFFillTile(TIFF *tif, uint32_t tile)
5,346✔
1191
{
1192
    static const char module[] = "TIFFFillTile";
1193
    TIFFDirectory *td = &tif->tif_dir;
5,346✔
1194

1195
    if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
5,346✔
1196
    {
1197
        uint64_t bytecount = TIFFGetStrileByteCount(tif, tile);
5,344✔
1198
        if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
5,344✔
1199
        {
1200
            TIFFErrorExtR(tif, module,
×
1201
                          "%" PRIu64 ": Invalid tile byte count, tile %" PRIu32,
1202
                          bytecount, tile);
1203
            return (0);
×
1204
        }
1205

1206
        /* To avoid excessive memory allocations: */
1207
        /* Byte count should normally not be larger than a number of */
1208
        /* times the uncompressed size plus some margin */
1209
        if (bytecount > 1024 * 1024)
5,344✔
1210
        {
1211
            /* 10 and 4096 are just values that could be adjusted. */
1212
            /* Hopefully they are safe enough for all codecs */
1213
            tmsize_t stripsize = TIFFTileSize(tif);
2✔
1214
            if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
2✔
1215
            {
1216
                uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
1✔
1217
                TIFFErrorExtR(tif, module,
1✔
1218
                              "Too large tile byte count %" PRIu64
1219
                              ", tile %" PRIu32 ". Limiting to %" PRIu64,
1220
                              bytecount, tile, newbytecount);
1221
                bytecount = newbytecount;
1✔
1222
            }
1223
        }
1224

1225
        if (isMapped(tif))
5,344✔
1226
        {
1227
            /*
1228
             * We must check for overflow, potentially causing
1229
             * an OOB read. Instead of simple
1230
             *
1231
             *  TIFFGetStrileOffset(tif, tile)+bytecount > tif->tif_size
1232
             *
1233
             * comparison (which can overflow) we do the following
1234
             * two comparisons:
1235
             */
1236
            if (bytecount > (uint64_t)tif->tif_size ||
4✔
1237
                TIFFGetStrileOffset(tif, tile) >
4✔
1238
                    (uint64_t)tif->tif_size - bytecount)
4✔
1239
            {
1240
                tif->tif_curtile = NOTILE;
2✔
1241
                return (0);
2✔
1242
            }
1243
        }
1244

1245
        if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
5,342✔
1246
                              (tif->tif_flags & TIFF_NOBITREV)))
×
1247
        {
1248
            /*
1249
             * The image is mapped into memory and we either don't
1250
             * need to flip bits or the compression routine is
1251
             * going to handle this operation itself.  In this
1252
             * case, avoid copying the raw data and instead just
1253
             * reference the data from the memory mapped file
1254
             * image.  This assumes that the decompression
1255
             * routines do not modify the contents of the raw data
1256
             * buffer (if they try to, the application will get a
1257
             * fault since the file is mapped read-only).
1258
             */
1259
            if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
2✔
1260
            {
1261
                _TIFFfreeExt(tif, tif->tif_rawdata);
×
1262
                tif->tif_rawdata = NULL;
×
1263
                tif->tif_rawdatasize = 0;
×
1264
            }
1265
            tif->tif_flags &= ~TIFF_MYBUFFER;
2✔
1266

1267
            tif->tif_rawdatasize = (tmsize_t)bytecount;
2✔
1268
            tif->tif_rawdata =
2✔
1269
                tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, tile);
2✔
1270
            tif->tif_rawdataoff = 0;
2✔
1271
            tif->tif_rawdataloaded = (tmsize_t)bytecount;
2✔
1272
            tif->tif_flags |= TIFF_BUFFERMMAP;
2✔
1273
        }
1274
        else
1275
        {
1276
            /*
1277
             * Expand raw data buffer, if needed, to hold data
1278
             * tile coming from file (perhaps should set upper
1279
             * bound on the size of a buffer we'll use?).
1280
             */
1281
            tmsize_t bytecountm;
1282
            bytecountm = (tmsize_t)bytecount;
5,340✔
1283
            if ((uint64_t)bytecountm != bytecount)
5,340✔
1284
            {
1285
                TIFFErrorExtR(tif, module, "Integer overflow");
×
1286
                return (0);
×
1287
            }
1288
            if (bytecountm > tif->tif_rawdatasize)
5,340✔
1289
            {
1290
                tif->tif_curtile = NOTILE;
738✔
1291
                if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
738✔
1292
                {
1293
                    TIFFErrorExtR(tif, module,
×
1294
                                  "Data buffer too small to hold tile %" PRIu32,
1295
                                  tile);
1296
                    return (0);
×
1297
                }
1298
            }
1299
            if (tif->tif_flags & TIFF_BUFFERMMAP)
5,340✔
1300
            {
1301
                tif->tif_curtile = NOTILE;
×
1302
                tif->tif_rawdata = NULL;
×
1303
                tif->tif_rawdatasize = 0;
×
1304
                tif->tif_flags &= ~TIFF_BUFFERMMAP;
×
1305
            }
1306

1307
            if (isMapped(tif))
5,340✔
1308
            {
1309
                if (bytecountm > tif->tif_rawdatasize &&
×
1310
                    !TIFFReadBufferSetup(tif, 0, bytecountm))
×
1311
                {
1312
                    return (0);
×
1313
                }
1314
                if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata, bytecountm,
×
1315
                                     module) != bytecountm)
1316
                {
1317
                    return (0);
×
1318
                }
1319
            }
1320
            else
1321
            {
1322
                if (TIFFReadRawStripOrTile2(tif, tile, 0, bytecountm, module) !=
5,340✔
1323
                    bytecountm)
1324
                {
1325
                    return (0);
27✔
1326
                }
1327
            }
1328

1329
            tif->tif_rawdataoff = 0;
5,313✔
1330
            tif->tif_rawdataloaded = bytecountm;
5,313✔
1331

1332
            if (tif->tif_rawdata != NULL &&
5,313✔
1333
                !isFillOrder(tif, td->td_fillorder) &&
5,313✔
1334
                (tif->tif_flags & TIFF_NOBITREV) == 0)
×
1335
                TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdataloaded);
×
1336
        }
1337
    }
1338
    return (TIFFStartTile(tif, tile));
5,317✔
1339
}
1340

1341
/*
1342
 * Setup the raw data buffer in preparation for
1343
 * reading a strip of raw data.  If the buffer
1344
 * is specified as zero, then a buffer of appropriate
1345
 * size is allocated by the library.  Otherwise,
1346
 * the client must guarantee that the buffer is
1347
 * large enough to hold any individual strip of
1348
 * raw data.
1349
 */
1350
int TIFFReadBufferSetup(TIFF *tif, void *bp, tmsize_t size)
×
1351
{
1352
    static const char module[] = "TIFFReadBufferSetup";
1353

1354
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
×
1355
    tif->tif_flags &= ~TIFF_BUFFERMMAP;
×
1356

1357
    if (tif->tif_rawdata)
×
1358
    {
1359
        if (tif->tif_flags & TIFF_MYBUFFER)
×
1360
            _TIFFfreeExt(tif, tif->tif_rawdata);
×
1361
        tif->tif_rawdata = NULL;
×
1362
        tif->tif_rawdatasize = 0;
×
1363
    }
1364
    if (bp)
×
1365
    {
1366
        tif->tif_rawdatasize = size;
×
1367
        tif->tif_rawdata = (uint8_t *)bp;
×
1368
        tif->tif_flags &= ~TIFF_MYBUFFER;
×
1369
    }
1370
    else
1371
    {
1372
        tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64_t)size, 1024);
×
1373
        if (tif->tif_rawdatasize == 0)
×
1374
        {
1375
            TIFFErrorExtR(tif, module, "Invalid buffer size");
×
1376
            return (0);
×
1377
        }
1378
        /* Initialize to zero to avoid uninitialized buffers in case of */
1379
        /* short reads (http://bugzilla.maptools.org/show_bug.cgi?id=2651) */
1380
        tif->tif_rawdata =
×
1381
            (uint8_t *)_TIFFcallocExt(tif, 1, tif->tif_rawdatasize);
×
1382
        tif->tif_flags |= TIFF_MYBUFFER;
×
1383
    }
1384
    if (tif->tif_rawdata == NULL)
×
1385
    {
1386
        TIFFErrorExtR(tif, module,
×
1387
                      "No space for data buffer at scanline %" PRIu32,
1388
                      tif->tif_row);
1389
        tif->tif_rawdatasize = 0;
×
1390
        return (0);
×
1391
    }
1392
    return (1);
×
1393
}
1394

1395
/*
1396
 * Set state to appear as if a
1397
 * strip has just been read in.
1398
 */
1399
static int TIFFStartStrip(TIFF *tif, uint32_t strip)
12,404✔
1400
{
1401
    TIFFDirectory *td = &tif->tif_dir;
12,404✔
1402

1403
    if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
12,404✔
1404
    {
1405
        if (!(*tif->tif_setupdecode)(tif))
3,032✔
1406
            return (0);
1✔
1407
        tif->tif_flags |= TIFF_CODERSETUP;
3,031✔
1408
    }
1409
    tif->tif_curstrip = strip;
12,403✔
1410
    tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
12,403✔
1411
    tif->tif_flags &= ~TIFF_BUF4WRITE;
12,403✔
1412

1413
    if (tif->tif_flags & TIFF_NOREADRAW)
12,403✔
1414
    {
1415
        tif->tif_rawcp = NULL;
×
1416
        tif->tif_rawcc = 0;
×
1417
    }
1418
    else
1419
    {
1420
        tif->tif_rawcp = tif->tif_rawdata;
12,403✔
1421
        if (tif->tif_rawdataloaded > 0)
12,403✔
1422
            tif->tif_rawcc = tif->tif_rawdataloaded;
12,401✔
1423
        else
1424
            tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, strip);
2✔
1425
    }
1426
    if ((*tif->tif_predecode)(tif, (uint16_t)(strip / td->td_stripsperimage)) ==
12,401✔
1427
        0)
1428
    {
1429
        /* Needed for example for scanline access, if tif_predecode */
1430
        /* fails, and we try to read the same strip again. Without invalidating
1431
         */
1432
        /* tif_curstrip, we'd call tif_decoderow() on a possibly invalid */
1433
        /* codec state. */
1434
        tif->tif_curstrip = NOSTRIP;
2✔
1435
        return 0;
2✔
1436
    }
1437
    return 1;
12,401✔
1438
}
1439

1440
/*
1441
 * Set state to appear as if a
1442
 * tile has just been read in.
1443
 */
1444
static int TIFFStartTile(TIFF *tif, uint32_t tile)
5,531✔
1445
{
1446
    static const char module[] = "TIFFStartTile";
1447
    TIFFDirectory *td = &tif->tif_dir;
5,531✔
1448
    uint32_t howmany32;
1449

1450
    if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
5,531✔
1451
    {
1452
        if (!(*tif->tif_setupdecode)(tif))
776✔
1453
            return (0);
×
1454
        tif->tif_flags |= TIFF_CODERSETUP;
776✔
1455
    }
1456
    tif->tif_curtile = tile;
5,531✔
1457
    if (td->td_tilewidth == 0)
5,531✔
1458
    {
1459
        TIFFErrorExtR(tif, module, "Zero tilewidth");
×
1460
        return 0;
×
1461
    }
1462
    howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
5,531✔
1463
    if (howmany32 == 0)
5,531✔
1464
    {
1465
        TIFFErrorExtR(tif, module, "Zero tiles");
×
1466
        return 0;
×
1467
    }
1468
    tif->tif_row = (tile % howmany32) * td->td_tilelength;
5,531✔
1469
    howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
5,531✔
1470
    if (howmany32 == 0)
5,531✔
1471
    {
1472
        TIFFErrorExtR(tif, module, "Zero tiles");
×
1473
        return 0;
×
1474
    }
1475
    tif->tif_col = (tile % howmany32) * td->td_tilewidth;
5,531✔
1476
    tif->tif_flags &= ~TIFF_BUF4WRITE;
5,531✔
1477
    if (tif->tif_flags & TIFF_NOREADRAW)
5,531✔
1478
    {
1479
        tif->tif_rawcp = NULL;
2✔
1480
        tif->tif_rawcc = 0;
2✔
1481
    }
1482
    else
1483
    {
1484
        tif->tif_rawcp = tif->tif_rawdata;
5,529✔
1485
        if (tif->tif_rawdataloaded > 0)
5,529✔
1486
            tif->tif_rawcc = tif->tif_rawdataloaded;
5,529✔
1487
        else
1488
            tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, tile);
×
1489
    }
1490
    return (
1491
        (*tif->tif_predecode)(tif, (uint16_t)(tile / td->td_stripsperimage)));
5,531✔
1492
}
1493

1494
static int TIFFCheckRead(TIFF *tif, int tiles)
2,250,530✔
1495
{
1496
    if (tif->tif_mode == O_WRONLY)
2,250,530✔
1497
    {
1498
        TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
×
1499
        return (0);
×
1500
    }
1501
    if (tiles ^ isTiled(tif))
2,250,530✔
1502
    {
1503
        TIFFErrorExtR(tif, tif->tif_name,
×
1504
                      tiles ? "Can not read tiles from a striped image"
1505
                            : "Can not read scanlines from a tiled image");
1506
        return (0);
×
1507
    }
1508
    return (1);
2,250,530✔
1509
}
1510

1511
/* Use the provided input buffer (inbuf, insize) and decompress it into
1512
 * (outbuf, outsize).
1513
 * This function replaces the use of
1514
 * TIFFReadEncodedStrip()/TIFFReadEncodedTile() when the user can provide the
1515
 * buffer for the input data, for example when he wants to avoid libtiff to read
1516
 * the strile offset/count values from the [Strip|Tile][Offsets/ByteCounts]
1517
 * array. inbuf content must be writable (if bit reversal is needed) Returns 1
1518
 * in case of success, 0 otherwise.
1519
 */
1520
int TIFFReadFromUserBuffer(TIFF *tif, uint32_t strile, void *inbuf,
2,572✔
1521
                           tmsize_t insize, void *outbuf, tmsize_t outsize)
1522
{
1523
    static const char module[] = "TIFFReadFromUserBuffer";
1524
    TIFFDirectory *td = &tif->tif_dir;
2,572✔
1525
    int ret = 1;
2,572✔
1526
    uint32_t old_tif_flags = tif->tif_flags;
2,572✔
1527
    tmsize_t old_rawdatasize = tif->tif_rawdatasize;
2,572✔
1528
    void *old_rawdata = tif->tif_rawdata;
2,572✔
1529

1530
    if (tif->tif_mode == O_WRONLY)
2,572✔
1531
    {
1532
        TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
×
1533
        return 0;
×
1534
    }
1535
    if (tif->tif_flags & TIFF_NOREADRAW)
2,572✔
1536
    {
1537
        TIFFErrorExtR(tif, module,
×
1538
                      "Compression scheme does not support access to raw "
1539
                      "uncompressed data");
1540
        return 0;
×
1541
    }
1542

1543
    tif->tif_flags &= ~TIFF_MYBUFFER;
2,572✔
1544
    tif->tif_flags |= TIFF_BUFFERMMAP;
2,572✔
1545
    tif->tif_rawdatasize = insize;
2,572✔
1546
    tif->tif_rawdata = inbuf;
2,572✔
1547
    tif->tif_rawdataoff = 0;
2,572✔
1548
    tif->tif_rawdataloaded = insize;
2,572✔
1549

1550
    if (!isFillOrder(tif, td->td_fillorder) &&
2,572✔
1551
        (tif->tif_flags & TIFF_NOBITREV) == 0)
×
1552
    {
1553
        TIFFReverseBits(inbuf, insize);
×
1554
    }
1555

1556
    if (TIFFIsTiled(tif))
2,572✔
1557
    {
1558
        if (!TIFFStartTile(tif, strile) ||
430✔
1559
            !(*tif->tif_decodetile)(tif, (uint8_t *)outbuf, outsize,
214✔
1560
                                    (uint16_t)(strile / td->td_stripsperimage)))
214✔
1561
        {
1562
            ret = 0;
×
1563
        }
1564
    }
1565
    else
1566
    {
1567
        uint32_t rowsperstrip = td->td_rowsperstrip;
2,352✔
1568
        uint32_t stripsperplane;
1569
        if (rowsperstrip > td->td_imagelength)
2,352✔
1570
            rowsperstrip = td->td_imagelength;
×
1571
        if (rowsperstrip == 0)
2,352✔
1572
        {
1573
            TIFFErrorExtR(tif, module, "rowsperstrip is zero");
×
1574
            ret = 0;
×
1575
        }
1576
        else
1577
        {
1578
            stripsperplane =
2,352✔
1579
                TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
2,352✔
1580
            if (!TIFFStartStrip(tif, strile) ||
4,706✔
1581
                !(*tif->tif_decodestrip)(tif, (uint8_t *)outbuf, outsize,
2,355✔
1582
                                         (uint16_t)(strile / stripsperplane)))
2,355✔
1583
            {
1584
                ret = 0;
×
1585
            }
1586
        }
1587
    }
1588
    if (ret)
2,569✔
1589
    {
1590
        (*tif->tif_postdecode)(tif, (uint8_t *)outbuf, outsize);
2,569✔
1591
    }
1592

1593
    if (!isFillOrder(tif, td->td_fillorder) &&
2,570✔
1594
        (tif->tif_flags & TIFF_NOBITREV) == 0)
×
1595
    {
1596
        TIFFReverseBits(inbuf, insize);
×
1597
    }
1598

1599
    tif->tif_flags = (old_tif_flags & (TIFF_MYBUFFER | TIFF_BUFFERMMAP)) |
2,569✔
1600
                     (tif->tif_flags & ~(TIFF_MYBUFFER | TIFF_BUFFERMMAP));
2,569✔
1601
    tif->tif_rawdatasize = old_rawdatasize;
2,569✔
1602
    tif->tif_rawdata = old_rawdata;
2,569✔
1603
    tif->tif_rawdataoff = 0;
2,569✔
1604
    tif->tif_rawdataloaded = 0;
2,569✔
1605

1606
    return ret;
2,569✔
1607
}
1608

1609
void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
2,474,310✔
1610
{
1611
    (void)tif;
1612
    (void)buf;
1613
    (void)cc;
1614
}
2,474,310✔
1615

1616
void _TIFFSwab16BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
1,943✔
1617
{
1618
    (void)tif;
1619
    assert((cc & 1) == 0);
1,943✔
1620
    TIFFSwabArrayOfShort((uint16_t *)buf, cc / 2);
1,943✔
1621
}
1,943✔
1622

1623
void _TIFFSwab24BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
×
1624
{
1625
    (void)tif;
1626
    assert((cc % 3) == 0);
×
1627
    TIFFSwabArrayOfTriples((uint8_t *)buf, cc / 3);
×
1628
}
×
1629

1630
void _TIFFSwab32BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
188✔
1631
{
1632
    (void)tif;
1633
    assert((cc & 3) == 0);
188✔
1634
    TIFFSwabArrayOfLong((uint32_t *)buf, cc / 4);
188✔
1635
}
188✔
1636

1637
void _TIFFSwab64BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
4✔
1638
{
1639
    (void)tif;
1640
    assert((cc & 7) == 0);
4✔
1641
    TIFFSwabArrayOfDouble((double *)buf, cc / 8);
4✔
1642
}
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