ReactOS 0.4.16-dev-2617-g01a0906
tif_read.c File Reference
#include "tiffiop.h"
#include <stdio.h>
Include dependency graph for tif_read.c:

Go to the source code of this file.

Macros

#define NOSTRIP   ((uint32_t)(-1)) /* undefined state */
 
#define NOTILE   ((uint32_t)(-1)) /* undefined state */
 
#define INITIAL_THRESHOLD   (1024 * 1024)
 
#define THRESHOLD_MULTIPLIER   10
 
#define MAX_THRESHOLD
 
#define TIFF_INT64_MAX   ((((int64_t)0x7FFFFFFF) << 32) | 0xFFFFFFFF)
 

Functions

int TIFFFillStrip (TIFF *tif, uint32_t strip)
 
int TIFFFillTile (TIFF *tif, uint32_t tile)
 
static int TIFFStartStrip (TIFF *tif, uint32_t strip)
 
static int TIFFStartTile (TIFF *tif, uint32_t tile)
 
static int TIFFCheckRead (TIFF *, int)
 
static tmsize_t TIFFReadRawStrip1 (TIFF *tif, uint32_t strip, void *buf, tmsize_t size, const char *module)
 
static tmsize_t TIFFReadRawTile1 (TIFF *tif, uint32_t tile, void *buf, tmsize_t size, const char *module)
 
static int TIFFReadAndRealloc (TIFF *tif, tmsize_t size, tmsize_t rawdata_offset, int is_strip, uint32_t strip_or_tile, const char *module)
 
static int TIFFFillStripPartial (TIFF *tif, int strip, tmsize_t read_ahead, int restart)
 
static int TIFFSeek (TIFF *tif, uint32_t row, uint16_t sample)
 
int TIFFReadScanline (TIFF *tif, void *buf, uint32_t row, uint16_t sample)
 
static tmsize_t TIFFReadEncodedStripGetStripSize (TIFF *tif, uint32_t strip, uint16_t *pplane)
 
tmsize_t TIFFReadEncodedStrip (TIFF *tif, uint32_t strip, void *buf, tmsize_t size)
 
tmsize_t _TIFFReadEncodedStripAndAllocBuffer (TIFF *tif, uint32_t strip, void **buf, tmsize_t bufsizetoalloc, tmsize_t size_to_read)
 
static tmsize_t TIFFReadRawStripOrTile2 (TIFF *tif, uint32_t strip_or_tile, int is_strip, tmsize_t size, const char *module)
 
tmsize_t TIFFReadRawStrip (TIFF *tif, uint32_t strip, void *buf, tmsize_t size)
 
static TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW uint64_t NoSanitizeSubUInt64 (uint64_t a, uint64_t b)
 
