LCOV - code coverage report
Current view: top level - frmts/gtiff/libtiff - tif_dir.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 680 1266 53.7 %
Date: 2026-02-23 15:56:29 Functions: 25 38 65.8 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 1988-1997 Sam Leffler
       3             :  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
       4             :  *
       5             :  * Permission to use, copy, modify, distribute, and sell this software and
       6             :  * its documentation for any purpose is hereby granted without fee, provided
       7             :  * that (i) the above copyright notices and this permission notice appear in
       8             :  * all copies of the software and related documentation, and (ii) the names of
       9             :  * Sam Leffler and Silicon Graphics may not be used in any advertising or
      10             :  * publicity relating to the software without the specific, prior written
      11             :  * permission of Sam Leffler and Silicon Graphics.
      12             :  *
      13             :  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
      14             :  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
      15             :  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
      16             :  *
      17             :  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
      18             :  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
      19             :  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
      20             :  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
      21             :  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
      22             :  * OF THIS SOFTWARE.
      23             :  */
      24             : 
      25             : /*
      26             :  * TIFF Library.
      27             :  *
      28             :  * Directory Tag Get & Set Routines.
      29             :  * (and also some miscellaneous stuff)
      30             :  */
      31             : #include "tiffiop.h"
      32             : #include <float.h> /*--: for Rational2Double */
      33             : #include <limits.h>
      34             : 
      35             : /*
      36             :  * These are used in the backwards compatibility code...
      37             :  */
      38             : #define DATATYPE_VOID 0   /* !untyped data */
      39             : #define DATATYPE_INT 1    /* !signed integer data */
      40             : #define DATATYPE_UINT 2   /* !unsigned integer data */
      41             : #define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
      42             : 
      43       58555 : static void setByteArray(TIFF *tif, void **vpp, const void *vp, size_t nmemb,
      44             :                          size_t elem_size)
      45             : {
      46       58555 :     if (*vpp)
      47             :     {
      48        1442 :         _TIFFfreeExt(tif, *vpp);
      49        1442 :         *vpp = 0;
      50             :     }
      51       58555 :     if (vp)
      52             :     {
      53       58464 :         tmsize_t bytes = _TIFFMultiplySSize(NULL, nmemb, elem_size, NULL);
      54       58482 :         if (bytes)
      55       58388 :             *vpp = (void *)_TIFFmallocExt(tif, bytes);
      56       58610 :         if (*vpp)
      57       58484 :             _TIFFmemcpy(*vpp, vp, bytes);
      58             :     }
      59       58676 : }
      60           0 : void _TIFFsetByteArray(void **vpp, const void *vp, uint32_t n)
      61             : {
      62           0 :     setByteArray(NULL, vpp, vp, n, 1);
      63           0 : }
      64        3086 : void _TIFFsetByteArrayExt(TIFF *tif, void **vpp, const void *vp, uint32_t n)
      65             : {
      66        3086 :     setByteArray(tif, vpp, vp, n, 1);
      67        3086 : }
      68             : 
      69           0 : static void _TIFFsetNString(TIFF *tif, char **cpp, const char *cp, uint32_t n)
      70             : {
      71           0 :     setByteArray(tif, (void **)cpp, cp, n, 1);
      72           0 : }
      73             : 
      74           0 : void _TIFFsetShortArray(uint16_t **wpp, const uint16_t *wp, uint32_t n)
      75             : {
      76           0 :     setByteArray(NULL, (void **)wpp, wp, n, sizeof(uint16_t));
      77           0 : }
      78        9994 : void _TIFFsetShortArrayExt(TIFF *tif, uint16_t **wpp, const uint16_t *wp,
      79             :                            uint32_t n)
      80             : {
      81        9994 :     setByteArray(tif, (void **)wpp, wp, n, sizeof(uint16_t));
      82        9994 : }
      83             : 
      84           0 : void _TIFFsetLongArray(uint32_t **lpp, const uint32_t *lp, uint32_t n)
      85             : {
      86           0 :     setByteArray(NULL, (void **)lpp, lp, n, sizeof(uint32_t));
      87           0 : }
      88           0 : void _TIFFsetLongArrayExt(TIFF *tif, uint32_t **lpp, const uint32_t *lp,
      89             :                           uint32_t n)
      90             : {
      91           0 :     setByteArray(tif, (void **)lpp, lp, n, sizeof(uint32_t));
      92           0 : }
      93             : 
      94          81 : static void _TIFFsetLong8Array(TIFF *tif, uint64_t **lpp, const uint64_t *lp,
      95             :                                uint32_t n)
      96             : {
      97          81 :     setByteArray(tif, (void **)lpp, lp, n, sizeof(uint64_t));
      98          81 : }
      99             : 
     100           0 : void _TIFFsetFloatArray(float **fpp, const float *fp, uint32_t n)
     101             : {
     102           0 :     setByteArray(NULL, (void **)fpp, fp, n, sizeof(float));
     103           0 : }
     104        1799 : void _TIFFsetFloatArrayExt(TIFF *tif, float **fpp, const float *fp, uint32_t n)
     105             : {
     106        1799 :     setByteArray(tif, (void **)fpp, fp, n, sizeof(float));
     107        1799 : }
     108             : 
     109           0 : void _TIFFsetDoubleArray(double **dpp, const double *dp, uint32_t n)
     110             : {
     111           0 :     setByteArray(NULL, (void **)dpp, dp, n, sizeof(double));
     112           0 : }
     113           0 : void _TIFFsetDoubleArrayExt(TIFF *tif, double **dpp, const double *dp,
     114             :                             uint32_t n)
     115             : {
     116           0 :     setByteArray(tif, (void **)dpp, dp, n, sizeof(double));
     117           0 : }
     118             : 
     119           0 : static void setDoubleArrayOneValue(TIFF *tif, double **vpp, double value,
     120             :                                    size_t nmemb)
     121             : {
     122           0 :     if (*vpp)
     123           0 :         _TIFFfreeExt(tif, *vpp);
     124           0 :     *vpp = (double *)_TIFFmallocExt(tif, nmemb * sizeof(double));
     125           0 :     if (*vpp)
     126             :     {
     127           0 :         while (nmemb--)
     128           0 :             ((double *)*vpp)[nmemb] = value;
     129             :     }
     130           0 : }
     131             : 
     132             : /*
     133             :  * Install extra samples information.
     134             :  */
     135        8626 : static int setExtraSamples(TIFF *tif, va_list ap, uint32_t *v)
     136             : {
     137             : /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
     138             : #define EXTRASAMPLE_COREL_UNASSALPHA 999
     139             : 
     140             :     uint16_t *va;
     141             :     uint32_t i;
     142        8626 :     TIFFDirectory *td = &tif->tif_dir;
     143             :     static const char module[] = "setExtraSamples";
     144             : 
     145        8626 :     *v = (uint16_t)va_arg(ap, uint16_vap);
     146        8626 :     if ((uint16_t)*v > td->td_samplesperpixel)
     147           0 :         return 0;
     148        8626 :     va = va_arg(ap, uint16_t *);
     149        8626 :     if (*v > 0 && va == NULL) /* typically missing param */
     150           0 :         return 0;
     151     1562710 :     for (i = 0; i < *v; i++)
     152             :     {
     153     1554080 :         if (va[i] > EXTRASAMPLE_UNASSALPHA)
     154             :         {
     155             :             /*
     156             :              * XXX: Corel Draw is known to produce incorrect
     157             :              * ExtraSamples tags which must be patched here if we
     158             :              * want to be able to open some of the damaged TIFF
     159             :              * files:
     160             :              */
     161           0 :             if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
     162           0 :                 va[i] = EXTRASAMPLE_UNASSALPHA;
     163             :             else
     164           0 :                 return 0;
     165             :         }
     166             :     }
     167             : 
     168        8626 :     if (td->td_transferfunction[0] != NULL &&
     169           0 :         (td->td_samplesperpixel - *v > 1) &&
     170           0 :         !(td->td_samplesperpixel - td->td_extrasamples > 1))
     171             :     {
     172           0 :         TIFFWarningExtR(tif, module,
     173             :                         "ExtraSamples tag value is changing, "
     174             :                         "but TransferFunction was read with a different value. "
     175             :                         "Canceling it");
     176           0 :         TIFFClrFieldBit(tif, FIELD_TRANSFERFUNCTION);
     177           0 :         _TIFFfreeExt(tif, td->td_transferfunction[0]);
     178           0 :         td->td_transferfunction[0] = NULL;
     179             :     }
     180             : 
     181        8626 :     td->td_extrasamples = (uint16_t)*v;
     182        8626 :     _TIFFsetShortArrayExt(tif, &td->td_sampleinfo, va, td->td_extrasamples);
     183        8626 :     return 1;
     184             : 
     185             : #undef EXTRASAMPLE_COREL_UNASSALPHA
     186             : }
     187             : 
     188             : /*
     189             :  * Count ink names separated by \0.  Returns
     190             :  * zero if the ink names are not as expected.
     191             :  */
     192           0 : static uint16_t countInkNamesString(TIFF *tif, uint32_t slen, const char *s)
     193             : {
     194           0 :     uint16_t i = 0;
     195             : 
     196           0 :     if (slen > 0)
     197             :     {
     198           0 :         const char *ep = s + slen;
     199           0 :         const char *cp = s;
     200             :         do
     201             :         {
     202           0 :             for (; cp < ep && *cp != '\0'; cp++)
     203             :             {
     204             :             }
     205           0 :             if (cp >= ep)
     206           0 :                 goto bad;
     207           0 :             cp++; /* skip \0 */
     208           0 :             i++;
     209           0 :         } while (cp < ep);
     210           0 :         return (i);
     211             :     }
     212           0 : bad:
     213           0 :     TIFFErrorExtR(tif, "TIFFSetField",
     214             :                   "%s: Invalid InkNames value; no null at given buffer end "
     215             :                   "location %" PRIu32 ", after %" PRIu16 " ink",
     216             :                   tif->tif_name, slen, i);
     217           0 :     return (0);
     218             : }
     219             : 
     220     1226880 : static int _TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap)
     221             : {
     222             :     static const char module[] = "_TIFFVSetField";
     223             : 
     224     1226880 :     TIFFDirectory *td = &tif->tif_dir;
     225     1226880 :     int status = 1;
     226             :     uint32_t v32, v;
     227             :     double dblval;
     228             :     char *s;
     229     1226880 :     const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
     230     1226500 :     uint32_t standard_tag = tag;
     231     1226500 :     if (fip == NULL) /* cannot happen since OkToChangeTag() already checks it */
     232           0 :         return 0;
     233             :     /*
     234             :      * We want to force the custom code to be used for custom
     235             :      * fields even if the tag happens to match a well known
     236             :      * one - important for reinterpreted handling of standard
     237             :      * tag values in custom directories (i.e. EXIF)
     238             :      */
     239     1226500 :     if (fip->field_bit == FIELD_CUSTOM)
     240             :     {
     241      158131 :         standard_tag = 0;
     242             :     }
     243             : 
     244     1226500 :     switch (standard_tag)
     245             :     {
     246        6667 :         case TIFFTAG_SUBFILETYPE:
     247        6667 :             td->td_subfiletype = (uint32_t)va_arg(ap, uint32_t);
     248        6667 :             break;
     249       94248 :         case TIFFTAG_IMAGEWIDTH:
     250       94248 :             td->td_imagewidth = (uint32_t)va_arg(ap, uint32_t);
     251       94247 :             break;
     252       94240 :         case TIFFTAG_IMAGELENGTH:
     253       94240 :             td->td_imagelength = (uint32_t)va_arg(ap, uint32_t);
     254       94249 :             break;
     255       94217 :         case TIFFTAG_BITSPERSAMPLE:
     256       94217 :             td->td_bitspersample = (uint16_t)va_arg(ap, uint16_vap);
     257             :             /*
     258             :              * If the data require post-decoding processing to byte-swap
     259             :              * samples, set it up here.  Note that since tags are required
     260             :              * to be ordered, compression code can override this behavior
     261             :              * in the setup method if it wants to roll the post decoding
     262             :              * work in with its normal work.
     263             :              */
     264       94227 :             if (tif->tif_flags & TIFF_SWAB)
     265             :             {
     266         696 :                 if (td->td_bitspersample == 8)
     267         386 :                     tif->tif_postdecode = _TIFFNoPostDecode;
     268         310 :                 else if (td->td_bitspersample == 16)
     269         126 :                     tif->tif_postdecode = _TIFFSwab16BitData;
     270         184 :                 else if (td->td_bitspersample == 24)
     271           0 :                     tif->tif_postdecode = _TIFFSwab24BitData;
     272         184 :                 else if (td->td_bitspersample == 32)
     273         138 :                     tif->tif_postdecode = _TIFFSwab32BitData;
     274          46 :                 else if (td->td_bitspersample == 64)
     275          21 :                     tif->tif_postdecode = _TIFFSwab64BitData;
     276          25 :                 else if (td->td_bitspersample == 128) /* two 64's */
     277           7 :                     tif->tif_postdecode = _TIFFSwab64BitData;
     278             :             }
     279       94227 :             break;
     280      230111 :         case TIFFTAG_COMPRESSION:
     281      230111 :             v = (uint16_t)va_arg(ap, uint16_vap);
     282             :             /*
     283             :              * If we're changing the compression scheme, notify the
     284             :              * previous module so that it can cleanup any state it's
     285             :              * setup.
     286             :              */
     287      230130 :             if (TIFFFieldSet(tif, FIELD_COMPRESSION))
     288             :             {
     289       94016 :                 if ((uint32_t)td->td_compression == v)
     290       57690 :                     break;
     291       36326 :                 (*tif->tif_cleanup)(tif);
     292       36325 :                 tif->tif_flags &= ~TIFF_CODERSETUP;
     293             :             }
     294             :             /*
     295             :              * Setup new compression routine state.
     296             :              */
     297      172439 :             if ((status = TIFFSetCompressionScheme(tif, v)) != 0)
     298      172387 :                 td->td_compression = (uint16_t)v;
     299             :             else
     300           0 :                 status = 0;
     301      172372 :             break;
     302       94265 :         case TIFFTAG_PHOTOMETRIC:
     303       94265 :             td->td_photometric = (uint16_t)va_arg(ap, uint16_vap);
     304       94257 :             break;
     305           0 :         case TIFFTAG_THRESHHOLDING:
     306           0 :             td->td_threshholding = (uint16_t)va_arg(ap, uint16_vap);
     307           0 :             break;
     308           2 :         case TIFFTAG_FILLORDER:
     309           2 :             v = (uint16_t)va_arg(ap, uint16_vap);
     310           2 :             if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
     311           0 :                 goto badvalue;
     312           2 :             td->td_fillorder = (uint16_t)v;
     313           2 :             break;
     314         108 :         case TIFFTAG_ORIENTATION:
     315         108 :             v = (uint16_t)va_arg(ap, uint16_vap);
     316         108 :             if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
     317           0 :                 goto badvalue;
     318             :             else
     319         108 :                 td->td_orientation = (uint16_t)v;
     320         108 :             break;
     321       94205 :         case TIFFTAG_SAMPLESPERPIXEL:
     322       94205 :             v = (uint16_t)va_arg(ap, uint16_vap);
     323       94204 :             if (v == 0)
     324           0 :                 goto badvalue;
     325       94204 :             if (v != td->td_samplesperpixel)
     326             :             {
     327             :                 /* See http://bugzilla.maptools.org/show_bug.cgi?id=2500 */
     328       23032 :                 if (td->td_sminsamplevalue != NULL)
     329             :                 {
     330           0 :                     TIFFWarningExtR(tif, module,
     331             :                                     "SamplesPerPixel tag value is changing, "
     332             :                                     "but SMinSampleValue tag was read with a "
     333             :                                     "different value. Canceling it");
     334           0 :                     TIFFClrFieldBit(tif, FIELD_SMINSAMPLEVALUE);
     335           0 :                     _TIFFfreeExt(tif, td->td_sminsamplevalue);
     336           0 :                     td->td_sminsamplevalue = NULL;
     337             :                 }
     338       23032 :                 if (td->td_smaxsamplevalue != NULL)
     339             :                 {
     340           0 :                     TIFFWarningExtR(tif, module,
     341             :                                     "SamplesPerPixel tag value is changing, "
     342             :                                     "but SMaxSampleValue tag was read with a "
     343             :                                     "different value. Canceling it");
     344           0 :                     TIFFClrFieldBit(tif, FIELD_SMAXSAMPLEVALUE);
     345           0 :                     _TIFFfreeExt(tif, td->td_smaxsamplevalue);
     346           0 :                     td->td_smaxsamplevalue = NULL;
     347             :                 }
     348             :                 /* Test if 3 transfer functions instead of just one are now
     349             :                    needed See http://bugzilla.maptools.org/show_bug.cgi?id=2820
     350             :                  */
     351       23032 :                 if (td->td_transferfunction[0] != NULL &&
     352           0 :                     (v - td->td_extrasamples > 1) &&
     353           0 :                     !(td->td_samplesperpixel - td->td_extrasamples > 1))
     354             :                 {
     355           0 :                     TIFFWarningExtR(tif, module,
     356             :                                     "SamplesPerPixel tag value is changing, "
     357             :                                     "but TransferFunction was read with a "
     358             :                                     "different value. Canceling it");
     359           0 :                     TIFFClrFieldBit(tif, FIELD_TRANSFERFUNCTION);
     360           0 :                     _TIFFfreeExt(tif, td->td_transferfunction[0]);
     361           0 :                     td->td_transferfunction[0] = NULL;
     362             :                 }
     363             :             }
     364       94157 :             td->td_samplesperpixel = (uint16_t)v;
     365       94157 :             break;
     366       83353 :         case TIFFTAG_ROWSPERSTRIP:
     367       83353 :             v32 = (uint32_t)va_arg(ap, uint32_t);
     368       83353 :             if (v32 == 0)
     369           0 :                 goto badvalue32;
     370       83353 :             td->td_rowsperstrip = v32;
     371       83353 :             if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
     372             :             {
     373       83324 :                 td->td_tilelength = v32;
     374       83324 :                 td->td_tilewidth = td->td_imagewidth;
     375             :             }
     376       83353 :             break;
     377           8 :         case TIFFTAG_MINSAMPLEVALUE:
     378           8 :             td->td_minsamplevalue = (uint16_t)va_arg(ap, uint16_vap);
     379           8 :             break;
     380           8 :         case TIFFTAG_MAXSAMPLEVALUE:
     381           8 :             td->td_maxsamplevalue = (uint16_t)va_arg(ap, uint16_vap);
     382           8 :             break;
     383           0 :         case TIFFTAG_SMINSAMPLEVALUE:
     384           0 :             if (tif->tif_flags & TIFF_PERSAMPLE)
     385           0 :                 _TIFFsetDoubleArrayExt(tif, &td->td_sminsamplevalue,
     386           0 :                                        va_arg(ap, double *),
     387           0 :                                        td->td_samplesperpixel);
     388             :             else
     389           0 :                 setDoubleArrayOneValue(tif, &td->td_sminsamplevalue,
     390             :                                        va_arg(ap, double),
     391           0 :                                        td->td_samplesperpixel);
     392           0 :             break;
     393           0 :         case TIFFTAG_SMAXSAMPLEVALUE:
     394           0 :             if (tif->tif_flags & TIFF_PERSAMPLE)
     395           0 :                 _TIFFsetDoubleArrayExt(tif, &td->td_smaxsamplevalue,
     396           0 :                                        va_arg(ap, double *),
     397           0 :                                        td->td_samplesperpixel);
     398             :             else
     399           0 :                 setDoubleArrayOneValue(tif, &td->td_smaxsamplevalue,
     400             :                                        va_arg(ap, double),
     401           0 :                                        td->td_samplesperpixel);
     402           0 :             break;
     403         130 :         case TIFFTAG_XRESOLUTION:
     404         130 :             dblval = va_arg(ap, double);
     405         130 :             if (dblval != dblval || dblval < 0)
     406           0 :                 goto badvaluedouble;
     407         130 :             td->td_xresolution = _TIFFClampDoubleToFloat(dblval);
     408         130 :             break;
     409         130 :         case TIFFTAG_YRESOLUTION:
     410         130 :             dblval = va_arg(ap, double);
     411         130 :             if (dblval != dblval || dblval < 0)
     412           0 :                 goto badvaluedouble;
     413         130 :             td->td_yresolution = _TIFFClampDoubleToFloat(dblval);
     414         130 :             break;
     415      148236 :         case TIFFTAG_PLANARCONFIG:
     416      148236 :             v = (uint16_t)va_arg(ap, uint16_vap);
     417      148276 :             if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
     418           0 :                 goto badvalue;
     419      148276 :             td->td_planarconfig = (uint16_t)v;
     420      148276 :             break;
     421           2 :         case TIFFTAG_XPOSITION:
     422           2 :             td->td_xposition = _TIFFClampDoubleToFloat(va_arg(ap, double));
     423           2 :             break;
     424           2 :         case TIFFTAG_YPOSITION:
     425           2 :             td->td_yposition = _TIFFClampDoubleToFloat(va_arg(ap, double));
     426           2 :             break;
     427         134 :         case TIFFTAG_RESOLUTIONUNIT:
     428         134 :             v = (uint16_t)va_arg(ap, uint16_vap);
     429         134 :             if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
     430           0 :                 goto badvalue;
     431         134 :             td->td_resolutionunit = (uint16_t)v;
     432         134 :             break;
     433           0 :         case TIFFTAG_PAGENUMBER:
     434           0 :             td->td_pagenumber[0] = (uint16_t)va_arg(ap, uint16_vap);
     435           0 :             td->td_pagenumber[1] = (uint16_t)va_arg(ap, uint16_vap);
     436           0 :             break;
     437           0 :         case TIFFTAG_HALFTONEHINTS:
     438           0 :             td->td_halftonehints[0] = (uint16_t)va_arg(ap, uint16_vap);
     439           0 :             td->td_halftonehints[1] = (uint16_t)va_arg(ap, uint16_vap);
     440           0 :             break;
     441         436 :         case TIFFTAG_COLORMAP:
     442         436 :             v32 = (uint32_t)(1L << td->td_bitspersample);
     443         436 :             _TIFFsetShortArrayExt(tif, &td->td_colormap[0],
     444         436 :                                   va_arg(ap, uint16_t *), v32);
     445         436 :             _TIFFsetShortArrayExt(tif, &td->td_colormap[1],
     446         436 :                                   va_arg(ap, uint16_t *), v32);
     447         436 :             _TIFFsetShortArrayExt(tif, &td->td_colormap[2],
     448         436 :                                   va_arg(ap, uint16_t *), v32);
     449         436 :             break;
     450        8626 :         case TIFFTAG_EXTRASAMPLES:
     451        8626 :             if (!setExtraSamples(tif, ap, &v))
     452           0 :                 goto badvalue;
     453        8626 :             break;
     454           2 :         case TIFFTAG_MATTEING:
     455           2 :             td->td_extrasamples = (((uint16_t)va_arg(ap, uint16_vap)) != 0);
     456           2 :             if (td->td_extrasamples)
     457             :             {
     458           0 :                 uint16_t sv = EXTRASAMPLE_ASSOCALPHA;
     459           0 :                 _TIFFsetShortArrayExt(tif, &td->td_sampleinfo, &sv, 1);
     460             :             }
     461           2 :             break;
     462       11006 :         case TIFFTAG_TILEWIDTH:
     463       11006 :             v32 = (uint32_t)va_arg(ap, uint32_t);
     464       11006 :             if (v32 % 16)
     465             :             {
     466           3 :                 if (tif->tif_mode != O_RDONLY)
     467           0 :                     goto badvalue32;
     468           3 :                 TIFFWarningExtR(
     469           3 :                     tif, tif->tif_name,
     470             :                     "Nonstandard tile width %" PRIu32 ", convert file", v32);
     471             :             }
     472       11006 :             td->td_tilewidth = v32;
     473       11006 :             tif->tif_flags |= TIFF_ISTILED;
     474       11006 :             break;
     475       11006 :         case TIFFTAG_TILELENGTH:
     476       11006 :             v32 = (uint32_t)va_arg(ap, uint32_t);
     477       11006 :             if (v32 % 16)
     478             :             {
     479           4 :                 if (tif->tif_mode != O_RDONLY)
     480           0 :                     goto badvalue32;
     481           4 :                 TIFFWarningExtR(
     482           4 :                     tif, tif->tif_name,
     483             :                     "Nonstandard tile length %" PRIu32 ", convert file", v32);
     484             :             }
     485       11006 :             td->td_tilelength = v32;
     486       11006 :             tif->tif_flags |= TIFF_ISTILED;
     487       11006 :             break;
     488           0 :         case TIFFTAG_TILEDEPTH:
     489           0 :             v32 = (uint32_t)va_arg(ap, uint32_t);
     490           0 :             if (v32 == 0)
     491           0 :                 goto badvalue32;
     492           0 :             td->td_tiledepth = v32;
     493           0 :             break;
     494           0 :         case TIFFTAG_DATATYPE:
     495           0 :             v = (uint16_t)va_arg(ap, uint16_vap);
     496           0 :             switch (v)
     497             :             {
     498           0 :                 case DATATYPE_VOID:
     499           0 :                     v = SAMPLEFORMAT_VOID;
     500           0 :                     break;
     501           0 :                 case DATATYPE_INT:
     502           0 :                     v = SAMPLEFORMAT_INT;
     503           0 :                     break;
     504           0 :                 case DATATYPE_UINT:
     505           0 :                     v = SAMPLEFORMAT_UINT;
     506           0 :                     break;
     507           0 :                 case DATATYPE_IEEEFP:
     508           0 :                     v = SAMPLEFORMAT_IEEEFP;
     509           0 :                     break;
     510           0 :                 default:
     511           0 :                     goto badvalue;
     512             :             }
     513           0 :             td->td_sampleformat = (uint16_t)v;
     514           0 :             break;
     515       93197 :         case TIFFTAG_SAMPLEFORMAT:
     516       93197 :             v = (uint16_t)va_arg(ap, uint16_vap);
     517       93170 :             if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
     518          23 :                 goto badvalue;
     519       93147 :             td->td_sampleformat = (uint16_t)v;
     520             : 
     521             :             /*  Try to fix up the SWAB function for complex data. */
     522       93147 :             if (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT &&
     523        1498 :                 td->td_bitspersample == 32 &&
     524         830 :                 tif->tif_postdecode == _TIFFSwab32BitData)
     525          81 :                 tif->tif_postdecode = _TIFFSwab16BitData;
     526       93066 :             else if ((td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT ||
     527       91677 :                       td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP) &&
     528        3179 :                      td->td_bitspersample == 64 &&
     529        1402 :                      tif->tif_postdecode == _TIFFSwab64BitData)
     530          14 :                 tif->tif_postdecode = _TIFFSwab32BitData;
     531       93147 :             break;
     532           0 :         case TIFFTAG_IMAGEDEPTH:
     533           0 :             td->td_imagedepth = (uint32_t)va_arg(ap, uint32_t);
     534           0 :             break;
     535          81 :         case TIFFTAG_SUBIFD:
     536          81 :             if ((tif->tif_flags & TIFF_INSUBIFD) == 0)
     537             :             {
     538          81 :                 td->td_nsubifd = (uint16_t)va_arg(ap, uint16_vap);
     539          81 :                 _TIFFsetLong8Array(tif, &td->td_subifd,
     540          81 :                                    (uint64_t *)va_arg(ap, uint64_t *),
     541          81 :                                    (uint32_t)td->td_nsubifd);
     542             :             }
     543             :             else
     544             :             {
     545           0 :                 TIFFErrorExtR(tif, module, "%s: Sorry, cannot nest SubIFDs",
     546             :                               tif->tif_name);
     547           0 :                 status = 0;
     548             :             }
     549          81 :             break;
     550          11 :         case TIFFTAG_YCBCRPOSITIONING:
     551          11 :             td->td_ycbcrpositioning = (uint16_t)va_arg(ap, uint16_vap);
     552          11 :             break;
     553        2118 :         case TIFFTAG_YCBCRSUBSAMPLING:
     554        2118 :             td->td_ycbcrsubsampling[0] = (uint16_t)va_arg(ap, uint16_vap);
     555        2118 :             td->td_ycbcrsubsampling[1] = (uint16_t)va_arg(ap, uint16_vap);
     556        2118 :             break;
     557          19 :         case TIFFTAG_TRANSFERFUNCTION:
     558             :         {
     559             :             uint32_t i;
     560          19 :             v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
     561          76 :             for (i = 0; i < v; i++)
     562          57 :                 _TIFFsetShortArrayExt(tif, &td->td_transferfunction[i],
     563          57 :                                       va_arg(ap, uint16_t *),
     564          57 :                                       1U << td->td_bitspersample);
     565          19 :             break;
     566             :         }
     567        1799 :         case TIFFTAG_REFERENCEBLACKWHITE:
     568             :             /* XXX should check for null range */
     569        1799 :             _TIFFsetFloatArrayExt(tif, &td->td_refblackwhite,
     570        1799 :                                   va_arg(ap, float *), 6);
     571        1799 :             break;
     572           0 :         case TIFFTAG_INKNAMES:
     573             :         {
     574           0 :             v = (uint16_t)va_arg(ap, uint16_vap);
     575           0 :             s = va_arg(ap, char *);
     576             :             uint16_t ninksinstring;
     577           0 :             ninksinstring = countInkNamesString(tif, v, s);
     578           0 :             status = ninksinstring > 0;
     579           0 :             if (ninksinstring > 0)
     580             :             {
     581           0 :                 _TIFFsetNString(tif, &td->td_inknames, s, v);
     582           0 :                 td->td_inknameslen = v;
     583             :                 /* Set NumberOfInks to the value ninksinstring */
     584           0 :                 if (TIFFFieldSet(tif, FIELD_NUMBEROFINKS))
     585             :                 {
     586           0 :                     if (td->td_numberofinks != ninksinstring)
     587             :                     {
     588           0 :                         TIFFErrorExtR(
     589             :                             tif, module,
     590             :                             "Warning %s; Tag %s:\n  Value %" PRIu16
     591             :                             " of NumberOfInks is different from the number of "
     592             :                             "inks %" PRIu16
     593             :                             ".\n  -> NumberOfInks value adapted to %" PRIu16 "",
     594           0 :                             tif->tif_name, fip->field_name, td->td_numberofinks,
     595             :                             ninksinstring, ninksinstring);
     596           0 :                         td->td_numberofinks = ninksinstring;
     597             :                     }
     598             :                 }
     599             :                 else
     600             :                 {
     601           0 :                     td->td_numberofinks = ninksinstring;
     602           0 :                     TIFFSetFieldBit(tif, FIELD_NUMBEROFINKS);
     603             :                 }
     604           0 :                 if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
     605             :                 {
     606           0 :                     if (td->td_numberofinks != td->td_samplesperpixel)
     607             :                     {
     608           0 :                         TIFFErrorExtR(tif, module,
     609             :                                       "Warning %s; Tag %s:\n  Value %" PRIu16
     610             :                                       " of NumberOfInks is different from the "
     611             :                                       "SamplesPerPixel value %" PRIu16 "",
     612             :                                       tif->tif_name, fip->field_name,
     613           0 :                                       td->td_numberofinks,
     614           0 :                                       td->td_samplesperpixel);
     615             :                     }
     616             :                 }
     617             :             }
     618             :         }
     619           0 :         break;
     620           0 :         case TIFFTAG_NUMBEROFINKS:
     621           0 :             v = (uint16_t)va_arg(ap, uint16_vap);
     622             :             /* If InkNames already set also NumberOfInks is set accordingly and
     623             :              * should be equal */
     624           0 :             if (TIFFFieldSet(tif, FIELD_INKNAMES))
     625             :             {
     626           0 :                 if (v != td->td_numberofinks)
     627             :                 {
     628           0 :                     TIFFErrorExtR(
     629             :                         tif, module,
     630             :                         "Error %s; Tag %s:\n  It is not possible to set the "
     631             :                         "value %" PRIu32
     632             :                         " for NumberOfInks\n  which is different from the "
     633             :                         "number of inks in the InkNames tag (%" PRIu16 ")",
     634           0 :                         tif->tif_name, fip->field_name, v, td->td_numberofinks);
     635             :                     /* Do not set / overwrite number of inks already set by
     636             :                      * InkNames case accordingly. */
     637           0 :                     status = 0;
     638             :                 }
     639             :             }
     640             :             else
     641             :             {
     642           0 :                 td->td_numberofinks = (uint16_t)v;
     643           0 :                 if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
     644             :                 {
     645           0 :                     if (td->td_numberofinks != td->td_samplesperpixel)
     646             :                     {
     647           0 :                         TIFFErrorExtR(tif, module,
     648             :                                       "Warning %s; Tag %s:\n  Value %" PRIu32
     649             :                                       " of NumberOfInks is different from the "
     650             :                                       "SamplesPerPixel value %" PRIu16 "",
     651             :                                       tif->tif_name, fip->field_name, v,
     652           0 :                                       td->td_samplesperpixel);
     653             :                     }
     654             :                 }
     655             :             }
     656           0 :             break;
     657           0 :         case TIFFTAG_PERSAMPLE:
     658           0 :             v = (uint16_t)va_arg(ap, uint16_vap);
     659           0 :             if (v == PERSAMPLE_MULTI)
     660           0 :                 tif->tif_flags |= TIFF_PERSAMPLE;
     661             :             else
     662           0 :                 tif->tif_flags &= ~TIFF_PERSAMPLE;
     663           0 :             break;
     664      158135 :         default:
     665             :         {
     666             :             TIFFTagValue *tv;
     667             :             int tv_size, iCustom;
     668             : 
     669             :             /*
     670             :              * This can happen if multiple images are open with different
     671             :              * codecs which have private tags.  The global tag information
     672             :              * table may then have tags that are valid for one file but not
     673             :              * the other. If the client tries to set a tag that is not valid
     674             :              * for the image's codec then we'll arrive here.  This
     675             :              * happens, for example, when tiffcp is used to convert between
     676             :              * compression schemes and codec-specific tags are blindly copied.
     677             :              *
     678             :              * This also happens when a FIELD_IGNORE tag is written.
     679             :              */
     680      158135 :             if (fip->field_bit == FIELD_IGNORE)
     681             :             {
     682           0 :                 TIFFErrorExtR(
     683             :                     tif, module,
     684             :                     "%s: Ignored %stag \"%s\" (not supported by libtiff)",
     685             :                     tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
     686             :                     fip->field_name);
     687           0 :                 status = 0;
     688           0 :                 break;
     689             :             }
     690      158135 :             if (fip->field_bit != FIELD_CUSTOM)
     691             :             {
     692           0 :                 TIFFErrorExtR(
     693             :                     tif, module,
     694             :                     "%s: Invalid %stag \"%s\" (not supported by codec)",
     695             :                     tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
     696             :                     fip->field_name);
     697           0 :                 status = 0;
     698           0 :                 break;
     699             :             }
     700             : 
     701             :             /*
     702             :              * Find the existing entry for this custom value.
     703             :              */
     704      158135 :             tv = NULL;
     705      414272 :             for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++)
     706             :             {
     707      258211 :                 if (td->td_customValues[iCustom].info->field_tag == tag)
     708             :                 {
     709        2074 :                     tv = td->td_customValues + iCustom;
     710        2074 :                     if (tv->value != NULL)
     711             :                     {
     712        2074 :                         _TIFFfreeExt(tif, tv->value);
     713        2074 :                         tv->value = NULL;
     714             :                     }
     715        2074 :                     break;
     716             :                 }
     717             :             }
     718             : 
     719             :             /*
     720             :              * Grow the custom list if the entry was not found.
     721             :              */
     722      158135 :             if (tv == NULL)
     723             :             {
     724             :                 TIFFTagValue *new_customValues;
     725             : 
     726      155999 :                 new_customValues = (TIFFTagValue *)_TIFFreallocExt(
     727      155999 :                     tif, td->td_customValues,
     728      155999 :                     sizeof(TIFFTagValue) * (td->td_customValueCount + 1));
     729      156313 :                 if (!new_customValues)
     730             :                 {
     731          80 :                     TIFFErrorExtR(tif, module,
     732             :                                   "%s: Failed to allocate space for list of "
     733             :                                   "custom values",
     734             :                                   tif->tif_name);
     735           0 :                     status = 0;
     736           0 :                     goto end;
     737             :                 }
     738             : 
     739      156233 :                 td->td_customValueCount++;
     740      156233 :                 td->td_customValues = new_customValues;
     741             : 
     742      156233 :                 tv = td->td_customValues + (td->td_customValueCount - 1);
     743      156233 :                 tv->info = fip;
     744      156233 :                 tv->value = NULL;
     745      156233 :                 tv->count = 0;
     746             :             }
     747             : 
     748             :             /*
     749             :              * Set custom value ... save a copy of the custom tag value.
     750             :              */
     751             :             /*--: Rational2Double: For Rationals evaluate "set_get_field_type"
     752             :              * to determine internal storage size. */
     753      158369 :             tv_size = TIFFFieldSetGetSize(fip);
     754      158191 :             if (tv_size == 0)
     755             :             {
     756          19 :                 status = 0;
     757          19 :                 TIFFErrorExtR(tif, module, "%s: Bad field type %u for \"%s\"",
     758          19 :                               tif->tif_name, fip->field_type, fip->field_name);
     759           0 :                 goto end;
     760             :             }
     761             : 
     762      158172 :             if (fip->field_type == TIFF_ASCII)
     763             :             {
     764             :                 uint32_t ma;
     765             :                 const char *mb;
     766       43538 :                 if (fip->field_passcount)
     767             :                 {
     768           0 :                     assert(fip->field_writecount == TIFF_VARIABLE2);
     769           0 :                     ma = (uint32_t)va_arg(ap, uint32_t);
     770           0 :                     mb = (const char *)va_arg(ap, const char *);
     771             :                 }
     772             :                 else
     773             :                 {
     774       43538 :                     mb = (const char *)va_arg(ap, const char *);
     775       43525 :                     size_t len = strlen(mb) + 1;
     776       43525 :                     if (len >= 0x80000000U)
     777             :                     {
     778          11 :                         status = 0;
     779          11 :                         TIFFErrorExtR(tif, module,
     780             :                                       "%s: Too long string value for \"%s\". "
     781             :                                       "Maximum supported is 2147483647 bytes",
     782             :                                       tif->tif_name, fip->field_name);
     783           0 :                         goto end;
     784             :                     }
     785       43514 :                     ma = (uint32_t)len;
     786             :                 }
     787       43514 :                 tv->count = ma;
     788       43514 :                 setByteArray(tif, &tv->value, mb, ma, 1);
     789             :             }
     790             :             else
     791             :             {
     792      114634 :                 if (fip->field_passcount)
     793             :                 {
     794      114554 :                     if (fip->field_writecount == TIFF_VARIABLE2)
     795        3017 :                         tv->count = (uint32_t)va_arg(ap, uint32_t);
     796             :                     else
     797      111537 :                         tv->count = (int)va_arg(ap, int);
     798             :                 }
     799          80 :                 else if (fip->field_writecount == TIFF_VARIABLE ||
     800          71 :                          fip->field_writecount == TIFF_VARIABLE2)
     801           9 :                     tv->count = 1;
     802          71 :                 else if (fip->field_writecount == TIFF_SPP)
     803           0 :                     tv->count = td->td_samplesperpixel;
     804             :                 else
     805          71 :                     tv->count = fip->field_writecount;
     806             : 
     807      114548 :                 if (tv->count == 0)
     808             :                 {
     809           0 :                     TIFFWarningExtR(tif, module,
     810             :                                     "%s: Null count for \"%s\" (type "
     811             :                                     "%u, writecount %d, passcount %d)",
     812             :                                     tif->tif_name, fip->field_name,
     813           0 :                                     fip->field_type, fip->field_writecount,
     814           0 :                                     fip->field_passcount);
     815           0 :                     break;
     816             :                 }
     817             : 
     818      114548 :                 tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size,
     819             :                                              "custom tag binary object");
     820      114788 :                 if (!tv->value)
     821             :                 {
     822           0 :                     status = 0;
     823           0 :                     goto end;
     824             :                 }
     825             : 
     826      114788 :                 if (fip->field_tag == TIFFTAG_DOTRANGE &&
     827           0 :                     strcmp(fip->field_name, "DotRange") == 0)
     828           0 :                 {
     829             :                     /* TODO: This is an evil exception and should not have been
     830             :                        handled this way ... likely best if we move it into
     831             :                        the directory structure with an explicit field in
     832             :                        libtiff 4.1 and assign it a FIELD_ value */
     833             :                     uint16_t v2[2];
     834           0 :                     v2[0] = (uint16_t)va_arg(ap, int);
     835           0 :                     v2[1] = (uint16_t)va_arg(ap, int);
     836           0 :                     _TIFFmemcpy(tv->value, &v2, 4);
     837             :                 }
     838             : 
     839      114788 :                 else if (fip->field_passcount ||
     840          71 :                          fip->field_writecount == TIFF_VARIABLE ||
     841          71 :                          fip->field_writecount == TIFF_VARIABLE2 ||
     842          71 :                          fip->field_writecount == TIFF_SPP || tv->count > 1)
     843             :                 {
     844             :                     /*--: Rational2Double: For Rationals tv_size is set above to
     845             :                      * 4 or 8 according to fip->set_get_field_type! */
     846      114697 :                     _TIFFmemcpy(tv->value, va_arg(ap, void *),
     847      114780 :                                 tv->count * tv_size);
     848             :                     /* Test here for too big values for LONG8, IFD8, SLONG8 in
     849             :                      * ClassicTIFF and delete custom field from custom list */
     850      114564 :                     if (!(tif->tif_flags & TIFF_BIGTIFF))
     851             :                     {
     852      114453 :                         if (tv->info->field_type == TIFF_LONG8 ||
     853      114584 :                             tv->info->field_type == TIFF_IFD8)
     854           0 :                         {
     855           0 :                             uint64_t *pui64 = (uint64_t *)tv->value;
     856           0 :                             for (int i = 0; i < tv->count; i++)
     857             :                             {
     858           0 :                                 if (pui64[i] > 0xffffffffu)
     859             :                                 {
     860           0 :                                     TIFFErrorExtR(
     861             :                                         tif, module,
     862             :                                         "%s: Bad %s value %" PRIu64
     863             :                                         " at %d. array position for \"%s\" tag "
     864             :                                         "%u in ClassicTIFF. Tag won't be "
     865             :                                         "written to file",
     866             :                                         tif->tif_name,
     867           0 :                                         (tv->info->field_type == TIFF_LONG8
     868             :                                              ? "LONG8"
     869             :                                              : "IFD8"),
     870           0 :                                         pui64[i], i, fip->field_name, tag);
     871           0 :                                     goto badvalueifd8long8;
     872             :                                 }
     873             :                             }
     874             :                         }
     875      114453 :                         else if (tv->info->field_type == TIFF_SLONG8)
     876             :                         {
     877           0 :                             int64_t *pi64 = (int64_t *)tv->value;
     878           0 :                             for (int i = 0; i < tv->count; i++)
     879             :                             {
     880           0 :                                 if (pi64[i] > 2147483647 ||
     881           0 :                                     pi64[i] < (-2147483647 - 1))
     882             :                                 {
     883           0 :                                     TIFFErrorExtR(
     884             :                                         tif, module,
     885             :                                         "%s: Bad SLONG8 value %" PRIi64
     886             :                                         " at %d. array position for \"%s\" tag "
     887             :                                         "%u in ClassicTIFF. Tag won't be "
     888             :                                         "written to file",
     889           0 :                                         tif->tif_name, pi64[i], i,
     890             :                                         fip->field_name, tag);
     891           0 :                                     goto badvalueifd8long8;
     892             :                                 }
     893             :                             }
     894             :                         }
     895             :                     }
     896             :                 }
     897             :                 else
     898             :                 {
     899           8 :                     char *val = (char *)tv->value;
     900           8 :                     assert(tv->count == 1);
     901             : 
     902           8 :                     switch (fip->field_type)
     903             :                     {
     904           0 :                         case TIFF_BYTE:
     905             :                         case TIFF_UNDEFINED:
     906             :                         {
     907           0 :                             uint8_t v2 = (uint8_t)va_arg(ap, int);
     908           0 :                             _TIFFmemcpy(val, &v2, tv_size);
     909             :                         }
     910           0 :                         break;
     911           0 :                         case TIFF_SBYTE:
     912             :                         {
     913           0 :                             int8_t v2 = (int8_t)va_arg(ap, int);
     914           0 :                             _TIFFmemcpy(val, &v2, tv_size);
     915             :                         }
     916           0 :                         break;
     917           0 :                         case TIFF_SHORT:
     918             :                         {
     919           0 :                             uint16_t v2 = (uint16_t)va_arg(ap, int);
     920           0 :                             _TIFFmemcpy(val, &v2, tv_size);
     921             :                         }
     922           0 :                         break;
     923           0 :                         case TIFF_SSHORT:
     924             :                         {
     925           0 :                             int16_t v2 = (int16_t)va_arg(ap, int);
     926           0 :                             _TIFFmemcpy(val, &v2, tv_size);
     927             :                         }
     928           0 :                         break;
     929           0 :                         case TIFF_LONG:
     930             :                         case TIFF_IFD:
     931             :                         {
     932           0 :                             uint32_t v2 = va_arg(ap, uint32_t);
     933           0 :                             _TIFFmemcpy(val, &v2, tv_size);
     934             :                         }
     935           0 :                         break;
     936           0 :                         case TIFF_SLONG:
     937             :                         {
     938           0 :                             int32_t v2 = va_arg(ap, int32_t);
     939           0 :                             _TIFFmemcpy(val, &v2, tv_size);
     940             :                         }
     941           0 :                         break;
     942           8 :                         case TIFF_LONG8:
     943             :                         case TIFF_IFD8:
     944             :                         {
     945           8 :                             uint64_t v2 = va_arg(ap, uint64_t);
     946           8 :                             _TIFFmemcpy(val, &v2, tv_size);
     947             :                             /* Test here for too big values for ClassicTIFF and
     948             :                              * delete custom field from custom list */
     949           8 :                             if (!(tif->tif_flags & TIFF_BIGTIFF) &&
     950           8 :                                 (v2 > 0xffffffffu))
     951             :                             {
     952           0 :                                 TIFFErrorExtR(
     953             :                                     tif, module,
     954             :                                     "%s: Bad LONG8 or IFD8 value %" PRIu64
     955             :                                     " for \"%s\" tag %u in ClassicTIFF. Tag "
     956             :                                     "won't be written to file",
     957             :                                     tif->tif_name, v2, fip->field_name, tag);
     958           0 :                                 goto badvalueifd8long8;
     959             :                             }
     960             :                         }
     961           8 :                         break;
     962           0 :                         case TIFF_SLONG8:
     963             :                         {
     964           0 :                             int64_t v2 = va_arg(ap, int64_t);
     965           0 :                             _TIFFmemcpy(val, &v2, tv_size);
     966             :                             /* Test here for too big values for ClassicTIFF and
     967             :                              * delete custom field from custom list */
     968           0 :                             if (!(tif->tif_flags & TIFF_BIGTIFF) &&
     969           0 :                                 ((v2 > 2147483647) || (v2 < (-2147483647 - 1))))
     970             :                             {
     971           0 :                                 TIFFErrorExtR(
     972             :                                     tif, module,
     973             :                                     "%s: Bad SLONG8 value %" PRIi64
     974             :                                     " for \"%s\" tag %u in ClassicTIFF. Tag "
     975             :                                     "won't be written to file",
     976             :                                     tif->tif_name, v2, fip->field_name, tag);
     977           0 :                                 goto badvalueifd8long8;
     978             :                             }
     979             :                         }
     980           0 :                         break;
     981           0 :                         case TIFF_RATIONAL:
     982             :                         case TIFF_SRATIONAL:
     983             :                             /*-- Rational2Double: For Rationals tv_size is set
     984             :                              * above to 4 or 8 according to
     985             :                              * fip->set_get_field_type!
     986             :                              */
     987             :                             {
     988           0 :                                 if (tv_size == 8)
     989             :                                 {
     990           0 :                                     double v2 = va_arg(ap, double);
     991           0 :                                     _TIFFmemcpy(val, &v2, tv_size);
     992             :                                 }
     993             :                                 else
     994             :                                 {
     995             :                                     /*-- default should be tv_size == 4 */
     996           0 :                                     float v3 = (float)va_arg(ap, double);
     997           0 :                                     _TIFFmemcpy(val, &v3, tv_size);
     998             :                                     /*-- ToDo: After Testing, this should be
     999             :                                      * removed and tv_size==4 should be set as
    1000             :                                      * default. */
    1001           0 :                                     if (tv_size != 4)
    1002             :                                     {
    1003           0 :                                         TIFFErrorExtR(tif, module,
    1004             :                                                       "Rational2Double: "
    1005             :                                                       ".set_get_field_type "
    1006             :                                                       "in not 4 but %d",
    1007             :                                                       tv_size);
    1008             :                                     }
    1009             :                                 }
    1010             :                             }
    1011           0 :                             break;
    1012           0 :                         case TIFF_FLOAT:
    1013             :                         {
    1014           0 :                             float v2 =
    1015           0 :                                 _TIFFClampDoubleToFloat(va_arg(ap, double));
    1016           0 :                             _TIFFmemcpy(val, &v2, tv_size);
    1017             :                         }
    1018           0 :                         break;
    1019           0 :                         case TIFF_DOUBLE:
    1020             :                         {
    1021           0 :                             double v2 = va_arg(ap, double);
    1022           0 :                             _TIFFmemcpy(val, &v2, tv_size);
    1023             :                         }
    1024           0 :                         break;
    1025           0 :                         case TIFF_NOTYPE:
    1026             :                         case TIFF_ASCII:
    1027             :                         default:
    1028           0 :                             _TIFFmemset(val, 0, tv_size);
    1029           0 :                             status = 0;
    1030           0 :                             break;
    1031             :                     }
    1032             :                 }
    1033             :             }
    1034             :         }
    1035             :     }
    1036     1225840 :     if (status)
    1037             :     {
    1038     1225840 :         const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
    1039     1226230 :         if (fip2)
    1040     1226120 :             TIFFSetFieldBit(tif, fip2->field_bit);
    1041     1226230 :         tif->tif_flags |= TIFF_DIRTYDIRECT;
    1042             :     }
    1043             : 
    1044           0 : end:
    1045     1226230 :     va_end(ap);
    1046     1226230 :     return (status);
    1047          23 : badvalue:
    1048             : {
    1049          23 :     const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
    1050           0 :     TIFFErrorExtR(tif, module, "%s: Bad value %" PRIu32 " for \"%s\" tag",
    1051             :                   tif->tif_name, v, fip2 ? fip2->field_name : "Unknown");
    1052           0 :     va_end(ap);
    1053             : }
    1054           0 :     return (0);
    1055           0 : badvalue32:
    1056             : {
    1057           0 :     const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
    1058           0 :     TIFFErrorExtR(tif, module, "%s: Bad value %" PRIu32 " for \"%s\" tag",
    1059             :                   tif->tif_name, v32, fip2 ? fip2->field_name : "Unknown");
    1060           0 :     va_end(ap);
    1061             : }
    1062           0 :     return (0);
    1063           0 : badvaluedouble:
    1064             : {
    1065           0 :     const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
    1066           0 :     TIFFErrorExtR(tif, module, "%s: Bad value %f for \"%s\" tag", tif->tif_name,
    1067             :                   dblval, fip2 ? fip2->field_name : "Unknown");
    1068           0 :     va_end(ap);
    1069             : }
    1070           0 :     return (0);
    1071           0 : badvalueifd8long8:
    1072             : {
    1073             :     /* Error message issued already above. */
    1074           0 :     TIFFTagValue *tv2 = NULL;
    1075             :     int iCustom2, iC2;
    1076             :     /* Find the existing entry for this custom value. */
    1077           0 :     for (iCustom2 = 0; iCustom2 < td->td_customValueCount; iCustom2++)
    1078             :     {
    1079           0 :         if (td->td_customValues[iCustom2].info->field_tag == tag)
    1080             :         {
    1081           0 :             tv2 = td->td_customValues + (iCustom2);
    1082           0 :             break;
    1083             :         }
    1084             :     }
    1085           0 :     if (tv2 != NULL)
    1086             :     {
    1087             :         /* Remove custom field from custom list */
    1088           0 :         if (tv2->value != NULL)
    1089             :         {
    1090           0 :             _TIFFfreeExt(tif, tv2->value);
    1091           0 :             tv2->value = NULL;
    1092             :         }
    1093             :         /* Shorten list and close gap in customValues list.
    1094             :          * Re-allocation of td_customValues not necessary here. */
    1095           0 :         td->td_customValueCount--;
    1096           0 :         for (iC2 = iCustom2; iC2 < td->td_customValueCount; iC2++)
    1097             :         {
    1098           0 :             td->td_customValues[iC2] = td->td_customValues[iC2 + 1];
    1099             :         }
    1100             :     }
    1101             :     else
    1102             :     {
    1103           0 :         assert(0);
    1104             :     }
    1105           0 :     va_end(ap);
    1106             : }
    1107           0 :     return (0);
    1108             : } /*-- _TIFFVSetField() --*/
    1109             : 
    1110             : /*
    1111             :  * Return 1/0 according to whether or not
    1112             :  * it is permissible to set the tag's value.
    1113             :  * Note that we allow ImageLength to be changed
    1114             :  * so that we can append and extend to images.
    1115             :  * Any other tag may not be altered once writing
    1116             :  * has commenced, unless its value has no effect
    1117             :  * on the format of the data that is written.
    1118             :  */
    1119     1250530 : static int OkToChangeTag(TIFF *tif, uint32_t tag)
    1120             : {
    1121     1250530 :     const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
    1122     1251600 :     if (!fip)
    1123             :     { /* unknown tag */
    1124           0 :         TIFFErrorExtR(tif, "TIFFSetField", "%s: Unknown %stag %" PRIu32,
    1125             :                       tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
    1126           0 :         return (0);
    1127             :     }
    1128     1251600 :     if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
    1129         847 :         !fip->field_oktochange)
    1130             :     {
    1131             :         /*
    1132             :          * Consult info table to see if tag can be changed
    1133             :          * after we've started writing.  We only allow changes
    1134             :          * to those tags that don't/shouldn't affect the
    1135             :          * compression and/or format of the data.
    1136             :          */
    1137           2 :         TIFFErrorExtR(tif, "TIFFSetField",
    1138             :                       "%s: Cannot modify tag \"%s\" while writing",
    1139             :                       tif->tif_name, fip->field_name);
    1140           2 :         return (0);
    1141             :     }
    1142     1251600 :     return (1);
    1143             : }
    1144             : 
    1145             : /*
    1146             :  * Record the value of a field in the
    1147             :  * internal directory structure.  The
    1148             :  * field will be written to the file
    1149             :  * when/if the directory structure is
    1150             :  * updated.
    1151             :  */
    1152     1251300 : int TIFFSetField(TIFF *tif, uint32_t tag, ...)
    1153             : {
    1154             :     va_list ap;
    1155             :     int status;
    1156             : 
    1157     1251300 :     va_start(ap, tag);
    1158     1251300 :     status = TIFFVSetField(tif, tag, ap);
    1159     1250910 :     va_end(ap);
    1160     1250910 :     return (status);
    1161             : }
    1162             : 
    1163             : /*
    1164             :  * Clear the contents of the field in the internal structure.
    1165             :  */
    1166        5525 : int TIFFUnsetField(TIFF *tif, uint32_t tag)
    1167             : {
    1168        5525 :     const TIFFField *fip = TIFFFieldWithTag(tif, tag);
    1169        5525 :     TIFFDirectory *td = &tif->tif_dir;
    1170             : 
    1171        5525 :     if (!fip)
    1172           0 :         return 0;
    1173             : 
    1174        5525 :     if (fip->field_bit != FIELD_CUSTOM)
    1175           5 :         TIFFClrFieldBit(tif, fip->field_bit);
    1176             :     else
    1177             :     {
    1178        5520 :         TIFFTagValue *tv = NULL;
    1179             :         int i;
    1180             : 
    1181        6103 :         for (i = 0; i < td->td_customValueCount; i++)
    1182             :         {
    1183             : 
    1184         698 :             tv = td->td_customValues + i;
    1185         698 :             if (tv->info->field_tag == tag)
    1186         115 :                 break;
    1187             :         }
    1188             : 
    1189        5520 :         if (i < td->td_customValueCount)
    1190             :         {
    1191         115 :             _TIFFfreeExt(tif, tv->value);
    1192         268 :             for (; i < td->td_customValueCount - 1; i++)
    1193             :             {
    1194         153 :                 td->td_customValues[i] = td->td_customValues[i + 1];
    1195             :             }
    1196         115 :             td->td_customValueCount--;
    1197             :         }
    1198             :     }
    1199             : 
    1200        5525 :     tif->tif_flags |= TIFF_DIRTYDIRECT;
    1201             : 
    1202        5525 :     return (1);
    1203             : }
    1204             : 
    1205             : /*
    1206             :  * Like TIFFSetField, but taking a varargs
    1207             :  * parameter list.  This routine is useful
    1208             :  * for building higher-level interfaces on
    1209             :  * top of the library.
    1210             :  */
    1211     1251180 : int TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap)
    1212             : {
    1213     1251180 :     return OkToChangeTag(tif, tag)
    1214     1251490 :                ? (*tif->tif_tagmethods.vsetfield)(tif, tag, ap)
    1215     2502440 :                : 0;
    1216             : }
    1217             : 
    1218     2022530 : static int _TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap)
    1219             : {
    1220     2022530 :     TIFFDirectory *td = &tif->tif_dir;
    1221     2022530 :     int ret_val = 1;
    1222     2022530 :     uint32_t standard_tag = tag;
    1223     2022530 :     const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
    1224     2021830 :     if (fip == NULL) /* cannot happen since TIFFGetField() already checks it */
    1225           0 :         return 0;
    1226             : 
    1227             :     /*
    1228             :      * We want to force the custom code to be used for custom
    1229             :      * fields even if the tag happens to match a well known
    1230             :      * one - important for reinterpreted handling of standard
    1231             :      * tag values in custom directories (i.e. EXIF)
    1232             :      */
    1233     2021830 :     if (fip->field_bit == FIELD_CUSTOM)
    1234             :     {
    1235      398362 :         standard_tag = 0;
    1236             :     }
    1237             : 
    1238     2021830 :     switch (standard_tag)
    1239             :     {
    1240        1381 :         case TIFFTAG_SUBFILETYPE:
    1241        1381 :             *va_arg(ap, uint32_t *) = td->td_subfiletype;
    1242        1381 :             break;
    1243       51988 :         case TIFFTAG_IMAGEWIDTH:
    1244       51988 :             *va_arg(ap, uint32_t *) = td->td_imagewidth;
    1245       52023 :             break;
    1246       52159 :         case TIFFTAG_IMAGELENGTH:
    1247       52159 :             *va_arg(ap, uint32_t *) = td->td_imagelength;
    1248       52166 :             break;
    1249       34732 :         case TIFFTAG_BITSPERSAMPLE:
    1250       34732 :             *va_arg(ap, uint16_t *) = td->td_bitspersample;
    1251       34708 :             break;
    1252       59640 :         case TIFFTAG_COMPRESSION:
    1253       59640 :             *va_arg(ap, uint16_t *) = td->td_compression;
    1254       59622 :             break;
    1255       43019 :         case TIFFTAG_PHOTOMETRIC:
    1256       43019 :             *va_arg(ap, uint16_t *) = td->td_photometric;
    1257       42951 :             break;
    1258           0 :         case TIFFTAG_THRESHHOLDING:
    1259           0 :             *va_arg(ap, uint16_t *) = td->td_threshholding;
    1260           0 :             break;
    1261           0 :         case TIFFTAG_FILLORDER:
    1262           0 :             *va_arg(ap, uint16_t *) = td->td_fillorder;
    1263           0 :             break;
    1264          16 :         case TIFFTAG_ORIENTATION:
    1265          16 :             *va_arg(ap, uint16_t *) = td->td_orientation;
    1266          16 :             break;
    1267       29308 :         case TIFFTAG_SAMPLESPERPIXEL:
    1268       29308 :             *va_arg(ap, uint16_t *) = td->td_samplesperpixel;
    1269       29193 :             break;
    1270       29293 :         case TIFFTAG_ROWSPERSTRIP:
    1271       29293 :             *va_arg(ap, uint32_t *) = td->td_rowsperstrip;
    1272       29221 :             break;
    1273           5 :         case TIFFTAG_MINSAMPLEVALUE:
    1274           5 :             *va_arg(ap, uint16_t *) = td->td_minsamplevalue;
    1275           5 :             break;
    1276           5 :         case TIFFTAG_MAXSAMPLEVALUE:
    1277           5 :             *va_arg(ap, uint16_t *) = td->td_maxsamplevalue;
    1278           5 :             break;
    1279           0 :         case TIFFTAG_SMINSAMPLEVALUE:
    1280           0 :             if (tif->tif_flags & TIFF_PERSAMPLE)
    1281           0 :                 *va_arg(ap, double **) = td->td_sminsamplevalue;
    1282             :             else
    1283             :             {
    1284             :                 /* libtiff historically treats this as a single value. */
    1285             :                 uint16_t i;
    1286           0 :                 double v = td->td_sminsamplevalue[0];
    1287           0 :                 for (i = 1; i < td->td_samplesperpixel; ++i)
    1288           0 :                     if (td->td_sminsamplevalue[i] < v)
    1289           0 :                         v = td->td_sminsamplevalue[i];
    1290           0 :                 *va_arg(ap, double *) = v;
    1291             :             }
    1292           0 :             break;
    1293           0 :         case TIFFTAG_SMAXSAMPLEVALUE:
    1294           0 :             if (tif->tif_flags & TIFF_PERSAMPLE)
    1295           0 :                 *va_arg(ap, double **) = td->td_smaxsamplevalue;
    1296             :             else
    1297             :             {
    1298             :                 /* libtiff historically treats this as a single value. */
    1299             :                 uint16_t i;
    1300           0 :                 double v = td->td_smaxsamplevalue[0];
    1301           0 :                 for (i = 1; i < td->td_samplesperpixel; ++i)
    1302           0 :                     if (td->td_smaxsamplevalue[i] > v)
    1303           0 :                         v = td->td_smaxsamplevalue[i];
    1304           0 :                 *va_arg(ap, double *) = v;
    1305             :             }
    1306           0 :             break;
    1307          87 :         case TIFFTAG_XRESOLUTION:
    1308          87 :             *va_arg(ap, float *) = td->td_xresolution;
    1309          87 :             break;
    1310          86 :         case TIFFTAG_YRESOLUTION:
    1311          86 :             *va_arg(ap, float *) = td->td_yresolution;
    1312          86 :             break;
    1313       35779 :         case TIFFTAG_PLANARCONFIG:
    1314       35779 :             *va_arg(ap, uint16_t *) = td->td_planarconfig;
    1315       35667 :             break;
    1316           0 :         case TIFFTAG_XPOSITION:
    1317           0 :             *va_arg(ap, float *) = td->td_xposition;
    1318           0 :             break;
    1319           0 :         case TIFFTAG_YPOSITION:
    1320           0 :             *va_arg(ap, float *) = td->td_yposition;
    1321           0 :             break;
    1322          89 :         case TIFFTAG_RESOLUTIONUNIT:
    1323          89 :             *va_arg(ap, uint16_t *) = td->td_resolutionunit;
    1324          89 :             break;
    1325           0 :         case TIFFTAG_PAGENUMBER:
    1326           0 :             *va_arg(ap, uint16_t *) = td->td_pagenumber[0];
    1327           0 :             *va_arg(ap, uint16_t *) = td->td_pagenumber[1];
    1328           0 :             break;
    1329           0 :         case TIFFTAG_HALFTONEHINTS:
    1330           0 :             *va_arg(ap, uint16_t *) = td->td_halftonehints[0];
    1331           0 :             *va_arg(ap, uint16_t *) = td->td_halftonehints[1];
    1332           0 :             break;
    1333         169 :         case TIFFTAG_COLORMAP:
    1334         169 :             *va_arg(ap, const uint16_t **) = td->td_colormap[0];
    1335         169 :             *va_arg(ap, const uint16_t **) = td->td_colormap[1];
    1336         169 :             *va_arg(ap, const uint16_t **) = td->td_colormap[2];
    1337         169 :             break;
    1338      140597 :         case TIFFTAG_STRIPOFFSETS:
    1339             :         case TIFFTAG_TILEOFFSETS:
    1340      140597 :             _TIFFFillStriles(tif);
    1341      140607 :             *va_arg(ap, const uint64_t **) = td->td_stripoffset_p;
    1342      140605 :             if (td->td_stripoffset_p == NULL)
    1343           0 :                 ret_val = 0;
    1344      140605 :             break;
    1345      168949 :         case TIFFTAG_STRIPBYTECOUNTS:
    1346             :         case TIFFTAG_TILEBYTECOUNTS:
    1347      168949 :             _TIFFFillStriles(tif);
    1348      168950 :             *va_arg(ap, const uint64_t **) = td->td_stripbytecount_p;
    1349      168947 :             if (td->td_stripbytecount_p == NULL)
    1350           0 :                 ret_val = 0;
    1351      168947 :             break;
    1352           0 :         case TIFFTAG_MATTEING:
    1353           0 :             *va_arg(ap, uint16_t *) =
    1354           0 :                 (td->td_extrasamples == 1 &&
    1355           0 :                  td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
    1356           0 :             break;
    1357      929424 :         case TIFFTAG_EXTRASAMPLES:
    1358      929424 :             *va_arg(ap, uint16_t *) = td->td_extrasamples;
    1359      929424 :             *va_arg(ap, const uint16_t **) = td->td_sampleinfo;
    1360      929424 :             break;
    1361        4277 :         case TIFFTAG_TILEWIDTH:
    1362        4277 :             *va_arg(ap, uint32_t *) = td->td_tilewidth;
    1363        4277 :             break;
    1364        4277 :         case TIFFTAG_TILELENGTH:
    1365        4277 :             *va_arg(ap, uint32_t *) = td->td_tilelength;
    1366        4277 :             break;
    1367           0 :         case TIFFTAG_TILEDEPTH:
    1368           0 :             *va_arg(ap, uint32_t *) = td->td_tiledepth;
    1369           0 :             break;
    1370           0 :         case TIFFTAG_DATATYPE:
    1371           0 :             switch (td->td_sampleformat)
    1372             :             {
    1373           0 :                 case SAMPLEFORMAT_UINT:
    1374           0 :                     *va_arg(ap, uint16_t *) = DATATYPE_UINT;
    1375           0 :                     break;
    1376           0 :                 case SAMPLEFORMAT_INT:
    1377           0 :                     *va_arg(ap, uint16_t *) = DATATYPE_INT;
    1378           0 :                     break;
    1379           0 :                 case SAMPLEFORMAT_IEEEFP:
    1380           0 :                     *va_arg(ap, uint16_t *) = DATATYPE_IEEEFP;
    1381           0 :                     break;
    1382           0 :                 case SAMPLEFORMAT_VOID:
    1383           0 :                     *va_arg(ap, uint16_t *) = DATATYPE_VOID;
    1384           0 :                     break;
    1385           0 :                 default:
    1386           0 :                     break;
    1387             :             }
    1388           0 :             break;
    1389       33356 :         case TIFFTAG_SAMPLEFORMAT:
    1390       33356 :             *va_arg(ap, uint16_t *) = td->td_sampleformat;
    1391       33421 :             break;
    1392           0 :         case TIFFTAG_IMAGEDEPTH:
    1393           0 :             *va_arg(ap, uint32_t *) = td->td_imagedepth;
    1394           0 :             break;
    1395          22 :         case TIFFTAG_SUBIFD:
    1396          22 :             *va_arg(ap, uint16_t *) = td->td_nsubifd;
    1397          22 :             *va_arg(ap, const uint64_t **) = td->td_subifd;
    1398          22 :             break;
    1399           0 :         case TIFFTAG_YCBCRPOSITIONING:
    1400           0 :             *va_arg(ap, uint16_t *) = td->td_ycbcrpositioning;
    1401           0 :             break;
    1402        3567 :         case TIFFTAG_YCBCRSUBSAMPLING:
    1403        3567 :             *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[0];
    1404        3567 :             *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[1];
    1405        2794 :             break;
    1406           6 :         case TIFFTAG_TRANSFERFUNCTION:
    1407           6 :             *va_arg(ap, const uint16_t **) = td->td_transferfunction[0];
    1408           6 :             if (td->td_samplesperpixel - td->td_extrasamples > 1)
    1409             :             {
    1410           6 :                 *va_arg(ap, const uint16_t **) = td->td_transferfunction[1];
    1411           6 :                 *va_arg(ap, const uint16_t **) = td->td_transferfunction[2];
    1412             :             }
    1413             :             else
    1414             :             {
    1415           0 :                 *va_arg(ap, const uint16_t **) = NULL;
    1416           0 :                 *va_arg(ap, const uint16_t **) = NULL;
    1417             :             }
    1418           6 :             break;
    1419         769 :         case TIFFTAG_REFERENCEBLACKWHITE:
    1420         769 :             *va_arg(ap, const float **) = td->td_refblackwhite;
    1421         769 :             break;
    1422           0 :         case TIFFTAG_INKNAMES:
    1423           0 :             *va_arg(ap, const char **) = td->td_inknames;
    1424           0 :             break;
    1425           0 :         case TIFFTAG_NUMBEROFINKS:
    1426           0 :             *va_arg(ap, uint16_t *) = td->td_numberofinks;
    1427           0 :             break;
    1428      398830 :         default:
    1429             :         {
    1430             :             int i;
    1431             : 
    1432             :             /*
    1433             :              * This can happen if multiple images are open
    1434             :              * with different codecs which have private
    1435             :              * tags.  The global tag information table may
    1436             :              * then have tags that are valid for one file
    1437             :              * but not the other. If the client tries to
    1438             :              * get a tag that is not valid for the image's
    1439             :              * codec then we'll arrive here.
    1440             :              */
    1441      398830 :             if (fip->field_bit != FIELD_CUSTOM)
    1442             :             {
    1443           0 :                 TIFFErrorExtR(tif, "_TIFFVGetField",
    1444             :                               "%s: Invalid %stag \"%s\" "
    1445             :                               "(not supported by codec)",
    1446             :                               tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
    1447             :                               fip->field_name);
    1448           0 :                 ret_val = 0;
    1449           0 :                 break;
    1450             :             }
    1451             : 
    1452             :             /*
    1453             :              * Do we have a custom value?
    1454             :              */
    1455      398830 :             ret_val = 0;
    1456     1690270 :             for (i = 0; i < td->td_customValueCount; i++)
    1457             :             {
    1458     1379160 :                 TIFFTagValue *tv = td->td_customValues + i;
    1459             : 
    1460     1379160 :                 if (tv->info->field_tag != tag)
    1461     1291440 :                     continue;
    1462             : 
    1463       87722 :                 if (fip->field_passcount)
    1464             :                 {
    1465       63380 :                     if (fip->field_readcount == TIFF_VARIABLE2)
    1466         392 :                         *va_arg(ap, uint32_t *) = (uint32_t)tv->count;
    1467             :                     else /* Assume TIFF_VARIABLE */
    1468       62988 :                         *va_arg(ap, uint16_t *) = (uint16_t)tv->count;
    1469       63380 :                     *va_arg(ap, const void **) = tv->value;
    1470       63380 :                     ret_val = 1;
    1471             :                 }
    1472       24342 :                 else if (fip->field_tag == TIFFTAG_DOTRANGE &&
    1473           0 :                          strcmp(fip->field_name, "DotRange") == 0)
    1474             :                 {
    1475             :                     /* TODO: This is an evil exception and should not have been
    1476             :                        handled this way ... likely best if we move it into
    1477             :                        the directory structure with an explicit field in
    1478             :                        libtiff 4.1 and assign it a FIELD_ value */
    1479           0 :                     *va_arg(ap, uint16_t *) = ((uint16_t *)tv->value)[0];
    1480           0 :                     *va_arg(ap, uint16_t *) = ((uint16_t *)tv->value)[1];
    1481           0 :                     ret_val = 1;
    1482             :                 }
    1483             :                 else
    1484             :                 {
    1485       24342 :                     if (fip->field_type == TIFF_ASCII ||
    1486          39 :                         fip->field_readcount == TIFF_VARIABLE ||
    1487          39 :                         fip->field_readcount == TIFF_VARIABLE2 ||
    1488          39 :                         fip->field_readcount == TIFF_SPP || tv->count > 1)
    1489             :                     {
    1490       24336 :                         *va_arg(ap, void **) = tv->value;
    1491       24337 :                         ret_val = 1;
    1492             :                     }
    1493             :                     else
    1494             :                     {
    1495           6 :                         char *val = (char *)tv->value;
    1496           6 :                         assert(tv->count == 1);
    1497           6 :                         switch (fip->field_type)
    1498             :                         {
    1499           0 :                             case TIFF_BYTE:
    1500             :                             case TIFF_UNDEFINED:
    1501           0 :                                 *va_arg(ap, uint8_t *) = *(uint8_t *)val;
    1502           0 :                                 ret_val = 1;
    1503           0 :                                 break;
    1504           0 :                             case TIFF_SBYTE:
    1505           0 :                                 *va_arg(ap, int8_t *) = *(int8_t *)val;
    1506           0 :                                 ret_val = 1;
    1507           0 :                                 break;
    1508           0 :                             case TIFF_SHORT:
    1509           0 :                                 *va_arg(ap, uint16_t *) = *(uint16_t *)val;
    1510           0 :                                 ret_val = 1;
    1511           0 :                                 break;
    1512           0 :                             case TIFF_SSHORT:
    1513           0 :                                 *va_arg(ap, int16_t *) = *(int16_t *)val;
    1514           0 :                                 ret_val = 1;
    1515           0 :                                 break;
    1516           0 :                             case TIFF_LONG:
    1517             :                             case TIFF_IFD:
    1518           0 :                                 *va_arg(ap, uint32_t *) = *(uint32_t *)val;
    1519           0 :                                 ret_val = 1;
    1520           0 :                                 break;
    1521           0 :                             case TIFF_SLONG:
    1522           0 :                                 *va_arg(ap, int32_t *) = *(int32_t *)val;
    1523           0 :                                 ret_val = 1;
    1524           0 :                                 break;
    1525           6 :                             case TIFF_LONG8:
    1526             :                             case TIFF_IFD8:
    1527           6 :                                 *va_arg(ap, uint64_t *) = *(uint64_t *)val;
    1528           6 :                                 ret_val = 1;
    1529           6 :                                 break;
    1530           0 :                             case TIFF_SLONG8:
    1531           0 :                                 *va_arg(ap, int64_t *) = *(int64_t *)val;
    1532           0 :                                 ret_val = 1;
    1533           0 :                                 break;
    1534           0 :                             case TIFF_RATIONAL:
    1535             :                             case TIFF_SRATIONAL:
    1536             :                             {
    1537             :                                 /*-- Rational2Double: For Rationals evaluate
    1538             :                                  * "set_get_field_type" to determine internal
    1539             :                                  * storage size and return value size. */
    1540           0 :                                 int tv_size = TIFFFieldSetGetSize(fip);
    1541           0 :                                 if (tv_size == 8)
    1542             :                                 {
    1543           0 :                                     *va_arg(ap, double *) = *(double *)val;
    1544           0 :                                     ret_val = 1;
    1545             :                                 }
    1546             :                                 else
    1547             :                                 {
    1548             :                                     /*-- default should be tv_size == 4  */
    1549           0 :                                     *va_arg(ap, float *) = *(float *)val;
    1550           0 :                                     ret_val = 1;
    1551             :                                     /*-- ToDo: After Testing, this should be
    1552             :                                      * removed and tv_size==4 should be set as
    1553             :                                      * default. */
    1554           0 :                                     if (tv_size != 4)
    1555             :                                     {
    1556           0 :                                         TIFFErrorExtR(tif, "_TIFFVGetField",
    1557             :                                                       "Rational2Double: "
    1558             :                                                       ".set_get_field_type "
    1559             :                                                       "in not 4 but %d",
    1560             :                                                       tv_size);
    1561             :                                     }
    1562             :                                 }
    1563             :                             }
    1564           0 :                             break;
    1565           0 :                             case TIFF_FLOAT:
    1566           0 :                                 *va_arg(ap, float *) = *(float *)val;
    1567           0 :                                 ret_val = 1;
    1568           0 :                                 break;
    1569           0 :                             case TIFF_DOUBLE:
    1570           0 :                                 *va_arg(ap, double *) = *(double *)val;
    1571           0 :                                 ret_val = 1;
    1572           0 :                                 break;
    1573           0 :                             case TIFF_NOTYPE:
    1574             :                             case TIFF_ASCII:
    1575             :                             default:
    1576           0 :                                 ret_val = 0;
    1577           0 :                                 break;
    1578             :                         }
    1579             :                     }
    1580             :                 }
    1581       87723 :                 break;
    1582             :             }
    1583             :         }
    1584             :     }
    1585     2020760 :     return (ret_val);
    1586             : }
    1587             : 
    1588             : /*
    1589             :  * Return the value of a field in the
    1590             :  * internal directory structure.
    1591             :  */
    1592     2450080 : int TIFFGetField(TIFF *tif, uint32_t tag, ...)
    1593             : {
    1594             :     int status;
    1595             :     va_list ap;
    1596             : 
    1597     2450080 :     va_start(ap, tag);
    1598     2450080 :     status = TIFFVGetField(tif, tag, ap);
    1599     2448230 :     va_end(ap);
    1600     2448230 :     return (status);
    1601             : }
    1602             : 
    1603             : /*
    1604             :  * Like TIFFGetField, but taking a varargs
    1605             :  * parameter list.  This routine is useful
    1606             :  * for building higher-level interfaces on
    1607             :  * top of the library.
    1608             :  */
    1609     2461520 : int TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap)
    1610             : {
    1611     2461520 :     const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
    1612     2463150 :     return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit))
    1613     2054520 :                 ? (*tif->tif_tagmethods.vgetfield)(tif, tag, ap)
    1614     4924370 :                 : 0);
    1615             : }
    1616             : 
    1617             : #define CleanupField(member)                                                   \
    1618             :     {                                                                          \
    1619             :         if (td->member)                                                        \
    1620             :         {                                                                      \
    1621             :             _TIFFfreeExt(tif, td->member);                                     \
    1622             :             td->member = 0;                                                    \
    1623             :         }                                                                      \
    1624             :     }
    1625             : 
    1626             : /*
    1627             :  * Release storage associated with a directory.
    1628             :  */
    1629      208912 : void TIFFFreeDirectory(TIFF *tif)
    1630             : {
    1631      208912 :     TIFFDirectory *td = &tif->tif_dir;
    1632             :     int i;
    1633             : 
    1634      208912 :     (*tif->tif_cleanup)(tif);
    1635      208869 :     _TIFFmemset(td->td_fieldsset, 0, sizeof(td->td_fieldsset));
    1636      208847 :     CleanupField(td_sminsamplevalue);
    1637      208847 :     CleanupField(td_smaxsamplevalue);
    1638      208847 :     CleanupField(td_colormap[0]);
    1639      208847 :     CleanupField(td_colormap[1]);
    1640      208847 :     CleanupField(td_colormap[2]);
    1641      208847 :     CleanupField(td_sampleinfo);
    1642      208847 :     CleanupField(td_subifd);
    1643      208847 :     CleanupField(td_inknames);
    1644      208847 :     CleanupField(td_refblackwhite);
    1645      208847 :     CleanupField(td_transferfunction[0]);
    1646      208847 :     CleanupField(td_transferfunction[1]);
    1647      208847 :     CleanupField(td_transferfunction[2]);
    1648      208847 :     CleanupField(td_stripoffset_p);
    1649      208850 :     CleanupField(td_stripbytecount_p);
    1650      208846 :     td->td_stripoffsetbyteallocsize = 0;
    1651      208846 :     TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);
    1652      208846 :     TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);
    1653             : 
    1654             :     /* Cleanup custom tag values */
    1655      365193 :     for (i = 0; i < td->td_customValueCount; i++)
    1656             :     {
    1657      156346 :         if (td->td_customValues[i].value)
    1658      156347 :             _TIFFfreeExt(tif, td->td_customValues[i].value);
    1659             :     }
    1660             : 
    1661      208847 :     td->td_customValueCount = 0;
    1662      208847 :     CleanupField(td_customValues);
    1663             : 
    1664      208848 :     _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
    1665      208837 :     _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
    1666             : 
    1667             :     /* Reset some internal parameters for IFD data size checking. */
    1668      208834 :     tif->tif_dir.td_dirdatasize_read = 0;
    1669      208834 :     tif->tif_dir.td_dirdatasize_write = 0;
    1670      208834 :     if (tif->tif_dir.td_dirdatasize_offsets != NULL)
    1671             :     {
    1672       54077 :         _TIFFfreeExt(tif, tif->tif_dir.td_dirdatasize_offsets);
    1673       54076 :         tif->tif_dir.td_dirdatasize_offsets = NULL;
    1674       54076 :         tif->tif_dir.td_dirdatasize_Noffsets = 0;
    1675             :     }
    1676      208833 :     tif->tif_dir.td_iswrittentofile = FALSE;
    1677      208833 : }
    1678             : #undef CleanupField
    1679             : 
    1680             : /*
    1681             :  * Client Tag extension support (from Niles Ritter).
    1682             :  */
    1683             : static TIFFExtendProc _TIFFextender = (TIFFExtendProc)NULL;
    1684             : 
    1685        1382 : TIFFExtendProc TIFFSetTagExtender(TIFFExtendProc extender)
    1686             : {
    1687        1382 :     TIFFExtendProc prev = _TIFFextender;
    1688        1382 :     _TIFFextender = extender;
    1689        1382 :     return (prev);
    1690             : }
    1691             : 
    1692             : /*
    1693             :  * Setup for a new directory.  Should we automatically call
    1694             :  * TIFFWriteDirectory() if the current one is dirty?
    1695             :  *
    1696             :  * The newly created directory will not exist on the file till
    1697             :  * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
    1698             :  */
    1699       42612 : int TIFFCreateDirectory(TIFF *tif)
    1700             : {
    1701             :     /* Free previously allocated memory and setup default values. */
    1702       42612 :     TIFFFreeDirectory(tif);
    1703       42609 :     TIFFDefaultDirectory(tif);
    1704       42612 :     tif->tif_diroff = 0;
    1705       42612 :     tif->tif_nextdiroff = 0;
    1706       42612 :     tif->tif_curoff = 0;
    1707       42612 :     tif->tif_row = (uint32_t)-1;
    1708       42612 :     tif->tif_curstrip = (uint32_t)-1;
    1709       42612 :     tif->tif_dir.td_iswrittentofile = FALSE;
    1710             : 
    1711       42612 :     return 0;
    1712             : }
    1713             : 
    1714           0 : int TIFFCreateCustomDirectory(TIFF *tif, const TIFFFieldArray *infoarray)
    1715             : {
    1716             :     /* Free previously allocated memory and setup default values. */
    1717           0 :     TIFFFreeDirectory(tif);
    1718           0 :     TIFFDefaultDirectory(tif);
    1719             : 
    1720             :     /*
    1721             :      * Reset the field definitions to match the application provided list.
    1722             :      * Hopefully TIFFDefaultDirectory() won't have done anything irreversible
    1723             :      * based on it's assumption this is an image directory.
    1724             :      */
    1725           0 :     _TIFFSetupFields(tif, infoarray);
    1726             : 
    1727           0 :     tif->tif_diroff = 0;
    1728           0 :     tif->tif_nextdiroff = 0;
    1729           0 :     tif->tif_curoff = 0;
    1730           0 :     tif->tif_row = (uint32_t)-1;
    1731           0 :     tif->tif_curstrip = (uint32_t)-1;
    1732             :     /* invalidate directory index */
    1733           0 :     tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
    1734             :     /* invalidate IFD loop lists */
    1735           0 :     _TIFFCleanupIFDOffsetAndNumberMaps(tif);
    1736             :     /* To be able to return from SubIFD or custom-IFD to main-IFD */
    1737           0 :     tif->tif_setdirectory_force_absolute = TRUE;
    1738             : 
    1739           0 :     return 0;
    1740             : }
    1741             : 
    1742           0 : int TIFFCreateEXIFDirectory(TIFF *tif)
    1743             : {
    1744             :     const TIFFFieldArray *exifFieldArray;
    1745           0 :     exifFieldArray = _TIFFGetExifFields();
    1746           0 :     return TIFFCreateCustomDirectory(tif, exifFieldArray);
    1747             : }
    1748             : 
    1749             : /*
    1750             :  * Creates the EXIF GPS custom directory
    1751             :  */
    1752           0 : int TIFFCreateGPSDirectory(TIFF *tif)
    1753             : {
    1754             :     const TIFFFieldArray *gpsFieldArray;
    1755           0 :     gpsFieldArray = _TIFFGetGpsFields();
    1756           0 :     return TIFFCreateCustomDirectory(tif, gpsFieldArray);
    1757             : }
    1758             : 
    1759             : /*
    1760             :  * Setup a default directory structure.
    1761             :  */
    1762      136132 : int TIFFDefaultDirectory(TIFF *tif)
    1763             : {
    1764      136132 :     TIFFDirectory *td = &tif->tif_dir;
    1765             :     const TIFFFieldArray *tiffFieldArray;
    1766             : 
    1767      136132 :     tiffFieldArray = _TIFFGetFields();
    1768      136105 :     _TIFFSetupFields(tif, tiffFieldArray);
    1769             : 
    1770      136124 :     _TIFFmemset(td, 0, sizeof(*td));
    1771      136122 :     td->td_fillorder = FILLORDER_MSB2LSB;
    1772      136122 :     td->td_bitspersample = 1;
    1773      136122 :     td->td_threshholding = THRESHHOLD_BILEVEL;
    1774      136122 :     td->td_orientation = ORIENTATION_TOPLEFT;
    1775      136122 :     td->td_samplesperpixel = 1;
    1776      136122 :     td->td_rowsperstrip = (uint32_t)-1;
    1777      136122 :     td->td_tilewidth = 0;
    1778      136122 :     td->td_tilelength = 0;
    1779      136122 :     td->td_tiledepth = 1;
    1780             : #ifdef STRIPBYTECOUNTSORTED_UNUSED
    1781             :     td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */
    1782             : #endif
    1783      136122 :     td->td_resolutionunit = RESUNIT_INCH;
    1784      136122 :     td->td_sampleformat = SAMPLEFORMAT_UINT;
    1785      136122 :     td->td_imagedepth = 1;
    1786      136122 :     td->td_ycbcrsubsampling[0] = 2;
    1787      136122 :     td->td_ycbcrsubsampling[1] = 2;
    1788      136122 :     td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
    1789      136122 :     tif->tif_postdecode = _TIFFNoPostDecode;
    1790      136122 :     tif->tif_foundfield = NULL;
    1791      136122 :     tif->tif_tagmethods.vsetfield = _TIFFVSetField;
    1792      136122 :     tif->tif_tagmethods.vgetfield = _TIFFVGetField;
    1793      136122 :     tif->tif_tagmethods.printdir = NULL;
    1794             :     /* additional default values */
    1795      136122 :     td->td_planarconfig = PLANARCONFIG_CONTIG;
    1796      136122 :     td->td_compression = COMPRESSION_NONE;
    1797      136122 :     td->td_subfiletype = 0;
    1798      136122 :     td->td_minsamplevalue = 0;
    1799             :     /* td_bitspersample=1 is always set in TIFFDefaultDirectory().
    1800             :      * Therefore, td_maxsamplevalue has to be re-calculated in
    1801             :      * TIFFGetFieldDefaulted(). */
    1802      136122 :     td->td_maxsamplevalue = 1; /* Default for td_bitspersample=1 */
    1803      136122 :     td->td_extrasamples = 0;
    1804      136122 :     td->td_sampleinfo = NULL;
    1805             : 
    1806             :     /*
    1807             :      *  Give client code a chance to install their own
    1808             :      *  tag extensions & methods, prior to compression overloads,
    1809             :      *  but do some prior cleanup first.
    1810             :      * (http://trac.osgeo.org/gdal/ticket/5054)
    1811             :      */
    1812      136122 :     if (tif->tif_nfieldscompat > 0)
    1813             :     {
    1814             :         uint32_t i;
    1815             : 
    1816      196661 :         for (i = 0; i < tif->tif_nfieldscompat; i++)
    1817             :         {
    1818      131106 :             if (tif->tif_fieldscompat[i].allocated_size)
    1819      131108 :                 _TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields);
    1820             :         }
    1821       65555 :         _TIFFfreeExt(tif, tif->tif_fieldscompat);
    1822       65555 :         tif->tif_nfieldscompat = 0;
    1823       65555 :         tif->tif_fieldscompat = NULL;
    1824             :     }
    1825      136123 :     if (_TIFFextender)
    1826      136122 :         (*_TIFFextender)(tif);
    1827      136143 :     (void)TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
    1828             :     /*
    1829             :      * NB: The directory is marked dirty as a result of setting
    1830             :      * up the default compression scheme.  However, this really
    1831             :      * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
    1832             :      * if the user does something.  We could just do the setup
    1833             :      * by hand, but it seems better to use the normal mechanism
    1834             :      * (i.e. TIFFSetField).
    1835             :      */
    1836      136060 :     tif->tif_flags &= ~TIFF_DIRTYDIRECT;
    1837             : 
    1838             :     /*
    1839             :      * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
    1840             :      * we clear the ISTILED flag when setting up a new directory.
    1841             :      * Should we also be clearing stuff like INSUBIFD?
    1842             :      */
    1843      136060 :     tif->tif_flags &= ~TIFF_ISTILED;
    1844             : 
    1845      136060 :     return (1);
    1846             : }
    1847             : 
    1848       16472 : static int TIFFAdvanceDirectory(TIFF *tif, uint64_t *nextdiroff, uint64_t *off,
    1849             :                                 tdir_t *nextdirnum)
    1850             : {
    1851             :     static const char module[] = "TIFFAdvanceDirectory";
    1852             : 
    1853             :     /* Add this directory to the directory list, if not already in. */
    1854       16472 :     if (!_TIFFCheckDirNumberAndOffset(tif, *nextdirnum, *nextdiroff))
    1855             :     {
    1856           0 :         TIFFErrorExtR(tif, module,
    1857             :                       "Starting directory %u at offset 0x%" PRIx64 " (%" PRIu64
    1858             :                       ") might cause an IFD loop",
    1859             :                       *nextdirnum, *nextdiroff, *nextdiroff);
    1860           0 :         *nextdiroff = 0;
    1861           0 :         *nextdirnum = 0;
    1862           0 :         return (0);
    1863             :     }
    1864             : 
    1865       16472 :     if (isMapped(tif))
    1866             :     {
    1867           0 :         uint64_t poff = *nextdiroff;
    1868           0 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    1869             :         {
    1870             :             tmsize_t poffa, poffb, poffc, poffd;
    1871             :             uint16_t dircount;
    1872             :             uint32_t nextdir32;
    1873           0 :             poffa = (tmsize_t)poff;
    1874           0 :             poffb = poffa + sizeof(uint16_t);
    1875           0 :             if (((uint64_t)poffa != poff) || (poffb < poffa) ||
    1876           0 :                 (poffb < (tmsize_t)sizeof(uint16_t)) || (poffb > tif->tif_size))
    1877             :             {
    1878           0 :                 TIFFErrorExtR(tif, module,
    1879             :                               "%s:%d: %s: Error fetching directory count",
    1880             :                               __FILE__, __LINE__, tif->tif_name);
    1881           0 :                 *nextdiroff = 0;
    1882           0 :                 return (0);
    1883             :             }
    1884           0 :             _TIFFmemcpy(&dircount, tif->tif_base + poffa, sizeof(uint16_t));
    1885           0 :             if (tif->tif_flags & TIFF_SWAB)
    1886           0 :                 TIFFSwabShort(&dircount);
    1887           0 :             poffc = poffb + dircount * 12;
    1888           0 :             poffd = poffc + sizeof(uint32_t);
    1889           0 :             if ((poffc < poffb) || (poffc < dircount * 12) || (poffd < poffc) ||
    1890           0 :                 (poffd < (tmsize_t)sizeof(uint32_t)) || (poffd > tif->tif_size))
    1891             :             {
    1892           0 :                 TIFFErrorExtR(tif, module, "Error fetching directory link");
    1893           0 :                 return (0);
    1894             :             }
    1895           0 :             if (off != NULL)
    1896           0 :                 *off = (uint64_t)poffc;
    1897           0 :             _TIFFmemcpy(&nextdir32, tif->tif_base + poffc, sizeof(uint32_t));
    1898           0 :             if (tif->tif_flags & TIFF_SWAB)
    1899           0 :                 TIFFSwabLong(&nextdir32);
    1900           0 :             *nextdiroff = nextdir32;
    1901             :         }
    1902             :         else
    1903             :         {
    1904             :             tmsize_t poffa, poffb, poffc, poffd;
    1905             :             uint64_t dircount64;
    1906             :             uint16_t dircount16;
    1907           0 :             if (poff > (uint64_t)TIFF_TMSIZE_T_MAX - sizeof(uint64_t))
    1908             :             {
    1909           0 :                 TIFFErrorExtR(tif, module,
    1910             :                               "%s:%d: %s: Error fetching directory count",
    1911             :                               __FILE__, __LINE__, tif->tif_name);
    1912           0 :                 return (0);
    1913             :             }
    1914           0 :             poffa = (tmsize_t)poff;
    1915           0 :             poffb = poffa + sizeof(uint64_t);
    1916           0 :             if (poffb > tif->tif_size)
    1917             :             {
    1918           0 :                 TIFFErrorExtR(tif, module,
    1919             :                               "%s:%d: %s: Error fetching directory count",
    1920             :                               __FILE__, __LINE__, tif->tif_name);
    1921           0 :                 return (0);
    1922             :             }
    1923           0 :             _TIFFmemcpy(&dircount64, tif->tif_base + poffa, sizeof(uint64_t));
    1924           0 :             if (tif->tif_flags & TIFF_SWAB)
    1925           0 :                 TIFFSwabLong8(&dircount64);
    1926           0 :             if (dircount64 > 0xFFFF)
    1927             :             {
    1928           0 :                 TIFFErrorExtR(tif, module,
    1929             :                               "Sanity check on directory count failed");
    1930           0 :                 return (0);
    1931             :             }
    1932           0 :             dircount16 = (uint16_t)dircount64;
    1933           0 :             if (poffb > TIFF_TMSIZE_T_MAX - (tmsize_t)(dircount16 * 20) -
    1934             :                             (tmsize_t)sizeof(uint64_t))
    1935             :             {
    1936           0 :                 TIFFErrorExtR(tif, module, "Error fetching directory link");
    1937           0 :                 return (0);
    1938             :             }
    1939           0 :             poffc = poffb + dircount16 * 20;
    1940           0 :             poffd = poffc + sizeof(uint64_t);
    1941           0 :             if (poffd > tif->tif_size)
    1942             :             {
    1943           0 :                 TIFFErrorExtR(tif, module, "Error fetching directory link");
    1944           0 :                 return (0);
    1945             :             }
    1946           0 :             if (off != NULL)
    1947           0 :                 *off = (uint64_t)poffc;
    1948           0 :             _TIFFmemcpy(nextdiroff, tif->tif_base + poffc, sizeof(uint64_t));
    1949           0 :             if (tif->tif_flags & TIFF_SWAB)
    1950           0 :                 TIFFSwabLong8(nextdiroff);
    1951             :         }
    1952             :     }
    1953             :     else
    1954             :     {
    1955       16472 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    1956             :         {
    1957             :             uint16_t dircount;
    1958             :             uint32_t nextdir32;
    1959       31270 :             if (!SeekOK(tif, *nextdiroff) ||
    1960       15635 :                 !ReadOK(tif, &dircount, sizeof(uint16_t)))
    1961             :             {
    1962           7 :                 TIFFErrorExtR(tif, module,
    1963             :                               "%s:%d: %s: Error fetching directory count",
    1964             :                               __FILE__, __LINE__, tif->tif_name);
    1965          10 :                 return (0);
    1966             :             }
    1967       15628 :             if (tif->tif_flags & TIFF_SWAB)
    1968         272 :                 TIFFSwabShort(&dircount);
    1969       15628 :             if (off != NULL)
    1970           9 :                 *off = TIFFSeekFile(tif, dircount * 12, SEEK_CUR);
    1971             :             else
    1972       15619 :                 (void)TIFFSeekFile(tif, dircount * 12, SEEK_CUR);
    1973       15628 :             if (!ReadOK(tif, &nextdir32, sizeof(uint32_t)))
    1974             :             {
    1975           3 :                 TIFFErrorExtR(tif, module, "%s: Error fetching directory link",
    1976             :                               tif->tif_name);
    1977           3 :                 return (0);
    1978             :             }
    1979       15625 :             if (tif->tif_flags & TIFF_SWAB)
    1980         272 :                 TIFFSwabLong(&nextdir32);
    1981       15625 :             *nextdiroff = nextdir32;
    1982             :         }
    1983             :         else
    1984             :         {
    1985             :             uint64_t dircount64;
    1986             :             uint16_t dircount16;
    1987        1674 :             if (!SeekOK(tif, *nextdiroff) ||
    1988         837 :                 !ReadOK(tif, &dircount64, sizeof(uint64_t)))
    1989             :             {
    1990           0 :                 TIFFErrorExtR(tif, module,
    1991             :                               "%s:%d: %s: Error fetching directory count",
    1992             :                               __FILE__, __LINE__, tif->tif_name);
    1993           0 :                 return (0);
    1994             :             }
    1995         837 :             if (tif->tif_flags & TIFF_SWAB)
    1996           4 :                 TIFFSwabLong8(&dircount64);
    1997         837 :             if (dircount64 > 0xFFFF)
    1998             :             {
    1999           0 :                 TIFFErrorExtR(tif, module,
    2000             :                               "%s:%d: %s: Error fetching directory count",
    2001             :                               __FILE__, __LINE__, tif->tif_name);
    2002           0 :                 return (0);
    2003             :             }
    2004         837 :             dircount16 = (uint16_t)dircount64;
    2005         837 :             if (off != NULL)
    2006           0 :                 *off = TIFFSeekFile(tif, dircount16 * 20, SEEK_CUR);
    2007             :             else
    2008         837 :                 (void)TIFFSeekFile(tif, dircount16 * 20, SEEK_CUR);
    2009         837 :             if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
    2010             :             {
    2011           0 :                 TIFFErrorExtR(tif, module, "%s: Error fetching directory link",
    2012             :                               tif->tif_name);
    2013           0 :                 return (0);
    2014             :             }
    2015         837 :             if (tif->tif_flags & TIFF_SWAB)
    2016           4 :                 TIFFSwabLong8(nextdiroff);
    2017             :         }
    2018             :     }
    2019       16462 :     if (*nextdiroff != 0)
    2020             :     {
    2021        5824 :         (*nextdirnum)++;
    2022             :         /* Check next directory for IFD looping and if so, set it as last
    2023             :          * directory. */
    2024        5824 :         if (!_TIFFCheckDirNumberAndOffset(tif, *nextdirnum, *nextdiroff))
    2025             :         {
    2026           0 :             TIFFWarningExtR(
    2027             :                 tif, module,
    2028             :                 "the next directory %u at offset 0x%" PRIx64 " (%" PRIu64
    2029             :                 ") might be an IFD loop. Treating directory %d as "
    2030             :                 "last directory",
    2031           0 :                 *nextdirnum, *nextdiroff, *nextdiroff, (int)(*nextdirnum) - 1);
    2032           0 :             *nextdiroff = 0;
    2033           0 :             (*nextdirnum)--;
    2034             :         }
    2035             :     }
    2036       16462 :     return (1);
    2037             : }
    2038             : 
    2039             : /*
    2040             :  * Count the number of directories in a file.
    2041             :  */
    2042       10642 : tdir_t TIFFNumberOfDirectories(TIFF *tif)
    2043             : {
    2044             :     uint64_t nextdiroff;
    2045             :     tdir_t nextdirnum;
    2046             :     tdir_t n;
    2047       10642 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    2048       10365 :         nextdiroff = tif->tif_header.classic.tiff_diroff;
    2049             :     else
    2050         277 :         nextdiroff = tif->tif_header.big.tiff_diroff;
    2051       10642 :     nextdirnum = 0;
    2052       10642 :     n = 0;
    2053       43546 :     while (nextdiroff != 0 &&
    2054       16457 :            TIFFAdvanceDirectory(tif, &nextdiroff, NULL, &nextdirnum))
    2055             :     {
    2056       16447 :         ++n;
    2057             :     }
    2058             :     /* Update number of main-IFDs in file. */
    2059       10642 :     tif->tif_curdircount = n;
    2060       10642 :     return (n);
    2061             : }
    2062             : 
    2063             : /*
    2064             :  * Set the n-th directory as the current directory.
    2065             :  * NB: Directories are numbered starting at 0.
    2066             :  */
    2067       10811 : int TIFFSetDirectory(TIFF *tif, tdir_t dirn)
    2068             : {
    2069             :     uint64_t nextdiroff;
    2070       10811 :     tdir_t nextdirnum = 0;
    2071             :     tdir_t n;
    2072             : 
    2073       10811 :     if (tif->tif_setdirectory_force_absolute)
    2074             :     {
    2075             :         /* tif_setdirectory_force_absolute=1 will force parsing the main IFD
    2076             :          * chain from the beginning, thus IFD directory list needs to be cleared
    2077             :          * from possible SubIFD offsets.
    2078             :          */
    2079           0 :         _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */
    2080             :     }
    2081             : 
    2082             :     /* Even faster path, if offset is available within IFD loop hash list. */
    2083       21622 :     if (!tif->tif_setdirectory_force_absolute &&
    2084       10811 :         _TIFFGetOffsetFromDirNumber(tif, dirn, &nextdiroff))
    2085             :     {
    2086             :         /* Set parameters for following TIFFReadDirectory() below. */
    2087       10811 :         tif->tif_nextdiroff = nextdiroff;
    2088       10811 :         tif->tif_curdir = dirn;
    2089             :         /* Reset to relative stepping */
    2090       10811 :         tif->tif_setdirectory_force_absolute = FALSE;
    2091             :     }
    2092             :     else
    2093             :     {
    2094             : 
    2095             :         /* Fast path when we just advance relative to the current directory:
    2096             :          * start at the current dir offset and continue to seek from there.
    2097             :          * Check special cases when relative is not allowed:
    2098             :          * - jump back from SubIFD or custom directory
    2099             :          * - right after TIFFWriteDirectory() jump back to that directory
    2100             :          *   using TIFFSetDirectory() */
    2101           0 :         const int relative = (dirn >= tif->tif_curdir) &&
    2102           0 :                              (tif->tif_diroff != 0) &&
    2103           0 :                              !tif->tif_setdirectory_force_absolute;
    2104             : 
    2105           0 :         if (relative)
    2106             :         {
    2107           0 :             nextdiroff = tif->tif_diroff;
    2108           0 :             dirn -= tif->tif_curdir;
    2109           0 :             nextdirnum = tif->tif_curdir;
    2110             :         }
    2111           0 :         else if (!(tif->tif_flags & TIFF_BIGTIFF))
    2112           0 :             nextdiroff = tif->tif_header.classic.tiff_diroff;
    2113             :         else
    2114           0 :             nextdiroff = tif->tif_header.big.tiff_diroff;
    2115             : 
    2116             :         /* Reset to relative stepping */
    2117           0 :         tif->tif_setdirectory_force_absolute = FALSE;
    2118             : 
    2119           0 :         for (n = dirn; n > 0 && nextdiroff != 0; n--)
    2120           0 :             if (!TIFFAdvanceDirectory(tif, &nextdiroff, NULL, &nextdirnum))
    2121           0 :                 return (0);
    2122             :         /* If the n-th directory could not be reached (does not exist),
    2123             :          * return here without touching anything further. */
    2124           0 :         if (nextdiroff == 0 || n > 0)
    2125           0 :             return (0);
    2126             : 
    2127           0 :         tif->tif_nextdiroff = nextdiroff;
    2128             : 
    2129             :         /* Set curdir to the actual directory index. */
    2130           0 :         if (relative)
    2131           0 :             tif->tif_curdir += dirn - n;
    2132             :         else
    2133           0 :             tif->tif_curdir = dirn - n;
    2134             :     }
    2135             : 
    2136             :     /* The -1 decrement is because TIFFReadDirectory will increment
    2137             :      * tif_curdir after successfully reading the directory. */
    2138       10811 :     if (tif->tif_curdir == 0)
    2139       10038 :         tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
    2140             :     else
    2141         773 :         tif->tif_curdir--;
    2142             : 
    2143       10811 :     tdir_t curdir = tif->tif_curdir;
    2144             : 
    2145       10811 :     int retval = TIFFReadDirectory(tif);
    2146             : 
    2147       10811 :     if (!retval && tif->tif_curdir == curdir)
    2148             :     {
    2149             :         /* If tif_curdir has not be incremented, TIFFFetchDirectory() in
    2150             :          * TIFFReadDirectory() has failed and tif_curdir shall be set
    2151             :          * specifically. */
    2152           4 :         tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
    2153             :     }
    2154       10811 :     return (retval);
    2155             : }
    2156             : 
    2157             : /*
    2158             :  * Set the current directory to be the directory
    2159             :  * located at the specified file offset.  This interface
    2160             :  * is used mainly to access directories linked with
    2161             :  * the SubIFD tag (e.g. thumbnail images).
    2162             :  */
    2163       19486 : int TIFFSetSubDirectory(TIFF *tif, uint64_t diroff)
    2164             : {
    2165             :     /* Match nextdiroff and curdir for consistent IFD-loop checking.
    2166             :      * Only with TIFFSetSubDirectory() the IFD list can be corrupted with
    2167             :      * invalid offsets within the main IFD tree. In the case of several subIFDs
    2168             :      * of a main image, there are two possibilities that are not even mutually
    2169             :      * exclusive. a.) The subIFD tag contains an array with all offsets of the
    2170             :      * subIFDs. b.) The SubIFDs are concatenated with their NextIFD parameters.
    2171             :      * (refer to
    2172             :      * https://www.awaresystems.be/imaging/tiff/specification/TIFFPM6.pdf.)
    2173             :      */
    2174             :     int retval;
    2175       19486 :     uint32_t curdir = 0;
    2176       19486 :     int8_t probablySubIFD = 0;
    2177       19486 :     if (diroff == 0)
    2178             :     {
    2179             :         /* Special case to set tif_diroff=0, which is done in
    2180             :          * TIFFReadDirectory() below to indicate that the currently read IFD is
    2181             :          * treated as a new, fresh IFD. */
    2182        8498 :         tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
    2183        8498 :         tif->tif_dir.td_iswrittentofile = FALSE;
    2184             :     }
    2185             :     else
    2186             :     {
    2187       10988 :         if (!_TIFFGetDirNumberFromOffset(tif, diroff, &curdir))
    2188             :         {
    2189             :             /* Non-existing offsets might point to a SubIFD or invalid IFD.*/
    2190          59 :             probablySubIFD = 1;
    2191             :         }
    2192             :         /* -1 because TIFFReadDirectory() will increment tif_curdir. */
    2193       10988 :         if (curdir >= 1)
    2194        1784 :             tif->tif_curdir = curdir - 1;
    2195             :         else
    2196        9204 :             tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
    2197             :     }
    2198       19486 :     curdir = tif->tif_curdir;
    2199             : 
    2200       19486 :     tif->tif_nextdiroff = diroff;
    2201       19486 :     retval = TIFFReadDirectory(tif);
    2202             : 
    2203             :     /* tif_curdir is incremented in TIFFReadDirectory(), but if it has not been
    2204             :      * incremented, TIFFFetchDirectory() has failed there and tif_curdir shall
    2205             :      * be set specifically. */
    2206       19486 :     if (!retval && diroff != 0 && tif->tif_curdir == curdir)
    2207             :     {
    2208           1 :         tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
    2209             :     }
    2210             : 
    2211       19486 :     if (probablySubIFD)
    2212             :     {
    2213          59 :         if (retval)
    2214             :         {
    2215             :             /* Reset IFD list to start new one for SubIFD chain and also start
    2216             :              * SubIFD chain with tif_curdir=0 for IFD loop checking. */
    2217             :             /* invalidate IFD loop lists */
    2218          58 :             _TIFFCleanupIFDOffsetAndNumberMaps(tif);
    2219          58 :             tif->tif_curdir = 0; /* first directory of new chain */
    2220             :             /* add this offset to new IFD list */
    2221          58 :             retval = _TIFFCheckDirNumberAndOffset(tif, tif->tif_curdir, diroff);
    2222             :         }
    2223             :         /* To be able to return from SubIFD or custom-IFD to main-IFD */
    2224          59 :         tif->tif_setdirectory_force_absolute = TRUE;
    2225             :     }
    2226             : 
    2227       19486 :     return (retval);
    2228             : }
    2229             : 
    2230             : /*
    2231             :  * Return file offset of the current directory.
    2232             :  */
    2233      134952 : uint64_t TIFFCurrentDirOffset(TIFF *tif) { return (tif->tif_diroff); }
    2234             : 
    2235             : /*
    2236             :  * Return an indication of whether or not we are
    2237             :  * at the last directory in the file.
    2238             :  */
    2239        9260 : int TIFFLastDirectory(TIFF *tif) { return (tif->tif_nextdiroff == 0); }
    2240             : 
    2241             : /*
    2242             :  * Unlink the specified directory from the directory chain.
    2243             :  * Note: First directory starts with number dirn=1.
    2244             :  * This is different to TIFFSetDirectory() where the first directory starts with
    2245             :  * zero.
    2246             :  */
    2247           6 : int TIFFUnlinkDirectory(TIFF *tif, tdir_t dirn)
    2248             : {
    2249             :     static const char module[] = "TIFFUnlinkDirectory";
    2250             :     uint64_t nextdir;
    2251             :     tdir_t nextdirnum;
    2252             :     uint64_t off;
    2253             :     tdir_t n;
    2254             : 
    2255           6 :     if (tif->tif_mode == O_RDONLY)
    2256             :     {
    2257           0 :         TIFFErrorExtR(tif, module,
    2258             :                       "Can not unlink directory in read-only file");
    2259           0 :         return (0);
    2260             :     }
    2261           6 :     if (dirn == 0)
    2262             :     {
    2263           0 :         TIFFErrorExtR(tif, module,
    2264             :                       "For TIFFUnlinkDirectory() first directory starts with "
    2265             :                       "number 1 and not 0");
    2266           0 :         return (0);
    2267             :     }
    2268             :     /*
    2269             :      * Go to the directory before the one we want
    2270             :      * to unlink and nab the offset of the link
    2271             :      * field we'll need to patch.
    2272             :      */
    2273           6 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    2274             :     {
    2275           6 :         nextdir = tif->tif_header.classic.tiff_diroff;
    2276           6 :         off = 4;
    2277             :     }
    2278             :     else
    2279             :     {
    2280           0 :         nextdir = tif->tif_header.big.tiff_diroff;
    2281           0 :         off = 8;
    2282             :     }
    2283           6 :     nextdirnum = 0; /* First directory is dirn=0 */
    2284             : 
    2285          15 :     for (n = dirn - 1; n > 0; n--)
    2286             :     {
    2287           9 :         if (nextdir == 0)
    2288             :         {
    2289           0 :             TIFFErrorExtR(tif, module, "Directory %u does not exist", dirn);
    2290           0 :             return (0);
    2291             :         }
    2292           9 :         if (!TIFFAdvanceDirectory(tif, &nextdir, &off, &nextdirnum))
    2293           0 :             return (0);
    2294             :     }
    2295             :     /*
    2296             :      * Advance to the directory to be unlinked and fetch
    2297             :      * the offset of the directory that follows.
    2298             :      */
    2299           6 :     if (!TIFFAdvanceDirectory(tif, &nextdir, NULL, &nextdirnum))
    2300           0 :         return (0);
    2301             :     /*
    2302             :      * Go back and patch the link field of the preceding
    2303             :      * directory to point to the offset of the directory
    2304             :      * that follows.
    2305             :      */
    2306           6 :     (void)TIFFSeekFile(tif, off, SEEK_SET);
    2307           6 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    2308             :     {
    2309             :         uint32_t nextdir32;
    2310           6 :         nextdir32 = (uint32_t)nextdir;
    2311           6 :         assert((uint64_t)nextdir32 == nextdir);
    2312           6 :         if (tif->tif_flags & TIFF_SWAB)
    2313           0 :             TIFFSwabLong(&nextdir32);
    2314           6 :         if (!WriteOK(tif, &nextdir32, sizeof(uint32_t)))
    2315             :         {
    2316           0 :             TIFFErrorExtR(tif, module, "Error writing directory link");
    2317           0 :             return (0);
    2318             :         }
    2319             :     }
    2320             :     else
    2321             :     {
    2322             :         /* Need local swap because nextdir has to be used unswapped below. */
    2323           0 :         uint64_t nextdir64 = nextdir;
    2324           0 :         if (tif->tif_flags & TIFF_SWAB)
    2325           0 :             TIFFSwabLong8(&nextdir64);
    2326           0 :         if (!WriteOK(tif, &nextdir64, sizeof(uint64_t)))
    2327             :         {
    2328           0 :             TIFFErrorExtR(tif, module, "Error writing directory link");
    2329           0 :             return (0);
    2330             :         }
    2331             :     }
    2332             : 
    2333             :     /* For dirn=1 (first directory) also update the libtiff internal
    2334             :      * base offset variables. */
    2335           6 :     if (dirn == 1)
    2336             :     {
    2337           0 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    2338           0 :             tif->tif_header.classic.tiff_diroff = (uint32_t)nextdir;
    2339             :         else
    2340           0 :             tif->tif_header.big.tiff_diroff = nextdir;
    2341             :     }
    2342             : 
    2343             :     /*
    2344             :      * Leave directory state setup safely.  We don't have
    2345             :      * facilities for doing inserting and removing directories,
    2346             :      * so it's safest to just invalidate everything.  This
    2347             :      * means that the caller can only append to the directory
    2348             :      * chain.
    2349             :      */
    2350           6 :     if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
    2351             :     {
    2352           0 :         _TIFFfreeExt(tif, tif->tif_rawdata);
    2353           0 :         tif->tif_rawdata = NULL;
    2354           0 :         tif->tif_rawcc = 0;
    2355           0 :         tif->tif_rawcp = NULL;
    2356           0 :         tif->tif_rawdataoff = 0;
    2357           0 :         tif->tif_rawdataloaded = 0;
    2358             :     }
    2359           6 :     tif->tif_flags &= ~(TIFF_BEENWRITING | TIFF_BUFFERSETUP | TIFF_POSTENCODE |
    2360             :                         TIFF_BUF4WRITE);
    2361           6 :     TIFFFreeDirectory(tif);
    2362           6 :     TIFFDefaultDirectory(tif);
    2363           6 :     tif->tif_diroff = 0;     /* force link on next write */
    2364           6 :     tif->tif_nextdiroff = 0; /* next write must be at end */
    2365           6 :     tif->tif_lastdiroff = 0; /* will be updated on next link */
    2366           6 :     tif->tif_curoff = 0;
    2367           6 :     tif->tif_row = (uint32_t)-1;
    2368           6 :     tif->tif_curstrip = (uint32_t)-1;
    2369           6 :     tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
    2370           6 :     if (tif->tif_curdircount > 0)
    2371           6 :         tif->tif_curdircount--;
    2372             :     else
    2373           0 :         tif->tif_curdircount = TIFF_NON_EXISTENT_DIR_NUMBER;
    2374           6 :     _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */
    2375           6 :     return (1);
    2376             : }

Generated by: LCOV version 1.14