tools/tiffcrop.c:5915: error: Buffer Overrun L3
  Offset: [1, +oo] (⇐ [0, +oo] + 1) Size: [0, +oo] by call to `writeSingleSection`.

tools/tiffcrop.c:5863:1: Parameter `out->tif_dir.td_samplesperpixel`
5861.   } /* end writeRegions */
5862. 
5863. static int
      ^
5864. writeImageSections(TIFF *in, TIFF *out, struct image_data *image,
5865. 		   struct pagedef *page, struct pageseg *sections,

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

tools/tiffcrop.c:5928:1: Parameter `out->tif_dir.td_samplesperpixel`
  5926.  * with modifications by Richard Nolde to handle orientation correctly.
  5927.  */
  5928. static int  
        ^
  5929. writeSingleSection(TIFF *in, TIFF *out, struct image_data *image,
  5930.                    struct dump_opts *dump, uint32 width, uint32 length,

tools/tiffcrop.c:6129:7: Call
  6127.     if (config == PLANARCONFIG_CONTIG)
  6128.       {
  6129.       writeBufferToContigStrips (out, sect_buff, length, width, spp);
              ^
  6130.       }
  6131.     else

tools/tiffcrop.c:850:1: Parameter `out->tif_dir.td_samplesperpixel`
    848. }
    849. 
    850. static int writeBufferToContigStrips(TIFF* out, uint8* buf, uint32 imagelength, 
         ^
    851.                                      uint32 imagewidth, tsample_t spp)
    852. {

tools/tiffcrop.c:857:9: Call
    855. 
    856. 	(void) imagewidth; (void) spp;
    857. 	(void) TIFFGetFieldDefaulted(out, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
                 ^
    858. 	for (row = 0; row < imagelength; row += rowsperstrip) {
    859. 		uint32 nrows = (row+rowsperstrip > imagelength) ?

libtiff/tif_aux.c:263:1: Parameter `tif->tif_dir.td_samplesperpixel`
      261.  * value if the tag is not present in the directory.
      262.  */
      263. int
           ^
      264. TIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...)
      265. {

libtiff/tif_aux.c:270:8: Call
      268. 
      269. 	va_start(ap, tag);
      270. 	ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
                  ^
      271. 	va_end(ap);
      272. 	return (ok);

libtiff/tif_aux.c:111:1: Parameter `tif->tif_dir.td_extrasamples`
        109.  *	place in the library -- in TIFFDefaultDirectory.
        110.  */
        111. int
             ^
        112. TIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap)
        113. {

tools/tiffcrop.c:6137:8: Call
  6135.     }
  6136. 
  6137.   if (!TIFFWriteDirectory(out))
               ^
  6138.     {
  6139.     TIFFClose(out);

libtiff/tif_dirwrite.c:163:1: Parameter `tif->tif_dir.td_samplesperpixel`
    161.  * storage that's been changed.
    162.  */
    163. int
         ^
    164. TIFFWriteDirectory(TIFF* tif)
    165. {

libtiff/tif_dirwrite.c:166:9: Call
    164. TIFFWriteDirectory(TIFF* tif)
    165. {
    166. 	return TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);
                 ^
    167. }
    168. 

libtiff/tif_dirwrite.c:345:1: Parameter `tif->tif_dir.td_samplesperpixel`
      343. }
      344. 
      345. static int
           ^
      346. TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)
      347. {

libtiff/tif_dirwrite.c:443:10: Call
      441. 			if (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
      442. 			{
      443. 				if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))
                    ^
      444. 					goto bad;
      445. 			}

libtiff/tif_dirwrite.c:1070:1: Parameter `tif->tif_dir.td_samplesperpixel`
        1068. }
        1069. 
        1070. static int
              ^
        1071. TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
        1072. {

libtiff/tif_dirwrite.c:1083:4: Call
        1081. 		return(1);
        1082. 	}
        1083. 	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint16));
                 ^
        1084. 	if (m==NULL)
        1085. 	{

libtiff/tif_unix.c:253:1: Parameter `s`
          251. #endif
          252. 
          253. void*
               ^
          254. _TIFFmalloc(tmsize_t s)
          255. {

libtiff/tif_unix.c:256:10: Array declaration
          254. _TIFFmalloc(tmsize_t s)
          255. {
          256. 	return (malloc((size_t) s));
                        ^
          257. }
          258. 

libtiff/tif_unix.c:256:2: Assignment
          254. _TIFFmalloc(tmsize_t s)
          255. {
          256. 	return (malloc((size_t) s));
                ^
          257. }
          258. 

libtiff/tif_dirwrite.c:1083:2: Assignment
        1081. 		return(1);
        1082. 	}
        1083. 	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint16));
               ^
        1084. 	if (m==NULL)
        1085. 	{

libtiff/tif_dirwrite.c:1091:4: Call
        1089. 	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
        1090. 		*na=value;
        1091. 	o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
                 ^
        1092. 	_TIFFfree(m);
        1093. 	return(o);

libtiff/tif_dirwrite.c:1830:1: Parameter `*value`
          1828. }
          1829. 
          1830. static int
                ^
          1831. TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value)
          1832. {

libtiff/tif_dirwrite.c:1836:3: Call
          1834. 	assert(sizeof(uint16)==2);
          1835. 	if (tif->tif_flags&TIFF_SWAB)
          1836. 		TIFFSwabArrayOfShort(value,count);
                  ^
          1837. 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,count,count*2,value));
          1838. }