tmsize_t TIFFReadTile (TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
 
tmsize_t TIFFReadEncodedTile (TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
 
tmsize_t _TIFFReadTileAndAllocBuffer (TIFF *tif, void **buf, tmsize_t bufsizetoalloc, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
 
tmsize_t _TIFFReadEncodedTileAndAllocBuffer (TIFF *tif, uint32_t tile, void **buf, tmsize_t bufsizetoalloc, tmsize_t size_to_read)
 
tmsize_t TIFFReadRawTile (TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
 
int TIFFReadBufferSetup (TIFF *tif, void *bp, tmsize_t size)
 
int TIFFReadFromUserBuffer (TIFF *tif, uint32_t strile, void *inbuf, tmsize_t insize, void *outbuf, tmsize_t outsize)
 
void _TIFFNoPostDecode (TIFF *tif, uint8_t *buf, tmsize_t cc)
 
void _TIFFSwab16BitData (TIFF *tif, uint8_t *buf, tmsize_t cc)
 
void _TIFFSwab24BitData (TIFF *tif, uint8_t *buf, tmsize_t cc)
 
void _TIFFSwab32BitData (TIFF *tif, uint8_t *buf, tmsize_t cc)
 
void _TIFFSwab64BitData (TIFF *tif, uint8_t *buf, tmsize_t cc)
 

Macro Definition Documentation

◆ INITIAL_THRESHOLD

#define INITIAL_THRESHOLD   (1024 * 1024)

Definition at line 45 of file tif_read.c.

◆ MAX_THRESHOLD

#define MAX_THRESHOLD
Value:
#define INITIAL_THRESHOLD
Definition: tif_read.c:45
#define THRESHOLD_MULTIPLIER
Definition: tif_read.c:46

Definition at line 47 of file tif_read.c.

◆ NOSTRIP

#define NOSTRIP   ((uint32_t)(-1)) /* undefined state */

Definition at line 42 of file tif_read.c.

◆ NOTILE

#define NOTILE   ((uint32_t)(-1)) /* undefined state */

Definition at line 43 of file tif_read.c.

◆ THRESHOLD_MULTIPLIER

#define THRESHOLD_MULTIPLIER   10

Definition at line 46 of file tif_read.c.

◆ TIFF_INT64_MAX

#define TIFF_INT64_MAX   ((((int64_t)0x7FFFFFFF) << 32) | 0xFFFFFFFF)

Definition at line 51 of file tif_read.c.

Function Documentation

◆ _TIFFNoPostDecode()

void _TIFFNoPostDecode ( TIFF tif,
uint8_t buf,
tmsize_t  cc 
)

Definition at line 1648 of file tif_read.c.

1649{
1650 (void)tif;
1651 (void)buf;
1652 (void)cc;
1653}
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
uint32_t cc
Definition: isohybrid.c:75

Referenced by _TIFFVSetField(), PredictorSetupDecode(), PredictorSetupEncode(), and TIFFDefaultDirectory().

◆ _TIFFReadEncodedStripAndAllocBuffer()

tmsize_t _TIFFReadEncodedStripAndAllocBuffer ( TIFF tif,
uint32_t  strip,
void **  buf,
tmsize_t  bufsizetoalloc,
tmsize_t  size_to_read 
)

Definition at line 584 of file tif_read.c.

588{
589 tmsize_t this_stripsize;
591
592 if (*buf != NULL)
593 {
594 return TIFFReadEncodedStrip(tif, strip, *buf, size_to_read);
595 }
596
597 this_stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
598 if (this_stripsize == ((tmsize_t)(-1)))
599 return ((tmsize_t)(-1));
600
601 if ((size_to_read != (tmsize_t)(-1)) && (size_to_read < this_stripsize))
602 this_stripsize = size_to_read;
603 if (!TIFFFillStrip(tif, strip))
604 return ((tmsize_t)(-1));
605
606 *buf = _TIFFmallocExt(tif, bufsizetoalloc);
607 if (*buf == NULL)
608 {
609 TIFFErrorExtR(tif, TIFFFileName(tif), "No space for strip buffer");
610 return ((tmsize_t)(-1));
611 }
612 _TIFFmemset(*buf, 0, bufsizetoalloc);
613
614 if ((*tif->tif_decodestrip)(tif, *buf, this_stripsize, plane) <= 0)
615 return ((tmsize_t)(-1));
616 (*tif->tif_postdecode)(tif, *buf, this_stripsize);
617 return (this_stripsize);
618}
#define NULL
Definition: types.h:112
unsigned short uint16_t
Definition: stdint.h:35
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint GLenum GLenum GLenum GLint GLuint GLenum GLenum GLfloat GLenum GLfloat GLenum GLint const GLfloat GLenum GLint const GLushort GLint GLint GLsizei GLsizei GLenum GLsizei GLsizei GLenum GLenum const GLvoid GLenum plane
Definition: glfuncs.h:270
TIFFCodeMethod tif_decodestrip
Definition: tiffiop.h:207
TIFFPostMethod tif_postdecode
Definition: tiffiop.h:239
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Definition: tif_error.c:107
const char * TIFFFileName(TIFF *tif)
Definition: tif_open.c:798
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Definition: tif_open.c:173
static tmsize_t TIFFReadEncodedStripGetStripSize(TIFF *tif, uint32_t strip, uint16_t *pplane)
Definition: tif_read.c:488
tmsize_t TIFFReadEncodedStrip(TIFF *tif, uint32_t strip, void *buf, tmsize_t size)
Definition: tif_read.c:534
int TIFFFillStrip(TIFF *tif, uint32_t strip)
Definition: tif_read.c:759
void _TIFFmemset(void *p, int v, tmsize_t c)
Definition: tif_unix.c:353
TIFF_SSIZE_T tmsize_t
Definition: tiffio.h:67

Referenced by gtStripContig(), and gtStripSeparate().

◆ _TIFFReadEncodedTileAndAllocBuffer()

tmsize_t _TIFFReadEncodedTileAndAllocBuffer ( TIFF tif,
uint32_t  tile,
void **  buf,
tmsize_t  bufsizetoalloc,
tmsize_t  size_to_read 
)

Definition at line 1029 of file tif_read.c.

1032{
1033 static const char module[] = "_TIFFReadEncodedTileAndAllocBuffer";
1034 TIFFDirectory *td = &tif->tif_dir;
1035 tmsize_t tilesize = tif->tif_tilesize;
1036
1037 if (*buf != NULL)
1038 {
1039 return TIFFReadEncodedTile(tif, tile, *buf, size_to_read);
1040 }
1041
1042 if (!TIFFCheckRead(tif, 1))
1043 return ((tmsize_t)(-1));
1044 if (tile >= td->td_nstrips)
1045 {
1046 TIFFErrorExtR(tif, module,
1047 "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
1048 td->td_nstrips);
1049 return ((tmsize_t)(-1));
1050 }
1051
1052 if (!TIFFFillTile(tif, tile))
1053 return ((tmsize_t)(-1));
1054
1055 /* Sanity checks to avoid excessive memory allocation */
1056 /* Cf https://gitlab.com/libtiff/libtiff/-/issues/479 */
1058 {
1059 if (tif->tif_rawdatasize != tilesize)
1060 {
1061 TIFFErrorExtR(tif, TIFFFileName(tif),
1062 "Invalid tile byte count for tile %u. "
1063 "Expected %" PRIu64 ", got %" PRIu64,
1064 tile, (uint64_t)tilesize,
1066 return ((tmsize_t)(-1));
1067 }
1068 }
1069 else
1070 {
1071 /* Max compression ratio experimentally determined. Might be fragile...
1072 * Only apply this heuristics to situations where the memory allocation
1073 * would be big, to avoid breaking nominal use cases.
1074 */
1075 const int maxCompressionRatio =
1076 td->td_compression == COMPRESSION_ZSTD ? 33000
1078 ?
1079 /* Evaluated on a 8000x8000 tile */
1080 25000 * (td->td_planarconfig == PLANARCONFIG_CONTIG
1081 ? td->td_samplesperpixel
1082 : 1)
1083 : td->td_compression == COMPRESSION_LZMA ? 7000 : 1000;
1084 if (bufsizetoalloc > 100 * 1000 * 1000 &&
1085 tif->tif_rawdatasize < tilesize / maxCompressionRatio)
1086 {
1087 TIFFErrorExtR(tif, TIFFFileName(tif),
1088 "Likely invalid tile byte count for tile %u. "
1089 "Uncompressed tile size is %" PRIu64 ", "
1090 "compressed one is %" PRIu64,
1091 tile, (uint64_t)tilesize,
1093 return ((tmsize_t)(-1));
1094 }
1095 }
1096
1097 *buf = _TIFFmallocExt(tif, bufsizetoalloc);
1098 if (*buf == NULL)
1099 {
1100 TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer");
1101 return ((tmsize_t)(-1));
1102 }
1103 _TIFFmemset(*buf, 0, bufsizetoalloc);
1104
1105 if (size_to_read == (tmsize_t)(-1))
1106 size_to_read = tilesize;
1107 else if (size_to_read > tilesize)
1108 size_to_read = tilesize;
1109 if ((*tif->tif_decodetile)(tif, (uint8_t *)*buf, size_to_read,
1110 (uint16_t)(tile / td->td_stripsperimage)))
1111 {
1112 (*tif->tif_postdecode)(tif, (uint8_t *)*buf, size_to_read);
1113 return (size_to_read);
1114 }
1115 else
1116 return ((tmsize_t)(-1));
1117}
UINT64 uint64_t
Definition: types.h:77
#define PRIu32
Definition: inttypes.h:84
#define PRIu64
Definition: inttypes.h:28
unsigned char uint8_t
Definition: stdint.h:33
if(dx< 0)
Definition: linetemp.h:194
uint16_t td_compression
Definition: tif_dir.h:88
uint32_t td_stripsperimage
Definition: tif_dir.h:110
uint16_t td_planarconfig
Definition: tif_dir.h:100
uint16_t td_samplesperpixel
Definition: tif_dir.h:93
uint32_t td_nstrips
Definition: tif_dir.h:111
TIFFDirectory tif_dir
Definition: tiffiop.h:157
tmsize_t tif_rawdatasize
Definition: tiffiop.h:221
tmsize_t tif_tilesize
Definition: tiffiop.h:195
TIFFCodeMethod tif_decodetile
Definition: tiffiop.h:209
static int TIFFCheckRead(TIFF *, int)
Definition: tif_read.c:1519
int TIFFFillTile(TIFF *tif, uint32_t tile)
Definition: tif_read.c:1215
tmsize_t TIFFReadEncodedTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
Definition: tif_read.c:953
#define COMPRESSION_NONE
Definition: tiff.h:182
#define COMPRESSION_ZSTD
Definition: tiff.h:216
#define COMPRESSION_JXL
Definition: tiff.h:218
#define COMPRESSION_LZMA
Definition: tiff.h:215
#define PLANARCONFIG_CONTIG
Definition: tiff.h:265

Referenced by _TIFFReadTileAndAllocBuffer().

◆ _TIFFReadTileAndAllocBuffer()

tmsize_t _TIFFReadTileAndAllocBuffer ( TIFF tif,
void **  buf,
tmsize_t  bufsizetoalloc,
uint32_t  x,
uint32_t  y,
uint32_t  z,
uint16_t  s 
)

Definition at line 1012 of file tif_read.c.

1015{
1016 if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
1017 return ((tmsize_t)(-1));
1019 tif, TIFFComputeTile(tif, x, y, z, s), buf, bufsizetoalloc,
1020 (tmsize_t)(-1)));
1021}
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLdouble s
Definition: gl.h:2039
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
GLdouble GLdouble z
Definition: glext.h:5874
tmsize_t _TIFFReadEncodedTileAndAllocBuffer(TIFF *tif, uint32_t tile, void **buf, tmsize_t bufsizetoalloc, tmsize_t size_to_read)
Definition: tif_read.c:1029
uint32_t TIFFComputeTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
Definition: tif_tile.c:35
int TIFFCheckTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
Definition: tif_tile.c:71

Referenced by gtTileContig(), and gtTileSeparate().

◆ _TIFFSwab16BitData()

void _TIFFSwab16BitData ( TIFF tif,
uint8_t buf,
tmsize_t  cc 
)

Definition at line 1655 of file tif_read.c.

1656{
1657 (void)tif;
1658 assert((cc & 1) == 0);
1660}
#define assert(_expr)
Definition: assert.h:32
void TIFFSwabArrayOfShort(register uint16_t *wp, tmsize_t n)
Definition: tif_swab.c:81

Referenced by _TIFFVSetField().

◆ _TIFFSwab24BitData()

void _TIFFSwab24BitData ( TIFF tif,
uint8_t buf,
tmsize_t  cc 
)

Definition at line 1662 of file tif_read.c.

1663{
1664 (void)tif;
1665 assert((cc % 3) == 0);
1667}
void TIFFSwabArrayOfTriples(register uint8_t *tp, tmsize_t n)
Definition: tif_swab.c:99

Referenced by _TIFFVSetField().

◆ _TIFFSwab32BitData()

void _TIFFSwab32BitData ( TIFF tif,
uint8_t buf,
tmsize_t  cc 
)

Definition at line 1669 of file tif_read.c.

1670{
1671 (void)tif;
1672 assert((cc & 3) == 0);
1674}
UINT32 uint32_t
Definition: types.h:75
void TIFFSwabArrayOfLong(register uint32_t *lp, tmsize_t n)
Definition: tif_swab.c:117

Referenced by _TIFFVSetField().

◆ _TIFFSwab64BitData()

void _TIFFSwab64BitData ( TIFF tif,
uint8_t buf,
tmsize_t  cc 
)

Definition at line 1676 of file tif_read.c.

1677{
1678 (void)tif;
1679 assert((cc & 7) == 0);
1680 TIFFSwabArrayOfDouble((double *)buf, cc / 8);
1681}
void TIFFSwabArrayOfDouble(double *dp, tmsize_t n)
Definition: tif_swab.c:222

Referenced by _TIFFVSetField().

◆ NoSanitizeSubUInt64()

static TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW uint64_t NoSanitizeSubUInt64 ( uint64_t  a,
uint64_t  b 
)
static

Definition at line 753 of file tif_read.c.

753{ return a - b; }
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
#define b
Definition: ke_i.h:79

Referenced by TIFFFillStrip().

◆ TIFFCheckRead()

static int TIFFCheckRead ( TIFF tif,
int  tiles 
)
static

Definition at line 1519 of file tif_read.c.

1520{
1521 if (tif->tif_mode == O_WRONLY)
1522 {
1523 TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
1524 return (0);
1525 }
1526 if (tiles ^ isTiled(tif))
1527 {
1528 TIFFErrorExtR(tif, tif->tif_name,
1529 tiles ? "Can not read tiles from a striped image"
1530 : "Can not read scanlines from a tiled image");
1531 return (0);
1532 }
1533 return (1);
1534}
#define O_WRONLY
Definition: fcntl.h:35
char * tif_name
Definition: tiffiop.h:114
int tif_mode
Definition: tiffiop.h:116
#define isTiled(tif)
Definition: tiffiop.h:274

Referenced by _TIFFReadEncodedTileAndAllocBuffer(), _TIFFReadTileAndAllocBuffer(), TIFFReadEncodedStripGetStripSize(), TIFFReadEncodedTile(), TIFFReadRawStrip(), TIFFReadRawTile(), TIFFReadScanline(), and TIFFReadTile().

◆ TIFFFillStrip()

int TIFFFillStrip ( TIFF tif,
uint32_t  strip 
)

Definition at line 759 of file tif_read.c.

760{
761 static const char module[] = "TIFFFillStrip";
762 TIFFDirectory *td = &tif->tif_dir;
763
764 if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
765 {
766 uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
767 if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
768 {
770 "Invalid strip byte count %" PRIu64
771 ", strip %" PRIu32,
772 bytecount, strip);
773 return (0);
774 }
775
776 /* To avoid excessive memory allocations: */
777 /* Byte count should normally not be larger than a number of */
778 /* times the uncompressed size plus some margin */
779 if (bytecount > 1024 * 1024)
780 {
781 /* 10 and 4096 are just values that could be adjusted. */
782 /* Hopefully they are safe enough for all codecs */
783 tmsize_t stripsize = TIFFStripSize(tif);
784 if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
785 {
786 uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
788 "Too large strip byte count %" PRIu64
789 ", strip %" PRIu32 ". Limiting to %" PRIu64,
790 bytecount, strip, newbytecount);
791 bytecount = newbytecount;
792 }
793 }
794
795 if (isMapped(tif))
796 {
797 /*
798 * We must check for overflow, potentially causing
799 * an OOB read. Instead of simple
800 *
801 * TIFFGetStrileOffset(tif, strip)+bytecount > tif->tif_size
802 *
803 * comparison (which can overflow) we do the following
804 * two comparisons:
805 */
806 if (bytecount > (uint64_t)tif->tif_size ||
807 TIFFGetStrileOffset(tif, strip) >
808 (uint64_t)tif->tif_size - bytecount)
809 {
810 /*
811 * This error message might seem strange, but
812 * it's what would happen if a read were done
813 * instead.
814 */
816 tif, module,
817
818 "Read error on strip %" PRIu32 "; "
819 "got %" PRIu64 " bytes, expected %" PRIu64,
820 strip,
822 TIFFGetStrileOffset(tif, strip)),
823 bytecount);
824 tif->tif_curstrip = NOSTRIP;
825 return (0);
826 }
827 }
828
829 if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
830 (tif->tif_flags & TIFF_NOBITREV)))
831 {
832 /*
833 * The image is mapped into memory and we either don't
834 * need to flip bits or the compression routine is
835 * going to handle this operation itself. In this
836 * case, avoid copying the raw data and instead just
837 * reference the data from the memory mapped file
838 * image. This assumes that the decompression
839 * routines do not modify the contents of the raw data
840 * buffer (if they try to, the application will get a
841 * fault since the file is mapped read-only).
842 */
843 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
844 {
845 _TIFFfreeExt(tif, tif->tif_rawdata);
846 tif->tif_rawdata = NULL;
847 tif->tif_rawdatasize = 0;
848 }
849 tif->tif_flags &= ~TIFF_MYBUFFER;
850 tif->tif_rawdatasize = (tmsize_t)bytecount;
851 tif->tif_rawdata =
852 tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, strip);
853 tif->tif_rawdataoff = 0;
854 tif->tif_rawdataloaded = (tmsize_t)bytecount;
855
856 /*
857 * When we have tif_rawdata reference directly into the memory
858 * mapped file we need to be pretty careful about how we use the
859 * rawdata. It is not a general purpose working buffer as it
860 * normally otherwise is. So we keep track of this fact to avoid
861 * using it improperly.
862 */
864 }
865 else
866 {
867 /*
868 * Expand raw data buffer, if needed, to hold data
869 * strip coming from file (perhaps should set upper
870 * bound on the size of a buffer we'll use?).
871 */
872 tmsize_t bytecountm;
873 bytecountm = (tmsize_t)bytecount;
874 if ((uint64_t)bytecountm != bytecount)
875 {
876 TIFFErrorExtR(tif, module, "Integer overflow");
877 return (0);
878 }
879 if (bytecountm > tif->tif_rawdatasize)
880 {
881 tif->tif_curstrip = NOSTRIP;
882 if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
883 {
885 tif, module,
886 "Data buffer too small to hold strip %" PRIu32, strip);
887 return (0);
888 }
889 }
890 if (tif->tif_flags & TIFF_BUFFERMMAP)
891 {
892 tif->tif_curstrip = NOSTRIP;
893 tif->tif_rawdata = NULL;
894 tif->tif_rawdatasize = 0;
895 tif->tif_flags &= ~TIFF_BUFFERMMAP;
896 }
897
898 if (isMapped(tif))
899 {
900 if (bytecountm > tif->tif_rawdatasize &&
901 !TIFFReadBufferSetup(tif, 0, bytecountm))
902 {
903 return (0);
904 }
905 if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata, bytecountm,
906 module) != bytecountm)
907 {
908 return (0);
909 }
910 }
911 else
912 {
913 if (TIFFReadRawStripOrTile2(tif, strip, 1, bytecountm,
914 module) != bytecountm)
915 {
916 return (0);
917 }
918 }
919
920 tif->tif_rawdataoff = 0;
921 tif->tif_rawdataloaded = bytecountm;
922
923 if (!isFillOrder(tif, td->td_fillorder) &&
924 (tif->tif_flags & TIFF_NOBITREV) == 0)
925 TIFFReverseBits(tif->tif_rawdata, bytecountm);
926 }
927 }
928 return (TIFFStartStrip(tif, strip));
929}
#define uint64_t
Definition: nsiface.idl:62
uint16_t td_fillorder
Definition: tif_dir.h:91
tmsize_t tif_rawdataloaded
Definition: tiffiop.h:223
tmsize_t tif_size
Definition: tiffiop.h:228
uint8_t * tif_rawdata
Definition: tiffiop.h:220
tmsize_t tif_rawdataoff
Definition: tiffiop.h:222
uint32_t tif_flags
Definition: tiffiop.h:117
uint32_t tif_curstrip
Definition: tiffiop.h:184
uint8_t * tif_base
Definition: tiffiop.h:227
uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile)
Definition: tif_dirread.c:8349
uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
Definition: tif_dirread.c:8366
void _TIFFfreeExt(TIFF *tif, void *p)
Definition: tif_open.c:275
#define TIFF_INT64_MAX
Definition: tif_read.c:51
int TIFFReadBufferSetup(TIFF *tif, void *bp, tmsize_t size)
Definition: tif_read.c:1375
static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf, tmsize_t size, const char *module)
Definition: tif_read.c:620
static TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW uint64_t NoSanitizeSubUInt64(uint64_t a, uint64_t b)
Definition: tif_read.c:753
static tmsize_t TIFFReadRawStripOrTile2(TIFF *tif, uint32_t strip_or_tile, int is_strip, tmsize_t size, const char *module)
Definition: tif_read.c:681
#define NOSTRIP
Definition: tif_read.c:42
static int TIFFStartStrip(TIFF *tif, uint32_t strip)
Definition: tif_read.c:1424
tmsize_t TIFFStripSize(TIFF *tif)
Definition: tif_strip.c:204
void TIFFReverseBits(uint8_t *cp, tmsize_t n)
Definition: tif_swab.c:310
#define TIFF_MYBUFFER
Definition: tiffiop.h:126
#define isMapped(tif)
Definition: tiffiop.h:275
#define isFillOrder(tif, o)
Definition: tiffiop.h:276
#define TIFF_NOREADRAW
Definition: tiffiop.h:135
#define TIFF_NOBITREV
Definition: tiffiop.h:125
#define TIFF_BUFFERMMAP
Definition: tiffiop.h:142

