tools/tiffsplit.c:90: error: Buffer Overrun L3
  Offset: [1, +oo] Size: [0, +oo] by call to `TIFFReadDirectory`.

tools/tiffsplit.c:87:9: Call
85. 			if (out == NULL)
86. 				return (-2);
87. 			if (!tiffcp(in, out))
            ^
88. 				return (-1);
89. 			TIFFClose(out);

tools/tiffsplit.c:155:1: Parameter `in->tif_dir.td_stripoffset_p`
  153. }
  154. 
  155. static int
       ^
  156. tiffcp(TIFF* in, TIFF* out)
  157. {

tools/tiffsplit.c:223:11: Call
  221. 	CopyField(TIFFTAG_FAXDCS, stringv);
  222. 	if (TIFFIsTiled(in))
  223. 		return (cpTiles(in, out));
                 ^
  224. 	else
  225. 		return (cpStrips(in, out));

tools/tiffsplit.c:262:1: Parameter `in->tif_dir.td_stripoffset_p`
    260. }
    261. 
    262. static int
         ^
    263. cpTiles(TIFF* in, TIFF* out)
    264. {

tools/tiffsplit.c:90:12: Call
88. 				return (-1);
89. 			TIFFClose(out);
90. 		} while (TIFFReadDirectory(in));
               ^
91. 		(void) TIFFClose(in);
92. 	}

libtiff/tif_dirread.c:4050:13: Call
  4048.         {
  4049.             /* Directory typically created with TIFFDeferStrileArrayWriting() */
  4050.             TIFFSetupStrips(tif);
                    ^
  4051.         }
  4052.         else if( !(tif->tif_flags&TIFF_DEFERSTRILELOAD) )

