Coverage Report

Created: 2025-06-12 06:52

/src/opencv/3rdparty/libtiff/tif_read.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1988-1997 Sam Leffler
3
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software and
6
 * its documentation for any purpose is hereby granted without fee, provided
7
 * that (i) the above copyright notices and this permission notice appear in
8
 * all copies of the software and related documentation, and (ii) the names of
9
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10
 * publicity relating to the software without the specific, prior written
11
 * permission of Sam Leffler and Silicon Graphics.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22
 * OF THIS SOFTWARE.
23
 */
24
25
/*
26
 * TIFF Library.
27
 * Scanline-oriented Read Support
28
 */
29
#include "tiffiop.h"
30
#include <stdio.h>
31
32
int TIFFFillStrip(TIFF *tif, uint32_t strip);
33
int TIFFFillTile(TIFF *tif, uint32_t tile);
34
static int TIFFStartStrip(TIFF *tif, uint32_t strip);
35
static int TIFFStartTile(TIFF *tif, uint32_t tile);
36
static int TIFFCheckRead(TIFF *, int);
37
static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf,
38
                                  tmsize_t size, const char *module);
39
static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf,
40
                                 tmsize_t size, const char *module);
41
42
4.16k
#define NOSTRIP ((uint32_t)(-1)) /* undefined state */
43
6.17k
#define NOTILE ((uint32_t)(-1))  /* undefined state */
44
45
408k
#define INITIAL_THRESHOLD (1024 * 1024)
46
384
#define THRESHOLD_MULTIPLIER 10
47
#define MAX_THRESHOLD                                                          \
48
408k
    (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER *      \
49
408k
     INITIAL_THRESHOLD)
50
51
840k
#define TIFF_INT64_MAX ((((int64_t)0x7FFFFFFF) << 32) | 0xFFFFFFFF)
52
53
/* Read 'size' bytes in tif_rawdata buffer starting at offset 'rawdata_offset'
54
 * Returns 1 in case of success, 0 otherwise. */
55
static int TIFFReadAndRealloc(TIFF *tif, tmsize_t size, tmsize_t rawdata_offset,
56
                              int is_strip, uint32_t strip_or_tile,
57
                              const char *module)
58
408k
{
59
408k
#if SIZEOF_SIZE_T == 8
60
408k
    tmsize_t threshold = INITIAL_THRESHOLD;
61
408k
#endif
62
408k
    tmsize_t already_read = 0;
63
64
#if SIZEOF_SIZE_T != 8
65
    /* On 32 bit processes, if the request is large enough, check against */
66
    /* file size */
67
    if (size > 1000 * 1000 * 1000)
68
    {
69
        uint64_t filesize = TIFFGetFileSize(tif);
70
        if ((uint64_t)size >= filesize)
71
        {
72
            TIFFErrorExtR(tif, module,
73
                          "Chunk size requested is larger than file size.");
74
            return 0;
75
        }
76
    }
77
#endif
78
79
    /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
80
    /* so as to avoid allocating too much memory in case the file is too */
81
    /* short. We could ask for the file size, but this might be */
82
    /* expensive with some I/O layers (think of reading a gzipped file) */
83
    /* Restrict to 64 bit processes, so as to avoid reallocs() */
84
    /* on 32 bit processes where virtual memory is scarce.  */
85
814k
    while (already_read < size)
86
408k
    {
87
408k
        tmsize_t bytes_read;
88
408k
        tmsize_t to_read = size - already_read;
89
408k
#if SIZEOF_SIZE_T == 8
90
408k
        if (to_read >= threshold && threshold < MAX_THRESHOLD &&
91
408k
            already_read + to_read + rawdata_offset > tif->tif_rawdatasize)
92
96
        {
93
96
            to_read = threshold;
94
96
            threshold *= THRESHOLD_MULTIPLIER;
95
96
        }
96
408k
#endif
97
408k
        if (already_read + to_read + rawdata_offset > tif->tif_rawdatasize)
98
7.24k
        {
99
7.24k
            uint8_t *new_rawdata;
100
7.24k
            assert((tif->tif_flags & TIFF_MYBUFFER) != 0);
101
7.24k
            tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64(
102
7.24k
                (uint64_t)already_read + to_read + rawdata_offset, 1024);
103
7.24k
            if (tif->tif_rawdatasize == 0)
104
0
            {
105
0
                TIFFErrorExtR(tif, module, "Invalid buffer size");
106
0
                return 0;
107
0
            }
108
7.24k
            new_rawdata =
109
7.24k
                (uint8_t *)_TIFFrealloc(tif->tif_rawdata, tif->tif_rawdatasize);
110
7.24k
            if (new_rawdata == 0)
111
0
            {
112
0
                TIFFErrorExtR(tif, module,
113
0
                              "No space for data buffer at scanline %" PRIu32,
114
0
                              tif->tif_row);
115
0
                _TIFFfreeExt(tif, tif->tif_rawdata);
116
0
                tif->tif_rawdata = 0;
117
0
                tif->tif_rawdatasize = 0;
118
0
                return 0;
119
0
            }
120
7.24k
            tif->tif_rawdata = new_rawdata;
121
7.24k
        }
122
408k
        if (tif->tif_rawdata == NULL)
123
0
        {
124
            /* should not happen in practice but helps CoverityScan */
125
0
            return 0;
126
0
        }
127
128
408k
        bytes_read = TIFFReadFile(
129
408k
            tif, tif->tif_rawdata + rawdata_offset + already_read, to_read);
130
408k
        already_read += bytes_read;
131
408k
        if (bytes_read != to_read)
132
2.16k
        {
133
2.16k
            memset(tif->tif_rawdata + rawdata_offset + already_read, 0,
134
2.16k
                   tif->tif_rawdatasize - rawdata_offset - already_read);
135
2.16k
            if (is_strip)
136
538
            {
137
538
                TIFFErrorExtR(tif, module,
138
538
                              "Read error at scanline %" PRIu32
139
538
                              "; got %" TIFF_SSIZE_FORMAT " bytes, "
140
538
                              "expected %" TIFF_SSIZE_FORMAT,
141
538
                              tif->tif_row, already_read, size);
142
538
            }
143
1.62k
            else
144
1.62k
            {
145
1.62k
                TIFFErrorExtR(tif, module,
146
1.62k
                              "Read error at row %" PRIu32 ", col %" PRIu32
147
1.62k
                              ", tile %" PRIu32 "; "
148
1.62k
                              "got %" TIFF_SSIZE_FORMAT
149
1.62k
                              " bytes, expected %" TIFF_SSIZE_FORMAT "",
150
1.62k
                              tif->tif_row, tif->tif_col, strip_or_tile,
151
1.62k
                              already_read, size);
152
1.62k
            }
153
2.16k
            return 0;
154
2.16k
        }
155
408k
    }
156
406k
    return 1;
157
408k
}
158
159
static int TIFFFillStripPartial(TIFF *tif, int strip, tmsize_t read_ahead,
160
                                int restart)
161
0
{
162
0
    static const char module[] = "TIFFFillStripPartial";
163
0
    register TIFFDirectory *td = &tif->tif_dir;
164
0
    tmsize_t unused_data;
165
0
    uint64_t read_offset;
166
0
    tmsize_t to_read;
167
0
    tmsize_t read_ahead_mod;
168
    /* tmsize_t bytecountm; */
169
170
    /*
171
     * Expand raw data buffer, if needed, to hold data
172
     * strip coming from file (perhaps should set upper
173
     * bound on the size of a buffer we'll use?).
174
     */
175
176
    /* bytecountm=(tmsize_t) TIFFGetStrileByteCount(tif, strip); */
177
178
    /* Not completely sure where the * 2 comes from, but probably for */
179
    /* an exponentional growth strategy of tif_rawdatasize */
180
0
    if (read_ahead < TIFF_TMSIZE_T_MAX / 2)
181
0
        read_ahead_mod = read_ahead * 2;
182
0
    else
183
0
        read_ahead_mod = read_ahead;
184
0
    if (read_ahead_mod > tif->tif_rawdatasize)
185
0
    {
186
0
        assert(restart);
187
188
0
        tif->tif_curstrip = NOSTRIP;
189
0
        if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
190
0
        {
191
0
            TIFFErrorExtR(tif, module,
192
0
                          "Data buffer too small to hold part of strip %d",
193
0
                          strip);
194
0
            return (0);
195
0
        }
196
0
    }
197
198
0
    if (restart)
199
0
    {
200
0
        tif->tif_rawdataloaded = 0;
201
0
        tif->tif_rawdataoff = 0;
202
0
    }
203
204
    /*
205
    ** If we are reading more data, move any unused data to the
206
    ** start of the buffer.
207
    */
208
0
    if (tif->tif_rawdataloaded > 0)
209
0
        unused_data =
210
0
            tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);