Referenced by _TIFFReadEncodedStripAndAllocBuffer(), TIFFFillStripPartial(), TIFFReadEncodedStrip(), and TIFFSeek().

◆ TIFFFillStripPartial()

static int TIFFFillStripPartial ( TIFF tif,
int  strip,
tmsize_t  read_ahead,
int  restart 
)
static

Definition at line 159 of file tif_read.c.

161{
162 static const char module[] = "TIFFFillStripPartial";
163 register TIFFDirectory *td = &tif->tif_dir;
164 tmsize_t unused_data;
165 uint64_t read_offset;
167 tmsize_t read_ahead_mod;
168 /* tmsize_t bytecountm; */
169
170 /*
171 * Expand raw data buffer, if needed, to hold data
172 * strip coming from file (perhaps should set upper
173 * bound on the size of a buffer we'll use?).
174 */
175
176 /* bytecountm=(tmsize_t) TIFFGetStrileByteCount(tif, strip); */
177
178 /* Not completely sure where the * 2 comes from, but probably for */
179 /* an exponentional growth strategy of tif_rawdatasize */
180 if (read_ahead < TIFF_TMSIZE_T_MAX / 2)
181 read_ahead_mod = read_ahead * 2;
182 else
183 read_ahead_mod = read_ahead;
184 if (read_ahead_mod > tif->tif_rawdatasize)
185 {
187
188 tif->tif_curstrip = NOSTRIP;
189 if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
190 {
192 "Data buffer too small to hold part of strip %d",
193 strip);
194 return (0);
195 }
196 }
197
198 if (restart)
199 {
200 tif->tif_rawdataloaded = 0;
201 tif->tif_rawdataoff = 0;
202 }
203
204 /*
205 ** If we are reading more data, move any unused data to the
206 ** start of the buffer.
207 */
208 if (tif->tif_rawdataloaded > 0)
209 unused_data =
210 tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);
211 else
212 unused_data = 0;
213
214 if (unused_data > 0)
215 {
216 assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);
217 memmove(tif->tif_rawdata, tif->tif_rawcp, unused_data);
218 }
219
220 /*
221 ** Seek to the point in the file where more data should be read.
222 */
223 read_offset = TIFFGetStrileOffset(tif, strip) + tif->tif_rawdataoff +
225
226 if (!SeekOK(tif, read_offset))
227 {
229 "Seek error at scanline %" PRIu32 ", strip %d",
230 tif->tif_row, strip);
231 return 0;
232 }
233
234 /*
235 ** How much do we want to read?
236 */
237 if (read_ahead_mod > tif->tif_rawdatasize)
238 to_read = read_ahead_mod - unused_data;
239 else
240 to_read = tif->tif_rawdatasize - unused_data;
241 if ((uint64_t)to_read > TIFFGetStrileByteCount(tif, strip) -
243 {
246 }
247
248 assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);
249 if (!TIFFReadAndRealloc(tif, to_read, unused_data, 1, /* is_strip */
250 0, /* strip_or_tile */
251 module))
252 {
253 return 0;
254 }
255
256 tif->tif_rawdataoff =
257 tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data;
258 tif->tif_rawdataloaded = unused_data + to_read;
259
260 tif->tif_rawcc = tif->tif_rawdataloaded;
261 tif->tif_rawcp = tif->tif_rawdata;
262
263 if (!isFillOrder(tif, td->td_fillorder) &&
264 (tif->tif_flags & TIFF_NOBITREV) == 0)
265 {
266 assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);
267 TIFFReverseBits(tif->tif_rawdata + unused_data, to_read);
268 }
269
270 /*
271 ** When starting a strip from the beginning we need to
272 ** restart the decoder.
273 */
274 if (restart)
275 {
276
277#ifdef JPEG_SUPPORT
278 /* A bit messy since breaks the codec abstraction. Ultimately */
279 /* there should be a function pointer for that, but it seems */
280 /* only JPEG is affected. */
281 /* For JPEG, if there are multiple scans (can generally be known */
282 /* with the read_ahead used), we need to read the whole strip */
284 (uint64_t)tif->tif_rawcc < TIFFGetStrileByteCount(tif, strip))
285 {
287 {
288 return TIFFFillStrip(tif, strip);
289 }
290 }
291#endif
292
293 return TIFFStartStrip(tif, strip);
294 }
295 else
296 {
297 return 1;
298 }
299}
void restart(int argc, const char *argv[])
Definition: cmds.c:2115
ULONG to_read
Definition: btrfs.c:4260
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
tmsize_t tif_rawcc
Definition: tiffiop.h:225
uint8_t * tif_rawcp
Definition: tiffiop.h:224
uint32_t tif_row
Definition: tiffiop.h:162
static int TIFFReadAndRealloc(TIFF *tif, tmsize_t size, tmsize_t rawdata_offset, int is_strip, uint32_t strip_or_tile, const char *module)
Definition: tif_read.c:55
#define COMPRESSION_JPEG
Definition: tiff.h:190
#define TIFF_TMSIZE_T_MAX
Definition: tiffio.h:68
#define SeekOK(tif, off)
Definition: tiffiop.h:298
int TIFFJPEGIsFullStripRequired(TIFF *)