libtiff/tif_write.c:537:46: Assignment
    535. 	td->td_nstrips = td->td_stripsperimage;
    536.         /* TIFFWriteDirectoryTagData has a limitation to 0x80000000U bytes */
    537.         if( td->td_nstrips >= 0x80000000U / ((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U) )
                                                      ^
    538.         {
    539.             TIFFErrorExt(tif->tif_clientdata, "TIFFSetupStrips",

libtiff/tif_dirread.c:4317:13: Call
  4315.             TIFFStripSize64(tif) > 0x7FFFFFFFUL )
  4316.         {
  4317.             TryChopUpUncompressedBigTiff(tif);
                    ^
  4318.         }
  4319. 

libtiff/tif_dirread.c:5977:10: Assignment
    5975. 
    5976.     /* Check that the strips are contiguous and of the expected size */
    5977.     for( i = 0; i < td->td_nstrips; i++ )
                   ^
    5978.     {
    5979.         if( i == td->td_nstrips - 1 )

libtiff/tif_dirread.c:5993:26: Call
    5991.                 return;
    5992.             }
    5993.             if( i > 0 && TIFFGetStrileOffset(tif, i) !=
                                   ^
    5994.                     TIFFGetStrileOffset(tif, i-1) + TIFFGetStrileByteCount(tif, i-1) )
    5995.             {

libtiff/tif_dirread.c:6348:1: Parameter `strile`
      6346. 
      6347. /* Return the value of the TileOffsets/StripOffsets array for the specified tile/strile */
      6348. uint64 TIFFGetStrileOffset(TIFF *tif, uint32 strile)
            ^
      6349. {
      6350.     return TIFFGetStrileOffsetWithErr(tif, strile, NULL);

libtiff/tif_dirread.c:6350:12: Call
      6348. uint64 TIFFGetStrileOffset(TIFF *tif, uint32 strile)
      6349. {
      6350.     return TIFFGetStrileOffsetWithErr(tif, strile, NULL);
                       ^
      6351. }
      6352. 

libtiff/tif_dirread.c:6354:1: Parameter `strile`
        6352. 
        6353. /* Return the value of the TileOffsets/StripOffsets array for the specified tile/strile */
        6354. uint64 TIFFGetStrileOffsetWithErr(TIFF *tif, uint32 strile, int *pbErr)
              ^
        6355. {
        6356.     TIFFDirectory *td = &tif->tif_dir;

libtiff/tif_dirread.c:6357:12: Call
        6355. {
        6356.     TIFFDirectory *td = &tif->tif_dir;
        6357.     return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
                         ^
        6358.                                &(td->td_stripoffset_entry),
        6359.                                &(td->td_stripoffset_p), pbErr);

libtiff/tif_dirread.c:6304:1: Parameter `strile`
          6302. }
          6303. 
          6304. static uint64 _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32 strile,
                ^
          6305.                                                    TIFFDirEntry* dirent,
          6306.                                                    uint64** parray,

libtiff/tif_dirread.c:6330:19: Call
          6328.         else
          6329.         {
          6330.              if( !_TIFFFetchStrileValue(tif, strile, dirent, parray) )
                                  ^
          6331.              {
          6332.                 if( pbErr )

libtiff/tif_dirread.c:6201:1: <Offset trace>
6199. }
6200. 
6201. static int _TIFFFetchStrileValue(TIFF* tif,
      ^
6202.                                  uint32 strile,
6203.                                  TIFFDirEntry* dirent,

libtiff/tif_dirread.c:6201:1: Parameter `strile`
            6199. }
            6200. 
            6201. static int _TIFFFetchStrileValue(TIFF* tif,
                  ^
            6202.                                  uint32 strile,
            6203.                                  TIFFDirEntry* dirent,

libtiff/tif_dirread.c:6201:1: <Length trace>
6199. }
6200. 
6201. static int _TIFFFetchStrileValue(TIFF* tif,
      ^
6202.                                  uint32 strile,
6203.                                  TIFFDirEntry* dirent,

libtiff/tif_dirread.c:6201:1: Parameter `**parray`
            6199. }
            6200. 
            6201. static int _TIFFFetchStrileValue(TIFF* tif,
                  ^
            6202.                                  uint32 strile,
            6203.                                  TIFFDirEntry* dirent,

libtiff/tif_dirread.c:6292:10: Array access: Offset: [1, +oo] Size: [0, +oo] by call to `TIFFReadDirectory` 
            6290.         return 0;
            6291. 
            6292.     if( ~((*parray)[strile]) == 0 )
                           ^
            6293.     {
            6294.         if( !_TIFFPartialReadStripArray( tif, dirent, strile, *parray ) )

static int _TIFFFetchStrileValue(TIFF* tif,
                                 uint32 strile,
                                 TIFFDirEntry* dirent,
                                 uint64** parray)
{
    static const char module[] = "_TIFFFetchStrileValue";
    TIFFDirectory *td = &tif->tif_dir;
    if( strile >= dirent->tdir_count )
    {
        return 0;
    }
    if( strile >= td->td_stripoffsetbyteallocsize )
    {
        uint32 nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;
        uint32 nStripArrayAllocNew;
        uint64 nArraySize64;
        size_t nArraySize;
        uint64* offsetArray;
        uint64* bytecountArray;
        if( strile > 1000000 )
        {
            uint64 filesize = TIFFGetFileSize(tif);
            if( strile > filesize / sizeof(uint32) )
            {
                TIFFErrorExt(tif->tif_clientdata, module, "File too short");
                return 0;
            }
        }
        if( td->td_stripoffsetbyteallocsize == 0 &&
            td->td_nstrips < 1024 * 1024 )
        {
            nStripArrayAllocNew = td->td_nstrips;
        }
        else
        {
#define TIFF_MAX(a,b) (((a)>(b)) ? (a) : (b))
#define TIFF_MIN(a,b) (((a)<(b)) ? (a) : (b))
            nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U );
            if( nStripArrayAllocNew < 0xFFFFFFFFU / 2  )
                nStripArrayAllocNew *= 2;
            nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
        }
        assert( strile < nStripArrayAllocNew );
        nArraySize64 = (uint64)sizeof(uint64) * nStripArrayAllocNew;
        nArraySize = (size_t)(nArraySize64);
#if SIZEOF_SIZE_T == 4
        if( nArraySize != nArraySize64 )
        {
            TIFFErrorExt(tif->tif_clientdata, module,
                        "Cannot allocate strip offset and bytecount arrays");
            return 0;
        }
#endif
        offsetArray = (uint64*)(
            _TIFFrealloc( td->td_stripoffset_p, nArraySize ) );
        bytecountArray = (uint64*)(
            _TIFFrealloc( td->td_stripbytecount_p, nArraySize ) );
        if( offsetArray )
            td->td_stripoffset_p = offsetArray;
        if( bytecountArray )
            td->td_stripbytecount_p = bytecountArray;
        if( offsetArray && bytecountArray )
        {
            td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
            memset(td->td_stripoffset_p + nStripArrayAllocBefore,
                0xFF,
                (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64) );
            memset(td->td_stripbytecount_p + nStripArrayAllocBefore,
                0xFF,
                (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64) );
        }
        else
        {
            TIFFErrorExt(tif->tif_clientdata, module,
                        "Cannot allocate strip offset and bytecount arrays");
            _TIFFfree(td->td_stripoffset_p);
            td->td_stripoffset_p = NULL;
            _TIFFfree(td->td_stripbytecount_p);
            td->td_stripbytecount_p = NULL;
            td->td_stripoffsetbyteallocsize = 0;
        }
    }
    if( *parray == NULL || strile >= td->td_stripoffsetbyteallocsize )
        return 0;
    if( ~((*parray)[strile]) == 0 )
    {
        if( !_TIFFPartialReadStripArray( tif, dirent, strile, *parray ) )
        {
            (*parray)[strile] = 0;
            return 0;
        }
    }
    return 1;
}
['int\nmain(int argc, char* argv[])\n{\n\tTIFF *in, *out;\n\tif (argc < 2) {\n                fprintf(stderr, "%s\\n\\n", TIFFGetVersion());\n\t\tfprintf(stderr, "usage: tiffsplit input.tif [prefix]\\n");\n\t\treturn (-3);\n\t}\n\tif (argc > 2) {\n\t\tstrncpy(fname, argv[2], sizeof(fname));\n\t\tfname[sizeof(fname) - 1] = \'\\0\';\n\t}\n\tin = TIFFOpen(argv[1], "r");\n\tif (in != NULL) {\n\t\tdo {\n\t\t\tsize_t path_len;\n\t\t\tchar *path;\n\t\t\tnewfilename();\n\t\t\tpath_len = strlen(fname) + sizeof(TIFF_SUFFIX);\n\t\t\tpath = (char *) _TIFFmalloc(path_len);\n\t\t\tstrncpy(path, fname, path_len);\n\t\t\tpath[path_len - 1] = \'\\0\';\n\t\t\tstrncat(path, TIFF_SUFFIX, path_len - strlen(path) - 1);\n\t\t\tout = TIFFOpen(path, TIFFIsBigEndian(in)?"wb":"wl");\n\t\t\t_TIFFfree(path);\n\t\t\tif (out == NULL)\n\t\t\t\treturn (-2);\n\t\t\tif (!tiffcp(in, out))\n\t\t\t\treturn (-1);\n\t\t\tTIFFClose(out);\n\t\t} while (TIFFReadDirectory(in));\n\t\t(void) TIFFClose(in);\n\t}\n\treturn (0);\n}', 'static int\ntiffcp(TIFF* in, TIFF* out)\n{\n\tuint16 bitspersample, samplesperpixel, compression, shortv, *shortav;\n\tuint32 w, l;\n\tfloat floatv;\n\tchar *stringv;\n\tuint32 longv;\n\tCopyField(TIFFTAG_SUBFILETYPE, longv);\n\tCopyField(TIFFTAG_TILEWIDTH, w);\n\tCopyField(TIFFTAG_TILELENGTH, l);\n\tCopyField(TIFFTAG_IMAGEWIDTH, w);\n\tCopyField(TIFFTAG_IMAGELENGTH, l);\n\tCopyField(TIFFTAG_BITSPERSAMPLE, bitspersample);\n\tCopyField(TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);\n\tCopyField(TIFFTAG_COMPRESSION, compression);\n\tif (compression == COMPRESSION_JPEG) {\n\t\tuint32 count = 0;\n\t\tvoid *table = NULL;\n\t\tif (TIFFGetField(in, TIFFTAG_JPEGTABLES, &count, &table)\n\t\t    && count > 0 && table) {\n\t\t    TIFFSetField(out, TIFFTAG_JPEGTABLES, count, table);\n\t\t}\n\t}\n        CopyField(TIFFTAG_PHOTOMETRIC, shortv);\n\tCopyField(TIFFTAG_PREDICTOR, shortv);\n\tCopyField(TIFFTAG_THRESHHOLDING, shortv);\n\tCopyField(TIFFTAG_FILLORDER, shortv);\n\tCopyField(TIFFTAG_ORIENTATION, shortv);\n\tCopyField(TIFFTAG_MINSAMPLEVALUE, shortv);\n\tCopyField(TIFFTAG_MAXSAMPLEVALUE, shortv);\n\tCopyField(TIFFTAG_XRESOLUTION, floatv);\n\tCopyField(TIFFTAG_YRESOLUTION, floatv);\n\tCopyField(TIFFTAG_GROUP3OPTIONS, longv);\n\tCopyField(TIFFTAG_GROUP4OPTIONS, longv);\n\tCopyField(TIFFTAG_RESOLUTIONUNIT, shortv);\n\tCopyField(TIFFTAG_PLANARCONFIG, shortv);\n\tCopyField(TIFFTAG_ROWSPERSTRIP, longv);\n\tCopyField(TIFFTAG_XPOSITION, floatv);\n\tCopyField(TIFFTAG_YPOSITION, floatv);\n\tCopyField(TIFFTAG_IMAGEDEPTH, longv);\n\tCopyField(TIFFTAG_TILEDEPTH, longv);\n\tCopyField(TIFFTAG_SAMPLEFORMAT, shortv);\n\tCopyField2(TIFFTAG_EXTRASAMPLES, shortv, shortav);\n\t{ uint16 *red, *green, *blue;\n\t  CopyField3(TIFFTAG_COLORMAP, red, green, blue);\n\t}\n\t{ uint16 shortv2;\n\t  CopyField2(TIFFTAG_PAGENUMBER, shortv, shortv2);\n\t}\n\tCopyField(TIFFTAG_ARTIST, stringv);\n\tCopyField(TIFFTAG_IMAGEDESCRIPTION, stringv);\n\tCopyField(TIFFTAG_MAKE, stringv);\n\tCopyField(TIFFTAG_MODEL, stringv);\n\tCopyField(TIFFTAG_SOFTWARE, stringv);\n\tCopyField(TIFFTAG_DATETIME, stringv);\n\tCopyField(TIFFTAG_HOSTCOMPUTER, stringv);\n\tCopyField(TIFFTAG_PAGENAME, stringv);\n\tCopyField(TIFFTAG_DOCUMENTNAME, stringv);\n\tCopyField(TIFFTAG_BADFAXLINES, longv);\n\tCopyField(TIFFTAG_CLEANFAXDATA, longv);\n\tCopyField(TIFFTAG_CONSECUTIVEBADFAXLINES, longv);\n\tCopyField(TIFFTAG_FAXRECVPARAMS, longv);\n\tCopyField(TIFFTAG_FAXRECVTIME, longv);\n\tCopyField(TIFFTAG_FAXSUBADDRESS, stringv);\n\tCopyField(TIFFTAG_FAXDCS, stringv);\n\tif (TIFFIsTiled(in))\n\t\treturn (cpTiles(in, out));\n\telse\n\t\treturn (cpStrips(in, out));\n}', 'static int\ncpTiles(TIFF* in, TIFF* out)\n{\n\ttmsize_t bufsize = TIFFTileSize(in);\n\tunsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);\n\tif (buf) {\n\t\tttile_t t, nt = TIFFNumberOfTiles(in);\n\t\tuint64 *bytecounts;\n\t\tif (!TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts)) {\n\t\t\tfprintf(stderr, "tiffsplit: tile byte counts are missing\\n");\n                        _TIFFfree(buf);\n\t\t\treturn (0);\n\t\t}\n\t\tfor (t = 0; t < nt; t++) {\n\t\t\tif (bytecounts[t] > (uint64) bufsize) {\n\t\t\t\tbuf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[t]);\n\t\t\t\tif (!buf)\n\t\t\t\t\treturn (0);\n\t\t\t\tbufsize = (tmsize_t)bytecounts[t];\n\t\t\t}\n\t\t\tif (TIFFReadRawTile(in, t, buf, (tmsize_t)bytecounts[t]) < 0 ||\n\t\t\t    TIFFWriteRawTile(out, t, buf, (tmsize_t)bytecounts[t]) < 0) {\n\t\t\t\t_TIFFfree(buf);\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\t_TIFFfree(buf);\n\t\treturn (1);\n\t}\n\treturn (0);\n}', '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\tswitch( dp->tdir_type )\n\t\t\t\t\t{\n\t\t\t\t\t    case TIFF_SHORT:\n\t\t\t\t\t    case TIFF_LONG:\n\t\t\t\t\t    case TIFF_LONG8:\n\t\t\t\t\t        break;\n\t\t\t\t\t    default:\n                                                if( !(tif->tif_mode == O_RDWR &&\n                                                      dp->tdir_count == 0 &&\n                                                      dp->tdir_type == 0 &&\n                                                      dp->tdir_offset.toff_long8 == 0) )\n                                                {\n                                                    fip = TIFFFieldWithTag(tif,dp->tdir_tag);\n                                                    TIFFWarningExt(tif->tif_clientdata,module,\n                                                                   "Invalid data type for tag %s",\n                                                                   fip ? fip->field_name : "unknown tagname");\n                                                }\n                                                break;\n                                        }\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\tswitch( dp->tdir_type )\n\t\t\t\t\t{\n\t\t\t\t\t    case TIFF_SHORT:\n\t\t\t\t\t    case TIFF_LONG:\n\t\t\t\t\t    case TIFF_LONG8:\n\t\t\t\t\t        break;\n\t\t\t\t\t    default:\n                                                if( !(tif->tif_mode == O_RDWR &&\n                                                      dp->tdir_count == 0 &&\n                                                      dp->tdir_type == 0 &&\n                                                      dp->tdir_offset.toff_long8 == 0) )\n                                                {\n                                                    fip = TIFFFieldWithTag(tif,dp->tdir_tag);\n                                                    TIFFWarningExt(tif->tif_clientdata,module,\n                                                                   "Invalid data type for tag %s",\n                                                                   fip ? fip->field_name : "unknown tagname");\n                                                }\n                                                break;\n                                        }\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}', 'int\nTIFFSetupStrips(TIFF* tif)\n{\n\tTIFFDirectory* td = &tif->tif_dir;\n\tif (isTiled(tif))\n\t\ttd->td_stripsperimage =\n\t\t    isUnspecified(tif, FIELD_TILEDIMENSIONS) ?\n\t\t\ttd->td_samplesperpixel : TIFFNumberOfTiles(tif);\n\telse\n\t\ttd->td_stripsperimage =\n\t\t    isUnspecified(tif, FIELD_ROWSPERSTRIP) ?\n\t\t\ttd->td_samplesperpixel : TIFFNumberOfStrips(tif);\n\ttd->td_nstrips = td->td_stripsperimage;\n        if( td->td_nstrips >= 0x80000000U / ((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U) )\n        {\n            TIFFErrorExt(tif->tif_clientdata, "TIFFSetupStrips",\n                         "Too large Strip/Tile Offsets/ByteCounts arrays");\n            return 0;\n        }\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\ttd->td_stripsperimage /= td->td_samplesperpixel;\n\ttd->td_stripoffset_p = (uint64 *)\n            _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),\n                             "for \\"StripOffsets\\" array");\n\ttd->td_stripbytecount_p = (uint64 *)\n            _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),\n                             "for \\"StripByteCounts\\" array");\n\tif (td->td_stripoffset_p == NULL || td->td_stripbytecount_p == NULL)\n\t\treturn (0);\n\t_TIFFmemset(td->td_stripoffset_p, 0, td->td_nstrips*sizeof (uint64));\n\t_TIFFmemset(td->td_stripbytecount_p, 0, td->td_nstrips*sizeof (uint64));\n\tTIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);\n\tTIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);\n\treturn (1);\n}', 'static void TryChopUpUncompressedBigTiff( TIFF* tif )\n{\n    TIFFDirectory *td = &tif->tif_dir;\n    uint32 rowblock;\n    uint64 rowblockbytes;\n    uint32 i;\n    uint64 stripsize;\n    uint32 rowblocksperstrip;\n    uint32 rowsperstrip;\n    uint64 stripbytes;\n    uint32 nstrips;\n    stripsize = TIFFStripSize64(tif);\n    assert( tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG );\n    assert( tif->tif_dir.td_compression == COMPRESSION_NONE );\n    assert( (tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP );\n    assert( stripsize > 0x7FFFFFFFUL );\n    if( TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY )\n        return;\n    if ((td->td_photometric == PHOTOMETRIC_YCBCR)&&\n        (!isUpSampled(tif)))\n        rowblock = td->td_ycbcrsubsampling[1];\n    else\n        rowblock = 1;\n    rowblockbytes = TIFFVStripSize64(tif, rowblock);\n    if( rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL )\n    {\n        return;\n    }\n    for( i = 0; i < td->td_nstrips; i++ )\n    {\n        if( i == td->td_nstrips - 1 )\n        {\n            if( TIFFGetStrileByteCount(tif, i) < TIFFVStripSize64(\n                    tif, td->td_imagelength - i * td->td_rowsperstrip ) )\n            {\n                return;\n            }\n        }\n        else\n        {\n            if( TIFFGetStrileByteCount(tif, i) != stripsize )\n            {\n                return;\n            }\n            if( i > 0 && TIFFGetStrileOffset(tif, i) !=\n                    TIFFGetStrileOffset(tif, i-1) + TIFFGetStrileByteCount(tif, i-1) )\n            {\n                return;\n            }\n        }\n    }\n    rowblocksperstrip = (uint32) (512 * 1024 * 1024 / rowblockbytes);\n    if( rowblocksperstrip == 0 )\n        rowblocksperstrip = 1;\n    rowsperstrip = rowblocksperstrip * rowblock;\n    stripbytes = rowblocksperstrip * rowblockbytes;\n    assert( stripbytes <= 0x7FFFFFFFUL );\n    nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);\n    if( nstrips == 0 )\n        return;\n    if( tif->tif_mode == O_RDONLY &&\n        nstrips > 1000000 )\n    {\n        uint64 last_offset = TIFFGetStrileOffset(tif, td->td_nstrips-1);\n        uint64 filesize = TIFFGetFileSize(tif);\n        uint64 last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips-1);\n        if( last_offset > filesize ||\n            last_bytecount > filesize - last_offset )\n        {\n            return;\n        }\n    }\n    allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);\n}', 'uint64 TIFFGetStrileOffset(TIFF *tif, uint32 strile)\n{\n    return TIFFGetStrileOffsetWithErr(tif, strile, NULL);\n}', 'uint64 TIFFGetStrileOffsetWithErr(TIFF *tif, uint32 strile, int *pbErr)\n{\n    TIFFDirectory *td = &tif->tif_dir;\n    return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,\n                               &(td->td_stripoffset_entry),\n                               &(td->td_stripoffset_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 _TIFFFetchStrileValue(TIFF* tif,\n                                 uint32 strile,\n                                 TIFFDirEntry* dirent,\n                                 uint64** parray)\n{\n    static const char module[] = "_TIFFFetchStrileValue";\n    TIFFDirectory *td = &tif->tif_dir;\n    if( strile >= dirent->tdir_count )\n    {\n        return 0;\n    }\n    if( strile >= td->td_stripoffsetbyteallocsize )\n    {\n        uint32 nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;\n        uint32 nStripArrayAllocNew;\n        uint64 nArraySize64;\n        size_t nArraySize;\n        uint64* offsetArray;\n        uint64* bytecountArray;\n        if( strile > 1000000 )\n        {\n            uint64 filesize = TIFFGetFileSize(tif);\n            if( strile > filesize / sizeof(uint32) )\n            {\n                TIFFErrorExt(tif->tif_clientdata, module, "File too short");\n                return 0;\n            }\n        }\n        if( td->td_stripoffsetbyteallocsize == 0 &&\n            td->td_nstrips < 1024 * 1024 )\n        {\n            nStripArrayAllocNew = td->td_nstrips;\n        }\n        else\n        {\n#define TIFF_MAX(a,b) (((a)>(b)) ? (a) : (b))\n#define TIFF_MIN(a,b) (((a)<(b)) ? (a) : (b))\n            nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U );\n            if( nStripArrayAllocNew < 0xFFFFFFFFU / 2  )\n                nStripArrayAllocNew *= 2;\n            nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);\n        }\n        assert( strile < nStripArrayAllocNew );\n        nArraySize64 = (uint64)sizeof(uint64) * nStripArrayAllocNew;\n        nArraySize = (size_t)(nArraySize64);\n#if SIZEOF_SIZE_T == 4\n        if( nArraySize != nArraySize64 )\n        {\n            TIFFErrorExt(tif->tif_clientdata, module,\n                        "Cannot allocate strip offset and bytecount arrays");\n            return 0;\n        }\n#endif\n        offsetArray = (uint64*)(\n            _TIFFrealloc( td->td_stripoffset_p, nArraySize ) );\n        bytecountArray = (uint64*)(\n            _TIFFrealloc( td->td_stripbytecount_p, nArraySize ) );\n        if( offsetArray )\n            td->td_stripoffset_p = offsetArray;\n        if( bytecountArray )\n            td->td_stripbytecount_p = bytecountArray;\n        if( offsetArray && bytecountArray )\n        {\n            td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;\n            memset(td->td_stripoffset_p + nStripArrayAllocBefore,\n                0xFF,\n                (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64) );\n            memset(td->td_stripbytecount_p + nStripArrayAllocBefore,\n                0xFF,\n                (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * sizeof(uint64) );\n        }\n        else\n        {\n            TIFFErrorExt(tif->tif_clientdata, module,\n                        "Cannot allocate strip offset and bytecount arrays");\n            _TIFFfree(td->td_stripoffset_p);\n            td->td_stripoffset_p = NULL;\n            _TIFFfree(td->td_stripbytecount_p);\n            td->td_stripbytecount_p = NULL;\n            td->td_stripoffsetbyteallocsize = 0;\n        }\n    }\n    if( *parray == NULL || strile >= td->td_stripoffsetbyteallocsize )\n        return 0;\n    if( ~((*parray)[strile]) == 0 )\n    {\n        if( !_TIFFPartialReadStripArray( tif, dirent, strile, *parray ) )\n        {\n            (*parray)[strile] = 0;\n            return 0;\n        }\n    }\n    return 1;\n}']