211
0
    else
212
0
        unused_data = 0;
213
214
0
    if (unused_data > 0)
215
0
    {
216
0
        assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);
217
0
        memmove(tif->tif_rawdata, tif->tif_rawcp, unused_data);
218
0
    }
219
220
    /*
221
    ** Seek to the point in the file where more data should be read.
222
    */
223
0
    read_offset = TIFFGetStrileOffset(tif, strip) + tif->tif_rawdataoff +
224
0
                  tif->tif_rawdataloaded;
225
226
0
    if (!SeekOK(tif, read_offset))
227
0
    {
228
0
        TIFFErrorExtR(tif, module,
229
0
                      "Seek error at scanline %" PRIu32 ", strip %d",
230
0
                      tif->tif_row, strip);
231
0
        return 0;
232
0
    }
233
234
    /*
235
    ** How much do we want to read?
236
    */
237
0
    if (read_ahead_mod > tif->tif_rawdatasize)
238
0
        to_read = read_ahead_mod - unused_data;
239
0
    else
240
0
        to_read = tif->tif_rawdatasize - unused_data;
241
0
    if ((uint64_t)to_read > TIFFGetStrileByteCount(tif, strip) -
242
0
                                tif->tif_rawdataoff - tif->tif_rawdataloaded)
243
0
    {
244
0
        to_read = (tmsize_t)TIFFGetStrileByteCount(tif, strip) -
245
0
                  tif->tif_rawdataoff - tif->tif_rawdataloaded;
246
0
    }
247
248
0
    assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);
249
0
    if (!TIFFReadAndRealloc(tif, to_read, unused_data, 1, /* is_strip */
250
0
                            0,                            /* strip_or_tile */
251
0
                            module))
252
0
    {
253
0
        return 0;
254
0
    }
255
256
0
    tif->tif_rawdataoff =
257
0
        tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data;
258
0
    tif->tif_rawdataloaded = unused_data + to_read;
259
260
0
    tif->tif_rawcc = tif->tif_rawdataloaded;
261
0
    tif->tif_rawcp = tif->tif_rawdata;
262
263
0
    if (!isFillOrder(tif, td->td_fillorder) &&
264
0
        (tif->tif_flags & TIFF_NOBITREV) == 0)
265
0
    {
266
0
        assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);
267
0
        TIFFReverseBits(tif->tif_rawdata + unused_data, to_read);
268
0
    }
269
270
    /*
271
    ** When starting a strip from the beginning we need to
272
    ** restart the decoder.
273
    */
274
0
    if (restart)
275
0
    {
276
277
0
#ifdef JPEG_SUPPORT
278
        /* A bit messy since breaks the codec abstraction. Ultimately */
279
        /* there should be a function pointer for that, but it seems */
280
        /* only JPEG is affected. */
281
        /* For JPEG, if there are multiple scans (can generally be known */
282
        /* with the  read_ahead used), we need to read the whole strip */
283
0
        if (tif->tif_dir.td_compression == COMPRESSION_JPEG &&
284
0
            (uint64_t)tif->tif_rawcc < TIFFGetStrileByteCount(tif, strip))
285
0
        {
286
0
            if (TIFFJPEGIsFullStripRequired(tif))
287
0
            {
288
0
                return TIFFFillStrip(tif, strip);
289
0
            }
290
0
        }
291
0
#endif
292
293
0
        return TIFFStartStrip(tif, strip);
294
0
    }
295
0
    else
296
0
    {
297
0
        return 1;
298
0
    }
299
0
}
300
301
/*
302
 * Seek to a random row+sample in a file.
303
 *
304
 * Only used by TIFFReadScanline, and is only used on
305
 * strip organized files.  We do some tricky stuff to try
306
 * and avoid reading the whole compressed raw data for big
307
 * strips.
308
 */
309
static int TIFFSeek(TIFF *tif, uint32_t row, uint16_t sample)
310
609
{
311
609
    register TIFFDirectory *td = &tif->tif_dir;
312
609
    uint32_t strip;
313
609
    int whole_strip;
314
609
    tmsize_t read_ahead = 0;
315
316
    /*
317
    ** Establish what strip we are working from.
318
    */
319
609
    if (row >= td->td_imagelength)
320
0
    { /* out of range */
321
0
        TIFFErrorExtR(tif, tif->tif_name,
322
0
                      "%" PRIu32 ": Row out of range, max %" PRIu32 "", row,
323
0
                      td->td_imagelength);
324
0
        return (0);
325
0
    }
326
609
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
327
0
    {
328
0
        if (sample >= td->td_samplesperpixel)
329
0
        {
330
0
            TIFFErrorExtR(tif, tif->tif_name,
331
0
                          "%" PRIu16 ": Sample out of range, max %" PRIu16 "",
332
0
                          sample, td->td_samplesperpixel);
333
0
            return (0);
334
0
        }
335
0
        strip = (uint32_t)sample * td->td_stripsperimage +
336
0
                row / td->td_rowsperstrip;
337
0
    }
338
609
    else
339
609
        strip = row / td->td_rowsperstrip;
340
341
        /*
342
         * Do we want to treat this strip as one whole chunk or
343
         * read it a few lines at a time?
344
         */
345
#if defined(CHUNKY_STRIP_READ_SUPPORT)
346
    whole_strip = TIFFGetStrileByteCount(tif, strip) < 10 || isMapped(tif);
347
    if (td->td_compression == COMPRESSION_LERC ||
348
        td->td_compression == COMPRESSION_JBIG)
349
    {
350
        /* Ideally plugins should have a way to declare they don't support
351
         * chunk strip */
352
        whole_strip = 1;
353
    }
354
#else
355
609
    whole_strip = 1;
356
609
#endif
357
358
609
    if (!whole_strip)
359
0
    {
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
0
        if (tif->tif_scanlinesize < TIFF_TMSIZE_T_MAX / 16 &&
364
0
            tif->tif_scanlinesize * 16 < TIFF_TMSIZE_T_MAX - 5000)
365
0
        {
366
0
            read_ahead = tif->tif_scanlinesize * 16 + 5000;
367
0
        }
368
0
        else
369
0
        {
370
0
            read_ahead = tif->tif_scanlinesize;
371
0
        }
372
0
    }
373
374
    /*
375
     * If we haven't loaded this strip, do so now, possibly
376
     * only reading the first part.
377
     */
378
609
    if (strip != tif->tif_curstrip)
379
41
    { /* different strip, refill */
380
381
41
        if (whole_strip)
382
41
        {
383
41
            if (!TIFFFillStrip(tif, strip))
384
11
                return (0);
385
41
        }
386
0
        else
387
0
        {
388
0
            if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))
389
0
                return 0;
390
0
        }
391
41
    }
392
393
    /*
394
    ** If we already have some data loaded, do we need to read some more?
395
    */
396
568
    else if (!whole_strip)
397
0
    {
398
0
        if (((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) <
399
0
                read_ahead &&
400
0
            (uint64_t)tif->tif_rawdataoff + tif->tif_rawdataloaded <
401
0
                TIFFGetStrileByteCount(tif, strip))
402
0
        {
403
0
            if (!TIFFFillStripPartial(tif, strip, read_ahead, 0))
404
0
                return 0;
405
0
        }
406
0
    }
407
408
598
    if (row < tif->tif_row)
409
0
    {
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
0
        if (tif->tif_rawdataoff != 0)
420
0
        {
421
0
            if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))
422
0
                return 0;
423
0
        }
424
0
        else
425
0
        {
426
0
            if (!TIFFStartStrip(tif, strip))
427
0
                return (0);
428
0
        }
429
0
    }
430
431
598
    if (row != tif->tif_row)
432
0
    {
433
        /*
434
         * Seek forward to the desired row.
435
         */
436
437
        /* TODO: Will this really work with partial buffers? */
438
439
0
        if (!(*tif->tif_seek)(tif, row - tif->tif_row))
440
0
            return (0);
441
0
        tif->tif_row = row;
442
0
    }
443
444
598
    return (1);
