libtiff/tif_dirread.c:4210: error: Buffer Overrun L3
  Offset: [2, +oo] Size: [0, +oo] by call to `EstimateStripByteCounts`.

libtiff/tif_dirread.c:3795:29: Call
3793. 	 */
3794. 	if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
3795. 		tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);  
                                  ^
3796. 		tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
3797. 		tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;

libtiff/tif_strip.c:64:13: Assignment
  62. 	uint32 nstrips;
  63. 
  64. 	nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
                  ^
  65. 	     TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
  66. 	if (td->td_planarconfig == PLANARCONFIG_SEPARATE)

libtiff/tif_strip.c:64:2: Assignment
  62. 	uint32 nstrips;
  63. 
  64. 	nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
       ^
  65. 	     TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
  66. 	if (td->td_planarconfig == PLANARCONFIG_SEPARATE)

libtiff/tif_strip.c:69:2: Assignment
  67. 		nstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,
  68. 		    "TIFFNumberOfStrips");
  69. 	return (nstrips);
       ^
  70. }
  71. 

libtiff/tif_dirread.c:3795:3: Assignment
3793. 	 */
3794. 	if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
3795. 		tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);  
        ^
3796. 		tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
3797. 		tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;

libtiff/tif_dirread.c:4195:10: Call
4193. 			   && tif->tif_dir.td_nstrips > 2
4194. 			   && tif->tif_dir.td_compression == COMPRESSION_NONE
4195. 			   && TIFFGetStrileByteCount(tif, 0) != TIFFGetStrileByteCount(tif, 1)
               ^
