tools/tiffcrop.c:6905: error: Integer Overflow L2
  ([min(1, `out->tif_dir.td_imagedepth`), 4294967294] + [0, 4294967294-min(1, `out->tif_dir.td_imagedepth`)]):unsigned32 by call to `writeSingleSection`.

tools/tiffcrop.c:6863:1: Parameter `out->tif_dir.td_imagedepth`
6861.   } /* end writeRegions */
6862. 
6863. static int
      ^
6864. writeImageSections(TIFF *in, TIFF *out, struct image_data *image,
6865. 		   struct pagedef *page, struct pageseg *sections,

tools/tiffcrop.c:6905:9: Call
6903. 
6904.   /* call the write routine here instead of outside the loop */
6905.     if (writeSingleSection(in, out, image, dump, width, length, hres, vres, sect_buff))
              ^
6906.       {
6907.       TIFFError("writeImageSections", "Unable to write image section");

tools/tiffcrop.c:6921:1: Parameter `out->tif_dir.td_imagedepth`
  6919.  * original code assumes we are always copying all samples.
  6920.  */
  6921. static int  
        ^
  6922. writeSingleSection(TIFF *in, TIFF *out, struct image_data *image,
  6923.                    struct dump_opts *dump, uint32 width, uint32 length,

tools/tiffcrop.c:7169:7: Call
  7167.     {
  7168.     if (config == PLANARCONFIG_CONTIG)
  7169.       writeBufferToContigTiles (out, sect_buff, length, width, spp, dump);
              ^
  7170.     else
  7171.       writeBufferToSeparateTiles (out, sect_buff, length, width, spp, dump);

tools/tiffcrop.c:1194:1: Parameter `out->tif_dir.td_imagedepth`
    1192.  * to be written out as a tile.
    1193.  */
    1194. static int writeBufferToContigTiles (TIFF* out, uint8* buf, uint32 imagelength,
          ^
    1195. 				       uint32 imagewidth, tsample_t spp, 
    1196.                                        struct dump_opts* dump)

tools/tiffcrop.c:1252:11: Call
    1250.         }
    1251. 
    1252.       if (TIFFWriteTile(out, tilebuf, col, row, 0, 0) < 0)
                    ^
    1253.         {
    1254. 	TIFFError("writeBufferToContigTiles",

libtiff/tif_write.c:336:1: Parameter `tif->tif_dir.td_imagedepth`
      334.  * tile is selected by the (x,y,z,s) coordinates.
      335.  */
      336. tmsize_t
           ^
      337. TIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
      338. {

libtiff/tif_write.c:339:7: Call
      337. TIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
      338. {
      339. 	if (!TIFFCheckTile(tif, x, y, z, s))
                 ^
      340. 		return ((tmsize_t)(-1));
      341. 	/*

libtiff/tif_tile.c:74:1: Parameter `z`
        72.  * against the image bounds.
        73.  */
        74. int
            ^
        75. TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
        76. {

libtiff/tif_write.c:348:6: Call
      346. 	 */
      347. 	return (TIFFWriteEncodedTile(tif,
      348. 	    TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
                ^
      349. }
      350. 

libtiff/tif_tile.c:37:1: Parameter `tif->tif_dir.td_imagedepth`
        35.  * Compute which tile an (x,y,z,s) value is in.
        36.  */
        37. uint32
            ^
        38. TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
        39. {

libtiff/tif_write.c:347:10: Call
      345. 	 *     after the output buffer is setup in TIFFWriteBufferSetup.
      346. 	 */
      347. 	return (TIFFWriteEncodedTile(tif,
                    ^
      348. 	    TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
      349. }

libtiff/tif_write.c:363:1: Parameter `tif->tif_dir.td_imagedepth`
        361.  *     the image on each write (as TIFFWriteScanline does).
        362.  */
        363. tmsize_t
             ^
        364. TIFFWriteEncodedTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)
        365. {

libtiff/tif_write.c:371:7: Call
        369.         uint32 howmany32;
        370. 
        371. 	if (!WRITECHECKTILES(tif, module))
                   ^
        372. 		return ((tmsize_t)(-1));
        373. 	td = &tif->tif_dir;

libtiff/tif_write.c:532:1: Parameter `tif->tif_dir.td_imagedepth`
          530.  * that important information is not changed.
          531.  */
          532. int
               ^
          533. TIFFWriteCheck(TIFF* tif, int tiles, const char* module)
          534. {

libtiff/tif_write.c:579:46: Call
          577. 		}
          578. 	}
          579. 	if (tif->tif_dir.td_stripoffset == NULL && !TIFFSetupStrips(tif)) {
                                                            ^
          580. 		tif->tif_dir.td_nstrips = 0;
          581. 		TIFFErrorExt(tif->tif_clientdata, module, "No space for %s arrays",

libtiff/tif_write.c:492:1: Parameter `tif->tif_dir.td_imagedepth`
            490.     (TIFFFieldSet(tif,f) && (tif)->tif_dir.td_imagelength == 0)
            491. 
            492. int
                 ^
            493. TIFFSetupStrips(TIFF* tif)
            494. {

libtiff/tif_write.c:500:29: Call
            498. 		td->td_stripsperimage =
            499. 		    isUnspecified(tif, FIELD_TILEDIMENSIONS) ?
            500. 			td->td_samplesperpixel : TIFFNumberOfTiles(tif);
                                             ^
            501. 	else
            502. 		td->td_stripsperimage =

libtiff/tif_tile.c:114:1: <LHS trace>
112.  * Compute how many tiles are in an image.
113.  */
114. uint32
     ^
115. TIFFNumberOfTiles(TIFF* tif)
116. {

libtiff/tif_tile.c:114:1: Parameter `tif->tif_dir.td_imagedepth`
              112.  * Compute how many tiles are in an image.
              113.  */
              114. uint32
                   ^
              115. TIFFNumberOfTiles(TIFF* tif)
              116. {

libtiff/tif_tile.c:114:1: <RHS trace>
112.  * Compute how many tiles are in an image.
113.  */
114. uint32
     ^
115. TIFFNumberOfTiles(TIFF* tif)
116. {

libtiff/tif_tile.c:114:1: Parameter `tif->tif_dir.td_imagedepth`
              112.  * Compute how many tiles are in an image.
              113.  */
              114. uint32
                   ^
              115. TIFFNumberOfTiles(TIFF* tif)
              116. {

libtiff/tif_tile.c:133:6: Binary operation: ([min(1, out->tif_dir.td_imagedepth), 4294967294] + [0, 4294967294-min(1, out->tif_dir.td_imagedepth)]):unsigned32 by call to `writeSingleSection` 
              131. 	    TIFFhowmany_32(td->td_imagelength, dy),
              132. 	    "TIFFNumberOfTiles"),
              133. 	    TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
                        ^
              134. 	if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
              135. 		ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,

uint32
TIFFNumberOfTiles(TIFF* tif)
{
	TIFFDirectory *td = &tif->tif_dir;
	uint32 dx = td->td_tilewidth;
	uint32 dy = td->td_tilelength;
	uint32 dz = td->td_tiledepth;
	uint32 ntiles;
	if (dx == (uint32) -1)
		dx = td->td_imagewidth;
	if (dy == (uint32) -1)
		dy = td->td_imagelength;
	if (dz == (uint32) -1)
		dz = td->td_imagedepth;
	ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
	    _TIFFMultiply32(tif, _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
	    TIFFhowmany_32(td->td_imagelength, dy),
	    "TIFFNumberOfTiles"),
	    TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
	if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
		ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
		    "TIFFNumberOfTiles");
	return (ntiles);
}
['static int\nwriteImageSections(TIFF *in, TIFF *out, struct image_data *image,\n\t\t   struct pagedef *page, struct pageseg *sections,\n\t\t   struct dump_opts * dump, unsigned char *src_buff,\n                   unsigned char **sect_buff_ptr)\n  {\n  double  hres, vres;\n  uint32  i, k, width, length, sectsize;\n  unsigned char *sect_buff = *sect_buff_ptr;\n  hres = page->hres;\n  vres = page->vres;\n  k = page->cols * page->rows;\n  if ((k < 1) || (k > MAX_SECTIONS))\n   {\n   TIFFError("writeImageSections",\n\t     "%d Rows and Columns exceed maximum sections\\nIncrease resolution or reduce sections", k);\n   return (-1);\n   }\n  for (i = 0; i < k; i++)\n    {\n    width  = sections[i].x2 - sections[i].x1 + 1;\n    length = sections[i].y2 - sections[i].y1 + 1;\n    sectsize = (uint32)\n\t    ceil((width * image->bps + 7) / (double)8) * image->spp * length;\n    if (createImageSection(sectsize, sect_buff_ptr))\n      {\n      TIFFError("writeImageSections", "Unable to allocate section buffer");\n      exit (-1);\n      }\n    sect_buff = *sect_buff_ptr;\n    if (extractImageSection (image, &sections[i], src_buff, sect_buff))\n      {\n      TIFFError("writeImageSections", "Unable to extract image sections");\n      exit (-1);\n      }\n    if (writeSingleSection(in, out, image, dump, width, length, hres, vres, sect_buff))\n      {\n      TIFFError("writeImageSections", "Unable to write image section");\n      exit (-1);\n      }\n    }\n  return (0);\n  }', 'static int\nwriteSingleSection(TIFF *in, TIFF *out, struct image_data *image,\n                   struct dump_opts *dump, uint32 width, uint32 length,\n                   double hres, double vres,\n                   unsigned char *sect_buff)\n  {\n  uint16 bps, spp;\n  uint16 input_compression, input_photometric;\n  uint16 input_planar;\n  struct cpTag* p;\n  input_compression = image->compression;\n  input_photometric = image->photometric;\n  spp = image->spp;\n  bps = image->bps;\n  TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);\n  TIFFSetField(out, TIFFTAG_IMAGELENGTH, length);\n  TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bps);\n  TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);\n#ifdef DEBUG2\n  TIFFError("writeSingleSection", "Input compression: %s",\n\t    (input_compression == COMPRESSION_OJPEG) ? "Old Jpeg" :\n\t    ((input_compression == COMPRESSION_JPEG) ?  "New Jpeg" : "Non Jpeg"));\n#endif\n  if (compression != (uint16)-1)\n    TIFFSetField(out, TIFFTAG_COMPRESSION, compression);\n  else\n    {\n    if (input_compression == COMPRESSION_OJPEG)\n      {\n      compression = COMPRESSION_JPEG;\n      jpegcolormode = JPEGCOLORMODE_RAW;\n      TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);\n      }\n    else\n      TIFFSetField(out, TIFFTAG_COMPRESSION, compression);\n    }\n  if (compression == COMPRESSION_JPEG)\n    {\n    if ((input_photometric == PHOTOMETRIC_PALETTE) ||\n        (input_photometric == PHOTOMETRIC_MASK))\n      {\n      TIFFError ("writeSingleSection",\n                 "JPEG compression cannot be used with %s image data",\n\t\t (input_photometric == PHOTOMETRIC_PALETTE) ?\n                 "palette" : "mask");\n      return (-1);\n      }\n    if ((input_photometric == PHOTOMETRIC_RGB) &&\n\t(jpegcolormode == JPEGCOLORMODE_RGB))\n      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);\n    else\n\tTIFFSetField(out, TIFFTAG_PHOTOMETRIC, input_photometric);\n    }\n  else\n    {\n    if (compression == COMPRESSION_SGILOG || compression == COMPRESSION_SGILOG24)\n      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, spp == 1 ?\n\t\t\tPHOTOMETRIC_LOGL : PHOTOMETRIC_LOGLUV);\n    else\n      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, image->photometric);\n    }\n#ifdef DEBUG2\n  TIFFError("writeSingleSection", "Input photometric: %s",\n\t    (input_photometric == PHOTOMETRIC_RGB) ? "RGB" :\n\t    ((input_photometric == PHOTOMETRIC_YCBCR) ?  "YCbCr" : "Not RGB or YCbCr"));\n#endif\n  if (((input_photometric == PHOTOMETRIC_LOGL) ||\n       (input_photometric ==  PHOTOMETRIC_LOGLUV)) &&\n      ((compression != COMPRESSION_SGILOG) &&\n       (compression != COMPRESSION_SGILOG24)))\n    {\n    TIFFError("writeSingleSection",\n              "LogL and LogLuv source data require SGI_LOG or SGI_LOG24 compression");\n    return (-1);\n    }\n  if (fillorder != 0)\n    TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);\n  else\n    CopyTag(TIFFTAG_FILLORDER, 1, TIFF_SHORT);\n  TIFFSetField(out, TIFFTAG_ORIENTATION, image->orientation);\n  if (outtiled == -1)\n    outtiled = TIFFIsTiled(in);\n  if (outtiled) {\n    if (tilewidth == (uint32) 0)\n      TIFFGetField(in, TIFFTAG_TILEWIDTH, &tilewidth);\n    if (tilelength == (uint32) 0)\n      TIFFGetField(in, TIFFTAG_TILELENGTH, &tilelength);\n    if (tilewidth == 0 || tilelength == 0)\n      TIFFDefaultTileSize(out, &tilewidth, &tilelength);\n    TIFFDefaultTileSize(out, &tilewidth, &tilelength);\n    TIFFSetField(out, TIFFTAG_TILEWIDTH, tilewidth);\n    TIFFSetField(out, TIFFTAG_TILELENGTH, tilelength);\n    } else {\n\tif (rowsperstrip == (uint32) 0)\n          {\n\t  if (!TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip))\n\t    rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);\n          if (compression != COMPRESSION_JPEG)\n            {\n  \t    if (rowsperstrip > length)\n\t      rowsperstrip = length;\n\t    }\n\t  }\n\telse\n          if (rowsperstrip == (uint32) -1)\n\t    rowsperstrip = length;\n\tTIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);\n\t}\n  TIFFGetFieldDefaulted(in, TIFFTAG_PLANARCONFIG, &input_planar);\n  if (config != (uint16) -1)\n    TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);\n  else\n    CopyField(TIFFTAG_PLANARCONFIG, config);\n  if (spp <= 4)\n    CopyTag(TIFFTAG_TRANSFERFUNCTION, 4, TIFF_SHORT);\n  CopyTag(TIFFTAG_COLORMAP, 4, TIFF_SHORT);\n  switch (compression) {\n    case COMPRESSION_JPEG:\n         if (((bps % 8) == 0) || ((bps % 12) == 0))\n\t   {\n           TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);\n\t   TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);\n           }\n         else\n           {\n\t   TIFFError("writeSingleSection",\n                     "JPEG compression requires 8 or 12 bits per sample");\n           return (-1);\n           }\n\t break;\n   case COMPRESSION_LZW:\n   case COMPRESSION_ADOBE_DEFLATE:\n   case COMPRESSION_DEFLATE:\n\tif (predictor != (uint16)-1)\n          TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);\n\telse\n\t  CopyField(TIFFTAG_PREDICTOR, predictor);\n\tbreak;\n   case COMPRESSION_CCITTFAX3:\n   case COMPRESSION_CCITTFAX4:\n\tif (compression == COMPRESSION_CCITTFAX3) {\n          if (g3opts != (uint32) -1)\n\t    TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, g3opts);\n\t  else\n\t    CopyField(TIFFTAG_GROUP3OPTIONS, g3opts);\n\t} else {\n\t    CopyTag(TIFFTAG_GROUP4OPTIONS, 1, TIFF_LONG);\n        }\n        CopyTag(TIFFTAG_BADFAXLINES, 1, TIFF_LONG);\n        CopyTag(TIFFTAG_CLEANFAXDATA, 1, TIFF_LONG);\n        CopyTag(TIFFTAG_CONSECUTIVEBADFAXLINES, 1, TIFF_LONG);\n        CopyTag(TIFFTAG_FAXRECVPARAMS, 1, TIFF_LONG);\n        CopyTag(TIFFTAG_FAXRECVTIME, 1, TIFF_LONG);\n        CopyTag(TIFFTAG_FAXSUBADDRESS, 1, TIFF_ASCII);\n\tbreak;\n   }\n   { uint32 len32;\n     void** data;\n     if (TIFFGetField(in, TIFFTAG_ICCPROFILE, &len32, &data))\n       TIFFSetField(out, TIFFTAG_ICCPROFILE, len32, data);\n   }\n   { uint16 ninks;\n     const char* inknames;\n     if (TIFFGetField(in, TIFFTAG_NUMBEROFINKS, &ninks)) {\n       TIFFSetField(out, TIFFTAG_NUMBEROFINKS, ninks);\n       if (TIFFGetField(in, TIFFTAG_INKNAMES, &inknames)) {\n\t int inknameslen = strlen(inknames) + 1;\n\t const char* cp = inknames;\n\t while (ninks > 1) {\n\t   cp = strchr(cp, \'\\0\');\n\t   if (cp) {\n\t     cp++;\n\t     inknameslen += (strlen(cp) + 1);\n\t   }\n\t   ninks--;\n         }\n\t TIFFSetField(out, TIFFTAG_INKNAMES, inknameslen, inknames);\n       }\n     }\n   }\n   {\n   unsigned short pg0, pg1;\n   if (TIFFGetField(in, TIFFTAG_PAGENUMBER, &pg0, &pg1)) {\n     if (pageNum < 0)\n\tTIFFSetField(out, TIFFTAG_PAGENUMBER, pg0, pg1);\n     else\n\tTIFFSetField(out, TIFFTAG_PAGENUMBER, pageNum++, 0);\n     }\n   }\n  for (p = tags; p < &tags[NTAGS]; p++)\n\t\tCopyTag(p->tag, p->count, p->type);\n  TIFFSetField(out, TIFFTAG_XRESOLUTION, (float)hres);\n  TIFFSetField(out, TIFFTAG_YRESOLUTION, (float)vres);\n  if (outtiled)\n    {\n    if (config == PLANARCONFIG_CONTIG)\n      writeBufferToContigTiles (out, sect_buff, length, width, spp, dump);\n    else\n      writeBufferToSeparateTiles (out, sect_buff, length, width, spp, dump);\n    }\n  else\n    {\n    if (config == PLANARCONFIG_CONTIG)\n      writeBufferToContigStrips (out, sect_buff, length);\n    else\n      writeBufferToSeparateStrips(out, sect_buff, length, width, spp, dump);\n    }\n  if (!TIFFWriteDirectory(out))\n    {\n    TIFFClose(out);\n    return (-1);\n    }\n  return (0);\n  }', 'static int writeBufferToContigTiles (TIFF* out, uint8* buf, uint32 imagelength,\n\t\t\t\t       uint32 imagewidth, tsample_t spp,\n                                       struct dump_opts* dump)\n  {\n  uint16 bps;\n  uint32 tl, tw;\n  uint32 row, col, nrow, ncol;\n  uint32 src_rowsize, col_offset;\n  uint32 tile_rowsize  = TIFFTileRowSize(out);\n  uint8* bufp = (uint8*) buf;\n  tsize_t tile_buffsize = 0;\n  tsize_t tilesize = TIFFTileSize(out);\n  unsigned char *tilebuf = NULL;\n  if( !TIFFGetField(out, TIFFTAG_TILELENGTH, &tl) ||\n      !TIFFGetField(out, TIFFTAG_TILEWIDTH, &tw) ||\n      !TIFFGetField(out, TIFFTAG_BITSPERSAMPLE, &bps) )\n      return 1;\n  tile_buffsize = tilesize;\n  if (tilesize < (tsize_t)(tl * tile_rowsize))\n    {\n#ifdef DEBUG2\n    TIFFError("writeBufferToContigTiles",\n\t      "Tilesize %lu is too small, using alternate calculation %u",\n              tilesize, tl * tile_rowsize);\n#endif\n    tile_buffsize = tl * tile_rowsize;\n    }\n  tilebuf = _TIFFmalloc(tile_buffsize);\n  if (tilebuf == 0)\n    return 1;\n  src_rowsize = ((imagewidth * spp * bps) + 7) / 8;\n  for (row = 0; row < imagelength; row += tl)\n    {\n    nrow = (row + tl > imagelength) ? imagelength - row : tl;\n    for (col = 0; col < imagewidth; col += tw)\n      {\n      if (col + tw > imagewidth)\n\tncol = imagewidth - col;\n      else\n        ncol = tw;\n      col_offset = (((col * bps * spp) + 7) / 8);\n      bufp = buf + (row * src_rowsize) + col_offset;\n      if (extractContigSamplesToTileBuffer(tilebuf, bufp, nrow, ncol, imagewidth,\n\t\t\t\t\t   tw, 0, spp, spp, bps, dump) > 0)\n        {\n\tTIFFError("writeBufferToContigTiles",\n                  "Unable to extract data to tile for row %lu, col %lu",\n                  (unsigned long) row, (unsigned long)col);\n\t_TIFFfree(tilebuf);\n\treturn 1;\n        }\n      if (TIFFWriteTile(out, tilebuf, col, row, 0, 0) < 0)\n        {\n\tTIFFError("writeBufferToContigTiles",\n\t          "Cannot write tile at %lu %lu",\n\t          (unsigned long) col, (unsigned long) row);\n\t _TIFFfree(tilebuf);\n\treturn 1;\n\t}\n      }\n    }\n  _TIFFfree(tilebuf);\n  return 0;\n  }', 'tmsize_t\nTIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)\n{\n\tif (!TIFFCheckTile(tif, x, y, z, s))\n\t\treturn ((tmsize_t)(-1));\n\treturn (TIFFWriteEncodedTile(tif,\n\t    TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));\n}', 'int\nTIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tif (x >= td->td_imagewidth) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t     "%lu: Col out of range, max %lu",\n\t\t\t     (unsigned long) x,\n\t\t\t     (unsigned long) (td->td_imagewidth - 1));\n\t\treturn (0);\n\t}\n\tif (y >= td->td_imagelength) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t     "%lu: Row out of range, max %lu",\n\t\t\t     (unsigned long) y,\n\t\t\t     (unsigned long) (td->td_imagelength - 1));\n\t\treturn (0);\n\t}\n\tif (z >= td->td_imagedepth) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t     "%lu: Depth out of range, max %lu",\n\t\t\t     (unsigned long) z,\n\t\t\t     (unsigned long) (td->td_imagedepth - 1));\n\t\treturn (0);\n\t}\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE &&\n\t    s >= td->td_samplesperpixel) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t     "%lu: Sample out of range, max %lu",\n\t\t\t     (unsigned long) s,\n\t\t\t     (unsigned long) (td->td_samplesperpixel - 1));\n\t\treturn (0);\n\t}\n\treturn (1);\n}', 'uint32\nTIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 dx = td->td_tilewidth;\n\tuint32 dy = td->td_tilelength;\n\tuint32 dz = td->td_tiledepth;\n\tuint32 tile = 1;\n\tif (td->td_imagedepth == 1)\n\t\tz = 0;\n\tif (dx == (uint32) -1)\n\t\tdx = td->td_imagewidth;\n\tif (dy == (uint32) -1)\n\t\tdy = td->td_imagelength;\n\tif (dz == (uint32) -1)\n\t\tdz = td->td_imagedepth;\n\tif (dx != 0 && dy != 0 && dz != 0) {\n\t\tuint32 xpt = TIFFhowmany_32(td->td_imagewidth, dx);\n\t\tuint32 ypt = TIFFhowmany_32(td->td_imagelength, dy);\n\t\tuint32 zpt = TIFFhowmany_32(td->td_imagedepth, dz);\n\t\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\t\ttile = (xpt*ypt*zpt)*s +\n\t\t\t     (xpt*ypt)*(z/dz) +\n\t\t\t     xpt*(y/dy) +\n\t\t\t     x/dx;\n\t\telse\n\t\t\ttile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx;\n\t}\n\treturn (tile);\n}', 'tmsize_t\nTIFFWriteEncodedTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)\n{\n\tstatic const char module[] = "TIFFWriteEncodedTile";\n\tTIFFDirectory *td;\n\tuint16 sample;\n        uint32 howmany32;\n\tif (!WRITECHECKTILES(tif, module))\n\t\treturn ((tmsize_t)(-1));\n\ttd = &tif->tif_dir;\n\tif (tile >= td->td_nstrips) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",\n\t\t    (unsigned long) tile, (unsigned long) td->td_nstrips);\n\t\treturn ((tmsize_t)(-1));\n\t}\n\tif (!BUFFERCHECK(tif))\n\t\treturn ((tmsize_t)(-1));\n        tif->tif_flags |= TIFF_BUF4WRITE;\n\ttif->tif_curtile = tile;\n\tif( td->td_stripbytecount[tile] > 0 )\n        {\n            if( tif->tif_rawdatasize <= (tmsize_t) td->td_stripbytecount[tile] )\n            {\n                if( !(TIFFWriteBufferSetup(tif, NULL,\n                    (tmsize_t)TIFFroundup_64((uint64)(td->td_stripbytecount[tile] + 1), 1024))) )\n                    return ((tmsize_t)(-1));\n            }\n            tif->tif_curoff = 0;\n        }\n\ttif->tif_rawcc = 0;\n\ttif->tif_rawcp = tif->tif_rawdata;\n        howmany32=TIFFhowmany_32(td->td_imagelength, td->td_tilelength);\n        if (howmany32 == 0) {\n                 TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");\n                return ((tmsize_t)(-1));\n        }\n\ttif->tif_row = (tile % howmany32) * td->td_tilelength;\n        howmany32=TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);\n        if (howmany32 == 0) {\n                 TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");\n                return ((tmsize_t)(-1));\n        }\n\ttif->tif_col = (tile % howmany32) * td->td_tilewidth;\n\tif ((tif->tif_flags & TIFF_CODERSETUP) == 0) {\n\t\tif (!(*tif->tif_setupencode)(tif))\n\t\t\treturn ((tmsize_t)(-1));\n\t\ttif->tif_flags |= TIFF_CODERSETUP;\n\t}\n\ttif->tif_flags &= ~TIFF_POSTENCODE;\n\tsample = (uint16)(tile/td->td_stripsperimage);\n\tif (!(*tif->tif_preencode)(tif, sample))\n\t\treturn ((tmsize_t)(-1));\n\tif ( cc < 1 || cc > tif->tif_tilesize)\n\t\tcc = tif->tif_tilesize;\n\ttif->tif_postdecode( tif, (uint8*) data, cc );\n\tif (!(*tif->tif_encodetile)(tif, (uint8*) data, cc, sample))\n\t\treturn (0);\n\tif (!(*tif->tif_postencode)(tif))\n\t\treturn ((tmsize_t)(-1));\n\tif (!isFillOrder(tif, td->td_fillorder) &&\n\t    (tif->tif_flags & TIFF_NOBITREV) == 0)\n\t\tTIFFReverseBits((uint8*)tif->tif_rawdata, tif->tif_rawcc);\n\tif (tif->tif_rawcc > 0 && !TIFFAppendToStrip(tif, tile,\n\t    tif->tif_rawdata, tif->tif_rawcc))\n\t\treturn ((tmsize_t)(-1));\n\ttif->tif_rawcc = 0;\n\ttif->tif_rawcp = tif->tif_rawdata;\n\treturn (cc);\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 stripped 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 == 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}', '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\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\ttd->td_stripsperimage /= td->td_samplesperpixel;\n\ttd->td_stripoffset = (uint64 *)\n\t    _TIFFmalloc(td->td_nstrips * sizeof (uint64));\n\ttd->td_stripbytecount = (uint64 *)\n\t    _TIFFmalloc(td->td_nstrips * sizeof (uint64));\n\tif (td->td_stripoffset == NULL || td->td_stripbytecount == NULL)\n\t\treturn (0);\n\t_TIFFmemset(td->td_stripoffset, 0, td->td_nstrips*sizeof (uint64));\n\t_TIFFmemset(td->td_stripbytecount, 0, td->td_nstrips*sizeof (uint64));\n\tTIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);\n\tTIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);\n\treturn (1);\n}', 'uint32\nTIFFNumberOfTiles(TIFF* tif)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 dx = td->td_tilewidth;\n\tuint32 dy = td->td_tilelength;\n\tuint32 dz = td->td_tiledepth;\n\tuint32 ntiles;\n\tif (dx == (uint32) -1)\n\t\tdx = td->td_imagewidth;\n\tif (dy == (uint32) -1)\n\t\tdy = td->td_imagelength;\n\tif (dz == (uint32) -1)\n\t\tdz = td->td_imagedepth;\n\tntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :\n\t    _TIFFMultiply32(tif, _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),\n\t    TIFFhowmany_32(td->td_imagelength, dy),\n\t    "TIFFNumberOfTiles"),\n\t    TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\tntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,\n\t\t    "TIFFNumberOfTiles");\n\treturn (ntiles);\n}']