Referenced by TIFFSeek().

◆ TIFFFillTile()

int TIFFFillTile ( TIFF tif,
uint32_t  tile 
)

Definition at line 1215 of file tif_read.c.

1216{
1217 static const char module[] = "TIFFFillTile";
1218 TIFFDirectory *td = &tif->tif_dir;
1219
1220 if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
1221 {
1222 uint64_t bytecount = TIFFGetStrileByteCount(tif, tile);
1223 if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
1224 {
1225 TIFFErrorExtR(tif, module,
1226 "%" PRIu64 ": Invalid tile byte count, tile %" PRIu32,
1227 bytecount, tile);
1228 return (0);
1229 }
1230
1231 /* To avoid excessive memory allocations: */
1232 /* Byte count should normally not be larger than a number of */
1233 /* times the uncompressed size plus some margin */
1234 if (bytecount > 1024 * 1024)
1235 {
1236 /* 10 and 4096 are just values that could be adjusted. */
1237 /* Hopefully they are safe enough for all codecs */
1238 tmsize_t stripsize = TIFFTileSize(tif);
1239 if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
1240 {
1241 uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
1242 TIFFErrorExtR(tif, module,
1243 "Too large tile byte count %" PRIu64
1244 ", tile %" PRIu32 ". Limiting to %" PRIu64,
1245 bytecount, tile, newbytecount);
1246 bytecount = newbytecount;
1247 }
1248 }
1249
1250 if (isMapped(tif))
1251 {
1252 /*
1253 * We must check for overflow, potentially causing
1254 * an OOB read. Instead of simple
1255 *
1256 * TIFFGetStrileOffset(tif, tile)+bytecount > tif->tif_size
1257 *
1258 * comparison (which can overflow) we do the following
1259 * two comparisons:
1260 */
1261 if (bytecount > (uint64_t)tif->tif_size ||
1262 TIFFGetStrileOffset(tif, tile) >
1263 (uint64_t)tif->tif_size - bytecount)
1264 {
1265 tif->tif_curtile = NOTILE;
1266 return (0);
1267 }
1268 }
1269
1270 if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
1271 (tif->tif_flags & TIFF_NOBITREV)))
1272 {
1273 /*
1274 * The image is mapped into memory and we either don't
1275 * need to flip bits or the compression routine is
1276 * going to handle this operation itself. In this
1277 * case, avoid copying the raw data and instead just
1278 * reference the data from the memory mapped file
1279 * image. This assumes that the decompression
1280 * routines do not modify the contents of the raw data
1281 * buffer (if they try to, the application will get a
1282 * fault since the file is mapped read-only).
1283 */
1284 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
1285 {
1286 _TIFFfreeExt(tif, tif->tif_rawdata);
1287 tif->tif_rawdata = NULL;
1288 tif->tif_rawdatasize = 0;
1289 }
1290 tif->tif_flags &= ~TIFF_MYBUFFER;
1291
1292 tif->tif_rawdatasize = (tmsize_t)bytecount;
1293 tif->tif_rawdata =
1294 tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, tile);
1295 tif->tif_rawdataoff = 0;
1296 tif->tif_rawdataloaded = (tmsize_t)bytecount;
1297 tif->tif_flags |= TIFF_BUFFERMMAP;
1298 }
1299 else
1300 {
1301 /*
1302 * Expand raw data buffer, if needed, to hold data
1303 * tile coming from file (perhaps should set upper
1304 * bound on the size of a buffer we'll use?).
1305 */
1306 tmsize_t bytecountm;
1307 bytecountm = (tmsize_t)bytecount;
1308 if ((uint64_t)bytecountm != bytecount)
1309 {
1310 TIFFErrorExtR(tif, module, "Integer overflow");
1311 return (0);
1312 }
1313 if (bytecountm > tif->tif_rawdatasize)
1314 {
1315 tif->tif_curtile = NOTILE;
1316 if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
1317 {
1318 TIFFErrorExtR(tif, module,
1319 "Data buffer too small to hold tile %" PRIu32,
1320 tile);
1321 return (0);
1322 }
1323 }
1324 if (tif->tif_flags & TIFF_BUFFERMMAP)
1325 {
1326 tif->tif_curtile = NOTILE;
1327 tif->tif_rawdata = NULL;
1328 tif->tif_rawdatasize = 0;
1329 tif->tif_flags &= ~TIFF_BUFFERMMAP;
1330 }
1331
1332 if (isMapped(tif))
1333 {
1334 if (bytecountm > tif->tif_rawdatasize &&
1335 !TIFFReadBufferSetup(tif, 0, bytecountm))
1336 {
1337 return (0);
1338 }
1339 if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata, bytecountm,
1340 module) != bytecountm)
1341 {
1342 return (0);
1343 }
1344 }
1345 else
1346 {
1347 if (TIFFReadRawStripOrTile2(tif, tile, 0, bytecountm, module) !=
1348 bytecountm)
1349 {
1350 return (0);
1351 }
1352 }
1353
1354 tif->tif_rawdataoff = 0;
1355 tif->tif_rawdataloaded = bytecountm;
1356
1357 if (tif->tif_rawdata != NULL &&
1358 !isFillOrder(tif, td->td_fillorder) &&
1359 (tif->tif_flags & TIFF_NOBITREV) == 0)
1361 }
1362 }
1363 return (TIFFStartTile(tif, tile));
1364}
uint32_t tif_curtile
Definition: tiffiop.h:194
#define NOTILE
Definition: tif_read.c:43
static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf, tmsize_t size, const char *module)
Definition: tif_read.c:1119
static int TIFFStartTile(TIFF *tif, uint32_t tile)
Definition: tif_read.c:1469
tmsize_t TIFFTileSize(TIFF *tif)
Definition: tif_tile.c:253