4196. 			   && TIFFGetStrileByteCount(tif, 0) != 0
4197. 			   && TIFFGetStrileByteCount(tif, 1) != 0 ) {

libtiff/tif_dirread.c:6305:1: Parameter `tif->tif_dir.td_nstrips`
  6303. 
  6304. /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
  6305. uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
        ^
  6306. {
  6307.     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);

libtiff/tif_dirread.c:6307:12: Call
  6305. uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
  6306. {
  6307.     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
                   ^
  6308. }
  6309. 

libtiff/tif_dirread.c:6311:1: Parameter `tif->tif_dir.td_nstrips`
    6309. 
    6310. /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
    6311. uint64 TIFFGetStrileByteCountWithErr(TIFF *tif, uint32 strile, int *pbErr)
          ^
    6312. {
    6313.     TIFFDirectory *td = &tif->tif_dir;

libtiff/tif_dirread.c:6314:12: Call
    6312. {
    6313.     TIFFDirectory *td = &tif->tif_dir;
    6314.     return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
                     ^
    6315.                                &(td->td_stripbytecount_entry),
    6316.                                &(td->td_stripbytecount_p), pbErr);

libtiff/tif_dirread.c:6246:1: Parameter `strile`
      6244. }
      6245. 
      6246. static uint64 _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32 strile,
            ^
      6247.                                                    TIFFDirEntry* dirent,
      6248.                                                    uint64** parray,

libtiff/tif_dirread.c:4195:44: Call
4193. 			   && tif->tif_dir.td_nstrips > 2
4194. 			   && tif->tif_dir.td_compression == COMPRESSION_NONE
4195. 			   && TIFFGetStrileByteCount(tif, 0) != TIFFGetStrileByteCount(tif, 1)
                                                 ^
4196. 			   && TIFFGetStrileByteCount(tif, 0) != 0
4197. 			   && TIFFGetStrileByteCount(tif, 1) != 0 ) {

libtiff/tif_dirread.c:6305:1: Parameter `tif->tif_dir.td_nstrips`
  6303. 
  6304. /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
  6305. uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
        ^
  6306. {
  6307.     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);

libtiff/tif_dirread.c:6307:12: Call
  6305. uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
  6306. {
  6307.     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
                   ^
  6308. }
  6309. 

libtiff/tif_dirread.c:6311:1: Parameter `tif->tif_dir.td_nstrips`
    6309. 
    6310. /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
    6311. uint64 TIFFGetStrileByteCountWithErr(TIFF *tif, uint32 strile, int *pbErr)
          ^
    6312. {
    6313.     TIFFDirectory *td = &tif->tif_dir;

libtiff/tif_dirread.c:6314:12: Call
    6312. {
    6313.     TIFFDirectory *td = &tif->tif_dir;
    6314.     return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
                     ^
    6315.                                &(td->td_stripbytecount_entry),
    6316.                                &(td->td_stripbytecount_p), pbErr);

libtiff/tif_dirread.c:6246:1: Parameter `strile`
      6244. }
      6245. 
      6246. static uint64 _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32 strile,
            ^
      6247.                                                    TIFFDirEntry* dirent,
      6248.                                                    uint64** parray,

libtiff/tif_dirread.c:4196:10: Call
4194. 			   && tif->tif_dir.td_compression == COMPRESSION_NONE
4195. 			   && TIFFGetStrileByteCount(tif, 0) != TIFFGetStrileByteCount(tif, 1)
4196. 			   && TIFFGetStrileByteCount(tif, 0) != 0
               ^
4197. 			   && TIFFGetStrileByteCount(tif, 1) != 0 ) {
4198. 			/*

libtiff/tif_dirread.c:6305:1: Parameter `tif->tif_dir.td_nstrips`
  6303. 
  6304. /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
  6305. uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
        ^
  6306. {
  6307.     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);

libtiff/tif_dirread.c:6307:12: Call
  6305. uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
  6306. {
  6307.     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
                   ^
  6308. }
  6309. 

libtiff/tif_dirread.c:6311:1: Parameter `tif->tif_dir.td_nstrips`
    6309. 
    6310. /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
    6311. uint64 TIFFGetStrileByteCountWithErr(TIFF *tif, uint32 strile, int *pbErr)
          ^
    6312. {
    6313.     TIFFDirectory *td = &tif->tif_dir;

libtiff/tif_dirread.c:6314:12: Call
    6312. {
    6313.     TIFFDirectory *td = &tif->tif_dir;
    6314.     return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
                     ^
    6315.                                &(td->td_stripbytecount_entry),
    6316.                                &(td->td_stripbytecount_p), pbErr);

libtiff/tif_dirread.c:6246:1: Parameter `strile`
      6244. }
      6245. 
      6246. static uint64 _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32 strile,
            ^
      6247.                                                    TIFFDirEntry* dirent,
      6248.                                                    uint64** parray,

libtiff/tif_dirread.c:4197:10: Call
4195. 			   && TIFFGetStrileByteCount(tif, 0) != TIFFGetStrileByteCount(tif, 1)
4196. 			   && TIFFGetStrileByteCount(tif, 0) != 0
4197. 			   && TIFFGetStrileByteCount(tif, 1) != 0 ) {
               ^
4198. 			/*
4199. 			 * XXX: Some vendors fill StripByteCount array with

libtiff/tif_dirread.c:6305:1: Parameter `tif->tif_dir.td_nstrips`
  6303. 
  6304. /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
  6305. uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
        ^
  6306. {
  6307.     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);

libtiff/tif_dirread.c:6307:12: Call
  6305. uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
  6306. {
  6307.     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
                   ^
  6308. }
  6309. 

libtiff/tif_dirread.c:6311:1: Parameter `tif->tif_dir.td_nstrips`
    6309. 
    6310. /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
    6311. uint64 TIFFGetStrileByteCountWithErr(TIFF *tif, uint32 strile, int *pbErr)
          ^
    6312. {
    6313.     TIFFDirectory *td = &tif->tif_dir;

libtiff/tif_dirread.c:6314:12: Call
    6312. {
    6313.     TIFFDirectory *td = &tif->tif_dir;
    6314.     return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
                     ^
    6315.                                &(td->td_stripbytecount_entry),
    6316.                                &(td->td_stripbytecount_p), pbErr);

libtiff/tif_dirread.c:6246:1: Parameter `strile`
      6244. }
      6245. 
      6246. static uint64 _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32 strile,
            ^
      6247.                                                    TIFFDirEntry* dirent,
      6248.                                                    uint64** parray,

libtiff/tif_dirread.c:4210:8: Call
4208. 			TIFFWarningExt(tif->tif_clientdata, module,
4209. 			    "Wrong \"StripByteCounts\" field, ignoring and calculating from imagelength");
4210. 			if (EstimateStripByteCounts(tif, dir, dircount) < 0)
             ^
4211. 			    goto bad;
4212. 		}

libtiff/tif_dirread.c:4508:1: <Offset trace>
4506. }
4507. 
4508. static int
      ^
4509. EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
4510. {

libtiff/tif_dirread.c:4508:1: Parameter `tif->tif_dir.td_nstrips`
  4506. }
  4507. 
  4508. static int
        ^
  4509. EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
  4510. {

libtiff/tif_dirread.c:4583:3: Assignment
  4581. 		 * of data in the strip and trim this number back accordingly.
  4582. 		 */
  4583. 		strip--;
          ^
  4584.                 if (td->td_stripoffset_p[strip] > TIFF_UINT64_MAX - td->td_stripbytecount_p[strip])
  4585.                     return -1;

libtiff/tif_dirread.c:4508:1: <Length trace>
4506. }
4507. 
4508. static int
      ^
