contrib/addtiffo/tif_overview.c:865: error: Buffer Overrun L3
  Offset: 1 Size: [0, +oo] by call to `TIFFCreateOvrCache`.

contrib/addtiffo/tif_overview.c:856:22: Call
854.         }
855. 
856.         nDirOffset = TIFF_WriteOverview( hTIFF, nOXSize, nOYSize,
                          ^
857.                                          nBitsPerPixel, nPlanarConfig,
858.                                          nSamples, nOBlockXSize, nOBlockYSize,

contrib/addtiffo/tif_overview.c:149:9: Call
  147. /*      Write directory, and return byte offset.                        */
  148. /* -------------------------------------------------------------------- */
  149.     if( TIFFWriteCheck( hTIFF, bTiled, "TIFFBuildOverviews" ) == 0 )
               ^
  150.         return 0;
  151. 

libtiff/tif_write.c:564:1: Parameter `*tif->tif_dir.td_stripbytecount_p`
    562.  * that important information is not changed.
    563.  */
    564. int
         ^
    565. TIFFWriteCheck(TIFF* tif, int tiles, const char* module)
    566. {

contrib/addtiffo/tif_overview.c:865:25: Call
863.                                          nHorSubsampling, nVerSubsampling );
864.         
865.         papoRawBIs[i] = TIFFCreateOvrCache( hTIFF, nDirOffset );
                             ^
866.     }
867. 