445
598
}
446
447
int TIFFReadScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample)
448
609
{
449
609
    int e;
450
451
609
    if (!TIFFCheckRead(tif, 0))
452
0
        return (-1);
453
609
    if ((e = TIFFSeek(tif, row, sample)) != 0)
454
598
    {
455
        /*
456
         * Decompress desired row into user buffer.
457
         */
458
598
        e = (*tif->tif_decoderow)(tif, (uint8_t *)buf, tif->tif_scanlinesize,
459
598
                                  sample);
460
461
        /* we are now poised at the beginning of the next row */
462
598
        tif->tif_row = row + 1;
463
464
598
        if (e)
465
568
            (*tif->tif_postdecode)(tif, (uint8_t *)buf, tif->tif_scanlinesize);
466
598
    }
467
609
    return (e > 0 ? 1 : -1);
468
609
}
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,
476
                                                 uint16_t *pplane)
477
747k
{
478
747k
    static const char module[] = "TIFFReadEncodedStrip";
479
747k
    TIFFDirectory *td = &tif->tif_dir;
480
747k
    uint32_t rowsperstrip;
481
747k
    uint32_t stripsperplane;
482
747k
    uint32_t stripinplane;
483
747k
    uint32_t rows;
484
747k
    tmsize_t stripsize;
485
747k
    if (!TIFFCheckRead(tif, 0))
486
0
        return ((tmsize_t)(-1));
487
747k
    if (strip >= td->td_nstrips)
488
0
    {
489
0
        TIFFErrorExtR(tif, module,
490
0
                      "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,
491
0
                      td->td_nstrips);
492
0
        return ((tmsize_t)(-1));
493
0
    }
494
495
747k
    rowsperstrip = td->td_rowsperstrip;
496
747k
    if (rowsperstrip > td->td_imagelength)
497
953
        rowsperstrip = td->td_imagelength;
498
747k
    stripsperplane =
499
747k
        TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
500
747k
    stripinplane = (strip % stripsperplane);
501
747k
    if (pplane)
502
747k
        *pplane = (uint16_t)(strip / stripsperplane);
503
747k
    rows = td->td_imagelength - stripinplane * rowsperstrip;
504
747k
    if (rows > rowsperstrip)
505
744k
        rows = rowsperstrip;
506
747k
    stripsize = TIFFVStripSize(tif, rows);
507
747k
    if (stripsize == 0)
508
0
        return ((tmsize_t)(-1));
509
747k
    return stripsize;
510
747k
}
511
512
/*
513
 * Read a strip of data and decompress the specified
514
 * amount into the user-supplied buffer.
515
 */
516
tmsize_t TIFFReadEncodedStrip(TIFF *tif, uint32_t strip, void *buf,
517
                              tmsize_t size)
518
613k
{
519
613k
    static const char module[] = "TIFFReadEncodedStrip";
520
613k
    TIFFDirectory *td = &tif->tif_dir;
521
613k
    tmsize_t stripsize;
522
613k
    uint16_t plane;
523
524
613k
    stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
525
613k
    if (stripsize == ((tmsize_t)(-1)))
526
0
        return ((tmsize_t)(-1));
527
528
    /* shortcut to avoid an extra memcpy() */
529
613k
    if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
530
613k
        size >= stripsize && !isMapped(tif) &&
531
613k
        ((tif->tif_flags & TIFF_NOREADRAW) == 0))
532
589k
    {
533
589k
        if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize)
534
1.27k
            return ((tmsize_t)(-1));
535
536
587k
        if (!isFillOrder(tif, td->td_fillorder) &&
537
587k
            (tif->tif_flags & TIFF_NOBITREV) == 0)
538
515
            TIFFReverseBits(buf, stripsize);
539
540
587k
        (*tif->tif_postdecode)(tif, buf, stripsize);
541
587k
        return (stripsize);
542
589k
    }
543
544
24.4k
    if ((size != (tmsize_t)(-1)) && (size < stripsize))
545
3
        stripsize = size;
546
24.4k
    if (!TIFFFillStrip(tif, strip))
547
6.48k
        return ((tmsize_t)(-1));
548
17.9k
    if ((*tif->tif_decodestrip)(tif, buf, stripsize, plane) <= 0)
549
15.1k
        return ((tmsize_t)(-1));
550
2.81k
    (*tif->tif_postdecode)(tif, buf, stripsize);
551
2.81k
    return (stripsize);
552
17.9k
}
553
554
/* Variant of TIFFReadEncodedStrip() that does
555
 * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
556
 * TIFFFillStrip() has succeeded. This avoid excessive memory allocation in case
557
 * of truncated file.
558
 * * calls regular TIFFReadEncodedStrip() if *buf != NULL
559
 */
560
tmsize_t _TIFFReadEncodedStripAndAllocBuffer(TIFF *tif, uint32_t strip,
561
                                             void **buf,
562
                                             tmsize_t bufsizetoalloc,
563
                                             tmsize_t size_to_read)
564
134k
{
565
134k
    tmsize_t this_stripsize;
566
134k
    uint16_t plane;
567
568
134k
    if (*buf != NULL)
569
0
    {
570
0
        return TIFFReadEncodedStrip(tif, strip, *buf, size_to_read);
571
0
    }
572
573
134k
    this_stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
574
134k
    if (this_stripsize == ((tmsize_t)(-1)))
575
0
        return ((tmsize_t)(-1));
576
577
134k
    if ((size_to_read != (tmsize_t)(-1)) && (size_to_read < this_stripsize))
578
711
        this_stripsize = size_to_read;
579
134k
    if (!TIFFFillStrip(tif, strip))
580
1.93k
        return ((tmsize_t)(-1));
581
582
132k
    *buf = _TIFFmallocExt(tif, bufsizetoalloc);
583
132k
    if (*buf == NULL)
584
0
    {
585
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "No space for strip buffer");
586
0
        return ((tmsize_t)(-1));
587
0
    }
588
132k
    _TIFFmemset(*buf, 0, bufsizetoalloc);
589
590
132k
    if ((*tif->tif_decodestrip)(tif, *buf, this_stripsize, plane) <= 0)
591
64.2k
        return ((tmsize_t)(-1));
592
68.2k
    (*tif->tif_postdecode)(tif, *buf, this_stripsize);
593
68.2k
    return (this_stripsize);
594
132k
}
595
596
static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf,
597
                                  tmsize_t size, const char *module)
598
589k
{
599
589k
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
600
589k
    if (!isMapped(tif))
601
589k
    {
602
589k
        tmsize_t cc;
603
604
589k
        if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip)))
605
441
        {
606
441
            TIFFErrorExtR(tif, module,
607
441
                          "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
608
441
                          tif->tif_row, strip);
609
441
            return ((tmsize_t)(-1));
610
441
        }
611
588k
        cc = TIFFReadFile(tif, buf, size);
612
588k
        if (cc != size)
613
835
        {
614
835
            TIFFErrorExtR(tif, module,
615
835
                          "Read error at scanline %" PRIu32
616
835
                          "; got %" TIFF_SSIZE_FORMAT
617
835
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
618
835
                          tif->tif_row, cc, size);
619
835
            return ((tmsize_t)(-1));
620
835
        }
621
588k
    }
622
722
    else
623
722
    {
624
722
        tmsize_t ma = 0;
625
722
        tmsize_t n;
626
722
        if ((TIFFGetStrileOffset(tif, strip) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
627
722
            ((ma = (tmsize_t)TIFFGetStrileOffset(tif, strip)) > tif->tif_size))
628
0
        {
629
0
            n = 0;
630
0
        }
631
722
        else if (ma > TIFF_TMSIZE_T_MAX - size)
632
0
        {
633
0
            n = 0;
634
0
        }
635
722
        else
636
722
        {
637
722
            tmsize_t mb = ma + size;
638
722
            if (mb > tif->tif_size)
639
0
                n = tif->tif_size - ma;
640
722
            else
641
722
                n = size;
642
722
        }
643
722
        if (n != size)
644
0
        {
645
0
            TIFFErrorExtR(tif, module,
646
0
                          "Read error at scanline %" PRIu32 ", strip %" PRIu32
647
0
                          "; got %" TIFF_SSIZE_FORMAT
648
0
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
649
0
                          tif->tif_row, strip, n, size);
650
0
            return ((tmsize_t)(-1));
651
0
        }
652
722
        _TIFFmemcpy(buf, tif->tif_base + ma, size);
653
722
    }
654
588k
    return (size);
655
589k
}
656
657
static tmsize_t TIFFReadRawStripOrTile2(TIFF *tif, uint32_t strip_or_tile,
658
                                        int is_strip, tmsize_t size,
659
                                        const char *module)