4509. EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
4510. {

libtiff/tif_dirread.c:4508:1: Parameter `*tif->tif_dir.td_stripoffset_p`
  4506. }
  4507. 
  4508. static int
        ^
  4509. EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
  4510. {

libtiff/tif_dirread.c:4518:14: Call
  4516. 
  4517.     /* Do not try to load stripbytecount as we will compute it */
  4518.         if( !_TIFFFillStrilesInternal( tif, 0 ) )
                     ^
  4519.             return -1;
  4520. 

libtiff/tif_dirread.c:6325:1: Parameter `*tif->tif_dir.td_stripoffset_p`
    6323. }
    6324. 
    6325. static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount )
          ^
    6326. {
    6327.     register TIFFDirectory *td = &tif->tif_dir;

libtiff/tif_dirread.c:4584:21: Array access: Offset: [2, +oo] Size: [0, +oo] by call to `EstimateStripByteCounts` 
  4582. 		 */
  4583. 		strip--;
  4584.                 if (td->td_stripoffset_p[strip] > TIFF_UINT64_MAX - td->td_stripbytecount_p[strip])
                            ^
  4585.                     return -1;
  4586. 		if (td->td_stripoffset_p[strip]+td->td_stripbytecount_p[strip] > filesize) {

static int
EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
{
	static const char module[] = "EstimateStripByteCounts";
	TIFFDirEntry *dp;
	TIFFDirectory *td = &tif->tif_dir;
	uint32 strip;
        if( !_TIFFFillStrilesInternal( tif, 0 ) )
            return -1;
	if (td->td_stripbytecount_p)
		_TIFFfree(td->td_stripbytecount_p);
	td->td_stripbytecount_p = (uint64*)
	    _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
		"for \"StripByteCounts\" array");
        if( td->td_stripbytecount_p == NULL )
            return -1;
	if (td->td_compression != COMPRESSION_NONE) {
		uint64 space;
		uint64 filesize;
		uint16 n;
		filesize = TIFFGetFileSize(tif);
		if (!(tif->tif_flags&TIFF_BIGTIFF))
			space=sizeof(TIFFHeaderClassic)+2+dircount*12+4;
		else
			space=sizeof(TIFFHeaderBig)+8+dircount*20+8;
		for (dp = dir, n = dircount; n > 0; n--, dp++)
		{
			uint32 typewidth;
			uint64 datasize;
			typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
			if (typewidth == 0) {
				TIFFErrorExt(tif->tif_clientdata, module,
				    "Cannot determine size of unknown tag type %d",
				    dp->tdir_type);
				return -1;
			}
			if( dp->tdir_count > TIFF_UINT64_MAX / typewidth )
                            return -1;
			datasize=(uint64)typewidth*dp->tdir_count;
			if (!(tif->tif_flags&TIFF_BIGTIFF))
			{
				if (datasize<=4)
					datasize=0;
			}
			else
			{
				if (datasize<=8)
					datasize=0;
			}
			if( space > TIFF_UINT64_MAX - datasize )
                            return -1;
			space+=datasize;
		}
		if( filesize < space )
                    space = filesize;
                else
                    space = filesize - space;
		if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
			space /= td->td_samplesperpixel;
		for (strip = 0; strip < td->td_nstrips; strip++)
			td->td_stripbytecount_p[strip] = space;
		strip--;
                if (td->td_stripoffset_p[strip] > TIFF_UINT64_MAX - td->td_stripbytecount_p[strip])
                    return -1;
		if (td->td_stripoffset_p[strip]+td->td_stripbytecount_p[strip] > filesize) {
                    if( td->td_stripoffset_p[strip] >= filesize ) {
                        td->td_stripbytecount_p[strip] = 0;
                    } else {
                        td->td_stripbytecount_p[strip] = filesize - td->td_stripoffset_p[strip];
                    }
                }
	} else if (isTiled(tif)) {
		uint64 bytespertile = TIFFTileSize64(tif);
		for (strip = 0; strip < td->td_nstrips; strip++)
		    td->td_stripbytecount_p[strip] = bytespertile;
	} else {
		uint64 rowbytes = TIFFScanlineSize64(tif);
		uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
		for (strip = 0; strip < td->td_nstrips; strip++)
                {
                    if( rowbytes > 0 && rowsperstrip > TIFF_UINT64_MAX / rowbytes )
                        return -1;
                    td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;
                }
	}
	TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
	if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
		td->td_rowsperstrip = td->td_imagelength;
	return 1;
}
['int\nTIFFReadDirectory(TIFF* tif)\n{\n\tstatic const char module[] = "TIFFReadDirectory";\n\tTIFFDirEntry* dir;\n\tuint16 dircount;\n\tTIFFDirEntry* dp;\n\tuint16 di;\n\tconst TIFFField* fip;\n\tuint32 fii=FAILED_FII;\n        toff_t nextdiroff;\n    int bitspersample_read = FALSE;\n        int color_channels;\n\ttif->tif_diroff=tif->tif_nextdiroff;\n\tif (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))\n\t\treturn 0;\n\t(*tif->tif_cleanup)(tif);\n\ttif->tif_curdir++;\n        nextdiroff = tif->tif_nextdiroff;\n\tdircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);\n\tif (!dircount)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t    "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);\n\t\treturn 0;\n\t}\n\tTIFFReadDirectoryCheckOrder(tif,dir,dircount);\n\t{\n\t\tTIFFDirEntry* ma;\n\t\tuint16 mb;\n\t\tfor (ma=dir, mb=0; mb<dircount; ma++, mb++)\n\t\t{\n\t\t\tTIFFDirEntry* na;\n\t\t\tuint16 nb;\n\t\t\tfor (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)\n\t\t\t{\n\t\t\t\tif (ma->tdir_tag == na->tdir_tag) {\n\t\t\t\t\tna->tdir_ignore = TRUE;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\ttif->tif_flags &= ~TIFF_BEENWRITING;\n\ttif->tif_flags &= ~TIFF_BUF4WRITE;\n\ttif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;\n\tTIFFFreeDirectory(tif);\n\tTIFFDefaultDirectory(tif);\n\tTIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);\n\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);\n\tif (dp)\n\t{\n\t\tif (!TIFFFetchNormalTag(tif,dp,0))\n\t\t\tgoto bad;\n\t\tdp->tdir_ignore = TRUE;\n\t}\n\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);\n\tif (dp)\n\t{\n\t\tuint16 value;\n\t\tenum TIFFReadDirEntryErr err;\n\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t{\n\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);\n\t\t\tgoto bad;\n\t\t}\n\t\tif (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))\n\t\t\tgoto bad;\n\t\tdp->tdir_ignore = TRUE;\n\t}\n\telse\n\t{\n\t\tif (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))\n\t\t\tgoto bad;\n\t}\n\tfor (di=0, dp=dir; di<dircount; di++, dp++)\n\t{\n\t\tif (!dp->tdir_ignore)\n\t\t{\n\t\t\tTIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);\n\t\t\tif (fii == FAILED_FII)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t\t    "Unknown field with tag %d (0x%x) encountered",\n\t\t\t\t    dp->tdir_tag,dp->tdir_tag);\n\t\t\t\tif (!_TIFFMergeFields(tif,\n\t\t\t\t\t_TIFFCreateAnonField(tif,\n\t\t\t\t\t\tdp->tdir_tag,\n\t\t\t\t\t\t(TIFFDataType) dp->tdir_type),\n\t\t\t\t\t1)) {\n\t\t\t\t\tTIFFWarningExt(tif->tif_clientdata,\n\t\t\t\t\t    module,\n\t\t\t\t\t    "Registering anonymous field with tag %d (0x%x) failed",\n\t\t\t\t\t    dp->tdir_tag,\n\t\t\t\t\t    dp->tdir_tag);\n\t\t\t\t\tdp->tdir_ignore = TRUE;\n\t\t\t\t} else {\n\t\t\t\t\tTIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);\n\t\t\t\t\tassert(fii != FAILED_FII);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (!dp->tdir_ignore)\n\t\t{\n\t\t\tfip=tif->tif_fields[fii];\n\t\t\tif (fip->field_bit==FIELD_IGNORE)\n\t\t\t\tdp->tdir_ignore = TRUE;\n\t\t\telse\n\t\t\t{\n\t\t\t\tswitch (dp->tdir_tag)\n\t\t\t\t{\n\t\t\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\t\t\tcase TIFFTAG_TILEOFFSETS:\n\t\t\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n\t\t\t\t\t\tTIFFSetFieldBit(tif,fip->field_bit);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase TIFFTAG_IMAGEWIDTH:\n\t\t\t\t\tcase TIFFTAG_IMAGELENGTH:\n\t\t\t\t\tcase TIFFTAG_IMAGEDEPTH:\n\t\t\t\t\tcase TIFFTAG_TILELENGTH:\n\t\t\t\t\tcase TIFFTAG_TILEWIDTH:\n\t\t\t\t\tcase TIFFTAG_TILEDEPTH:\n\t\t\t\t\tcase TIFFTAG_PLANARCONFIG:\n\t\t\t\t\tcase TIFFTAG_ROWSPERSTRIP:\n\t\t\t\t\tcase TIFFTAG_EXTRASAMPLES:\n\t\t\t\t\t\tif (!TIFFFetchNormalTag(tif,dp,0))\n\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\tdp->tdir_ignore = TRUE;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tif( !_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag) )\n\t\t\t\t\t\t\tdp->tdir_ignore = TRUE;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&\n\t    (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))\n\t{\n\t\tif (!_TIFFFillStriles(tif))\n\t\t    goto bad;\n\t\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);\n\t\tif ((dp!=0)&&(dp->tdir_count==1))\n\t\t{\n\t\t\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,\n\t\t\t    TIFFTAG_STRIPBYTECOUNTS);\n\t\t\tif ((dp!=0)&&(dp->tdir_count==1))\n\t\t\t{\n\t\t\t\ttif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t    "Planarconfig tag value assumed incorrect, "\n\t\t\t\t    "assuming data is contig instead of chunky");\n\t\t\t}\n\t\t}\n\t}\n\tif (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))\n\t{\n\t\tMissingRequired(tif,"ImageLength");\n\t\tgoto bad;\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {\n\t\ttif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);\n\t\ttif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;\n\t\ttif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;\n\t\ttif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;\n\t\ttif->tif_flags &= ~TIFF_ISTILED;\n\t} else {\n\t\ttif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);\n\t\ttif->tif_flags |= TIFF_ISTILED;\n\t}\n\tif (!tif->tif_dir.td_nstrips) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t    "Cannot handle zero number of %s",\n\t\t    isTiled(tif) ? "tiles" : "strips");\n\t\tgoto bad;\n\t}\n\ttif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;\n\tif (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\ttif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;\n\tif (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {\n#ifdef OJPEG_SUPPORT\n\t\tif ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&\n\t\t    (isTiled(tif)==0) &&\n\t\t    (tif->tif_dir.td_nstrips==1)) {\n\t\t\tTIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);\n\t\t} else\n#endif\n        {\n\t\t\tMissingRequired(tif,\n\t\t\t\tisTiled(tif) ? "TileOffsets" : "StripOffsets");\n\t\t\tgoto bad;\n\t\t}\n\t}\n\tfor (di=0, dp=dir; di<dircount; di++, dp++)\n\t{\n\t\tif (!dp->tdir_ignore) {\n\t\t\tswitch (dp->tdir_tag)\n\t\t\t{\n\t\t\t\tcase TIFFTAG_MINSAMPLEVALUE:\n\t\t\t\tcase TIFFTAG_MAXSAMPLEVALUE:\n\t\t\t\tcase TIFFTAG_BITSPERSAMPLE:\n\t\t\t\tcase TIFFTAG_DATATYPE:\n\t\t\t\tcase TIFFTAG_SAMPLEFORMAT:\n\t\t\t\t\t{\n\t\t\t\t\t\tuint16 value;\n\t\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\t\t\t\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\t\t\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);\n\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!TIFFSetField(tif,dp->tdir_tag,value))\n\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\tif( dp->tdir_tag == TIFFTAG_BITSPERSAMPLE )\n\t\t\t\t\t\t    bitspersample_read = TRUE;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFFTAG_SMINSAMPLEVALUE:\n\t\t\t\tcase TIFFTAG_SMAXSAMPLEVALUE:\n\t\t\t\t\t{\n\t\t\t\t\t\tdouble *data = NULL;\n\t\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\t\tuint32 saved_flags;\n\t\t\t\t\t\tint m;\n\t\t\t\t\t\tif (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)\n\t\t\t\t\t\t\terr = TIFFReadDirEntryErrCount;\n\t\t\t\t\t\telse\n\t\t\t\t\t\t\terr = TIFFReadDirEntryDoubleArray(tif, dp, &data);\n\t\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);\n\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tsaved_flags = tif->tif_flags;\n\t\t\t\t\t\ttif->tif_flags |= TIFF_PERSAMPLE;\n\t\t\t\t\t\tm = TIFFSetField(tif,dp->tdir_tag,data);\n\t\t\t\t\t\ttif->tif_flags = saved_flags;\n\t\t\t\t\t\t_TIFFfree(data);\n\t\t\t\t\t\tif (!m)\n\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\t\tcase TIFFTAG_TILEOFFSETS:\n\t\t\t\t\t_TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),\n\t\t\t\t\t   dp, sizeof(TIFFDirEntry) );\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n\t\t\t\t\t_TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),\n\t\t\t\t\t   dp, sizeof(TIFFDirEntry) );\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFFTAG_COLORMAP:\n\t\t\t\tcase TIFFTAG_TRANSFERFUNCTION:\n\t\t\t\t\t{\n\t\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\t\tuint32 countpersample;\n\t\t\t\t\t\tuint32 countrequired;\n\t\t\t\t\t\tuint32 incrementpersample;\n\t\t\t\t\t\tuint16* value=NULL;\n\t\t\t\t\t\tif( !bitspersample_read )\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t\t\t\t\t"Ignoring %s since BitsPerSample tag not found",\n\t\t\t\t\t\t\t\tfip ? fip->field_name : "unknown tagname");\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (tif->tif_dir.td_bitspersample > 24)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t\t\t\t\t"Ignoring %s because BitsPerSample=%d>24",\n\t\t\t\t\t\t\t\tfip ? fip->field_name : "unknown tagname",\n\t\t\t\t\t\t\t\ttif->tif_dir.td_bitspersample);\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcountpersample=(1U<<tif->tif_dir.td_bitspersample);\n\t\t\t\t\t\tif ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcountrequired=countpersample;\n\t\t\t\t\t\t\tincrementpersample=0;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcountrequired=3*countpersample;\n\t\t\t\t\t\t\tincrementpersample=countpersample;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (dp->tdir_count!=(uint64)countrequired)\n\t\t\t\t\t\t\terr=TIFFReadDirEntryErrCount;\n\t\t\t\t\t\telse\n\t\t\t\t\t\t\terr=TIFFReadDirEntryShortArray(tif,dp,&value);\n\t\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tTIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);\n\t\t\t\t\t\t\t_TIFFfree(value);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFFTAG_OSUBFILETYPE:\n\t\t\t\t\t{\n\t\t\t\t\t\tuint16 valueo;\n\t\t\t\t\t\tuint32 value;\n\t\t\t\t\t\tif (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tswitch (valueo)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tcase OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;\n\t\t\t\t\t\t\t\tcase OFILETYPE_PAGE: value=FILETYPE_PAGE; break;\n\t\t\t\t\t\t\t\tdefault: value=0; break;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (value!=0)\n\t\t\t\t\t\t\t\tTIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\t(void) TIFFFetchNormalTag(tif, dp, TRUE);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n        if( tif->tif_mode == O_RDWR &&\n            tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&\n            tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&\n            tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&\n            tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&\n            tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&\n            tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&\n            tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&\n            tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0 )\n        {\n            TIFFSetupStrips(tif);\n        }\n        else if( !(tif->tif_flags&TIFF_DEFERSTRILELOAD) )\n        {\n            if( tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 )\n            {\n                if (!TIFFFetchStripThing(tif,&(tif->tif_dir.td_stripoffset_entry),\n                                         tif->tif_dir.td_nstrips,\n                                         &tif->tif_dir.td_stripoffset_p))\n                {\n                    goto bad;\n                }\n            }\n            if( tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 )\n            {\n                if (!TIFFFetchStripThing(tif,&(tif->tif_dir.td_stripbytecount_entry),\n                                         tif->tif_dir.td_nstrips,\n                                         &tif->tif_dir.td_stripbytecount_p))\n                {\n                    goto bad;\n                }\n            }\n        }\n\tif (tif->tif_dir.td_compression==COMPRESSION_OJPEG)\n\t{\n\t\tif (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))\n\t\t{\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t    "Photometric tag is missing, assuming data is YCbCr");\n\t\t\tif (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))\n\t\t\t\tgoto bad;\n\t\t}\n\t\telse if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)\n\t\t{\n\t\t\ttif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t    "Photometric tag value assumed incorrect, "\n\t\t\t    "assuming data is YCbCr instead of RGB");\n\t\t}\n\t\tif (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))\n\t\t{\n\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t    "BitsPerSample tag is missing, assuming 8 bits per sample");\n\t\t\tif (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))\n\t\t\t\tgoto bad;\n\t\t}\n\t\tif (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))\n\t\t{\n\t\t\tif (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t    "SamplesPerPixel tag is missing, "\n\t\t\t\t    "assuming correct SamplesPerPixel value is 3");\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t    "SamplesPerPixel tag is missing, "\n\t\t\t\t    "applying correct SamplesPerPixel value of 3");\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\telse if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)\n\t\t\t\t || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))\n\t\t\t{\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t}\n\t}\n        color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);\n        if (color_channels && tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples > color_channels) {\n                uint16 old_extrasamples;\n                uint16 *new_sampleinfo;\n                TIFFWarningExt(tif->tif_clientdata,module, "Sum of Photometric type-related "\n                    "color channels and ExtraSamples doesn\'t match SamplesPerPixel. "\n                    "Defining non-color channels as ExtraSamples.");\n                old_extrasamples = tif->tif_dir.td_extrasamples;\n                tif->tif_dir.td_extrasamples = (uint16) (tif->tif_dir.td_samplesperpixel - color_channels);\n                new_sampleinfo = (uint16*) _TIFFcalloc(tif->tif_dir.td_extrasamples, sizeof(uint16));\n                if (!new_sampleinfo) {\n                    TIFFErrorExt(tif->tif_clientdata, module, "Failed to allocate memory for "\n                                "temporary new sampleinfo array (%d 16 bit elements)",\n                                tif->tif_dir.td_extrasamples);\n                    goto bad;\n                }\n                memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, old_extrasamples * sizeof(uint16));\n                _TIFFsetShortArray(&tif->tif_dir.td_sampleinfo, new_sampleinfo, tif->tif_dir.td_extrasamples);\n                _TIFFfree(new_sampleinfo);\n        }\n\tif (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&\n\t    !TIFFFieldSet(tif, FIELD_COLORMAP)) {\n\t\tif ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)\n\t\t\ttif->tif_dir.td_photometric = PHOTOMETRIC_RGB;\n\t\telse if (tif->tif_dir.td_bitspersample>=8)\n\t\t\ttif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;\n\t\telse {\n\t\t\tMissingRequired(tif, "Colormap");\n\t\t\tgoto bad;\n\t\t}\n\t}\n\tif (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)\n\t{\n\t\tif (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {\n\t\t\tif ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&\n\t\t\t    tif->tif_dir.td_nstrips > 1) ||\n\t\t\t    (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&\n\t\t\t     tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {\n\t\t\t    MissingRequired(tif, "StripByteCounts");\n\t\t\t    goto bad;\n\t\t\t}\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t\t"TIFF directory is missing required "\n\t\t\t\t"\\"StripByteCounts\\" field, calculating from imagelength");\n\t\t\tif (EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t    goto bad;\n\t\t} else if (tif->tif_dir.td_nstrips == 1\n                           && !(tif->tif_flags&TIFF_ISTILED)\n\t\t\t   && ByteCountLooksBad(tif)) {\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t    "Bogus \\"StripByteCounts\\" field, ignoring and calculating from imagelength");\n\t\t\tif(EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t    goto bad;\n\t\t} else if (!(tif->tif_flags&TIFF_DEFERSTRILELOAD)\n\t\t\t   && tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG\n\t\t\t   && tif->tif_dir.td_nstrips > 2\n\t\t\t   && tif->tif_dir.td_compression == COMPRESSION_NONE\n\t\t\t   && TIFFGetStrileByteCount(tif, 0) != TIFFGetStrileByteCount(tif, 1)\n\t\t\t   && TIFFGetStrileByteCount(tif, 0) != 0\n\t\t\t   && TIFFGetStrileByteCount(tif, 1) != 0 ) {\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t    "Wrong \\"StripByteCounts\\" field, ignoring and calculating from imagelength");\n\t\t\tif (EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t    goto bad;\n\t\t}\n\t}\n\tif (dir)\n\t{\n\t\t_TIFFfree(dir);\n\t\tdir=NULL;\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))\n\t{\n\t\tif (tif->tif_dir.td_bitspersample>=16)\n\t\t\ttif->tif_dir.td_maxsamplevalue=0xFFFF;\n\t\telse\n\t\t\ttif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);\n\t}\n#ifdef STRIPBYTECOUNTSORTED_UNUSED\n\tif (!(tif->tif_flags&TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1) {\n\t\tuint32 strip;\n\t\ttif->tif_dir.td_stripbytecountsorted = 1;\n\t\tfor (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {\n\t\t\tif (TIFFGetStrileOffset(tif, strip - 1) >\n\t\t\t    TIFFGetStrileOffset(tif, strip)) {\n\t\t\t\ttif->tif_dir.td_stripbytecountsorted = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n#endif\n\t(*tif->tif_fixuptags)(tif);\n\tif ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&\n\t    (tif->tif_dir.td_nstrips==1)&&\n\t    (tif->tif_dir.td_compression==COMPRESSION_NONE)&&\n\t    ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))\n        {\n            ChopUpSingleUncompressedStrip(tif);\n        }\n        if( tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&\n            tif->tif_dir.td_compression == COMPRESSION_NONE &&\n            (tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP &&\n            TIFFStripSize64(tif) > 0x7FFFFFFFUL )\n        {\n            TryChopUpUncompressedBigTiff(tif);\n        }\n\ttif->tif_flags &= ~TIFF_DIRTYDIRECT;\n\ttif->tif_flags &= ~TIFF_DIRTYSTRIP;\n\ttif->tif_row = (uint32) -1;\n\ttif->tif_curstrip = (uint32) -1;\n\ttif->tif_col = (uint32) -1;\n\ttif->tif_curtile = (uint32) -1;\n\ttif->tif_tilesize = (tmsize_t) -1;\n\ttif->tif_scanlinesize = TIFFScanlineSize(tif);\n\tif (!tif->tif_scanlinesize) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t    "Cannot handle zero scanline size");\n\t\treturn (0);\n\t}\n\tif (isTiled(tif)) {\n\t\ttif->tif_tilesize = TIFFTileSize(tif);\n\t\tif (!tif->tif_tilesize) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t     "Cannot handle zero tile size");\n\t\t\treturn (0);\n\t\t}\n\t} else {\n\t\tif (!TIFFStripSize(tif)) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t    "Cannot handle zero strip size");\n\t\t\treturn (0);\n\t\t}\n\t}\n\treturn (1);\nbad:\n\tif (dir)\n\t\t_TIFFfree(dir);\n\treturn (0);\n}', 'uint32\nTIFFNumberOfStrips(TIFF* tif)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 nstrips;\n\tnstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :\n\t     TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\tnstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,\n\t\t    "TIFFNumberOfStrips");\n\treturn (nstrips);\n}', 'uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)\n{\n    return TIFFGetStrileByteCountWithErr(tif, strile, NULL);\n}', 'uint64 TIFFGetStrileByteCountWithErr(TIFF *tif, uint32 strile, int *pbErr)\n{\n    TIFFDirectory *td = &tif->tif_dir;\n    return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,\n                               &(td->td_stripbytecount_entry),\n                               &(td->td_stripbytecount_p), pbErr);\n}', 'static uint64 _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32 strile,\n                                                   TIFFDirEntry* dirent,\n                                                   uint64** parray,\n                                                   int *pbErr)\n{\n    TIFFDirectory *td = &tif->tif_dir;\n    if( pbErr )\n        *pbErr = 0;\n    if( (tif->tif_flags&TIFF_DEFERSTRILELOAD) && !(tif->tif_flags&TIFF_CHOPPEDUPARRAYS) )\n    {\n        if( !(tif->tif_flags&TIFF_LAZYSTRILELOAD) ||\n            dirent->tdir_count <= 4 )\n        {\n            if( !_TIFFFillStriles(tif) )\n            {\n                if( pbErr )\n                    *pbErr = 1;\n            }\n        }\n        else\n        {\n             if( !_TIFFFetchStrileValue(tif, strile, dirent, parray) )\n             {\n                if( pbErr )\n                    *pbErr = 1;\n                 return 0;\n             }\n        }\n    }\n    if( *parray == NULL || strile >= td->td_nstrips )\n    {\n        if( pbErr )\n            *pbErr = 1;\n        return 0;\n    }\n    return (*parray)[strile];\n}', 'static int\nEstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)\n{\n\tstatic const char module[] = "EstimateStripByteCounts";\n\tTIFFDirEntry *dp;\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 strip;\n        if( !_TIFFFillStrilesInternal( tif, 0 ) )\n            return -1;\n\tif (td->td_stripbytecount_p)\n\t\t_TIFFfree(td->td_stripbytecount_p);\n\ttd->td_stripbytecount_p = (uint64*)\n\t    _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),\n\t\t"for \\"StripByteCounts\\" array");\n        if( td->td_stripbytecount_p == NULL )\n            return -1;\n\tif (td->td_compression != COMPRESSION_NONE) {\n\t\tuint64 space;\n\t\tuint64 filesize;\n\t\tuint16 n;\n\t\tfilesize = TIFFGetFileSize(tif);\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\tspace=sizeof(TIFFHeaderClassic)+2+dircount*12+4;\n\t\telse\n\t\t\tspace=sizeof(TIFFHeaderBig)+8+dircount*20+8;\n\t\tfor (dp = dir, n = dircount; n > 0; n--, dp++)\n\t\t{\n\t\t\tuint32 typewidth;\n\t\t\tuint64 datasize;\n\t\t\ttypewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);\n\t\t\tif (typewidth == 0) {\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t    "Cannot determine size of unknown tag type %d",\n\t\t\t\t    dp->tdir_type);\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\tif( dp->tdir_count > TIFF_UINT64_MAX / typewidth )\n                            return -1;\n\t\t\tdatasize=(uint64)typewidth*dp->tdir_count;\n\t\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\t{\n\t\t\t\tif (datasize<=4)\n\t\t\t\t\tdatasize=0;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (datasize<=8)\n\t\t\t\t\tdatasize=0;\n\t\t\t}\n\t\t\tif( space > TIFF_UINT64_MAX - datasize )\n                            return -1;\n\t\t\tspace+=datasize;\n\t\t}\n\t\tif( filesize < space )\n                    space = filesize;\n                else\n                    space = filesize - space;\n\t\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\t\tspace /= td->td_samplesperpixel;\n\t\tfor (strip = 0; strip < td->td_nstrips; strip++)\n\t\t\ttd->td_stripbytecount_p[strip] = space;\n\t\tstrip--;\n                if (td->td_stripoffset_p[strip] > TIFF_UINT64_MAX - td->td_stripbytecount_p[strip])\n                    return -1;\n\t\tif (td->td_stripoffset_p[strip]+td->td_stripbytecount_p[strip] > filesize) {\n                    if( td->td_stripoffset_p[strip] >= filesize ) {\n                        td->td_stripbytecount_p[strip] = 0;\n                    } else {\n                        td->td_stripbytecount_p[strip] = filesize - td->td_stripoffset_p[strip];\n                    }\n                }\n\t} else if (isTiled(tif)) {\n\t\tuint64 bytespertile = TIFFTileSize64(tif);\n\t\tfor (strip = 0; strip < td->td_nstrips; strip++)\n\t\t    td->td_stripbytecount_p[strip] = bytespertile;\n\t} else {\n\t\tuint64 rowbytes = TIFFScanlineSize64(tif);\n\t\tuint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;\n\t\tfor (strip = 0; strip < td->td_nstrips; strip++)\n                {\n                    if( rowbytes > 0 && rowsperstrip > TIFF_UINT64_MAX / rowbytes )\n                        return -1;\n                    td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;\n                }\n\t}\n\tTIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);\n\tif (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))\n\t\ttd->td_rowsperstrip = td->td_imagelength;\n\treturn 1;\n}', 'static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount )\n{\n    register TIFFDirectory *td = &tif->tif_dir;\n    int return_value = 1;\n    if( !(tif->tif_flags&TIFF_DEFERSTRILELOAD) || (tif->tif_flags&TIFF_CHOPPEDUPARRAYS) != 0 )\n        return 1;\n    if( tif->tif_flags&TIFF_LAZYSTRILELOAD )\n    {\n        _TIFFfree(td->td_stripoffset_p);\n        _TIFFfree(td->td_stripbytecount_p);\n        td->td_stripoffset_p = NULL;\n        td->td_stripbytecount_p = NULL;\n        td->td_stripoffsetbyteallocsize = 0;\n        tif->tif_flags &= ~TIFF_LAZYSTRILELOAD;\n    }\n    if( td->td_stripoffset_p != NULL )\n            return 1;\n    if( td->td_stripoffset_entry.tdir_count == 0 )\n            return 0;\n    if (!TIFFFetchStripThing(tif,&(td->td_stripoffset_entry),\n                                td->td_nstrips,&td->td_stripoffset_p))\n    {\n            return_value = 0;\n    }\n    if (loadStripByteCount &&\n        !TIFFFetchStripThing(tif,&(td->td_stripbytecount_entry),\n                                td->td_nstrips,&td->td_stripbytecount_p))\n    {\n            return_value = 0;\n    }\n    _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));\n    _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));\n#ifdef STRIPBYTECOUNTSORTED_UNUSED\n    if (tif->tif_dir.td_nstrips > 1 && return_value == 1 ) {\n            uint32 strip;\n            tif->tif_dir.td_stripbytecountsorted = 1;\n            for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {\n                    if (tif->tif_dir.td_stripoffset_p[strip - 1] >\n                        tif->tif_dir.td_stripoffset_p[strip]) {\n                            tif->tif_dir.td_stripbytecountsorted = 0;\n                            break;\n                    }\n            }\n    }\n#endif\n    return return_value;\n}']