libtiff/tif_swab.c:72:1: <Length trace>
70. 
71. #ifndef TIFFSwabArrayOfShort
72. void
    ^
73. TIFFSwabArrayOfShort(register uint16* wp, tmsize_t n)
74. {

libtiff/tif_swab.c:72:1: Parameter `*wp`
            70. 
            71. #ifndef TIFFSwabArrayOfShort
            72. void
                ^
            73. TIFFSwabArrayOfShort(register uint16* wp, tmsize_t n)
            74. {

libtiff/tif_swab.c:80:3: Assignment
            78. 	/* XXX unroll loop some */
            79. 	while (n-- > 0) {
            80. 		cp = (unsigned char*) wp;
                  ^
            81. 		t = cp[1]; cp[1] = cp[0]; cp[0] = t;
            82. 		wp++;

libtiff/tif_swab.c:81:7: Array access: Offset: [1, +oo] (⇐ [0, +oo] + 1) Size: [0, +oo] by call to `writeSingleSection` 
            79. 	while (n-- > 0) {
            80. 		cp = (unsigned char*) wp;
            81. 		t = cp[1]; cp[1] = cp[0]; cp[0] = t;
                      ^
            82. 		wp++;
            83. 	}

void
TIFFSwabArrayOfShort(register uint16* wp, tmsize_t n)
{
	register unsigned char* cp;
	register unsigned char t;
	assert(sizeof(uint16)==2);
	while (n-- > 0) {
		cp = (unsigned char*) wp;
		t = cp[1]; cp[1] = cp[0]; cp[0] = t;
		wp++;
	}
}
['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#ifdef DEBUG\n  TIFFError("",\n    "Writing %d sections for each original page. Hres: %3.2f Vres: %3.2f\\n",\n          page->rows * page->cols, hres, vres);\n#endif\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#ifdef DEBUG\n    TIFFError ("", "\\nSection: %d, Width: %4d, Length: %4d, x1: %4d  x2: %4d  y1: %4d  y2: %4d\\n",\n             i + 1, width, length, sections[i].x1, sections[i].x2, sections[i].y1, sections[i].y2);\n#endif\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  struct cpTag* p;\n#ifdef DEBUG\n  TIFFError ("",\n"\\nWriting single section: Width %d Length: %d Hres: %4.1f, Vres: %4.1f\\n\\n",\n\t   width, length, hres, vres);\n#endif\n  TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);\n  TIFFSetField(out, TIFFTAG_IMAGELENGTH, length);\n  CopyField(TIFFTAG_BITSPERSAMPLE, bps);\n  CopyField(TIFFTAG_SAMPLESPERPIXEL, spp);\n  if (compression != (uint16)-1)\n    TIFFSetField(out, TIFFTAG_COMPRESSION, compression);\n  else\n    CopyField(TIFFTAG_COMPRESSION, compression);\n  if (compression == COMPRESSION_JPEG) {\n    uint16 input_compression, input_photometric;\n    if (TIFFGetField(in, TIFFTAG_COMPRESSION, &input_compression)\n        && input_compression == COMPRESSION_JPEG) {\n          TIFFSetField(in, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);\n        }\n    if (TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &input_photometric)) {\n\tif(input_photometric == PHOTOMETRIC_RGB) {\n\t   if (jpegcolormode == JPEGCOLORMODE_RGB)\n\t     TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);\n\t   else\n\t     TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);\n\t   } else\n\t      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, input_photometric);\n\t}\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  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) -1)\n      TIFFGetField(in, TIFFTAG_TILEWIDTH, &tilewidth);\n    if (tilelength == (uint32) -1)\n      TIFFGetField(in, TIFFTAG_TILELENGTH, &tilelength);\n    if (tilewidth > width)\n      tilewidth = width;\n    if (tilelength > length)\n      tilelength = length;\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\t  if (!TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip)) {\n\t      rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);\n\t     }\n\t  if (rowsperstrip > length && rowsperstrip != (uint32)-1)\n\t       rowsperstrip = length;\n\t  }\n\telse if (rowsperstrip == (uint32) -1)\n\t\trowsperstrip = length;\n\t\tTIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);\n\t}\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         TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);\n\t TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);\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\t    CopyTag(TIFFTAG_BADFAXLINES, 1, TIFF_LONG);\n\t    CopyTag(TIFFTAG_CLEANFAXDATA, 1, TIFF_LONG);\n\t    CopyTag(TIFFTAG_CONSECUTIVEBADFAXLINES, 1, TIFF_LONG);\n\t    CopyTag(TIFFTAG_FAXRECVPARAMS, 1, TIFF_LONG);\n\t    CopyTag(TIFFTAG_FAXRECVTIME, 1, TIFF_LONG);\n\t    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      {\n      writeBufferToContigTiles (out, sect_buff, length, width, spp);\n      }\n    else\n      writeBufferToSeparateTiles (out, sect_buff, length, width, spp, dump);\n    }\n  else\n    {\n    if (config == PLANARCONFIG_CONTIG)\n      {\n      writeBufferToContigStrips (out, sect_buff, length, width, spp);\n      }\n    else\n      {\n      writeBufferToSeparateStrips(out, sect_buff, length, width, spp, dump);\n      }\n    }\n  if (!TIFFWriteDirectory(out))\n    {\n    TIFFClose(out);\n    return (-1);\n    }\n  return (0);\n  }', 'static int writeBufferToContigStrips(TIFF* out, uint8* buf, uint32 imagelength,\n                                     uint32 imagewidth, tsample_t spp)\n{\n\tuint32 row, rowsperstrip;\n\ttstrip_t strip = 0;\n\t(void) imagewidth; (void) spp;\n\t(void) TIFFGetFieldDefaulted(out, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);\n\tfor (row = 0; row < imagelength; row += rowsperstrip) {\n\t\tuint32 nrows = (row+rowsperstrip > imagelength) ?\n\t\t    imagelength-row : rowsperstrip;\n\t\ttsize_t stripsize = TIFFVStripSize(out, nrows);\n\t\tif (TIFFWriteEncodedStrip(out, strip++, buf, stripsize) < 0) {\n\t\t\tTIFFError(TIFFFileName(out),\n\t\t\t\t  "Error, can\'t write strip %u", strip - 1);\n\t\t\treturn 0;\n\t\t}\n\t\tbuf += stripsize;\n\t}\n\treturn 1;\n}', 'int\nTIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...)\n{\n\tint ok;\n\tva_list ap;\n\tva_start(ap, tag);\n\tok =  TIFFVGetFieldDefaulted(tif, tag, ap);\n\tva_end(ap);\n\treturn (ok);\n}', 'int\nTIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tif (TIFFVGetField(tif, tag, ap))\n\t\treturn (1);\n\tswitch (tag) {\n\tcase TIFFTAG_SUBFILETYPE:\n\t\t*va_arg(ap, uint32 *) = td->td_subfiletype;\n\t\treturn (1);\n\tcase TIFFTAG_BITSPERSAMPLE:\n\t\t*va_arg(ap, uint16 *) = td->td_bitspersample;\n\t\treturn (1);\n\tcase TIFFTAG_THRESHHOLDING:\n\t\t*va_arg(ap, uint16 *) = td->td_threshholding;\n\t\treturn (1);\n\tcase TIFFTAG_FILLORDER:\n\t\t*va_arg(ap, uint16 *) = td->td_fillorder;\n\t\treturn (1);\n\tcase TIFFTAG_ORIENTATION:\n\t\t*va_arg(ap, uint16 *) = td->td_orientation;\n\t\treturn (1);\n\tcase TIFFTAG_SAMPLESPERPIXEL:\n\t\t*va_arg(ap, uint16 *) = td->td_samplesperpixel;\n\t\treturn (1);\n\tcase TIFFTAG_ROWSPERSTRIP:\n\t\t*va_arg(ap, uint32 *) = td->td_rowsperstrip;\n\t\treturn (1);\n\tcase TIFFTAG_MINSAMPLEVALUE:\n\t\t*va_arg(ap, uint16 *) = td->td_minsamplevalue;\n\t\treturn (1);\n\tcase TIFFTAG_MAXSAMPLEVALUE:\n\t\t*va_arg(ap, uint16 *) = td->td_maxsamplevalue;\n\t\treturn (1);\n\tcase TIFFTAG_PLANARCONFIG:\n\t\t*va_arg(ap, uint16 *) = td->td_planarconfig;\n\t\treturn (1);\n\tcase TIFFTAG_RESOLUTIONUNIT:\n\t\t*va_arg(ap, uint16 *) = td->td_resolutionunit;\n\t\treturn (1);\n\tcase TIFFTAG_PREDICTOR:\n                {\n\t\t\tTIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;\n\t\t\t*va_arg(ap, uint16*) = (uint16) sp->predictor;\n\t\t\treturn 1;\n                }\n\tcase TIFFTAG_DOTRANGE:\n\t\t*va_arg(ap, uint16 *) = 0;\n\t\t*va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;\n\t\treturn (1);\n\tcase TIFFTAG_INKSET:\n\t\t*va_arg(ap, uint16 *) = INKSET_CMYK;\n\t\treturn 1;\n\tcase TIFFTAG_NUMBEROFINKS:\n\t\t*va_arg(ap, uint16 *) = 4;\n\t\treturn (1);\n\tcase TIFFTAG_EXTRASAMPLES:\n\t\t*va_arg(ap, uint16 *) = td->td_extrasamples;\n\t\t*va_arg(ap, uint16 **) = td->td_sampleinfo;\n\t\treturn (1);\n\tcase TIFFTAG_MATTEING:\n\t\t*va_arg(ap, uint16 *) =\n\t\t    (td->td_extrasamples == 1 &&\n\t\t     td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);\n\t\treturn (1);\n\tcase TIFFTAG_TILEDEPTH:\n\t\t*va_arg(ap, uint32 *) = td->td_tiledepth;\n\t\treturn (1);\n\tcase TIFFTAG_DATATYPE:\n\t\t*va_arg(ap, uint16 *) = td->td_sampleformat-1;\n\t\treturn (1);\n\tcase TIFFTAG_SAMPLEFORMAT:\n\t\t*va_arg(ap, uint16 *) = td->td_sampleformat;\n                return(1);\n\tcase TIFFTAG_IMAGEDEPTH:\n\t\t*va_arg(ap, uint32 *) = td->td_imagedepth;\n\t\treturn (1);\n\tcase TIFFTAG_YCBCRCOEFFICIENTS:\n\t\t{\n\t\t\tstatic float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };\n\t\t\t*va_arg(ap, float **) = ycbcrcoeffs;\n\t\t\treturn 1;\n\t\t}\n\tcase TIFFTAG_YCBCRSUBSAMPLING:\n\t\t*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];\n\t\t*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];\n\t\treturn (1);\n\tcase TIFFTAG_YCBCRPOSITIONING:\n\t\t*va_arg(ap, uint16 *) = td->td_ycbcrpositioning;\n\t\treturn (1);\n\tcase TIFFTAG_WHITEPOINT:\n\t\t{\n\t\t\tstatic float whitepoint[2];\n\t\t\twhitepoint[0] =\tD50_X0 / (D50_X0 + D50_Y0 + D50_Z0);\n\t\t\twhitepoint[1] =\tD50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);\n\t\t\t*va_arg(ap, float **) = whitepoint;\n\t\t\treturn 1;\n\t\t}\n\tcase TIFFTAG_TRANSFERFUNCTION:\n\t\tif (!td->td_transferfunction[0] &&\n\t\t    !TIFFDefaultTransferFunction(td)) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \\"TransferFunction\\" tag");\n\t\t\treturn (0);\n\t\t}\n\t\t*va_arg(ap, uint16 **) = td->td_transferfunction[0];\n\t\tif (td->td_samplesperpixel - td->td_extrasamples > 1) {\n\t\t\t*va_arg(ap, uint16 **) = td->td_transferfunction[1];\n\t\t\t*va_arg(ap, uint16 **) = td->td_transferfunction[2];\n\t\t}\n\t\treturn (1);\n\tcase TIFFTAG_REFERENCEBLACKWHITE:\n\t\t{\n\t\t\tint i;\n\t\t\tstatic float ycbcr_refblackwhite[] =\n\t\t\t{ 0.0F, 255.0F, 128.0F, 255.0F, 128.0F, 255.0F };\n\t\t\tstatic float rgb_refblackwhite[6];\n\t\t\tfor (i = 0; i < 3; i++) {\n\t\t\t\trgb_refblackwhite[2 * i + 0] = 0.0F;\n\t\t\t\trgb_refblackwhite[2 * i + 1] =\n\t\t\t\t\t(float)((1L<<td->td_bitspersample)-1L);\n\t\t\t}\n\t\t\tif (td->td_photometric == PHOTOMETRIC_YCBCR) {\n\t\t\t\t*va_arg(ap, float **) = ycbcr_refblackwhite;\n\t\t\t} else {\n\t\t\t\t*va_arg(ap, float **) = rgb_refblackwhite;\n\t\t\t}\n\t\t\treturn 1;\n\t\t}\n\t}\n\treturn 0;\n}', 'int\nTIFFWriteDirectory(TIFF* tif)\n{\n\treturn TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);\n}', 'static int\nTIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)\n{\n\tstatic const char module[] = "TIFFWriteDirectorySec";\n\tuint32 ndir;\n\tTIFFDirEntry* dir;\n\tuint32 dirsize;\n\tvoid* dirmem;\n\tuint32 m;\n\tif (tif->tif_mode == O_RDONLY)\n\t\treturn (1);\n\tif (imagedone)\n\t{\n\t\tif (tif->tif_flags & TIFF_POSTENCODE)\n\t\t{\n\t\t\ttif->tif_flags &= ~TIFF_POSTENCODE;\n\t\t\tif (!(*tif->tif_postencode)(tif))\n\t\t\t{\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t\t\t    "Error post-encoding before directory write");\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\t(*tif->tif_close)(tif);\n\t\tif (tif->tif_rawcc > 0\n\t\t    && (tif->tif_flags & TIFF_BEENWRITING) != 0 )\n\t\t{\n\t\t    if( !TIFFFlushData1(tif) )\n                    {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t    "Error flushing data before directory write");\n\t\t\treturn (0);\n                    }\n\t\t}\n\t\tif ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)\n\t\t{\n\t\t\t_TIFFfree(tif->tif_rawdata);\n\t\t\ttif->tif_rawdata = NULL;\n\t\t\ttif->tif_rawcc = 0;\n\t\t\ttif->tif_rawdatasize = 0;\n\t\t}\n\t\ttif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);\n\t}\n\tdir=NULL;\n\tdirmem=NULL;\n\tdirsize=0;\n\twhile (1)\n\t{\n\t\tndir=0;\n\t\tif (isimage)\n\t\t{\n\t\t\tif (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGEWIDTH,tif->tif_dir.td_imagewidth))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGELENGTH,tif->tif_dir.td_imagelength))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILEWIDTH,tif->tif_dir.td_tilewidth))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILELENGTH,tif->tif_dir.td_tilelength))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_RESOLUTION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XRESOLUTION,tif->tif_dir.td_xresolution))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YRESOLUTION,tif->tif_dir.td_yresolution))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_POSITION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XPOSITION,tif->tif_dir.td_xposition))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YPOSITION,tif->tif_dir.td_yposition))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SUBFILETYPE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_SUBFILETYPE,tif->tif_dir.td_subfiletype))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_COMPRESSION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_COMPRESSION,tif->tif_dir.td_compression))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PHOTOMETRIC))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PHOTOMETRIC,tif->tif_dir.td_photometric))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_THRESHHOLDING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_THRESHHOLDING,tif->tif_dir.td_threshholding))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_FILLORDER))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_FILLORDER,tif->tif_dir.td_fillorder))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_ORIENTATION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_ORIENTATION,tif->tif_dir.td_orientation))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_SAMPLESPERPIXEL,tif->tif_dir.td_samplesperpixel))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_ROWSPERSTRIP,tif->tif_dir.td_rowsperstrip))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MINSAMPLEVALUE,tif->tif_dir.td_minsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MAXSAMPLEVALUE,tif->tif_dir.td_maxsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PLANARCONFIG))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PLANARCONFIG,tif->tif_dir.td_planarconfig))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_RESOLUTIONUNIT,tif->tif_dir.td_resolutionunit))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PAGENUMBER))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_PAGENUMBER,2,&tif->tif_dir.td_pagenumber[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_STRIPBYTECOUNTS))\n\t\t\t{\n\t\t\t\tif (!isTiled(tif))\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_STRIPOFFSETS))\n\t\t\t{\n\t\t\t\tif (!isTiled(tif))\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_COLORMAP))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagColormap(tif,&ndir,dir))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_EXTRASAMPLES))\n\t\t\t{\n\t\t\t\tif (tif->tif_dir.td_extrasamples)\n\t\t\t\t{\n\t\t\t\t\tuint16 na;\n\t\t\t\t\tuint16* nb;\n\t\t\t\t\tTIFFGetFieldDefaulted(tif,TIFFTAG_EXTRASAMPLES,&na,&nb);\n\t\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_EXTRASAMPLES,na,nb))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_SAMPLEFORMAT,tif->tif_dir.td_sampleformat))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagSampleformatPerSample(tif,&ndir,dir,TIFFTAG_SMINSAMPLEVALUE,tif->tif_dir.td_sminsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagSampleformatPerSample(tif,&ndir,dir,TIFFTAG_SMAXSAMPLEVALUE,tif->tif_dir.td_smaxsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_IMAGEDEPTH,tif->tif_dir.td_imagedepth))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TILEDEPTH))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_TILEDEPTH,tif->tif_dir.td_tiledepth))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_HALFTONEHINTS,2,&tif->tif_dir.td_halftonehints[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_YCBCRSUBSAMPLING,2,&tif->tif_dir.td_ycbcrsubsampling[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_YCBCRPOSITIONING,tif->tif_dir.td_ycbcrpositioning))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagTransferfunction(tif,&ndir,dir))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_INKNAMES))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,TIFFTAG_INKNAMES,tif->tif_dir.td_inknameslen,tif->tif_dir.td_inknames))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SUBIFD))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagSubifd(tif,&ndir,dir))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\t{\n\t\t\t\tuint32 n;\n\t\t\t\tfor (n=0; n<tif->tif_nfields; n++) {\n\t\t\t\t\tconst TIFFField* o;\n\t\t\t\t\to = tif->tif_fields[n];\n\t\t\t\t\tif ((o->field_bit>=FIELD_CODEC)&&(TIFFFieldSet(tif,o->field_bit)))\n\t\t\t\t\t{\n\t\t\t\t\t\tswitch (o->get_field_type)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcase TIFF_SETGET_ASCII:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint32 pa;\n\t\t\t\t\t\t\t\t\tchar* pb;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_ASCII);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==TIFF_VARIABLE);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==0);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&pb);\n\t\t\t\t\t\t\t\t\tpa=(uint32)(strlen(pb));\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,o->field_tag,pa,pb))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tcase TIFF_SETGET_UINT16:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint16 p;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_SHORT);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==1);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==0);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&p);\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,o->field_tag,p))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tcase TIFF_SETGET_UINT32:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint32 p;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_LONG);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==1);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==0);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&p);\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,o->field_tag,p))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tcase TIFF_SETGET_C32_UINT8:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint32 pa;\n\t\t\t\t\t\t\t\t\tvoid* pb;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_UNDEFINED);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==TIFF_VARIABLE2);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==1);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&pa,&pb);\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,o->field_tag,pa,pb))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\t\tassert(0);\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (m=0; m<(uint32)(tif->tif_dir.td_customValueCount); m++)\n\t\t{\n\t\t\tswitch (tif->tif_dir.td_customValues[m].info->field_type)\n\t\t\t{\n\t\t\t\tcase TIFF_ASCII:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_UNDEFINED:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_BYTE:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagByteArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SBYTE:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSbyteArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SHORT:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SSHORT:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSshortArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_LONG:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SLONG:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSlongArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_LONG8:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLong8Array(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SLONG8:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSlong8Array(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_RATIONAL:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SRATIONAL:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSrationalArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_FLOAT:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagFloatArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_DOUBLE:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagDoubleArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_IFD:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagIfdArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_IFD8:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagIfdIfd8Array(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\tassert(0);\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (dir!=NULL)\n\t\t\tbreak;\n\t\tdir=_TIFFmalloc(ndir*sizeof(TIFFDirEntry));\n\t\tif (dir==NULL)\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n\t\t\tgoto bad;\n\t\t}\n\t\tif (isimage)\n\t\t{\n\t\t\tif ((tif->tif_diroff==0)&&(!TIFFLinkDirectory(tif)))\n\t\t\t\tgoto bad;\n\t\t}\n\t\telse\n\t\t\ttif->tif_diroff=(TIFFSeekFile(tif,0,SEEK_END)+1)&(~1);\n\t\tif (pdiroff!=NULL)\n\t\t\t*pdiroff=tif->tif_diroff;\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\tdirsize=2+ndir*12+4;\n\t\telse\n\t\t\tdirsize=8+ndir*20+8;\n\t\ttif->tif_dataoff=tif->tif_diroff+dirsize;\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\ttif->tif_dataoff=(uint32)tif->tif_dataoff;\n\t\tif ((tif->tif_dataoff<tif->tif_diroff)||(tif->tif_dataoff<(uint64)dirsize))\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");\n\t\t\tgoto bad;\n\t\t}\n\t\tif (tif->tif_dataoff&1)\n\t\t\ttif->tif_dataoff++;\n\t\tif (isimage)\n\t\t\ttif->tif_curdir++;\n\t}\n\tif (isimage)\n\t{\n\t\tif (TIFFFieldSet(tif,FIELD_SUBIFD)&&(tif->tif_subifdoff==0))\n\t\t{\n\t\t\tuint32 na;\n\t\t\tTIFFDirEntry* nb;\n\t\t\tfor (na=0, nb=dir; ; na++, nb++)\n\t\t\t{\n\t\t\t\tassert(na<ndir);\n\t\t\t\tif (nb->tdir_tag==TIFFTAG_SUBIFD)\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\t\ttif->tif_subifdoff=tif->tif_diroff+2+na*12+8;\n\t\t\telse\n\t\t\t\ttif->tif_subifdoff=tif->tif_diroff+8+na*20+12;\n\t\t}\n\t}\n\tdirmem=_TIFFmalloc(dirsize);\n\tif (dirmem==NULL)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n\t\tgoto bad;\n\t}\n\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t{\n\t\tuint8* n;\n\t\tuint32 nTmp;\n\t\tTIFFDirEntry* o;\n\t\tn=dirmem;\n\t\t*(uint16*)n=ndir;\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabShort((uint16*)n);\n\t\tn+=2;\n\t\to=dir;\n\t\tfor (m=0; m<ndir; m++)\n\t\t{\n\t\t\t*(uint16*)n=o->tdir_tag;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\t*(uint16*)n=o->tdir_type;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\tnTmp = (uint32)o->tdir_count;\n\t\t\t_TIFFmemcpy(n,&nTmp,4);\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabLong((uint32*)n);\n\t\t\tn+=4;\n\t\t\t_TIFFmemcpy(n,&o->tdir_offset,4);\n\t\t\tn+=4;\n\t\t\to++;\n\t\t}\n\t\tnTmp = (uint32)tif->tif_nextdiroff;\n\t\t_TIFFmemcpy(n,&nTmp,4);\n\t}\n\telse\n\t{\n\t\tuint8* n;\n\t\tTIFFDirEntry* o;\n\t\tn=dirmem;\n\t\t*(uint64*)n=ndir;\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabLong8((uint64*)n);\n\t\tn+=8;\n\t\to=dir;\n\t\tfor (m=0; m<ndir; m++)\n\t\t{\n\t\t\t*(uint16*)n=o->tdir_tag;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\t*(uint16*)n=o->tdir_type;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\t_TIFFmemcpy(n,&o->tdir_count,8);\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabLong8((uint64*)n);\n\t\t\tn+=8;\n\t\t\t_TIFFmemcpy(n,&o->tdir_offset,8);\n\t\t\tn+=8;\n\t\t\to++;\n\t\t}\n\t\t_TIFFmemcpy(n,&tif->tif_nextdiroff,8);\n\t}\n\t_TIFFfree(dir);\n\tdir=NULL;\n\tif (!SeekOK(tif,tif->tif_diroff))\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");\n\t\tgoto bad;\n\t}\n\tif (!WriteOK(tif,dirmem,(tmsize_t)dirsize))\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");\n\t\tgoto bad;\n\t}\n\t_TIFFfree(dirmem);\n\tif (imagedone)\n\t{\n\t\tTIFFFreeDirectory(tif);\n\t\ttif->tif_flags &= ~TIFF_DIRTYDIRECT;\n\t\ttif->tif_flags &= ~TIFF_DIRTYSTRIP;\n\t\t(*tif->tif_cleanup)(tif);\n\t\tTIFFCreateDirectory(tif);\n\t}\n\treturn(1);\nbad:\n\tif (dir!=NULL)\n\t\t_TIFFfree(dir);\n\tif (dirmem!=NULL)\n\t\t_TIFFfree(dirmem);\n\treturn(0);\n}', 'static int\nTIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)\n{\n\tstatic const char module[] = "TIFFWriteDirectoryTagShortPerSample";\n\tuint16* m;\n\tuint16* na;\n\tuint16 nb;\n\tint o;\n\tif (dir==NULL)\n\t{\n\t\t(*ndir)++;\n\t\treturn(1);\n\t}\n\tm=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint16));\n\tif (m==NULL)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n\t\treturn(0);\n\t}\n\tfor (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)\n\t\t*na=value;\n\to=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);\n\t_TIFFfree(m);\n\treturn(o);\n}', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n\treturn (malloc((size_t) s));\n}', 'static int\nTIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value)\n{\n\tassert(count<0x80000000);\n\tassert(sizeof(uint16)==2);\n\tif (tif->tif_flags&TIFF_SWAB)\n\t\tTIFFSwabArrayOfShort(value,count);\n\treturn(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,count,count*2,value));\n}', 'void\nTIFFSwabArrayOfShort(register uint16* wp, tmsize_t n)\n{\n\tregister unsigned char* cp;\n\tregister unsigned char t;\n\tassert(sizeof(uint16)==2);\n\twhile (n-- > 0) {\n\t\tcp = (unsigned char*) wp;\n\t\tt = cp[1]; cp[1] = cp[0]; cp[0] = t;\n\t\twp++;\n\t}\n}']