Referenced by _TIFFReadEncodedTileAndAllocBuffer(), and TIFFReadEncodedTile().

◆ TIFFReadAndRealloc()

static int TIFFReadAndRealloc ( TIFF tif,
tmsize_t  size,
tmsize_t  rawdata_offset,
int  is_strip,
uint32_t  strip_or_tile,
const char module 
)
static

Definition at line 55 of file tif_read.c.

58{
59#if SIZEOF_SIZE_T == 8
60 tmsize_t threshold = INITIAL_THRESHOLD;
61#endif
62 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 {
73 "Chunk size requested is larger than file size.");
74 return 0;
75 }
76 }
77#endif
78
79 /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
80 /* so as to avoid allocating too much memory in case the file is too */
81 /* short. We could ask for the file size, but this might be */
82 /* expensive with some I/O layers (think of reading a gzipped file) */
83 /* Restrict to 64 bit processes, so as to avoid reallocs() */
84 /* on 32 bit processes where virtual memory is scarce. */
85 while (already_read < size)
86 {
87 tmsize_t bytes_read;
88 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 if (already_read + to_read + rawdata_offset > tif->tif_rawdatasize)
98 {
99 uint8_t *new_rawdata;
100 assert((tif->tif_flags & TIFF_MYBUFFER) != 0);
102 (uint64_t)already_read + to_read + rawdata_offset, 1024);
103 if (tif->tif_rawdatasize == 0)
104 {
105 TIFFErrorExtR(tif, module, "Invalid buffer size");
106 return 0;
107 }
108 new_rawdata = (uint8_t *)_TIFFreallocExt(tif, tif->tif_rawdata,
109 tif->tif_rawdatasize);
110 if (new_rawdata == 0)
111 {
113 "No space for data buffer at scanline %" PRIu32,
114 tif->tif_row);
115 _TIFFfreeExt(tif, tif->tif_rawdata);
116 tif->tif_rawdata = 0;
117 tif->tif_rawdatasize = 0;
118 return 0;
119 }
120 tif->tif_rawdata = new_rawdata;
121 }
122 if (tif->tif_rawdata == NULL)
123 {
124 /* should not happen in practice but helps CoverityScan */
125 return 0;
126 }
127
128 bytes_read = TIFFReadFile(
129 tif, tif->tif_rawdata + rawdata_offset + already_read, to_read);
130 already_read += bytes_read;
131 if (bytes_read != to_read)
132 {
133 memset(tif->tif_rawdata + rawdata_offset + already_read, 0,
134 tif->tif_rawdatasize - rawdata_offset - already_read);
135 if (is_strip)
136 {
138 "Read error at scanline %" PRIu32
139 "; got %" TIFF_SSIZE_FORMAT " bytes, "
140 "expected %" TIFF_SSIZE_FORMAT,
141 tif->tif_row, already_read, size);
142 }
143 else
144 {
146 "Read error at row %" PRIu32 ", col %" PRIu32
147 ", tile %" PRIu32 "; "
148 "got %" TIFF_SSIZE_FORMAT
149 " bytes, expected %" TIFF_SSIZE_FORMAT "",
150 tif->tif_row, tif->tif_col, strip_or_tile,
151 already_read, size);
152 }
153 return 0;
154 }
155 }
156 return 1;
157}
GLsizeiptr size
Definition: glext.h:5919
#define memset(x, y, z)
Definition: compat.h:39
uint32_t tif_col
Definition: tiffiop.h:193
#define TIFF_SSIZE_FORMAT
Definition: tif_config.h:176
void * _TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
Definition: tif_open.c:235
#define MAX_THRESHOLD
Definition: tif_read.c:47
#define TIFFroundup_64(x, y)
Definition: tiffiop.h:321
#define TIFFReadFile(tif, buf, size)
Definition: tiffiop.h:278
#define TIFFGetFileSize(tif)
Definition: tiffiop.h:285

Referenced by TIFFFillStripPartial(), and TIFFReadRawStripOrTile2().

◆ TIFFReadBufferSetup()

int TIFFReadBufferSetup ( TIFF tif,
void bp,
tmsize_t  size 
)

Definition at line 1375 of file tif_read.c.

1376{
1377 static const char module[] = "TIFFReadBufferSetup";
1378
1379 assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
1380 tif->tif_flags &= ~TIFF_BUFFERMMAP;
1381
1382 if (tif->tif_rawdata)
1383 {
1384 if (tif->tif_flags & TIFF_MYBUFFER)
1385 _TIFFfreeExt(tif, tif->tif_rawdata);
1386 tif->tif_rawdata = NULL;
1387 tif->tif_rawdatasize = 0;
1388 }
1389 if (bp)
1390 {
1391 tif->tif_rawdatasize = size;
1392 tif->tif_rawdata = (uint8_t *)bp;
1393 tif->tif_flags &= ~TIFF_MYBUFFER;
1394 }
1395 else
1396 {
1398 if (tif->tif_rawdatasize == 0)
1399 {
1400 TIFFErrorExtR(tif, module, "Invalid buffer size");
1401 return (0);
1402 }
1403 /* Initialize to zero to avoid uninitialized buffers in case of */
1404 /* short reads (http://bugzilla.maptools.org/show_bug.cgi?id=2651) */
1405 tif->tif_rawdata =
1406 (uint8_t *)_TIFFcallocExt(tif, 1, tif->tif_rawdatasize);
1407 tif->tif_flags |= TIFF_MYBUFFER;
1408 }
1409 if (tif->tif_rawdata == NULL)
1410 {
1411 TIFFErrorExtR(tif, module,
1412 "No space for data buffer at scanline %" PRIu32,
1413 tif->tif_row);
1414 tif->tif_rawdatasize = 0;
1415 return (0);
1416 }
1417 return (1);
1418}
void * _TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
Definition: tif_open.c:201

Referenced by TIFFFillStrip(), and TIFFFillTile().

◆ TIFFReadEncodedStrip()

tmsize_t TIFFReadEncodedStrip ( TIFF tif,
uint32_t  strip,
void buf,
tmsize_t  size 
)