660
409k
{
661
409k
    assert(!isMapped(tif));
662
409k
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
663
664
409k
    if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip_or_tile)))
665
1.32k
    {
666
1.32k
        if (is_strip)
667
713
        {
668
713
            TIFFErrorExtR(tif, module,
669
713
                          "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
670
713
                          tif->tif_row, strip_or_tile);
671
713
        }
672
608
        else
673
608
        {
674
608
            TIFFErrorExtR(tif, module,
675
608
                          "Seek error at row %" PRIu32 ", col %" PRIu32
676
608
                          ", tile %" PRIu32,
677
608
                          tif->tif_row, tif->tif_col, strip_or_tile);
678
608
        }
679
1.32k
        return ((tmsize_t)(-1));
680
1.32k
    }
681
682
408k
    if (!TIFFReadAndRealloc(tif, size, 0, is_strip, strip_or_tile, module))
683
2.16k
    {
684
2.16k
        return ((tmsize_t)(-1));
685
2.16k
    }
686
687
406k
    return (size);
688
408k
}
689
690
/*
691
 * Read a strip of data from the file.
692
 */
693
tmsize_t TIFFReadRawStrip(TIFF *tif, uint32_t strip, void *buf, tmsize_t size)
694
0
{
695
0
    static const char module[] = "TIFFReadRawStrip";
696
0
    TIFFDirectory *td = &tif->tif_dir;
697
0
    uint64_t bytecount64;
698
0
    tmsize_t bytecountm;
699
700
0
    if (!TIFFCheckRead(tif, 0))
701
0
        return ((tmsize_t)(-1));
702
0
    if (strip >= td->td_nstrips)
703
0
    {
704
0
        TIFFErrorExtR(tif, module,
705
0
                      "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,
706
0
                      td->td_nstrips);
707
0
        return ((tmsize_t)(-1));
708
0
    }
709
0
    if (tif->tif_flags & TIFF_NOREADRAW)
710
0
    {
711
0
        TIFFErrorExtR(tif, module,
712
0
                      "Compression scheme does not support access to raw "
713
0
                      "uncompressed data");
714
0
        return ((tmsize_t)(-1));
715
0
    }
716
0
    bytecount64 = TIFFGetStrileByteCount(tif, strip);
717
0
    if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)
718
0
        bytecountm = size;
719
0
    else
720
0
        bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
721
0
    if (bytecountm == 0)
722
0
    {
723
0
        return ((tmsize_t)(-1));
724
0
    }
725
0
    return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
726
0
}
727
728
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
729
308
static uint64_t NoSanitizeSubUInt64(uint64_t a, uint64_t b) { return a - b; }
730
731
/*
732
 * Read the specified strip and setup for decoding. The data buffer is
733
 * expanded, as necessary, to hold the strip's data.
734
 */
735
int TIFFFillStrip(TIFF *tif, uint32_t strip)
736
158k
{
737
158k
    static const char module[] = "TIFFFillStrip";
738
158k
    TIFFDirectory *td = &tif->tif_dir;
739
740
158k
    if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
741
158k
    {
742
158k
        uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
743
158k
        if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
744
6.57k
        {
745
6.57k
            TIFFErrorExtR(tif, module,
746
6.57k
                          "Invalid strip byte count %" PRIu64
747
6.57k
                          ", strip %" PRIu32,
748
6.57k
                          bytecount, strip);
749
6.57k
            return (0);
750
6.57k
        }
751
752
        /* To avoid excessive memory allocations: */
753
        /* Byte count should normally not be larger than a number of */
754
        /* times the uncompressed size plus some margin */
755
152k
        if (bytecount > 1024 * 1024)
756
3.67k
        {
757
            /* 10 and 4096 are just values that could be adjusted. */
758
            /* Hopefully they are safe enough for all codecs */
759
3.67k
            tmsize_t stripsize = TIFFStripSize(tif);
760
3.67k
            if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
761
3.63k
            {
762
3.63k
                uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
763
3.63k
                TIFFErrorExtR(tif, module,
764
3.63k
                              "Too large strip byte count %" PRIu64
765
3.63k
                              ", strip %" PRIu32 ". Limiting to %" PRIu64,
766
3.63k
                              bytecount, strip, newbytecount);
767
3.63k
                bytecount = newbytecount;
768
3.63k
            }
769
3.67k
        }
770
771
152k
        if (isMapped(tif))
772
22.4k
        {
773
            /*
774
             * We must check for overflow, potentially causing
775
             * an OOB read. Instead of simple
776
             *
777
             *  TIFFGetStrileOffset(tif, strip)+bytecount > tif->tif_size
778
             *
779
             * comparison (which can overflow) we do the following
780
             * two comparisons:
781
             */
782
22.4k
            if (bytecount > (uint64_t)tif->tif_size ||
783
22.4k
                TIFFGetStrileOffset(tif, strip) >
784
22.3k
                    (uint64_t)tif->tif_size - bytecount)
785
308
            {
786
                /*
787
                 * This error message might seem strange, but
788
                 * it's what would happen if a read were done
789
                 * instead.
790
                 */
791
308
                TIFFErrorExtR(
792
308
                    tif, module,
793
794
308
                    "Read error on strip %" PRIu32 "; "
795
308
                    "got %" PRIu64 " bytes, expected %" PRIu64,
796
308
                    strip,
797
308
                    NoSanitizeSubUInt64(tif->tif_size,
798
308
                                        TIFFGetStrileOffset(tif, strip)),
799
308
                    bytecount);
800
308
                tif->tif_curstrip = NOSTRIP;
801
308
                return (0);
802
308
            }
803
22.4k
        }
804
805
151k
        if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
806
22.1k
                              (tif->tif_flags & TIFF_NOBITREV)))
807
21.3k
        {
808
            /*
809
             * The image is mapped into memory and we either don't
810
             * need to flip bits or the compression routine is
811
             * going to handle this operation itself.  In this
812
             * case, avoid copying the raw data and instead just
813
             * reference the data from the memory mapped file
814
             * image.  This assumes that the decompression
815
             * routines do not modify the contents of the raw data
816
             * buffer (if they try to, the application will get a
817
             * fault since the file is mapped read-only).
818
             */
819
21.3k
            if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
820
0
            {
821
0
                _TIFFfreeExt(tif, tif->tif_rawdata);
822
0
                tif->tif_rawdata = NULL;
823
0
                tif->tif_rawdatasize = 0;
824
0
            }
825
21.3k
            tif->tif_flags &= ~TIFF_MYBUFFER;
826
21.3k
            tif->tif_rawdatasize = (tmsize_t)bytecount;
827
21.3k
            tif->tif_rawdata =
828
21.3k
                tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, strip);
829
21.3k
            tif->tif_rawdataoff = 0;
830
21.3k
            tif->tif_rawdataloaded = (tmsize_t)bytecount;
831
832
            /*
833
             * When we have tif_rawdata reference directly into the memory
834
             * mapped file we need to be pretty careful about how we use the
835
             * rawdata.  It is not a general purpose working buffer as it
836
             * normally otherwise is.  So we keep track of this fact to avoid
837
             * using it improperly.
838
             */
839
21.3k
            tif->tif_flags |= TIFF_BUFFERMMAP;
840
21.3k
        }
841
130k
        else
842
130k
        {
843
            /*
844
             * Expand raw data buffer, if needed, to hold data
845
             * strip coming from file (perhaps should set upper
846
             * bound on the size of a buffer we'll use?).
847
             */
848
130k
            tmsize_t bytecountm;
849
130k
            bytecountm = (tmsize_t)bytecount;
850
130k
            if ((uint64_t)bytecountm != bytecount)
851
0
            {
852
0
                TIFFErrorExtR(tif, module, "Integer overflow");
853
0
                return (0);
854
0
            }
855
130k
            if (bytecountm > tif->tif_rawdatasize)
856
3.56k
            {
857
3.56k
                tif->tif_curstrip = NOSTRIP;
858
3.56k
                if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
859
0
                {
860
0
                    TIFFErrorExtR(
861
0
                        tif, module,
862
0
                        "Data buffer too small to hold strip %" PRIu32, strip);
863
0
                    return (0);
864
0
                }
865
3.56k
            }
866
130k
            if (tif->tif_flags & TIFF_BUFFERMMAP)
867
0
            {
868
0
                tif->tif_curstrip = NOSTRIP;
869
0
                tif->tif_rawdata = NULL;
870
0
                tif->tif_rawdatasize = 0;
871
0
                tif->tif_flags &= ~TIFF_BUFFERMMAP;
872
0
            }
873
874
130k
            if (isMapped(tif))
875
722
            {
876
722
                if (bytecountm > tif->tif_rawdatasize &&
877
722
                    !TIFFReadBufferSetup(tif, 0, bytecountm))
878
0
                {
879
0
                    return (0);
880
0
                }
881
722
                if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata, bytecountm,
882
722
                                      module) != bytecountm)