contrib/addtiffo/tif_ovrcache.c:41:1: Parameter `*hTIFF->tif_dir.td_stripbytecount_p`
  39. /************************************************************************/
  40. 
  41. TIFFOvrCache *TIFFCreateOvrCache( TIFF *hTIFF, toff_t nDirOffset )
      ^
  42. 
  43. {

contrib/addtiffo/tif_ovrcache.c:55:5: Call
  53. /* -------------------------------------------------------------------- */
  54.     nBaseDirOffset = TIFFCurrentDirOffset( psCache->hTIFF );
  55.     TIFFSetSubDirectory( hTIFF, nDirOffset );
          ^
  56.     
  57.     TIFFGetField( hTIFF, TIFFTAG_IMAGEWIDTH, &(psCache->nXSize) );

libtiff/tif_dir.c:1649:1: Parameter `*tif->tif_dir.td_stripbytecount_p`
    1647.  * the SubIFD tag (e.g. thumbnail images).
    1648.  */
    1649. int
          ^
    1650. TIFFSetSubDirectory(TIFF* tif, uint64 diroff)
    1651. {

libtiff/tif_dir.c:1658:10: Call
    1656. 	 */
    1657. 	tif->tif_dirnumber = 0;
    1658. 	return (TIFFReadDirectory(tif));
                   ^
    1659. }
    1660. 

libtiff/tif_dirread.c:3578:1: Parameter `*tif->tif_dir.td_stripbytecount_p`
      3576.  * format. We read directories sequentially.
      3577.  */
      3578. int
            ^
      3579. TIFFReadDirectory(TIFF* tif)
      3580. {

libtiff/tif_dirread.c:3631:2: Call
      3629. 
      3630. 	/* free any old stuff and reinit */
      3631. 	TIFFFreeDirectory(tif);
             ^
      3632. 	TIFFDefaultDirectory(tif);
      3633. 	/*

libtiff/tif_dir.c:1266:1: Parameter `*tif->tif_dir.td_stripbytecount_p`
        1264.  * Release storage associated with a directory.
        1265.  */
        1266. void
              ^
        1267. TIFFFreeDirectory(TIFF* tif)
        1268. {

libtiff/tif_dirread.c:4199:10: Call
      4197. 			   && tif->tif_dir.td_nstrips > 2
      4198. 			   && tif->tif_dir.td_compression == COMPRESSION_NONE
      4199. 			   && TIFFGetStrileByteCount(tif, 0) != TIFFGetStrileByteCount(tif, 1)
                     ^
      4200. 			   && TIFFGetStrileByteCount(tif, 0) != 0
      4201. 			   && TIFFGetStrileByteCount(tif, 1) != 0 ) {

libtiff/tif_dirread.c:6263:1: Parameter `*tif->tif_dir.td_stripbytecount_p`
        6261. 
        6262. /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
        6263. uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
              ^
        6264. {
        6265.     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);

libtiff/tif_dirread.c:6265:12: Call
        6263. uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
        6264. {
        6265.     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
                         ^
        6266. }
        6267. 

libtiff/tif_dirread.c:6269:1: Parameter `*tif->tif_dir.td_stripbytecount_p`
          6267. 
          6268. /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
          6269. uint64 TIFFGetStrileByteCountWithErr(TIFF *tif, uint32 strile, int *pbErr)
                ^
          6270. {
          6271.     TIFFDirectory *td = &tif->tif_dir;

libtiff/tif_dirread.c:6272:12: Call
          6270. {
          6271.     TIFFDirectory *td = &tif->tif_dir;
          6272.     return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
                           ^
          6273.                                &(td->td_stripbytecount_entry),
          6274.                                &(td->td_stripbytecount_p), pbErr);

libtiff/tif_dirread.c:6204:1: Parameter `*tif->tif_dir.td_stripbytecount_p`
            6202. }
            6203. 
            6204. static uint64 _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32 strile,
                  ^
            6205.                                                    TIFFDirEntry* dirent,
            6206.                                                    uint64** parray,

libtiff/tif_dirread.c:4199:44: Call
      4197. 			   && tif->tif_dir.td_nstrips > 2
      4198. 			   && tif->tif_dir.td_compression == COMPRESSION_NONE
      4199. 			   && TIFFGetStrileByteCount(tif, 0) != TIFFGetStrileByteCount(tif, 1)
                                                       ^
      4200. 			   && TIFFGetStrileByteCount(tif, 0) != 0
      4201. 			   && TIFFGetStrileByteCount(tif, 1) != 0 ) {

libtiff/tif_dirread.c:6263:1: Parameter `strile`
        6261. 
        6262. /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
        6263. uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
              ^
        6264. {
        6265.     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);

libtiff/tif_dirread.c:6265:12: Call
        6263. uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
        6264. {
        6265.     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
                         ^
        6266. }
        6267. 

libtiff/tif_dirread.c:6269:1: Parameter `strile`
          6267. 
          6268. /* Return the value of the TileByteCounts/StripByteCounts array for the specified tile/strile */
          6269. uint64 TIFFGetStrileByteCountWithErr(TIFF *tif, uint32 strile, int *pbErr)
                ^
          6270. {
          6271.     TIFFDirectory *td = &tif->tif_dir;

libtiff/tif_dirread.c:6272:12: Call
          6270. {
          6271.     TIFFDirectory *td = &tif->tif_dir;
          6272.     return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
                           ^
          6273.                                &(td->td_stripbytecount_entry),
          6274.                                &(td->td_stripbytecount_p), pbErr);

libtiff/tif_dirread.c:6204:1: Parameter `strile`
            6202. }
            6203. 
            6204. static uint64 _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32 strile,
                  ^
            6205.                                                    TIFFDirEntry* dirent,
            6206.                                                    uint64** parray,

libtiff/tif_dirread.c:6230:19: Call
            6228.         else
            6229.         {
            6230.              if( !_TIFFFetchStrileValue(tif, strile, dirent, parray) )
                                    ^
            6231.              {
            6232.                 if( pbErr )

libtiff/tif_dirread.c:6101:1: <Offset trace>
6099. }
6100. 
6101. static int _TIFFFetchStrileValue(TIFF* tif,
      ^
6102.                                  uint32 strile,
6103.                                  TIFFDirEntry* dirent,

libtiff/tif_dirread.c:6101:1: Parameter `strile`
              6099. }
              6100. 
              6101. static int _TIFFFetchStrileValue(TIFF* tif,
                    ^
              6102.                                  uint32 strile,
              6103.                                  TIFFDirEntry* dirent,

libtiff/tif_dirread.c:6101:1: <Length trace>
6099. }
6100. 
6101. static int _TIFFFetchStrileValue(TIFF* tif,
      ^
6102.                                  uint32 strile,
6103.                                  TIFFDirEntry* dirent,

libtiff/tif_dirread.c:6101:1: Parameter `**parray`
              6099. }
              6100. 
              6101. static int _TIFFFetchStrileValue(TIFF* tif,
                    ^
              6102.                                  uint32 strile,
              6103.                                  TIFFDirEntry* dirent,

libtiff/tif_dirread.c:6192:10: Array access: Offset: 1 Size: [0, +oo] by call to `TIFFCreateOvrCache` 
              6190.         return 0;
              6191. 
              6192.     if( ~((*parray)[strile]) == 0 )
                             ^
              6193.     {
              6194.         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;
}
['void TIFFBuildOverviews( TIFF *hTIFF, int nOverviews, int * panOvList,\n                         int bUseSubIFDs, const char *pszResampleMethod,\n                         int (*pfnProgress)( double, void * ),\n                         void * pProgressData )\n{\n    TIFFOvrCache\t**papoRawBIs;\n    uint32\t\tnXSize, nYSize, nBlockXSize, nBlockYSize;\n    uint16\t\tnBitsPerPixel, nPhotometric, nCompressFlag, nSamples,\n        nPlanarConfig, nSampleFormat;\n    int         bSubsampled;\n    uint16      nHorSubsampling, nVerSubsampling;\n    int\t\t\tbTiled, nSXOff, nSYOff, i;\n    unsigned char\t*pabySrcTile;\n    uint16\t\t*panRedMap, *panGreenMap, *panBlueMap;\n    TIFFErrorHandler    pfnWarning;\n    (void) pfnProgress;\n    (void) pProgressData;\n    TIFFGetField( hTIFF, TIFFTAG_IMAGEWIDTH, &nXSize );\n    TIFFGetField( hTIFF, TIFFTAG_IMAGELENGTH, &nYSize );\n    TIFFGetField( hTIFF, TIFFTAG_BITSPERSAMPLE, &nBitsPerPixel );\n    TIFFGetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, &nSamples );\n    TIFFGetFieldDefaulted( hTIFF, TIFFTAG_PLANARCONFIG, &nPlanarConfig );\n    TIFFGetFieldDefaulted( hTIFF, TIFFTAG_PHOTOMETRIC, &nPhotometric );\n    TIFFGetFieldDefaulted( hTIFF, TIFFTAG_COMPRESSION, &nCompressFlag );\n    TIFFGetFieldDefaulted( hTIFF, TIFFTAG_SAMPLEFORMAT, &nSampleFormat );\n    if( nPhotometric == PHOTOMETRIC_YCBCR || nPhotometric == PHOTOMETRIC_ITULAB )\n    {\n        if( nBitsPerPixel != 8 || nSamples != 3 || nPlanarConfig != PLANARCONFIG_CONTIG ||\n            nSampleFormat != SAMPLEFORMAT_UINT)\n        {\n            TIFFErrorExt( TIFFClientdata(hTIFF), "TIFFBuildOverviews",\n                          "File `%s\' has an unsupported subsampling configuration.\\n",\n                          TIFFFileName(hTIFF) );\n            return;\n        }\n        bSubsampled = 1;\n        TIFFGetField( hTIFF, TIFFTAG_YCBCRSUBSAMPLING, &nHorSubsampling, &nVerSubsampling );\n    }\n    else\n    {\n        if( nBitsPerPixel < 8 )\n        {\n            TIFFErrorExt( TIFFClientdata(hTIFF), "TIFFBuildOverviews",\n                          "File `%s\' has samples of %d bits per sample.  Sample\\n"\n                          "sizes of less than 8 bits per sample are not supported.\\n",\n                          TIFFFileName(hTIFF), nBitsPerPixel );\n            return;\n        }\n        bSubsampled = 0;\n        nHorSubsampling = 1;\n        nVerSubsampling = 1;\n    }\n    pfnWarning = TIFFSetWarningHandler( NULL );\n    if( TIFFGetField( hTIFF, TIFFTAG_ROWSPERSTRIP, &(nBlockYSize) ) )\n    {\n        nBlockXSize = nXSize;\n        bTiled = FALSE;\n    }\n    else\n    {\n        TIFFGetField( hTIFF, TIFFTAG_TILEWIDTH, &nBlockXSize );\n        TIFFGetField( hTIFF, TIFFTAG_TILELENGTH, &nBlockYSize );\n        bTiled = TRUE;\n    }\n    if( TIFFGetField( hTIFF, TIFFTAG_COLORMAP,\n                      &panRedMap, &panGreenMap, &panBlueMap ) )\n    {\n        uint16\t\t*panRed2, *panGreen2, *panBlue2;\n        int             nColorCount = 1 << nBitsPerPixel;\n        panRed2 = (uint16 *) _TIFFmalloc(2*nColorCount);\n        panGreen2 = (uint16 *) _TIFFmalloc(2*nColorCount);\n        panBlue2 = (uint16 *) _TIFFmalloc(2*nColorCount);\n        memcpy( panRed2, panRedMap, 2 * nColorCount );\n        memcpy( panGreen2, panGreenMap, 2 * nColorCount );\n        memcpy( panBlue2, panBlueMap, 2 * nColorCount );\n        panRedMap = panRed2;\n        panGreenMap = panGreen2;\n        panBlueMap = panBlue2;\n    }\n    else\n    {\n        panRedMap = panGreenMap = panBlueMap = NULL;\n    }\n    papoRawBIs = (TIFFOvrCache **) _TIFFmalloc(nOverviews*sizeof(void*));\n    for( i = 0; i < nOverviews; i++ )\n    {\n        uint32  nOXSize, nOYSize, nOBlockXSize, nOBlockYSize;\n        toff_t  nDirOffset;\n        nOXSize = (nXSize + panOvList[i] - 1) / panOvList[i];\n        nOYSize = (nYSize + panOvList[i] - 1) / panOvList[i];\n        nOBlockXSize = MIN(nBlockXSize,nOXSize);\n        nOBlockYSize = MIN(nBlockYSize,nOYSize);\n        if( bTiled )\n        {\n            if( (nOBlockXSize % 16) != 0 )\n                nOBlockXSize = nOBlockXSize + 16 - (nOBlockXSize % 16);\n            if( (nOBlockYSize % 16) != 0 )\n                nOBlockYSize = nOBlockYSize + 16 - (nOBlockYSize % 16);\n        }\n        nDirOffset = TIFF_WriteOverview( hTIFF, nOXSize, nOYSize,\n                                         nBitsPerPixel, nPlanarConfig,\n                                         nSamples, nOBlockXSize, nOBlockYSize,\n                                         bTiled, nCompressFlag, nPhotometric,\n                                         nSampleFormat,\n                                         panRedMap, panGreenMap, panBlueMap,\n                                         bUseSubIFDs,\n                                         nHorSubsampling, nVerSubsampling );\n        papoRawBIs[i] = TIFFCreateOvrCache( hTIFF, nDirOffset );\n    }\n    if( panRedMap != NULL )\n    {\n        _TIFFfree( panRedMap );\n        _TIFFfree( panGreenMap );\n        _TIFFfree( panBlueMap );\n    }\n    if( bTiled )\n        pabySrcTile = (unsigned char *) _TIFFmalloc(TIFFTileSize(hTIFF));\n    else\n        pabySrcTile = (unsigned char *) _TIFFmalloc(TIFFStripSize(hTIFF));\n    for( nSYOff = 0; nSYOff < (int) nYSize; nSYOff += nBlockYSize )\n    {\n        for( nSXOff = 0; nSXOff < (int) nXSize; nSXOff += nBlockXSize )\n        {\n            TIFF_ProcessFullResBlock( hTIFF, nPlanarConfig,\n                                      bSubsampled,nHorSubsampling,nVerSubsampling,\n                                      nOverviews, panOvList,\n                                      nBitsPerPixel, nSamples, papoRawBIs,\n                                      nSXOff, nSYOff, pabySrcTile,\n                                      nBlockXSize, nBlockYSize,\n                                      nSampleFormat, pszResampleMethod );\n        }\n    }\n    _TIFFfree( pabySrcTile );\n    for( i = 0; i < nOverviews; i++ )\n    {\n        TIFFDestroyOvrCache( papoRawBIs[i] );\n    }\n    if( papoRawBIs != NULL )\n        _TIFFfree( papoRawBIs );\n    TIFFSetWarningHandler( pfnWarning );\n}', 'uint32 TIFF_WriteOverview( TIFF *hTIFF, uint32 nXSize, uint32 nYSize,\n                           int nBitsPerPixel, int nPlanarConfig, int nSamples,\n                           int nBlockXSize, int nBlockYSize,\n                           int bTiled, int nCompressFlag, int nPhotometric,\n                           int nSampleFormat,\n                           unsigned short *panRed,\n                           unsigned short *panGreen,\n                           unsigned short *panBlue,\n                           int bUseSubIFDs,\n                           int nHorSubsampling, int nVerSubsampling )\n{\n    toff_t\tnBaseDirOffset;\n    toff_t\tnOffset;\n    tdir_t\tiNumDir;\n    (void) bUseSubIFDs;\n    nBaseDirOffset = TIFFCurrentDirOffset( hTIFF );\n    TIFFCreateDirectory( hTIFF );\n    TIFFSetField( hTIFF, TIFFTAG_IMAGEWIDTH, nXSize );\n    TIFFSetField( hTIFF, TIFFTAG_IMAGELENGTH, nYSize );\n    if( nSamples == 1 )\n        TIFFSetField( hTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );\n    else\n        TIFFSetField( hTIFF, TIFFTAG_PLANARCONFIG, nPlanarConfig );\n    TIFFSetField( hTIFF, TIFFTAG_BITSPERSAMPLE, nBitsPerPixel );\n    TIFFSetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, nSamples );\n    TIFFSetField( hTIFF, TIFFTAG_COMPRESSION, nCompressFlag );\n    TIFFSetField( hTIFF, TIFFTAG_PHOTOMETRIC, nPhotometric );\n    TIFFSetField( hTIFF, TIFFTAG_SAMPLEFORMAT, nSampleFormat );\n    if( bTiled )\n    {\n        TIFFSetField( hTIFF, TIFFTAG_TILEWIDTH, nBlockXSize );\n        TIFFSetField( hTIFF, TIFFTAG_TILELENGTH, nBlockYSize );\n    }\n    else\n        TIFFSetField( hTIFF, TIFFTAG_ROWSPERSTRIP, nBlockYSize );\n    TIFFSetField( hTIFF, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE );\n    if( nPhotometric == PHOTOMETRIC_YCBCR || nPhotometric == PHOTOMETRIC_ITULAB )\n    {\n        TIFFSetField( hTIFF, TIFFTAG_YCBCRSUBSAMPLING, nHorSubsampling, nVerSubsampling);\n    }\n    if( panRed != NULL )\n    {\n        TIFFSetField( hTIFF, TIFFTAG_COLORMAP, panRed, panGreen, panBlue );\n    }\n    if( TIFFWriteCheck( hTIFF, bTiled, "TIFFBuildOverviews" ) == 0 )\n        return 0;\n    TIFFWriteDirectory( hTIFF );\n    iNumDir = TIFFNumberOfDirectories(hTIFF);\n    if( iNumDir > TIFF_DIR_MAX )\n    {\n        TIFFErrorExt( TIFFClientdata(hTIFF),\n                      "TIFF_WriteOverview",\n                      "File `%s\' has too many directories.\\n",\n                      TIFFFileName(hTIFF) );\n        exit(-1);\n    }\n    TIFFSetDirectory( hTIFF, (tdir_t) (iNumDir - 1) );\n    nOffset = TIFFCurrentDirOffset( hTIFF );\n    TIFFSetSubDirectory( hTIFF, nBaseDirOffset );\n    return nOffset;\n}', 'int\nTIFFWriteCheck(TIFF* tif, int tiles, const char* module)\n{\n\tif (tif->tif_mode == O_RDONLY) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module, "File not open for writing");\n\t\treturn (0);\n\t}\n\tif (tiles ^ isTiled(tif)) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module, tiles ?\n\t\t    "Can not write tiles to a striped image" :\n\t\t    "Can not write scanlines to a tiled image");\n\t\treturn (0);\n\t}\n        _TIFFFillStriles( tif );\n\tif (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t    "Must set \\"ImageWidth\\" before writing data");\n\t\treturn (0);\n\t}\n\tif (tif->tif_dir.td_samplesperpixel == 1) {\n\t\tif (!TIFFFieldSet(tif, FIELD_PLANARCONFIG))\n                    tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;\n\t} else {\n\t\tif (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t    "Must set \\"PlanarConfiguration\\" before writing data");\n\t\t\treturn (0);\n\t\t}\n\t}\n\tif (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif)) {\n\t\ttif->tif_dir.td_nstrips = 0;\n\t\tTIFFErrorExt(tif->tif_clientdata, module, "No space for %s arrays",\n\t\t    isTiled(tif) ? "tile" : "strip");\n\t\treturn (0);\n\t}\n\tif (isTiled(tif))\n\t{\n\t\ttif->tif_tilesize = TIFFTileSize(tif);\n\t\tif (tif->tif_tilesize == 0)\n\t\t\treturn (0);\n\t}\n\telse\n\t\ttif->tif_tilesize = (tmsize_t)(-1);\n\ttif->tif_scanlinesize = TIFFScanlineSize(tif);\n\tif (tif->tif_scanlinesize == 0)\n\t\treturn (0);\n\ttif->tif_flags |= TIFF_BEENWRITING;\n\treturn (1);\n}', 'TIFFOvrCache *TIFFCreateOvrCache( TIFF *hTIFF, toff_t nDirOffset )\n{\n    TIFFOvrCache\t*psCache;\n    toff_t\t\tnBaseDirOffset;\n    psCache = (TIFFOvrCache *) _TIFFmalloc(sizeof(TIFFOvrCache));\n    psCache->nDirOffset = nDirOffset;\n    psCache->hTIFF = hTIFF;\n    nBaseDirOffset = TIFFCurrentDirOffset( psCache->hTIFF );\n    TIFFSetSubDirectory( hTIFF, nDirOffset );\n    TIFFGetField( hTIFF, TIFFTAG_IMAGEWIDTH, &(psCache->nXSize) );\n    TIFFGetField( hTIFF, TIFFTAG_IMAGELENGTH, &(psCache->nYSize) );\n    TIFFGetField( hTIFF, TIFFTAG_BITSPERSAMPLE, &(psCache->nBitsPerPixel) );\n    TIFFGetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, &(psCache->nSamples) );\n    TIFFGetField( hTIFF, TIFFTAG_PLANARCONFIG, &(psCache->nPlanarConfig) );\n    if( !TIFFIsTiled( hTIFF ) )\n    {\n        TIFFGetField( hTIFF, TIFFTAG_ROWSPERSTRIP, &(psCache->nBlockYSize) );\n        psCache->nBlockXSize = psCache->nXSize;\n        psCache->nBytesPerBlock = TIFFStripSize(hTIFF);\n        psCache->bTiled = FALSE;\n    }\n    else\n    {\n        TIFFGetField( hTIFF, TIFFTAG_TILEWIDTH, &(psCache->nBlockXSize) );\n        TIFFGetField( hTIFF, TIFFTAG_TILELENGTH, &(psCache->nBlockYSize) );\n        psCache->nBytesPerBlock = TIFFTileSize(hTIFF);\n        psCache->bTiled = TRUE;\n    }\n    psCache->nBlocksPerRow = (psCache->nXSize + psCache->nBlockXSize - 1)\n        \t\t/ psCache->nBlockXSize;\n    psCache->nBlocksPerColumn = (psCache->nYSize + psCache->nBlockYSize - 1)\n        \t\t/ psCache->nBlockYSize;\n    if (psCache->nPlanarConfig == PLANARCONFIG_SEPARATE)\n        psCache->nBytesPerRow = psCache->nBytesPerBlock\n            * psCache->nBlocksPerRow * psCache->nSamples;\n    else\n        psCache->nBytesPerRow =\n            psCache->nBytesPerBlock * psCache->nBlocksPerRow;\n    psCache->pabyRow1Blocks =\n        (unsigned char *) _TIFFmalloc(psCache->nBytesPerRow);\n    psCache->pabyRow2Blocks =\n        (unsigned char *) _TIFFmalloc(psCache->nBytesPerRow);\n    if ( psCache->pabyRow1Blocks == NULL\n         || psCache->pabyRow2Blocks == NULL )\n    {\n\t\tTIFFErrorExt( hTIFF->tif_clientdata, hTIFF->tif_name,\n\t\t\t\t\t  "Can\'t allocate memory for overview cache." );\n                if (psCache->pabyRow1Blocks) _TIFFfree(psCache->pabyRow1Blocks);\n                if (psCache->pabyRow2Blocks) _TIFFfree(psCache->pabyRow2Blocks);\n                _TIFFfree( psCache );\n        return NULL;\n    }\n    _TIFFmemset( psCache->pabyRow1Blocks, 0, psCache->nBytesPerRow );\n    _TIFFmemset( psCache->pabyRow2Blocks, 0, psCache->nBytesPerRow );\n    psCache->nBlockOffset = 0;\n    TIFFSetSubDirectory( psCache->hTIFF, nBaseDirOffset );\n    return psCache;\n}', 'int\nTIFFSetSubDirectory(TIFF* tif, uint64 diroff)\n{\n\ttif->tif_nextdiroff = diroff;\n\ttif->tif_dirnumber = 0;\n\treturn (TIFFReadDirectory(tif));\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_tag=IGNORE;\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_tag=IGNORE;\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_tag=IGNORE;\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_tag!=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_tag=IGNORE;\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_tag!=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_tag=IGNORE;\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_tag=IGNORE;\n\t\t\t\t\t\tbreak;\n                                        default:\n                                            if( !_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag) )\n                                                dp->tdir_tag=IGNORE;\n                                            break;\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        if (!_TIFFFillStriles(tif))\n            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\tswitch (dp->tdir_tag)\n\t\t{\n\t\t\tcase IGNORE:\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_MINSAMPLEVALUE:\n\t\t\tcase TIFFTAG_MAXSAMPLEVALUE:\n\t\t\tcase TIFFTAG_BITSPERSAMPLE:\n\t\t\tcase TIFFTAG_DATATYPE:\n\t\t\tcase TIFFTAG_SAMPLEFORMAT:\n\t\t\t\t{\n\t\t\t\t\tuint16 value;\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\t\t\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\t\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tif (!TIFFSetField(tif,dp->tdir_tag,value))\n\t\t\t\t\t\tgoto bad;\n                    if( dp->tdir_tag == TIFFTAG_BITSPERSAMPLE )\n                        bitspersample_read = TRUE;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_SMINSAMPLEVALUE:\n\t\t\tcase TIFFTAG_SMAXSAMPLEVALUE:\n\t\t\t\t{\n\t\t\t\t\tdouble *data = NULL;\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\tuint32 saved_flags;\n\t\t\t\t\tint m;\n\t\t\t\t\tif (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)\n\t\t\t\t\t\terr = TIFFReadDirEntryErrCount;\n\t\t\t\t\telse\n\t\t\t\t\t\terr = TIFFReadDirEntryDoubleArray(tif, dp, &data);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tsaved_flags = tif->tif_flags;\n\t\t\t\t\ttif->tif_flags |= TIFF_PERSAMPLE;\n\t\t\t\t\tm = TIFFSetField(tif,dp->tdir_tag,data);\n\t\t\t\t\ttif->tif_flags = saved_flags;\n\t\t\t\t\t_TIFFfree(data);\n\t\t\t\t\tif (!m)\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\tcase TIFFTAG_TILEOFFSETS:\n                                _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),\n                                             dp, sizeof(TIFFDirEntry) );\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n                                _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),\n                                             dp, sizeof(TIFFDirEntry) );\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_COLORMAP:\n\t\t\tcase TIFFTAG_TRANSFERFUNCTION:\n\t\t\t\t{\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\tuint32 countpersample;\n\t\t\t\t\tuint32 countrequired;\n\t\t\t\t\tuint32 incrementpersample;\n\t\t\t\t\tuint16* value=NULL;\n                    if( !bitspersample_read )\n                    {\n                        fip = TIFFFieldWithTag(tif,dp->tdir_tag);\n                        TIFFWarningExt(tif->tif_clientdata,module,\n                                       "Ignoring %s since BitsPerSample tag not found",\n                                       fip ? fip->field_name : "unknown tagname");\n                        continue;\n                    }\n\t\t\t\t\tif (tif->tif_dir.td_bitspersample > 24)\n\t\t\t\t\t{\n\t\t\t\t\t    fip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t    TIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t\t\t"Ignoring %s because BitsPerSample=%d>24",\n\t\t\t\t\t\tfip ? fip->field_name : "unknown tagname",\n\t\t\t\t\t\ttif->tif_dir.td_bitspersample);\n\t\t\t\t\t    continue;\n\t\t\t\t\t}\n\t\t\t\t\tcountpersample=(1U<<tif->tif_dir.td_bitspersample);\n\t\t\t\t\tif ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))\n\t\t\t\t\t{\n\t\t\t\t\t\tcountrequired=countpersample;\n\t\t\t\t\t\tincrementpersample=0;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tcountrequired=3*countpersample;\n\t\t\t\t\t\tincrementpersample=countpersample;\n\t\t\t\t\t}\n\t\t\t\t\tif (dp->tdir_count!=(uint64)countrequired)\n\t\t\t\t\t\terr=TIFFReadDirEntryErrCount;\n\t\t\t\t\telse\n\t\t\t\t\t\terr=TIFFReadDirEntryShortArray(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n                    {\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);\n                    }\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tTIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);\n\t\t\t\t\t\t_TIFFfree(value);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_OSUBFILETYPE:\n\t\t\t\t{\n\t\t\t\t\tuint16 valueo;\n\t\t\t\t\tuint32 value;\n\t\t\t\t\tif (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tswitch (valueo)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcase OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;\n\t\t\t\t\t\t\tcase OFILETYPE_PAGE: value=FILETYPE_PAGE; break;\n\t\t\t\t\t\t\tdefault: value=0; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (value!=0)\n\t\t\t\t\t\t\tTIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\t(void) TIFFFetchNormalTag(tif, dp, TRUE);\n\t\t\t\tbreak;\n\t\t}\n\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}', 'void\nTIFFFreeDirectory(TIFF* tif)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tint            i;\n\t_TIFFmemset(td->td_fieldsset, 0, FIELD_SETLONGS);\n\tCleanupField(td_sminsamplevalue);\n\tCleanupField(td_smaxsamplevalue);\n\tCleanupField(td_colormap[0]);\n\tCleanupField(td_colormap[1]);\n\tCleanupField(td_colormap[2]);\n\tCleanupField(td_sampleinfo);\n\tCleanupField(td_subifd);\n\tCleanupField(td_inknames);\n\tCleanupField(td_refblackwhite);\n\tCleanupField(td_transferfunction[0]);\n\tCleanupField(td_transferfunction[1]);\n\tCleanupField(td_transferfunction[2]);\n\tCleanupField(td_stripoffset_p);\n\tCleanupField(td_stripbytecount_p);\n        td->td_stripoffsetbyteallocsize = 0;\n\tTIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);\n\tTIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);\n\tfor( i = 0; i < td->td_customValueCount; i++ ) {\n\t\tif (td->td_customValues[i].value)\n\t\t\t_TIFFfree(td->td_customValues[i].value);\n\t}\n\ttd->td_customValueCount = 0;\n\tCleanupField(td_customValues);\n        _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));\n        _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));\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 _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}']