Coverage Report

Created: 2023-06-07 08:11

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