883
0
                {
884
0
                    return (0);
885
0
                }
886
722
            }
887
129k
            else
888
129k
            {
889
129k
                if (TIFFReadRawStripOrTile2(tif, strip, 1, bytecountm,
890
129k
                                            module) != bytecountm)
891
1.25k
                {
892
1.25k
                    return (0);
893
1.25k
                }
894
129k
            }
895
896
129k
            tif->tif_rawdataoff = 0;
897
129k
            tif->tif_rawdataloaded = bytecountm;
898
899
129k
            if (!isFillOrder(tif, td->td_fillorder) &&
900
129k
                (tif->tif_flags & TIFF_NOBITREV) == 0)
901
1.97k
                TIFFReverseBits(tif->tif_rawdata, bytecountm);
902
129k
        }
903
151k
    }
904
150k
    return (TIFFStartStrip(tif, strip));
905
158k
}
906
907
/*
908
 * Tile-oriented Read Support
909
 * Contributed by Nancy Cam (Silicon Graphics).
910
 */
911
912
/*
913
 * Read and decompress a tile of data.  The
914
 * tile is selected by the (x,y,z,s) coordinates.
915
 */
916
tmsize_t TIFFReadTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z,
917
                      uint16_t s)
918
384k
{
919
384k
    if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
920
13.0k
        return ((tmsize_t)(-1));
921
371k
    return (TIFFReadEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf,
922
371k
                                (tmsize_t)(-1)));
923
384k
}
924
925
/*
926
 * Read a tile of data and decompress the specified
927
 * amount into the user-supplied buffer.
928
 */
929
tmsize_t TIFFReadEncodedTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
930
375k
{
931
375k
    static const char module[] = "TIFFReadEncodedTile";
932
375k
    TIFFDirectory *td = &tif->tif_dir;
933
375k
    tmsize_t tilesize = tif->tif_tilesize;
934
935
375k
    if (!TIFFCheckRead(tif, 1))
936
0
        return ((tmsize_t)(-1));
937
375k
    if (tile >= td->td_nstrips)
938
0
    {
939
0
        TIFFErrorExtR(tif, module,
940
0
                      "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
941
0
                      td->td_nstrips);
942
0
        return ((tmsize_t)(-1));
943
0
    }
944
945
    /* shortcut to avoid an extra memcpy() */
946
375k
    if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
947
375k
        size >= tilesize && !isMapped(tif) &&
948
375k
        ((tif->tif_flags & TIFF_NOREADRAW) == 0))
949
3.11k
    {
950
3.11k
        if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize)
951
31
            return ((tmsize_t)(-1));
952
953
3.07k
        if (!isFillOrder(tif, td->td_fillorder) &&
954
3.07k
            (tif->tif_flags & TIFF_NOBITREV) == 0)
955
622
            TIFFReverseBits(buf, tilesize);
956
957
3.07k
        (*tif->tif_postdecode)(tif, buf, tilesize);
958
3.07k
        return (tilesize);
959
3.11k
    }
960
961
372k
    if (size == (tmsize_t)(-1))
962
371k
        size = tilesize;
963
923
    else if (size > tilesize)
964
512
        size = tilesize;
965
372k
    if (TIFFFillTile(tif, tile) &&
966
372k
        (*tif->tif_decodetile)(tif, (uint8_t *)buf, size,
967
295k
                               (uint16_t)(tile / td->td_stripsperimage)))
968
22.4k
    {
969
22.4k
        (*tif->tif_postdecode)(tif, (uint8_t *)buf, size);
970
22.4k
        return (size);
971
22.4k
    }
972
350k
    else
973
350k
        return ((tmsize_t)(-1));
974
372k
}
975
976
/* Variant of TIFFReadTile() that does
977
 * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
978
 * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case
979
 * of truncated file.
980
 * * calls regular TIFFReadEncodedTile() if *buf != NULL
981
 */
982
tmsize_t _TIFFReadTileAndAllocBuffer(TIFF *tif, void **buf,
983
                                     tmsize_t bufsizetoalloc, uint32_t x,
984
                                     uint32_t y, uint32_t z, uint16_t s)
985
371k
{
986
371k
    if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
987
0
        return ((tmsize_t)(-1));
988
371k
    return (_TIFFReadEncodedTileAndAllocBuffer(
989
371k
        tif, TIFFComputeTile(tif, x, y, z, s), buf, bufsizetoalloc,
990
371k
        (tmsize_t)(-1)));
991
371k
}
992
993
/* Variant of TIFFReadEncodedTile() that does
994
 * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
995
 * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case
996
 * of truncated file.
997
 * * calls regular TIFFReadEncodedTile() if *buf != NULL
998
 */
999
tmsize_t _TIFFReadEncodedTileAndAllocBuffer(TIFF *tif, uint32_t tile,
1000
                                            void **buf, tmsize_t bufsizetoalloc,
1001
                                            tmsize_t size_to_read)
1002
371k
{
1003
371k
    static const char module[] = "_TIFFReadEncodedTileAndAllocBuffer";
1004
371k
    TIFFDirectory *td = &tif->tif_dir;
1005
371k
    tmsize_t tilesize = tif->tif_tilesize;
1006
1007
371k
    if (*buf != NULL)
1008
0
    {
1009
0
        return TIFFReadEncodedTile(tif, tile, *buf, size_to_read);
1010
0
    }
1011
1012
371k
    if (!TIFFCheckRead(tif, 1))
1013
0
        return ((tmsize_t)(-1));
1014
371k
    if (tile >= td->td_nstrips)
1015
0
    {
1016
0
        TIFFErrorExtR(tif, module,
1017
0
                      "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
1018
0
                      td->td_nstrips);
1019
0
        return ((tmsize_t)(-1));
1020
0
    }
1021
1022
371k
    if (!TIFFFillTile(tif, tile))
1023
3.24k
        return ((tmsize_t)(-1));
1024
1025
    /* Sanity checks to avoid excessive memory allocation */
1026
    /* Cf https://gitlab.com/libtiff/libtiff/-/issues/479 */
1027
367k
    if (td->td_compression == COMPRESSION_NONE)
1028
1.08k
    {
1029
1.08k
        if (tif->tif_rawdatasize != tilesize)
1030
21
        {
1031
21
            TIFFErrorExtR(tif, TIFFFileName(tif),
1032
21
                          "Invalid tile byte count for tile %u. "
1033
21
                          "Expected %" PRIu64 ", got %" PRIu64,
1034
21
                          tile, (uint64_t)tilesize,
1035
21
                          (uint64_t)tif->tif_rawdatasize);
1036
21
            return ((tmsize_t)(-1));
1037
21
        }
1038
1.08k
    }
1039
366k
    else
1040
366k
    {
1041
        /* Max compression ratio experimentally determined. Might be fragile...
1042
         * Only apply this heuristics to situations where the memory allocation
1043
         * would be big, to avoid breaking nominal use cases.
1044
         */
1045
366k
        const int maxCompressionRatio =
1046
366k
            td->td_compression == COMPRESSION_ZSTD ? 33000
1047
366k
            : td->td_compression == COMPRESSION_JXL
1048
366k
                ?
1049
                /* Evaluated on a 8000x8000 tile */
1050
0
                25000 * (td->td_planarconfig == PLANARCONFIG_CONTIG
1051
0
                             ? td->td_samplesperpixel
1052
0
                             : 1)
1053
366k
                : td->td_compression == COMPRESSION_LZMA ? 7000 : 1000;
1054
366k
        if (bufsizetoalloc > 100 * 1000 * 1000 &&
1055
366k
            tif->tif_rawdatasize < tilesize / maxCompressionRatio)
1056
2
        {
1057
2
            TIFFErrorExtR(tif, TIFFFileName(tif),
1058
2
                          "Likely invalid tile byte count for tile %u. "
1059
2
                          "Uncompressed tile size is %" PRIu64 ", "
1060
2
                          "compressed one is %" PRIu64,
1061
2
                          tile, (uint64_t)tilesize,
1062
2
                          (uint64_t)tif->tif_rawdatasize);
1063
2
            return ((tmsize_t)(-1));
1064
2
        }
1065
366k
    }
1066
1067
367k
    *buf = _TIFFmallocExt(tif, bufsizetoalloc);
1068
367k
    if (*buf == NULL)
1069
0
    {
1070
0
        TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer");
1071
0
        return ((tmsize_t)(-1));
1072
0
    }
1073
367k
    _TIFFmemset(*buf, 0, bufsizetoalloc);
1074
1075
367k
    if (size_to_read == (tmsize_t)(-1))
1076
367k
        size_to_read = tilesize;
1077
0
    else if (size_to_read > tilesize)
1078
0
        size_to_read = tilesize;
1079
367k
    if ((*tif->tif_decodetile)(tif, (uint8_t *)*buf, size_to_read,
1080
367k
                               (uint16_t)(tile / td->td_stripsperimage)))
1081
88.8k
    {
1082
88.8k
        (*tif->tif_postdecode)(tif, (uint8_t *)*buf, size_to_read);
1083
88.8k
        return (size_to_read);
1084
88.8k
    }
1085
279k
    else
1086
279k
        return ((tmsize_t)(-1));
1087
367k
}
1088
1089
static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf,
1090
                                 tmsize_t size, const char *module)