Definition at line 534 of file tif_read.c.

536{
537 static const char module[] = "TIFFReadEncodedStrip";
538 TIFFDirectory *td = &tif->tif_dir;
539 tmsize_t stripsize;
541
542 stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
543 if (stripsize == ((tmsize_t)(-1)))
544 return ((tmsize_t)(-1));
545
546 /* shortcut to avoid an extra memcpy() */
547 if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
548 size >= stripsize && !isMapped(tif) &&
549 ((tif->tif_flags & TIFF_NOREADRAW) == 0))
550 {
551 if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize)
552 return ((tmsize_t)(-1));
553
554 if (!isFillOrder(tif, td->td_fillorder) &&
555 (tif->tif_flags & TIFF_NOBITREV) == 0)
556 TIFFReverseBits(buf, stripsize);
557
558 (*tif->tif_postdecode)(tif, buf, stripsize);
559 return (stripsize);
560 }
561
562 if ((size != (tmsize_t)(-1)) && (size < stripsize))
563 stripsize = size;
564 if (!TIFFFillStrip(tif, strip))
565 {
566 /* The output buf may be NULL, in particular if TIFFTAG_FAXFILLFUNC
567 is being used. Thus, memset must be conditional on buf not NULL. */
568 if (buf)
569 memset(buf, 0, (size_t)stripsize);
570 return ((tmsize_t)(-1));
571 }
572 if ((*tif->tif_decodestrip)(tif, buf, stripsize, plane) <= 0)
573 return ((tmsize_t)(-1));
574 (*tif->tif_postdecode)(tif, buf, stripsize);
575 return (stripsize);
576}

Referenced by _TIFFReadEncodedStripAndAllocBuffer(), gtStripSeparate(), and tiff_decoder_read_tile().

◆ TIFFReadEncodedStripGetStripSize()

static tmsize_t TIFFReadEncodedStripGetStripSize ( TIFF tif,
uint32_t  strip,
uint16_t pplane 
)
static

Definition at line 488 of file tif_read.c.

490{
491 static const char module[] = "TIFFReadEncodedStrip";
492 TIFFDirectory *td = &tif->tif_dir;
493 uint32_t rowsperstrip;
494 uint32_t stripsperplane;
495 uint32_t stripinplane;
496 uint32_t rows;
497 tmsize_t stripsize;
498 if (!TIFFCheckRead(tif, 0))
499 return ((tmsize_t)(-1));
500 if (strip >= td->td_nstrips)
501 {
503 "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,
504 td->td_nstrips);
505 return ((tmsize_t)(-1));
506 }
507
508 rowsperstrip = td->td_rowsperstrip;
509 if (rowsperstrip > td->td_imagelength)
510 rowsperstrip = td->td_imagelength;
511 if (rowsperstrip == 0)
512 {
513 TIFFErrorExtR(tif, module, "rowsperstrip is zero");
514 return ((tmsize_t)(-1));
515 }
516 stripsperplane =
518 stripinplane = (strip % stripsperplane);
519 if (pplane)
520 *pplane = (uint16_t)(strip / stripsperplane);
521 rows = td->td_imagelength - stripinplane * rowsperstrip;
522 if (rows > rowsperstrip)
523 rows = rowsperstrip;
524 stripsize = TIFFVStripSize(tif, rows);
525 if (stripsize == 0)
526 return ((tmsize_t)(-1));
527 return stripsize;
528}
#define uint16_t
Definition: nsiface.idl:60
uint32_t td_rowsperstrip
Definition: tif_dir.h:94
uint32_t td_imagelength
Definition: tif_dir.h:83
tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
Definition: tif_strip.c:142
#define TIFFhowmany_32_maxuint_compat(x, y)
Definition: tiffiop.h:311

Referenced by _TIFFReadEncodedStripAndAllocBuffer(), and TIFFReadEncodedStrip().

◆ TIFFReadEncodedTile()

tmsize_t TIFFReadEncodedTile ( TIFF tif,
uint32_t  tile,
void buf,
tmsize_t  size 
)

Definition at line 953 of file tif_read.c.

954{
955 static const char module[] = "TIFFReadEncodedTile";
956 TIFFDirectory *td = &tif->tif_dir;
957 tmsize_t tilesize = tif->tif_tilesize;
958
959 if (!TIFFCheckRead(tif, 1))
960 return ((tmsize_t)(-1));
961 if (tile >= td->td_nstrips)
962 {
964 "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
965 td->td_nstrips);
966 return ((tmsize_t)(-1));
967 }
968
969 /* shortcut to avoid an extra memcpy() */
970 if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
971 size >= tilesize && !isMapped(tif) &&
972 ((tif->tif_flags & TIFF_NOREADRAW) == 0))
973 {
974 if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize)
975 return ((tmsize_t)(-1));
976
977 if (!isFillOrder(tif, td->td_fillorder) &&
978 (tif->tif_flags & TIFF_NOBITREV) == 0)
979 TIFFReverseBits(buf, tilesize);
980
981 (*tif->tif_postdecode)(tif, buf, tilesize);
982 return (tilesize);
983 }
984
985 if (size == (tmsize_t)(-1))
986 size = tilesize;
987 else if (size > tilesize)
988 size = tilesize;
989 if (!TIFFFillTile(tif, tile))
990 {
991 /* See TIFFReadEncodedStrip comment regarding TIFFTAG_FAXFILLFUNC. */
992 if (buf)
993 memset(buf, 0, (size_t)size);
994 return ((tmsize_t)(-1));
995 }
996 else if ((*tif->tif_decodetile)(tif, (uint8_t *)buf, size,
997 (uint16_t)(tile / td->td_stripsperimage)))
998 {
999 (*tif->tif_postdecode)(tif, (uint8_t *)buf, size);
1000 return (size);
1001 }
1002 else
1003 return ((tmsize_t)(-1));
1004}

Referenced by _TIFFReadEncodedTileAndAllocBuffer(), tiff_decoder_read_tile(), and TIFFReadTile().

◆ TIFFReadFromUserBuffer()

int TIFFReadFromUserBuffer ( TIFF tif,
uint32_t  strile,
void inbuf,
tmsize_t  insize,
void outbuf,
tmsize_t  outsize 
)

Definition at line 1545 of file tif_read.c.

1547{
1548 static const char module[] = "TIFFReadFromUserBuffer";
1549 TIFFDirectory *td = &tif->tif_dir;
1550 int ret = 1;
1551 uint32_t old_tif_flags = tif->tif_flags;
1552 tmsize_t old_rawdatasize = tif->tif_rawdatasize;
1553 void *old_rawdata = tif->tif_rawdata;
1554
1555 if (tif->tif_mode == O_WRONLY)
1556 {
1557 TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
1558 return 0;
1559 }
1560 if (tif->tif_flags & TIFF_NOREADRAW)
1561 {
1562 TIFFErrorExtR(tif, module,
1563 "Compression scheme does not support access to raw "
1564 "uncompressed data");
1565 return 0;
1566 }
1567
1568 tif->tif_flags &= ~TIFF_MYBUFFER;
1569 tif->tif_flags |= TIFF_BUFFERMMAP;
1570 tif->tif_rawdatasize = insize;
1571 tif->tif_rawdata = inbuf;
1572 tif->tif_rawdataoff = 0;
1574
1575 if (!isFillOrder(tif, td->td_fillorder) &&
1576 (tif->tif_flags & TIFF_NOBITREV) == 0)
1577 {
1579 }
1580
1581 if (TIFFIsTiled(tif))
1582 {
1583 if (!TIFFStartTile(tif, strile))
1584 {
1585 ret = 0;
1586 /* See related TIFFReadEncodedStrip comment. */
1587 if (outbuf)
1588 memset(outbuf, 0, (size_t)outsize);
1589 }
1590 else if (!(*tif->tif_decodetile)(
1591 tif, (uint8_t *)outbuf, outsize,
1592 (uint16_t)(strile / td->td_stripsperimage)))
1593 {
1594 ret = 0;
1595 }
1596 }
1597 else
1598 {
1599 uint32_t rowsperstrip = td->td_rowsperstrip;
1600 uint32_t stripsperplane;
1601 if (rowsperstrip > td->td_imagelength)
1602 rowsperstrip = td->td_imagelength;
1603 if (rowsperstrip == 0)
1604 {
1605 TIFFErrorExtR(tif, module, "rowsperstrip is zero");
1606 ret = 0;
1607 }
1608 else
1609 {
1610 stripsperplane =
1612 if (!TIFFStartStrip(tif, strile))
1613 {
1614 ret = 0;
1615 /* See related TIFFReadEncodedStrip comment. */
1616 if (outbuf)
1617 memset(outbuf, 0, (size_t)outsize);
1618 }
1619 else if (!(*tif->tif_decodestrip)(
1620 tif, (uint8_t *)outbuf, outsize,
1621 (uint16_t)(strile / stripsperplane)))
1622 {
1623 ret = 0;
1624 }
1625 }
1626 }
1627 if (ret)
1628 {
1629 (*tif->tif_postdecode)(tif, (uint8_t *)outbuf, outsize);
1630 }
1631
1632 if (!isFillOrder(tif, td->td_fillorder) &&
1633 (tif->tif_flags & TIFF_NOBITREV) == 0)
1634 {
1636 }
1637
1638 tif->tif_flags = (old_tif_flags & (TIFF_MYBUFFER | TIFF_BUFFERMMAP)) |
1640 tif->tif_rawdatasize = old_rawdatasize;
1641 tif->tif_rawdata = old_rawdata;
1642 tif->tif_rawdataoff = 0;
1643 tif->tif_rawdataloaded = 0;
1644
1645 return ret;
1646}
static int inbuf
Definition: adnsresfilter.c:73
return ret
Definition: mutex.c:146
const unsigned char size_t insize
Definition: jpeglib.h:986
unsigned char size_t * outsize
Definition: jpeglib.h:983
int TIFFIsTiled(TIFF *tif)
Definition: tif_open.c:859

◆ TIFFReadRawStrip()

tmsize_t TIFFReadRawStrip ( TIFF tif,
uint32_t  strip,
void buf,
tmsize_t  size 
)

Definition at line 717 of file tif_read.c.

718{
719 static const char module[] = "TIFFReadRawStrip";
720 TIFFDirectory *td = &tif->tif_dir;
721 uint64_t bytecount64;
722 tmsize_t bytecountm;
723
724 if (!TIFFCheckRead(tif, 0))
725 return ((tmsize_t)(-1));
726 if (strip >= td->td_nstrips)
727 {
729 "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,
730 td->td_nstrips);
731 return ((tmsize_t)(-1));
732 }
733 if (tif->tif_flags & TIFF_NOREADRAW)
734 {
736 "Compression scheme does not support access to raw "
737 "uncompressed data");
738 return ((tmsize_t)(-1));
739 }
740 bytecount64 = TIFFGetStrileByteCount(tif, strip);
741 if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)
742 bytecountm = size;
743 else
744 bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
745 if (bytecountm == 0)
746 {
747 return ((tmsize_t)(-1));
748 }
749 return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
750}
tmsize_t _TIFFCastUInt64ToSSize(TIFF *tif, uint64_t val, const char *module)
Definition: tif_aux.c:84

◆ TIFFReadRawStrip1()

static tmsize_t TIFFReadRawStrip1 ( TIFF tif,
uint32_t  strip,
void buf,
tmsize_t  size,
const char module 
)
static

Definition at line 620 of file tif_read.c.

622{
623 assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
624 if (!isMapped(tif))
625 {
626 tmsize_t cc;
627
628 if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip)))
629 {
631 "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
632 tif->tif_row, strip);
633 return ((tmsize_t)(-1));
634 }
635 cc = TIFFReadFile(tif, buf, size);
636 if (cc != size)
637 {
639 "Read error at scanline %" PRIu32
640 "; got %" TIFF_SSIZE_FORMAT
641 " bytes, expected %" TIFF_SSIZE_FORMAT,
642 tif->tif_row, cc, size);
643 return ((tmsize_t)(-1));
644 }
645 }
646 else
647 {
648 tmsize_t ma = 0;
649 tmsize_t n;
650 if ((TIFFGetStrileOffset(tif, strip) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
651 ((ma = (tmsize_t)TIFFGetStrileOffset(tif, strip)) > tif->tif_size))
652 {
653 n = 0;
654 }
655 else if (ma > TIFF_TMSIZE_T_MAX - size)
656 {
657 n = 0;
658 }
659 else
660 {
661 tmsize_t mb = ma + size;
662 if (mb > tif->tif_size)
663 n = tif->tif_size - ma;
664 else
665 n = size;
666 }
667 if (n != size)
668 {
670 "Read error at scanline %" PRIu32 ", strip %" PRIu32
671 "; got %" TIFF_SSIZE_FORMAT
672 " bytes, expected %" TIFF_SSIZE_FORMAT,
673 tif->tif_row, strip, n, size);
674 return ((tmsize_t)(-1));
675 }
676 _TIFFmemcpy(buf, tif->tif_base + ma, size);
677 }
678 return (size);
679}
GLdouble n
Definition: glext.h:7729
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Definition: tif_unix.c:355

Referenced by TIFFFillStrip(), TIFFReadEncodedStrip(), and TIFFReadRawStrip().

◆ TIFFReadRawStripOrTile2()

static tmsize_t TIFFReadRawStripOrTile2 ( TIFF tif,
uint32_t  strip_or_tile,
int  is_strip,
tmsize_t  size,
const char module 
)
static

Definition at line 681 of file tif_read.c.

684{
685 assert(!isMapped(tif));
686 assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
687
688 if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip_or_tile)))
689 {
690 if (is_strip)
691 {
693 "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
694 tif->tif_row, strip_or_tile);
695 }
696 else
697 {
699 "Seek error at row %" PRIu32 ", col %" PRIu32
700 ", tile %" PRIu32,
701 tif->tif_row, tif->tif_col, strip_or_tile);
702 }
703 return ((tmsize_t)(-1));
704 }
705
706 if (!TIFFReadAndRealloc(tif, size, 0, is_strip, strip_or_tile, module))
707 {
708 return ((tmsize_t)(-1));
709 }
710
711 return (size);
712}

Referenced by TIFFFillStrip(), and TIFFFillTile().

◆ TIFFReadRawTile()

tmsize_t TIFFReadRawTile ( TIFF tif,
uint32_t  tile,
void buf,
tmsize_t  size 
)

Definition at line 1176 of file tif_read.c.

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

◆ TIFFReadRawTile1()

static tmsize_t TIFFReadRawTile1 ( TIFF tif,
uint32_t  tile,
void buf,
tmsize_t  size,
const char module 
)
static

Definition at line 1119 of file tif_read.c.

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

Referenced by TIFFFillTile(), TIFFReadEncodedTile(), and TIFFReadRawTile().

◆ TIFFReadScanline()

int TIFFReadScanline ( TIFF tif,
void buf,
uint32_t  row,
uint16_t  sample 
)

Definition at line 454 of file tif_read.c.

455{
456 int e;
457
458 if (!TIFFCheckRead(tif, 0))
459 return (-1);
460 if ((e = TIFFSeek(tif, row, sample)) != 0)
461 {
462 /*
463 * Decompress desired row into user buffer.
464 */
465 e = (*tif->tif_decoderow)(tif, (uint8_t *)buf, tif->tif_scanlinesize,
466 sample);
467
468 /* we are now poised at the beginning of the next row */
469 tif->tif_row = row + 1;
470
471 if (e)
472 (*tif->tif_postdecode)(tif, (uint8_t *)buf, tif->tif_scanlinesize);
473 }
474 else
475 {
476 /* See TIFFReadEncodedStrip comment regarding TIFFTAG_FAXFILLFUNC. */
477 if (buf)
478 memset(buf, 0, (size_t)tif->tif_scanlinesize);
479 }
480 return (e > 0 ? 1 : -1);
481}
struct png_info_def *typedef unsigned char **typedef struct png_info_def *typedef struct png_info_def *typedef struct png_info_def *typedef unsigned char ** row
Definition: typeof.h:78
#define e
Definition: ke_i.h:82
tmsize_t tif_scanlinesize
Definition: tiffiop.h:218
TIFFCodeMethod tif_decoderow
Definition: tiffiop.h:205
static int TIFFSeek(TIFF *tif, uint32_t row, uint16_t sample)
Definition: tif_read.c:309

◆ TIFFReadTile()

tmsize_t TIFFReadTile ( TIFF tif,
void buf,
uint32_t  x,
uint32_t  y,
uint32_t  z,
uint16_t  s 
)

Definition at line 940 of file tif_read.c.

942{
943 if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
944 return ((tmsize_t)(-1));
945 return (TIFFReadEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf,
946 (tmsize_t)(-1)));
947}

Referenced by gtTileSeparate().

◆ TIFFSeek()

static int TIFFSeek ( TIFF tif,
uint32_t  row,
uint16_t  sample 
)
static