1091
5.51k
{
1092
5.51k
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
1093
5.51k
    if (!isMapped(tif))
1094
3.11k
    {
1095
3.11k
        tmsize_t cc;
1096
1097
3.11k
        if (!SeekOK(tif, TIFFGetStrileOffset(tif, tile)))
1098
15
        {
1099
15
            TIFFErrorExtR(tif, module,
1100
15
                          "Seek error at row %" PRIu32 ", col %" PRIu32
1101
15
                          ", tile %" PRIu32,
1102
15
                          tif->tif_row, tif->tif_col, tile);
1103
15
            return ((tmsize_t)(-1));
1104
15
        }
1105
3.09k
        cc = TIFFReadFile(tif, buf, size);
1106
3.09k
        if (cc != size)
1107
16
        {
1108
16
            TIFFErrorExtR(tif, module,
1109
16
                          "Read error at row %" PRIu32 ", col %" PRIu32
1110
16
                          "; got %" TIFF_SSIZE_FORMAT
1111
16
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
1112
16
                          tif->tif_row, tif->tif_col, cc, size);
1113
16
            return ((tmsize_t)(-1));
1114
16
        }
1115
3.09k
    }
1116
2.40k
    else
1117
2.40k
    {
1118
2.40k
        tmsize_t ma, mb;
1119
2.40k
        tmsize_t n;
1120
2.40k
        ma = (tmsize_t)TIFFGetStrileOffset(tif, tile);
1121
2.40k
        mb = ma + size;
1122
2.40k
        if ((TIFFGetStrileOffset(tif, tile) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
1123
2.40k
            (ma > tif->tif_size))
1124
0
            n = 0;
1125
2.40k
        else if ((mb < ma) || (mb < size) || (mb > tif->tif_size))
1126
0
            n = tif->tif_size - ma;
1127
2.40k
        else
1128
2.40k
            n = size;
1129
2.40k
        if (n != size)
1130
0
        {
1131
0
            TIFFErrorExtR(tif, module,
1132
0
                          "Read error at row %" PRIu32 ", col %" PRIu32
1133
0
                          ", tile %" PRIu32 "; got %" TIFF_SSIZE_FORMAT
1134
0
                          " bytes, expected %" TIFF_SSIZE_FORMAT,
1135
0
                          tif->tif_row, tif->tif_col, tile, n, size);
1136
0
            return ((tmsize_t)(-1));
1137
0
        }
1138
2.40k
        _TIFFmemcpy(buf, tif->tif_base + ma, size);
1139
2.40k
    }
1140
5.48k
    return (size);
1141
5.51k
}
1142
1143
/*
1144
 * Read a tile of data from the file.
1145
 */
1146
tmsize_t TIFFReadRawTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
1147
0
{
1148
0
    static const char module[] = "TIFFReadRawTile";
1149
0
    TIFFDirectory *td = &tif->tif_dir;
1150
0
    uint64_t bytecount64;
1151
0
    tmsize_t bytecountm;
1152
1153
0
    if (!TIFFCheckRead(tif, 1))
1154
0
        return ((tmsize_t)(-1));
1155
0
    if (tile >= td->td_nstrips)
1156
0
    {
1157
0
        TIFFErrorExtR(tif, module,
1158
0
                      "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
1159
0
                      td->td_nstrips);
1160
0
        return ((tmsize_t)(-1));
1161
0
    }
1162
0
    if (tif->tif_flags & TIFF_NOREADRAW)
1163
0
    {
1164
0
        TIFFErrorExtR(tif, module,
1165
0
                      "Compression scheme does not support access to raw "
1166
0
                      "uncompressed data");
1167
0
        return ((tmsize_t)(-1));
1168
0
    }
1169
0
    bytecount64 = TIFFGetStrileByteCount(tif, tile);
1170
0
    if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)
1171
0
        bytecountm = size;
1172
0
    else
1173
0
        bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
1174
0
    if (bytecountm == 0)
1175
0
    {
1176
0
        return ((tmsize_t)(-1));
1177
0
    }
1178
0
    return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
1179
0
}
1180
1181
/*
1182
 * Read the specified tile and setup for decoding. The data buffer is
1183
 * expanded, as necessary, to hold the tile's data.
1184
 */
1185
int TIFFFillTile(TIFF *tif, uint32_t tile)
1186
744k
{
1187
744k
    static const char module[] = "TIFFFillTile";
1188
744k
    TIFFDirectory *td = &tif->tif_dir;
1189
1190
744k
    if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
1191
744k
    {
1192
744k
        uint64_t bytecount = TIFFGetStrileByteCount(tif, tile);
1193
744k
        if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
1194
57.0k
        {
1195
57.0k
            TIFFErrorExtR(tif, module,
1196
57.0k
                          "%" PRIu64 ": Invalid tile byte count, tile %" PRIu32,
1197
57.0k
                          bytecount, tile);
1198
57.0k
            return (0);
1199
57.0k
        }
1200
1201
        /* To avoid excessive memory allocations: */
1202
        /* Byte count should normally not be larger than a number of */
1203
        /* times the uncompressed size plus some margin */
1204
686k
        if (bytecount > 1024 * 1024)
1205
5.61k
        {
1206
            /* 10 and 4096 are just values that could be adjusted. */
1207
            /* Hopefully they are safe enough for all codecs */
1208
5.61k
            tmsize_t stripsize = TIFFTileSize(tif);
1209
5.61k
            if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
1210
5.59k
            {
1211
5.59k
                uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
1212
5.59k
                TIFFErrorExtR(tif, module,
1213
5.59k
                              "Too large tile byte count %" PRIu64
1214
5.59k
                              ", tile %" PRIu32 ". Limiting to %" PRIu64,
1215
5.59k
                              bytecount, tile, newbytecount);
1216
5.59k
                bytecount = newbytecount;
1217
5.59k
            }
1218
5.61k
        }
1219
1220
686k
        if (isMapped(tif))
1221
407k
        {
1222
            /*
1223
             * We must check for overflow, potentially causing
1224
             * an OOB read. Instead of simple
1225
             *
1226
             *  TIFFGetStrileOffset(tif, tile)+bytecount > tif->tif_size
1227
             *
1228
             * comparison (which can overflow) we do the following
1229
             * two comparisons:
1230
             */
1231
407k
            if (bytecount > (uint64_t)tif->tif_size ||
1232
407k
                TIFFGetStrileOffset(tif, tile) >
1233
406k
                    (uint64_t)tif->tif_size - bytecount)
1234
2.19k
            {
1235
2.19k
                tif->tif_curtile = NOTILE;
1236
2.19k
                return (0);
1237
2.19k
            }
1238
407k
        }
1239
1240
684k
        if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
1241
404k
                              (tif->tif_flags & TIFF_NOBITREV)))