Definition at line 309 of file tif_read.c.

310{
311 register TIFFDirectory *td = &tif->tif_dir;
312 uint32_t strip;
313 int whole_strip;
314 tmsize_t read_ahead = 0;
315
316 /*
317 ** Establish what strip we are working from.
318 */
319 if (row >= td->td_imagelength)
320 { /* out of range */
321 TIFFErrorExtR(tif, tif->tif_name,
322 "%" PRIu32 ": Row out of range, max %" PRIu32 "", row,
323 td->td_imagelength);
324 return (0);
325 }
327 {
328 if (sample >= td->td_samplesperpixel)
329 {
330 TIFFErrorExtR(tif, tif->tif_name,
331 "%" PRIu16 ": Sample out of range, max %" PRIu16 "",
332 sample, td->td_samplesperpixel);
333 return (0);
334 }
335 strip = (uint32_t)sample * td->td_stripsperimage +
336 row / td->td_rowsperstrip;
337 }
338 else
339 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 ||
349 {
350 /* Ideally plugins should have a way to declare they don't support
351 * chunk strip */
352 whole_strip = 1;
353 }
354
355 if (!whole_strip)
356 {
357 /* 16 is for YCbCr mode where we may need to read 16 */
358 /* lines at a time to get a decompressed line, and 5000 */
359 /* is some constant value, for example for JPEG tables */
360
361 /* coverity[dead_error_line:SUPPRESS] */
362 if (tif->tif_scanlinesize < TIFF_TMSIZE_T_MAX / 16 &&
363 tif->tif_scanlinesize * 16 < TIFF_TMSIZE_T_MAX - 5000)
364 {
365 read_ahead = tif->tif_scanlinesize * 16 + 5000;
366 }
367 else
368 {
369 read_ahead = tif->tif_scanlinesize;
370 }
371 }
372#else
373 whole_strip = 1;
374#endif
375
376 /*
377 * If we haven't loaded this strip, do so now, possibly
378 * only reading the first part.
379 */
380 if (strip != tif->tif_curstrip)
381 { /* different strip, refill */
382
383 if (whole_strip)
384 {
385 if (!TIFFFillStrip(tif, strip))
386 return (0);
387 }
388#if defined(CHUNKY_STRIP_READ_SUPPORT)
389 else
390 {
391 if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))
392 return 0;
393 }
394#endif
395 }
396
397#if defined(CHUNKY_STRIP_READ_SUPPORT)
398 /*
399 ** If we already have some data loaded, do we need to read some more?
400 */
401 else if (!whole_strip)
402 {
403 /* coverity[dead_error_line:SUPPRESS] */
404 if (((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) <
405 read_ahead &&
407 TIFFGetStrileByteCount(tif, strip))
408 {
409 if (!TIFFFillStripPartial(tif, strip, read_ahead, 0))
410 return 0;
411 }
412 }
413#endif
414
415 if (row < tif->tif_row)
416 {
417 /*
418 * Moving backwards within the same strip: backup
419 * to the start and then decode forward (below).
420 *
421 * NB: If you're planning on lots of random access within a
422 * strip, it's better to just read and decode the entire
423 * strip, and then access the decoded data in a random fashion.
424 */
425
426 if (tif->tif_rawdataoff != 0)
427 {
428 if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))
429 return 0;
430 }
431 else
432 {
433 if (!TIFFStartStrip(tif, strip))
434 return (0);
435 }
436 }
437
438 if (row != tif->tif_row)
439 {
440 /*
441 * Seek forward to the desired row.
442 */
443
444 /* TODO: Will this really work with partial buffers? */
445
446 if (!(*tif->tif_seek)(tif, row - tif->tif_row))
447 return (0);
448 tif->tif_row = row;
449 }
450
451 return (1);
452}
#define PRIu16
Definition: inttypes.h:83
#define uint32_t
Definition: nsiface.idl:61
TIFFSeekMethod tif_seek
Definition: tiffiop.h:212
static int TIFFFillStripPartial(TIFF *tif, int strip, tmsize_t read_ahead, int restart)
Definition: tif_read.c:159
#define PLANARCONFIG_SEPARATE
Definition: tiff.h:266
#define COMPRESSION_JBIG
Definition: tiff.h:209
#define COMPRESSION_LERC
Definition: tiff.h:213

Referenced by TIFFReadScanline().

◆ TIFFStartStrip()

static int TIFFStartStrip ( TIFF tif,
uint32_t  strip 
)
static

Definition at line 1424 of file tif_read.c.

1425{
1426 TIFFDirectory *td = &tif->tif_dir;
1427
1428 if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
1429 {
1430 if (!(*tif->tif_setupdecode)(tif))
1431 return (0);
1432 tif->tif_flags |= TIFF_CODERSETUP;
1433 }
1434 tif->tif_curstrip = strip;
1435 tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
1436 tif->tif_flags &= ~TIFF_BUF4WRITE;
1437
1438 if (tif->tif_flags & TIFF_NOREADRAW)
1439 {
1440 tif->tif_rawcp = NULL;
1441 tif->tif_rawcc = 0;
1442 }
1443 else
1444 {
1445 tif->tif_rawcp = tif->tif_rawdata;
1446 if (tif->tif_rawdataloaded > 0)
1447 tif->tif_rawcc = tif->tif_rawdataloaded;
1448 else
1449 tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, strip);
1450 }
1451 if ((*tif->tif_predecode)(tif, (uint16_t)(strip / td->td_stripsperimage)) ==
1452 0)
1453 {
1454 /* Needed for example for scanline access, if tif_predecode */
1455 /* fails, and we try to read the same strip again. Without invalidating
1456 */
1457 /* tif_curstrip, we'd call tif_decoderow() on a possibly invalid */
1458 /* codec state. */
1459 tif->tif_curstrip = NOSTRIP;
1460 return 0;
1461 }
1462 return 1;
1463}
TIFFPreMethod tif_predecode
Definition: tiffiop.h:200
TIFFBoolMethod tif_setupdecode
Definition: tiffiop.h:199
#define TIFF_CODERSETUP
Definition: tiffiop.h:122
#define TIFF_BUF4WRITE
Definition: tiffiop.h:139

Referenced by TIFFFillStrip(), TIFFFillStripPartial(), TIFFReadFromUserBuffer(), and TIFFSeek().

◆ TIFFStartTile()

static int TIFFStartTile ( TIFF tif,
uint32_t  tile 
)
static

Definition at line 1469 of file tif_read.c.

1470{
1471 static const char module[] = "TIFFStartTile";
1472 TIFFDirectory *td = &tif->tif_dir;
1473 uint32_t howmany32;
1474
1475 if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
1476 {
1477 if (!(*tif->tif_setupdecode)(tif))
1478 return (0);
1479 tif->tif_flags |= TIFF_CODERSETUP;
1480 }
1481 tif->tif_curtile = tile;
1482 if (td->td_tilewidth == 0)
1483 {
1484 TIFFErrorExtR(tif, module, "Zero tilewidth");
1485 return 0;
1486 }
1487 howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
1488 if (howmany32 == 0)
1489 {
1490 TIFFErrorExtR(tif, module, "Zero tiles");
1491 return 0;
1492 }
1493 tif->tif_row = (tile % howmany32) * td->td_tilelength;
1494 howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
1495 if (howmany32 == 0)
1496 {
1497 TIFFErrorExtR(tif, module, "Zero tiles");
1498 return 0;
1499 }
1500 tif->tif_col = (tile % howmany32) * td->td_tilewidth;
1501 tif->tif_flags &= ~TIFF_BUF4WRITE;
1502 if (tif->tif_flags & TIFF_NOREADRAW)
1503 {
1504 tif->tif_rawcp = NULL;
1505 tif->tif_rawcc = 0;
1506 }
1507 else
1508 {
1509 tif->tif_rawcp = tif->tif_rawdata;
1510 if (tif->tif_rawdataloaded > 0)
1511 tif->tif_rawcc = tif->tif_rawdataloaded;
1512 else
1513 tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, tile);
1514 }
1515 return (
1516 (*tif->tif_predecode)(tif, (uint16_t)(tile / td->td_stripsperimage)));
1517}
uint32_t td_imagewidth
Definition: tif_dir.h:83
uint32_t td_tilewidth
Definition: tif_dir.h:84
uint32_t td_tilelength
Definition: tif_dir.h:84
#define TIFFhowmany_32(x, y)
Definition: tiffiop.h:305

Referenced by TIFFFillTile(), and TIFFReadFromUserBuffer().