1242
402k
        {
1243
            /*
1244
             * The image is mapped into memory and we either don't
1245
             * need to flip bits or the compression routine is
1246
             * going to handle this operation itself.  In this
1247
             * case, avoid copying the raw data and instead just
1248
             * reference the data from the memory mapped file
1249
             * image.  This assumes that the decompression
1250
             * routines do not modify the contents of the raw data
1251
             * buffer (if they try to, the application will get a
1252
             * fault since the file is mapped read-only).
1253
             */
1254
402k
            if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
1255
0
            {
1256
0
                _TIFFfreeExt(tif, tif->tif_rawdata);
1257
0
                tif->tif_rawdata = NULL;
1258
0
                tif->tif_rawdatasize = 0;
1259
0
            }
1260
402k
            tif->tif_flags &= ~TIFF_MYBUFFER;
1261
1262
402k
            tif->tif_rawdatasize = (tmsize_t)bytecount;
1263
402k
            tif->tif_rawdata =
1264
402k
                tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, tile);
1265
402k
            tif->tif_rawdataoff = 0;
1266
402k
            tif->tif_rawdataloaded = (tmsize_t)bytecount;
1267
402k
            tif->tif_flags |= TIFF_BUFFERMMAP;
1268
402k
        }
1269
282k
        else
1270
282k
        {
1271
            /*
1272
             * Expand raw data buffer, if needed, to hold data
1273
             * tile coming from file (perhaps should set upper
1274
             * bound on the size of a buffer we'll use?).
1275
             */
1276
282k
            tmsize_t bytecountm;
1277
282k
            bytecountm = (tmsize_t)bytecount;
1278
282k
            if ((uint64_t)bytecountm != bytecount)
1279
0
            {
1280
0
                TIFFErrorExtR(tif, module, "Integer overflow");
1281
0
                return (0);
1282
0
            }
1283
282k
            if (bytecountm > tif->tif_rawdatasize)
1284
3.97k
            {
1285
3.97k
                tif->tif_curtile = NOTILE;
1286
3.97k
                if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
1287
0
                {
1288
0
                    TIFFErrorExtR(tif, module,
1289
0
                                  "Data buffer too small to hold tile %" PRIu32,
1290
0
                                  tile);
1291
0
                    return (0);
1292
0
                }
1293
3.97k
            }
1294
282k
            if (tif->tif_flags & TIFF_BUFFERMMAP)
1295
0
            {
1296
0
                tif->tif_curtile = NOTILE;
1297
0
                tif->tif_rawdata = NULL;
1298
0
                tif->tif_rawdatasize = 0;
1299
0
                tif->tif_flags &= ~TIFF_BUFFERMMAP;
1300
0
            }
1301
1302
282k
            if (isMapped(tif))
1303
2.40k
            {
1304
2.40k
                if (bytecountm > tif->tif_rawdatasize &&
1305
2.40k
                    !TIFFReadBufferSetup(tif, 0, bytecountm))
1306
0
                {
1307
0
                    return (0);
1308
0
                }
1309
2.40k
                if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata, bytecountm,
1310
2.40k
                                     module) != bytecountm)
1311
0
                {
1312
0
                    return (0);
1313
0
                }
1314
2.40k
            }
1315
279k
            else
1316
279k
            {
1317
279k
                if (TIFFReadRawStripOrTile2(tif, tile, 0, bytecountm, module) !=
1318
279k
                    bytecountm)
1319
2.23k
                {
1320
2.23k
                    return (0);
1321
2.23k
                }
1322
279k
            }
1323
1324
279k
            tif->tif_rawdataoff = 0;
1325
279k
            tif->tif_rawdataloaded = bytecountm;
1326
1327
279k
            if (tif->tif_rawdata != NULL &&
1328
279k
                !isFillOrder(tif, td->td_fillorder) &&
1329
279k
                (tif->tif_flags & TIFF_NOBITREV) == 0)
1330
2.88k
                TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdataloaded);
1331
279k
        }
1332
684k
    }
1333
682k
    return (TIFFStartTile(tif, tile));
1334
744k
}
1335
1336
/*
1337
 * Setup the raw data buffer in preparation for
1338
 * reading a strip of raw data.  If the buffer
1339
 * is specified as zero, then a buffer of appropriate
1340
 * size is allocated by the library.  Otherwise,
1341
 * the client must guarantee that the buffer is
1342
 * large enough to hold any individual strip of
1343
 * raw data.
1344
 */
1345
int TIFFReadBufferSetup(TIFF *tif, void *bp, tmsize_t size)
1346
105
{
1347
105
    static const char module[] = "TIFFReadBufferSetup";
1348
1349
105
    assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
1350
105
    tif->tif_flags &= ~TIFF_BUFFERMMAP;
1351
1352
105
    if (tif->tif_rawdata)
1353
36
    {
1354
36
        if (tif->tif_flags & TIFF_MYBUFFER)
1355
36
            _TIFFfreeExt(tif, tif->tif_rawdata);
1356
36
        tif->tif_rawdata = NULL;
1357
36
        tif->tif_rawdatasize = 0;
1358
36
    }
1359
105
    if (bp)
1360
0
    {
1361
0
        tif->tif_rawdatasize = size;
1362
0
        tif->tif_rawdata = (uint8_t *)bp;
1363
0
        tif->tif_flags &= ~TIFF_MYBUFFER;
1364
0
    }
1365
105
    else
1366
105
    {
1367
105
        tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64_t)size, 1024);
1368
105
        if (tif->tif_rawdatasize == 0)
1369
0
        {
1370
0
            TIFFErrorExtR(tif, module, "Invalid buffer size");
1371
0
            return (0);
1372
0
        }
1373
        /* Initialize to zero to avoid uninitialized buffers in case of */
1374
        /* short reads (http://bugzilla.maptools.org/show_bug.cgi?id=2651) */
1375
105
        tif->tif_rawdata =
1376
105
            (uint8_t *)_TIFFcallocExt(tif, 1, tif->tif_rawdatasize);
1377
105
        tif->tif_flags |= TIFF_MYBUFFER;
1378
105
    }
1379
105
    if (tif->tif_rawdata == NULL)
1380
0
    {
1381
0
        TIFFErrorExtR(tif, module,
1382
0
                      "No space for data buffer at scanline %" PRIu32,
1383
0
                      tif->tif_row);
1384
0
        tif->tif_rawdatasize = 0;
1385
0
        return (0);
1386
0
    }
1387
105
    return (1);
1388
105
}
1389
1390
/*
1391
 * Set state to appear as if a
1392
 * strip has just been read in.
1393
 */
1394
static int TIFFStartStrip(TIFF *tif, uint32_t strip)
1395
150k
{
1396
150k
    TIFFDirectory *td = &tif->tif_dir;
1397
1398
150k
    if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
1399
3.63k
    {
1400
3.63k
        if (!(*tif->tif_setupdecode)(tif))
1401
12
            return (0);
1402
3.62k
        tif->tif_flags |= TIFF_CODERSETUP;
1403
3.62k
    }
1404
150k
    tif->tif_curstrip = strip;
1405
150k
    tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
1406
150k
    tif->tif_flags &= ~TIFF_BUF4WRITE;
1407
1408
150k
    if (tif->tif_flags & TIFF_NOREADRAW)
1409
0
    {
1410
0
        tif->tif_rawcp = NULL;
1411
0
        tif->tif_rawcc = 0;
1412
0
    }
1413
150k
    else
1414
150k
    {
1415
150k
        tif->tif_rawcp = tif->tif_rawdata;
1416
150k
        if (tif->tif_rawdataloaded > 0)
1417
150k
            tif->tif_rawcc = tif->tif_rawdataloaded;
1418
0
        else
1419
0
            tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, strip);
1420
150k
    }
1421
150k
    if ((*tif->tif_predecode)(tif, (uint16_t)(strip / td->td_stripsperimage)) ==
1422
150k
        0)
1423
287
    {
1424
        /* Needed for example for scanline access, if tif_predecode */
1425
        /* fails, and we try to read the same strip again. Without invalidating
1426
         */
1427
        /* tif_curstrip, we'd call tif_decoderow() on a possibly invalid */
1428
        /* codec state. */
1429
287
        tif->tif_curstrip = NOSTRIP;
1430
287
        return 0;
1431
287
    }
1432
150k
    return 1;
1433
150k
}
1434
1435
/*
1436
 * Set state to appear as if a
1437
 * tile has just been read in.
1438
 */
1439
static int TIFFStartTile(TIFF *tif, uint32_t tile)
1440
682k
{
1441
682k
    static const char module[] = "TIFFStartTile";
1442
682k
    TIFFDirectory *td = &tif->tif_dir;
1443
682k
    uint32_t howmany32;
1444
1445
682k
    if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
1446
3.91k
    {
1447
3.91k
        if (!(*tif->tif_setupdecode)(tif))
1448
9
            return (0);
1449
3.90k
        tif->tif_flags |= TIFF_CODERSETUP;
1450
3.90k
    }
1451
682k
    tif->tif_curtile = tile;
1452
682k
    howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
1453
682k
    if (howmany32 == 0)
1454
0
    {
1455
0
        TIFFErrorExtR(tif, module, "Zero tiles");
1456
0
        return 0;
1457
0
    }
1458
682k
    tif->tif_row = (tile % howmany32) * td->td_tilelength;
1459
682k
    howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
1460
682k
    if (howmany32 == 0)
1461
0
    {
1462
0
        TIFFErrorExtR(tif, module, "Zero tiles");
1463
0
        return 0;
1464
0
    }
1465
682k
    tif->tif_col = (tile % howmany32) * td->td_tilewidth;
1466
682k
    tif->tif_flags &= ~TIFF_BUF4WRITE;
1467
682k
    if (tif->tif_flags & TIFF_NOREADRAW)
1468
0
    {
1469
0
        tif->tif_rawcp = NULL;
1470
0
        tif->tif_rawcc = 0;
1471
0
    }
1472
682k
    else
1473
682k
    {
1474
682k
        tif->tif_rawcp = tif->tif_rawdata;
1475
682k
        if (tif->tif_rawdataloaded > 0)
1476
682k
            tif->tif_rawcc = tif->tif_rawdataloaded;
1477
0
        else
1478
0
            tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, tile);
1479
682k
    }
1480
682k
    return (
1481
682k
        (*tif->tif_predecode)(tif, (uint16_t)(tile / td->td_stripsperimage)));
1482
682k
}
1483
1484
static int TIFFCheckRead(TIFF *tif, int tiles)
1485
2.25M
{
1486
2.25M
    if (tif->tif_mode == O_WRONLY)
1487
0
    {
1488
0
        TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
1489
0
        return (0);
1490
0
    }
1491
2.25M
    if (tiles ^ isTiled(tif))
1492
0
    {
1493
0
        TIFFErrorExtR(tif, tif->tif_name,
1494
0
                      tiles ? "Can not read tiles from a striped image"
1495
0
                            : "Can not read scanlines from a tiled image");
1496
0
        return (0);
1497
0
    }
1498
2.25M
    return (1);
1499
2.25M
}
1500
1501
/* Use the provided input buffer (inbuf, insize) and decompress it into
1502
 * (outbuf, outsize).
1503
 * This function replaces the use of
1504
 * TIFFReadEncodedStrip()/TIFFReadEncodedTile() when the user can provide the
1505
 * buffer for the input data, for example when he wants to avoid libtiff to read
1506
 * the strile offset/count values from the [Strip|Tile][Offsets/ByteCounts]
1507
 * array. inbuf content must be writable (if bit reversal is needed) Returns 1
1508
 * in case of success, 0 otherwise.
1509
 */
1510
int TIFFReadFromUserBuffer(TIFF *tif, uint32_t strile, void *inbuf,
1511
                           tmsize_t insize, void *outbuf, tmsize_t outsize)
1512
0
{
1513
0
    static const char module[] = "TIFFReadFromUserBuffer";
1514
0
    TIFFDirectory *td = &tif->tif_dir;
1515
0
    int ret = 1;
1516
0
    uint32_t old_tif_flags = tif->tif_flags;
1517
0
    tmsize_t old_rawdatasize = tif->tif_rawdatasize;
1518
0
    void *old_rawdata = tif->tif_rawdata;
1519
1520
0
    if (tif->tif_mode == O_WRONLY)
1521
0
    {
1522
0
        TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
1523
0
        return 0;
1524
0
    }
1525
0
    if (tif->tif_flags & TIFF_NOREADRAW)
1526
0
    {
1527
0
        TIFFErrorExtR(tif, module,
1528
0
                      "Compression scheme does not support access to raw "
1529
0
                      "uncompressed data");
1530
0
        return 0;
1531
0
    }
1532
1533
0
    tif->tif_flags &= ~TIFF_MYBUFFER;
1534
0
    tif->tif_flags |= TIFF_BUFFERMMAP;
1535
0
    tif->tif_rawdatasize = insize;
1536
0
    tif->tif_rawdata = inbuf;
1537
0
    tif->tif_rawdataoff = 0;
1538
0
    tif->tif_rawdataloaded = insize;
1539
1540
0
    if (!isFillOrder(tif, td->td_fillorder) &&
1541
0
        (tif->tif_flags & TIFF_NOBITREV) == 0)
1542
0
    {
1543
0
        TIFFReverseBits(inbuf, insize);
1544
0
    }
1545
1546
0
    if (TIFFIsTiled(tif))
1547
0
    {
1548
0
        if (!TIFFStartTile(tif, strile) ||
1549
0
            !(*tif->tif_decodetile)(tif, (uint8_t *)outbuf, outsize,
1550
0
                                    (uint16_t)(strile / td->td_stripsperimage)))
1551
0
        {
1552
0
            ret = 0;
1553
0
        }
1554
0
    }
1555
0
    else
1556
0
    {
1557
0
        uint32_t rowsperstrip = td->td_rowsperstrip;
1558
0
        uint32_t stripsperplane;
1559
0
        if (rowsperstrip > td->td_imagelength)
1560
0
            rowsperstrip = td->td_imagelength;
1561
0
        stripsperplane =
1562
0
            TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
1563
0
        if (!TIFFStartStrip(tif, strile) ||
1564
0
            !(*tif->tif_decodestrip)(tif, (uint8_t *)outbuf, outsize,
1565
0
                                     (uint16_t)(strile / stripsperplane)))
1566
0
        {
1567
0
            ret = 0;
1568
0
        }
1569
0
    }
1570
0
    if (ret)
1571
0
    {
1572
0
        (*tif->tif_postdecode)(tif, (uint8_t *)outbuf, outsize);
1573
0
    }
1574
1575
0
    if (!isFillOrder(tif, td->td_fillorder) &&
1576
0
        (tif->tif_flags & TIFF_NOBITREV) == 0)
1577
0
    {
1578
0
        TIFFReverseBits(inbuf, insize);
1579
0
    }
1580
1581
0
    tif->tif_flags = (old_tif_flags & (TIFF_MYBUFFER | TIFF_BUFFERMMAP)) |
1582
0
                     (tif->tif_flags & ~(TIFF_MYBUFFER | TIFF_BUFFERMMAP));
1583
0
    tif->tif_rawdatasize = old_rawdatasize;
1584
0
    tif->tif_rawdata = old_rawdata;
1585
0
    tif->tif_rawdataoff = 0;
1586
0
    tif->tif_rawdataloaded = 0;
1587
1588
0
    return ret;
1589
0
}
1590
1591
void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
1592
765k
{
1593
765k
    (void)tif;
1594
765k
    (void)buf;
1595
765k
    (void)cc;
1596
765k
}
1597
1598
void _TIFFSwab16BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
1599
1.04k
{
1600
1.04k
    (void)tif;
1601
1.04k
    assert((cc & 1) == 0);
1602
1.04k
    TIFFSwabArrayOfShort((uint16_t *)buf, cc / 2);
1603
1.04k
}
1604
1605
void _TIFFSwab24BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
1606
0
{
1607
0
    (void)tif;
1608
0
    assert((cc % 3) == 0);
1609
0
    TIFFSwabArrayOfTriples((uint8_t *)buf, cc / 3);
1610
0
}
1611
1612
void _TIFFSwab32BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
1613
2.48k
{
1614
2.48k
    (void)tif;
1615
2.48k
    assert((cc & 3) == 0);
1616
2.48k
    TIFFSwabArrayOfLong((uint32_t *)buf, cc / 4);
1617
2.48k
}
1618
1619
void _TIFFSwab64BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
1620
5.30k
{
1621
5.30k
    (void)tif;
1622
5.30k
    assert((cc & 7) == 0);
1623
5.30k
    TIFFSwabArrayOfDouble((double *)buf, cc / 8);
1624
5.30k
}