LCOV - code coverage report
Current view: top level - frmts/gtiff/libtiff - tif_dirread.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 1466 4221 34.7 %
Date: 2026-08-15 20:04:23 Functions: 58 120 48.3 %

          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 Read Support Routines.
      29             :  */
      30             : 
      31             : /* Suggested pending improvements:
      32             :  * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
      33             :  *   the pointer to the appropriate TIFFField structure early on in
      34             :  *   TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
      35             :  */
      36             : 
      37             : #include "tiffconf.h"
      38             : #include "tiffiop.h"
      39             : #include <float.h>
      40             : #include <limits.h>
      41             : #include <stdlib.h>
      42             : #include <string.h>
      43             : 
      44             : #define FAILED_FII ((uint32_t)-1)
      45             : 
      46             : #ifdef HAVE_IEEEFP
      47             : #define TIFFCvtIEEEFloatToNative(tif, n, fp)
      48             : #define TIFFCvtIEEEDoubleToNative(tif, n, dp)
      49             : #else
      50             : /* If your machine does not support IEEE floating point then you will need to
      51             :  * add support to tif_machdep.c to convert between the native format and
      52             :  * IEEE format. */
      53             : extern void TIFFCvtIEEEFloatToNative(TIFF *, uint32_t, float *);
      54             : extern void TIFFCvtIEEEDoubleToNative(TIFF *, uint32_t, double *);
      55             : #endif
      56             : 
      57             : enum TIFFReadDirEntryErr
      58             : {
      59             :     TIFFReadDirEntryErrOk = 0,
      60             :     TIFFReadDirEntryErrCount = 1,
      61             :     TIFFReadDirEntryErrType = 2,
      62             :     TIFFReadDirEntryErrIo = 3,
      63             :     TIFFReadDirEntryErrRange = 4,
      64             :     TIFFReadDirEntryErrPsdif = 5,
      65             :     TIFFReadDirEntryErrSizesan = 6,
      66             :     TIFFReadDirEntryErrAlloc = 7,
      67             : };
      68             : 
      69             : static enum TIFFReadDirEntryErr
      70             : TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value);
      71             : static enum TIFFReadDirEntryErr
      72             : TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value);
      73             : static enum TIFFReadDirEntryErr
      74             : TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value);
      75             : static enum TIFFReadDirEntryErr
      76             : TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value);
      77             : static enum TIFFReadDirEntryErr
      78             : TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value);
      79             : static enum TIFFReadDirEntryErr
      80             : TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value);
      81             : static enum TIFFReadDirEntryErr
      82             : TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value);
      83             : static enum TIFFReadDirEntryErr
      84             : TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value);
      85             : static enum TIFFReadDirEntryErr
      86             : TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value);
      87             : static enum TIFFReadDirEntryErr
      88             : TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
      89             : static enum TIFFReadDirEntryErr
      90             : TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value);
      91             : 
      92             : static enum TIFFReadDirEntryErr
      93             : TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
      94             :                       uint32_t desttypesize, void **value);
      95             : static enum TIFFReadDirEntryErr
      96             : TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value);
      97             : static enum TIFFReadDirEntryErr
      98             : TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value);
      99             : static enum TIFFReadDirEntryErr
     100             : TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value);
     101             : static enum TIFFReadDirEntryErr
     102             : TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value);
     103             : static enum TIFFReadDirEntryErr
     104             : TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value);
     105             : static enum TIFFReadDirEntryErr
     106             : TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value);
     107             : static enum TIFFReadDirEntryErr
     108             : TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value);
     109             : static enum TIFFReadDirEntryErr
     110             : TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value);
     111             : static enum TIFFReadDirEntryErr
     112             : TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value);
     113             : static enum TIFFReadDirEntryErr
     114             : TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value);
     115             : static enum TIFFReadDirEntryErr
     116             : TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value);
     117             : 
     118             : static enum TIFFReadDirEntryErr
     119             : TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
     120             :                                uint16_t *value);
     121             : 
     122             : static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
     123             :                                         uint8_t *value);
     124             : static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
     125             :                                          int8_t *value);
     126             : static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
     127             :                                          uint16_t *value);
     128             : static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
     129             :                                           int16_t *value);
     130             : static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
     131             :                                         uint32_t *value);
     132             : static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
     133             :                                          int32_t *value);
     134             : static enum TIFFReadDirEntryErr
     135             : TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry,
     136             :                              uint64_t *value);
     137             : static enum TIFFReadDirEntryErr
     138             : TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry,
     139             :                               int64_t *value);
     140             : static enum TIFFReadDirEntryErr
     141             : TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
     142             :                                 double *value);
     143             : static enum TIFFReadDirEntryErr
     144             : TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
     145             :                                  double *value);
     146             : static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
     147             :                                          float *value);
     148             : static enum TIFFReadDirEntryErr
     149             : TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
     150             : #if 0
     151             : static enum TIFFReadDirEntryErr
     152             : TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
     153             :                                       TIFFRational_t *value);
     154             : #endif
     155             : static enum TIFFReadDirEntryErr
     156             : TIFFReadDirEntryCheckRangeByteSbyte(int8_t value);
     157             : static enum TIFFReadDirEntryErr
     158             : TIFFReadDirEntryCheckRangeByteShort(uint16_t value);
     159             : static enum TIFFReadDirEntryErr
     160             : TIFFReadDirEntryCheckRangeByteSshort(int16_t value);
     161             : static enum TIFFReadDirEntryErr
     162             : TIFFReadDirEntryCheckRangeByteLong(uint32_t value);
     163             : static enum TIFFReadDirEntryErr
     164             : TIFFReadDirEntryCheckRangeByteSlong(int32_t value);
     165             : static enum TIFFReadDirEntryErr
     166             : TIFFReadDirEntryCheckRangeByteLong8(uint64_t value);
     167             : static enum TIFFReadDirEntryErr
     168             : TIFFReadDirEntryCheckRangeByteSlong8(int64_t value);
     169             : 
     170             : static enum TIFFReadDirEntryErr
     171             : TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value);
     172             : static enum TIFFReadDirEntryErr
     173             : TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value);
     174             : static enum TIFFReadDirEntryErr
     175             : TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value);
     176             : static enum TIFFReadDirEntryErr
     177             : TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value);
     178             : static enum TIFFReadDirEntryErr
     179             : TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value);
     180             : static enum TIFFReadDirEntryErr
     181             : TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value);
     182             : static enum TIFFReadDirEntryErr
     183             : TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value);
     184             : 
     185             : static enum TIFFReadDirEntryErr
     186             : TIFFReadDirEntryCheckRangeShortSbyte(int8_t value);
     187             : static enum TIFFReadDirEntryErr
     188             : TIFFReadDirEntryCheckRangeShortSshort(int16_t value);
     189             : static enum TIFFReadDirEntryErr
     190             : TIFFReadDirEntryCheckRangeShortLong(uint32_t value);
     191             : static enum TIFFReadDirEntryErr
     192             : TIFFReadDirEntryCheckRangeShortSlong(int32_t value);
     193             : static enum TIFFReadDirEntryErr
     194             : TIFFReadDirEntryCheckRangeShortLong8(uint64_t value);
     195             : static enum TIFFReadDirEntryErr
     196             : TIFFReadDirEntryCheckRangeShortSlong8(int64_t value);
     197             : 
     198             : static enum TIFFReadDirEntryErr
     199             : TIFFReadDirEntryCheckRangeSshortShort(uint16_t value);
     200             : static enum TIFFReadDirEntryErr
     201             : TIFFReadDirEntryCheckRangeSshortLong(uint32_t value);
     202             : static enum TIFFReadDirEntryErr
     203             : TIFFReadDirEntryCheckRangeSshortSlong(int32_t value);
     204             : static enum TIFFReadDirEntryErr
     205             : TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value);
     206             : static enum TIFFReadDirEntryErr
     207             : TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value);
     208             : 
     209             : static enum TIFFReadDirEntryErr
     210             : TIFFReadDirEntryCheckRangeLongSbyte(int8_t value);
     211             : static enum TIFFReadDirEntryErr
     212             : TIFFReadDirEntryCheckRangeLongSshort(int16_t value);
     213             : static enum TIFFReadDirEntryErr
     214             : TIFFReadDirEntryCheckRangeLongSlong(int32_t value);
     215             : static enum TIFFReadDirEntryErr
     216             : TIFFReadDirEntryCheckRangeLongLong8(uint64_t value);
     217             : static enum TIFFReadDirEntryErr
     218             : TIFFReadDirEntryCheckRangeLongSlong8(int64_t value);
     219             : 
     220             : static enum TIFFReadDirEntryErr
     221             : TIFFReadDirEntryCheckRangeSlongLong(uint32_t value);
     222             : static enum TIFFReadDirEntryErr
     223             : TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value);
     224             : static enum TIFFReadDirEntryErr
     225             : TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value);
     226             : 
     227             : static enum TIFFReadDirEntryErr
     228             : TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value);
     229             : static enum TIFFReadDirEntryErr
     230             : TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value);
     231             : static enum TIFFReadDirEntryErr
     232             : TIFFReadDirEntryCheckRangeLong8Slong(int32_t value);
     233             : static enum TIFFReadDirEntryErr
     234             : TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value);
     235             : 
     236             : static enum TIFFReadDirEntryErr
     237             : TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value);
     238             : 
     239             : static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
     240             :                                                      tmsize_t size, void *dest);
     241             : static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
     242             :                                       const char *module, const char *tagname,
     243             :                                       int recover);
     244             : 
     245             : static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
     246             :                                         uint16_t dircount);
     247             : static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
     248             :                                                 uint16_t dircount,
     249             :                                                 uint16_t tagid);
     250             : static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
     251             :                                            uint32_t *fii);
     252             : 
     253             : static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
     254             :                                    uint16_t dircount);
     255             : static void MissingRequired(TIFF *, const char *);
     256             : static int CheckDirCount(TIFF *, TIFFDirEntry *, uint32_t);
     257             : static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
     258             :                                    TIFFDirEntry **pdir, uint64_t *nextdiroff);
     259             : static int TIFFFetchNormalTag(TIFF *, TIFFDirEntry *, int recover);
     260             : static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
     261             :                                uint64_t **lpp);
     262             : static int TIFFFetchSubjectDistance(TIFF *, TIFFDirEntry *);
     263             : static void ChopUpSingleUncompressedStrip(TIFF *);
     264             : static void TryChopUpUncompressedBigTiff(TIFF *);
     265             : static uint64_t TIFFReadUInt64(const uint8_t *value);
     266             : static int _TIFFGetMaxColorChannels(uint16_t photometric);
     267             : 
     268             : static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount);
     269             : 
     270             : typedef union _UInt64Aligned_t
     271             : {
     272             :     double d;
     273             :     uint64_t l;
     274             :     uint32_t i[2];
     275             :     uint16_t s[4];
     276             :     uint8_t c[8];
     277             : } UInt64Aligned_t;
     278             : 
     279             : /*
     280             :   Unaligned safe copy of a uint64_t value from an octet array.
     281             : */
     282       28108 : static uint64_t TIFFReadUInt64(const uint8_t *value)
     283             : {
     284             :     UInt64Aligned_t result;
     285             : 
     286       28108 :     result.c[0] = value[0];
     287       28108 :     result.c[1] = value[1];
     288       28108 :     result.c[2] = value[2];
     289       28108 :     result.c[3] = value[3];
     290       28108 :     result.c[4] = value[4];
     291       28108 :     result.c[5] = value[5];
     292       28108 :     result.c[6] = value[6];
     293       28108 :     result.c[7] = value[7];
     294             : 
     295       28108 :     return result.l;
     296             : }
     297             : 
     298             : static enum TIFFReadDirEntryErr
     299           0 : TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value)
     300             : {
     301             :     enum TIFFReadDirEntryErr err;
     302           0 :     if (direntry->tdir_count != 1)
     303           0 :         return (TIFFReadDirEntryErrCount);
     304           0 :     switch (direntry->tdir_type)
     305             :     {
     306           0 :         case TIFF_BYTE:
     307             :         case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
     308             :                                 field_readcount==1 */
     309           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, value);
     310           0 :             return (TIFFReadDirEntryErrOk);
     311           0 :         case TIFF_SBYTE:
     312             :         {
     313             :             int8_t m;
     314           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     315           0 :             err = TIFFReadDirEntryCheckRangeByteSbyte(m);
     316           0 :             if (err != TIFFReadDirEntryErrOk)
     317           0 :                 return (err);
     318           0 :             *value = (uint8_t)m;
     319           0 :             return (TIFFReadDirEntryErrOk);
     320             :         }
     321           0 :         case TIFF_SHORT:
     322             :         {
     323             :             uint16_t m;
     324           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     325           0 :             err = TIFFReadDirEntryCheckRangeByteShort(m);
     326           0 :             if (err != TIFFReadDirEntryErrOk)
     327           0 :                 return (err);
     328           0 :             *value = (uint8_t)m;
     329           0 :             return (TIFFReadDirEntryErrOk);
     330             :         }
     331           0 :         case TIFF_SSHORT:
     332             :         {
     333             :             int16_t m;
     334           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     335           0 :             err = TIFFReadDirEntryCheckRangeByteSshort(m);
     336           0 :             if (err != TIFFReadDirEntryErrOk)
     337           0 :                 return (err);
     338           0 :             *value = (uint8_t)m;
     339           0 :             return (TIFFReadDirEntryErrOk);
     340             :         }
     341           0 :         case TIFF_LONG:
     342             :         {
     343             :             uint32_t m;
     344           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     345           0 :             err = TIFFReadDirEntryCheckRangeByteLong(m);
     346           0 :             if (err != TIFFReadDirEntryErrOk)
     347           0 :                 return (err);
     348           0 :             *value = (uint8_t)m;
     349           0 :             return (TIFFReadDirEntryErrOk);
     350             :         }
     351           0 :         case TIFF_SLONG:
     352             :         {
     353             :             int32_t m;
     354           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     355           0 :             err = TIFFReadDirEntryCheckRangeByteSlong(m);
     356           0 :             if (err != TIFFReadDirEntryErrOk)
     357           0 :                 return (err);
     358           0 :             *value = (uint8_t)m;
     359           0 :             return (TIFFReadDirEntryErrOk);
     360             :         }
     361           0 :         case TIFF_LONG8:
     362             :         {
     363             :             uint64_t m;
     364           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     365           0 :             if (err != TIFFReadDirEntryErrOk)
     366           0 :                 return (err);
     367           0 :             err = TIFFReadDirEntryCheckRangeByteLong8(m);
     368           0 :             if (err != TIFFReadDirEntryErrOk)
     369           0 :                 return (err);
     370           0 :             *value = (uint8_t)m;
     371           0 :             return (TIFFReadDirEntryErrOk);
     372             :         }
     373           0 :         case TIFF_SLONG8:
     374             :         {
     375             :             int64_t m;
     376           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     377           0 :             if (err != TIFFReadDirEntryErrOk)
     378           0 :                 return (err);
     379           0 :             err = TIFFReadDirEntryCheckRangeByteSlong8(m);
     380           0 :             if (err != TIFFReadDirEntryErrOk)
     381           0 :                 return (err);
     382           0 :             *value = (uint8_t)m;
     383           0 :             return (TIFFReadDirEntryErrOk);
     384             :         }
     385           0 :         default:
     386           0 :             return (TIFFReadDirEntryErrType);
     387             :     }
     388             : }
     389             : 
     390             : static enum TIFFReadDirEntryErr
     391           0 : TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value)
     392             : {
     393             :     enum TIFFReadDirEntryErr err;
     394           0 :     if (direntry->tdir_count != 1)
     395           0 :         return (TIFFReadDirEntryErrCount);
     396           0 :     switch (direntry->tdir_type)
     397             :     {
     398           0 :         case TIFF_BYTE:
     399             :         case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
     400             :                                 field_readcount==1 */
     401             :         {
     402             :             uint8_t m;
     403           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     404           0 :             err = TIFFReadDirEntryCheckRangeSbyteByte(m);
     405           0 :             if (err != TIFFReadDirEntryErrOk)
     406           0 :                 return (err);
     407           0 :             *value = (int8_t)m;
     408           0 :             return (TIFFReadDirEntryErrOk);
     409             :         }
     410           0 :         case TIFF_SBYTE:
     411             :         {
     412           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, value);
     413           0 :             return (TIFFReadDirEntryErrOk);
     414             :         }
     415           0 :         case TIFF_SHORT:
     416             :         {
     417             :             uint16_t m;
     418           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     419           0 :             err = TIFFReadDirEntryCheckRangeSbyteShort(m);
     420           0 :             if (err != TIFFReadDirEntryErrOk)
     421           0 :                 return (err);
     422           0 :             *value = (int8_t)m;
     423           0 :             return (TIFFReadDirEntryErrOk);
     424             :         }
     425           0 :         case TIFF_SSHORT:
     426             :         {
     427             :             int16_t m;
     428           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     429           0 :             err = TIFFReadDirEntryCheckRangeSbyteSshort(m);
     430           0 :             if (err != TIFFReadDirEntryErrOk)
     431           0 :                 return (err);
     432           0 :             *value = (int8_t)m;
     433           0 :             return (TIFFReadDirEntryErrOk);
     434             :         }
     435           0 :         case TIFF_LONG:
     436             :         {
     437             :             uint32_t m;
     438           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     439           0 :             err = TIFFReadDirEntryCheckRangeSbyteLong(m);
     440           0 :             if (err != TIFFReadDirEntryErrOk)
     441           0 :                 return (err);
     442           0 :             *value = (int8_t)m;
     443           0 :             return (TIFFReadDirEntryErrOk);
     444             :         }
     445           0 :         case TIFF_SLONG:
     446             :         {
     447             :             int32_t m;
     448           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     449           0 :             err = TIFFReadDirEntryCheckRangeSbyteSlong(m);
     450           0 :             if (err != TIFFReadDirEntryErrOk)
     451           0 :                 return (err);
     452           0 :             *value = (int8_t)m;
     453           0 :             return (TIFFReadDirEntryErrOk);
     454             :         }
     455           0 :         case TIFF_LONG8:
     456             :         {
     457             :             uint64_t m;
     458           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     459           0 :             if (err != TIFFReadDirEntryErrOk)
     460           0 :                 return (err);
     461           0 :             err = TIFFReadDirEntryCheckRangeSbyteLong8(m);
     462           0 :             if (err != TIFFReadDirEntryErrOk)
     463           0 :                 return (err);
     464           0 :             *value = (int8_t)m;
     465           0 :             return (TIFFReadDirEntryErrOk);
     466             :         }
     467           0 :         case TIFF_SLONG8:
     468             :         {
     469             :             int64_t m;
     470           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     471           0 :             if (err != TIFFReadDirEntryErrOk)
     472           0 :                 return (err);
     473           0 :             err = TIFFReadDirEntryCheckRangeSbyteSlong8(m);
     474           0 :             if (err != TIFFReadDirEntryErrOk)
     475           0 :                 return (err);
     476           0 :             *value = (int8_t)m;
     477           0 :             return (TIFFReadDirEntryErrOk);
     478             :         }
     479           0 :         default:
     480           0 :             return (TIFFReadDirEntryErrType);
     481             :     }
     482             : } /*-- TIFFReadDirEntrySbyte() --*/
     483             : 
     484             : static enum TIFFReadDirEntryErr
     485      335061 : TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value)
     486             : {
     487             :     enum TIFFReadDirEntryErr err;
     488      335061 :     if (direntry->tdir_count != 1)
     489       32867 :         return (TIFFReadDirEntryErrCount);
     490      302194 :     switch (direntry->tdir_type)
     491             :     {
     492           0 :         case TIFF_BYTE:
     493             :         {
     494             :             uint8_t m;
     495           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     496           0 :             *value = (uint16_t)m;
     497           0 :             return (TIFFReadDirEntryErrOk);
     498             :         }
     499           0 :         case TIFF_SBYTE:
     500             :         {
     501             :             int8_t m;
     502           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     503           0 :             err = TIFFReadDirEntryCheckRangeShortSbyte(m);
     504           0 :             if (err != TIFFReadDirEntryErrOk)
     505           0 :                 return (err);
     506           0 :             *value = (uint16_t)m;
     507           0 :             return (TIFFReadDirEntryErrOk);
     508             :         }
     509      302113 :         case TIFF_SHORT:
     510      302113 :             TIFFReadDirEntryCheckedShort(tif, direntry, value);
     511      302120 :             return (TIFFReadDirEntryErrOk);
     512           0 :         case TIFF_SSHORT:
     513             :         {
     514             :             int16_t m;
     515           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     516           0 :             err = TIFFReadDirEntryCheckRangeShortSshort(m);
     517           0 :             if (err != TIFFReadDirEntryErrOk)
     518           0 :                 return (err);
     519           0 :             *value = (uint16_t)m;
     520           0 :             return (TIFFReadDirEntryErrOk);
     521             :         }
     522           1 :         case TIFF_LONG:
     523             :         {
     524             :             uint32_t m;
     525           1 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     526           1 :             err = TIFFReadDirEntryCheckRangeShortLong(m);
     527           1 :             if (err != TIFFReadDirEntryErrOk)
     528           0 :                 return (err);
     529           1 :             *value = (uint16_t)m;
     530           1 :             return (TIFFReadDirEntryErrOk);
     531             :         }
     532           0 :         case TIFF_SLONG:
     533             :         {
     534             :             int32_t m;
     535           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     536           0 :             err = TIFFReadDirEntryCheckRangeShortSlong(m);
     537           0 :             if (err != TIFFReadDirEntryErrOk)
     538           0 :                 return (err);
     539           0 :             *value = (uint16_t)m;
     540           0 :             return (TIFFReadDirEntryErrOk);
     541             :         }
     542           0 :         case TIFF_LONG8:
     543             :         {
     544             :             uint64_t m;
     545           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     546           0 :             if (err != TIFFReadDirEntryErrOk)
     547           0 :                 return (err);
     548           0 :             err = TIFFReadDirEntryCheckRangeShortLong8(m);
     549           0 :             if (err != TIFFReadDirEntryErrOk)
     550           0 :                 return (err);
     551           0 :             *value = (uint16_t)m;
     552           0 :             return (TIFFReadDirEntryErrOk);
     553             :         }
     554           0 :         case TIFF_SLONG8:
     555             :         {
     556             :             int64_t m;
     557           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     558           0 :             if (err != TIFFReadDirEntryErrOk)
     559           0 :                 return (err);
     560           0 :             err = TIFFReadDirEntryCheckRangeShortSlong8(m);
     561           0 :             if (err != TIFFReadDirEntryErrOk)
     562           0 :                 return (err);
     563           0 :             *value = (uint16_t)m;
     564           0 :             return (TIFFReadDirEntryErrOk);
     565             :         }
     566          80 :         default:
     567          80 :             return (TIFFReadDirEntryErrType);
     568             :     }
     569             : } /*-- TIFFReadDirEntryShort() --*/
     570             : 
     571             : static enum TIFFReadDirEntryErr
     572           0 : TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value)
     573             : {
     574             :     enum TIFFReadDirEntryErr err;
     575           0 :     if (direntry->tdir_count != 1)
     576           0 :         return (TIFFReadDirEntryErrCount);
     577           0 :     switch (direntry->tdir_type)
     578             :     {
     579           0 :         case TIFF_BYTE:
     580             :         {
     581             :             uint8_t m;
     582           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     583           0 :             *value = (int16_t)m;
     584           0 :             return (TIFFReadDirEntryErrOk);
     585             :         }
     586           0 :         case TIFF_SBYTE:
     587             :         {
     588             :             int8_t m;
     589           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     590           0 :             *value = (int16_t)m;
     591           0 :             return (TIFFReadDirEntryErrOk);
     592             :         }
     593           0 :         case TIFF_SHORT:
     594             :         {
     595             :             uint16_t m;
     596           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     597           0 :             err = TIFFReadDirEntryCheckRangeSshortShort(m);
     598           0 :             if (err != TIFFReadDirEntryErrOk)
     599           0 :                 return (err);
     600           0 :             *value = (int16_t)m;
     601           0 :             return (TIFFReadDirEntryErrOk);
     602             :         }
     603           0 :         case TIFF_SSHORT:
     604           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, value);
     605           0 :             return (TIFFReadDirEntryErrOk);
     606           0 :         case TIFF_LONG:
     607             :         {
     608             :             uint32_t m;
     609           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     610           0 :             err = TIFFReadDirEntryCheckRangeSshortLong(m);
     611           0 :             if (err != TIFFReadDirEntryErrOk)
     612           0 :                 return (err);
     613           0 :             *value = (int16_t)m;
     614           0 :             return (TIFFReadDirEntryErrOk);
     615             :         }
     616           0 :         case TIFF_SLONG:
     617             :         {
     618             :             int32_t m;
     619           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     620           0 :             err = TIFFReadDirEntryCheckRangeSshortSlong(m);
     621           0 :             if (err != TIFFReadDirEntryErrOk)
     622           0 :                 return (err);
     623           0 :             *value = (int16_t)m;
     624           0 :             return (TIFFReadDirEntryErrOk);
     625             :         }
     626           0 :         case TIFF_LONG8:
     627             :         {
     628             :             uint64_t m;
     629           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     630           0 :             if (err != TIFFReadDirEntryErrOk)
     631           0 :                 return (err);
     632           0 :             err = TIFFReadDirEntryCheckRangeSshortLong8(m);
     633           0 :             if (err != TIFFReadDirEntryErrOk)
     634           0 :                 return (err);
     635           0 :             *value = (int16_t)m;
     636           0 :             return (TIFFReadDirEntryErrOk);
     637             :         }
     638           0 :         case TIFF_SLONG8:
     639             :         {
     640             :             int64_t m;
     641           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     642           0 :             if (err != TIFFReadDirEntryErrOk)
     643           0 :                 return (err);
     644           0 :             err = TIFFReadDirEntryCheckRangeSshortSlong8(m);
     645           0 :             if (err != TIFFReadDirEntryErrOk)
     646           0 :                 return (err);
     647           0 :             *value = (int16_t)m;
     648           0 :             return (TIFFReadDirEntryErrOk);
     649             :         }
     650           0 :         default:
     651           0 :             return (TIFFReadDirEntryErrType);
     652             :     }
     653             : } /*-- TIFFReadDirEntrySshort() --*/
     654             : 
     655             : static enum TIFFReadDirEntryErr
     656      180504 : TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value)
     657             : {
     658             :     enum TIFFReadDirEntryErr err;
     659      180504 :     if (direntry->tdir_count != 1)
     660           0 :         return (TIFFReadDirEntryErrCount);
     661      180504 :     switch (direntry->tdir_type)
     662             :     {
     663           0 :         case TIFF_BYTE:
     664             :         {
     665             :             uint8_t m;
     666           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     667           0 :             *value = (uint32_t)m;
     668           0 :             return (TIFFReadDirEntryErrOk);
     669             :         }
     670           0 :         case TIFF_SBYTE:
     671             :         {
     672             :             int8_t m;
     673           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     674           0 :             err = TIFFReadDirEntryCheckRangeLongSbyte(m);
     675           0 :             if (err != TIFFReadDirEntryErrOk)
     676           0 :                 return (err);
     677           0 :             *value = (uint32_t)m;
     678           0 :             return (TIFFReadDirEntryErrOk);
     679             :         }
     680      174351 :         case TIFF_SHORT:
     681             :         {
     682             :             uint16_t m;
     683      174351 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     684      174368 :             *value = (uint32_t)m;
     685      174368 :             return (TIFFReadDirEntryErrOk);
     686             :         }
     687           0 :         case TIFF_SSHORT:
     688             :         {
     689             :             int16_t m;
     690           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     691           0 :             err = TIFFReadDirEntryCheckRangeLongSshort(m);
     692           0 :             if (err != TIFFReadDirEntryErrOk)
     693           0 :                 return (err);
     694           0 :             *value = (uint32_t)m;
     695           0 :             return (TIFFReadDirEntryErrOk);
     696             :         }
     697        6120 :         case TIFF_LONG:
     698             :         case TIFF_IFD:
     699        6120 :             TIFFReadDirEntryCheckedLong(tif, direntry, value);
     700        6120 :             return (TIFFReadDirEntryErrOk);
     701           0 :         case TIFF_SLONG:
     702             :         {
     703             :             int32_t m;
     704           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     705           0 :             err = TIFFReadDirEntryCheckRangeLongSlong(m);
     706           0 :             if (err != TIFFReadDirEntryErrOk)
     707           0 :                 return (err);
     708           0 :             *value = (uint32_t)m;
     709           0 :             return (TIFFReadDirEntryErrOk);
     710             :         }
     711           0 :         case TIFF_LONG8:
     712             :         case TIFF_IFD8:
     713             :         {
     714             :             uint64_t m;
     715           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     716           0 :             if (err != TIFFReadDirEntryErrOk)
     717           0 :                 return (err);
     718           0 :             err = TIFFReadDirEntryCheckRangeLongLong8(m);
     719           0 :             if (err != TIFFReadDirEntryErrOk)
     720           0 :                 return (err);
     721           0 :             *value = (uint32_t)m;
     722           0 :             return (TIFFReadDirEntryErrOk);
     723             :         }
     724           0 :         case TIFF_SLONG8:
     725             :         {
     726             :             int64_t m;
     727           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     728           0 :             if (err != TIFFReadDirEntryErrOk)
     729           0 :                 return (err);
     730           0 :             err = TIFFReadDirEntryCheckRangeLongSlong8(m);
     731           0 :             if (err != TIFFReadDirEntryErrOk)
     732           0 :                 return (err);
     733           0 :             *value = (uint32_t)m;
     734           0 :             return (TIFFReadDirEntryErrOk);
     735             :         }
     736          33 :         default:
     737          33 :             return (TIFFReadDirEntryErrType);
     738             :     }
     739             : } /*-- TIFFReadDirEntryLong() --*/
     740             : 
     741             : static enum TIFFReadDirEntryErr
     742           0 : TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value)
     743             : {
     744             :     enum TIFFReadDirEntryErr err;
     745           0 :     if (direntry->tdir_count != 1)
     746           0 :         return (TIFFReadDirEntryErrCount);
     747           0 :     switch (direntry->tdir_type)
     748             :     {
     749           0 :         case TIFF_BYTE:
     750             :         {
     751             :             uint8_t m;
     752           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     753           0 :             *value = (int32_t)m;
     754           0 :             return (TIFFReadDirEntryErrOk);
     755             :         }
     756           0 :         case TIFF_SBYTE:
     757             :         {
     758             :             int8_t m;
     759           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     760           0 :             *value = (int32_t)m;
     761           0 :             return (TIFFReadDirEntryErrOk);
     762             :         }
     763           0 :         case TIFF_SHORT:
     764             :         {
     765             :             uint16_t m;
     766           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     767           0 :             *value = (int32_t)m;
     768           0 :             return (TIFFReadDirEntryErrOk);
     769             :         }
     770           0 :         case TIFF_SSHORT:
     771             :         {
     772             :             int16_t m;
     773           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     774           0 :             *value = (int32_t)m;
     775           0 :             return (TIFFReadDirEntryErrOk);
     776             :         }
     777           0 :         case TIFF_LONG:
     778             :         {
     779             :             uint32_t m;
     780           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     781           0 :             err = TIFFReadDirEntryCheckRangeSlongLong(m);
     782           0 :             if (err != TIFFReadDirEntryErrOk)
     783           0 :                 return (err);
     784           0 :             *value = (int32_t)m;
     785           0 :             return (TIFFReadDirEntryErrOk);
     786             :         }
     787           0 :         case TIFF_SLONG:
     788           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, value);
     789           0 :             return (TIFFReadDirEntryErrOk);
     790           0 :         case TIFF_LONG8:
     791             :         {
     792             :             uint64_t m;
     793           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     794           0 :             if (err != TIFFReadDirEntryErrOk)
     795           0 :                 return (err);
     796           0 :             err = TIFFReadDirEntryCheckRangeSlongLong8(m);
     797           0 :             if (err != TIFFReadDirEntryErrOk)
     798           0 :                 return (err);
     799           0 :             *value = (int32_t)m;
     800           0 :             return (TIFFReadDirEntryErrOk);
     801             :         }
     802           0 :         case TIFF_SLONG8:
     803             :         {
     804             :             int64_t m;
     805           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     806           0 :             if (err != TIFFReadDirEntryErrOk)
     807           0 :                 return (err);
     808           0 :             err = TIFFReadDirEntryCheckRangeSlongSlong8(m);
     809           0 :             if (err != TIFFReadDirEntryErrOk)
     810           0 :                 return (err);
     811           0 :             *value = (int32_t)m;
     812           0 :             return (TIFFReadDirEntryErrOk);
     813             :         }
     814           0 :         default:
     815           0 :             return (TIFFReadDirEntryErrType);
     816             :     }
     817             : } /*-- TIFFReadDirEntrySlong() --*/
     818             : 
     819             : static enum TIFFReadDirEntryErr
     820           8 : TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
     821             : {
     822             :     enum TIFFReadDirEntryErr err;
     823           8 :     if (direntry->tdir_count != 1)
     824           0 :         return (TIFFReadDirEntryErrCount);
     825           8 :     switch (direntry->tdir_type)
     826             :     {
     827           0 :         case TIFF_BYTE:
     828             :         {
     829             :             uint8_t m;
     830           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     831           0 :             *value = (uint64_t)m;
     832           0 :             return (TIFFReadDirEntryErrOk);
     833             :         }
     834           0 :         case TIFF_SBYTE:
     835             :         {
     836             :             int8_t m;
     837           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     838           0 :             err = TIFFReadDirEntryCheckRangeLong8Sbyte(m);
     839           0 :             if (err != TIFFReadDirEntryErrOk)
     840           0 :                 return (err);
     841           0 :             *value = (uint64_t)m;
     842           0 :             return (TIFFReadDirEntryErrOk);
     843             :         }
     844           0 :         case TIFF_SHORT:
     845             :         {
     846             :             uint16_t m;
     847           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     848           0 :             *value = (uint64_t)m;
     849           0 :             return (TIFFReadDirEntryErrOk);
     850             :         }
     851           0 :         case TIFF_SSHORT:
     852             :         {
     853             :             int16_t m;
     854           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     855           0 :             err = TIFFReadDirEntryCheckRangeLong8Sshort(m);
     856           0 :             if (err != TIFFReadDirEntryErrOk)
     857           0 :                 return (err);
     858           0 :             *value = (uint64_t)m;
     859           0 :             return (TIFFReadDirEntryErrOk);
     860             :         }
     861           8 :         case TIFF_LONG:
     862             :         case TIFF_IFD:
     863             :         {
     864             :             uint32_t m;
     865           8 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     866           8 :             *value = (uint64_t)m;
     867           8 :             return (TIFFReadDirEntryErrOk);
     868             :         }
     869           0 :         case TIFF_SLONG:
     870             :         {
     871             :             int32_t m;
     872           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     873           0 :             err = TIFFReadDirEntryCheckRangeLong8Slong(m);
     874           0 :             if (err != TIFFReadDirEntryErrOk)
     875           0 :                 return (err);
     876           0 :             *value = (uint64_t)m;
     877           0 :             return (TIFFReadDirEntryErrOk);
     878             :         }
     879           0 :         case TIFF_LONG8:
     880             :         case TIFF_IFD8:
     881           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
     882           0 :             return (err);
     883           0 :         case TIFF_SLONG8:
     884             :         {
     885             :             int64_t m;
     886           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     887           0 :             if (err != TIFFReadDirEntryErrOk)
     888           0 :                 return (err);
     889           0 :             err = TIFFReadDirEntryCheckRangeLong8Slong8(m);
     890           0 :             if (err != TIFFReadDirEntryErrOk)
     891           0 :                 return (err);
     892           0 :             *value = (uint64_t)m;
     893           0 :             return (TIFFReadDirEntryErrOk);
     894             :         }
     895           0 :         default:
     896           0 :             return (TIFFReadDirEntryErrType);
     897             :     }
     898             : } /*-- TIFFReadDirEntryLong8() --*/
     899             : 
     900             : static enum TIFFReadDirEntryErr
     901           0 : TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
     902             : {
     903             :     enum TIFFReadDirEntryErr err;
     904           0 :     if (direntry->tdir_count != 1)
     905           0 :         return (TIFFReadDirEntryErrCount);
     906           0 :     switch (direntry->tdir_type)
     907             :     {
     908           0 :         case TIFF_BYTE:
     909             :         {
     910             :             uint8_t m;
     911           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     912           0 :             *value = (int64_t)m;
     913           0 :             return (TIFFReadDirEntryErrOk);
     914             :         }
     915           0 :         case TIFF_SBYTE:
     916             :         {
     917             :             int8_t m;
     918           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     919           0 :             *value = (int64_t)m;
     920           0 :             return (TIFFReadDirEntryErrOk);
     921             :         }
     922           0 :         case TIFF_SHORT:
     923             :         {
     924             :             uint16_t m;
     925           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     926           0 :             *value = (int64_t)m;
     927           0 :             return (TIFFReadDirEntryErrOk);
     928             :         }
     929           0 :         case TIFF_SSHORT:
     930             :         {
     931             :             int16_t m;
     932           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     933           0 :             *value = (int64_t)m;
     934           0 :             return (TIFFReadDirEntryErrOk);
     935             :         }
     936           0 :         case TIFF_LONG:
     937             :         {
     938             :             uint32_t m;
     939           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     940           0 :             *value = (int64_t)m;
     941           0 :             return (TIFFReadDirEntryErrOk);
     942             :         }
     943           0 :         case TIFF_SLONG:
     944             :         {
     945             :             int32_t m;
     946           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     947           0 :             *value = (int64_t)m;
     948           0 :             return (TIFFReadDirEntryErrOk);
     949             :         }
     950           0 :         case TIFF_LONG8:
     951             :         {
     952             :             uint64_t m;
     953           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     954           0 :             if (err != TIFFReadDirEntryErrOk)
     955           0 :                 return (err);
     956           0 :             err = TIFFReadDirEntryCheckRangeSlong8Long8(m);
     957           0 :             if (err != TIFFReadDirEntryErrOk)
     958           0 :                 return (err);
     959           0 :             *value = (int64_t)m;
     960           0 :             return (TIFFReadDirEntryErrOk);
     961             :         }
     962           0 :         case TIFF_SLONG8:
     963           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, value);
     964           0 :             return (err);
     965           0 :         default:
     966           0 :             return (TIFFReadDirEntryErrType);
     967             :     }
     968             : } /*-- TIFFReadDirEntrySlong8() --*/
     969             : 
     970             : static enum TIFFReadDirEntryErr
     971         248 : TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value)
     972             : {
     973             :     enum TIFFReadDirEntryErr err;
     974         248 :     if (direntry->tdir_count != 1)
     975           0 :         return (TIFFReadDirEntryErrCount);
     976         248 :     switch (direntry->tdir_type)
     977             :     {
     978           0 :         case TIFF_BYTE:
     979             :         {
     980             :             uint8_t m;
     981           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     982           0 :             *value = (float)m;
     983           0 :             return (TIFFReadDirEntryErrOk);
     984             :         }
     985           0 :         case TIFF_SBYTE:
     986             :         {
     987             :             int8_t m;
     988           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     989           0 :             *value = (float)m;
     990           0 :             return (TIFFReadDirEntryErrOk);
     991             :         }
     992           0 :         case TIFF_SHORT:
     993             :         {
     994             :             uint16_t m;
     995           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     996           0 :             *value = (float)m;
     997           0 :             return (TIFFReadDirEntryErrOk);
     998             :         }
     999           0 :         case TIFF_SSHORT:
    1000             :         {
    1001             :             int16_t m;
    1002           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
    1003           0 :             *value = (float)m;
    1004           0 :             return (TIFFReadDirEntryErrOk);
    1005             :         }
    1006           0 :         case TIFF_LONG:
    1007             :         {
    1008             :             uint32_t m;
    1009           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
    1010           0 :             *value = (float)m;
    1011           0 :             return (TIFFReadDirEntryErrOk);
    1012             :         }
    1013           0 :         case TIFF_SLONG:
    1014             :         {
    1015             :             int32_t m;
    1016           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
    1017           0 :             *value = (float)m;
    1018           0 :             return (TIFFReadDirEntryErrOk);
    1019             :         }
    1020           0 :         case TIFF_LONG8:
    1021             :         {
    1022             :             uint64_t m;
    1023           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
    1024           0 :             if (err != TIFFReadDirEntryErrOk)
    1025           0 :                 return (err);
    1026           0 :             *value = (float)m;
    1027           0 :             return (TIFFReadDirEntryErrOk);
    1028             :         }
    1029           0 :         case TIFF_SLONG8:
    1030             :         {
    1031             :             int64_t m;
    1032           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
    1033           0 :             if (err != TIFFReadDirEntryErrOk)
    1034           0 :                 return (err);
    1035           0 :             *value = (float)m;
    1036           0 :             return (TIFFReadDirEntryErrOk);
    1037             :         }
    1038         248 :         case TIFF_RATIONAL:
    1039             :         {
    1040             :             double m;
    1041         248 :             err = TIFFReadDirEntryCheckedRational(tif, direntry, &m);
    1042         248 :             if (err != TIFFReadDirEntryErrOk)
    1043           0 :                 return (err);
    1044         248 :             *value = (float)m;
    1045         248 :             return (TIFFReadDirEntryErrOk);
    1046             :         }
    1047           0 :         case TIFF_SRATIONAL:
    1048             :         {
    1049             :             double m;
    1050           0 :             err = TIFFReadDirEntryCheckedSrational(tif, direntry, &m);
    1051           0 :             if (err != TIFFReadDirEntryErrOk)
    1052           0 :                 return (err);
    1053           0 :             *value = (float)m;
    1054           0 :             return (TIFFReadDirEntryErrOk);
    1055             :         }
    1056           0 :         case TIFF_FLOAT:
    1057           0 :             TIFFReadDirEntryCheckedFloat(tif, direntry, value);
    1058           0 :             return (TIFFReadDirEntryErrOk);
    1059           0 :         case TIFF_DOUBLE:
    1060             :         {
    1061             :             double m;
    1062           0 :             err = TIFFReadDirEntryCheckedDouble(tif, direntry, &m);
    1063           0 :             if (err != TIFFReadDirEntryErrOk)
    1064           0 :                 return (err);
    1065           0 :             if ((m > (double)FLT_MAX) || (m < -(double)FLT_MAX))
    1066           0 :                 return (TIFFReadDirEntryErrRange);
    1067           0 :             *value = (float)m;
    1068           0 :             return (TIFFReadDirEntryErrOk);
    1069             :         }
    1070           0 :         default:
    1071           0 :             return (TIFFReadDirEntryErrType);
    1072             :     }
    1073             : }
    1074             : 
    1075             : static enum TIFFReadDirEntryErr
    1076           0 : TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
    1077             : {
    1078             :     enum TIFFReadDirEntryErr err;
    1079           0 :     if (direntry->tdir_count != 1)
    1080           0 :         return (TIFFReadDirEntryErrCount);
    1081           0 :     switch (direntry->tdir_type)
    1082             :     {
    1083           0 :         case TIFF_BYTE:
    1084             :         {
    1085             :             uint8_t m;
    1086           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
    1087           0 :             *value = (double)m;
    1088           0 :             return (TIFFReadDirEntryErrOk);
    1089             :         }
    1090           0 :         case TIFF_SBYTE:
    1091             :         {
    1092             :             int8_t m;
    1093           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
    1094           0 :             *value = (double)m;
    1095           0 :             return (TIFFReadDirEntryErrOk);
    1096             :         }
    1097           0 :         case TIFF_SHORT:
    1098             :         {
    1099             :             uint16_t m;
    1100           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
    1101           0 :             *value = (double)m;
    1102           0 :             return (TIFFReadDirEntryErrOk);
    1103             :         }
    1104           0 :         case TIFF_SSHORT:
    1105             :         {
    1106             :             int16_t m;
    1107           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
    1108           0 :             *value = (double)m;
    1109           0 :             return (TIFFReadDirEntryErrOk);
    1110             :         }
    1111           0 :         case TIFF_LONG:
    1112             :         {
    1113             :             uint32_t m;
    1114           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
    1115           0 :             *value = (double)m;
    1116           0 :             return (TIFFReadDirEntryErrOk);
    1117             :         }
    1118           0 :         case TIFF_SLONG:
    1119             :         {
    1120             :             int32_t m;
    1121           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
    1122           0 :             *value = (double)m;
    1123           0 :             return (TIFFReadDirEntryErrOk);
    1124             :         }
    1125           0 :         case TIFF_LONG8:
    1126             :         {
    1127             :             uint64_t m;
    1128           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
    1129           0 :             if (err != TIFFReadDirEntryErrOk)
    1130           0 :                 return (err);
    1131           0 :             *value = (double)m;
    1132           0 :             return (TIFFReadDirEntryErrOk);
    1133             :         }
    1134           0 :         case TIFF_SLONG8:
    1135             :         {
    1136             :             int64_t m;
    1137           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
    1138           0 :             if (err != TIFFReadDirEntryErrOk)
    1139           0 :                 return (err);
    1140           0 :             *value = (double)m;
    1141           0 :             return (TIFFReadDirEntryErrOk);
    1142             :         }
    1143           0 :         case TIFF_RATIONAL:
    1144           0 :             err = TIFFReadDirEntryCheckedRational(tif, direntry, value);
    1145           0 :             return (err);
    1146           0 :         case TIFF_SRATIONAL:
    1147           0 :             err = TIFFReadDirEntryCheckedSrational(tif, direntry, value);
    1148           0 :             return (err);
    1149           0 :         case TIFF_FLOAT:
    1150             :         {
    1151             :             float m;
    1152           0 :             TIFFReadDirEntryCheckedFloat(tif, direntry, &m);
    1153           0 :             *value = (double)m;
    1154           0 :             return (TIFFReadDirEntryErrOk);
    1155             :         }
    1156           0 :         case TIFF_DOUBLE:
    1157           0 :             err = TIFFReadDirEntryCheckedDouble(tif, direntry, value);
    1158           0 :             return (err);
    1159           0 :         default:
    1160           0 :             return (TIFFReadDirEntryErrType);
    1161             :     }
    1162             : }
    1163             : 
    1164             : static enum TIFFReadDirEntryErr
    1165           0 : TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
    1166             : {
    1167             :     enum TIFFReadDirEntryErr err;
    1168           0 :     if (direntry->tdir_count != 1)
    1169           0 :         return (TIFFReadDirEntryErrCount);
    1170           0 :     switch (direntry->tdir_type)
    1171             :     {
    1172           0 :         case TIFF_LONG:
    1173             :         case TIFF_IFD:
    1174             :         {
    1175             :             uint32_t m;
    1176           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
    1177           0 :             *value = (uint64_t)m;
    1178           0 :             return (TIFFReadDirEntryErrOk);
    1179             :         }
    1180           0 :         case TIFF_LONG8:
    1181             :         case TIFF_IFD8:
    1182           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
    1183           0 :             return (err);
    1184           0 :         default:
    1185           0 :             return (TIFFReadDirEntryErrType);
    1186             :     }
    1187             : }
    1188             : 
    1189             : #define INITIAL_THRESHOLD (1024 * 1024)
    1190             : #define THRESHOLD_MULTIPLIER 10
    1191             : #define MAX_THRESHOLD                                                          \
    1192             :     (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER *      \
    1193             :      INITIAL_THRESHOLD)
    1194             : 
    1195      184623 : static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(TIFF *tif,
    1196             :                                                                uint64_t offset,
    1197             :                                                                tmsize_t size,
    1198             :                                                                void **pdest)
    1199             : {
    1200             : #if SIZEOF_SIZE_T == 8
    1201      184623 :     tmsize_t threshold = INITIAL_THRESHOLD;
    1202             : #endif
    1203      184623 :     tmsize_t already_read = 0;
    1204             : 
    1205      184623 :     assert(!isMapped(tif));
    1206             : 
    1207      184623 :     if (!SeekOK(tif, offset))
    1208           0 :         return (TIFFReadDirEntryErrIo);
    1209             : 
    1210             :     /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
    1211             :     /* so as to avoid allocating too much memory in case the file is too */
    1212             :     /* short. We could ask for the file size, but this might be */
    1213             :     /* expensive with some I/O layers (think of reading a gzipped file) */
    1214             :     /* Restrict to 64 bit processes, so as to avoid reallocs() */
    1215             :     /* on 32 bit processes where virtual memory is scarce.  */
    1216      369372 :     while (already_read < size)
    1217             :     {
    1218             :         void *new_dest;
    1219             :         tmsize_t bytes_read;
    1220      184729 :         tmsize_t to_read = size - already_read;
    1221             : #if SIZEOF_SIZE_T == 8
    1222      184729 :         if (to_read >= threshold && threshold < MAX_THRESHOLD)
    1223             :         {
    1224          18 :             to_read = threshold;
    1225          18 :             threshold *= THRESHOLD_MULTIPLIER;
    1226             :         }
    1227             : #endif
    1228             : 
    1229             :         new_dest =
    1230      184729 :             (uint8_t *)_TIFFreallocExt(tif, *pdest, already_read + to_read);
    1231      184752 :         if (new_dest == NULL)
    1232             :         {
    1233           0 :             TIFFErrorExtR(tif, tif->tif_name,
    1234             :                           "Failed to allocate memory for %s "
    1235             :                           "(%" TIFF_SSIZE_FORMAT
    1236             :                           " elements of %" TIFF_SSIZE_FORMAT " bytes each)",
    1237             :                           "TIFFReadDirEntryArray", (tmsize_t)1,
    1238             :                           already_read + to_read);
    1239           0 :             return TIFFReadDirEntryErrAlloc;
    1240             :         }
    1241      184752 :         *pdest = new_dest;
    1242             : 
    1243      184752 :         bytes_read = TIFFReadFile(tif, (char *)*pdest + already_read, to_read);
    1244      184681 :         if (bytes_read < 0)
    1245           0 :             return TIFFReadDirEntryErrIo;
    1246      184681 :         already_read += bytes_read;
    1247      184681 :         if (bytes_read != to_read)
    1248             :         {
    1249          21 :             return TIFFReadDirEntryErrIo;
    1250             :         }
    1251             :     }
    1252      184643 :     return TIFFReadDirEntryErrOk;
    1253             : }
    1254             : 
    1255             : /* Caution: if raising that value, make sure int32 / uint32 overflows can't
    1256             :  * occur elsewhere */
    1257             : #define MAX_SIZE_TAG_DATA 2147483647U
    1258             : 
    1259             : static enum TIFFReadDirEntryErr
    1260      270200 : TIFFReadDirEntryArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry,
    1261             :                                uint32_t *count, uint32_t desttypesize,
    1262             :                                void **value, uint64_t maxcount)
    1263             : {
    1264             :     int typesize;
    1265             :     uint32_t datasize;
    1266             :     void *data;
    1267             :     uint64_t target_count64;
    1268             :     int original_datasize_clamped;
    1269      270200 :     typesize = TIFFDataWidth((TIFFDataType)direntry->tdir_type);
    1270             : 
    1271      270155 :     target_count64 =
    1272      270155 :         (direntry->tdir_count > maxcount) ? maxcount : direntry->tdir_count;
    1273             : 
    1274      270155 :     if ((target_count64 == 0) || (typesize == 0))
    1275             :     {
    1276          17 :         *value = 0;
    1277          17 :         return (TIFFReadDirEntryErrOk);
    1278             :     }
    1279             :     (void)desttypesize;
    1280             : 
    1281             :     /* We just want to know if the original tag size is more than 4 bytes
    1282             :      * (classic TIFF) or 8 bytes (BigTIFF)
    1283             :      */
    1284      270138 :     original_datasize_clamped =
    1285      270138 :         ((direntry->tdir_count > 10) ? 10 : (int)direntry->tdir_count) *
    1286             :         typesize;
    1287             : 
    1288             :     /*
    1289             :      * As a sanity check, make sure we have no more than a 2GB tag array
    1290             :      * in either the current data type or the dest data type.  This also
    1291             :      * avoids problems with overflow of tmsize_t on 32bit systems.
    1292             :      */
    1293      270138 :     if ((uint64_t)(MAX_SIZE_TAG_DATA / (unsigned int)typesize) < target_count64)
    1294           3 :         return (TIFFReadDirEntryErrSizesan);
    1295      270135 :     if ((uint64_t)(MAX_SIZE_TAG_DATA / desttypesize) < target_count64)
    1296           0 :         return (TIFFReadDirEntryErrSizesan);
    1297             : 
    1298      270135 :     *count = (uint32_t)target_count64;
    1299      270135 :     datasize = (uint32_t)(*count) * (unsigned int)typesize;
    1300      270135 :     assert((tmsize_t)datasize > 0);
    1301             : 
    1302      270135 :     if (datasize > 100 * 1024 * 1024)
    1303             :     {
    1304             :         /* Before allocating a huge amount of memory for corrupted files, check
    1305             :          * if size of requested memory is not greater than file size.
    1306             :          */
    1307          15 :         const uint64_t filesize = TIFFGetFileSize(tif);
    1308          15 :         if (datasize > filesize)
    1309             :         {
    1310          15 :             TIFFWarningExtR(tif, "ReadDirEntryArray",
    1311             :                             "Requested memory size for tag %d (0x%x) %" PRIu32
    1312             :                             " is greater than filesize %" PRIu64
    1313             :                             ". Memory not allocated, tag not read",
    1314          15 :                             direntry->tdir_tag, direntry->tdir_tag, datasize,
    1315             :                             filesize);
    1316          15 :             return (TIFFReadDirEntryErrAlloc);
    1317             :         }
    1318             :     }
    1319             : 
    1320      270120 :     if (isMapped(tif) && datasize > (uint64_t)tif->tif_size)
    1321           0 :         return TIFFReadDirEntryErrIo;
    1322             : 
    1323      270120 :     if (!isMapped(tif) && (((tif->tif_flags & TIFF_BIGTIFF) && datasize > 8) ||
    1324      268857 :                            (!(tif->tif_flags & TIFF_BIGTIFF) && datasize > 4)))
    1325             :     {
    1326      184535 :         data = NULL;
    1327             :     }
    1328             :     else
    1329             :     {
    1330       85585 :         data = _TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
    1331       85588 :         if (data == 0)
    1332           0 :             return (TIFFReadDirEntryErrAlloc);
    1333             :     }
    1334      270123 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    1335             :     {
    1336             :         /* Only the condition on original_datasize_clamped. The second
    1337             :          * one is implied, but Coverity Scan cannot see it. */
    1338      267131 :         if (original_datasize_clamped <= 4 && datasize <= 4)
    1339       83912 :             _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
    1340             :         else
    1341             :         {
    1342             :             enum TIFFReadDirEntryErr err;
    1343      183219 :             uint32_t offset = direntry->tdir_offset.toff_long;
    1344      183219 :             if (tif->tif_flags & TIFF_SWAB)
    1345        1941 :                 TIFFSwabLong(&offset);
    1346      183289 :             if (isMapped(tif))
    1347          32 :                 err = TIFFReadDirEntryData(tif, (uint64_t)offset,
    1348             :                                            (tmsize_t)datasize, data);
    1349             :             else
    1350      183257 :                 err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
    1351             :                                                      (tmsize_t)datasize, &data);
    1352      183339 :             if (err != TIFFReadDirEntryErrOk)
    1353             :             {
    1354          21 :                 _TIFFfreeExt(tif, data);
    1355          21 :                 return (err);
    1356             :             }
    1357             :         }
    1358             :     }
    1359             :     else
    1360             :     {
    1361             :         /* See above comment for the Classic TIFF case */
    1362        2992 :         if (original_datasize_clamped <= 8 && datasize <= 8)
    1363        1628 :             _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
    1364             :         else
    1365             :         {
    1366             :             enum TIFFReadDirEntryErr err;
    1367        1364 :             uint64_t offset = direntry->tdir_offset.toff_long8;
    1368        1364 :             if (tif->tif_flags & TIFF_SWAB)
    1369          14 :                 TIFFSwabLong8(&offset);
    1370        1320 :             if (isMapped(tif))
    1371           0 :                 err = TIFFReadDirEntryData(tif, (uint64_t)offset,
    1372             :                                            (tmsize_t)datasize, data);
    1373             :             else
    1374        1320 :                 err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
    1375             :                                                      (tmsize_t)datasize, &data);
    1376        1320 :             if (err != TIFFReadDirEntryErrOk)
    1377             :             {
    1378           0 :                 _TIFFfreeExt(tif, data);
    1379           0 :                 return (err);
    1380             :             }
    1381             :         }
    1382             :     }
    1383      270191 :     *value = data;
    1384      270191 :     return (TIFFReadDirEntryErrOk);
    1385             : }
    1386             : 
    1387             : static enum TIFFReadDirEntryErr
    1388      184101 : TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
    1389             :                       uint32_t desttypesize, void **value)
    1390             : {
    1391      184101 :     return TIFFReadDirEntryArrayWithLimit(tif, direntry, count, desttypesize,
    1392             :                                           value, ~((uint64_t)0));
    1393             : }
    1394             : 
    1395             : static enum TIFFReadDirEntryErr
    1396       41219 : TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value)
    1397             : {
    1398             :     enum TIFFReadDirEntryErr err;
    1399             :     uint32_t count;
    1400             :     void *origdata;
    1401             :     uint8_t *data;
    1402       41219 :     switch (direntry->tdir_type)
    1403             :     {
    1404       41207 :         case TIFF_ASCII:
    1405             :         case TIFF_UNDEFINED:
    1406             :         case TIFF_BYTE:
    1407             :         case TIFF_SBYTE:
    1408             :         case TIFF_SHORT:
    1409             :         case TIFF_SSHORT:
    1410             :         case TIFF_LONG:
    1411             :         case TIFF_SLONG:
    1412             :         case TIFF_LONG8:
    1413             :         case TIFF_SLONG8:
    1414       41207 :             break;
    1415          12 :         default:
    1416          12 :             return (TIFFReadDirEntryErrType);
    1417             :     }
    1418       41207 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
    1419       41156 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    1420             :     {
    1421          44 :         *value = 0;
    1422          44 :         return (err);
    1423             :     }
    1424       41112 :     switch (direntry->tdir_type)
    1425             :     {
    1426       41118 :         case TIFF_ASCII:
    1427             :         case TIFF_UNDEFINED:
    1428             :         case TIFF_BYTE:
    1429       41118 :             *value = (uint8_t *)origdata;
    1430       41118 :             return (TIFFReadDirEntryErrOk);
    1431           0 :         case TIFF_SBYTE:
    1432             :         {
    1433             :             int8_t *m;
    1434             :             uint32_t n;
    1435           0 :             m = (int8_t *)origdata;
    1436           0 :             for (n = 0; n < count; n++)
    1437             :             {
    1438           0 :                 err = TIFFReadDirEntryCheckRangeByteSbyte(*m);
    1439           0 :                 if (err != TIFFReadDirEntryErrOk)
    1440             :                 {
    1441           0 :                     _TIFFfreeExt(tif, origdata);
    1442           0 :                     return (err);
    1443             :                 }
    1444           0 :                 m++;
    1445             :             }
    1446           0 :             *value = (uint8_t *)origdata;
    1447           0 :             return (TIFFReadDirEntryErrOk);
    1448             :         }
    1449           0 :         default:
    1450           0 :             break;
    1451             :     }
    1452           0 :     data = (uint8_t *)_TIFFmallocExt(tif, count);
    1453           0 :     if (data == 0)
    1454             :     {
    1455           0 :         _TIFFfreeExt(tif, origdata);
    1456           0 :         return (TIFFReadDirEntryErrAlloc);
    1457             :     }
    1458           0 :     switch (direntry->tdir_type)
    1459             :     {
    1460           0 :         case TIFF_SHORT:
    1461             :         {
    1462             :             uint16_t *ma;
    1463             :             uint8_t *mb;
    1464             :             uint32_t n;
    1465           0 :             ma = (uint16_t *)origdata;
    1466           0 :             mb = data;
    1467           0 :             for (n = 0; n < count; n++)
    1468             :             {
    1469           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1470           0 :                     TIFFSwabShort(ma);
    1471           0 :                 err = TIFFReadDirEntryCheckRangeByteShort(*ma);
    1472           0 :                 if (err != TIFFReadDirEntryErrOk)
    1473           0 :                     break;
    1474           0 :                 *mb++ = (uint8_t)(*ma++);
    1475             :             }
    1476             :         }
    1477           0 :         break;
    1478           0 :         case TIFF_SSHORT:
    1479             :         {
    1480             :             int16_t *ma;
    1481             :             uint8_t *mb;
    1482             :             uint32_t n;
    1483           0 :             ma = (int16_t *)origdata;
    1484           0 :             mb = data;
    1485           0 :             for (n = 0; n < count; n++)
    1486             :             {
    1487           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1488           0 :                     TIFFSwabShort((uint16_t *)ma);
    1489           0 :                 err = TIFFReadDirEntryCheckRangeByteSshort(*ma);
    1490           0 :                 if (err != TIFFReadDirEntryErrOk)
    1491           0 :                     break;
    1492           0 :                 *mb++ = (uint8_t)(*ma++);
    1493             :             }
    1494             :         }
    1495           0 :         break;
    1496           0 :         case TIFF_LONG:
    1497             :         {
    1498             :             uint32_t *ma;
    1499             :             uint8_t *mb;
    1500             :             uint32_t n;
    1501           0 :             ma = (uint32_t *)origdata;
    1502           0 :             mb = data;
    1503           0 :             for (n = 0; n < count; n++)
    1504             :             {
    1505           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1506           0 :                     TIFFSwabLong(ma);
    1507           0 :                 err = TIFFReadDirEntryCheckRangeByteLong(*ma);
    1508           0 :                 if (err != TIFFReadDirEntryErrOk)
    1509           0 :                     break;
    1510           0 :                 *mb++ = (uint8_t)(*ma++);
    1511             :             }
    1512             :         }
    1513           0 :         break;
    1514           0 :         case TIFF_SLONG:
    1515             :         {
    1516             :             int32_t *ma;
    1517             :             uint8_t *mb;
    1518             :             uint32_t n;
    1519           0 :             ma = (int32_t *)origdata;
    1520           0 :             mb = data;
    1521           0 :             for (n = 0; n < count; n++)
    1522             :             {
    1523           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1524           0 :                     TIFFSwabLong((uint32_t *)ma);
    1525           0 :                 err = TIFFReadDirEntryCheckRangeByteSlong(*ma);
    1526           0 :                 if (err != TIFFReadDirEntryErrOk)
    1527           0 :                     break;
    1528           0 :                 *mb++ = (uint8_t)(*ma++);
    1529             :             }
    1530             :         }
    1531           0 :         break;
    1532           0 :         case TIFF_LONG8:
    1533             :         {
    1534             :             uint64_t *ma;
    1535             :             uint8_t *mb;
    1536             :             uint32_t n;
    1537           0 :             ma = (uint64_t *)origdata;
    1538           0 :             mb = data;
    1539           0 :             for (n = 0; n < count; n++)
    1540             :             {
    1541           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1542           0 :                     TIFFSwabLong8(ma);
    1543           0 :                 err = TIFFReadDirEntryCheckRangeByteLong8(*ma);
    1544           0 :                 if (err != TIFFReadDirEntryErrOk)
    1545           0 :                     break;
    1546           0 :                 *mb++ = (uint8_t)(*ma++);
    1547             :             }
    1548             :         }
    1549           0 :         break;
    1550           0 :         case TIFF_SLONG8:
    1551             :         {
    1552             :             int64_t *ma;
    1553             :             uint8_t *mb;
    1554             :             uint32_t n;
    1555           0 :             ma = (int64_t *)origdata;
    1556           0 :             mb = data;
    1557           0 :             for (n = 0; n < count; n++)
    1558             :             {
    1559           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1560           0 :                     TIFFSwabLong8((uint64_t *)ma);
    1561           0 :                 err = TIFFReadDirEntryCheckRangeByteSlong8(*ma);
    1562           0 :                 if (err != TIFFReadDirEntryErrOk)
    1563           0 :                     break;
    1564           0 :                 *mb++ = (uint8_t)(*ma++);
    1565             :             }
    1566             :         }
    1567           0 :         break;
    1568           0 :         default:
    1569           0 :             break;
    1570             :     }
    1571           0 :     _TIFFfreeExt(tif, origdata);
    1572           0 :     if (err != TIFFReadDirEntryErrOk)
    1573             :     {
    1574           0 :         _TIFFfreeExt(tif, data);
    1575           0 :         return (err);
    1576             :     }
    1577           0 :     *value = data;
    1578           0 :     return (TIFFReadDirEntryErrOk);
    1579             : }
    1580             : 
    1581             : static enum TIFFReadDirEntryErr
    1582           0 : TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value)
    1583             : {
    1584             :     enum TIFFReadDirEntryErr err;
    1585             :     uint32_t count;
    1586             :     void *origdata;
    1587             :     int8_t *data;
    1588           0 :     switch (direntry->tdir_type)
    1589             :     {
    1590           0 :         case TIFF_UNDEFINED:
    1591             :         case TIFF_BYTE:
    1592             :         case TIFF_SBYTE:
    1593             :         case TIFF_SHORT:
    1594             :         case TIFF_SSHORT:
    1595             :         case TIFF_LONG:
    1596             :         case TIFF_SLONG:
    1597             :         case TIFF_LONG8:
    1598             :         case TIFF_SLONG8:
    1599           0 :             break;
    1600           0 :         default:
    1601           0 :             return (TIFFReadDirEntryErrType);
    1602             :     }
    1603           0 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
    1604           0 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    1605             :     {
    1606           0 :         *value = 0;
    1607           0 :         return (err);
    1608             :     }
    1609           0 :     switch (direntry->tdir_type)
    1610             :     {
    1611           0 :         case TIFF_UNDEFINED:
    1612             :         case TIFF_BYTE:
    1613             :         {
    1614             :             uint8_t *m;
    1615             :             uint32_t n;
    1616           0 :             m = (uint8_t *)origdata;
    1617           0 :             for (n = 0; n < count; n++)
    1618             :             {
    1619           0 :                 err = TIFFReadDirEntryCheckRangeSbyteByte(*m);
    1620           0 :                 if (err != TIFFReadDirEntryErrOk)
    1621             :                 {
    1622           0 :                     _TIFFfreeExt(tif, origdata);
    1623           0 :                     return (err);
    1624             :                 }
    1625           0 :                 m++;
    1626             :             }
    1627           0 :             *value = (int8_t *)origdata;
    1628           0 :             return (TIFFReadDirEntryErrOk);
    1629             :         }
    1630           0 :         case TIFF_SBYTE:
    1631           0 :             *value = (int8_t *)origdata;
    1632           0 :             return (TIFFReadDirEntryErrOk);
    1633           0 :         default:
    1634           0 :             break;
    1635             :     }
    1636           0 :     data = (int8_t *)_TIFFmallocExt(tif, count);
    1637           0 :     if (data == 0)
    1638             :     {
    1639           0 :         _TIFFfreeExt(tif, origdata);
    1640           0 :         return (TIFFReadDirEntryErrAlloc);
    1641             :     }
    1642           0 :     switch (direntry->tdir_type)
    1643             :     {
    1644           0 :         case TIFF_SHORT:
    1645             :         {
    1646             :             uint16_t *ma;
    1647             :             int8_t *mb;
    1648             :             uint32_t n;
    1649           0 :             ma = (uint16_t *)origdata;
    1650           0 :             mb = data;
    1651           0 :             for (n = 0; n < count; n++)
    1652             :             {
    1653           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1654           0 :                     TIFFSwabShort(ma);
    1655           0 :                 err = TIFFReadDirEntryCheckRangeSbyteShort(*ma);
    1656           0 :                 if (err != TIFFReadDirEntryErrOk)
    1657           0 :                     break;
    1658           0 :                 *mb++ = (int8_t)(*ma++);
    1659             :             }
    1660             :         }
    1661           0 :         break;
    1662           0 :         case TIFF_SSHORT:
    1663             :         {
    1664             :             int16_t *ma;
    1665             :             int8_t *mb;
    1666             :             uint32_t n;
    1667           0 :             ma = (int16_t *)origdata;
    1668           0 :             mb = data;
    1669           0 :             for (n = 0; n < count; n++)
    1670             :             {
    1671           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1672           0 :                     TIFFSwabShort((uint16_t *)ma);
    1673           0 :                 err = TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
    1674           0 :                 if (err != TIFFReadDirEntryErrOk)
    1675           0 :                     break;
    1676           0 :                 *mb++ = (int8_t)(*ma++);
    1677             :             }
    1678             :         }
    1679           0 :         break;
    1680           0 :         case TIFF_LONG:
    1681             :         {
    1682             :             uint32_t *ma;
    1683             :             int8_t *mb;
    1684             :             uint32_t n;
    1685           0 :             ma = (uint32_t *)origdata;
    1686           0 :             mb = data;
    1687           0 :             for (n = 0; n < count; n++)
    1688             :             {
    1689           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1690           0 :                     TIFFSwabLong(ma);
    1691           0 :                 err = TIFFReadDirEntryCheckRangeSbyteLong(*ma);
    1692           0 :                 if (err != TIFFReadDirEntryErrOk)
    1693           0 :                     break;
    1694           0 :                 *mb++ = (int8_t)(*ma++);
    1695             :             }
    1696             :         }
    1697           0 :         break;
    1698           0 :         case TIFF_SLONG:
    1699             :         {
    1700             :             int32_t *ma;
    1701             :             int8_t *mb;
    1702             :             uint32_t n;
    1703           0 :             ma = (int32_t *)origdata;
    1704           0 :             mb = data;
    1705           0 :             for (n = 0; n < count; n++)
    1706             :             {
    1707           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1708           0 :                     TIFFSwabLong((uint32_t *)ma);
    1709           0 :                 err = TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
    1710           0 :                 if (err != TIFFReadDirEntryErrOk)
    1711           0 :                     break;
    1712           0 :                 *mb++ = (int8_t)(*ma++);
    1713             :             }
    1714             :         }
    1715           0 :         break;
    1716           0 :         case TIFF_LONG8:
    1717             :         {
    1718             :             uint64_t *ma;
    1719             :             int8_t *mb;
    1720             :             uint32_t n;
    1721           0 :             ma = (uint64_t *)origdata;
    1722           0 :             mb = data;
    1723           0 :             for (n = 0; n < count; n++)
    1724             :             {
    1725           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1726           0 :                     TIFFSwabLong8(ma);
    1727           0 :                 err = TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
    1728           0 :                 if (err != TIFFReadDirEntryErrOk)
    1729           0 :                     break;
    1730           0 :                 *mb++ = (int8_t)(*ma++);
    1731             :             }
    1732             :         }
    1733           0 :         break;
    1734           0 :         case TIFF_SLONG8:
    1735             :         {
    1736             :             int64_t *ma;
    1737             :             int8_t *mb;
    1738             :             uint32_t n;
    1739           0 :             ma = (int64_t *)origdata;
    1740           0 :             mb = data;
    1741           0 :             for (n = 0; n < count; n++)
    1742             :             {
    1743           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1744           0 :                     TIFFSwabLong8((uint64_t *)ma);
    1745           0 :                 err = TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
    1746           0 :                 if (err != TIFFReadDirEntryErrOk)
    1747           0 :                     break;
    1748           0 :                 *mb++ = (int8_t)(*ma++);
    1749             :             }
    1750             :         }
    1751           0 :         break;
    1752           0 :         default:
    1753           0 :             break;
    1754             :     }
    1755           0 :     _TIFFfreeExt(tif, origdata);
    1756           0 :     if (err != TIFFReadDirEntryErrOk)
    1757             :     {
    1758           0 :         _TIFFfreeExt(tif, data);
    1759           0 :         return (err);
    1760             :     }
    1761           0 :     *value = data;
    1762           0 :     return (TIFFReadDirEntryErrOk);
    1763             : }
    1764             : 
    1765             : static enum TIFFReadDirEntryErr
    1766       69140 : TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value)
    1767             : {
    1768             :     enum TIFFReadDirEntryErr err;
    1769             :     uint32_t count;
    1770             :     void *origdata;
    1771             :     uint16_t *data;
    1772       69140 :     switch (direntry->tdir_type)
    1773             :     {
    1774       69133 :         case TIFF_BYTE:
    1775             :         case TIFF_SBYTE:
    1776             :         case TIFF_SHORT:
    1777             :         case TIFF_SSHORT:
    1778             :         case TIFF_LONG:
    1779             :         case TIFF_SLONG:
    1780             :         case TIFF_LONG8:
    1781             :         case TIFF_SLONG8:
    1782       69133 :             break;
    1783           7 :         default:
    1784           7 :             return (TIFFReadDirEntryErrType);
    1785             :     }
    1786       69133 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
    1787       69072 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    1788             :     {
    1789           0 :         *value = 0;
    1790           0 :         return (err);
    1791             :     }
    1792       69074 :     switch (direntry->tdir_type)
    1793             :     {
    1794       69041 :         case TIFF_SHORT:
    1795       69041 :             *value = (uint16_t *)origdata;
    1796       69041 :             if (tif->tif_flags & TIFF_SWAB)
    1797        1014 :                 TIFFSwabArrayOfShort(*value, count);
    1798       69073 :             return (TIFFReadDirEntryErrOk);
    1799           0 :         case TIFF_SSHORT:
    1800             :         {
    1801             :             int16_t *m;
    1802             :             uint32_t n;
    1803           0 :             m = (int16_t *)origdata;
    1804           0 :             for (n = 0; n < count; n++)
    1805             :             {
    1806           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1807           0 :                     TIFFSwabShort((uint16_t *)m);
    1808           0 :                 err = TIFFReadDirEntryCheckRangeShortSshort(*m);
    1809           0 :                 if (err != TIFFReadDirEntryErrOk)
    1810             :                 {
    1811           0 :                     _TIFFfreeExt(tif, origdata);
    1812           0 :                     return (err);
    1813             :                 }
    1814           0 :                 m++;
    1815             :             }
    1816           0 :             *value = (uint16_t *)origdata;
    1817           0 :             return (TIFFReadDirEntryErrOk);
    1818             :         }
    1819          33 :         default:
    1820          33 :             break;
    1821             :     }
    1822          33 :     data = (uint16_t *)_TIFFCheckMalloc(tif, count, sizeof(uint16_t),
    1823             :                                         "TIFFReadDirEntryShortArray");
    1824           0 :     if (data == 0)
    1825             :     {
    1826           0 :         _TIFFfreeExt(tif, origdata);
    1827           0 :         return (TIFFReadDirEntryErrAlloc);
    1828             :     }
    1829           0 :     switch (direntry->tdir_type)
    1830             :     {
    1831           0 :         case TIFF_BYTE:
    1832             :         {
    1833             :             uint8_t *ma;
    1834             :             uint16_t *mb;
    1835             :             uint32_t n;
    1836           0 :             ma = (uint8_t *)origdata;
    1837           0 :             mb = data;
    1838           0 :             for (n = 0; n < count; n++)
    1839           0 :                 *mb++ = (uint16_t)(*ma++);
    1840             :         }
    1841           0 :         break;
    1842           0 :         case TIFF_SBYTE:
    1843             :         {
    1844             :             int8_t *ma;
    1845             :             uint16_t *mb;
    1846             :             uint32_t n;
    1847           0 :             ma = (int8_t *)origdata;
    1848           0 :             mb = data;
    1849           0 :             for (n = 0; n < count; n++)
    1850             :             {
    1851           0 :                 err = TIFFReadDirEntryCheckRangeShortSbyte(*ma);
    1852           0 :                 if (err != TIFFReadDirEntryErrOk)
    1853           0 :                     break;
    1854           0 :                 *mb++ = (uint16_t)(*ma++);
    1855             :             }
    1856             :         }
    1857           0 :         break;
    1858           0 :         case TIFF_LONG:
    1859             :         {
    1860             :             uint32_t *ma;
    1861             :             uint16_t *mb;
    1862             :             uint32_t n;
    1863           0 :             ma = (uint32_t *)origdata;
    1864           0 :             mb = data;
    1865           0 :             for (n = 0; n < count; n++)
    1866             :             {
    1867           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1868           0 :                     TIFFSwabLong(ma);
    1869           0 :                 err = TIFFReadDirEntryCheckRangeShortLong(*ma);
    1870           0 :                 if (err != TIFFReadDirEntryErrOk)
    1871           0 :                     break;
    1872           0 :                 *mb++ = (uint16_t)(*ma++);
    1873             :             }
    1874             :         }
    1875           0 :         break;
    1876           0 :         case TIFF_SLONG:
    1877             :         {
    1878             :             int32_t *ma;
    1879             :             uint16_t *mb;
    1880             :             uint32_t n;
    1881           0 :             ma = (int32_t *)origdata;
    1882           0 :             mb = data;
    1883           0 :             for (n = 0; n < count; n++)
    1884             :             {
    1885           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1886           0 :                     TIFFSwabLong((uint32_t *)ma);
    1887           0 :                 err = TIFFReadDirEntryCheckRangeShortSlong(*ma);
    1888           0 :                 if (err != TIFFReadDirEntryErrOk)
    1889           0 :                     break;
    1890           0 :                 *mb++ = (uint16_t)(*ma++);
    1891             :             }
    1892             :         }
    1893           0 :         break;
    1894           0 :         case TIFF_LONG8:
    1895             :         {
    1896             :             uint64_t *ma;
    1897             :             uint16_t *mb;
    1898             :             uint32_t n;
    1899           0 :             ma = (uint64_t *)origdata;
    1900           0 :             mb = data;
    1901           0 :             for (n = 0; n < count; n++)
    1902             :             {
    1903           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1904           0 :                     TIFFSwabLong8(ma);
    1905           0 :                 err = TIFFReadDirEntryCheckRangeShortLong8(*ma);
    1906           0 :                 if (err != TIFFReadDirEntryErrOk)
    1907           0 :                     break;
    1908           0 :                 *mb++ = (uint16_t)(*ma++);
    1909             :             }
    1910             :         }
    1911           0 :         break;
    1912           0 :         case TIFF_SLONG8:
    1913             :         {
    1914             :             int64_t *ma;
    1915             :             uint16_t *mb;
    1916             :             uint32_t n;
    1917           0 :             ma = (int64_t *)origdata;
    1918           0 :             mb = data;
    1919           0 :             for (n = 0; n < count; n++)
    1920             :             {
    1921           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1922           0 :                     TIFFSwabLong8((uint64_t *)ma);
    1923           0 :                 err = TIFFReadDirEntryCheckRangeShortSlong8(*ma);
    1924           0 :                 if (err != TIFFReadDirEntryErrOk)
    1925           0 :                     break;
    1926           0 :                 *mb++ = (uint16_t)(*ma++);
    1927             :             }
    1928             :         }
    1929           0 :         break;
    1930           0 :         default:
    1931           0 :             break;
    1932             :     }
    1933           0 :     _TIFFfreeExt(tif, origdata);
    1934           0 :     if (err != TIFFReadDirEntryErrOk)
    1935             :     {
    1936           0 :         _TIFFfreeExt(tif, data);
    1937           0 :         return (err);
    1938             :     }
    1939           0 :     *value = data;
    1940           0 :     return (TIFFReadDirEntryErrOk);
    1941             : }
    1942             : 
    1943             : static enum TIFFReadDirEntryErr
    1944           0 : TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value)
    1945             : {
    1946             :     enum TIFFReadDirEntryErr err;
    1947             :     uint32_t count;
    1948             :     void *origdata;
    1949             :     int16_t *data;
    1950           0 :     switch (direntry->tdir_type)
    1951             :     {
    1952           0 :         case TIFF_BYTE:
    1953             :         case TIFF_SBYTE:
    1954             :         case TIFF_SHORT:
    1955             :         case TIFF_SSHORT:
    1956             :         case TIFF_LONG:
    1957             :         case TIFF_SLONG:
    1958             :         case TIFF_LONG8:
    1959             :         case TIFF_SLONG8:
    1960           0 :             break;
    1961           0 :         default:
    1962           0 :             return (TIFFReadDirEntryErrType);
    1963             :     }
    1964           0 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
    1965           0 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    1966             :     {
    1967           0 :         *value = 0;
    1968           0 :         return (err);
    1969             :     }
    1970           0 :     switch (direntry->tdir_type)
    1971             :     {
    1972           0 :         case TIFF_SHORT:
    1973             :         {
    1974             :             uint16_t *m;
    1975             :             uint32_t n;
    1976           0 :             m = (uint16_t *)origdata;
    1977           0 :             for (n = 0; n < count; n++)
    1978             :             {
    1979           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1980           0 :                     TIFFSwabShort(m);
    1981           0 :                 err = TIFFReadDirEntryCheckRangeSshortShort(*m);
    1982           0 :                 if (err != TIFFReadDirEntryErrOk)
    1983             :                 {
    1984           0 :                     _TIFFfreeExt(tif, origdata);
    1985           0 :                     return (err);
    1986             :                 }
    1987           0 :                 m++;
    1988             :             }
    1989           0 :             *value = (int16_t *)origdata;
    1990           0 :             return (TIFFReadDirEntryErrOk);
    1991             :         }
    1992           0 :         case TIFF_SSHORT:
    1993           0 :             *value = (int16_t *)origdata;
    1994           0 :             if (tif->tif_flags & TIFF_SWAB)
    1995           0 :                 TIFFSwabArrayOfShort((uint16_t *)(*value), count);
    1996           0 :             return (TIFFReadDirEntryErrOk);
    1997           0 :         default:
    1998           0 :             break;
    1999             :     }
    2000           0 :     data = (int16_t *)_TIFFCheckMalloc(tif, count, sizeof(int16_t),
    2001             :                                        "TIFFReadDirEntrySshortArray");
    2002           0 :     if (data == 0)
    2003             :     {
    2004           0 :         _TIFFfreeExt(tif, origdata);
    2005           0 :         return (TIFFReadDirEntryErrAlloc);
    2006             :     }
    2007           0 :     switch (direntry->tdir_type)
    2008             :     {
    2009           0 :         case TIFF_BYTE:
    2010             :         {
    2011             :             uint8_t *ma;
    2012             :             int16_t *mb;
    2013             :             uint32_t n;
    2014           0 :             ma = (uint8_t *)origdata;
    2015           0 :             mb = data;
    2016           0 :             for (n = 0; n < count; n++)
    2017           0 :                 *mb++ = (int16_t)(*ma++);
    2018             :         }
    2019           0 :         break;
    2020           0 :         case TIFF_SBYTE:
    2021             :         {
    2022             :             int8_t *ma;
    2023             :             int16_t *mb;
    2024             :             uint32_t n;
    2025           0 :             ma = (int8_t *)origdata;
    2026           0 :             mb = data;
    2027           0 :             for (n = 0; n < count; n++)
    2028           0 :                 *mb++ = (int16_t)(*ma++);
    2029             :         }
    2030           0 :         break;
    2031           0 :         case TIFF_LONG:
    2032             :         {
    2033             :             uint32_t *ma;
    2034             :             int16_t *mb;
    2035             :             uint32_t n;
    2036           0 :             ma = (uint32_t *)origdata;
    2037           0 :             mb = data;
    2038           0 :             for (n = 0; n < count; n++)
    2039             :             {
    2040           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2041           0 :                     TIFFSwabLong(ma);
    2042           0 :                 err = TIFFReadDirEntryCheckRangeSshortLong(*ma);
    2043           0 :                 if (err != TIFFReadDirEntryErrOk)
    2044           0 :                     break;
    2045           0 :                 *mb++ = (int16_t)(*ma++);
    2046             :             }
    2047             :         }
    2048           0 :         break;
    2049           0 :         case TIFF_SLONG:
    2050             :         {
    2051             :             int32_t *ma;
    2052             :             int16_t *mb;
    2053             :             uint32_t n;
    2054           0 :             ma = (int32_t *)origdata;
    2055           0 :             mb = data;
    2056           0 :             for (n = 0; n < count; n++)
    2057             :             {
    2058           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2059           0 :                     TIFFSwabLong((uint32_t *)ma);
    2060           0 :                 err = TIFFReadDirEntryCheckRangeSshortSlong(*ma);
    2061           0 :                 if (err != TIFFReadDirEntryErrOk)
    2062           0 :                     break;
    2063           0 :                 *mb++ = (int16_t)(*ma++);
    2064             :             }
    2065             :         }
    2066           0 :         break;
    2067           0 :         case TIFF_LONG8:
    2068             :         {
    2069             :             uint64_t *ma;
    2070             :             int16_t *mb;
    2071             :             uint32_t n;
    2072           0 :             ma = (uint64_t *)origdata;
    2073           0 :             mb = data;
    2074           0 :             for (n = 0; n < count; n++)
    2075             :             {
    2076           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2077           0 :                     TIFFSwabLong8(ma);
    2078           0 :                 err = TIFFReadDirEntryCheckRangeSshortLong8(*ma);
    2079           0 :                 if (err != TIFFReadDirEntryErrOk)
    2080           0 :                     break;
    2081           0 :                 *mb++ = (int16_t)(*ma++);
    2082             :             }
    2083             :         }
    2084           0 :         break;
    2085           0 :         case TIFF_SLONG8:
    2086             :         {
    2087             :             int64_t *ma;
    2088             :             int16_t *mb;
    2089             :             uint32_t n;
    2090           0 :             ma = (int64_t *)origdata;
    2091           0 :             mb = data;
    2092           0 :             for (n = 0; n < count; n++)
    2093             :             {
    2094           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2095           0 :                     TIFFSwabLong8((uint64_t *)ma);
    2096           0 :                 err = TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
    2097           0 :                 if (err != TIFFReadDirEntryErrOk)
    2098           0 :                     break;
    2099           0 :                 *mb++ = (int16_t)(*ma++);
    2100             :             }
    2101             :         }
    2102           0 :         break;
    2103           0 :         default:
    2104           0 :             break;
    2105             :     }
    2106           0 :     _TIFFfreeExt(tif, origdata);
    2107           0 :     if (err != TIFFReadDirEntryErrOk)
    2108             :     {
    2109           0 :         _TIFFfreeExt(tif, data);
    2110           0 :         return (err);
    2111             :     }
    2112           0 :     *value = data;
    2113           0 :     return (TIFFReadDirEntryErrOk);
    2114             : }
    2115             : 
    2116             : static enum TIFFReadDirEntryErr
    2117         800 : TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value)
    2118             : {
    2119             :     enum TIFFReadDirEntryErr err;
    2120             :     uint32_t count;
    2121             :     void *origdata;
    2122             :     uint32_t *data;
    2123         800 :     switch (direntry->tdir_type)
    2124             :     {
    2125         800 :         case TIFF_BYTE:
    2126             :         case TIFF_SBYTE:
    2127             :         case TIFF_SHORT:
    2128             :         case TIFF_SSHORT:
    2129             :         case TIFF_LONG:
    2130             :         case TIFF_SLONG:
    2131             :         case TIFF_LONG8:
    2132             :         case TIFF_SLONG8:
    2133             :         case TIFF_IFD:
    2134             :         case TIFF_IFD8:
    2135         800 :             break;
    2136           0 :         default:
    2137           0 :             return (TIFFReadDirEntryErrType);
    2138             :     }
    2139         800 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
    2140         800 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    2141             :     {
    2142           9 :         *value = 0;
    2143           9 :         return (err);
    2144             :     }
    2145         791 :     switch (direntry->tdir_type)
    2146             :     {
    2147         791 :         case TIFF_LONG:
    2148             :         case TIFF_IFD:
    2149         791 :             *value = (uint32_t *)origdata;
    2150         791 :             if (tif->tif_flags & TIFF_SWAB)
    2151           0 :                 TIFFSwabArrayOfLong(*value, count);
    2152         791 :             return (TIFFReadDirEntryErrOk);
    2153           0 :         case TIFF_SLONG:
    2154             :         {
    2155             :             int32_t *m;
    2156             :             uint32_t n;
    2157           0 :             m = (int32_t *)origdata;
    2158           0 :             for (n = 0; n < count; n++)
    2159             :             {
    2160           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2161           0 :                     TIFFSwabLong((uint32_t *)m);
    2162           0 :                 err = TIFFReadDirEntryCheckRangeLongSlong(*m);
    2163           0 :                 if (err != TIFFReadDirEntryErrOk)
    2164             :                 {
    2165           0 :                     _TIFFfreeExt(tif, origdata);
    2166           0 :                     return (err);
    2167             :                 }
    2168           0 :                 m++;
    2169             :             }
    2170           0 :             *value = (uint32_t *)origdata;
    2171           0 :             return (TIFFReadDirEntryErrOk);
    2172             :         }
    2173           0 :         default:
    2174           0 :             break;
    2175             :     }
    2176           0 :     data = (uint32_t *)_TIFFCheckMalloc(tif, count, sizeof(uint32_t),
    2177             :                                         "TIFFReadDirEntryLongArray");
    2178           0 :     if (data == 0)
    2179             :     {
    2180           0 :         _TIFFfreeExt(tif, origdata);
    2181           0 :         return (TIFFReadDirEntryErrAlloc);
    2182             :     }
    2183           0 :     switch (direntry->tdir_type)
    2184             :     {
    2185           0 :         case TIFF_BYTE:
    2186             :         {
    2187             :             uint8_t *ma;
    2188             :             uint32_t *mb;
    2189             :             uint32_t n;
    2190           0 :             ma = (uint8_t *)origdata;
    2191           0 :             mb = data;
    2192           0 :             for (n = 0; n < count; n++)
    2193           0 :                 *mb++ = (uint32_t)(*ma++);
    2194             :         }
    2195           0 :         break;
    2196           0 :         case TIFF_SBYTE:
    2197             :         {
    2198             :             int8_t *ma;
    2199             :             uint32_t *mb;
    2200             :             uint32_t n;
    2201           0 :             ma = (int8_t *)origdata;
    2202           0 :             mb = data;
    2203           0 :             for (n = 0; n < count; n++)
    2204             :             {
    2205           0 :                 err = TIFFReadDirEntryCheckRangeLongSbyte(*ma);
    2206           0 :                 if (err != TIFFReadDirEntryErrOk)
    2207           0 :                     break;
    2208           0 :                 *mb++ = (uint32_t)(*ma++);
    2209             :             }
    2210             :         }
    2211           0 :         break;
    2212           0 :         case TIFF_SHORT:
    2213             :         {
    2214             :             uint16_t *ma;
    2215             :             uint32_t *mb;
    2216             :             uint32_t n;
    2217           0 :             ma = (uint16_t *)origdata;
    2218           0 :             mb = data;
    2219           0 :             for (n = 0; n < count; n++)
    2220             :             {
    2221           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2222           0 :                     TIFFSwabShort(ma);
    2223           0 :                 *mb++ = (uint32_t)(*ma++);
    2224             :             }
    2225             :         }
    2226           0 :         break;
    2227           0 :         case TIFF_SSHORT:
    2228             :         {
    2229             :             int16_t *ma;
    2230             :             uint32_t *mb;
    2231             :             uint32_t n;
    2232           0 :             ma = (int16_t *)origdata;
    2233           0 :             mb = data;
    2234           0 :             for (n = 0; n < count; n++)
    2235             :             {
    2236           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2237           0 :                     TIFFSwabShort((uint16_t *)ma);
    2238           0 :                 err = TIFFReadDirEntryCheckRangeLongSshort(*ma);
    2239           0 :                 if (err != TIFFReadDirEntryErrOk)
    2240           0 :                     break;
    2241           0 :                 *mb++ = (uint32_t)(*ma++);
    2242             :             }
    2243             :         }
    2244           0 :         break;
    2245           0 :         case TIFF_LONG8:
    2246             :         case TIFF_IFD8:
    2247             :         {
    2248             :             uint64_t *ma;
    2249             :             uint32_t *mb;
    2250             :             uint32_t n;
    2251           0 :             ma = (uint64_t *)origdata;
    2252           0 :             mb = data;
    2253           0 :             for (n = 0; n < count; n++)
    2254             :             {
    2255           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2256           0 :                     TIFFSwabLong8(ma);
    2257           0 :                 err = TIFFReadDirEntryCheckRangeLongLong8(*ma);
    2258           0 :                 if (err != TIFFReadDirEntryErrOk)
    2259           0 :                     break;
    2260           0 :                 *mb++ = (uint32_t)(*ma++);
    2261             :             }
    2262             :         }
    2263           0 :         break;
    2264           0 :         case TIFF_SLONG8:
    2265             :         {
    2266             :             int64_t *ma;
    2267             :             uint32_t *mb;
    2268             :             uint32_t n;
    2269           0 :             ma = (int64_t *)origdata;
    2270           0 :             mb = data;
    2271           0 :             for (n = 0; n < count; n++)
    2272             :             {
    2273           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2274           0 :                     TIFFSwabLong8((uint64_t *)ma);
    2275           0 :                 err = TIFFReadDirEntryCheckRangeLongSlong8(*ma);
    2276           0 :                 if (err != TIFFReadDirEntryErrOk)
    2277           0 :                     break;
    2278           0 :                 *mb++ = (uint32_t)(*ma++);
    2279             :             }
    2280             :         }
    2281           0 :         break;
    2282           0 :         default:
    2283           0 :             break;
    2284             :     }
    2285           0 :     _TIFFfreeExt(tif, origdata);
    2286           0 :     if (err != TIFFReadDirEntryErrOk)
    2287             :     {
    2288           0 :         _TIFFfreeExt(tif, data);
    2289           0 :         return (err);
    2290             :     }
    2291           0 :     *value = data;
    2292           0 :     return (TIFFReadDirEntryErrOk);
    2293             : }
    2294             : 
    2295             : static enum TIFFReadDirEntryErr
    2296           0 : TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value)
    2297             : {
    2298             :     enum TIFFReadDirEntryErr err;
    2299             :     uint32_t count;
    2300             :     void *origdata;
    2301             :     int32_t *data;
    2302           0 :     switch (direntry->tdir_type)
    2303             :     {
    2304           0 :         case TIFF_BYTE:
    2305             :         case TIFF_SBYTE:
    2306             :         case TIFF_SHORT:
    2307             :         case TIFF_SSHORT:
    2308             :         case TIFF_LONG:
    2309             :         case TIFF_SLONG:
    2310             :         case TIFF_LONG8:
    2311             :         case TIFF_SLONG8:
    2312           0 :             break;
    2313           0 :         default:
    2314           0 :             return (TIFFReadDirEntryErrType);
    2315             :     }
    2316           0 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
    2317           0 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    2318             :     {
    2319           0 :         *value = 0;
    2320           0 :         return (err);
    2321             :     }
    2322           0 :     switch (direntry->tdir_type)
    2323             :     {
    2324           0 :         case TIFF_LONG:
    2325             :         {
    2326             :             uint32_t *m;
    2327             :             uint32_t n;
    2328           0 :             m = (uint32_t *)origdata;
    2329           0 :             for (n = 0; n < count; n++)
    2330             :             {
    2331           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2332           0 :                     TIFFSwabLong((uint32_t *)m);
    2333           0 :                 err = TIFFReadDirEntryCheckRangeSlongLong(*m);
    2334           0 :                 if (err != TIFFReadDirEntryErrOk)
    2335             :                 {
    2336           0 :                     _TIFFfreeExt(tif, origdata);
    2337           0 :                     return (err);
    2338             :                 }
    2339           0 :                 m++;
    2340             :             }
    2341           0 :             *value = (int32_t *)origdata;
    2342           0 :             return (TIFFReadDirEntryErrOk);
    2343             :         }
    2344           0 :         case TIFF_SLONG:
    2345           0 :             *value = (int32_t *)origdata;
    2346           0 :             if (tif->tif_flags & TIFF_SWAB)
    2347           0 :                 TIFFSwabArrayOfLong((uint32_t *)(*value), count);
    2348           0 :             return (TIFFReadDirEntryErrOk);
    2349           0 :         default:
    2350           0 :             break;
    2351             :     }
    2352           0 :     data = (int32_t *)_TIFFCheckMalloc(tif, count, sizeof(int32_t),
    2353             :                                        "TIFFReadDirEntrySlongArray");
    2354           0 :     if (data == 0)
    2355             :     {
    2356           0 :         _TIFFfreeExt(tif, origdata);
    2357           0 :         return (TIFFReadDirEntryErrAlloc);
    2358             :     }
    2359           0 :     switch (direntry->tdir_type)
    2360             :     {
    2361           0 :         case TIFF_BYTE:
    2362             :         {
    2363             :             uint8_t *ma;
    2364             :             int32_t *mb;
    2365             :             uint32_t n;
    2366           0 :             ma = (uint8_t *)origdata;
    2367           0 :             mb = data;
    2368           0 :             for (n = 0; n < count; n++)
    2369           0 :                 *mb++ = (int32_t)(*ma++);
    2370             :         }
    2371           0 :         break;
    2372           0 :         case TIFF_SBYTE:
    2373             :         {
    2374             :             int8_t *ma;
    2375             :             int32_t *mb;
    2376             :             uint32_t n;
    2377           0 :             ma = (int8_t *)origdata;
    2378           0 :             mb = data;
    2379           0 :             for (n = 0; n < count; n++)
    2380           0 :                 *mb++ = (int32_t)(*ma++);
    2381             :         }
    2382           0 :         break;
    2383           0 :         case TIFF_SHORT:
    2384             :         {
    2385             :             uint16_t *ma;
    2386             :             int32_t *mb;
    2387             :             uint32_t n;
    2388           0 :             ma = (uint16_t *)origdata;
    2389           0 :             mb = data;
    2390           0 :             for (n = 0; n < count; n++)
    2391             :             {
    2392           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2393           0 :                     TIFFSwabShort(ma);
    2394           0 :                 *mb++ = (int32_t)(*ma++);
    2395             :             }
    2396             :         }
    2397           0 :         break;
    2398           0 :         case TIFF_SSHORT:
    2399             :         {
    2400             :             int16_t *ma;
    2401             :             int32_t *mb;
    2402             :             uint32_t n;
    2403           0 :             ma = (int16_t *)origdata;
    2404           0 :             mb = data;
    2405           0 :             for (n = 0; n < count; n++)
    2406             :             {
    2407           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2408           0 :                     TIFFSwabShort((uint16_t *)ma);
    2409           0 :                 *mb++ = (int32_t)(*ma++);
    2410             :             }
    2411             :         }
    2412           0 :         break;
    2413           0 :         case TIFF_LONG8:
    2414             :         {
    2415             :             uint64_t *ma;
    2416             :             int32_t *mb;
    2417             :             uint32_t n;
    2418           0 :             ma = (uint64_t *)origdata;
    2419           0 :             mb = data;
    2420           0 :             for (n = 0; n < count; n++)
    2421             :             {
    2422           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2423           0 :                     TIFFSwabLong8(ma);
    2424           0 :                 err = TIFFReadDirEntryCheckRangeSlongLong8(*ma);
    2425           0 :                 if (err != TIFFReadDirEntryErrOk)
    2426           0 :                     break;
    2427           0 :                 *mb++ = (int32_t)(*ma++);
    2428             :             }
    2429             :         }
    2430           0 :         break;
    2431           0 :         case TIFF_SLONG8:
    2432             :         {
    2433             :             int64_t *ma;
    2434             :             int32_t *mb;
    2435             :             uint32_t n;
    2436           0 :             ma = (int64_t *)origdata;
    2437           0 :             mb = data;
    2438           0 :             for (n = 0; n < count; n++)
    2439             :             {
    2440           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2441           0 :                     TIFFSwabLong8((uint64_t *)ma);
    2442           0 :                 err = TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
    2443           0 :                 if (err != TIFFReadDirEntryErrOk)
    2444           0 :                     break;
    2445           0 :                 *mb++ = (int32_t)(*ma++);
    2446             :             }
    2447             :         }
    2448           0 :         break;
    2449           0 :         default:
    2450           0 :             break;
    2451             :     }
    2452           0 :     _TIFFfreeExt(tif, origdata);
    2453           0 :     if (err != TIFFReadDirEntryErrOk)
    2454             :     {
    2455           0 :         _TIFFfreeExt(tif, data);
    2456           0 :         return (err);
    2457             :     }
    2458           0 :     *value = data;
    2459           0 :     return (TIFFReadDirEntryErrOk);
    2460             : }
    2461             : 
    2462             : static enum TIFFReadDirEntryErr
    2463       86195 : TIFFReadDirEntryLong8ArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry,
    2464             :                                     uint64_t **value, uint64_t maxcount)
    2465             : {
    2466             :     enum TIFFReadDirEntryErr err;
    2467             :     uint32_t count;
    2468             :     void *origdata;
    2469             :     uint64_t *data;
    2470       86195 :     switch (direntry->tdir_type)
    2471             :     {
    2472       86209 :         case TIFF_BYTE:
    2473             :         case TIFF_SBYTE:
    2474             :         case TIFF_SHORT:
    2475             :         case TIFF_SSHORT:
    2476             :         case TIFF_LONG:
    2477             :         case TIFF_SLONG:
    2478             :         case TIFF_LONG8:
    2479             :         case TIFF_SLONG8:
    2480             :         case TIFF_IFD:
    2481             :         case TIFF_IFD8:
    2482       86209 :             break;
    2483           0 :         default:
    2484           0 :             return (TIFFReadDirEntryErrType);
    2485             :     }
    2486       86209 :     err = TIFFReadDirEntryArrayWithLimit(tif, direntry, &count, 8, &origdata,
    2487             :                                          maxcount);
    2488       86215 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    2489             :     {
    2490          22 :         *value = 0;
    2491          22 :         return (err);
    2492             :     }
    2493       86193 :     switch (direntry->tdir_type)
    2494             :     {
    2495         699 :         case TIFF_LONG8:
    2496             :         case TIFF_IFD8:
    2497         699 :             *value = (uint64_t *)origdata;
    2498         699 :             if (tif->tif_flags & TIFF_SWAB)
    2499          15 :                 TIFFSwabArrayOfLong8(*value, count);
    2500         699 :             return (TIFFReadDirEntryErrOk);
    2501           1 :         case TIFF_SLONG8:
    2502             :         {
    2503             :             int64_t *m;
    2504             :             uint32_t n;
    2505           1 :             m = (int64_t *)origdata;
    2506           2 :             for (n = 0; n < count; n++)
    2507             :             {
    2508           1 :                 if (tif->tif_flags & TIFF_SWAB)
    2509           0 :                     TIFFSwabLong8((uint64_t *)m);
    2510           1 :                 err = TIFFReadDirEntryCheckRangeLong8Slong8(*m);
    2511           1 :                 if (err != TIFFReadDirEntryErrOk)
    2512             :                 {
    2513           0 :                     _TIFFfreeExt(tif, origdata);
    2514           0 :                     return (err);
    2515             :                 }
    2516           1 :                 m++;
    2517             :             }
    2518           1 :             *value = (uint64_t *)origdata;
    2519           1 :             return (TIFFReadDirEntryErrOk);
    2520             :         }
    2521       85493 :         default:
    2522       85493 :             break;
    2523             :     }
    2524       85493 :     data = (uint64_t *)_TIFFCheckMalloc(tif, count, sizeof(uint64_t),
    2525             :                                         "TIFFReadDirEntryLong8ArrayWithLimit");
    2526       85534 :     if (data == 0)
    2527             :     {
    2528          15 :         _TIFFfreeExt(tif, origdata);
    2529           0 :         return (TIFFReadDirEntryErrAlloc);
    2530             :     }
    2531       85519 :     switch (direntry->tdir_type)
    2532             :     {
    2533           1 :         case TIFF_BYTE:
    2534             :         {
    2535             :             uint8_t *ma;
    2536             :             uint64_t *mb;
    2537             :             uint32_t n;
    2538           1 :             ma = (uint8_t *)origdata;
    2539           1 :             mb = data;
    2540           2 :             for (n = 0; n < count; n++)
    2541           1 :                 *mb++ = (uint64_t)(*ma++);
    2542             :         }
    2543           1 :         break;
    2544           0 :         case TIFF_SBYTE:
    2545             :         {
    2546             :             int8_t *ma;
    2547             :             uint64_t *mb;
    2548             :             uint32_t n;
    2549           0 :             ma = (int8_t *)origdata;
    2550           0 :             mb = data;
    2551           0 :             for (n = 0; n < count; n++)
    2552             :             {
    2553           0 :                 err = TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
    2554           0 :                 if (err != TIFFReadDirEntryErrOk)
    2555           0 :                     break;
    2556           0 :                 *mb++ = (uint64_t)(*ma++);
    2557             :             }
    2558             :         }
    2559           0 :         break;
    2560        4142 :         case TIFF_SHORT:
    2561             :         {
    2562             :             uint16_t *ma;
    2563             :             uint64_t *mb;
    2564             :             uint32_t n;
    2565        4142 :             ma = (uint16_t *)origdata;
    2566        4142 :             mb = data;
    2567     3453410 :             for (n = 0; n < count; n++)
    2568             :             {
    2569     3449260 :                 if (tif->tif_flags & TIFF_SWAB)
    2570       13767 :                     TIFFSwabShort(ma);
    2571     3449260 :                 *mb++ = (uint64_t)(*ma++);
    2572             :             }
    2573             :         }
    2574        4142 :         break;
    2575           0 :         case TIFF_SSHORT:
    2576             :         {
    2577             :             int16_t *ma;
    2578             :             uint64_t *mb;
    2579             :             uint32_t n;
    2580           0 :             ma = (int16_t *)origdata;
    2581           0 :             mb = data;
    2582           0 :             for (n = 0; n < count; n++)
    2583             :             {
    2584           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2585           0 :                     TIFFSwabShort((uint16_t *)ma);
    2586           0 :                 err = TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
    2587           0 :                 if (err != TIFFReadDirEntryErrOk)
    2588           0 :                     break;
    2589           0 :                 *mb++ = (uint64_t)(*ma++);
    2590             :             }
    2591             :         }
    2592           0 :         break;
    2593       81376 :         case TIFF_LONG:
    2594             :         case TIFF_IFD:
    2595             :         {
    2596             :             uint32_t *ma;
    2597             :             uint64_t *mb;
    2598             :             uint32_t n;
    2599       81376 :             ma = (uint32_t *)origdata;
    2600       81376 :             mb = data;
    2601     7878860 :             for (n = 0; n < count; n++)
    2602             :             {
    2603     7797500 :                 if (tif->tif_flags & TIFF_SWAB)
    2604      415494 :                     TIFFSwabLong(ma);
    2605     7797480 :                 *mb++ = (uint64_t)(*ma++);
    2606             :             }
    2607             :         }
    2608       81360 :         break;
    2609           0 :         case TIFF_SLONG:
    2610             :         {
    2611             :             int32_t *ma;
    2612             :             uint64_t *mb;
    2613             :             uint32_t n;
    2614           0 :             ma = (int32_t *)origdata;
    2615           0 :             mb = data;
    2616           0 :             for (n = 0; n < count; n++)
    2617             :             {
    2618           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2619           0 :                     TIFFSwabLong((uint32_t *)ma);
    2620           0 :                 err = TIFFReadDirEntryCheckRangeLong8Slong(*ma);
    2621           0 :                 if (err != TIFFReadDirEntryErrOk)
    2622           0 :                     break;
    2623           0 :                 *mb++ = (uint64_t)(*ma++);
    2624             :             }
    2625             :         }
    2626           0 :         break;
    2627           0 :         default:
    2628           0 :             break;
    2629             :     }
    2630       85503 :     _TIFFfreeExt(tif, origdata);
    2631       85515 :     if (err != TIFFReadDirEntryErrOk)
    2632             :     {
    2633          25 :         _TIFFfreeExt(tif, data);
    2634           0 :         return (err);
    2635             :     }
    2636       85490 :     *value = data;
    2637       85490 :     return (TIFFReadDirEntryErrOk);
    2638             : }
    2639             : 
    2640             : static enum TIFFReadDirEntryErr
    2641           6 : TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
    2642             : {
    2643           6 :     return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value,
    2644             :                                                ~((uint64_t)0));
    2645             : }
    2646             : 
    2647             : static enum TIFFReadDirEntryErr
    2648           0 : TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value)
    2649             : {
    2650             :     enum TIFFReadDirEntryErr err;
    2651             :     uint32_t count;
    2652             :     void *origdata;
    2653             :     int64_t *data;
    2654           0 :     switch (direntry->tdir_type)
    2655             :     {
    2656           0 :         case TIFF_BYTE:
    2657             :         case TIFF_SBYTE:
    2658             :         case TIFF_SHORT:
    2659             :         case TIFF_SSHORT:
    2660             :         case TIFF_LONG:
    2661             :         case TIFF_SLONG:
    2662             :         case TIFF_LONG8:
    2663             :         case TIFF_SLONG8:
    2664           0 :             break;
    2665           0 :         default:
    2666           0 :             return (TIFFReadDirEntryErrType);
    2667             :     }
    2668           0 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
    2669           0 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    2670             :     {
    2671           0 :         *value = 0;
    2672           0 :         return (err);
    2673             :     }
    2674           0 :     switch (direntry->tdir_type)
    2675             :     {
    2676           0 :         case TIFF_LONG8:
    2677             :         {
    2678             :             uint64_t *m;
    2679             :             uint32_t n;
    2680           0 :             m = (uint64_t *)origdata;
    2681           0 :             for (n = 0; n < count; n++)
    2682             :             {
    2683           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2684           0 :                     TIFFSwabLong8(m);
    2685           0 :                 err = TIFFReadDirEntryCheckRangeSlong8Long8(*m);
    2686           0 :                 if (err != TIFFReadDirEntryErrOk)
    2687             :                 {
    2688           0 :                     _TIFFfreeExt(tif, origdata);
    2689           0 :                     return (err);
    2690             :                 }
    2691           0 :                 m++;
    2692             :             }
    2693           0 :             *value = (int64_t *)origdata;
    2694           0 :             return (TIFFReadDirEntryErrOk);
    2695             :         }
    2696           0 :         case TIFF_SLONG8:
    2697           0 :             *value = (int64_t *)origdata;
    2698           0 :             if (tif->tif_flags & TIFF_SWAB)
    2699           0 :                 TIFFSwabArrayOfLong8((uint64_t *)(*value), count);
    2700           0 :             return (TIFFReadDirEntryErrOk);
    2701           0 :         default:
    2702           0 :             break;
    2703             :     }
    2704           0 :     data = (int64_t *)_TIFFCheckMalloc(tif, count, sizeof(int64_t),
    2705             :                                        "TIFFReadDirEntrySlong8Array");
    2706           0 :     if (data == 0)
    2707             :     {
    2708           0 :         _TIFFfreeExt(tif, origdata);
    2709           0 :         return (TIFFReadDirEntryErrAlloc);
    2710             :     }
    2711           0 :     switch (direntry->tdir_type)
    2712             :     {
    2713           0 :         case TIFF_BYTE:
    2714             :         {
    2715             :             uint8_t *ma;
    2716             :             int64_t *mb;
    2717             :             uint32_t n;
    2718           0 :             ma = (uint8_t *)origdata;
    2719           0 :             mb = data;
    2720           0 :             for (n = 0; n < count; n++)
    2721           0 :                 *mb++ = (int64_t)(*ma++);
    2722             :         }
    2723           0 :         break;
    2724           0 :         case TIFF_SBYTE:
    2725             :         {
    2726             :             int8_t *ma;
    2727             :             int64_t *mb;
    2728             :             uint32_t n;
    2729           0 :             ma = (int8_t *)origdata;
    2730           0 :             mb = data;
    2731           0 :             for (n = 0; n < count; n++)
    2732           0 :                 *mb++ = (int64_t)(*ma++);
    2733             :         }
    2734           0 :         break;
    2735           0 :         case TIFF_SHORT:
    2736             :         {
    2737             :             uint16_t *ma;
    2738             :             int64_t *mb;
    2739             :             uint32_t n;
    2740           0 :             ma = (uint16_t *)origdata;
    2741           0 :             mb = data;
    2742           0 :             for (n = 0; n < count; n++)
    2743             :             {
    2744           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2745           0 :                     TIFFSwabShort(ma);
    2746           0 :                 *mb++ = (int64_t)(*ma++);
    2747             :             }
    2748             :         }
    2749           0 :         break;
    2750           0 :         case TIFF_SSHORT:
    2751             :         {
    2752             :             int16_t *ma;
    2753             :             int64_t *mb;
    2754             :             uint32_t n;
    2755           0 :             ma = (int16_t *)origdata;
    2756           0 :             mb = data;
    2757           0 :             for (n = 0; n < count; n++)
    2758             :             {
    2759           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2760           0 :                     TIFFSwabShort((uint16_t *)ma);
    2761           0 :                 *mb++ = (int64_t)(*ma++);
    2762             :             }
    2763             :         }
    2764           0 :         break;
    2765           0 :         case TIFF_LONG:
    2766             :         {
    2767             :             uint32_t *ma;
    2768             :             int64_t *mb;
    2769             :             uint32_t n;
    2770           0 :             ma = (uint32_t *)origdata;
    2771           0 :             mb = data;
    2772           0 :             for (n = 0; n < count; n++)
    2773             :             {
    2774           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2775           0 :                     TIFFSwabLong(ma);
    2776           0 :                 *mb++ = (int64_t)(*ma++);
    2777             :             }
    2778             :         }
    2779           0 :         break;
    2780           0 :         case TIFF_SLONG:
    2781             :         {
    2782             :             int32_t *ma;
    2783             :             int64_t *mb;
    2784             :             uint32_t n;
    2785           0 :             ma = (int32_t *)origdata;
    2786           0 :             mb = data;
    2787           0 :             for (n = 0; n < count; n++)
    2788             :             {
    2789           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2790           0 :                     TIFFSwabLong((uint32_t *)ma);
    2791           0 :                 *mb++ = (int64_t)(*ma++);
    2792             :             }
    2793             :         }
    2794           0 :         break;
    2795           0 :         default:
    2796           0 :             break;
    2797             :     }
    2798           0 :     _TIFFfreeExt(tif, origdata);
    2799           0 :     *value = data;
    2800           0 :     return (TIFFReadDirEntryErrOk);
    2801             : }
    2802             : 
    2803             : static enum TIFFReadDirEntryErr
    2804        1026 : TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value)
    2805             : {
    2806             :     enum TIFFReadDirEntryErr err;
    2807             :     uint32_t count;
    2808             :     void *origdata;
    2809             :     float *data;
    2810        1026 :     switch (direntry->tdir_type)
    2811             :     {
    2812        1026 :         case TIFF_BYTE:
    2813             :         case TIFF_SBYTE:
    2814             :         case TIFF_SHORT:
    2815             :         case TIFF_SSHORT:
    2816             :         case TIFF_LONG:
    2817             :         case TIFF_SLONG:
    2818             :         case TIFF_LONG8:
    2819             :         case TIFF_SLONG8:
    2820             :         case TIFF_RATIONAL:
    2821             :         case TIFF_SRATIONAL:
    2822             :         case TIFF_FLOAT:
    2823             :         case TIFF_DOUBLE:
    2824        1026 :             break;
    2825           0 :         default:
    2826           0 :             return (TIFFReadDirEntryErrType);
    2827             :     }
    2828        1026 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
    2829        1026 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    2830             :     {
    2831           0 :         *value = 0;
    2832           0 :         return (err);
    2833             :     }
    2834        1026 :     switch (direntry->tdir_type)
    2835             :     {
    2836           0 :         case TIFF_FLOAT:
    2837           0 :             if (tif->tif_flags & TIFF_SWAB)
    2838           0 :                 TIFFSwabArrayOfLong((uint32_t *)origdata, count);
    2839             :             TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
    2840           0 :             *value = (float *)origdata;
    2841           0 :             return (TIFFReadDirEntryErrOk);
    2842        1026 :         default:
    2843        1026 :             break;
    2844             :     }
    2845        1026 :     data = (float *)_TIFFCheckMalloc(tif, count, sizeof(float),
    2846             :                                      "TIFFReadDirEntryFloatArray");
    2847        1026 :     if (data == 0)
    2848             :     {
    2849           0 :         _TIFFfreeExt(tif, origdata);
    2850           0 :         return (TIFFReadDirEntryErrAlloc);
    2851             :     }
    2852        1026 :     switch (direntry->tdir_type)
    2853             :     {
    2854           0 :         case TIFF_BYTE:
    2855             :         {
    2856             :             uint8_t *ma;
    2857             :             float *mb;
    2858             :             uint32_t n;
    2859           0 :             ma = (uint8_t *)origdata;
    2860           0 :             mb = data;
    2861           0 :             for (n = 0; n < count; n++)
    2862           0 :                 *mb++ = (float)(*ma++);
    2863             :         }
    2864           0 :         break;
    2865           0 :         case TIFF_SBYTE:
    2866             :         {
    2867             :             int8_t *ma;
    2868             :             float *mb;
    2869             :             uint32_t n;
    2870           0 :             ma = (int8_t *)origdata;
    2871           0 :             mb = data;
    2872           0 :             for (n = 0; n < count; n++)
    2873           0 :                 *mb++ = (float)(*ma++);
    2874             :         }
    2875           0 :         break;
    2876           0 :         case TIFF_SHORT:
    2877             :         {
    2878             :             uint16_t *ma;
    2879             :             float *mb;
    2880             :             uint32_t n;
    2881           0 :             ma = (uint16_t *)origdata;
    2882           0 :             mb = data;
    2883           0 :             for (n = 0; n < count; n++)
    2884             :             {
    2885           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2886           0 :                     TIFFSwabShort(ma);
    2887           0 :                 *mb++ = (float)(*ma++);
    2888             :             }
    2889             :         }
    2890           0 :         break;
    2891           0 :         case TIFF_SSHORT:
    2892             :         {
    2893             :             int16_t *ma;
    2894             :             float *mb;
    2895             :             uint32_t n;
    2896           0 :             ma = (int16_t *)origdata;
    2897           0 :             mb = data;
    2898           0 :             for (n = 0; n < count; n++)
    2899             :             {
    2900           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2901           0 :                     TIFFSwabShort((uint16_t *)ma);
    2902           0 :                 *mb++ = (float)(*ma++);
    2903             :             }
    2904             :         }
    2905           0 :         break;
    2906           2 :         case TIFF_LONG:
    2907             :         {
    2908             :             uint32_t *ma;
    2909             :             float *mb;
    2910             :             uint32_t n;
    2911           2 :             ma = (uint32_t *)origdata;
    2912           2 :             mb = data;
    2913          14 :             for (n = 0; n < count; n++)
    2914             :             {
    2915          12 :                 if (tif->tif_flags & TIFF_SWAB)
    2916          12 :                     TIFFSwabLong(ma);
    2917          12 :                 *mb++ = (float)(*ma++);
    2918             :             }
    2919             :         }
    2920           2 :         break;
    2921           0 :         case TIFF_SLONG:
    2922             :         {
    2923             :             int32_t *ma;
    2924             :             float *mb;
    2925             :             uint32_t n;
    2926           0 :             ma = (int32_t *)origdata;
    2927           0 :             mb = data;
    2928           0 :             for (n = 0; n < count; n++)
    2929             :             {
    2930           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2931           0 :                     TIFFSwabLong((uint32_t *)ma);
    2932           0 :                 *mb++ = (float)(*ma++);
    2933             :             }
    2934             :         }
    2935           0 :         break;
    2936           0 :         case TIFF_LONG8:
    2937             :         {
    2938             :             uint64_t *ma;
    2939             :             float *mb;
    2940             :             uint32_t n;
    2941           0 :             ma = (uint64_t *)origdata;
    2942           0 :             mb = data;
    2943           0 :             for (n = 0; n < count; n++)
    2944             :             {
    2945           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2946           0 :                     TIFFSwabLong8(ma);
    2947           0 :                 *mb++ = (float)(*ma++);
    2948             :             }
    2949             :         }
    2950           0 :         break;
    2951           0 :         case TIFF_SLONG8:
    2952             :         {
    2953             :             int64_t *ma;
    2954             :             float *mb;
    2955             :             uint32_t n;
    2956           0 :             ma = (int64_t *)origdata;
    2957           0 :             mb = data;
    2958           0 :             for (n = 0; n < count; n++)
    2959             :             {
    2960           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2961           0 :                     TIFFSwabLong8((uint64_t *)ma);
    2962           0 :                 *mb++ = (float)(*ma++);
    2963             :             }
    2964             :         }
    2965           0 :         break;
    2966        1024 :         case TIFF_RATIONAL:
    2967             :         {
    2968             :             uint32_t *ma;
    2969             :             uint32_t maa;
    2970             :             uint32_t mab;
    2971             :             float *mb;
    2972             :             uint32_t n;
    2973        1024 :             ma = (uint32_t *)origdata;
    2974        1024 :             mb = data;
    2975        7049 :             for (n = 0; n < count; n++)
    2976             :             {
    2977        6025 :                 if (tif->tif_flags & TIFF_SWAB)
    2978          96 :                     TIFFSwabLong(ma);
    2979        6025 :                 maa = *ma++;
    2980        6025 :                 if (tif->tif_flags & TIFF_SWAB)
    2981          96 :                     TIFFSwabLong(ma);
    2982        6025 :                 mab = *ma++;
    2983        6025 :                 if (mab == 0)
    2984           0 :                     *mb++ = 0.0;
    2985             :                 else
    2986        6025 :                     *mb++ = (float)maa / (float)mab;
    2987             :             }
    2988             :         }
    2989        1024 :         break;
    2990           0 :         case TIFF_SRATIONAL:
    2991             :         {
    2992             :             uint32_t *ma;
    2993             :             int32_t maa;
    2994             :             uint32_t mab;
    2995             :             float *mb;
    2996             :             uint32_t n;
    2997           0 :             ma = (uint32_t *)origdata;
    2998           0 :             mb = data;
    2999           0 :             for (n = 0; n < count; n++)
    3000             :             {
    3001           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3002           0 :                     TIFFSwabLong(ma);
    3003           0 :                 maa = *(int32_t *)ma;
    3004           0 :                 ma++;
    3005           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3006           0 :                     TIFFSwabLong(ma);
    3007           0 :                 mab = *ma++;
    3008           0 :                 if (mab == 0)
    3009           0 :                     *mb++ = 0.0;
    3010             :                 else
    3011           0 :                     *mb++ = (float)maa / (float)mab;
    3012             :             }
    3013             :         }
    3014           0 :         break;
    3015           0 :         case TIFF_DOUBLE:
    3016             :         {
    3017             :             double *ma;
    3018             :             float *mb;
    3019             :             uint32_t n;
    3020           0 :             if (tif->tif_flags & TIFF_SWAB)
    3021           0 :                 TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
    3022             :             TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
    3023           0 :             ma = (double *)origdata;
    3024           0 :             mb = data;
    3025           0 :             for (n = 0; n < count; n++)
    3026             :             {
    3027           0 :                 double val = *ma++;
    3028           0 :                 if (val > (double)FLT_MAX)
    3029           0 :                     val = (double)FLT_MAX;
    3030           0 :                 else if (val < -(double)FLT_MAX)
    3031           0 :                     val = -(double)FLT_MAX;
    3032           0 :                 *mb++ = (float)val;
    3033             :             }
    3034             :         }
    3035           0 :         break;
    3036           0 :         default:
    3037           0 :             break;
    3038             :     }
    3039        1026 :     _TIFFfreeExt(tif, origdata);
    3040        1026 :     *value = data;
    3041        1026 :     return (TIFFReadDirEntryErrOk);
    3042             : }
    3043             : 
    3044             : static enum TIFFReadDirEntryErr
    3045       71858 : TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
    3046             : {
    3047             :     enum TIFFReadDirEntryErr err;
    3048             :     uint32_t count;
    3049             :     void *origdata;
    3050             :     double *data;
    3051       71858 :     switch (direntry->tdir_type)
    3052             :     {
    3053       71789 :         case TIFF_BYTE:
    3054             :         case TIFF_SBYTE:
    3055             :         case TIFF_SHORT:
    3056             :         case TIFF_SSHORT:
    3057             :         case TIFF_LONG:
    3058             :         case TIFF_SLONG:
    3059             :         case TIFF_LONG8:
    3060             :         case TIFF_SLONG8:
    3061             :         case TIFF_RATIONAL:
    3062             :         case TIFF_SRATIONAL:
    3063             :         case TIFF_FLOAT:
    3064             :         case TIFF_DOUBLE:
    3065       71789 :             break;
    3066          69 :         default:
    3067          69 :             return (TIFFReadDirEntryErrType);
    3068             :     }
    3069       71789 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
    3070       71806 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    3071             :     {
    3072          95 :         *value = 0;
    3073          95 :         return (err);
    3074             :     }
    3075       71711 :     switch (direntry->tdir_type)
    3076             :     {
    3077       71808 :         case TIFF_DOUBLE:
    3078       71808 :             if (tif->tif_flags & TIFF_SWAB)
    3079         291 :                 TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
    3080             :             TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
    3081       71799 :             *value = (double *)origdata;
    3082       71799 :             return (TIFFReadDirEntryErrOk);
    3083           0 :         default:
    3084           0 :             break;
    3085             :     }
    3086           0 :     data = (double *)_TIFFmallocExt(tif,
    3087           0 :                                     (tmsize_t)((size_t)count * sizeof(double)));
    3088           0 :     if (data == 0)
    3089             :     {
    3090           0 :         _TIFFfreeExt(tif, origdata);
    3091           0 :         return (TIFFReadDirEntryErrAlloc);
    3092             :     }
    3093           0 :     switch (direntry->tdir_type)
    3094             :     {
    3095           0 :         case TIFF_BYTE:
    3096             :         {
    3097             :             uint8_t *ma;
    3098             :             double *mb;
    3099             :             uint32_t n;
    3100           0 :             ma = (uint8_t *)origdata;
    3101           0 :             mb = data;
    3102           0 :             for (n = 0; n < count; n++)
    3103           0 :                 *mb++ = (double)(*ma++);
    3104             :         }
    3105           0 :         break;
    3106           0 :         case TIFF_SBYTE:
    3107             :         {
    3108             :             int8_t *ma;
    3109             :             double *mb;
    3110             :             uint32_t n;
    3111           0 :             ma = (int8_t *)origdata;
    3112           0 :             mb = data;
    3113           0 :             for (n = 0; n < count; n++)
    3114           0 :                 *mb++ = (double)(*ma++);
    3115             :         }
    3116           0 :         break;
    3117           0 :         case TIFF_SHORT:
    3118             :         {
    3119             :             uint16_t *ma;
    3120             :             double *mb;
    3121             :             uint32_t n;
    3122           0 :             ma = (uint16_t *)origdata;
    3123           0 :             mb = data;
    3124           0 :             for (n = 0; n < count; n++)
    3125             :             {
    3126           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3127           0 :                     TIFFSwabShort(ma);
    3128           0 :                 *mb++ = (double)(*ma++);
    3129             :             }
    3130             :         }
    3131           0 :         break;
    3132           0 :         case TIFF_SSHORT:
    3133             :         {
    3134             :             int16_t *ma;
    3135             :             double *mb;
    3136             :             uint32_t n;
    3137           0 :             ma = (int16_t *)origdata;
    3138           0 :             mb = data;
    3139           0 :             for (n = 0; n < count; n++)
    3140             :             {
    3141           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3142           0 :                     TIFFSwabShort((uint16_t *)ma);
    3143           0 :                 *mb++ = (double)(*ma++);
    3144             :             }
    3145             :         }
    3146           0 :         break;
    3147           0 :         case TIFF_LONG:
    3148             :         {
    3149             :             uint32_t *ma;
    3150             :             double *mb;
    3151             :             uint32_t n;
    3152           0 :             ma = (uint32_t *)origdata;
    3153           0 :             mb = data;
    3154           0 :             for (n = 0; n < count; n++)
    3155             :             {
    3156           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3157           0 :                     TIFFSwabLong(ma);
    3158           0 :                 *mb++ = (double)(*ma++);
    3159             :             }
    3160             :         }
    3161           0 :         break;
    3162           0 :         case TIFF_SLONG:
    3163             :         {
    3164             :             int32_t *ma;
    3165             :             double *mb;
    3166             :             uint32_t n;
    3167           0 :             ma = (int32_t *)origdata;
    3168           0 :             mb = data;
    3169           0 :             for (n = 0; n < count; n++)
    3170             :             {
    3171           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3172           0 :                     TIFFSwabLong((uint32_t *)ma);
    3173           0 :                 *mb++ = (double)(*ma++);
    3174             :             }
    3175             :         }
    3176           0 :         break;
    3177           0 :         case TIFF_LONG8:
    3178             :         {
    3179             :             uint64_t *ma;
    3180             :             double *mb;
    3181             :             uint32_t n;
    3182           0 :             ma = (uint64_t *)origdata;
    3183           0 :             mb = data;
    3184           0 :             for (n = 0; n < count; n++)
    3185             :             {
    3186           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3187           0 :                     TIFFSwabLong8(ma);
    3188           0 :                 *mb++ = (double)(*ma++);
    3189             :             }
    3190             :         }
    3191           0 :         break;
    3192           0 :         case TIFF_SLONG8:
    3193             :         {
    3194             :             int64_t *ma;
    3195             :             double *mb;
    3196             :             uint32_t n;
    3197           0 :             ma = (int64_t *)origdata;
    3198           0 :             mb = data;
    3199           0 :             for (n = 0; n < count; n++)
    3200             :             {
    3201           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3202           0 :                     TIFFSwabLong8((uint64_t *)ma);
    3203           0 :                 *mb++ = (double)(*ma++);
    3204             :             }
    3205             :         }
    3206           0 :         break;
    3207           0 :         case TIFF_RATIONAL:
    3208             :         {
    3209             :             uint32_t *ma;
    3210             :             uint32_t maa;
    3211             :             uint32_t mab;
    3212             :             double *mb;
    3213             :             uint32_t n;
    3214           0 :             ma = (uint32_t *)origdata;
    3215           0 :             mb = data;
    3216           0 :             for (n = 0; n < count; n++)
    3217             :             {
    3218           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3219           0 :                     TIFFSwabLong(ma);
    3220           0 :                 maa = *ma++;
    3221           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3222           0 :                     TIFFSwabLong(ma);
    3223           0 :                 mab = *ma++;
    3224           0 :                 if (mab == 0)
    3225           0 :                     *mb++ = 0.0;
    3226             :                 else
    3227           0 :                     *mb++ = (double)maa / (double)mab;
    3228             :             }
    3229             :         }
    3230           0 :         break;
    3231           0 :         case TIFF_SRATIONAL:
    3232             :         {
    3233             :             uint32_t *ma;
    3234             :             int32_t maa;
    3235             :             uint32_t mab;
    3236             :             double *mb;
    3237             :             uint32_t n;
    3238           0 :             ma = (uint32_t *)origdata;
    3239           0 :             mb = data;
    3240           0 :             for (n = 0; n < count; n++)
    3241             :             {
    3242           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3243           0 :                     TIFFSwabLong(ma);
    3244           0 :                 maa = *(int32_t *)ma;
    3245           0 :                 ma++;
    3246           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3247           0 :                     TIFFSwabLong(ma);
    3248           0 :                 mab = *ma++;
    3249           0 :                 if (mab == 0)
    3250           0 :                     *mb++ = 0.0;
    3251             :                 else
    3252           0 :                     *mb++ = (double)maa / (double)mab;
    3253             :             }
    3254             :         }
    3255           0 :         break;
    3256           0 :         case TIFF_FLOAT:
    3257             :         {
    3258             :             float *ma;
    3259             :             double *mb;
    3260             :             uint32_t n;
    3261           0 :             if (tif->tif_flags & TIFF_SWAB)
    3262           0 :                 TIFFSwabArrayOfLong((uint32_t *)origdata, count);
    3263             :             TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
    3264           0 :             ma = (float *)origdata;
    3265           0 :             mb = data;
    3266           0 :             for (n = 0; n < count; n++)
    3267           0 :                 *mb++ = (double)(*ma++);
    3268             :         }
    3269           0 :         break;
    3270           0 :         default:
    3271           0 :             break;
    3272             :     }
    3273           0 :     _TIFFfreeExt(tif, origdata);
    3274           0 :     *value = data;
    3275           0 :     return (TIFFReadDirEntryErrOk);
    3276             : }
    3277             : 
    3278             : static enum TIFFReadDirEntryErr
    3279          81 : TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
    3280             : {
    3281             :     enum TIFFReadDirEntryErr err;
    3282             :     uint32_t count;
    3283             :     void *origdata;
    3284             :     uint64_t *data;
    3285          81 :     switch (direntry->tdir_type)
    3286             :     {
    3287          81 :         case TIFF_LONG:
    3288             :         case TIFF_LONG8:
    3289             :         case TIFF_IFD:
    3290             :         case TIFF_IFD8:
    3291          81 :             break;
    3292           0 :         default:
    3293           0 :             return (TIFFReadDirEntryErrType);
    3294             :     }
    3295          81 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
    3296          81 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    3297             :     {
    3298           0 :         *value = 0;
    3299           0 :         return (err);
    3300             :     }
    3301          81 :     switch (direntry->tdir_type)
    3302             :     {
    3303           0 :         case TIFF_LONG8:
    3304             :         case TIFF_IFD8:
    3305           0 :             *value = (uint64_t *)origdata;
    3306           0 :             if (tif->tif_flags & TIFF_SWAB)
    3307           0 :                 TIFFSwabArrayOfLong8(*value, count);
    3308           0 :             return (TIFFReadDirEntryErrOk);
    3309          81 :         default:
    3310          81 :             break;
    3311             :     }
    3312          81 :     data = (uint64_t *)_TIFFCheckMalloc(tif, count, sizeof(uint64_t),
    3313             :                                         "TIFFReadDirEntryIfd8Array");
    3314          81 :     if (data == 0)
    3315             :     {
    3316           0 :         _TIFFfreeExt(tif, origdata);
    3317           0 :         return (TIFFReadDirEntryErrAlloc);
    3318             :     }
    3319          81 :     switch (direntry->tdir_type)
    3320             :     {
    3321          81 :         case TIFF_LONG:
    3322             :         case TIFF_IFD:
    3323             :         {
    3324             :             uint32_t *ma;
    3325             :             uint64_t *mb;
    3326             :             uint32_t n;
    3327          81 :             ma = (uint32_t *)origdata;
    3328          81 :             mb = data;
    3329         333 :             for (n = 0; n < count; n++)
    3330             :             {
    3331         252 :                 if (tif->tif_flags & TIFF_SWAB)
    3332           0 :                     TIFFSwabLong(ma);
    3333         252 :                 *mb++ = (uint64_t)(*ma++);
    3334             :             }
    3335             :         }
    3336          81 :         break;
    3337           0 :         default:
    3338           0 :             break;
    3339             :     }
    3340          81 :     _TIFFfreeExt(tif, origdata);
    3341          81 :     *value = data;
    3342          81 :     return (TIFFReadDirEntryErrOk);
    3343             : }
    3344             : 
    3345             : static enum TIFFReadDirEntryErr
    3346       32869 : TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
    3347             :                                uint16_t *value)
    3348             : {
    3349             :     enum TIFFReadDirEntryErr err;
    3350             :     uint16_t *m;
    3351             :     uint16_t *na;
    3352             :     uint16_t nb;
    3353       32869 :     if (direntry->tdir_count != (uint64_t)tif->tif_dir.td_samplesperpixel)
    3354             :     {
    3355           0 :         const TIFFField *fip = TIFFFieldWithTag(tif, direntry->tdir_tag);
    3356           0 :         if (direntry->tdir_count == 0)
    3357             :         {
    3358           0 :             return TIFFReadDirEntryErrCount;
    3359             :         }
    3360           0 :         else if (direntry->tdir_count <
    3361           0 :                  (uint64_t)tif->tif_dir.td_samplesperpixel)
    3362             :         {
    3363           0 :             TIFFWarningExtR(
    3364             :                 tif, "TIFFReadDirEntryPersampleShort",
    3365             :                 "Tag %s entry count is %" PRIu64
    3366             :                 " , whereas it should be SamplesPerPixel=%d. Assuming that "
    3367             :                 "missing entries are all at the value of the first one",
    3368             :                 fip ? fip->field_name : "unknown tagname", direntry->tdir_count,
    3369           0 :                 tif->tif_dir.td_samplesperpixel);
    3370             :         }
    3371             :         else
    3372             :         {
    3373           0 :             TIFFWarningExtR(tif, "TIFFReadDirEntryPersampleShort",
    3374             :                             "Tag %s entry count is %" PRIu64
    3375             :                             " , whereas it should be SamplesPerPixel=%d. "
    3376             :                             "Ignoring extra entries",
    3377             :                             fip ? fip->field_name : "unknown tagname",
    3378             :                             direntry->tdir_count,
    3379           0 :                             tif->tif_dir.td_samplesperpixel);
    3380             :         }
    3381             :     }
    3382       32869 :     err = TIFFReadDirEntryShortArray(tif, direntry, &m);
    3383       32873 :     if (err != TIFFReadDirEntryErrOk || m == NULL)
    3384           2 :         return (err);
    3385       32871 :     na = m;
    3386       32871 :     nb = tif->tif_dir.td_samplesperpixel;
    3387       32871 :     if (direntry->tdir_count < nb)
    3388           0 :         nb = (uint16_t)direntry->tdir_count;
    3389       32871 :     *value = *na++;
    3390       32871 :     nb--;
    3391     2593620 :     while (nb > 0)
    3392             :     {
    3393     2560750 :         if (*na++ != *value)
    3394             :         {
    3395           0 :             err = TIFFReadDirEntryErrPsdif;
    3396           0 :             break;
    3397             :         }
    3398     2560750 :         nb--;
    3399             :     }
    3400       32871 :     _TIFFfreeExt(tif, m);
    3401       32874 :     return (err);
    3402             : }
    3403             : 
    3404           0 : static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
    3405             :                                         uint8_t *value)
    3406             : {
    3407             :     (void)tif;
    3408           0 :     *value = *(uint8_t *)(&direntry->tdir_offset);
    3409           0 : }
    3410             : 
    3411           0 : static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
    3412             :                                          int8_t *value)
    3413             : {
    3414             :     (void)tif;
    3415           0 :     *value = *(int8_t *)(&direntry->tdir_offset);
    3416           0 : }
    3417             : 
    3418      476493 : static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
    3419             :                                          uint16_t *value)
    3420             : {
    3421      476493 :     *value = direntry->tdir_offset.toff_short;
    3422             :     /* *value=*(uint16_t*)(&direntry->tdir_offset); */
    3423      476493 :     if (tif->tif_flags & TIFF_SWAB)
    3424        4781 :         TIFFSwabShort(value);
    3425      476493 : }
    3426             : 
    3427           0 : static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
    3428             :                                           int16_t *value)
    3429             : {
    3430           0 :     *value = *(int16_t *)(&direntry->tdir_offset);
    3431           0 :     if (tif->tif_flags & TIFF_SWAB)
    3432           0 :         TIFFSwabShort((uint16_t *)value);
    3433           0 : }
    3434             : 
    3435        6129 : static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
    3436             :                                         uint32_t *value)
    3437             : {
    3438        6129 :     *value = *(uint32_t *)(&direntry->tdir_offset);
    3439        6129 :     if (tif->tif_flags & TIFF_SWAB)
    3440         114 :         TIFFSwabLong(value);
    3441        6129 : }
    3442             : 
    3443           0 : static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
    3444             :                                          int32_t *value)
    3445             : {
    3446           0 :     *value = *(int32_t *)(&direntry->tdir_offset);
    3447           0 :     if (tif->tif_flags & TIFF_SWAB)
    3448           0 :         TIFFSwabLong((uint32_t *)value);
    3449           0 : }
    3450             : 
    3451             : static enum TIFFReadDirEntryErr
    3452           0 : TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
    3453             : {
    3454           0 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    3455             :     {
    3456             :         enum TIFFReadDirEntryErr err;
    3457           0 :         uint32_t offset = direntry->tdir_offset.toff_long;
    3458           0 :         if (tif->tif_flags & TIFF_SWAB)
    3459           0 :             TIFFSwabLong(&offset);
    3460           0 :         err = TIFFReadDirEntryData(tif, offset, 8, value);
    3461           0 :         if (err != TIFFReadDirEntryErrOk)
    3462           0 :             return (err);
    3463             :     }
    3464             :     else
    3465           0 :         *value = direntry->tdir_offset.toff_long8;
    3466           0 :     if (tif->tif_flags & TIFF_SWAB)
    3467           0 :         TIFFSwabLong8(value);
    3468           0 :     return (TIFFReadDirEntryErrOk);
    3469             : }
    3470             : 
    3471             : static enum TIFFReadDirEntryErr
    3472           0 : TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
    3473             : {
    3474           0 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    3475             :     {
    3476             :         enum TIFFReadDirEntryErr err;
    3477           0 :         uint32_t offset = direntry->tdir_offset.toff_long;
    3478           0 :         if (tif->tif_flags & TIFF_SWAB)
    3479           0 :             TIFFSwabLong(&offset);
    3480           0 :         err = TIFFReadDirEntryData(tif, offset, 8, value);
    3481           0 :         if (err != TIFFReadDirEntryErrOk)
    3482           0 :             return (err);
    3483             :     }
    3484             :     else
    3485           0 :         *value = *(int64_t *)(&direntry->tdir_offset);
    3486           0 :     if (tif->tif_flags & TIFF_SWAB)
    3487           0 :         TIFFSwabLong8((uint64_t *)value);
    3488           0 :     return (TIFFReadDirEntryErrOk);
    3489             : }
    3490             : 
    3491             : static enum TIFFReadDirEntryErr
    3492         248 : TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
    3493             :                                 double *value)
    3494             : {
    3495             :     UInt64Aligned_t m;
    3496             : 
    3497             :     assert(sizeof(double) == 8);
    3498             :     assert(sizeof(uint64_t) == 8);
    3499             :     assert(sizeof(uint32_t) == 4);
    3500         248 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    3501             :     {
    3502             :         enum TIFFReadDirEntryErr err;
    3503         248 :         uint32_t offset = direntry->tdir_offset.toff_long;
    3504         248 :         if (tif->tif_flags & TIFF_SWAB)
    3505           8 :             TIFFSwabLong(&offset);
    3506         248 :         err = TIFFReadDirEntryData(tif, offset, 8, m.i);
    3507         248 :         if (err != TIFFReadDirEntryErrOk)
    3508           0 :             return (err);
    3509             :     }
    3510             :     else
    3511           0 :         m.l = direntry->tdir_offset.toff_long8;
    3512         248 :     if (tif->tif_flags & TIFF_SWAB)
    3513           8 :         TIFFSwabArrayOfLong(m.i, 2);
    3514             :     /* Not completely sure what we should do when m.i[1]==0, but some */
    3515             :     /* sanitizers do not like division by 0.0: */
    3516             :     /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
    3517         248 :     if (m.i[0] == 0 || m.i[1] == 0)
    3518          36 :         *value = 0.0;
    3519             :     else
    3520         212 :         *value = (double)m.i[0] / (double)m.i[1];
    3521         248 :     return (TIFFReadDirEntryErrOk);
    3522             : }
    3523             : 
    3524             : static enum TIFFReadDirEntryErr
    3525           0 : TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
    3526             :                                  double *value)
    3527             : {
    3528             :     UInt64Aligned_t m;
    3529             :     assert(sizeof(double) == 8);
    3530             :     assert(sizeof(uint64_t) == 8);
    3531             :     assert(sizeof(int32_t) == 4);
    3532             :     assert(sizeof(uint32_t) == 4);
    3533           0 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    3534             :     {
    3535             :         enum TIFFReadDirEntryErr err;
    3536           0 :         uint32_t offset = direntry->tdir_offset.toff_long;
    3537           0 :         if (tif->tif_flags & TIFF_SWAB)
    3538           0 :             TIFFSwabLong(&offset);
    3539           0 :         err = TIFFReadDirEntryData(tif, offset, 8, m.i);
    3540           0 :         if (err != TIFFReadDirEntryErrOk)
    3541           0 :             return (err);
    3542             :     }
    3543             :     else
    3544           0 :         m.l = direntry->tdir_offset.toff_long8;
    3545           0 :     if (tif->tif_flags & TIFF_SWAB)
    3546           0 :         TIFFSwabArrayOfLong(m.i, 2);
    3547             :     /* Not completely sure what we should do when m.i[1]==0, but some */
    3548             :     /* sanitizers do not like division by 0.0: */
    3549             :     /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
    3550           0 :     if ((int32_t)m.i[0] == 0 || m.i[1] == 0)
    3551           0 :         *value = 0.0;
    3552             :     else
    3553           0 :         *value = (double)((int32_t)m.i[0]) / (double)m.i[1];
    3554           0 :     return (TIFFReadDirEntryErrOk);
    3555             : }
    3556             : 
    3557             : #if 0
    3558             : static enum TIFFReadDirEntryErr
    3559             : TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
    3560             :                                       TIFFRational_t *value)
    3561             : { /*--: SetGetRATIONAL_directly:_CustomTag: Read rational (and signed rationals)
    3562             :      directly --*/
    3563             :     UInt64Aligned_t m;
    3564             : 
    3565             :     assert(sizeof(double) == 8);
    3566             :     assert(sizeof(uint64_t) == 8);
    3567             :     assert(sizeof(uint32_t) == 4);
    3568             : 
    3569             :     if (direntry->tdir_count != 1)
    3570             :         return (TIFFReadDirEntryErrCount);
    3571             : 
    3572             :     if (direntry->tdir_type != TIFF_RATIONAL &&
    3573             :         direntry->tdir_type != TIFF_SRATIONAL)
    3574             :         return (TIFFReadDirEntryErrType);
    3575             : 
    3576             :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    3577             :     {
    3578             :         enum TIFFReadDirEntryErr err;
    3579             :         uint32_t offset = direntry->tdir_offset.toff_long;
    3580             :         if (tif->tif_flags & TIFF_SWAB)
    3581             :             TIFFSwabLong(&offset);
    3582             :         err = TIFFReadDirEntryData(tif, offset, 8, m.i);
    3583             :         if (err != TIFFReadDirEntryErrOk)
    3584             :             return (err);
    3585             :     }
    3586             :     else
    3587             :     {
    3588             :         m.l = direntry->tdir_offset.toff_long8;
    3589             :     }
    3590             : 
    3591             :     if (tif->tif_flags & TIFF_SWAB)
    3592             :         TIFFSwabArrayOfLong(m.i, 2);
    3593             : 
    3594             :     value->uNum = m.i[0];
    3595             :     value->uDenom = m.i[1];
    3596             :     return (TIFFReadDirEntryErrOk);
    3597             : } /*-- TIFFReadDirEntryCheckedRationalDirect() --*/
    3598             : #endif
    3599             : 
    3600           0 : static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
    3601             :                                          float *value)
    3602             : {
    3603             :     union
    3604             :     {
    3605             :         float f;
    3606             :         uint32_t i;
    3607             :     } float_union;
    3608             :     assert(sizeof(float) == 4);
    3609             :     assert(sizeof(uint32_t) == 4);
    3610             :     assert(sizeof(float_union) == 4);
    3611           0 :     float_union.i = *(uint32_t *)(&direntry->tdir_offset);
    3612           0 :     *value = float_union.f;
    3613           0 :     if (tif->tif_flags & TIFF_SWAB)
    3614           0 :         TIFFSwabLong((uint32_t *)value);
    3615           0 : }
    3616             : 
    3617             : static enum TIFFReadDirEntryErr
    3618           0 : TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
    3619             : {
    3620             :     assert(sizeof(double) == 8);
    3621             :     assert(sizeof(uint64_t) == 8);
    3622             :     assert(sizeof(UInt64Aligned_t) == 8);
    3623           0 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    3624             :     {
    3625             :         enum TIFFReadDirEntryErr err;
    3626           0 :         uint32_t offset = direntry->tdir_offset.toff_long;
    3627           0 :         if (tif->tif_flags & TIFF_SWAB)
    3628           0 :             TIFFSwabLong(&offset);
    3629           0 :         err = TIFFReadDirEntryData(tif, offset, 8, value);
    3630           0 :         if (err != TIFFReadDirEntryErrOk)
    3631           0 :             return (err);
    3632             :     }
    3633             :     else
    3634             :     {
    3635             :         UInt64Aligned_t uint64_union;
    3636           0 :         uint64_union.l = direntry->tdir_offset.toff_long8;
    3637           0 :         *value = uint64_union.d;
    3638             :     }
    3639           0 :     if (tif->tif_flags & TIFF_SWAB)
    3640           0 :         TIFFSwabLong8((uint64_t *)value);
    3641           0 :     return (TIFFReadDirEntryErrOk);
    3642             : }
    3643             : 
    3644             : static enum TIFFReadDirEntryErr
    3645           0 : TIFFReadDirEntryCheckRangeByteSbyte(int8_t value)
    3646             : {
    3647           0 :     if (value < 0)
    3648           0 :         return (TIFFReadDirEntryErrRange);
    3649             :     else
    3650           0 :         return (TIFFReadDirEntryErrOk);
    3651             : }
    3652             : 
    3653             : static enum TIFFReadDirEntryErr
    3654           0 : TIFFReadDirEntryCheckRangeByteShort(uint16_t value)
    3655             : {
    3656           0 :     if (value > 0xFF)
    3657           0 :         return (TIFFReadDirEntryErrRange);
    3658             :     else
    3659           0 :         return (TIFFReadDirEntryErrOk);
    3660             : }
    3661             : 
    3662             : static enum TIFFReadDirEntryErr
    3663           0 : TIFFReadDirEntryCheckRangeByteSshort(int16_t value)
    3664             : {
    3665           0 :     if ((value < 0) || (value > 0xFF))
    3666           0 :         return (TIFFReadDirEntryErrRange);
    3667             :     else
    3668           0 :         return (TIFFReadDirEntryErrOk);
    3669             : }
    3670             : 
    3671             : static enum TIFFReadDirEntryErr
    3672           0 : TIFFReadDirEntryCheckRangeByteLong(uint32_t value)
    3673             : {
    3674           0 :     if (value > 0xFF)
    3675           0 :         return (TIFFReadDirEntryErrRange);
    3676             :     else
    3677           0 :         return (TIFFReadDirEntryErrOk);
    3678             : }
    3679             : 
    3680             : static enum TIFFReadDirEntryErr
    3681           0 : TIFFReadDirEntryCheckRangeByteSlong(int32_t value)
    3682             : {
    3683           0 :     if ((value < 0) || (value > 0xFF))
    3684           0 :         return (TIFFReadDirEntryErrRange);
    3685             :     else
    3686           0 :         return (TIFFReadDirEntryErrOk);
    3687             : }
    3688             : 
    3689             : static enum TIFFReadDirEntryErr
    3690           0 : TIFFReadDirEntryCheckRangeByteLong8(uint64_t value)
    3691             : {
    3692           0 :     if (value > 0xFF)
    3693           0 :         return (TIFFReadDirEntryErrRange);
    3694             :     else
    3695           0 :         return (TIFFReadDirEntryErrOk);
    3696             : }
    3697             : 
    3698             : static enum TIFFReadDirEntryErr
    3699           0 : TIFFReadDirEntryCheckRangeByteSlong8(int64_t value)
    3700             : {
    3701           0 :     if ((value < 0) || (value > 0xFF))
    3702           0 :         return (TIFFReadDirEntryErrRange);
    3703             :     else
    3704           0 :         return (TIFFReadDirEntryErrOk);
    3705             : }
    3706             : 
    3707             : static enum TIFFReadDirEntryErr
    3708           0 : TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value)
    3709             : {
    3710           0 :     if (value > 0x7F)
    3711           0 :         return (TIFFReadDirEntryErrRange);
    3712             :     else
    3713           0 :         return (TIFFReadDirEntryErrOk);
    3714             : }
    3715             : 
    3716             : static enum TIFFReadDirEntryErr
    3717           0 : TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value)
    3718             : {
    3719           0 :     if (value > 0x7F)
    3720           0 :         return (TIFFReadDirEntryErrRange);
    3721             :     else
    3722           0 :         return (TIFFReadDirEntryErrOk);
    3723             : }
    3724             : 
    3725             : static enum TIFFReadDirEntryErr
    3726           0 : TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value)
    3727             : {
    3728           0 :     if ((value < -0x80) || (value > 0x7F))
    3729           0 :         return (TIFFReadDirEntryErrRange);
    3730             :     else
    3731           0 :         return (TIFFReadDirEntryErrOk);
    3732             : }
    3733             : 
    3734             : static enum TIFFReadDirEntryErr
    3735           0 : TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value)
    3736             : {
    3737           0 :     if (value > 0x7F)
    3738           0 :         return (TIFFReadDirEntryErrRange);
    3739             :     else
    3740           0 :         return (TIFFReadDirEntryErrOk);
    3741             : }
    3742             : 
    3743             : static enum TIFFReadDirEntryErr
    3744           0 : TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value)
    3745             : {
    3746           0 :     if ((value < -0x80) || (value > 0x7F))
    3747           0 :         return (TIFFReadDirEntryErrRange);
    3748             :     else
    3749           0 :         return (TIFFReadDirEntryErrOk);
    3750             : }
    3751             : 
    3752             : static enum TIFFReadDirEntryErr
    3753           0 : TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value)
    3754             : {
    3755           0 :     if (value > 0x7F)
    3756           0 :         return (TIFFReadDirEntryErrRange);
    3757             :     else
    3758           0 :         return (TIFFReadDirEntryErrOk);
    3759             : }
    3760             : 
    3761             : static enum TIFFReadDirEntryErr
    3762           0 : TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value)
    3763             : {
    3764           0 :     if ((value < -0x80) || (value > 0x7F))
    3765           0 :         return (TIFFReadDirEntryErrRange);
    3766             :     else
    3767           0 :         return (TIFFReadDirEntryErrOk);
    3768             : }
    3769             : 
    3770             : static enum TIFFReadDirEntryErr
    3771           0 : TIFFReadDirEntryCheckRangeShortSbyte(int8_t value)
    3772             : {
    3773           0 :     if (value < 0)
    3774           0 :         return (TIFFReadDirEntryErrRange);
    3775             :     else
    3776           0 :         return (TIFFReadDirEntryErrOk);
    3777             : }
    3778             : 
    3779             : static enum TIFFReadDirEntryErr
    3780           0 : TIFFReadDirEntryCheckRangeShortSshort(int16_t value)
    3781             : {
    3782           0 :     if (value < 0)
    3783           0 :         return (TIFFReadDirEntryErrRange);
    3784             :     else
    3785           0 :         return (TIFFReadDirEntryErrOk);
    3786             : }
    3787             : 
    3788             : static enum TIFFReadDirEntryErr
    3789           1 : TIFFReadDirEntryCheckRangeShortLong(uint32_t value)
    3790             : {
    3791           1 :     if (value > 0xFFFF)
    3792           0 :         return (TIFFReadDirEntryErrRange);
    3793             :     else
    3794           1 :         return (TIFFReadDirEntryErrOk);
    3795             : }
    3796             : 
    3797             : static enum TIFFReadDirEntryErr
    3798           0 : TIFFReadDirEntryCheckRangeShortSlong(int32_t value)
    3799             : {
    3800           0 :     if ((value < 0) || (value > 0xFFFF))
    3801           0 :         return (TIFFReadDirEntryErrRange);
    3802             :     else
    3803           0 :         return (TIFFReadDirEntryErrOk);
    3804             : }
    3805             : 
    3806             : static enum TIFFReadDirEntryErr
    3807           0 : TIFFReadDirEntryCheckRangeShortLong8(uint64_t value)
    3808             : {
    3809           0 :     if (value > 0xFFFF)
    3810           0 :         return (TIFFReadDirEntryErrRange);
    3811             :     else
    3812           0 :         return (TIFFReadDirEntryErrOk);
    3813             : }
    3814             : 
    3815             : static enum TIFFReadDirEntryErr
    3816           0 : TIFFReadDirEntryCheckRangeShortSlong8(int64_t value)
    3817             : {
    3818           0 :     if ((value < 0) || (value > 0xFFFF))
    3819           0 :         return (TIFFReadDirEntryErrRange);
    3820             :     else
    3821           0 :         return (TIFFReadDirEntryErrOk);
    3822             : }
    3823             : 
    3824             : static enum TIFFReadDirEntryErr
    3825           0 : TIFFReadDirEntryCheckRangeSshortShort(uint16_t value)
    3826             : {
    3827           0 :     if (value > 0x7FFF)
    3828           0 :         return (TIFFReadDirEntryErrRange);
    3829             :     else
    3830           0 :         return (TIFFReadDirEntryErrOk);
    3831             : }
    3832             : 
    3833             : static enum TIFFReadDirEntryErr
    3834           0 : TIFFReadDirEntryCheckRangeSshortLong(uint32_t value)
    3835             : {
    3836           0 :     if (value > 0x7FFF)
    3837           0 :         return (TIFFReadDirEntryErrRange);
    3838             :     else
    3839           0 :         return (TIFFReadDirEntryErrOk);
    3840             : }
    3841             : 
    3842             : static enum TIFFReadDirEntryErr
    3843           0 : TIFFReadDirEntryCheckRangeSshortSlong(int32_t value)
    3844             : {
    3845           0 :     if ((value < -0x8000) || (value > 0x7FFF))
    3846           0 :         return (TIFFReadDirEntryErrRange);
    3847             :     else
    3848           0 :         return (TIFFReadDirEntryErrOk);
    3849             : }
    3850             : 
    3851             : static enum TIFFReadDirEntryErr
    3852           0 : TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value)
    3853             : {
    3854           0 :     if (value > 0x7FFF)
    3855           0 :         return (TIFFReadDirEntryErrRange);
    3856             :     else
    3857           0 :         return (TIFFReadDirEntryErrOk);
    3858             : }
    3859             : 
    3860             : static enum TIFFReadDirEntryErr
    3861           0 : TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value)
    3862             : {
    3863           0 :     if ((value < -0x8000) || (value > 0x7FFF))
    3864           0 :         return (TIFFReadDirEntryErrRange);
    3865             :     else
    3866           0 :         return (TIFFReadDirEntryErrOk);
    3867             : }
    3868             : 
    3869             : static enum TIFFReadDirEntryErr
    3870           0 : TIFFReadDirEntryCheckRangeLongSbyte(int8_t value)
    3871             : {
    3872           0 :     if (value < 0)
    3873           0 :         return (TIFFReadDirEntryErrRange);
    3874             :     else
    3875           0 :         return (TIFFReadDirEntryErrOk);
    3876             : }
    3877             : 
    3878             : static enum TIFFReadDirEntryErr
    3879           0 : TIFFReadDirEntryCheckRangeLongSshort(int16_t value)
    3880             : {
    3881           0 :     if (value < 0)
    3882           0 :         return (TIFFReadDirEntryErrRange);
    3883             :     else
    3884           0 :         return (TIFFReadDirEntryErrOk);
    3885             : }
    3886             : 
    3887             : static enum TIFFReadDirEntryErr
    3888           0 : TIFFReadDirEntryCheckRangeLongSlong(int32_t value)
    3889             : {
    3890           0 :     if (value < 0)
    3891           0 :         return (TIFFReadDirEntryErrRange);
    3892             :     else
    3893           0 :         return (TIFFReadDirEntryErrOk);
    3894             : }
    3895             : 
    3896             : static enum TIFFReadDirEntryErr
    3897           0 : TIFFReadDirEntryCheckRangeLongLong8(uint64_t value)
    3898             : {
    3899           0 :     if (value > UINT32_MAX)
    3900           0 :         return (TIFFReadDirEntryErrRange);
    3901             :     else
    3902           0 :         return (TIFFReadDirEntryErrOk);
    3903             : }
    3904             : 
    3905             : static enum TIFFReadDirEntryErr
    3906           0 : TIFFReadDirEntryCheckRangeLongSlong8(int64_t value)
    3907             : {
    3908           0 :     if ((value < 0) || (value > (int64_t)UINT32_MAX))
    3909           0 :         return (TIFFReadDirEntryErrRange);
    3910             :     else
    3911           0 :         return (TIFFReadDirEntryErrOk);
    3912             : }
    3913             : 
    3914             : static enum TIFFReadDirEntryErr
    3915           0 : TIFFReadDirEntryCheckRangeSlongLong(uint32_t value)
    3916             : {
    3917           0 :     if (value > 0x7FFFFFFFUL)
    3918           0 :         return (TIFFReadDirEntryErrRange);
    3919             :     else
    3920           0 :         return (TIFFReadDirEntryErrOk);
    3921             : }
    3922             : 
    3923             : /* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
    3924             : static enum TIFFReadDirEntryErr
    3925           0 : TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value)
    3926             : {
    3927           0 :     if (value > 0x7FFFFFFF)
    3928           0 :         return (TIFFReadDirEntryErrRange);
    3929             :     else
    3930           0 :         return (TIFFReadDirEntryErrOk);
    3931             : }
    3932             : 
    3933             : /* Check that the 8-byte signed value can fit in a 4-byte signed range */
    3934             : static enum TIFFReadDirEntryErr
    3935           0 : TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value)
    3936             : {
    3937           0 :     if ((value < 0 - ((int64_t)0x7FFFFFFF + 1)) || (value > 0x7FFFFFFF))
    3938           0 :         return (TIFFReadDirEntryErrRange);
    3939             :     else
    3940           0 :         return (TIFFReadDirEntryErrOk);
    3941             : }
    3942             : 
    3943             : static enum TIFFReadDirEntryErr
    3944           0 : TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value)
    3945             : {
    3946           0 :     if (value < 0)
    3947           0 :         return (TIFFReadDirEntryErrRange);
    3948             :     else
    3949           0 :         return (TIFFReadDirEntryErrOk);
    3950             : }
    3951             : 
    3952             : static enum TIFFReadDirEntryErr
    3953           0 : TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value)
    3954             : {
    3955           0 :     if (value < 0)
    3956           0 :         return (TIFFReadDirEntryErrRange);
    3957             :     else
    3958           0 :         return (TIFFReadDirEntryErrOk);
    3959             : }
    3960             : 
    3961             : static enum TIFFReadDirEntryErr
    3962           0 : TIFFReadDirEntryCheckRangeLong8Slong(int32_t value)
    3963             : {
    3964           0 :     if (value < 0)
    3965           0 :         return (TIFFReadDirEntryErrRange);
    3966             :     else
    3967           0 :         return (TIFFReadDirEntryErrOk);
    3968             : }
    3969             : 
    3970             : static enum TIFFReadDirEntryErr
    3971           1 : TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value)
    3972             : {
    3973           1 :     if (value < 0)
    3974           0 :         return (TIFFReadDirEntryErrRange);
    3975             :     else
    3976           1 :         return (TIFFReadDirEntryErrOk);
    3977             : }
    3978             : 
    3979             : static enum TIFFReadDirEntryErr
    3980           0 : TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value)
    3981             : {
    3982           0 :     if (value > INT64_MAX)
    3983           0 :         return (TIFFReadDirEntryErrRange);
    3984             :     else
    3985           0 :         return (TIFFReadDirEntryErrOk);
    3986             : }
    3987             : 
    3988         280 : static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
    3989             :                                                      tmsize_t size, void *dest)
    3990             : {
    3991         280 :     assert(size > 0);
    3992         280 :     if (!isMapped(tif))
    3993             :     {
    3994         248 :         if (!SeekOK(tif, offset))
    3995           0 :             return (TIFFReadDirEntryErrIo);
    3996         248 :         if (!ReadOK(tif, dest, size))
    3997           0 :             return (TIFFReadDirEntryErrIo);
    3998             :     }
    3999             :     else
    4000             :     {
    4001             :         size_t ma, mb;
    4002          32 :         ma = (size_t)offset;
    4003          32 :         if ((uint64_t)ma != offset || ma > (~(size_t)0) - (size_t)size)
    4004             :         {
    4005           0 :             return TIFFReadDirEntryErrIo;
    4006             :         }
    4007          32 :         mb = (uint64_t)ma + (uint64_t)size;
    4008          32 :         if (mb > (uint64_t)tif->tif_size)
    4009           0 :             return (TIFFReadDirEntryErrIo);
    4010          32 :         _TIFFmemcpy(dest, tif->tif_base + ma, size);
    4011             :     }
    4012         280 :     return (TIFFReadDirEntryErrOk);
    4013             : }
    4014             : 
    4015          44 : static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
    4016             :                                       const char *module, const char *tagname,
    4017             :                                       int recover)
    4018             : {
    4019          44 :     if (!recover)
    4020             :     {
    4021           2 :         switch (err)
    4022             :         {
    4023           0 :             case TIFFReadDirEntryErrCount:
    4024           0 :                 TIFFErrorExtR(tif, module, "Incorrect count for \"%s\"",
    4025             :                               tagname);
    4026           0 :                 break;
    4027           0 :             case TIFFReadDirEntryErrType:
    4028           0 :                 TIFFErrorExtR(tif, module, "Incompatible type for \"%s\"",
    4029             :                               tagname);
    4030           0 :                 break;
    4031           2 :             case TIFFReadDirEntryErrIo:
    4032           2 :                 TIFFErrorExtR(tif, module, "IO error during reading of \"%s\"",
    4033             :                               tagname);
    4034           2 :                 break;
    4035           0 :             case TIFFReadDirEntryErrRange:
    4036           0 :                 TIFFErrorExtR(tif, module, "Incorrect value for \"%s\"",
    4037             :                               tagname);
    4038           0 :                 break;
    4039           0 :             case TIFFReadDirEntryErrPsdif:
    4040           0 :                 TIFFErrorExtR(
    4041             :                     tif, module,
    4042             :                     "Cannot handle different values per sample for \"%s\"",
    4043             :                     tagname);
    4044           0 :                 break;
    4045           0 :             case TIFFReadDirEntryErrSizesan:
    4046           0 :                 TIFFErrorExtR(tif, module,
    4047             :                               "Sanity check on size of \"%s\" value failed",
    4048             :                               tagname);
    4049           0 :                 break;
    4050           0 :             case TIFFReadDirEntryErrAlloc:
    4051           0 :                 TIFFErrorExtR(tif, module, "Out of memory reading of \"%s\"",
    4052             :                               tagname);
    4053           0 :                 break;
    4054           0 :             case TIFFReadDirEntryErrOk:
    4055             :             default:
    4056           0 :                 assert(0); /* we should never get here */
    4057             :                 break;
    4058             :         }
    4059             :     }
    4060             :     else
    4061             :     {
    4062          42 :         switch (err)
    4063             :         {
    4064           5 :             case TIFFReadDirEntryErrCount:
    4065           5 :                 TIFFWarningExtR(tif, module,
    4066             :                                 "Incorrect count for \"%s\"; tag ignored",
    4067             :                                 tagname);
    4068           5 :                 break;
    4069           0 :             case TIFFReadDirEntryErrType:
    4070           0 :                 TIFFWarningExtR(tif, module,
    4071             :                                 "Incompatible type for \"%s\"; tag ignored",
    4072             :                                 tagname);
    4073           0 :                 break;
    4074          19 :             case TIFFReadDirEntryErrIo:
    4075          19 :                 TIFFWarningExtR(
    4076             :                     tif, module,
    4077             :                     "IO error during reading of \"%s\"; tag ignored", tagname);
    4078          19 :                 break;
    4079           0 :             case TIFFReadDirEntryErrRange:
    4080           0 :                 TIFFWarningExtR(tif, module,
    4081             :                                 "Incorrect value for \"%s\"; tag ignored",
    4082             :                                 tagname);
    4083           0 :                 break;
    4084           0 :             case TIFFReadDirEntryErrPsdif:
    4085           0 :                 TIFFWarningExtR(tif, module,
    4086             :                                 "Cannot handle different values per sample for "
    4087             :                                 "\"%s\"; tag ignored",
    4088             :                                 tagname);
    4089           0 :                 break;
    4090           3 :             case TIFFReadDirEntryErrSizesan:
    4091           3 :                 TIFFWarningExtR(
    4092             :                     tif, module,
    4093             :                     "Sanity check on size of \"%s\" value failed; tag ignored",
    4094             :                     tagname);
    4095           3 :                 break;
    4096          15 :             case TIFFReadDirEntryErrAlloc:
    4097          15 :                 TIFFWarningExtR(tif, module,
    4098             :                                 "Out of memory reading of \"%s\"; tag ignored",
    4099             :                                 tagname);
    4100          15 :                 break;
    4101           0 :             case TIFFReadDirEntryErrOk:
    4102             :             default:
    4103           0 :                 assert(0); /* we should never get here */
    4104             :                 break;
    4105             :         }
    4106             :     }
    4107          44 : }
    4108             : 
    4109             : /*
    4110             :  * Return the maximum number of color channels specified for a given photometric
    4111             :  * type. 0 is returned if photometric type isn't supported or no default value
    4112             :  * is defined by the specification.
    4113             :  */
    4114       55012 : static int _TIFFGetMaxColorChannels(uint16_t photometric)
    4115             : {
    4116       55012 :     switch (photometric)
    4117             :     {
    4118       39778 :         case PHOTOMETRIC_PALETTE:
    4119             :         case PHOTOMETRIC_MINISWHITE:
    4120             :         case PHOTOMETRIC_MINISBLACK:
    4121       39778 :             return 1;
    4122       14384 :         case PHOTOMETRIC_YCBCR:
    4123             :         case PHOTOMETRIC_RGB:
    4124             :         case PHOTOMETRIC_CIELAB:
    4125             :         case PHOTOMETRIC_LOGLUV:
    4126             :         case PHOTOMETRIC_ITULAB:
    4127             :         case PHOTOMETRIC_ICCLAB:
    4128       14384 :             return 3;
    4129         824 :         case PHOTOMETRIC_SEPARATED:
    4130             :         case PHOTOMETRIC_MASK:
    4131         824 :             return 4;
    4132          26 :         case PHOTOMETRIC_LOGL:
    4133             :         case PHOTOMETRIC_CFA:
    4134             :         default:
    4135          26 :             return 0;
    4136             :     }
    4137             : }
    4138             : 
    4139       34240 : static int ByteCountLooksBad(TIFF *tif)
    4140             : {
    4141             :     /*
    4142             :      * Assume we have wrong StripByteCount value (in case
    4143             :      * of single strip) in following cases:
    4144             :      *   - it is equal to zero along with StripOffset;
    4145             :      *   - it is larger than file itself (in case of uncompressed
    4146             :      *     image);
    4147             :      *   - it is smaller than the size of the bytes per row
    4148             :      *     multiplied on the number of rows.  The last case should
    4149             :      *     not be checked in the case of writing new image,
    4150             :      *     because we may do not know the exact strip size
    4151             :      *     until the whole image will be written and directory
    4152             :      *     dumped out.
    4153             :      */
    4154       34240 :     uint64_t bytecount = TIFFGetStrileByteCount(tif, 0);
    4155       34262 :     uint64_t offset = TIFFGetStrileOffset(tif, 0);
    4156             :     uint64_t filesize;
    4157             : 
    4158       34247 :     if (offset == 0)
    4159       11111 :         return 0;
    4160       23136 :     if (bytecount == 0)
    4161           1 :         return 1;
    4162       23135 :     if (tif->tif_dir.td_compression != COMPRESSION_NONE)
    4163         774 :         return 0;
    4164       22361 :     filesize = TIFFGetFileSize(tif);
    4165       22383 :     if (offset <= filesize && bytecount > filesize - offset)
    4166           2 :         return 1;
    4167       22381 :     if (tif->tif_mode == O_RDONLY)
    4168             :     {
    4169       21235 :         uint64_t scanlinesize = TIFFScanlineSize64(tif);
    4170       21220 :         if (tif->tif_dir.td_imagelength > 0 &&
    4171       21218 :             scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength)
    4172             :         {
    4173           0 :             return 1;
    4174             :         }
    4175       21220 :         if (bytecount < scanlinesize * tif->tif_dir.td_imagelength)
    4176           2 :             return 1;
    4177             :     }
    4178       22364 :     return 0;
    4179             : }
    4180             : 
    4181             : /*
    4182             :  * To evaluate the IFD data size when reading, save the offset and data size of
    4183             :  * all data that does not fit into the IFD entries themselves.
    4184             :  */
    4185      369219 : static bool EvaluateIFDdatasizeReading(TIFF *tif, TIFFDirEntry *dp)
    4186             : {
    4187      369007 :     const uint64_t data_width =
    4188      369219 :         (uint64_t)TIFFDataWidth((TIFFDataType)dp->tdir_type);
    4189      369007 :     if (data_width != 0 && dp->tdir_count > UINT64_MAX / data_width)
    4190             :     {
    4191           1 :         TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
    4192             :                       "Too large IFD data size");
    4193           1 :         return false;
    4194             :     }
    4195      369006 :     const uint64_t datalength = dp->tdir_count * data_width;
    4196      369006 :     if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
    4197             :     {
    4198      200866 :         if (tif->tif_dir.td_dirdatasize_read > UINT64_MAX - datalength)
    4199             :         {
    4200           0 :             TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
    4201             :                           "Too large IFD data size");
    4202           0 :             return false;
    4203             :         }
    4204      200866 :         tif->tif_dir.td_dirdatasize_read += datalength;
    4205      200866 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    4206             :         {
    4207             :             /* The offset of TIFFDirEntry are not swapped when read in. That has
    4208             :              * to be done when used. */
    4209      199003 :             uint32_t offset = dp->tdir_offset.toff_long;
    4210      199003 :             if (tif->tif_flags & TIFF_SWAB)
    4211        2100 :                 TIFFSwabLong(&offset);
    4212             :             tif->tif_dir
    4213      199012 :                 .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
    4214      199012 :                 .offset = (uint64_t)offset;
    4215             :         }
    4216             :         else
    4217             :         {
    4218             :             tif->tif_dir
    4219        1863 :                 .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
    4220        1863 :                 .offset = dp->tdir_offset.toff_long8;
    4221        1863 :             if (tif->tif_flags & TIFF_SWAB)
    4222          30 :                 TIFFSwabLong8(
    4223             :                     &tif->tif_dir
    4224          30 :                          .td_dirdatasize_offsets[tif->tif_dir
    4225          30 :                                                      .td_dirdatasize_Noffsets]
    4226             :                          .offset);
    4227             :         }
    4228             :         tif->tif_dir
    4229      200889 :             .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
    4230      200889 :             .length = datalength;
    4231      200889 :         tif->tif_dir.td_dirdatasize_Noffsets++;
    4232             :     }
    4233      369029 :     return true;
    4234             : }
    4235             : 
    4236             : /*
    4237             :  * Compare function for qsort() sorting TIFFEntryOffsetAndLength array entries.
    4238             :  */
    4239       74559 : static int cmpTIFFEntryOffsetAndLength(const void *a, const void *b)
    4240             : {
    4241       74559 :     const TIFFEntryOffsetAndLength *ta = (const TIFFEntryOffsetAndLength *)a;
    4242       74559 :     const TIFFEntryOffsetAndLength *tb = (const TIFFEntryOffsetAndLength *)b;
    4243             :     /* Compare offsets */
    4244       74559 :     if (ta->offset > tb->offset)
    4245        9963 :         return 1;
    4246       64596 :     else if (ta->offset < tb->offset)
    4247       64596 :         return -1;
    4248             :     else
    4249           0 :         return 0;
    4250             : }
    4251             : 
    4252             : /*
    4253             :  * Determine the IFD data size after reading an IFD from the file that can be
    4254             :  * overwritten and saving it in tif_dir.td_dirdatasize_read. This data size
    4255             :  * includes the IFD entries themselves as well as the data that does not fit
    4256             :  * directly into the IFD entries but is located directly after the IFD entries
    4257             :  * in the file.
    4258             :  */
    4259       54968 : static void CalcFinalIFDdatasizeReading(TIFF *tif, uint16_t dircount)
    4260             : {
    4261             :     /* IFD data size is only needed if file-writing is enabled.
    4262             :      * This also avoids the seek() to EOF to determine the file size, which
    4263             :      * causes the stdin-streaming-friendly mode of libtiff for GDAL to fail. */
    4264       54968 :     if (tif->tif_mode == O_RDONLY)
    4265       35776 :         return;
    4266             : 
    4267             :     /* Sort TIFFEntryOffsetAndLength array in ascending order. */
    4268       19192 :     qsort(tif->tif_dir.td_dirdatasize_offsets,
    4269       19192 :           tif->tif_dir.td_dirdatasize_Noffsets,
    4270             :           sizeof(TIFFEntryOffsetAndLength), cmpTIFFEntryOffsetAndLength);
    4271             : 
    4272             :     /* Get offset of end of IFD entry space. */
    4273             :     uint64_t IFDendoffset;
    4274       19174 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    4275       18619 :         IFDendoffset = tif->tif_diroff + 2 + (uint64_t)dircount * 12 + 4;
    4276             :     else
    4277         555 :         IFDendoffset = tif->tif_diroff + 8 + (uint64_t)dircount * 20 + 8;
    4278             : 
    4279             :     /* Check which offsets are right behind IFD entries. However, LibTIFF
    4280             :      * increments the writing address for every external data to an even offset.
    4281             :      * Thus gaps of 1 byte can occur. */
    4282       19174 :     uint64_t size = 0;
    4283             :     uint64_t offset;
    4284             :     uint32_t i;
    4285       82484 :     for (i = 0; i < tif->tif_dir.td_dirdatasize_Noffsets; i++)
    4286             :     {
    4287       63346 :         offset = tif->tif_dir.td_dirdatasize_offsets[i].offset;
    4288       63346 :         if (offset == IFDendoffset)
    4289             :         {
    4290       60682 :             size += tif->tif_dir.td_dirdatasize_offsets[i].length;
    4291       60682 :             IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
    4292             :         }
    4293        2664 :         else if (offset == IFDendoffset + 1)
    4294             :         {
    4295             :             /* Add gap byte after previous IFD data set. */
    4296        2628 :             size += tif->tif_dir.td_dirdatasize_offsets[i].length + 1;
    4297        2628 :             IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
    4298             :         }
    4299             :         else
    4300             :         {
    4301             :             /* Further data is no more continuously after IFD */
    4302          36 :             break;
    4303             :         }
    4304             :     }
    4305             :     /* Check for gap byte of some easy cases. This should cover 90% of cases.
    4306             :      * Otherwise, IFD will be re-written even it might be safely overwritten. */
    4307       19174 :     if (tif->tif_nextdiroff != 0)
    4308             :     {
    4309        2900 :         if (tif->tif_nextdiroff == IFDendoffset + 1)
    4310         780 :             size++;
    4311             :     }
    4312             :     else
    4313             :     {
    4314             :         /* Check for IFD data ends at EOF. Then IFD can always be safely
    4315             :          * overwritten. */
    4316       16274 :         offset = TIFFSeekFile(tif, 0, SEEK_END);
    4317       16274 :         if (offset == IFDendoffset)
    4318             :         {
    4319       14754 :             tif->tif_dir.td_dirdatasize_read = UINT64_MAX;
    4320       14754 :             return;
    4321             :         }
    4322             :     }
    4323             : 
    4324             :     /* Finally, add the size of the IFD tag entries themselves. */
    4325        4420 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    4326        4214 :         tif->tif_dir.td_dirdatasize_read = 2U + dircount * 12U + 4U + size;
    4327             :     else
    4328         206 :         tif->tif_dir.td_dirdatasize_read = 8U + dircount * 20U + 8U + size;
    4329             : } /*-- CalcFinalIFDdatasizeReading() --*/
    4330             : 
    4331             : /*
    4332             :  * Read the next TIFF directory from a file and convert it to the internal
    4333             :  * format. We read directories sequentially.
    4334             :  */
    4335       63719 : int TIFFReadDirectory(TIFF *tif)
    4336             : {
    4337             :     static const char module[] = "TIFFReadDirectory";
    4338             :     TIFFDirEntry *dir;
    4339             :     uint16_t dircount;
    4340             :     TIFFDirEntry *dp;
    4341             :     uint16_t di;
    4342             :     const TIFFField *fip;
    4343       63719 :     uint32_t fii = FAILED_FII;
    4344             :     toff_t nextdiroff;
    4345       63719 :     int bitspersample_read = FALSE;
    4346             :     int color_channels;
    4347             : 
    4348       63719 :     if (tif->tif_nextdiroff == 0)
    4349             :     {
    4350             :         /* In this special case, tif_diroff needs also to be set to 0.
    4351             :          * This is behind the last IFD, thus no checking or reading necessary.
    4352             :          */
    4353        8693 :         tif->tif_diroff = tif->tif_nextdiroff;
    4354        8693 :         return 0;
    4355             :     }
    4356             : 
    4357       55026 :     nextdiroff = tif->tif_nextdiroff;
    4358             :     /* tif_curdir++ and tif_nextdiroff should only be updated after SUCCESSFUL
    4359             :      * reading of the directory. Otherwise, invalid IFD offsets could corrupt
    4360             :      * the IFD list. */
    4361       55026 :     if (!_TIFFCheckDirNumberAndOffset(tif,
    4362       55026 :                                       tif->tif_curdir ==
    4363             :                                               TIFF_NON_EXISTENT_DIR_NUMBER
    4364             :                                           ? 0
    4365        3826 :                                           : tif->tif_curdir + 1,
    4366             :                                       nextdiroff))
    4367             :     {
    4368           0 :         return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT
    4369             :                      IFDs) */
    4370             :     }
    4371       54997 :     dircount = TIFFFetchDirectory(tif, nextdiroff, &dir, &tif->tif_nextdiroff);
    4372       55007 :     if (!dircount)
    4373             :     {
    4374          21 :         TIFFErrorExtR(tif, module,
    4375             :                       "Failed to read directory at offset %" PRIu64,
    4376             :                       nextdiroff);
    4377          17 :         return 0;
    4378             :     }
    4379             :     /* Set global values after a valid directory has been fetched.
    4380             :      * tif_diroff is already set to nextdiroff in TIFFFetchDirectory() in the
    4381             :      * beginning. */
    4382       54986 :     if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER)
    4383       51161 :         tif->tif_curdir = 0;
    4384             :     else
    4385        3825 :         tif->tif_curdir++;
    4386             : 
    4387       54986 :     TIFFReadDirectoryCheckOrder(tif, dir, dircount);
    4388             : 
    4389             :     /*
    4390             :      * Mark duplicates of any tag to be ignored (bugzilla 1994)
    4391             :      * to avoid certain pathological problems.
    4392             :      */
    4393             :     {
    4394             :         TIFFDirEntry *ma;
    4395             :         uint16_t mb;
    4396      832985 :         for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
    4397             :         {
    4398             :             TIFFDirEntry *na;
    4399             :             uint16_t nb;
    4400     6054230 :             for (na = ma + 1, nb = (uint16_t)(mb + 1); nb < dircount;
    4401     5276250 :                  na++, nb++)
    4402             :             {
    4403     5276250 :                 if (ma->tdir_tag == na->tdir_tag)
    4404             :                 {
    4405       12237 :                     na->tdir_ignore = TRUE;
    4406             :                 }
    4407             :             }
    4408             :         }
    4409             :     }
    4410             : 
    4411       55003 :     tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
    4412       55003 :     tif->tif_flags &= ~TIFF_BUF4WRITE;   /* reset before new dir */
    4413       55003 :     tif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;
    4414             : 
    4415             :     /* When changing directory, in deferred strile loading mode, we must also
    4416             :      * unset the TIFF_LAZYSTRILELOAD_DONE bit if it was initially set,
    4417             :      * to make sure the strile offset/bytecount are read again (when they fit
    4418             :      * in the tag data area).
    4419             :      */
    4420       55003 :     tif->tif_flags &= ~TIFF_LAZYSTRILELOAD_DONE;
    4421             : 
    4422             :     /* Free any old stuff and reinit i/o and other parameters within
    4423             :      * TIFFDefaultDirectory() since we are starting on a new directory. */
    4424       55003 :     TIFFFreeDirectory(tif);
    4425       54976 :     TIFFDefaultDirectory(tif);
    4426             : 
    4427             :     /* After setup a fresh directory indicate that now active IFD is also
    4428             :      * present on file, even if its entries could not be read successfully
    4429             :      * below.  */
    4430       54993 :     tif->tif_dir.td_iswrittentofile = TRUE;
    4431             : 
    4432             :     /* Allocate arrays for offset values outside IFD entry for IFD data size
    4433             :      * checking. Note: Counter are reset within TIFFFreeDirectory(). */
    4434       54981 :     tif->tif_dir.td_dirdatasize_offsets =
    4435       54993 :         (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
    4436             :             tif,
    4437       54993 :             (tmsize_t)((size_t)dircount * sizeof(TIFFEntryOffsetAndLength)));
    4438       54981 :     if (tif->tif_dir.td_dirdatasize_offsets == NULL)
    4439             :     {
    4440           0 :         TIFFErrorExtR(
    4441             :             tif, module,
    4442             :             "Failed to allocate memory for counting IFD data size at reading");
    4443           0 :         goto bad;
    4444             :     }
    4445             :     /*
    4446             :      * Electronic Arts writes gray-scale TIFF files
    4447             :      * without a PlanarConfiguration directory entry.
    4448             :      * Thus we setup a default value here, even though
    4449             :      * the TIFF spec says there is no default value.
    4450             :      * After PlanarConfiguration is preset in TIFFDefaultDirectory()
    4451             :      * the following setting is not needed, but does not harm either.
    4452             :      */
    4453       54981 :     TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    4454             :     /*
    4455             :      * Setup default value and then make a pass over
    4456             :      * the fields to check type and tag information,
    4457             :      * and to extract info required to size data
    4458             :      * structures.  A second pass is made afterwards
    4459             :      * to read in everything not taken in the first pass.
    4460             :      * But we must process the Compression tag first
    4461             :      * in order to merge in codec-private tag definitions (otherwise
    4462             :      * we may get complaints about unknown tags).  However, the
    4463             :      * Compression tag may be dependent on the SamplesPerPixel
    4464             :      * tag value because older TIFF specs permitted Compression
    4465             :      * to be written as a SamplesPerPixel-count tag entry.
    4466             :      * Thus if we don't first figure out the correct SamplesPerPixel
    4467             :      * tag value then we may end up ignoring the Compression tag
    4468             :      * value because it has an incorrect count value (if the
    4469             :      * true value of SamplesPerPixel is not 1).
    4470             :      */
    4471             :     dp =
    4472       54976 :         TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_SAMPLESPERPIXEL);
    4473       54983 :     if (dp)
    4474             :     {
    4475       54953 :         if (!TIFFFetchNormalTag(tif, dp, 0))
    4476           0 :             goto bad;
    4477       54933 :         dp->tdir_ignore = TRUE;
    4478             :     }
    4479       54963 :     dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_COMPRESSION);
    4480       54981 :     if (dp)
    4481             :     {
    4482             :         /*
    4483             :          * The 5.0 spec says the Compression tag has one value, while
    4484             :          * earlier specs say it has one value per sample.  Because of
    4485             :          * this, we accept the tag if one value is supplied with either
    4486             :          * count.
    4487             :          */
    4488             :         uint16_t value;
    4489             :         enum TIFFReadDirEntryErr err;
    4490       54962 :         err = TIFFReadDirEntryShort(tif, dp, &value);
    4491       54970 :         if (err == TIFFReadDirEntryErrCount)
    4492           0 :             err = TIFFReadDirEntryPersampleShort(tif, dp, &value);
    4493       54930 :         if (err != TIFFReadDirEntryErrOk)
    4494             :         {
    4495           0 :             TIFFReadDirEntryOutputErr(tif, err, module, "Compression", 0);
    4496           0 :             goto bad;
    4497             :         }
    4498       54930 :         if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, value))
    4499           0 :             goto bad;
    4500       54973 :         dp->tdir_ignore = TRUE;
    4501             :     }
    4502             :     else
    4503             :     {
    4504          19 :         if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE))
    4505           0 :             goto bad;
    4506             :     }
    4507             :     /*
    4508             :      * First real pass over the directory.
    4509             :      */
    4510      832932 :     for (di = 0, dp = dir; di < dircount; di++, dp++)
    4511             :     {
    4512      777768 :         if (!dp->tdir_ignore)
    4513             :         {
    4514      667260 :             TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
    4515      667597 :             if (fii == FAILED_FII)
    4516             :             {
    4517         437 :                 if (tif->tif_warn_about_unknown_tags)
    4518             :                 {
    4519           0 :                     TIFFWarningExtR(tif, module,
    4520             :                                     "Unknown field with tag %" PRIu16
    4521             :                                     " (0x%" PRIx16 ") encountered",
    4522           0 :                                     dp->tdir_tag, dp->tdir_tag);
    4523             :                 }
    4524             :                 /* the following knowingly leaks the
    4525             :                    anonymous field structure */
    4526         437 :                 const TIFFField *fld = _TIFFCreateAnonField(
    4527         437 :                     tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
    4528         437 :                 if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
    4529             :                 {
    4530           0 :                     TIFFWarningExtR(
    4531             :                         tif, module,
    4532             :                         "Registering anonymous field with tag %" PRIu16
    4533             :                         " (0x%" PRIx16 ") failed",
    4534           0 :                         dp->tdir_tag, dp->tdir_tag);
    4535           0 :                     dp->tdir_ignore = TRUE;
    4536             :                 }
    4537             :                 else
    4538             :                 {
    4539         437 :                     TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
    4540         437 :                     assert(fii != FAILED_FII);
    4541             :                 }
    4542             :             }
    4543             :         }
    4544      778078 :         if (!dp->tdir_ignore)
    4545             :         {
    4546      667558 :             fip = tif->tif_fields[fii];
    4547      667558 :             if (fip->field_bit == FIELD_IGNORE)
    4548           0 :                 dp->tdir_ignore = TRUE;
    4549             :             else
    4550             :             {
    4551      667558 :                 switch (dp->tdir_tag)
    4552             :                 {
    4553      109994 :                     case TIFFTAG_STRIPOFFSETS:
    4554             :                     case TIFFTAG_STRIPBYTECOUNTS:
    4555             :                     case TIFFTAG_TILEOFFSETS:
    4556             :                     case TIFFTAG_TILEBYTECOUNTS:
    4557      109994 :                         TIFFSetFieldBit(tif, fip->field_bit);
    4558      109994 :                         break;
    4559      235222 :                     case TIFFTAG_IMAGEWIDTH:
    4560             :                     case TIFFTAG_IMAGELENGTH:
    4561             :                     case TIFFTAG_IMAGEDEPTH:
    4562             :                     case TIFFTAG_TILELENGTH:
    4563             :                     case TIFFTAG_TILEWIDTH:
    4564             :                     case TIFFTAG_TILEDEPTH:
    4565             :                     case TIFFTAG_PLANARCONFIG:
    4566             :                     case TIFFTAG_ROWSPERSTRIP:
    4567             :                     case TIFFTAG_EXTRASAMPLES:
    4568      235222 :                         if (!TIFFFetchNormalTag(tif, dp, 0))
    4569           0 :                             goto bad;
    4570      235178 :                         dp->tdir_ignore = TRUE;
    4571      235178 :                         break;
    4572      322342 :                     default:
    4573      322342 :                         if (!_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag))
    4574           9 :                             dp->tdir_ignore = TRUE;
    4575      322256 :                         break;
    4576             :                 }
    4577      110520 :             }
    4578             :         }
    4579             :     }
    4580             :     /*
    4581             :      * XXX: OJPEG hack.
    4582             :      * If a) compression is OJPEG, b) planarconfig tag says it's separate,
    4583             :      * c) strip offsets/bytecounts tag are both present and
    4584             :      * d) both contain exactly one value, then we consistently find
    4585             :      * that the buggy implementation of the buggy compression scheme
    4586             :      * matches contig planarconfig best. So we 'fix-up' the tag here
    4587             :      */
    4588       55164 :     if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
    4589           2 :         (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE))
    4590             :     {
    4591           0 :         if (!_TIFFFillStriles(tif))
    4592           0 :             goto bad;
    4593           0 :         dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
    4594             :                                         TIFFTAG_STRIPOFFSETS);
    4595           0 :         if ((dp != 0) && (dp->tdir_count == 1))
    4596             :         {
    4597           0 :             dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
    4598             :                                             TIFFTAG_STRIPBYTECOUNTS);
    4599           0 :             if ((dp != 0) && (dp->tdir_count == 1))
    4600             :             {
    4601           0 :                 tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
    4602           0 :                 TIFFWarningExtR(tif, module,
    4603             :                                 "Planarconfig tag value assumed incorrect, "
    4604             :                                 "assuming data is contig instead of chunky");
    4605             :             }
    4606             :         }
    4607             :     }
    4608             :     /*
    4609             :      * Allocate directory structure and setup defaults.
    4610             :      */
    4611       54996 :     if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS))
    4612             :     {
    4613           0 :         MissingRequired(tif, "ImageLength");
    4614           0 :         goto bad;
    4615             :     }
    4616             : 
    4617             :     /*
    4618             :      * Second pass: extract other information.
    4619             :      */
    4620      832749 :     for (di = 0, dp = dir; di < dircount; di++, dp++)
    4621             :     {
    4622      777644 :         if (!dp->tdir_ignore)
    4623             :         {
    4624      431992 :             switch (dp->tdir_tag)
    4625             :             {
    4626      109166 :                 case TIFFTAG_MINSAMPLEVALUE:
    4627             :                 case TIFFTAG_MAXSAMPLEVALUE:
    4628             :                 case TIFFTAG_BITSPERSAMPLE:
    4629             :                 case TIFFTAG_DATATYPE:
    4630             :                 case TIFFTAG_SAMPLEFORMAT:
    4631             :                     /*
    4632             :                      * The MinSampleValue, MaxSampleValue, BitsPerSample
    4633             :                      * DataType and SampleFormat tags are supposed to be
    4634             :                      * written as one value/sample, but some vendors
    4635             :                      * incorrectly write one value only -- so we accept
    4636             :                      * that as well (yuck). Other vendors write correct
    4637             :                      * value for NumberOfSamples, but incorrect one for
    4638             :                      * BitsPerSample and friends, and we will read this
    4639             :                      * too.
    4640             :                      */
    4641             :                     {
    4642             :                         uint16_t value;
    4643             :                         enum TIFFReadDirEntryErr err;
    4644      109166 :                         err = TIFFReadDirEntryShort(tif, dp, &value);
    4645      109148 :                         if (!EvaluateIFDdatasizeReading(tif, dp))
    4646           0 :                             goto bad;
    4647      109136 :                         if (err == TIFFReadDirEntryErrCount)
    4648             :                             err =
    4649       32869 :                                 TIFFReadDirEntryPersampleShort(tif, dp, &value);
    4650      109132 :                         if (err != TIFFReadDirEntryErrOk)
    4651             :                         {
    4652           0 :                             fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4653           0 :                             TIFFReadDirEntryOutputErr(
    4654             :                                 tif, err, module,
    4655             :                                 fip ? fip->field_name : "unknown tagname", 0);
    4656           0 :                             goto bad;
    4657             :                         }
    4658      109132 :                         if (!TIFFSetField(tif, dp->tdir_tag, value))
    4659           0 :                             goto bad;
    4660      109153 :                         if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE)
    4661       54945 :                             bitspersample_read = TRUE;
    4662             :                     }
    4663      109153 :                     break;
    4664           0 :                 case TIFFTAG_SMINSAMPLEVALUE:
    4665             :                 case TIFFTAG_SMAXSAMPLEVALUE:
    4666             :                 {
    4667             : 
    4668           0 :                     double *data = NULL;
    4669             :                     enum TIFFReadDirEntryErr err;
    4670             :                     uint32_t saved_flags;
    4671             :                     int m;
    4672           0 :                     if (dp->tdir_count !=
    4673           0 :                         (uint64_t)tif->tif_dir.td_samplesperpixel)
    4674           0 :                         err = TIFFReadDirEntryErrCount;
    4675             :                     else
    4676           0 :                         err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
    4677           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    4678           0 :                         goto bad;
    4679           0 :                     if (err != TIFFReadDirEntryErrOk)
    4680             :                     {
    4681           0 :                         fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4682           0 :                         TIFFReadDirEntryOutputErr(
    4683             :                             tif, err, module,
    4684             :                             fip ? fip->field_name : "unknown tagname", 0);
    4685           0 :                         goto bad;
    4686             :                     }
    4687           0 :                     saved_flags = tif->tif_flags;
    4688           0 :                     tif->tif_flags |= TIFF_PERSAMPLE;
    4689           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    4690           0 :                     tif->tif_flags = saved_flags;
    4691           0 :                     _TIFFfreeExt(tif, data);
    4692           0 :                     if (!m)
    4693           0 :                         goto bad;
    4694             :                 }
    4695           0 :                 break;
    4696       54972 :                 case TIFFTAG_STRIPOFFSETS:
    4697             :                 case TIFFTAG_TILEOFFSETS:
    4698             :                 {
    4699       54972 :                     switch (dp->tdir_type)
    4700             :                     {
    4701       53537 :                         case TIFF_SHORT:
    4702             :                         case TIFF_LONG:
    4703             :                         case TIFF_LONG8:
    4704       53537 :                             break;
    4705        1435 :                         default:
    4706             :                             /* Warn except if directory typically created with
    4707             :                              * TIFFDeferStrileArrayWriting() */
    4708        1435 :                             if (!(tif->tif_mode == O_RDWR &&
    4709        1402 :                                   dp->tdir_count == 0 && dp->tdir_type == 0 &&
    4710        1402 :                                   dp->tdir_offset.toff_long8 == 0))
    4711             :                             {
    4712          33 :                                 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4713          11 :                                 TIFFWarningExtR(
    4714             :                                     tif, module, "Invalid data type for tag %s",
    4715             :                                     fip ? fip->field_name : "unknown tagname");
    4716             :                             }
    4717        1413 :                             break;
    4718             :                     }
    4719       54950 :                     _TIFFmemcpy(&(tif->tif_dir.td_stripoffset_entry), dp,
    4720             :                                 sizeof(TIFFDirEntry));
    4721       54928 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    4722           1 :                         goto bad;
    4723             :                 }
    4724       54962 :                 break;
    4725       54968 :                 case TIFFTAG_STRIPBYTECOUNTS:
    4726             :                 case TIFFTAG_TILEBYTECOUNTS:
    4727             :                 {
    4728       54968 :                     switch (dp->tdir_type)
    4729             :                     {
    4730       53548 :                         case TIFF_SHORT:
    4731             :                         case TIFF_LONG:
    4732             :                         case TIFF_LONG8:
    4733       53548 :                             break;
    4734        1420 :                         default:
    4735             :                             /* Warn except if directory typically created with
    4736             :                              * TIFFDeferStrileArrayWriting() */
    4737        1420 :                             if (!(tif->tif_mode == O_RDWR &&
    4738        1402 :                                   dp->tdir_count == 0 && dp->tdir_type == 0 &&
    4739        1402 :                                   dp->tdir_offset.toff_long8 == 0))
    4740             :                             {
    4741          18 :                                 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4742           9 :                                 TIFFWarningExtR(
    4743             :                                     tif, module, "Invalid data type for tag %s",
    4744             :                                     fip ? fip->field_name : "unknown tagname");
    4745             :                             }
    4746        1411 :                             break;
    4747             :                     }
    4748       54959 :                     _TIFFmemcpy(&(tif->tif_dir.td_stripbytecount_entry), dp,
    4749             :                                 sizeof(TIFFDirEntry));
    4750       54937 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    4751           0 :                         goto bad;
    4752             :                 }
    4753       54976 :                 break;
    4754         376 :                 case TIFFTAG_COLORMAP:
    4755             :                 case TIFFTAG_TRANSFERFUNCTION:
    4756             :                 {
    4757             :                     enum TIFFReadDirEntryErr err;
    4758             :                     uint32_t countpersample;
    4759             :                     uint32_t countrequired;
    4760             :                     uint32_t incrementpersample;
    4761         376 :                     uint16_t *value = NULL;
    4762             :                     /* It would be dangerous to instantiate those tag values */
    4763             :                     /* since if td_bitspersample has not yet been read (due to
    4764             :                      */
    4765             :                     /* unordered tags), it could be read afterwards with a */
    4766             :                     /* values greater than the default one (1), which may cause
    4767             :                      */
    4768             :                     /* crashes in user code */
    4769         376 :                     if (!bitspersample_read)
    4770             :                     {
    4771           0 :                         fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4772           0 :                         TIFFWarningExtR(
    4773             :                             tif, module,
    4774             :                             "Ignoring %s since BitsPerSample tag not found",
    4775             :                             fip ? fip->field_name : "unknown tagname");
    4776           0 :                         continue;
    4777             :                     }
    4778             :                     /* ColorMap or TransferFunction for high bit */
    4779             :                     /* depths do not make much sense and could be */
    4780             :                     /* used as a denial of service vector */
    4781         376 :                     if (tif->tif_dir.td_bitspersample > 24)
    4782             :                     {
    4783           0 :                         fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4784           0 :                         TIFFWarningExtR(
    4785             :                             tif, module,
    4786             :                             "Ignoring %s because BitsPerSample=%" PRIu16 ">24",
    4787             :                             fip ? fip->field_name : "unknown tagname",
    4788           0 :                             tif->tif_dir.td_bitspersample);
    4789           0 :                         continue;
    4790             :                     }
    4791         376 :                     countpersample = (1U << tif->tif_dir.td_bitspersample);
    4792         376 :                     if ((dp->tdir_tag == TIFFTAG_TRANSFERFUNCTION) &&
    4793          15 :                         (dp->tdir_count == (uint64_t)countpersample))
    4794             :                     {
    4795           0 :                         countrequired = countpersample;
    4796           0 :                         incrementpersample = 0;
    4797             :                     }
    4798             :                     else
    4799             :                     {
    4800         376 :                         countrequired = 3 * countpersample;
    4801         376 :                         incrementpersample = countpersample;
    4802             :                     }
    4803         376 :                     if (dp->tdir_count != (uint64_t)countrequired)
    4804           0 :                         err = TIFFReadDirEntryErrCount;
    4805             :                     else
    4806         376 :                         err = TIFFReadDirEntryShortArray(tif, dp, &value);
    4807         376 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    4808           0 :                         goto bad;
    4809         376 :                     if (err != TIFFReadDirEntryErrOk)
    4810             :                     {
    4811           1 :                         fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4812           1 :                         TIFFReadDirEntryOutputErr(
    4813             :                             tif, err, module,
    4814             :                             fip ? fip->field_name : "unknown tagname", 1);
    4815             :                     }
    4816             :                     else
    4817             :                     {
    4818         375 :                         TIFFSetField(tif, dp->tdir_tag, value,
    4819         375 :                                      value + incrementpersample,
    4820         375 :                                      value + 2 * incrementpersample);
    4821         375 :                         _TIFFfreeExt(tif, value);
    4822             :                     }
    4823             :                 }
    4824         376 :                 break;
    4825             :                     /* BEGIN REV 4.0 COMPATIBILITY */
    4826           0 :                 case TIFFTAG_OSUBFILETYPE:
    4827             :                 {
    4828             :                     uint16_t valueo;
    4829             :                     uint32_t value;
    4830           0 :                     if (TIFFReadDirEntryShort(tif, dp, &valueo) ==
    4831             :                         TIFFReadDirEntryErrOk)
    4832             :                     {
    4833           0 :                         switch (valueo)
    4834             :                         {
    4835           0 :                             case OFILETYPE_REDUCEDIMAGE:
    4836           0 :                                 value = FILETYPE_REDUCEDIMAGE;
    4837           0 :                                 break;
    4838           0 :                             case OFILETYPE_PAGE:
    4839           0 :                                 value = FILETYPE_PAGE;
    4840           0 :                                 break;
    4841           0 :                             default:
    4842           0 :                                 value = 0;
    4843           0 :                                 break;
    4844             :                         }
    4845           0 :                         if (value != 0)
    4846           0 :                             TIFFSetField(tif, TIFFTAG_SUBFILETYPE, value);
    4847             :                     }
    4848             :                 }
    4849           0 :                 break;
    4850             :                 /* END REV 4.0 COMPATIBILITY */
    4851             : #if 0
    4852             :                 case TIFFTAG_EP_BATTERYLEVEL:
    4853             :                     /* TIFFTAG_EP_BATTERYLEVEL can be RATIONAL or ASCII.
    4854             :                      * LibTiff defines it as ASCII and converts RATIONAL to an
    4855             :                      * ASCII string. */
    4856             :                     switch (dp->tdir_type)
    4857             :                     {
    4858             :                         case TIFF_RATIONAL:
    4859             :                         {
    4860             :                             /* Read rational and convert to ASCII*/
    4861             :                             enum TIFFReadDirEntryErr err;
    4862             :                             TIFFRational_t rValue;
    4863             :                             err = TIFFReadDirEntryCheckedRationalDirect(
    4864             :                                 tif, dp, &rValue);
    4865             :                             if (err != TIFFReadDirEntryErrOk)
    4866             :                             {
    4867             :                                 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4868             :                                 TIFFReadDirEntryOutputErr(
    4869             :                                     tif, err, module,
    4870             :                                     fip ? fip->field_name : "unknown tagname",
    4871             :                                     1);
    4872             :                             }
    4873             :                             else
    4874             :                             {
    4875             :                                 char szAux[32];
    4876             :                                 snprintf(szAux, sizeof(szAux) - 1, "%d/%d",
    4877             :                                          rValue.uNum, rValue.uDenom);
    4878             :                                 TIFFSetField(tif, dp->tdir_tag, szAux);
    4879             :                             }
    4880             :                         }
    4881             :                         break;
    4882             :                         case TIFF_ASCII:
    4883             :                             (void)TIFFFetchNormalTag(tif, dp, TRUE);
    4884             :                             break;
    4885             :                         default:
    4886             :                             fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4887             :                             TIFFWarningExtR(tif, module,
    4888             :                                             "Invalid data type for tag %s. "
    4889             :                                             "ASCII or RATIONAL expected",
    4890             :                                             fip ? fip->field_name
    4891             :                                                 : "unknown tagname");
    4892             :                             break;
    4893             :                     }
    4894             :                     break;
    4895             : #endif
    4896      212510 :                 default:
    4897      212510 :                     (void)TIFFFetchNormalTag(tif, dp, TRUE);
    4898      212634 :                     break;
    4899             :             } /* -- switch (dp->tdir_tag) -- */
    4900      345652 :         } /* -- if (!dp->tdir_ignore) */
    4901             :     } /* -- for-loop -- */
    4902             : 
    4903             :     /* Evaluate final IFD data size. */
    4904       55105 :     CalcFinalIFDdatasizeReading(tif, dircount);
    4905             : 
    4906             :     /*
    4907             :      * OJPEG hack:
    4908             :      * - If a) compression is OJPEG, and b) photometric tag is missing,
    4909             :      * then we consistently find that photometric should be YCbCr
    4910             :      * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
    4911             :      * then we consistently find that the buggy implementation of the
    4912             :      * buggy compression scheme matches photometric YCbCr instead.
    4913             :      * - If a) compression is OJPEG, and b) bitspersample tag is missing,
    4914             :      * then we consistently find bitspersample should be 8.
    4915             :      * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
    4916             :      * and c) photometric is RGB or YCbCr, then we consistently find
    4917             :      * samplesperpixel should be 3
    4918             :      * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
    4919             :      * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
    4920             :      * find samplesperpixel should be 3
    4921             :      */
    4922       54954 :     if (tif->tif_dir.td_compression == COMPRESSION_OJPEG)
    4923             :     {
    4924           2 :         if (!TIFFFieldSet(tif, FIELD_PHOTOMETRIC))
    4925             :         {
    4926           0 :             TIFFWarningExtR(
    4927             :                 tif, module,
    4928             :                 "Photometric tag is missing, assuming data is YCbCr");
    4929           0 :             if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR))
    4930           0 :                 goto bad;
    4931             :         }
    4932           2 :         else if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
    4933             :         {
    4934           0 :             tif->tif_dir.td_photometric = PHOTOMETRIC_YCBCR;
    4935           0 :             TIFFWarningExtR(tif, module,
    4936             :                             "Photometric tag value assumed incorrect, "
    4937             :                             "assuming data is YCbCr instead of RGB");
    4938             :         }
    4939           2 :         if (!TIFFFieldSet(tif, FIELD_BITSPERSAMPLE))
    4940             :         {
    4941           0 :             TIFFWarningExtR(
    4942             :                 tif, module,
    4943             :                 "BitsPerSample tag is missing, assuming 8 bits per sample");
    4944           0 :             if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8))
    4945           0 :                 goto bad;
    4946             :         }
    4947           2 :         if (!TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
    4948             :         {
    4949           0 :             if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
    4950             :             {
    4951           0 :                 TIFFWarningExtR(tif, module,
    4952             :                                 "SamplesPerPixel tag is missing, "
    4953             :                                 "assuming correct SamplesPerPixel value is 3");
    4954           0 :                 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
    4955           0 :                     goto bad;
    4956             :             }
    4957           0 :             if (tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR)
    4958             :             {
    4959           0 :                 TIFFWarningExtR(tif, module,
    4960             :                                 "SamplesPerPixel tag is missing, "
    4961             :                                 "applying correct SamplesPerPixel value of 3");
    4962           0 :                 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
    4963           0 :                     goto bad;
    4964             :             }
    4965           0 :             else if ((tif->tif_dir.td_photometric == PHOTOMETRIC_MINISWHITE) ||
    4966           0 :                      (tif->tif_dir.td_photometric == PHOTOMETRIC_MINISBLACK))
    4967             :             {
    4968             :                 /*
    4969             :                  * SamplesPerPixel tag is missing, but is not required
    4970             :                  * by spec.  Assume correct SamplesPerPixel value of 1.
    4971             :                  */
    4972          22 :                 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1))
    4973           0 :                     goto bad;
    4974             :             }
    4975             :         }
    4976             :     }
    4977             : 
    4978             :     /*
    4979             :      * Setup appropriate structures (by strip or by tile)
    4980             :      * We do that only after the above OJPEG hack which alters SamplesPerPixel
    4981             :      * and thus influences the number of strips in the separate planarconfig.
    4982             :      */
    4983       54932 :     if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
    4984             :     {
    4985       45318 :         tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
    4986       45316 :         tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
    4987       45316 :         tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
    4988       45316 :         tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
    4989       45316 :         tif->tif_flags &= ~TIFF_ISTILED;
    4990             :     }
    4991             :     else
    4992             :     {
    4993        9614 :         tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
    4994        9614 :         tif->tif_flags |= TIFF_ISTILED;
    4995             :     }
    4996       54930 :     if (!tif->tif_dir.td_nstrips)
    4997             :     {
    4998           0 :         TIFFErrorExtR(tif, module, "Cannot handle zero number of %s",
    4999           0 :                       isTiled(tif) ? "tiles" : "strips");
    5000           0 :         goto bad;
    5001             :     }
    5002       54930 :     tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
    5003       54930 :     if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
    5004        9364 :         tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
    5005       54930 :     if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS))
    5006             :     {
    5007             : #ifdef OJPEG_SUPPORT
    5008           0 :         if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
    5009           0 :             (isTiled(tif) == 0) && (tif->tif_dir.td_nstrips == 1))
    5010             :         {
    5011             :             /*
    5012             :              * XXX: OJPEG hack.
    5013             :              * If a) compression is OJPEG, b) it's not a tiled TIFF,
    5014             :              * and c) the number of strips is 1,
    5015             :              * then we tolerate the absence of stripoffsets tag,
    5016             :              * because, presumably, all required data is in the
    5017             :              * JpegInterchangeFormat stream.
    5018             :              */
    5019           0 :             TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
    5020             :         }
    5021             :         else
    5022             : #endif
    5023             :         {
    5024           0 :             MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets");
    5025           0 :             goto bad;
    5026             :         }
    5027             :     }
    5028             : 
    5029       54930 :     if (tif->tif_mode == O_RDWR &&
    5030       19174 :         tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
    5031       19174 :         tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
    5032        1402 :         tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
    5033        1402 :         tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
    5034        1402 :         tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
    5035        1402 :         tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
    5036        1402 :         tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
    5037        1402 :         tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0)
    5038             :     {
    5039             :         /* Directory typically created with TIFFDeferStrileArrayWriting() */
    5040        1402 :         TIFFSetupStrips(tif);
    5041             :     }
    5042       53528 :     else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD))
    5043             :     {
    5044       17876 :         if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0)
    5045             :         {
    5046       17875 :             if (!TIFFFetchStripThing(tif, &(tif->tif_dir.td_stripoffset_entry),
    5047             :                                      tif->tif_dir.td_nstrips,
    5048             :                                      &tif->tif_dir.td_stripoffset_p))
    5049             :             {
    5050           0 :                 goto bad;
    5051             :             }
    5052             :         }
    5053       17877 :         if (tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0)
    5054             :         {
    5055       17876 :             if (!TIFFFetchStripThing(
    5056             :                     tif, &(tif->tif_dir.td_stripbytecount_entry),
    5057             :                     tif->tif_dir.td_nstrips, &tif->tif_dir.td_stripbytecount_p))
    5058             :             {
    5059           0 :                 goto bad;
    5060             :             }
    5061             :         }
    5062             :     }
    5063             : 
    5064             :     /*
    5065             :      * Make sure all non-color channels are extrasamples.
    5066             :      * If it's not the case, define them as such.
    5067             :      */
    5068       54932 :     color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
    5069       54906 :     if (color_channels &&
    5070       54911 :         tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples >
    5071             :             color_channels)
    5072             :     {
    5073             :         uint16_t old_extrasamples;
    5074             :         uint16_t *new_sampleinfo;
    5075             : 
    5076           3 :         TIFFWarningExtR(
    5077             :             tif, module,
    5078             :             "Sum of Photometric type-related "
    5079             :             "color channels and ExtraSamples doesn't match SamplesPerPixel. "
    5080             :             "Defining non-color channels as ExtraSamples.");
    5081             : 
    5082           3 :         old_extrasamples = tif->tif_dir.td_extrasamples;
    5083           3 :         tif->tif_dir.td_extrasamples =
    5084           3 :             (uint16_t)(tif->tif_dir.td_samplesperpixel - color_channels);
    5085             : 
    5086             :         // sampleinfo should contain information relative to these new extra
    5087             :         // samples
    5088           3 :         new_sampleinfo = (uint16_t *)_TIFFcallocExt(
    5089           3 :             tif, tif->tif_dir.td_extrasamples, sizeof(uint16_t));
    5090           3 :         if (!new_sampleinfo)
    5091             :         {
    5092           0 :             TIFFErrorExtR(tif, module,
    5093             :                           "Failed to allocate memory for "
    5094             :                           "temporary new sampleinfo array "
    5095             :                           "(%" PRIu16 " 16 bit elements)",
    5096           0 :                           tif->tif_dir.td_extrasamples);
    5097           0 :             goto bad;
    5098             :         }
    5099             : 
    5100           3 :         if (old_extrasamples > 0)
    5101           0 :             memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo,
    5102             :                    old_extrasamples * sizeof(uint16_t));
    5103           3 :         _TIFFsetShortArrayExt(tif, &tif->tif_dir.td_sampleinfo, new_sampleinfo,
    5104           3 :                               tif->tif_dir.td_extrasamples);
    5105           3 :         _TIFFfreeExt(tif, new_sampleinfo);
    5106             :     }
    5107             : 
    5108             :     /*
    5109             :      * Verify Palette image has a Colormap.
    5110             :      */
    5111       54908 :     if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
    5112         357 :         !TIFFFieldSet(tif, FIELD_COLORMAP))
    5113             :     {
    5114           1 :         if (tif->tif_dir.td_bitspersample >= 8 &&
    5115           1 :             tif->tif_dir.td_samplesperpixel == 3)
    5116           0 :             tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
    5117           1 :         else if (tif->tif_dir.td_bitspersample >= 8)
    5118           1 :             tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
    5119             :         else
    5120             :         {
    5121           0 :             MissingRequired(tif, "Colormap");
    5122           0 :             goto bad;
    5123             :         }
    5124             :     }
    5125             :     /*
    5126             :      * OJPEG hack:
    5127             :      * We do no further messing with strip/tile offsets/bytecounts in OJPEG
    5128             :      * TIFFs
    5129             :      */
    5130       54908 :     if (tif->tif_dir.td_compression != COMPRESSION_OJPEG)
    5131             :     {
    5132             :         /*
    5133             :          * Attempt to deal with a missing StripByteCounts tag.
    5134             :          */
    5135       54937 :         if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS))
    5136             :         {
    5137             :             /*
    5138             :              * Some manufacturers violate the spec by not giving
    5139             :              * the size of the strips.  In this case, assume there
    5140             :              * is one uncompressed strip of data.
    5141             :              */
    5142           5 :             if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
    5143           5 :                  tif->tif_dir.td_nstrips > 1) ||
    5144           5 :                 (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
    5145           0 :                  tif->tif_dir.td_nstrips !=
    5146           0 :                      (uint32_t)tif->tif_dir.td_samplesperpixel))
    5147             :             {
    5148           0 :                 MissingRequired(tif, "StripByteCounts");
    5149           0 :                 goto bad;
    5150             :             }
    5151           5 :             TIFFWarningExtR(
    5152             :                 tif, module,
    5153             :                 "TIFF directory is missing required "
    5154             :                 "\"StripByteCounts\" field, calculating from imagelength");
    5155           5 :             if (EstimateStripByteCounts(tif, dir, dircount) < 0)
    5156           0 :                 goto bad;
    5157             :         }
    5158       54932 :         else if (tif->tif_dir.td_nstrips == 1 &&
    5159       39048 :                  !(tif->tif_flags & TIFF_ISTILED) && ByteCountLooksBad(tif))
    5160             :         {
    5161             :             /*
    5162             :              * XXX: Plexus (and others) sometimes give a value of
    5163             :              * zero for a tag when they don't know what the
    5164             :              * correct value is!  Try and handle the simple case
    5165             :              * of estimating the size of a one strip image.
    5166             :              */
    5167           5 :             TIFFWarningExtR(tif, module,
    5168             :                             "Bogus \"StripByteCounts\" field, ignoring and "
    5169             :                             "calculating from imagelength");
    5170           5 :             if (EstimateStripByteCounts(tif, dir, dircount) < 0)
    5171           0 :                 goto bad;
    5172             :         }
    5173       54945 :         else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
    5174       19278 :                  tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
    5175       14816 :                  tif->tif_dir.td_nstrips > 2 &&
    5176        3779 :                  tif->tif_dir.td_compression == COMPRESSION_NONE &&
    5177        1287 :                  TIFFGetStrileByteCount(tif, 0) !=
    5178        1287 :                      TIFFGetStrileByteCount(tif, 1) &&
    5179           0 :                  TIFFGetStrileByteCount(tif, 0) != 0 &&
    5180           0 :                  TIFFGetStrileByteCount(tif, 1) != 0)
    5181             :         {
    5182             :             /*
    5183             :              * XXX: Some vendors fill StripByteCount array with
    5184             :              * absolutely wrong values (it can be equal to
    5185             :              * StripOffset array, for example). Catch this case
    5186             :              * here.
    5187             :              *
    5188             :              * We avoid this check if deferring strile loading
    5189             :              * as it would always force us to load the strip/tile
    5190             :              * information.
    5191             :              */
    5192           0 :             TIFFWarningExtR(tif, module,
    5193             :                             "Wrong \"StripByteCounts\" field, ignoring and "
    5194             :                             "calculating from imagelength");
    5195           0 :             if (EstimateStripByteCounts(tif, dir, dircount) < 0)
    5196           0 :                 goto bad;
    5197             :         }
    5198             :     }
    5199       54926 :     if (dir)
    5200             :     {
    5201       54991 :         _TIFFfreeExt(tif, dir);
    5202       54998 :         dir = NULL;
    5203             :     }
    5204       54933 :     if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
    5205             :     {
    5206       54988 :         if (tif->tif_dir.td_bitspersample >= 16)
    5207       10976 :             tif->tif_dir.td_maxsamplevalue = 0xFFFF;
    5208             :         else
    5209       44012 :             tif->tif_dir.td_maxsamplevalue =
    5210       44012 :                 (uint16_t)((1 << tif->tif_dir.td_bitspersample) - 1);
    5211             :     }
    5212             : 
    5213             : #ifdef STRIPBYTECOUNTSORTED_UNUSED
    5214             :     /*
    5215             :      * XXX: We can optimize checking for the strip bounds using the sorted
    5216             :      * bytecounts array. See also comments for TIFFAppendToStrip()
    5217             :      * function in tif_write.c.
    5218             :      */
    5219             :     if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1)
    5220             :     {
    5221             :         uint32_t strip;
    5222             : 
    5223             :         tif->tif_dir.td_stripbytecountsorted = 1;
    5224             :         for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
    5225             :         {
    5226             :             if (TIFFGetStrileOffset(tif, strip - 1) >
    5227             :                 TIFFGetStrileOffset(tif, strip))
    5228             :             {
    5229             :                 tif->tif_dir.td_stripbytecountsorted = 0;
    5230             :                 break;
    5231             :             }
    5232             :         }
    5233             :     }
    5234             : #endif
    5235             : 
    5236             :     /*
    5237             :      * An opportunity for compression mode dependent tag fixup
    5238             :      */
    5239       54933 :     (*tif->tif_fixuptags)(tif);
    5240             : 
    5241             :     /*
    5242             :      * Some manufacturers make life difficult by writing
    5243             :      * large amounts of uncompressed data as a single strip.
    5244             :      * This is contrary to the recommendations of the spec.
    5245             :      * The following makes an attempt at breaking such images
    5246             :      * into strips closer to the recommended 8k bytes.  A
    5247             :      * side effect, however, is that the RowsPerStrip tag
    5248             :      * value may be changed.
    5249             :      */
    5250       54977 :     if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
    5251       45593 :         (tif->tif_dir.td_nstrips == 1) &&
    5252       37069 :         (tif->tif_dir.td_compression == COMPRESSION_NONE) &&
    5253       30425 :         ((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP))
    5254             :     {
    5255       22472 :         ChopUpSingleUncompressedStrip(tif);
    5256             :     }
    5257             : 
    5258             :     /* There are also uncompressed striped files with strips larger than */
    5259             :     /* 2 GB, which make them unfriendly with a lot of code. If possible, */
    5260             :     /* try to expose smaller "virtual" strips. */
    5261       54977 :     if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
    5262       45603 :         tif->tif_dir.td_compression == COMPRESSION_NONE &&
    5263       60636 :         (tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP &&
    5264       25318 :         TIFFStripSize64(tif) > 0x7FFFFFFFUL)
    5265             :     {
    5266           5 :         TryChopUpUncompressedBigTiff(tif);
    5267             :     }
    5268             : 
    5269             :     /*
    5270             :      * Clear the dirty directory flag.
    5271             :      */
    5272       54996 :     tif->tif_flags &= ~TIFF_DIRTYDIRECT;
    5273       54996 :     tif->tif_flags &= ~TIFF_DIRTYSTRIP;
    5274             : 
    5275             :     /*
    5276             :      * Reinitialize some further i/o since we are starting on a new directory.
    5277             :      */
    5278       54996 :     tif->tif_dir.td_scanlinesize = TIFFScanlineSize(tif);
    5279       54965 :     if (!tif->tif_dir.td_scanlinesize)
    5280             :     {
    5281           5 :         TIFFErrorExtR(tif, module, "Cannot handle zero scanline size");
    5282           0 :         return (0);
    5283             :     }
    5284             : 
    5285       54960 :     if (isTiled(tif))
    5286             :     {
    5287        9614 :         tif->tif_dir.td_tilesize = TIFFTileSize(tif);
    5288        9614 :         if (!tif->tif_dir.td_tilesize)
    5289             :         {
    5290           0 :             TIFFErrorExtR(tif, module, "Cannot handle zero tile size");
    5291           0 :             return (0);
    5292             :         }
    5293             :     }
    5294             :     else
    5295             :     {
    5296       45346 :         if (!TIFFStripSize(tif))
    5297             :         {
    5298          19 :             TIFFErrorExtR(tif, module, "Cannot handle zero strip size");
    5299           0 :             return (0);
    5300             :         }
    5301             :     }
    5302       54984 :     return (1);
    5303           1 : bad:
    5304           1 :     if (dir)
    5305           1 :         _TIFFfreeExt(tif, dir);
    5306           1 :     return (0);
    5307             : } /*-- TIFFReadDirectory() --*/
    5308             : 
    5309       54976 : static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
    5310             :                                         uint16_t dircount)
    5311             : {
    5312             :     static const char module[] = "TIFFReadDirectoryCheckOrder";
    5313             :     uint32_t m;
    5314             :     uint16_t n;
    5315             :     TIFFDirEntry *o;
    5316       54976 :     m = 0;
    5317      831505 :     for (n = 0, o = dir; n < dircount; n++, o++)
    5318             :     {
    5319      776541 :         if (o->tdir_tag < m)
    5320             :         {
    5321          12 :             TIFFWarningExtR(tif, module,
    5322             :                             "Invalid TIFF directory; tags are not sorted in "
    5323             :                             "ascending order");
    5324          12 :             break;
    5325             :         }
    5326      776529 :         m = o->tdir_tag + 1U;
    5327             :     }
    5328       54976 : }
    5329             : 
    5330      109952 : static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
    5331             :                                                 uint16_t dircount,
    5332             :                                                 uint16_t tagid)
    5333             : {
    5334             :     TIFFDirEntry *m;
    5335             :     uint16_t n;
    5336             :     (void)tif;
    5337      608535 :     for (m = dir, n = 0; n < dircount; m++, n++)
    5338             :     {
    5339      608519 :         if (m->tdir_tag == tagid)
    5340      109936 :             return (m);
    5341             :     }
    5342          16 :     return (0);
    5343             : }
    5344             : 
    5345     1170420 : static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
    5346             :                                            uint32_t *fii)
    5347             : {
    5348             :     int32_t ma, mb, mc;
    5349     1170420 :     ma = -1;
    5350     1170420 :     mc = (int32_t)tif->tif_nfields;
    5351             :     while (1)
    5352             :     {
    5353     7458750 :         if (ma + 1 == mc)
    5354             :         {
    5355         437 :             *fii = FAILED_FII;
    5356         437 :             return;
    5357             :         }
    5358     7458320 :         mb = (ma + mc) / 2;
    5359     7458320 :         if (tif->tif_fields[mb]->field_tag == (uint32_t)tagid)
    5360     1169990 :             break;
    5361     6288330 :         if (tif->tif_fields[mb]->field_tag < (uint32_t)tagid)
    5362     2382890 :             ma = mb;
    5363             :         else
    5364     3905440 :             mc = mb;
    5365             :     }
    5366             :     while (1)
    5367             :     {
    5368     1170350 :         if (mb == 0)
    5369       11881 :             break;
    5370     1158470 :         if (tif->tif_fields[mb - 1]->field_tag != (uint32_t)tagid)
    5371     1158110 :             break;
    5372         362 :         mb--;
    5373             :     }
    5374     1169990 :     *fii = (uint32_t)mb;
    5375             : }
    5376             : 
    5377             : /*
    5378             :  * Read custom directory from the arbitrary offset.
    5379             :  * The code is very similar to TIFFReadDirectory().
    5380             :  */
    5381           0 : int TIFFReadCustomDirectory(TIFF *tif, toff_t diroff,
    5382             :                             const TIFFFieldArray *infoarray)
    5383             : {
    5384             :     static const char module[] = "TIFFReadCustomDirectory";
    5385             :     TIFFDirEntry *dir;
    5386             :     uint16_t dircount;
    5387             :     TIFFDirEntry *dp;
    5388             :     uint16_t di;
    5389             :     const TIFFField *fip;
    5390             :     uint32_t fii;
    5391             : 
    5392           0 :     assert(infoarray != NULL);
    5393           0 :     dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
    5394           0 :     if (!dircount)
    5395             :     {
    5396           0 :         TIFFErrorExtR(tif, module,
    5397             :                       "Failed to read custom directory at offset %" PRIu64,
    5398             :                       diroff);
    5399           0 :         return 0;
    5400             :     }
    5401           0 :     TIFFReadDirectoryCheckOrder(tif, dir, dircount);
    5402             : 
    5403             :     /*
    5404             :      * Mark duplicates of any tag to be ignored (bugzilla 1994)
    5405             :      * to avoid certain pathological problems.
    5406             :      */
    5407             :     {
    5408             :         TIFFDirEntry *ma;
    5409             :         uint16_t mb;
    5410           0 :         for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
    5411             :         {
    5412             :             TIFFDirEntry *na;
    5413             :             uint16_t nb;
    5414           0 :             for (na = ma + 1, nb = (uint16_t)(mb + 1); nb < dircount;
    5415           0 :                  na++, nb++)
    5416             :             {
    5417           0 :                 if (ma->tdir_tag == na->tdir_tag)
    5418             :                 {
    5419           0 :                     na->tdir_ignore = TRUE;
    5420             :                 }
    5421             :             }
    5422             :         }
    5423             :     }
    5424             : 
    5425             :     /* Free any old stuff and reinit. */
    5426           0 :     TIFFFreeDirectory(tif);
    5427             :     /* Even if custom directories do not need the default settings of a standard
    5428             :      * IFD, the pointer to the TIFFSetField() and TIFFGetField() (i.e.
    5429             :      * tif->tif_tagmethods.vsetfield and tif->tif_tagmethods.vgetfield) need to
    5430             :      * be initialized, which is done in TIFFDefaultDirectory().
    5431             :      * After that, the field array for the custom tags needs to be setup again.
    5432             :      */
    5433           0 :     TIFFDefaultDirectory(tif);
    5434           0 :     _TIFFSetupFields(tif, infoarray);
    5435             : 
    5436             :     /* Allocate arrays for offset values outside IFD entry for IFD data size
    5437             :      * checking. Note: Counter are reset within TIFFFreeDirectory(). */
    5438           0 :     tif->tif_dir.td_dirdatasize_offsets =
    5439           0 :         (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
    5440             :             tif,
    5441           0 :             (tmsize_t)((size_t)dircount * sizeof(TIFFEntryOffsetAndLength)));
    5442           0 :     if (tif->tif_dir.td_dirdatasize_offsets == NULL)
    5443             :     {
    5444           0 :         TIFFErrorExtR(
    5445             :             tif, module,
    5446             :             "Failed to allocate memory for counting IFD data size at reading");
    5447           0 :         if (dir)
    5448           0 :             _TIFFfreeExt(tif, dir);
    5449           0 :         return 0;
    5450             :     }
    5451             : 
    5452           0 :     for (di = 0, dp = dir; di < dircount; di++, dp++)
    5453             :     {
    5454           0 :         TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
    5455           0 :         if (fii == FAILED_FII)
    5456             :         {
    5457           0 :             if (tif->tif_warn_about_unknown_tags)
    5458             :             {
    5459           0 :                 TIFFWarningExtR(tif, module,
    5460             :                                 "Unknown field with tag %" PRIu16 " (0x%" PRIx16
    5461             :                                 ") encountered",
    5462           0 :                                 dp->tdir_tag, dp->tdir_tag);
    5463             :             }
    5464           0 :             const TIFFField *fld = _TIFFCreateAnonField(
    5465           0 :                 tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
    5466           0 :             if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
    5467             :             {
    5468           0 :                 if (tif->tif_warn_about_unknown_tags)
    5469             :                 {
    5470           0 :                     TIFFWarningExtR(
    5471             :                         tif, module,
    5472             :                         "Registering anonymous field with tag %" PRIu16
    5473             :                         " (0x%" PRIx16 ") failed",
    5474           0 :                         dp->tdir_tag, dp->tdir_tag);
    5475             :                 }
    5476           0 :                 dp->tdir_ignore = TRUE;
    5477             :             }
    5478             :             else
    5479             :             {
    5480           0 :                 TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
    5481           0 :                 assert(fii != FAILED_FII);
    5482             :             }
    5483             :         }
    5484           0 :         if (!dp->tdir_ignore)
    5485             :         {
    5486           0 :             fip = tif->tif_fields[fii];
    5487           0 :             if (fip->field_bit == FIELD_IGNORE)
    5488           0 :                 dp->tdir_ignore = TRUE;
    5489             :             else
    5490             :             {
    5491             :                 /* check data type */
    5492           0 :                 while ((fip->field_type != TIFF_ANY) &&
    5493           0 :                        (fip->field_type != dp->tdir_type))
    5494             :                 {
    5495           0 :                     fii++;
    5496           0 :                     if ((fii == tif->tif_nfields) ||
    5497           0 :                         (tif->tif_fields[fii]->field_tag !=
    5498           0 :                          (uint32_t)dp->tdir_tag))
    5499             :                     {
    5500           0 :                         fii = 0xFFFF;
    5501           0 :                         break;
    5502             :                     }
    5503           0 :                     fip = tif->tif_fields[fii];
    5504             :                 }
    5505           0 :                 if (fii == 0xFFFF)
    5506             :                 {
    5507           0 :                     TIFFWarningExtR(tif, module,
    5508             :                                     "Wrong data type %" PRIu16
    5509             :                                     " for \"%s\"; tag ignored",
    5510           0 :                                     dp->tdir_type, fip->field_name);
    5511           0 :                     dp->tdir_ignore = TRUE;
    5512             :                 }
    5513             :                 else
    5514             :                 {
    5515             :                     /* check count if known in advance */
    5516           0 :                     if ((fip->field_readcount != TIFF_VARIABLE) &&
    5517           0 :                         (fip->field_readcount != TIFF_VARIABLE2))
    5518             :                     {
    5519             :                         uint32_t expected;
    5520           0 :                         if (fip->field_readcount == TIFF_SPP)
    5521           0 :                             expected =
    5522           0 :                                 (uint32_t)tif->tif_dir.td_samplesperpixel;
    5523             :                         else
    5524           0 :                             expected = (uint32_t)fip->field_readcount;
    5525           0 :                         if (!CheckDirCount(tif, dp, expected))
    5526           0 :                             dp->tdir_ignore = TRUE;
    5527             :                     }
    5528             :                 }
    5529             :             }
    5530           0 :             if (!dp->tdir_ignore)
    5531             :             {
    5532           0 :                 switch (dp->tdir_tag)
    5533             :                 {
    5534           0 :                     case EXIFTAG_SUBJECTDISTANCE:
    5535           0 :                         if (!TIFFFieldIsAnonymous(fip))
    5536             :                         {
    5537             :                             /* should only be called on a Exif directory */
    5538             :                             /* when exifFields[] is active */
    5539           0 :                             (void)TIFFFetchSubjectDistance(tif, dp);
    5540             :                         }
    5541             :                         else
    5542             :                         {
    5543           0 :                             (void)TIFFFetchNormalTag(tif, dp, TRUE);
    5544             :                         }
    5545           0 :                         break;
    5546           0 :                     default:
    5547           0 :                         (void)TIFFFetchNormalTag(tif, dp, TRUE);
    5548           0 :                         break;
    5549             :                 }
    5550           0 :             } /*-- if (!dp->tdir_ignore) */
    5551             :         }
    5552             :     }
    5553             :     /* Evaluate final IFD data size. */
    5554           0 :     CalcFinalIFDdatasizeReading(tif, dircount);
    5555             : 
    5556             :     /* To be able to return from SubIFD or custom-IFD to main-IFD */
    5557           0 :     tif->tif_setdirectory_force_absolute = TRUE;
    5558           0 :     if (dir)
    5559           0 :         _TIFFfreeExt(tif, dir);
    5560           0 :     return 1;
    5561             : }
    5562             : 
    5563             : /*
    5564             :  * EXIF is important special case of custom IFD, so we have a special
    5565             :  * function to read it.
    5566             :  */
    5567           0 : int TIFFReadEXIFDirectory(TIFF *tif, toff_t diroff)
    5568             : {
    5569           0 :     return TIFFReadCustomDirectory(tif, diroff, _TIFFGetExifFields());
    5570             : }
    5571             : 
    5572             : /*
    5573             :  *--: EXIF-GPS custom directory reading as another special case of custom IFD.
    5574             :  */
    5575           0 : int TIFFReadGPSDirectory(TIFF *tif, toff_t diroff)
    5576             : {
    5577           0 :     return TIFFReadCustomDirectory(tif, diroff, _TIFFGetGpsFields());
    5578             : }
    5579             : 
    5580          10 : static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
    5581             :                                    uint16_t dircount)
    5582             : {
    5583             :     static const char module[] = "EstimateStripByteCounts";
    5584             : 
    5585             :     TIFFDirEntry *dp;
    5586          10 :     TIFFDirectory *td = &tif->tif_dir;
    5587             :     uint32_t strip;
    5588             : 
    5589             :     /* Do not try to load stripbytecount as we will compute it */
    5590          10 :     if (!_TIFFFillStrilesInternal(tif, 0))
    5591           0 :         return -1;
    5592             : 
    5593          10 :     const uint64_t allocsize = (uint64_t)td->td_nstrips * sizeof(uint64_t);
    5594          10 :     uint64_t filesize = 0;
    5595          10 :     if (allocsize > 100 * 1024 * 1024)
    5596             :     {
    5597             :         /* Before allocating a huge amount of memory for corrupted files, check
    5598             :          * if size of requested memory is not greater than file size. */
    5599           0 :         filesize = TIFFGetFileSize(tif);
    5600           0 :         if (allocsize > filesize)
    5601             :         {
    5602           0 :             TIFFWarningExtR(
    5603             :                 tif, module,
    5604             :                 "Requested memory size for StripByteCounts of %" PRIu64
    5605             :                 " is greater than filesize %" PRIu64 ". Memory not allocated",
    5606             :                 allocsize, filesize);
    5607           0 :             return -1;
    5608             :         }
    5609             :     }
    5610             : 
    5611          10 :     if (td->td_stripbytecount_p)
    5612           5 :         _TIFFfreeExt(tif, td->td_stripbytecount_p);
    5613          20 :     td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc(
    5614          10 :         tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array");
    5615          10 :     if (td->td_stripbytecount_p == NULL)
    5616           0 :         return -1;
    5617             : 
    5618          10 :     if (td->td_compression != COMPRESSION_NONE)
    5619             :     {
    5620             :         uint64_t space;
    5621             :         uint16_t n;
    5622           2 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    5623           2 :             space = sizeof(TIFFHeaderClassic) + 2 +
    5624           2 :                     (unsigned long)dircount * 12 + 4;
    5625             :         else
    5626           0 :             space =
    5627           0 :                 sizeof(TIFFHeaderBig) + 8 + (unsigned long)dircount * 20 + 8;
    5628             :         /* calculate amount of space used by indirect values */
    5629          17 :         for (dp = dir, n = dircount; n > 0; n--, dp++)
    5630             :         {
    5631             :             uint32_t typewidth;
    5632             :             uint64_t datasize;
    5633          15 :             typewidth = (uint32_t)TIFFDataWidth((TIFFDataType)dp->tdir_type);
    5634          15 :             if (typewidth == 0)
    5635             :             {
    5636           0 :                 TIFFErrorExtR(
    5637             :                     tif, module,
    5638             :                     "Cannot determine size of unknown tag type %" PRIu16,
    5639           0 :                     dp->tdir_type);
    5640           0 :                 return -1;
    5641             :             }
    5642          15 :             if (dp->tdir_count > UINT64_MAX / typewidth)
    5643           0 :                 return -1;
    5644          15 :             datasize = (uint64_t)typewidth * dp->tdir_count;
    5645          15 :             if (!(tif->tif_flags & TIFF_BIGTIFF))
    5646             :             {
    5647          15 :                 if (datasize <= 4)
    5648          11 :                     datasize = 0;
    5649             :             }
    5650             :             else
    5651             :             {
    5652           0 :                 if (datasize <= 8)
    5653           0 :                     datasize = 0;
    5654             :             }
    5655          15 :             if (space > UINT64_MAX - datasize)
    5656           0 :                 return -1;
    5657          15 :             space += datasize;
    5658             :         }
    5659           2 :         if (filesize == 0)
    5660           2 :             filesize = TIFFGetFileSize(tif);
    5661           2 :         if (filesize < space)
    5662             :             /* we should perhaps return in error ? */
    5663           1 :             space = filesize;
    5664             :         else
    5665           1 :             space = filesize - space;
    5666           2 :         if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
    5667           0 :             space /= td->td_samplesperpixel;
    5668           4 :         for (strip = 0; strip < td->td_nstrips; strip++)
    5669           2 :             td->td_stripbytecount_p[strip] = space;
    5670             :         /*
    5671             :          * This gross hack handles the case were the offset to
    5672             :          * the last strip is past the place where we think the strip
    5673             :          * should begin.  Since a strip of data must be contiguous,
    5674             :          * it's safe to assume that we've overestimated the amount
    5675             :          * of data in the strip and trim this number back accordingly.
    5676             :          */
    5677           2 :         strip--;
    5678           2 :         if (td->td_stripoffset_p[strip] >
    5679           2 :             UINT64_MAX - td->td_stripbytecount_p[strip])
    5680           0 :             return -1;
    5681           2 :         if (td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip] >
    5682             :             filesize)
    5683             :         {
    5684           1 :             if (td->td_stripoffset_p[strip] >= filesize)
    5685             :             {
    5686             :                 /* Not sure what we should in that case... */
    5687           0 :                 td->td_stripbytecount_p[strip] = 0;
    5688             :             }
    5689             :             else
    5690             :             {
    5691           1 :                 td->td_stripbytecount_p[strip] =
    5692           1 :                     filesize - td->td_stripoffset_p[strip];
    5693             :             }
    5694             :         }
    5695             :     }
    5696           8 :     else if (isTiled(tif))
    5697             :     {
    5698           0 :         uint64_t bytespertile = TIFFTileSize64(tif);
    5699             : 
    5700           0 :         for (strip = 0; strip < td->td_nstrips; strip++)
    5701           0 :             td->td_stripbytecount_p[strip] = bytespertile;
    5702             :     }
    5703             :     else
    5704             :     {
    5705           8 :         uint64_t rowbytes = TIFFScanlineSize64(tif);
    5706           8 :         uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage;
    5707          16 :         for (strip = 0; strip < td->td_nstrips; strip++)
    5708             :         {
    5709           8 :             if (rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes)
    5710           0 :                 return -1;
    5711           8 :             td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;
    5712             :         }
    5713             :     }
    5714          10 :     TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
    5715          10 :     if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
    5716           5 :         td->td_rowsperstrip = td->td_imagelength;
    5717          10 :     return 1;
    5718             : }
    5719             : 
    5720           0 : static void MissingRequired(TIFF *tif, const char *tagname)
    5721             : {
    5722             :     static const char module[] = "MissingRequired";
    5723             : 
    5724           0 :     TIFFErrorExtR(tif, module,
    5725             :                   "TIFF directory is missing required \"%s\" field", tagname);
    5726           0 : }
    5727             : 
    5728      295785 : static unsigned long hashFuncOffsetToNumber(const void *elt)
    5729             : {
    5730      295785 :     const TIFFOffsetAndDirNumber *offsetAndDirNumber =
    5731             :         (const TIFFOffsetAndDirNumber *)elt;
    5732      295785 :     const uint32_t hash = (uint32_t)(offsetAndDirNumber->offset >> 32) ^
    5733      295785 :                           ((uint32_t)offsetAndDirNumber->offset & 0xFFFFFFFFU);
    5734      295785 :     return hash;
    5735             : }
    5736             : 
    5737       56784 : static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2)
    5738             : {
    5739       56784 :     const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
    5740             :         (const TIFFOffsetAndDirNumber *)elt1;
    5741       56784 :     const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
    5742             :         (const TIFFOffsetAndDirNumber *)elt2;
    5743       56784 :     return offsetAndDirNumber1->offset == offsetAndDirNumber2->offset;
    5744             : }
    5745             : 
    5746      251744 : static unsigned long hashFuncNumberToOffset(const void *elt)
    5747             : {
    5748      251744 :     const TIFFOffsetAndDirNumber *offsetAndDirNumber =
    5749             :         (const TIFFOffsetAndDirNumber *)elt;
    5750      251744 :     return offsetAndDirNumber->dirNumber;
    5751             : }
    5752             : 
    5753       14074 : static bool equalFuncNumberToOffset(const void *elt1, const void *elt2)
    5754             : {
    5755       14074 :     const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
    5756             :         (const TIFFOffsetAndDirNumber *)elt1;
    5757       14074 :     const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
    5758             :         (const TIFFOffsetAndDirNumber *)elt2;
    5759       14074 :     return offsetAndDirNumber1->dirNumber == offsetAndDirNumber2->dirNumber;
    5760             : }
    5761             : 
    5762             : /*
    5763             :  * Check the directory number and offset against the list of already seen
    5764             :  * directory numbers and offsets. This is a trick to prevent IFD looping.
    5765             :  * The one can create TIFF file with looped directory pointers. We will
    5766             :  * maintain a list of already seen directories and check every IFD offset
    5767             :  * and its IFD number against that list. However, the offset of an IFD number
    5768             :  * can change - e.g. when writing updates to file.
    5769             :  * Returns 1 if all is ok; 0 if last directory or IFD loop is encountered,
    5770             :  * or an error has occurred.
    5771             :  */
    5772      120611 : int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
    5773             : {
    5774      120611 :     if (diroff == 0) /* no more directories */
    5775           0 :         return 0;
    5776             : 
    5777      120611 :     if (tif->tif_map_dir_offset_to_number == NULL)
    5778             :     {
    5779       71328 :         tif->tif_map_dir_offset_to_number = TIFFHashSetNew(
    5780             :             hashFuncOffsetToNumber, equalFuncOffsetToNumber, free);
    5781       71357 :         if (tif->tif_map_dir_offset_to_number == NULL)
    5782             :         {
    5783           0 :             TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5784             :                           "Not enough memory");
    5785           0 :             return 1;
    5786             :         }
    5787             :     }
    5788             : 
    5789      120640 :     if (tif->tif_map_dir_number_to_offset == NULL)
    5790             :     {
    5791             :         /* No free callback for this map, as it shares the same items as
    5792             :          * tif->tif_map_dir_offset_to_number. */
    5793       71352 :         tif->tif_map_dir_number_to_offset = TIFFHashSetNew(
    5794             :             hashFuncNumberToOffset, equalFuncNumberToOffset, NULL);
    5795       71378 :         if (tif->tif_map_dir_number_to_offset == NULL)
    5796             :         {
    5797           0 :             TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5798             :                           "Not enough memory");
    5799           0 :             return 1;
    5800             :         }
    5801             :     }
    5802             : 
    5803             :     /* Check if offset is already in the list:
    5804             :      * - yes: check, if offset is at the same IFD number - if not, it is an IFD
    5805             :      * loop
    5806             :      * -  no: add to list or update offset at that IFD number
    5807             :      */
    5808             :     TIFFOffsetAndDirNumber entry;
    5809      120666 :     entry.offset = diroff;
    5810      120666 :     entry.dirNumber = dirn;
    5811             : 
    5812             :     TIFFOffsetAndDirNumber *foundEntry =
    5813      120666 :         (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5814             :             tif->tif_map_dir_offset_to_number, &entry);
    5815      120583 :     if (foundEntry)
    5816             :     {
    5817       41344 :         if (foundEntry->dirNumber == dirn)
    5818             :         {
    5819       41344 :             return 1;
    5820             :         }
    5821             :         else
    5822             :         {
    5823           0 :             TIFFWarningExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5824             :                             "TIFF directory %d has IFD looping to directory %u "
    5825             :                             "at offset 0x%" PRIx64 " (%" PRIu64 ")",
    5826           0 :                             (int)dirn - 1, foundEntry->dirNumber, diroff,
    5827             :                             diroff);
    5828           0 :             return 0;
    5829             :         }
    5830             :     }
    5831             : 
    5832             :     /* Check if offset of an IFD has been changed and update offset of that IFD
    5833             :      * number. */
    5834       79239 :     foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5835             :         tif->tif_map_dir_number_to_offset, &entry);
    5836       79240 :     if (foundEntry)
    5837             :     {
    5838          83 :         if (foundEntry->offset != diroff)
    5839             :         {
    5840             :             TIFFOffsetAndDirNumber entryOld;
    5841          83 :             entryOld.offset = foundEntry->offset;
    5842          83 :             entryOld.dirNumber = dirn;
    5843             :             /* We must remove first from tif_map_dir_number_to_offset as the */
    5844             :             /* entry is owned (and thus freed) by */
    5845             :             /* tif_map_dir_offset_to_number */
    5846             :             TIFFOffsetAndDirNumber *foundEntryOld =
    5847          83 :                 (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5848             :                     tif->tif_map_dir_number_to_offset, &entryOld);
    5849          83 :             if (foundEntryOld)
    5850             :             {
    5851          83 :                 TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
    5852             :                                   foundEntryOld);
    5853             :             }
    5854          83 :             foundEntryOld = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5855             :                 tif->tif_map_dir_offset_to_number, &entryOld);
    5856          83 :             if (foundEntryOld)
    5857             :             {
    5858          83 :                 TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
    5859             :                                   foundEntryOld);
    5860             :             }
    5861             : 
    5862          83 :             TIFFOffsetAndDirNumber *entryPtr = (TIFFOffsetAndDirNumber *)malloc(
    5863             :                 sizeof(TIFFOffsetAndDirNumber));
    5864          83 :             if (entryPtr == NULL)
    5865             :             {
    5866           0 :                 return 0;
    5867             :             }
    5868             : 
    5869             :             /* Add IFD offset and dirn to IFD directory list */
    5870          83 :             *entryPtr = entry;
    5871             : 
    5872          83 :             if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
    5873             :             {
    5874           0 :                 TIFFErrorExtR(
    5875             :                     tif, "_TIFFCheckDirNumberAndOffset",
    5876             :                     "Insertion in tif_map_dir_offset_to_number failed");
    5877           0 :                 return 0;
    5878             :             }
    5879          83 :             if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
    5880             :             {
    5881           0 :                 TIFFErrorExtR(
    5882             :                     tif, "_TIFFCheckDirNumberAndOffset",
    5883             :                     "Insertion in tif_map_dir_number_to_offset failed");
    5884           0 :                 return 0;
    5885             :             }
    5886             :         }
    5887          83 :         return 1;
    5888             :     }
    5889             : 
    5890             :     /* Arbitrary (hopefully big enough) limit */
    5891       79157 :     if (TIFFHashSetSize(tif->tif_map_dir_offset_to_number) >=
    5892             :         TIFF_MAX_DIR_COUNT)
    5893             :     {
    5894          12 :         TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5895             :                       "Cannot handle more than %u TIFF directories",
    5896             :                       (unsigned)TIFF_MAX_DIR_COUNT);
    5897           0 :         return 0;
    5898             :     }
    5899             : 
    5900             :     TIFFOffsetAndDirNumber *entryPtr =
    5901       79141 :         (TIFFOffsetAndDirNumber *)malloc(sizeof(TIFFOffsetAndDirNumber));
    5902       79141 :     if (entryPtr == NULL)
    5903             :     {
    5904           0 :         TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5905             :                       "malloc(sizeof(TIFFOffsetAndDirNumber)) failed");
    5906           0 :         return 0;
    5907             :     }
    5908             : 
    5909             :     /* Add IFD offset and dirn to IFD directory list */
    5910       79141 :     *entryPtr = entry;
    5911             : 
    5912       79141 :     if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
    5913             :     {
    5914           0 :         TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5915             :                       "Insertion in tif_map_dir_offset_to_number failed");
    5916           0 :         return 0;
    5917             :     }
    5918       79141 :     if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
    5919             :     {
    5920          31 :         TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5921             :                       "Insertion in tif_map_dir_number_to_offset failed");
    5922           0 :         return 0;
    5923             :     }
    5924             : 
    5925       79161 :     return 1;
    5926             : } /* --- _TIFFCheckDirNumberAndOffset() ---*/
    5927             : 
    5928             : /*
    5929             :  * Retrieve the matching IFD directory number of a given IFD offset
    5930             :  * from the list of directories already seen.
    5931             :  * Returns 1 if the offset was in the list and the directory number
    5932             :  * can be returned.
    5933             :  * Otherwise returns 0 or if an error occurred.
    5934             :  */
    5935       11789 : int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn)
    5936             : {
    5937       11789 :     if (diroff == 0) /* no more directories */
    5938           0 :         return 0;
    5939             : 
    5940             :     /* Check if offset is already in the list and return matching directory
    5941             :      * number. Otherwise update IFD list using TIFFNumberOfDirectories() and
    5942             :      * search again in IFD list.
    5943             :      */
    5944       11789 :     if (tif->tif_map_dir_offset_to_number == NULL)
    5945           5 :         return 0;
    5946             :     TIFFOffsetAndDirNumber entry;
    5947       11784 :     entry.offset = diroff;
    5948       11784 :     entry.dirNumber = 0; /* not used */
    5949             : 
    5950             :     TIFFOffsetAndDirNumber *foundEntry =
    5951       11784 :         (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5952             :             tif->tif_map_dir_offset_to_number, &entry);
    5953       11784 :     if (foundEntry)
    5954             :     {
    5955        9903 :         *dirn = foundEntry->dirNumber;
    5956        9903 :         return 1;
    5957             :     }
    5958             : 
    5959             :     /* This updates the directory list for all main-IFDs in the file. */
    5960        1881 :     TIFFNumberOfDirectories(tif);
    5961             : 
    5962        1882 :     foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5963             :         tif->tif_map_dir_offset_to_number, &entry);
    5964        1882 :     if (foundEntry)
    5965             :     {
    5966        1828 :         *dirn = foundEntry->dirNumber;
    5967        1828 :         return 1;
    5968             :     }
    5969             : 
    5970          54 :     return 0;
    5971             : } /*--- _TIFFGetDirNumberFromOffset() ---*/
    5972             : 
    5973             : /*
    5974             :  * Retrieve the matching IFD directory offset of a given IFD number
    5975             :  * from the list of directories already seen.
    5976             :  * Returns 1 if the offset was in the list of already seen IFDs and the
    5977             :  * directory offset can be returned. The directory list is not updated.
    5978             :  * Otherwise returns 0 or if an error occurred.
    5979             :  */
    5980       10991 : int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, uint64_t *diroff)
    5981             : {
    5982             : 
    5983       10991 :     if (tif->tif_map_dir_number_to_offset == NULL)
    5984           0 :         return 0;
    5985             :     TIFFOffsetAndDirNumber entry;
    5986       10991 :     entry.offset = 0; /* not used */
    5987       10991 :     entry.dirNumber = dirn;
    5988             : 
    5989             :     TIFFOffsetAndDirNumber *foundEntry =
    5990       10991 :         (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5991             :             tif->tif_map_dir_number_to_offset, &entry);
    5992       10991 :     if (foundEntry)
    5993             :     {
    5994       10991 :         *diroff = foundEntry->offset;
    5995       10991 :         return 1;
    5996             :     }
    5997             : 
    5998           0 :     return 0;
    5999             : } /*--- _TIFFGetOffsetFromDirNumber() ---*/
    6000             : 
    6001             : /*
    6002             :  * Remove an entry from the directory list of already seen directories
    6003             :  * by directory offset.
    6004             :  * If an entry is to be removed from the list, it is also okay if the entry
    6005             :  * is not in the list or the list does not exist.
    6006             :  */
    6007        1428 : int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif, uint64_t diroff)
    6008             : {
    6009        1428 :     if (tif->tif_map_dir_offset_to_number == NULL)
    6010           0 :         return 1;
    6011             : 
    6012             :     TIFFOffsetAndDirNumber entryOld;
    6013        1428 :     entryOld.offset = diroff;
    6014        1428 :     entryOld.dirNumber = 0;
    6015             :     /* We must remove first from tif_map_dir_number_to_offset as the
    6016             :      * entry is owned (and thus freed) by tif_map_dir_offset_to_number.
    6017             :      * However, we need firstly to find the directory number from offset. */
    6018             : 
    6019             :     TIFFOffsetAndDirNumber *foundEntryOldOff =
    6020        1428 :         (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    6021             :             tif->tif_map_dir_offset_to_number, &entryOld);
    6022        1428 :     if (foundEntryOldOff)
    6023             :     {
    6024        1417 :         entryOld.dirNumber = foundEntryOldOff->dirNumber;
    6025        1417 :         if (tif->tif_map_dir_number_to_offset != NULL)
    6026             :         {
    6027             :             TIFFOffsetAndDirNumber *foundEntryOldDir =
    6028        1417 :                 (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    6029             :                     tif->tif_map_dir_number_to_offset, &entryOld);
    6030        1417 :             if (foundEntryOldDir)
    6031             :             {
    6032        1417 :                 TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
    6033             :                                   foundEntryOldDir);
    6034        1417 :                 TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
    6035             :                                   foundEntryOldOff);
    6036        1417 :                 return 1;
    6037             :             }
    6038             :         }
    6039             :         else
    6040             :         {
    6041           0 :             TIFFErrorExtR(tif, "_TIFFRemoveEntryFromDirectoryListByOffset",
    6042             :                           "Unexpectedly tif_map_dir_number_to_offset is "
    6043             :                           "missing but tif_map_dir_offset_to_number exists.");
    6044           0 :             return 0;
    6045             :         }
    6046             :     }
    6047          11 :     return 1;
    6048             : } /*--- _TIFFRemoveEntryFromDirectoryListByOffset() ---*/
    6049             : 
    6050             : /*
    6051             :  * Check the count field of a directory entry against a known value.  The
    6052             :  * caller is expected to skip/ignore the tag if there is a mismatch.
    6053             :  */
    6054           0 : static int CheckDirCount(TIFF *tif, TIFFDirEntry *dir, uint32_t count)
    6055             : {
    6056           0 :     if ((uint64_t)count > dir->tdir_count)
    6057             :     {
    6058           0 :         const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
    6059           0 :         TIFFWarningExtR(tif, tif->tif_name,
    6060             :                         "incorrect count for field \"%s\" (%" PRIu64
    6061             :                         ", expecting %" PRIu32 "); tag ignored",
    6062             :                         fip ? fip->field_name : "unknown tagname",
    6063             :                         dir->tdir_count, count);
    6064           0 :         return (0);
    6065             :     }
    6066           0 :     else if ((uint64_t)count < dir->tdir_count)
    6067             :     {
    6068           0 :         const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
    6069           0 :         TIFFWarningExtR(tif, tif->tif_name,
    6070             :                         "incorrect count for field \"%s\" (%" PRIu64
    6071             :                         ", expecting %" PRIu32 "); tag trimmed",
    6072             :                         fip ? fip->field_name : "unknown tagname",
    6073             :                         dir->tdir_count, count);
    6074           0 :         dir->tdir_count = count;
    6075           0 :         return (1);
    6076             :     }
    6077           0 :     return (1);
    6078             : }
    6079             : 
    6080             : /*
    6081             :  * Read IFD structure from the specified offset. If the pointer to
    6082             :  * nextdiroff variable has been specified, read it too. Function returns a
    6083             :  * number of fields in the directory or 0 if failed.
    6084             :  */
    6085       55015 : static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
    6086             :                                    TIFFDirEntry **pdir, uint64_t *nextdiroff)
    6087             : {
    6088             :     static const char module[] = "TIFFFetchDirectory";
    6089             : 
    6090             :     void *origdir;
    6091             :     uint16_t dircount16;
    6092             :     uint32_t dirsize;
    6093             :     TIFFDirEntry *dir;
    6094             :     uint8_t *ma;
    6095             :     TIFFDirEntry *mb;
    6096             :     uint16_t n;
    6097             : 
    6098       55015 :     assert(pdir);
    6099             : 
    6100       55015 :     tif->tif_diroff = diroff;
    6101       55015 :     if (nextdiroff)
    6102       55012 :         *nextdiroff = 0;
    6103       55015 :     if (!isMapped(tif))
    6104             :     {
    6105       54997 :         if (!SeekOK(tif, tif->tif_diroff))
    6106             :         {
    6107           6 :             TIFFErrorExtR(tif, module,
    6108             :                           "%s: Seek error accessing TIFF directory",
    6109             :                           tif->tif_name);
    6110           0 :             return 0;
    6111             :         }
    6112       55005 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    6113             :         {
    6114       54055 :             if (!ReadOK(tif, &dircount16, sizeof(uint16_t)))
    6115             :             {
    6116          12 :                 TIFFErrorExtR(tif, module,
    6117             :                               "%s: Can not read TIFF directory count",
    6118             :                               tif->tif_name);
    6119          12 :                 return 0;
    6120             :             }
    6121       54038 :             if (tif->tif_flags & TIFF_SWAB)
    6122         561 :                 TIFFSwabShort(&dircount16);
    6123       54026 :             if (dircount16 > 4096)
    6124             :             {
    6125           0 :                 TIFFErrorExtR(tif, module,
    6126             :                               "Sanity check on directory count failed, this is "
    6127             :                               "probably not a valid IFD offset");
    6128           0 :                 return 0;
    6129             :             }
    6130       54026 :             dirsize = 12;
    6131             :         }
    6132             :         else
    6133             :         {
    6134             :             uint64_t dircount64;
    6135         950 :             if (!ReadOK(tif, &dircount64, sizeof(uint64_t)))
    6136             :             {
    6137           0 :                 TIFFErrorExtR(tif, module,
    6138             :                               "%s: Can not read TIFF directory count",
    6139             :                               tif->tif_name);
    6140           0 :                 return 0;
    6141             :             }
    6142         950 :             if (tif->tif_flags & TIFF_SWAB)
    6143          19 :                 TIFFSwabLong8(&dircount64);
    6144         950 :             if (dircount64 > 4096)
    6145             :             {
    6146           0 :                 TIFFErrorExtR(tif, module,
    6147             :                               "Sanity check on directory count failed, this is "
    6148             :                               "probably not a valid IFD offset");
    6149           0 :                 return 0;
    6150             :             }
    6151         950 :             dircount16 = (uint16_t)dircount64;
    6152         950 :             dirsize = 20;
    6153             :         }
    6154       54976 :         origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
    6155             :                                    "to read TIFF directory");
    6156       55000 :         if (origdir == NULL)
    6157           4 :             return 0;
    6158       54996 :         if (!ReadOK(tif, origdir, (tmsize_t)dircount16 * dirsize))
    6159             :         {
    6160           1 :             TIFFErrorExtR(tif, module, "%.100s: Can not read TIFF directory",
    6161             :                           tif->tif_name);
    6162           1 :             _TIFFfreeExt(tif, origdir);
    6163           1 :             return 0;
    6164             :         }
    6165             :         /*
    6166             :          * Read offset to next directory for sequential scans if
    6167             :          * needed.
    6168             :          */
    6169       54975 :         if (nextdiroff)
    6170             :         {
    6171       54966 :             if (!(tif->tif_flags & TIFF_BIGTIFF))
    6172             :             {
    6173             :                 uint32_t nextdiroff32;
    6174       54036 :                 if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t)))
    6175           4 :                     nextdiroff32 = 0;
    6176       54057 :                 if (tif->tif_flags & TIFF_SWAB)
    6177         561 :                     TIFFSwabLong(&nextdiroff32);
    6178       54021 :                 *nextdiroff = nextdiroff32;
    6179             :             }
    6180             :             else
    6181             :             {
    6182         930 :                 if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
    6183           0 :                     *nextdiroff = 0;
    6184         950 :                 if (tif->tif_flags & TIFF_SWAB)
    6185          19 :                     TIFFSwabLong8(nextdiroff);
    6186             :             }
    6187             :         }
    6188             :     }
    6189             :     else
    6190             :     {
    6191             :         tmsize_t m;
    6192             :         tmsize_t off;
    6193          18 :         if (tif->tif_diroff > (uint64_t)INT64_MAX)
    6194             :         {
    6195           0 :             TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
    6196           0 :             return (0);
    6197             :         }
    6198          18 :         off = (tmsize_t)tif->tif_diroff;
    6199             : 
    6200             :         /*
    6201             :          * Check for integer overflow when validating the dir_off,
    6202             :          * otherwise a very high offset may cause an OOB read and
    6203             :          * crash the client. Make two comparisons instead of
    6204             :          *
    6205             :          *  off + sizeof(uint16_t) > tif->tif_size
    6206             :          *
    6207             :          * to avoid overflow.
    6208             :          */
    6209          18 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    6210             :         {
    6211           8 :             m = (tmsize_t)((uint64_t)off + sizeof(uint16_t));
    6212           8 :             if ((m < off) || ((uint64_t)m < sizeof(uint16_t)) ||
    6213           8 :                 ((uint64_t)m > (uint64_t)tif->tif_size))
    6214             :             {
    6215           0 :                 TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
    6216           0 :                 return 0;
    6217             :             }
    6218             :             else
    6219             :             {
    6220           8 :                 _TIFFmemcpy(&dircount16, tif->tif_base + off, sizeof(uint16_t));
    6221             :             }
    6222           8 :             off = (tmsize_t)((uint64_t)off + sizeof(uint16_t));
    6223           8 :             if (tif->tif_flags & TIFF_SWAB)
    6224           0 :                 TIFFSwabShort(&dircount16);
    6225           8 :             if (dircount16 > 4096)
    6226             :             {
    6227           0 :                 TIFFErrorExtR(tif, module,
    6228             :                               "Sanity check on directory count failed, this is "
    6229             :                               "probably not a valid IFD offset");
    6230           0 :                 return 0;
    6231             :             }
    6232           8 :             dirsize = 12;
    6233             :         }
    6234             :         else
    6235             :         {
    6236             :             uint64_t dircount64;
    6237          10 :             m = (tmsize_t)((uint64_t)off + sizeof(uint64_t));
    6238          10 :             if ((m < off) || ((uint64_t)m < sizeof(uint64_t)) ||
    6239           0 :                 ((uint64_t)m > (uint64_t)tif->tif_size))
    6240             :             {
    6241          10 :                 TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
    6242           0 :                 return 0;
    6243             :             }
    6244             :             else
    6245             :             {
    6246           0 :                 _TIFFmemcpy(&dircount64, tif->tif_base + off, sizeof(uint64_t));
    6247             :             }
    6248           0 :             off = (tmsize_t)((uint64_t)off + sizeof(uint64_t));
    6249           0 :             if (tif->tif_flags & TIFF_SWAB)
    6250           0 :                 TIFFSwabLong8(&dircount64);
    6251           0 :             if (dircount64 > 4096)
    6252             :             {
    6253           0 :                 TIFFErrorExtR(tif, module,
    6254             :                               "Sanity check on directory count failed, this is "
    6255             :                               "probably not a valid IFD offset");
    6256           0 :                 return 0;
    6257             :             }
    6258           0 :             dircount16 = (uint16_t)dircount64;
    6259           0 :             dirsize = 20;
    6260             :         }
    6261           8 :         if (dircount16 == 0)
    6262             :         {
    6263           0 :             TIFFErrorExtR(tif, module,
    6264             :                           "Sanity check on directory count failed, zero tag "
    6265             :                           "directories not supported");
    6266           0 :             return 0;
    6267             :         }
    6268             :         /* Before allocating a huge amount of memory for corrupted files, check
    6269             :          * if size of requested memory is not greater than file size. */
    6270           8 :         uint64_t filesize = TIFFGetFileSize(tif);
    6271           8 :         uint64_t allocsize = (uint64_t)dircount16 * dirsize;
    6272           8 :         if (allocsize > filesize)
    6273             :         {
    6274           0 :             TIFFWarningExtR(
    6275             :                 tif, module,
    6276             :                 "Requested memory size for TIFF directory of %" PRIu64
    6277             :                 " is greater than filesize %" PRIu64
    6278             :                 ". Memory not allocated, TIFF directory not read",
    6279             :                 allocsize, filesize);
    6280           0 :             return 0;
    6281             :         }
    6282           8 :         origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
    6283             :                                    "to read TIFF directory");
    6284           8 :         if (origdir == NULL)
    6285           0 :             return 0;
    6286           8 :         m = off + dircount16 * dirsize;
    6287           8 :         if ((m < off) || (m < (tmsize_t)dircount16 * (tmsize_t)dirsize) ||
    6288           8 :             (m > tif->tif_size))
    6289             :         {
    6290           0 :             TIFFErrorExtR(tif, module, "Can not read TIFF directory");
    6291           0 :             _TIFFfreeExt(tif, origdir);
    6292           0 :             return 0;
    6293             :         }
    6294             :         else
    6295             :         {
    6296           8 :             _TIFFmemcpy(origdir, tif->tif_base + off,
    6297           8 :                         (tmsize_t)dircount16 * dirsize);
    6298             :         }
    6299           8 :         if (nextdiroff)
    6300             :         {
    6301           8 :             off += dircount16 * dirsize;
    6302           8 :             if (!(tif->tif_flags & TIFF_BIGTIFF))
    6303             :             {
    6304             :                 uint32_t nextdiroff32;
    6305           8 :                 m = (tmsize_t)((uint64_t)off + sizeof(uint32_t));
    6306           8 :                 if ((m < off) || ((uint64_t)m < sizeof(uint32_t)) ||
    6307           8 :                     ((uint64_t)m > (uint64_t)tif->tif_size))
    6308           0 :                     nextdiroff32 = 0;
    6309             :                 else
    6310           8 :                     _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
    6311             :                                 sizeof(uint32_t));
    6312           8 :                 if (tif->tif_flags & TIFF_SWAB)
    6313           0 :                     TIFFSwabLong(&nextdiroff32);
    6314           8 :                 *nextdiroff = nextdiroff32;
    6315             :             }
    6316             :             else
    6317             :             {
    6318           0 :                 m = (tmsize_t)((uint64_t)off + sizeof(uint64_t));
    6319           0 :                 if ((m < off) || ((uint64_t)m < sizeof(uint64_t)) ||
    6320           0 :                     ((uint64_t)m > (uint64_t)tif->tif_size))
    6321           0 :                     *nextdiroff = 0;
    6322             :                 else
    6323           0 :                     _TIFFmemcpy(nextdiroff, tif->tif_base + off,
    6324             :                                 sizeof(uint64_t));
    6325           0 :                 if (tif->tif_flags & TIFF_SWAB)
    6326           0 :                     TIFFSwabLong8(nextdiroff);
    6327             :             }
    6328             :         }
    6329             :     }
    6330             :     /* No check against filesize needed here because "dir" should have same size
    6331             :      * than "origdir" checked above. */
    6332       54988 :     dir = (TIFFDirEntry *)_TIFFCheckMalloc(
    6333             :         tif, dircount16, sizeof(TIFFDirEntry), "to read TIFF directory");
    6334       55005 :     if (dir == 0)
    6335             :     {
    6336           9 :         _TIFFfreeExt(tif, origdir);
    6337           0 :         return 0;
    6338             :     }
    6339       54996 :     ma = (uint8_t *)origdir;
    6340       54996 :     mb = dir;
    6341      832832 :     for (n = 0; n < dircount16; n++)
    6342             :     {
    6343      777850 :         mb->tdir_ignore = FALSE;
    6344      777850 :         if (tif->tif_flags & TIFF_SWAB)
    6345        7746 :             TIFFSwabShort((uint16_t *)ma);
    6346      777850 :         mb->tdir_tag = *(uint16_t *)ma;
    6347      777850 :         ma += sizeof(uint16_t);
    6348      777850 :         if (tif->tif_flags & TIFF_SWAB)
    6349        7746 :             TIFFSwabShort((uint16_t *)ma);
    6350      777849 :         mb->tdir_type = *(uint16_t *)ma;
    6351      777849 :         ma += sizeof(uint16_t);
    6352      777849 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    6353             :         {
    6354      763795 :             if (tif->tif_flags & TIFF_SWAB)
    6355        7537 :                 TIFFSwabLong((uint32_t *)ma);
    6356      763809 :             mb->tdir_count = (uint64_t)(*(uint32_t *)ma);
    6357      763809 :             ma += sizeof(uint32_t);
    6358      763809 :             mb->tdir_offset.toff_long8 = 0;
    6359      763809 :             *(uint32_t *)(&mb->tdir_offset) = *(uint32_t *)ma;
    6360      763809 :             ma += sizeof(uint32_t);
    6361             :         }
    6362             :         else
    6363             :         {
    6364       14054 :             if (tif->tif_flags & TIFF_SWAB)
    6365         209 :                 TIFFSwabLong8((uint64_t *)ma);
    6366       14054 :             mb->tdir_count = TIFFReadUInt64(ma);
    6367       14054 :             ma += sizeof(uint64_t);
    6368       14054 :             mb->tdir_offset.toff_long8 = TIFFReadUInt64(ma);
    6369       14027 :             ma += sizeof(uint64_t);
    6370             :         }
    6371      777836 :         mb++;
    6372             :     }
    6373       54982 :     _TIFFfreeExt(tif, origdir);
    6374       54965 :     *pdir = dir;
    6375       54965 :     return dircount16;
    6376             : }
    6377             : 
    6378             : /*
    6379             :  * Fetch a tag that is not handled by special case code.
    6380             :  */
    6381      502745 : static int TIFFFetchNormalTag(TIFF *tif, TIFFDirEntry *dp, int recover)
    6382             : {
    6383             :     static const char module[] = "TIFFFetchNormalTag";
    6384             :     enum TIFFReadDirEntryErr err;
    6385             :     uint32_t fii;
    6386      502745 :     const TIFFField *fip = NULL;
    6387      502745 :     TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
    6388      502900 :     if (fii == FAILED_FII)
    6389             :     {
    6390         128 :         TIFFErrorExtR(tif, "TIFFFetchNormalTag",
    6391         128 :                       "No definition found for tag %" PRIu16, dp->tdir_tag);
    6392           0 :         return 0;
    6393             :     }
    6394      502772 :     fip = tif->tif_fields[fii];
    6395      502772 :     assert(fip != NULL); /* should not happen */
    6396      502772 :     assert(fip->set_get_field_type !=
    6397             :            TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with
    6398             :                                   this in specialized code */
    6399      502772 :     assert(fip->set_get_field_type !=
    6400             :            TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only
    6401             :                                 the case for pseudo-tags */
    6402      502772 :     err = TIFFReadDirEntryErrOk;
    6403      502772 :     switch (fip->set_get_field_type)
    6404             :     {
    6405         386 :         case TIFF_SETGET_UNDEFINED:
    6406         386 :             TIFFErrorExtR(
    6407             :                 tif, "TIFFFetchNormalTag",
    6408             :                 "Defined set_get_field_type of custom tag %u (%s) is "
    6409             :                 "TIFF_SETGET_UNDEFINED and thus tag is not read from file",
    6410             :                 fip->field_tag, fip->field_name);
    6411         386 :             break;
    6412       39088 :         case TIFF_SETGET_ASCII:
    6413             :         {
    6414             :             uint8_t *data;
    6415       39088 :             assert(fip->field_passcount == 0);
    6416       39088 :             err = TIFFReadDirEntryByteArray(tif, dp, &data);
    6417       39054 :             if (err == TIFFReadDirEntryErrOk)
    6418             :             {
    6419       39045 :                 size_t mb = 0;
    6420             :                 int n;
    6421       39045 :                 if (data != NULL)
    6422             :                 {
    6423       39036 :                     if (dp->tdir_count > 0 && data[dp->tdir_count - 1] == 0)
    6424             :                     {
    6425             :                         /* optimization: if data is known to be 0 terminated, we
    6426             :                          * can use strlen() */
    6427       39096 :                         mb = strlen((const char *)data);
    6428             :                     }
    6429             :                     else
    6430             :                     {
    6431             :                         /* general case. equivalent to non-portable */
    6432             :                         /* mb = strnlen((const char*)data,
    6433             :                          * (uint32_t)dp->tdir_count); */
    6434           0 :                         uint8_t *ma = data;
    6435           0 :                         while (mb < (uint32_t)dp->tdir_count)
    6436             :                         {
    6437          27 :                             if (*ma == 0)
    6438           0 :                                 break;
    6439          27 :                             ma++;
    6440          27 :                             mb++;
    6441             :                         }
    6442             :                     }
    6443             :                 }
    6444       39045 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    6445             :                 {
    6446          14 :                     if (data != NULL)
    6447           0 :                         _TIFFfreeExt(tif, data);
    6448           0 :                     return (0);
    6449             :                 }
    6450       39078 :                 if (mb + 1 < (uint32_t)dp->tdir_count)
    6451           0 :                     TIFFWarningExtR(
    6452             :                         tif, module,
    6453             :                         "ASCII value for tag \"%s\" contains null byte in "
    6454             :                         "value; value incorrectly truncated during reading due "
    6455             :                         "to implementation limitations",
    6456             :                         fip->field_name);
    6457       39078 :                 else if (mb + 1 > (uint32_t)dp->tdir_count)
    6458             :                 {
    6459           1 :                     TIFFWarningExtR(tif, module,
    6460             :                                     "ASCII value for tag \"%s\" does not end "
    6461             :                                     "in null byte. Forcing it to be null",
    6462             :                                     fip->field_name);
    6463             :                     /* TIFFReadDirEntryArrayWithLimit() ensures this can't be
    6464             :                      * larger than MAX_SIZE_TAG_DATA */
    6465           1 :                     assert((uint32_t)dp->tdir_count + 1 == dp->tdir_count + 1);
    6466           1 :                     uint8_t *o = (uint8_t *)_TIFFmallocExt(
    6467           1 :                         tif, (uint32_t)dp->tdir_count + 1);
    6468           1 :                     if (o == NULL)
    6469             :                     {
    6470           0 :                         if (data != NULL)
    6471           0 :                             _TIFFfreeExt(tif, data);
    6472           0 :                         return (0);
    6473             :                     }
    6474           1 :                     if (dp->tdir_count > 0)
    6475             :                     {
    6476           1 :                         _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
    6477             :                     }
    6478           1 :                     o[(uint32_t)dp->tdir_count] = 0;
    6479           1 :                     if (data != 0)
    6480           1 :                         _TIFFfreeExt(tif, data);
    6481           1 :                     data = o;
    6482             :                 }
    6483       39078 :                 n = TIFFSetField(tif, dp->tdir_tag, data);
    6484       39102 :                 if (data != 0)
    6485       39093 :                     _TIFFfreeExt(tif, data);
    6486       39069 :                 if (!n)
    6487           0 :                     return (0);
    6488             :             }
    6489             :         }
    6490       39078 :         break;
    6491           0 :         case TIFF_SETGET_UINT8:
    6492             :         {
    6493           0 :             uint8_t data = 0;
    6494           0 :             assert(fip->field_readcount == 1);
    6495           0 :             assert(fip->field_passcount == 0);
    6496           0 :             err = TIFFReadDirEntryByte(tif, dp, &data);
    6497           0 :             if (err == TIFFReadDirEntryErrOk)
    6498             :             {
    6499           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6500           0 :                     return (0);
    6501             :             }
    6502             :         }
    6503           0 :         break;
    6504           0 :         case TIFF_SETGET_SINT8:
    6505             :         {
    6506           0 :             int8_t data = 0;
    6507           0 :             assert(fip->field_readcount == 1);
    6508           0 :             assert(fip->field_passcount == 0);
    6509           0 :             err = TIFFReadDirEntrySbyte(tif, dp, &data);
    6510           0 :             if (err == TIFFReadDirEntryErrOk)
    6511             :             {
    6512           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6513           0 :                     return (0);
    6514             :             }
    6515             :         }
    6516           0 :         break;
    6517      170836 :         case TIFF_SETGET_UINT16:
    6518             :         {
    6519             :             uint16_t data;
    6520      170836 :             assert(fip->field_readcount == 1);
    6521      170836 :             assert(fip->field_passcount == 0);
    6522      170836 :             err = TIFFReadDirEntryShort(tif, dp, &data);
    6523      170820 :             if (err == TIFFReadDirEntryErrOk)
    6524             :             {
    6525      170825 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6526           0 :                     return (0);
    6527             :             }
    6528             :         }
    6529      170840 :         break;
    6530           0 :         case TIFF_SETGET_SINT16:
    6531             :         {
    6532             :             int16_t data;
    6533           0 :             assert(fip->field_readcount == 1);
    6534           0 :             assert(fip->field_passcount == 0);
    6535           0 :             err = TIFFReadDirEntrySshort(tif, dp, &data);
    6536           0 :             if (err == TIFFReadDirEntryErrOk)
    6537             :             {
    6538           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6539           0 :                     return (0);
    6540             :             }
    6541             :         }
    6542           0 :         break;
    6543      180497 :         case TIFF_SETGET_UINT32:
    6544             :         {
    6545             :             uint32_t data;
    6546      180497 :             assert(fip->field_readcount == 1);
    6547      180497 :             assert(fip->field_passcount == 0);
    6548      180497 :             err = TIFFReadDirEntryLong(tif, dp, &data);
    6549      180439 :             if (err == TIFFReadDirEntryErrOk)
    6550             :             {
    6551      180414 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6552           0 :                     return (0);
    6553             :             }
    6554             :         }
    6555      180440 :         break;
    6556           0 :         case TIFF_SETGET_SINT32:
    6557             :         {
    6558             :             int32_t data;
    6559           0 :             assert(fip->field_readcount == 1);
    6560           0 :             assert(fip->field_passcount == 0);
    6561           0 :             err = TIFFReadDirEntrySlong(tif, dp, &data);
    6562           0 :             if (err == TIFFReadDirEntryErrOk)
    6563             :             {
    6564           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6565           0 :                     return (0);
    6566             :             }
    6567             :         }
    6568           0 :         break;
    6569           8 :         case TIFF_SETGET_UINT64:
    6570             :         {
    6571             :             uint64_t data;
    6572           8 :             assert(fip->field_readcount == 1);
    6573           8 :             assert(fip->field_passcount == 0);
    6574           8 :             err = TIFFReadDirEntryLong8(tif, dp, &data);
    6575           8 :             if (err == TIFFReadDirEntryErrOk)
    6576             :             {
    6577           8 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    6578           0 :                     return 0;
    6579           8 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6580           0 :                     return (0);
    6581             :             }
    6582             :         }
    6583           8 :         break;
    6584           0 :         case TIFF_SETGET_SINT64:
    6585             :         {
    6586             :             int64_t data;
    6587           0 :             assert(fip->field_readcount == 1);
    6588           0 :             assert(fip->field_passcount == 0);
    6589           0 :             err = TIFFReadDirEntrySlong8(tif, dp, &data);
    6590           0 :             if (err == TIFFReadDirEntryErrOk)
    6591             :             {
    6592           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    6593           0 :                     return 0;
    6594           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6595           0 :                     return (0);
    6596             :             }
    6597             :         }
    6598           0 :         break;
    6599         248 :         case TIFF_SETGET_FLOAT:
    6600             :         {
    6601             :             float data;
    6602         248 :             assert(fip->field_readcount == 1);
    6603         248 :             assert(fip->field_passcount == 0);
    6604         248 :             err = TIFFReadDirEntryFloat(tif, dp, &data);
    6605         248 :             if (err == TIFFReadDirEntryErrOk)
    6606             :             {
    6607         248 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    6608           0 :                     return 0;
    6609         248 :                 if (!TIFFSetField(tif, dp->tdir_tag, (double)data))
    6610           0 :                     return (0);
    6611             :             }
    6612             :         }
    6613         248 :         break;
    6614           0 :         case TIFF_SETGET_DOUBLE:
    6615             :         {
    6616             :             double data;
    6617           0 :             assert(fip->field_readcount == 1);
    6618           0 :             assert(fip->field_passcount == 0);
    6619           0 :             err = TIFFReadDirEntryDouble(tif, dp, &data);
    6620           0 :             if (err == TIFFReadDirEntryErrOk)
    6621             :             {
    6622           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    6623           0 :                     return 0;
    6624           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6625           0 :                     return (0);
    6626             :             }
    6627             :         }
    6628           0 :         break;
    6629           0 :         case TIFF_SETGET_IFD8:
    6630             :         {
    6631             :             uint64_t data;
    6632           0 :             assert(fip->field_readcount == 1);
    6633           0 :             assert(fip->field_passcount == 0);
    6634           0 :             err = TIFFReadDirEntryIfd8(tif, dp, &data);
    6635           0 :             if (err == TIFFReadDirEntryErrOk)
    6636             :             {
    6637           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    6638           0 :                     return 0;
    6639           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6640           0 :                     return (0);
    6641             :             }
    6642             :         }
    6643           0 :         break;
    6644        1338 :         case TIFF_SETGET_UINT16_PAIR:
    6645             :         {
    6646             :             uint16_t *data;
    6647        1338 :             assert(fip->field_readcount == 2);
    6648        1338 :             assert(fip->field_passcount == 0);
    6649        1338 :             if (dp->tdir_count != 2)
    6650             :             {
    6651           0 :                 TIFFWarningExtR(tif, module,
    6652             :                                 "incorrect count for field \"%s\", expected 2, "
    6653             :                                 "got %" PRIu64,
    6654             :                                 fip->field_name, dp->tdir_count);
    6655           0 :                 return (0);
    6656             :             }
    6657        1338 :             err = TIFFReadDirEntryShortArray(tif, dp, &data);
    6658        1338 :             if (err == TIFFReadDirEntryErrOk)
    6659             :             {
    6660             :                 int m;
    6661        1338 :                 assert(data); /* avoid CLang static Analyzer false positive */
    6662        1338 :                 m = TIFFSetField(tif, dp->tdir_tag, data[0], data[1]);
    6663        1338 :                 _TIFFfreeExt(tif, data);
    6664        1338 :                 if (!m)
    6665           0 :                     return (0);
    6666             :             }
    6667             :         }
    6668        1338 :         break;
    6669           0 :         case TIFF_SETGET_C0_UINT8:
    6670             :         {
    6671             :             uint8_t *data;
    6672           0 :             assert(fip->field_readcount >= 1);
    6673           0 :             assert(fip->field_passcount == 0);
    6674           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6675             :             {
    6676           0 :                 TIFFWarningExtR(tif, module,
    6677             :                                 "incorrect count for field \"%s\", expected "
    6678             :                                 "%d, got %" PRIu64,
    6679           0 :                                 fip->field_name, (int)fip->field_readcount,
    6680             :                                 dp->tdir_count);
    6681           0 :                 return (0);
    6682             :             }
    6683             :             else
    6684             :             {
    6685           0 :                 err = TIFFReadDirEntryByteArray(tif, dp, &data);
    6686           0 :                 if (err == TIFFReadDirEntryErrOk)
    6687             :                 {
    6688           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6689             :                     {
    6690           0 :                         if (data != 0)
    6691           0 :                             _TIFFfreeExt(tif, data);
    6692           0 :                         return 0;
    6693             :                     }
    6694             :                     int m;
    6695           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6696           0 :                     if (data != 0)
    6697           0 :                         _TIFFfreeExt(tif, data);
    6698           0 :                     if (!m)
    6699           0 :                         return (0);
    6700             :                 }
    6701             :             }
    6702             :         }
    6703           0 :         break;
    6704           0 :         case TIFF_SETGET_C0_SINT8:
    6705             :         {
    6706             :             int8_t *data;
    6707           0 :             assert(fip->field_readcount >= 1);
    6708           0 :             assert(fip->field_passcount == 0);
    6709           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6710             :             {
    6711           0 :                 TIFFWarningExtR(tif, module,
    6712             :                                 "incorrect count for field \"%s\", expected "
    6713             :                                 "%d, got %" PRIu64,
    6714           0 :                                 fip->field_name, (int)fip->field_readcount,
    6715             :                                 dp->tdir_count);
    6716           0 :                 return (0);
    6717             :             }
    6718             :             else
    6719             :             {
    6720           0 :                 err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
    6721           0 :                 if (err == TIFFReadDirEntryErrOk)
    6722             :                 {
    6723           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6724             :                     {
    6725           0 :                         if (data != 0)
    6726           0 :                             _TIFFfreeExt(tif, data);
    6727           0 :                         return 0;
    6728             :                     }
    6729             :                     int m;
    6730           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6731           0 :                     if (data != 0)
    6732           0 :                         _TIFFfreeExt(tif, data);
    6733           0 :                     if (!m)
    6734           0 :                         return (0);
    6735             :                 }
    6736             :             }
    6737             :         }
    6738           0 :         break;
    6739           0 :         case TIFF_SETGET_C0_UINT16:
    6740             :         {
    6741             :             uint16_t *data;
    6742           0 :             assert(fip->field_readcount >= 1);
    6743           0 :             assert(fip->field_passcount == 0);
    6744           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6745             :             {
    6746           0 :                 TIFFWarningExtR(tif, module,
    6747             :                                 "incorrect count for field \"%s\", expected "
    6748             :                                 "%d, got %" PRIu64,
    6749           0 :                                 fip->field_name, (int)fip->field_readcount,
    6750             :                                 dp->tdir_count);
    6751           0 :                 return (0);
    6752             :             }
    6753             :             else
    6754             :             {
    6755           0 :                 err = TIFFReadDirEntryShortArray(tif, dp, &data);
    6756           0 :                 if (err == TIFFReadDirEntryErrOk)
    6757             :                 {
    6758           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6759             :                     {
    6760           0 :                         if (data != 0)
    6761           0 :                             _TIFFfreeExt(tif, data);
    6762           0 :                         return 0;
    6763             :                     }
    6764             :                     int m;
    6765           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6766           0 :                     if (data != 0)
    6767           0 :                         _TIFFfreeExt(tif, data);
    6768           0 :                     if (!m)
    6769           0 :                         return (0);
    6770             :                 }
    6771             :             }
    6772             :         }
    6773           0 :         break;
    6774           0 :         case TIFF_SETGET_C0_SINT16:
    6775             :         {
    6776             :             int16_t *data;
    6777           0 :             assert(fip->field_readcount >= 1);
    6778           0 :             assert(fip->field_passcount == 0);
    6779           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6780             :             {
    6781           0 :                 TIFFWarningExtR(tif, module,
    6782             :                                 "incorrect count for field \"%s\", expected "
    6783             :                                 "%d, got %" PRIu64,
    6784           0 :                                 fip->field_name, (int)fip->field_readcount,
    6785             :                                 dp->tdir_count);
    6786           0 :                 return (0);
    6787             :             }
    6788             :             else
    6789             :             {
    6790           0 :                 err = TIFFReadDirEntrySshortArray(tif, dp, &data);
    6791           0 :                 if (err == TIFFReadDirEntryErrOk)
    6792             :                 {
    6793           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6794             :                     {
    6795           0 :                         if (data != 0)
    6796           0 :                             _TIFFfreeExt(tif, data);
    6797           0 :                         return 0;
    6798             :                     }
    6799             :                     int m;
    6800           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6801           0 :                     if (data != 0)
    6802           0 :                         _TIFFfreeExt(tif, data);
    6803           0 :                     if (!m)
    6804           0 :                         return (0);
    6805             :                 }
    6806             :             }
    6807             :         }
    6808           0 :         break;
    6809           0 :         case TIFF_SETGET_C0_UINT32:
    6810             :         {
    6811             :             uint32_t *data;
    6812           0 :             assert(fip->field_readcount >= 1);
    6813           0 :             assert(fip->field_passcount == 0);
    6814           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6815             :             {
    6816           0 :                 TIFFWarningExtR(tif, module,
    6817             :                                 "incorrect count for field \"%s\", expected "
    6818             :                                 "%d, got %" PRIu64,
    6819           0 :                                 fip->field_name, (int)fip->field_readcount,
    6820             :                                 dp->tdir_count);
    6821           0 :                 return (0);
    6822             :             }
    6823             :             else
    6824             :             {
    6825           0 :                 err = TIFFReadDirEntryLongArray(tif, dp, &data);
    6826           0 :                 if (err == TIFFReadDirEntryErrOk)
    6827             :                 {
    6828           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6829             :                     {
    6830           0 :                         if (data != 0)
    6831           0 :                             _TIFFfreeExt(tif, data);
    6832           0 :                         return 0;
    6833             :                     }
    6834             :                     int m;
    6835           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6836           0 :                     if (data != 0)
    6837           0 :                         _TIFFfreeExt(tif, data);
    6838           0 :                     if (!m)
    6839           0 :                         return (0);
    6840             :                 }
    6841             :             }
    6842             :         }
    6843           0 :         break;
    6844           0 :         case TIFF_SETGET_C0_SINT32:
    6845             :         {
    6846             :             int32_t *data;
    6847           0 :             assert(fip->field_readcount >= 1);
    6848           0 :             assert(fip->field_passcount == 0);
    6849           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6850             :             {
    6851           0 :                 TIFFWarningExtR(tif, module,
    6852             :                                 "incorrect count for field \"%s\", expected "
    6853             :                                 "%d, got %" PRIu64,
    6854           0 :                                 fip->field_name, (int)fip->field_readcount,
    6855             :                                 dp->tdir_count);
    6856           0 :                 return (0);
    6857             :             }
    6858             :             else
    6859             :             {
    6860           0 :                 err = TIFFReadDirEntrySlongArray(tif, dp, &data);
    6861           0 :                 if (err == TIFFReadDirEntryErrOk)
    6862             :                 {
    6863           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6864             :                     {
    6865           0 :                         if (data != 0)
    6866           0 :                             _TIFFfreeExt(tif, data);
    6867           0 :                         return 0;
    6868             :                     }
    6869             :                     int m;
    6870           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6871           0 :                     if (data != 0)
    6872           0 :                         _TIFFfreeExt(tif, data);
    6873           0 :                     if (!m)
    6874           0 :                         return (0);
    6875             :                 }
    6876             :             }
    6877             :         }
    6878           0 :         break;
    6879           0 :         case TIFF_SETGET_C0_UINT64:
    6880             :         {
    6881             :             uint64_t *data;
    6882           0 :             assert(fip->field_readcount >= 1);
    6883           0 :             assert(fip->field_passcount == 0);
    6884           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6885             :             {
    6886           0 :                 TIFFWarningExtR(tif, module,
    6887             :                                 "incorrect count for field \"%s\", expected "
    6888             :                                 "%d, got %" PRIu64,
    6889           0 :                                 fip->field_name, (int)fip->field_readcount,
    6890             :                                 dp->tdir_count);
    6891           0 :                 return (0);
    6892             :             }
    6893             :             else
    6894             :             {
    6895           0 :                 err = TIFFReadDirEntryLong8Array(tif, dp, &data);
    6896           0 :                 if (err == TIFFReadDirEntryErrOk)
    6897             :                 {
    6898           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6899             :                     {
    6900           0 :                         if (data != 0)
    6901           0 :                             _TIFFfreeExt(tif, data);
    6902           0 :                         return 0;
    6903             :                     }
    6904             :                     int m;
    6905           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6906           0 :                     if (data != 0)
    6907           0 :                         _TIFFfreeExt(tif, data);
    6908           0 :                     if (!m)
    6909           0 :                         return (0);
    6910             :                 }
    6911             :             }
    6912             :         }
    6913           0 :         break;
    6914           0 :         case TIFF_SETGET_C0_SINT64:
    6915             :         {
    6916             :             int64_t *data;
    6917           0 :             assert(fip->field_readcount >= 1);
    6918           0 :             assert(fip->field_passcount == 0);
    6919           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6920             :             {
    6921           0 :                 TIFFWarningExtR(tif, module,
    6922             :                                 "incorrect count for field \"%s\", expected "
    6923             :                                 "%d, got %" PRIu64,
    6924           0 :                                 fip->field_name, (int)fip->field_readcount,
    6925             :                                 dp->tdir_count);
    6926           0 :                 return (0);
    6927             :             }
    6928             :             else
    6929             :             {
    6930           0 :                 err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
    6931           0 :                 if (err == TIFFReadDirEntryErrOk)
    6932             :                 {
    6933           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6934             :                     {
    6935           0 :                         if (data != 0)
    6936           0 :                             _TIFFfreeExt(tif, data);
    6937           0 :                         return 0;
    6938             :                     }
    6939             :                     int m;
    6940           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6941           0 :                     if (data != 0)
    6942           0 :                         _TIFFfreeExt(tif, data);
    6943           0 :                     if (!m)
    6944           0 :                         return (0);
    6945             :                 }
    6946             :             }
    6947             :         }
    6948           0 :         break;
    6949        1026 :         case TIFF_SETGET_C0_FLOAT:
    6950             :         {
    6951             :             float *data;
    6952        1026 :             assert(fip->field_readcount >= 1);
    6953        1026 :             assert(fip->field_passcount == 0);
    6954        1026 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6955             :             {
    6956           0 :                 TIFFWarningExtR(tif, module,
    6957             :                                 "incorrect count for field \"%s\", expected "
    6958             :                                 "%d, got %" PRIu64,
    6959           0 :                                 fip->field_name, (int)fip->field_readcount,
    6960             :                                 dp->tdir_count);
    6961           0 :                 return (0);
    6962             :             }
    6963             :             else
    6964             :             {
    6965        1026 :                 err = TIFFReadDirEntryFloatArray(tif, dp, &data);
    6966        1026 :                 if (err == TIFFReadDirEntryErrOk)
    6967             :                 {
    6968        1026 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6969             :                     {
    6970           0 :                         if (data != 0)
    6971           0 :                             _TIFFfreeExt(tif, data);
    6972           0 :                         return 0;
    6973             :                     }
    6974             :                     int m;
    6975        1026 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6976        1026 :                     if (data != 0)
    6977        1026 :                         _TIFFfreeExt(tif, data);
    6978        1026 :                     if (!m)
    6979           0 :                         return (0);
    6980             :                 }
    6981             :             }
    6982             :         }
    6983        1026 :         break;
    6984             :         /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read
    6985             :          * into Double-Arrays. */
    6986           0 :         case TIFF_SETGET_C0_DOUBLE:
    6987             :         {
    6988             :             double *data;
    6989           0 :             assert(fip->field_readcount >= 1);
    6990           0 :             assert(fip->field_passcount == 0);
    6991           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6992             :             {
    6993           0 :                 TIFFWarningExtR(tif, module,
    6994             :                                 "incorrect count for field \"%s\", expected "
    6995             :                                 "%d, got %" PRIu64,
    6996           0 :                                 fip->field_name, (int)fip->field_readcount,
    6997             :                                 dp->tdir_count);
    6998           0 :                 return (0);
    6999             :             }
    7000             :             else
    7001             :             {
    7002           0 :                 err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
    7003           0 :                 if (err == TIFFReadDirEntryErrOk)
    7004             :                 {
    7005           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7006             :                     {
    7007           0 :                         if (data != 0)
    7008           0 :                             _TIFFfreeExt(tif, data);
    7009           0 :                         return 0;
    7010             :                     }
    7011             :                     int m;
    7012           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    7013           0 :                     if (data != 0)
    7014           0 :                         _TIFFfreeExt(tif, data);
    7015           0 :                     if (!m)
    7016           0 :                         return (0);
    7017             :                 }
    7018             :             }
    7019             :         }
    7020           0 :         break;
    7021           0 :         case TIFF_SETGET_C0_IFD8:
    7022             :         {
    7023             :             uint64_t *data;
    7024           0 :             assert(fip->field_readcount >= 1);
    7025           0 :             assert(fip->field_passcount == 0);
    7026           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    7027             :             {
    7028           0 :                 TIFFWarningExtR(tif, module,
    7029             :                                 "incorrect count for field \"%s\", expected "
    7030             :                                 "%d, got %" PRIu64,
    7031           0 :                                 fip->field_name, (int)fip->field_readcount,
    7032             :                                 dp->tdir_count);
    7033           0 :                 return (0);
    7034             :             }
    7035             :             else
    7036             :             {
    7037           0 :                 err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
    7038           0 :                 if (err == TIFFReadDirEntryErrOk)
    7039             :                 {
    7040           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7041             :                     {
    7042           0 :                         if (data != 0)
    7043           0 :                             _TIFFfreeExt(tif, data);
    7044           0 :                         return 0;
    7045             :                     }
    7046             :                     int m;
    7047           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    7048           0 :                     if (data != 0)
    7049           0 :                         _TIFFfreeExt(tif, data);
    7050           0 :                     if (!m)
    7051           0 :                         return (0);
    7052             :                 }
    7053             :             }
    7054             :         }
    7055           0 :         break;
    7056           0 :         case TIFF_SETGET_C16_ASCII:
    7057             :         {
    7058             :             uint8_t *data;
    7059           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7060           0 :             assert(fip->field_passcount == 1);
    7061           0 :             if (dp->tdir_count > 0xFFFF)
    7062           0 :                 err = TIFFReadDirEntryErrCount;
    7063             :             else
    7064             :             {
    7065           0 :                 err = TIFFReadDirEntryByteArray(tif, dp, &data);
    7066           0 :                 if (err == TIFFReadDirEntryErrOk)
    7067             :                 {
    7068           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7069             :                     {
    7070           0 :                         if (data != 0)
    7071           0 :                             _TIFFfreeExt(tif, data);
    7072           0 :                         return 0;
    7073             :                     }
    7074             :                     int m;
    7075           0 :                     if (data != 0 && dp->tdir_count > 0 &&
    7076           0 :                         data[dp->tdir_count - 1] != '\0')
    7077             :                     {
    7078           0 :                         TIFFWarningExtR(tif, module,
    7079             :                                         "ASCII value for ASCII array tag "
    7080             :                                         "\"%s\" does not end in null "
    7081             :                                         "byte. Forcing it to be null",
    7082             :                                         fip->field_name);
    7083             :                         /* Enlarge buffer and add terminating null. */
    7084           0 :                         uint8_t *o = (uint8_t *)_TIFFmallocExt(
    7085           0 :                             tif, (uint32_t)dp->tdir_count + 1);
    7086           0 :                         if (o == NULL)
    7087             :                         {
    7088           0 :                             if (data != NULL)
    7089           0 :                                 _TIFFfreeExt(tif, data);
    7090           0 :                             return (0);
    7091             :                         }
    7092           0 :                         if (dp->tdir_count > 0)
    7093             :                         {
    7094           0 :                             _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
    7095             :                         }
    7096           0 :                         o[(uint32_t)dp->tdir_count] = 0;
    7097           0 :                         dp->tdir_count++; /* Increment for added null. */
    7098           0 :                         if (data != 0)
    7099           0 :                             _TIFFfreeExt(tif, data);
    7100           0 :                         data = o;
    7101             :                     }
    7102           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7103           0 :                                      (uint16_t)(dp->tdir_count), data);
    7104           0 :                     if (data != 0)
    7105           0 :                         _TIFFfreeExt(tif, data);
    7106           0 :                     if (!m)
    7107           0 :                         return (0);
    7108             :                 }
    7109             :             }
    7110             :         }
    7111           0 :         break;
    7112           0 :         case TIFF_SETGET_C16_UINT8:
    7113             :         {
    7114             :             uint8_t *data;
    7115           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7116           0 :             assert(fip->field_passcount == 1);
    7117           0 :             if (dp->tdir_count > 0xFFFF)
    7118           0 :                 err = TIFFReadDirEntryErrCount;
    7119             :             else
    7120             :             {
    7121           0 :                 err = TIFFReadDirEntryByteArray(tif, dp, &data);
    7122           0 :                 if (err == TIFFReadDirEntryErrOk)
    7123             :                 {
    7124           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7125             :                     {
    7126           0 :                         if (data != 0)
    7127           0 :                             _TIFFfreeExt(tif, data);
    7128           0 :                         return 0;
    7129             :                     }
    7130             :                     int m;
    7131           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7132           0 :                                      (uint16_t)(dp->tdir_count), data);
    7133           0 :                     if (data != 0)
    7134           0 :                         _TIFFfreeExt(tif, data);
    7135           0 :                     if (!m)
    7136           0 :                         return (0);
    7137             :                 }
    7138             :             }
    7139             :         }
    7140           0 :         break;
    7141           0 :         case TIFF_SETGET_C16_SINT8:
    7142             :         {
    7143             :             int8_t *data;
    7144           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7145           0 :             assert(fip->field_passcount == 1);
    7146           0 :             if (dp->tdir_count > 0xFFFF)
    7147           0 :                 err = TIFFReadDirEntryErrCount;
    7148             :             else
    7149             :             {
    7150           0 :                 err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
    7151           0 :                 if (err == TIFFReadDirEntryErrOk)
    7152             :                 {
    7153           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7154             :                     {
    7155           0 :                         if (data != 0)
    7156           0 :                             _TIFFfreeExt(tif, data);
    7157           0 :                         return 0;
    7158             :                     }
    7159             :                     int m;
    7160           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7161           0 :                                      (uint16_t)(dp->tdir_count), data);
    7162           0 :                     if (data != 0)
    7163           0 :                         _TIFFfreeExt(tif, data);
    7164           0 :                     if (!m)
    7165           0 :                         return (0);
    7166             :                 }
    7167             :             }
    7168             :         }
    7169           0 :         break;
    7170       34528 :         case TIFF_SETGET_C16_UINT16:
    7171             :         {
    7172             :             uint16_t *data;
    7173       34528 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7174       34528 :             assert(fip->field_passcount == 1);
    7175       34528 :             if (dp->tdir_count > 0xFFFF)
    7176           0 :                 err = TIFFReadDirEntryErrCount;
    7177             :             else
    7178             :             {
    7179       34528 :                 err = TIFFReadDirEntryShortArray(tif, dp, &data);
    7180       34466 :                 if (err == TIFFReadDirEntryErrOk)
    7181             :                 {
    7182       34455 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7183             :                     {
    7184           0 :                         if (data != 0)
    7185           0 :                             _TIFFfreeExt(tif, data);
    7186           0 :                         return 0;
    7187             :                     }
    7188             :                     int m;
    7189       34502 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7190       34502 :                                      (uint16_t)(dp->tdir_count), data);
    7191       34523 :                     if (data != 0)
    7192       34525 :                         _TIFFfreeExt(tif, data);
    7193       34478 :                     if (!m)
    7194           0 :                         return (0);
    7195             :                 }
    7196             :             }
    7197             :         }
    7198       34489 :         break;
    7199           0 :         case TIFF_SETGET_C16_SINT16:
    7200             :         {
    7201             :             int16_t *data;
    7202           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7203           0 :             assert(fip->field_passcount == 1);
    7204           0 :             if (dp->tdir_count > 0xFFFF)
    7205           0 :                 err = TIFFReadDirEntryErrCount;
    7206             :             else
    7207             :             {
    7208           0 :                 err = TIFFReadDirEntrySshortArray(tif, dp, &data);
    7209           0 :                 if (err == TIFFReadDirEntryErrOk)
    7210             :                 {
    7211           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7212             :                     {
    7213           0 :                         if (data != 0)
    7214           0 :                             _TIFFfreeExt(tif, data);
    7215           0 :                         return 0;
    7216             :                     }
    7217             :                     int m;
    7218           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7219           0 :                                      (uint16_t)(dp->tdir_count), data);
    7220           0 :                     if (data != 0)
    7221           0 :                         _TIFFfreeExt(tif, data);
    7222           0 :                     if (!m)
    7223           0 :                         return (0);
    7224             :                 }
    7225             :             }
    7226             :         }
    7227           0 :         break;
    7228           0 :         case TIFF_SETGET_C16_UINT32:
    7229             :         {
    7230             :             uint32_t *data;
    7231           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7232           0 :             assert(fip->field_passcount == 1);
    7233           0 :             if (dp->tdir_count > 0xFFFF)
    7234           0 :                 err = TIFFReadDirEntryErrCount;
    7235             :             else
    7236             :             {
    7237           0 :                 err = TIFFReadDirEntryLongArray(tif, dp, &data);
    7238           0 :                 if (err == TIFFReadDirEntryErrOk)
    7239             :                 {
    7240           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7241             :                     {
    7242           0 :                         if (data != 0)
    7243           0 :                             _TIFFfreeExt(tif, data);
    7244           0 :                         return 0;
    7245             :                     }
    7246             :                     int m;
    7247           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7248           0 :                                      (uint16_t)(dp->tdir_count), data);
    7249           0 :                     if (data != 0)
    7250           0 :                         _TIFFfreeExt(tif, data);
    7251           0 :                     if (!m)
    7252           0 :                         return (0);
    7253             :                 }
    7254             :             }
    7255             :         }
    7256           0 :         break;
    7257           0 :         case TIFF_SETGET_C16_SINT32:
    7258             :         {
    7259             :             int32_t *data;
    7260           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7261           0 :             assert(fip->field_passcount == 1);
    7262           0 :             if (dp->tdir_count > 0xFFFF)
    7263           0 :                 err = TIFFReadDirEntryErrCount;
    7264             :             else
    7265             :             {
    7266           0 :                 err = TIFFReadDirEntrySlongArray(tif, dp, &data);
    7267           0 :                 if (err == TIFFReadDirEntryErrOk)
    7268             :                 {
    7269           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7270             :                     {
    7271           0 :                         if (data != 0)
    7272           0 :                             _TIFFfreeExt(tif, data);
    7273           0 :                         return 0;
    7274             :                     }
    7275             :                     int m;
    7276           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7277           0 :                                      (uint16_t)(dp->tdir_count), data);
    7278           0 :                     if (data != 0)
    7279           0 :                         _TIFFfreeExt(tif, data);
    7280           0 :                     if (!m)
    7281           0 :                         return (0);
    7282             :                 }
    7283             :             }
    7284             :         }
    7285           0 :         break;
    7286           0 :         case TIFF_SETGET_C16_UINT64:
    7287             :         {
    7288             :             uint64_t *data;
    7289           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7290           0 :             assert(fip->field_passcount == 1);
    7291           0 :             if (dp->tdir_count > 0xFFFF)
    7292           0 :                 err = TIFFReadDirEntryErrCount;
    7293             :             else
    7294             :             {
    7295           0 :                 err = TIFFReadDirEntryLong8Array(tif, dp, &data);
    7296           0 :                 if (err == TIFFReadDirEntryErrOk)
    7297             :                 {
    7298           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7299             :                     {
    7300           0 :                         if (data != 0)
    7301           0 :                             _TIFFfreeExt(tif, data);
    7302           0 :                         return 0;
    7303             :                     }
    7304             :                     int m;
    7305           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7306           0 :                                      (uint16_t)(dp->tdir_count), data);
    7307           0 :                     if (data != 0)
    7308           0 :                         _TIFFfreeExt(tif, data);
    7309           0 :                     if (!m)
    7310           0 :                         return (0);
    7311             :                 }
    7312             :             }
    7313             :         }
    7314           0 :         break;
    7315           0 :         case TIFF_SETGET_C16_SINT64:
    7316             :         {
    7317             :             int64_t *data;
    7318           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7319           0 :             assert(fip->field_passcount == 1);
    7320           0 :             if (dp->tdir_count > 0xFFFF)
    7321           0 :                 err = TIFFReadDirEntryErrCount;
    7322             :             else
    7323             :             {
    7324           0 :                 err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
    7325           0 :                 if (err == TIFFReadDirEntryErrOk)
    7326             :                 {
    7327           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7328             :                     {
    7329           0 :                         if (data != 0)
    7330           0 :                             _TIFFfreeExt(tif, data);
    7331           0 :                         return 0;
    7332             :                     }
    7333             :                     int m;
    7334           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7335           0 :                                      (uint16_t)(dp->tdir_count), data);
    7336           0 :                     if (data != 0)
    7337           0 :                         _TIFFfreeExt(tif, data);
    7338           0 :                     if (!m)
    7339           0 :                         return (0);
    7340             :                 }
    7341             :             }
    7342             :         }
    7343           0 :         break;
    7344           0 :         case TIFF_SETGET_C16_FLOAT:
    7345             :         {
    7346             :             float *data;
    7347           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7348           0 :             assert(fip->field_passcount == 1);
    7349           0 :             if (dp->tdir_count > 0xFFFF)
    7350           0 :                 err = TIFFReadDirEntryErrCount;
    7351             :             else
    7352             :             {
    7353           0 :                 err = TIFFReadDirEntryFloatArray(tif, dp, &data);
    7354           0 :                 if (err == TIFFReadDirEntryErrOk)
    7355             :                 {
    7356           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7357             :                     {
    7358           0 :                         if (data != 0)
    7359           0 :                             _TIFFfreeExt(tif, data);
    7360           0 :                         return 0;
    7361             :                     }
    7362             :                     int m;
    7363           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7364           0 :                                      (uint16_t)(dp->tdir_count), data);
    7365           0 :                     if (data != 0)
    7366           0 :                         _TIFFfreeExt(tif, data);
    7367           0 :                     if (!m)
    7368           0 :                         return (0);
    7369             :                 }
    7370             :             }
    7371             :         }
    7372           0 :         break;
    7373       71858 :         case TIFF_SETGET_C16_DOUBLE:
    7374             :         {
    7375             :             double *data;
    7376       71858 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7377       71858 :             assert(fip->field_passcount == 1);
    7378       71858 :             if (dp->tdir_count > 0xFFFF)
    7379           0 :                 err = TIFFReadDirEntryErrCount;
    7380             :             else
    7381             :             {
    7382       71858 :                 err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
    7383       71792 :                 if (err == TIFFReadDirEntryErrOk)
    7384             :                 {
    7385       71767 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7386             :                     {
    7387           0 :                         if (data != 0)
    7388           0 :                             _TIFFfreeExt(tif, data);
    7389           0 :                         return 0;
    7390             :                     }
    7391             :                     int m;
    7392       71776 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7393       71776 :                                      (uint16_t)(dp->tdir_count), data);
    7394       71824 :                     if (data != 0)
    7395       71819 :                         _TIFFfreeExt(tif, data);
    7396       71800 :                     if (!m)
    7397           0 :                         return (0);
    7398             :                 }
    7399             :             }
    7400             :         }
    7401       71825 :         break;
    7402          81 :         case TIFF_SETGET_C16_IFD8:
    7403             :         {
    7404             :             uint64_t *data;
    7405          81 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7406          81 :             assert(fip->field_passcount == 1);
    7407          81 :             if (dp->tdir_count > 0xFFFF)
    7408           0 :                 err = TIFFReadDirEntryErrCount;
    7409             :             else
    7410             :             {
    7411          81 :                 err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
    7412          81 :                 if (err == TIFFReadDirEntryErrOk)
    7413             :                 {
    7414          81 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7415             :                     {
    7416           0 :                         if (data != 0)
    7417           0 :                             _TIFFfreeExt(tif, data);
    7418           0 :                         return 0;
    7419             :                     }
    7420             :                     int m;
    7421          81 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7422          81 :                                      (uint16_t)(dp->tdir_count), data);
    7423          81 :                     if (data != 0)
    7424          81 :                         _TIFFfreeExt(tif, data);
    7425          81 :                     if (!m)
    7426           0 :                         return (0);
    7427             :                 }
    7428             :             }
    7429             :         }
    7430          81 :         break;
    7431           9 :         case TIFF_SETGET_C32_ASCII:
    7432             :         {
    7433             :             uint8_t *data;
    7434           9 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7435           9 :             assert(fip->field_passcount == 1);
    7436           9 :             err = TIFFReadDirEntryByteArray(tif, dp, &data);
    7437           9 :             if (err == TIFFReadDirEntryErrOk)
    7438             :             {
    7439           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7440             :                 {
    7441           0 :                     if (data != 0)
    7442           0 :                         _TIFFfreeExt(tif, data);
    7443           0 :                     return 0;
    7444             :                 }
    7445             :                 int m;
    7446           0 :                 if (data != 0 && dp->tdir_count > 0 &&
    7447           0 :                     data[dp->tdir_count - 1] != '\0')
    7448             :                 {
    7449           0 :                     TIFFWarningExtR(
    7450             :                         tif, module,
    7451             :                         "ASCII value for ASCII array tag \"%s\" does not end "
    7452             :                         "in null byte. Forcing it to be null",
    7453             :                         fip->field_name);
    7454             :                     /* Enlarge buffer and add terminating null. */
    7455           0 :                     uint8_t *o = (uint8_t *)_TIFFmallocExt(
    7456           0 :                         tif, (uint32_t)dp->tdir_count + 1);
    7457           0 :                     if (o == NULL)
    7458             :                     {
    7459           0 :                         if (data != NULL)
    7460           0 :                             _TIFFfreeExt(tif, data);
    7461           0 :                         return (0);
    7462             :                     }
    7463           0 :                     if (dp->tdir_count > 0)
    7464             :                     {
    7465           0 :                         _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
    7466             :                     }
    7467           0 :                     o[(uint32_t)dp->tdir_count] = 0;
    7468           0 :                     dp->tdir_count++; /* Increment for added null. */
    7469           0 :                     if (data != 0)
    7470           0 :                         _TIFFfreeExt(tif, data);
    7471           0 :                     data = o;
    7472             :                 }
    7473           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7474             :                                  data);
    7475           0 :                 if (data != 0)
    7476           0 :                     _TIFFfreeExt(tif, data);
    7477           0 :                 if (!m)
    7478           0 :                     return (0);
    7479             :             }
    7480             :         }
    7481           9 :         break;
    7482        2087 :         case TIFF_SETGET_C32_UINT8:
    7483             :         {
    7484             :             uint8_t *data;
    7485        2087 :             uint32_t count = 0;
    7486        2087 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7487        2087 :             assert(fip->field_passcount == 1);
    7488        2087 :             if (fip->field_tag == TIFFTAG_RICHTIFFIPTC &&
    7489           0 :                 dp->tdir_type == TIFF_LONG)
    7490           0 :             {
    7491             :                 /* Adobe's software (wrongly) writes RichTIFFIPTC tag with
    7492             :                  * data type LONG instead of UNDEFINED. Work around this
    7493             :                  * frequently found issue */
    7494             :                 void *origdata;
    7495           0 :                 err = TIFFReadDirEntryArray(tif, dp, &count, 4, &origdata);
    7496           0 :                 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    7497             :                 {
    7498           0 :                     data = NULL;
    7499             :                 }
    7500             :                 else
    7501             :                 {
    7502           0 :                     if (tif->tif_flags & TIFF_SWAB)
    7503           0 :                         TIFFSwabArrayOfLong((uint32_t *)origdata, count);
    7504           0 :                     data = (uint8_t *)origdata;
    7505           0 :                     count = (uint32_t)(count * 4);
    7506             :                 }
    7507             :             }
    7508             :             else
    7509             :             {
    7510        2087 :                 err = TIFFReadDirEntryByteArray(tif, dp, &data);
    7511        2087 :                 count = (uint32_t)(dp->tdir_count);
    7512             :             }
    7513        2087 :             if (err == TIFFReadDirEntryErrOk)
    7514             :             {
    7515        2078 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7516             :                 {
    7517           0 :                     if (data != 0)
    7518           0 :                         _TIFFfreeExt(tif, data);
    7519           0 :                     return 0;
    7520             :                 }
    7521             :                 int m;
    7522        2078 :                 m = TIFFSetField(tif, dp->tdir_tag, count, data);
    7523        2078 :                 if (data != 0)
    7524        2078 :                     _TIFFfreeExt(tif, data);
    7525        2078 :                 if (!m)
    7526           0 :                     return (0);
    7527             :             }
    7528             :         }
    7529        2087 :         break;
    7530           0 :         case TIFF_SETGET_C32_SINT8:
    7531             :         {
    7532           0 :             int8_t *data = NULL;
    7533           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7534           0 :             assert(fip->field_passcount == 1);
    7535           0 :             err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
    7536           0 :             if (err == TIFFReadDirEntryErrOk)
    7537             :             {
    7538           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7539             :                 {
    7540           0 :                     if (data != 0)
    7541           0 :                         _TIFFfreeExt(tif, data);
    7542           0 :                     return 0;
    7543             :                 }
    7544             :                 int m;
    7545           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7546             :                                  data);
    7547           0 :                 if (data != 0)
    7548           0 :                     _TIFFfreeExt(tif, data);
    7549           0 :                 if (!m)
    7550           0 :                     return (0);
    7551             :             }
    7552             :         }
    7553           0 :         break;
    7554          12 :         case TIFF_SETGET_C32_UINT16:
    7555             :         {
    7556             :             uint16_t *data;
    7557          12 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7558          12 :             assert(fip->field_passcount == 1);
    7559          12 :             err = TIFFReadDirEntryShortArray(tif, dp, &data);
    7560          12 :             if (err == TIFFReadDirEntryErrOk)
    7561             :             {
    7562          12 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7563             :                 {
    7564           0 :                     if (data != 0)
    7565           0 :                         _TIFFfreeExt(tif, data);
    7566           0 :                     return 0;
    7567             :                 }
    7568             :                 int m;
    7569          12 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7570             :                                  data);
    7571          12 :                 if (data != 0)
    7572          12 :                     _TIFFfreeExt(tif, data);
    7573          12 :                 if (!m)
    7574           0 :                     return (0);
    7575             :             }
    7576             :         }
    7577          12 :         break;
    7578           0 :         case TIFF_SETGET_C32_SINT16:
    7579             :         {
    7580           0 :             int16_t *data = NULL;
    7581           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7582           0 :             assert(fip->field_passcount == 1);
    7583           0 :             err = TIFFReadDirEntrySshortArray(tif, dp, &data);
    7584           0 :             if (err == TIFFReadDirEntryErrOk)
    7585             :             {
    7586           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7587             :                 {
    7588           0 :                     if (data != 0)
    7589           0 :                         _TIFFfreeExt(tif, data);
    7590           0 :                     return 0;
    7591             :                 }
    7592             :                 int m;
    7593           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7594             :                                  data);
    7595           0 :                 if (data != 0)
    7596           0 :                     _TIFFfreeExt(tif, data);
    7597           0 :                 if (!m)
    7598           0 :                     return (0);
    7599             :             }
    7600             :         }
    7601           0 :         break;
    7602         800 :         case TIFF_SETGET_C32_UINT32:
    7603             :         {
    7604             :             uint32_t *data;
    7605         800 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7606         800 :             assert(fip->field_passcount == 1);
    7607         800 :             err = TIFFReadDirEntryLongArray(tif, dp, &data);
    7608         800 :             if (err == TIFFReadDirEntryErrOk)
    7609             :             {
    7610         791 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7611             :                 {
    7612           0 :                     if (data != 0)
    7613           0 :                         _TIFFfreeExt(tif, data);
    7614           0 :                     return 0;
    7615             :                 }
    7616             :                 int m;
    7617         791 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7618             :                                  data);
    7619         791 :                 if (data != 0)
    7620         791 :                     _TIFFfreeExt(tif, data);
    7621         791 :                 if (!m)
    7622           0 :                     return (0);
    7623             :             }
    7624             :         }
    7625         800 :         break;
    7626           0 :         case TIFF_SETGET_C32_SINT32:
    7627             :         {
    7628           0 :             int32_t *data = NULL;
    7629           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7630           0 :             assert(fip->field_passcount == 1);
    7631           0 :             err = TIFFReadDirEntrySlongArray(tif, dp, &data);
    7632           0 :             if (err == TIFFReadDirEntryErrOk)
    7633             :             {
    7634           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7635             :                 {
    7636           0 :                     if (data != 0)
    7637           0 :                         _TIFFfreeExt(tif, data);
    7638           0 :                     return 0;
    7639             :                 }
    7640             :                 int m;
    7641           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7642             :                                  data);
    7643           0 :                 if (data != 0)
    7644           0 :                     _TIFFfreeExt(tif, data);
    7645           0 :                 if (!m)
    7646           0 :                     return (0);
    7647             :             }
    7648             :         }
    7649           0 :         break;
    7650           6 :         case TIFF_SETGET_C32_UINT64:
    7651             :         {
    7652             :             uint64_t *data;
    7653           6 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7654           6 :             assert(fip->field_passcount == 1);
    7655           6 :             err = TIFFReadDirEntryLong8Array(tif, dp, &data);
    7656           6 :             if (err == TIFFReadDirEntryErrOk)
    7657             :             {
    7658           6 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7659             :                 {
    7660           0 :                     if (data != 0)
    7661           0 :                         _TIFFfreeExt(tif, data);
    7662           0 :                     return 0;
    7663             :                 }
    7664             :                 int m;
    7665           6 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7666             :                                  data);
    7667           6 :                 if (data != 0)
    7668           6 :                     _TIFFfreeExt(tif, data);
    7669           6 :                 if (!m)
    7670           0 :                     return (0);
    7671             :             }
    7672             :         }
    7673           6 :         break;
    7674           0 :         case TIFF_SETGET_C32_SINT64:
    7675             :         {
    7676           0 :             int64_t *data = NULL;
    7677           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7678           0 :             assert(fip->field_passcount == 1);
    7679           0 :             err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
    7680           0 :             if (err == TIFFReadDirEntryErrOk)
    7681             :             {
    7682           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7683             :                 {
    7684           0 :                     if (data != 0)
    7685           0 :                         _TIFFfreeExt(tif, data);
    7686           0 :                     return 0;
    7687             :                 }
    7688             :                 int m;
    7689           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7690             :                                  data);
    7691           0 :                 if (data != 0)
    7692           0 :                     _TIFFfreeExt(tif, data);
    7693           0 :                 if (!m)
    7694           0 :                     return (0);
    7695             :             }
    7696             :         }
    7697           0 :         break;
    7698           0 :         case TIFF_SETGET_C32_FLOAT:
    7699             :         {
    7700             :             float *data;
    7701           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7702           0 :             assert(fip->field_passcount == 1);
    7703           0 :             err = TIFFReadDirEntryFloatArray(tif, dp, &data);
    7704           0 :             if (err == TIFFReadDirEntryErrOk)
    7705             :             {
    7706           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7707             :                 {
    7708           0 :                     if (data != 0)
    7709           0 :                         _TIFFfreeExt(tif, data);
    7710           0 :                     return 0;
    7711             :                 }
    7712             :                 int m;
    7713           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7714             :                                  data);
    7715           0 :                 if (data != 0)
    7716           0 :                     _TIFFfreeExt(tif, data);
    7717           0 :                 if (!m)
    7718           0 :                     return (0);
    7719             :             }
    7720             :         }
    7721           0 :         break;
    7722           0 :         case TIFF_SETGET_C32_DOUBLE:
    7723             :         {
    7724             :             double *data;
    7725           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7726           0 :             assert(fip->field_passcount == 1);
    7727           0 :             err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
    7728           0 :             if (err == TIFFReadDirEntryErrOk)
    7729             :             {
    7730           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7731             :                 {
    7732           0 :                     if (data != 0)
    7733           0 :                         _TIFFfreeExt(tif, data);
    7734           0 :                     return 0;
    7735             :                 }
    7736             :                 int m;
    7737           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7738             :                                  data);
    7739           0 :                 if (data != 0)
    7740           0 :                     _TIFFfreeExt(tif, data);
    7741           0 :                 if (!m)
    7742           0 :                     return (0);
    7743             :             }
    7744             :         }
    7745           0 :         break;
    7746           0 :         case TIFF_SETGET_C32_IFD8:
    7747             :         {
    7748             :             uint64_t *data;
    7749           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7750           0 :             assert(fip->field_passcount == 1);
    7751           0 :             err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
    7752           0 :             if (err == TIFFReadDirEntryErrOk)
    7753             :             {
    7754           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7755             :                 {
    7756           0 :                     if (data != 0)
    7757           0 :                         _TIFFfreeExt(tif, data);
    7758           0 :                     return 0;
    7759             :                 }
    7760             :                 int m;
    7761           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7762             :                                  data);
    7763           0 :                 if (data != 0)
    7764           0 :                     _TIFFfreeExt(tif, data);
    7765           0 :                 if (!m)
    7766           0 :                     return (0);
    7767             :             }
    7768             :         }
    7769           0 :         break;
    7770           0 :         case TIFF_SETGET_INT:
    7771             :         case TIFF_SETGET_C0_ASCII:
    7772             :         case TIFF_SETGET_OTHER:
    7773           0 :             assert(0); /* these should not arrive here */
    7774             :             break;
    7775           0 :         default:
    7776           0 :             assert(0); /* we should never get here */
    7777             :             break;
    7778             :     }
    7779      502673 :     if (err != TIFFReadDirEntryErrOk)
    7780             :     {
    7781          36 :         TIFFReadDirEntryOutputErr(tif, err, module, fip->field_name, recover);
    7782          36 :         return (0);
    7783             :     }
    7784      502637 :     return (1);
    7785             : }
    7786             : 
    7787             : /*
    7788             :  * Fetch a set of offsets or lengths.
    7789             :  * While this routine says "strips", in fact it's also used for tiles.
    7790             :  */
    7791       86199 : static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
    7792             :                                uint64_t **lpp)
    7793             : {
    7794             :     static const char module[] = "TIFFFetchStripThing";
    7795             :     enum TIFFReadDirEntryErr err;
    7796             :     uint64_t *data;
    7797       86199 :     err = TIFFReadDirEntryLong8ArrayWithLimit(tif, dir, &data, nstrips);
    7798       86190 :     if (err != TIFFReadDirEntryErrOk)
    7799             :     {
    7800           2 :         const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
    7801           2 :         TIFFReadDirEntryOutputErr(tif, err, module,
    7802             :                                   fip ? fip->field_name : "unknown tagname", 0);
    7803           2 :         return (0);
    7804             :     }
    7805       86188 :     if (dir->tdir_count < (uint64_t)nstrips)
    7806             :     {
    7807             :         uint64_t *resizeddata;
    7808           5 :         const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
    7809           5 :         const char *pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
    7810           5 :         uint32_t max_nstrips = 1000000;
    7811           5 :         if (pszMax)
    7812           0 :             max_nstrips = (uint32_t)atoi(pszMax);
    7813           5 :         TIFFReadDirEntryOutputErr(tif, TIFFReadDirEntryErrCount, module,
    7814             :                                   fip ? fip->field_name : "unknown tagname",
    7815             :                                   (nstrips <= max_nstrips));
    7816             : 
    7817           5 :         if (nstrips > max_nstrips)
    7818             :         {
    7819           0 :             _TIFFfreeExt(tif, data);
    7820           0 :             return (0);
    7821             :         }
    7822             : 
    7823           5 :         const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t);
    7824           5 :         if (allocsize > 100 * 1024 * 1024)
    7825             :         {
    7826             :             /* Before allocating a huge amount of memory for corrupted files,
    7827             :              * check if size of requested memory is not greater than file size.
    7828             :              */
    7829           0 :             const uint64_t filesize = TIFFGetFileSize(tif);
    7830           0 :             if (allocsize > filesize)
    7831             :             {
    7832           0 :                 TIFFWarningExtR(
    7833             :                     tif, module,
    7834             :                     "Requested memory size for StripArray of %" PRIu64
    7835             :                     " is greater than filesize %" PRIu64
    7836             :                     ". Memory not allocated",
    7837             :                     allocsize, filesize);
    7838           0 :                 _TIFFfreeExt(tif, data);
    7839           0 :                 return (0);
    7840             :             }
    7841             :         }
    7842           5 :         resizeddata = (uint64_t *)_TIFFCheckMalloc(
    7843             :             tif, nstrips, sizeof(uint64_t), "for strip array");
    7844           5 :         if (resizeddata == 0)
    7845             :         {
    7846           0 :             _TIFFfreeExt(tif, data);
    7847           0 :             return (0);
    7848             :         }
    7849           5 :         if (dir->tdir_count)
    7850           5 :             _TIFFmemcpy(resizeddata, data,
    7851           5 :                         (tmsize_t)((size_t)dir->tdir_count * sizeof(uint64_t)));
    7852           5 :         _TIFFmemset(resizeddata + (uint32_t)dir->tdir_count, 0,
    7853           5 :                     (tmsize_t)((size_t)(nstrips - (uint32_t)dir->tdir_count) *
    7854             :                                sizeof(uint64_t)));
    7855           5 :         _TIFFfreeExt(tif, data);
    7856          16 :         data = resizeddata;
    7857             :     }
    7858       86199 :     *lpp = data;
    7859       86199 :     return (1);
    7860             : }
    7861             : 
    7862             : /*
    7863             :  * Fetch and set the SubjectDistance EXIF tag.
    7864             :  */
    7865           0 : static int TIFFFetchSubjectDistance(TIFF *tif, TIFFDirEntry *dir)
    7866             : {
    7867             :     static const char module[] = "TIFFFetchSubjectDistance";
    7868             :     enum TIFFReadDirEntryErr err;
    7869             :     UInt64Aligned_t m;
    7870           0 :     m.l = 0;
    7871             :     assert(sizeof(double) == 8);
    7872             :     assert(sizeof(uint64_t) == 8);
    7873             :     assert(sizeof(uint32_t) == 4);
    7874           0 :     if (dir->tdir_count != 1)
    7875           0 :         err = TIFFReadDirEntryErrCount;
    7876           0 :     else if (dir->tdir_type != TIFF_RATIONAL)
    7877           0 :         err = TIFFReadDirEntryErrType;
    7878             :     else
    7879             :     {
    7880           0 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    7881             :         {
    7882             :             uint32_t offset;
    7883           0 :             offset = *(uint32_t *)(&dir->tdir_offset);
    7884           0 :             if (tif->tif_flags & TIFF_SWAB)
    7885           0 :                 TIFFSwabLong(&offset);
    7886           0 :             err = TIFFReadDirEntryData(tif, offset, 8, m.i);
    7887             :         }
    7888             :         else
    7889             :         {
    7890           0 :             m.l = dir->tdir_offset.toff_long8;
    7891           0 :             err = TIFFReadDirEntryErrOk;
    7892             :         }
    7893             :     }
    7894           0 :     if (err == TIFFReadDirEntryErrOk)
    7895             :     {
    7896             :         double n;
    7897           0 :         if (tif->tif_flags & TIFF_SWAB)
    7898           0 :             TIFFSwabArrayOfLong(m.i, 2);
    7899           0 :         if (m.i[0] == 0)
    7900           0 :             n = 0.0;
    7901           0 :         else if (m.i[0] == 0xFFFFFFFF || m.i[1] == 0)
    7902             :             /*
    7903             :              * XXX: Numerator 0xFFFFFFFF means that we have infinite
    7904             :              * distance. Indicate that with a negative floating point
    7905             :              * SubjectDistance value.
    7906             :              */
    7907           0 :             n = -1.0;
    7908             :         else
    7909           0 :             n = (double)m.i[0] / (double)m.i[1];
    7910           0 :         return (TIFFSetField(tif, dir->tdir_tag, n));
    7911             :     }
    7912             :     else
    7913             :     {
    7914           0 :         TIFFReadDirEntryOutputErr(tif, err, module, "SubjectDistance", TRUE);
    7915           0 :         return (0);
    7916             :     }
    7917             : }
    7918             : 
    7919         159 : static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips,
    7920             :                                       uint64_t stripbytes,
    7921             :                                       uint32_t rowsperstrip)
    7922             : {
    7923         159 :     TIFFDirectory *td = &tif->tif_dir;
    7924             :     uint64_t bytecount;
    7925             :     uint64_t offset;
    7926             :     uint64_t last_offset;
    7927             :     uint64_t last_bytecount;
    7928             :     uint32_t i;
    7929             :     uint64_t *newcounts;
    7930             :     uint64_t *newoffsets;
    7931             : 
    7932         159 :     offset = TIFFGetStrileOffset(tif, 0);
    7933         159 :     last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
    7934         159 :     last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
    7935         159 :     if (last_offset > UINT64_MAX - last_bytecount ||
    7936         159 :         last_offset + last_bytecount < offset)
    7937             :     {
    7938           0 :         return;
    7939             :     }
    7940         159 :     bytecount = last_offset + last_bytecount - offset;
    7941             : 
    7942             :     /* Before allocating a huge amount of memory for corrupted files, check if
    7943             :      * size of StripByteCount and StripOffset tags is not greater than
    7944             :      * file size.
    7945             :      */
    7946         159 :     const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t) * 2;
    7947         159 :     if (allocsize > 100 * 1024 * 1024)
    7948             :     {
    7949           0 :         const uint64_t filesize = TIFFGetFileSize(tif);
    7950           0 :         if (allocsize > filesize)
    7951             :         {
    7952           0 :             TIFFWarningExtR(tif, "allocChoppedUpStripArrays",
    7953             :                             "Requested memory size for StripByteCount and "
    7954             :                             "StripOffsets %" PRIu64
    7955             :                             " is greater than filesize %" PRIu64
    7956             :                             ". Memory not allocated",
    7957             :                             allocsize, filesize);
    7958           0 :             return;
    7959             :         }
    7960             :     }
    7961             : 
    7962             :     newcounts =
    7963         159 :         (uint64_t *)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t),
    7964             :                                      "for chopped \"StripByteCounts\" array");
    7965         159 :     newoffsets = (uint64_t *)_TIFFCheckMalloc(
    7966             :         tif, nstrips, sizeof(uint64_t), "for chopped \"StripOffsets\" array");
    7967         159 :     if (newcounts == NULL || newoffsets == NULL)
    7968             :     {
    7969             :         /*
    7970             :          * Unable to allocate new strip information, give up and use
    7971             :          * the original one strip information.
    7972             :          */
    7973           0 :         if (newcounts != NULL)
    7974           0 :             _TIFFfreeExt(tif, newcounts);
    7975           0 :         if (newoffsets != NULL)
    7976           0 :             _TIFFfreeExt(tif, newoffsets);
    7977           0 :         return;
    7978             :     }
    7979             : 
    7980             :     /*
    7981             :      * Fill the strip information arrays with new bytecounts and offsets
    7982             :      * that reflect the broken-up format.
    7983             :      */
    7984      106924 :     for (i = 0; i < nstrips; i++)
    7985             :     {
    7986      106765 :         if (stripbytes > bytecount)
    7987          53 :             stripbytes = bytecount;
    7988      106765 :         newcounts[i] = stripbytes;
    7989      106765 :         newoffsets[i] = stripbytes ? offset : 0;
    7990      106765 :         offset += stripbytes;
    7991      106765 :         bytecount -= stripbytes;
    7992             :     }
    7993             : 
    7994             :     /*
    7995             :      * Replace old single strip info with multi-strip info.
    7996             :      */
    7997         159 :     td->td_stripsperimage = td->td_nstrips = nstrips;
    7998         159 :     TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
    7999             : 
    8000         159 :     _TIFFfreeExt(tif, td->td_stripbytecount_p);
    8001         159 :     _TIFFfreeExt(tif, td->td_stripoffset_p);
    8002         159 :     td->td_stripbytecount_p = newcounts;
    8003         159 :     td->td_stripoffset_p = newoffsets;
    8004             : #ifdef STRIPBYTECOUNTSORTED_UNUSED
    8005             :     td->td_stripbytecountsorted = 1;
    8006             : #endif
    8007         159 :     tif->tif_flags |= TIFF_CHOPPEDUPARRAYS;
    8008             : }
    8009             : 
    8010             : /*
    8011             :  * Replace a single strip (tile) of uncompressed data by multiple strips
    8012             :  * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
    8013             :  * dealing with large images or for dealing with machines with a limited
    8014             :  * amount memory.
    8015             :  */
    8016       22494 : static void ChopUpSingleUncompressedStrip(TIFF *tif)
    8017             : {
    8018       22494 :     TIFFDirectory *td = &tif->tif_dir;
    8019             :     uint64_t bytecount;
    8020             :     uint64_t offset;
    8021             :     uint32_t rowblock;
    8022             :     uint64_t rowblockbytes;
    8023             :     uint64_t stripbytes;
    8024             :     uint32_t nstrips;
    8025             :     uint32_t rowsperstrip;
    8026             : 
    8027       22494 :     bytecount = TIFFGetStrileByteCount(tif, 0);
    8028             :     /* On a newly created file, just re-opened to be filled, we */
    8029             :     /* don't want strip chop to trigger as it is going to cause issues */
    8030             :     /* later ( StripOffsets and StripByteCounts improperly filled) . */
    8031       22487 :     if (bytecount == 0 && tif->tif_mode != O_RDONLY)
    8032         590 :         return;
    8033       21897 :     offset = TIFFGetStrileOffset(tif, 0);
    8034       21903 :     assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
    8035       21886 :     if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
    8036           1 :         rowblock = td->td_ycbcrsubsampling[1];
    8037             :     else
    8038       21885 :         rowblock = 1;
    8039       21886 :     rowblockbytes = TIFFVTileSize64(tif, rowblock);
    8040             :     /*
    8041             :      * Make the rows hold at least one scanline, but fill specified amount
    8042             :      * of data if possible.
    8043             :      */
    8044       21901 :     if (rowblockbytes > STRIP_SIZE_DEFAULT)
    8045             :     {
    8046          23 :         stripbytes = rowblockbytes;
    8047          23 :         rowsperstrip = rowblock;
    8048             :     }
    8049       21878 :     else if (rowblockbytes > 0)
    8050             :     {
    8051             :         uint32_t rowblocksperstrip;
    8052       21857 :         rowblocksperstrip = (uint32_t)(STRIP_SIZE_DEFAULT / rowblockbytes);
    8053       21857 :         rowsperstrip = rowblocksperstrip * rowblock;
    8054       21857 :         stripbytes = rowblocksperstrip * rowblockbytes;
    8055             :     }
    8056             :     else
    8057          21 :         return;
    8058             : 
    8059             :     /*
    8060             :      * never increase the number of rows per strip
    8061             :      */
    8062       21880 :     if (rowsperstrip >= td->td_rowsperstrip || rowsperstrip == 0)
    8063       21722 :         return;
    8064         158 :     nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
    8065         158 :     if (nstrips == 0)
    8066           0 :         return;
    8067             : 
    8068             :     /* If we are going to allocate a lot of memory, make sure that the */
    8069             :     /* file is as big as needed */
    8070         159 :     if (tif->tif_mode == O_RDONLY && nstrips > 1000000 &&
    8071           1 :         (offset >= TIFFGetFileSize(tif) ||
    8072           1 :          stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)))
    8073             :     {
    8074           1 :         return;
    8075             :     }
    8076             : 
    8077         157 :     allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
    8078             : }
    8079             : 
    8080             : /*
    8081             :  * Replace a file with contiguous strips > 2 GB of uncompressed data by
    8082             :  * multiple smaller strips. This is useful for
    8083             :  * dealing with large images or for dealing with machines with a limited
    8084             :  * amount memory.
    8085             :  */
    8086           5 : static void TryChopUpUncompressedBigTiff(TIFF *tif)
    8087             : {
    8088           5 :     TIFFDirectory *td = &tif->tif_dir;
    8089             :     uint32_t rowblock;
    8090             :     uint64_t rowblockbytes;
    8091             :     uint32_t i;
    8092             :     uint64_t stripsize;
    8093             :     uint32_t rowblocksperstrip;
    8094             :     uint32_t rowsperstrip;
    8095             :     uint64_t stripbytes;
    8096             :     uint32_t nstrips;
    8097             : 
    8098           5 :     stripsize = TIFFStripSize64(tif);
    8099             : 
    8100           5 :     assert(tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG);
    8101           5 :     assert(tif->tif_dir.td_compression == COMPRESSION_NONE);
    8102           5 :     assert((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) ==
    8103             :            TIFF_STRIPCHOP);
    8104           5 :     assert(stripsize > 0x7FFFFFFFUL);
    8105             : 
    8106             :     /* On a newly created file, just re-opened to be filled, we */
    8107             :     /* don't want strip chop to trigger as it is going to cause issues */
    8108             :     /* later ( StripOffsets and StripByteCounts improperly filled) . */
    8109           5 :     if (TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY)
    8110           0 :         return;
    8111             : 
    8112           5 :     if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
    8113           0 :         rowblock = td->td_ycbcrsubsampling[1];
    8114             :     else
    8115           5 :         rowblock = 1;
    8116           5 :     rowblockbytes = TIFFVStripSize64(tif, rowblock);
    8117           5 :     if (rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL)
    8118             :     {
    8119             :         /* In case of file with gigantic width */
    8120           3 :         return;
    8121             :     }
    8122             : 
    8123             :     /* Check that the strips are contiguous and of the expected size */
    8124           5 :     for (i = 0; i < td->td_nstrips; i++)
    8125             :     {
    8126           3 :         if (i == td->td_nstrips - 1)
    8127             :         {
    8128           4 :             if (TIFFGetStrileByteCount(tif, i) <
    8129           2 :                 TIFFVStripSize64(tif,
    8130           2 :                                  td->td_imagelength - i * td->td_rowsperstrip))
    8131             :             {
    8132           0 :                 return;
    8133             :             }
    8134             :         }
    8135             :         else
    8136             :         {
    8137           1 :             if (TIFFGetStrileByteCount(tif, i) != stripsize)
    8138             :             {
    8139           0 :                 return;
    8140             :             }
    8141           1 :             if (i > 0 && TIFFGetStrileOffset(tif, i) !=
    8142           0 :                              TIFFGetStrileOffset(tif, i - 1) +
    8143           0 :                                  TIFFGetStrileByteCount(tif, i - 1))
    8144             :             {
    8145           0 :                 return;
    8146             :             }
    8147             :         }
    8148             :     }
    8149             : 
    8150             :     /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
    8151           2 :     rowblocksperstrip = (uint32_t)(512 * 1024 * 1024 / rowblockbytes);
    8152           2 :     if (rowblocksperstrip == 0)
    8153           0 :         rowblocksperstrip = 1;
    8154           2 :     rowsperstrip = rowblocksperstrip * rowblock;
    8155           2 :     stripbytes = rowblocksperstrip * rowblockbytes;
    8156           2 :     assert(stripbytes <= 0x7FFFFFFFUL);
    8157             : 
    8158           2 :     if (rowsperstrip == 0)
    8159           0 :         return;
    8160           2 :     nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
    8161           2 :     if (nstrips == 0)
    8162           0 :         return;
    8163             : 
    8164             :     /* If we are going to allocate a lot of memory, make sure that the */
    8165             :     /* file is as big as needed */
    8166           2 :     if (tif->tif_mode == O_RDONLY && nstrips > 1000000)
    8167             :     {
    8168           0 :         uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
    8169           0 :         uint64_t filesize = TIFFGetFileSize(tif);
    8170             :         uint64_t last_bytecount =
    8171           0 :             TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
    8172           0 :         if (last_offset > filesize || last_bytecount > filesize - last_offset)
    8173             :         {
    8174           0 :             return;
    8175             :         }
    8176             :     }
    8177             : 
    8178           2 :     allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
    8179             : }
    8180             : 
    8181             : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
    8182     6341440 : static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b)
    8183             : {
    8184     6341440 :     return a + (uint64_t)b;
    8185             : }
    8186             : 
    8187             : /* Read the value of [Strip|Tile]Offset or [Strip|Tile]ByteCount around
    8188             :  * strip/tile of number strile. Also fetch the neighbouring values using a
    8189             :  * 4096 byte page size.
    8190             :  */
    8191        6657 : static int _TIFFPartialReadStripArray(TIFF *tif, TIFFDirEntry *dirent,
    8192             :                                       int strile, uint64_t *panVals)
    8193             : {
    8194             :     static const char module[] = "_TIFFPartialReadStripArray";
    8195             : #define IO_CACHE_PAGE_SIZE 4096
    8196             : 
    8197             :     size_t sizeofval;
    8198        6657 :     const int bSwab = (tif->tif_flags & TIFF_SWAB) != 0;
    8199             :     int sizeofvalint;
    8200             :     uint64_t nBaseOffset;
    8201             :     uint64_t nOffset;
    8202             :     uint64_t nOffsetStartPage;
    8203             :     uint64_t nOffsetEndPage;
    8204             :     tmsize_t nToRead;
    8205             :     tmsize_t nRead;
    8206             :     uint64_t nLastStripOffset;
    8207             :     int iStartBefore;
    8208             :     int i;
    8209        6657 :     const uint32_t arraySize = tif->tif_dir.td_stripoffsetbyteallocsize;
    8210             :     unsigned char buffer[2 * IO_CACHE_PAGE_SIZE];
    8211             : 
    8212        6657 :     assert(dirent->tdir_count > 4);
    8213             : 
    8214        6657 :     if (dirent->tdir_type == TIFF_SHORT)
    8215             :     {
    8216        1984 :         sizeofval = sizeof(uint16_t);
    8217             :     }
    8218        4673 :     else if (dirent->tdir_type == TIFF_LONG)
    8219             :     {
    8220        4631 :         sizeofval = sizeof(uint32_t);
    8221             :     }
    8222          42 :     else if (dirent->tdir_type == TIFF_LONG8)
    8223             :     {
    8224          36 :         sizeofval = sizeof(uint64_t);
    8225             :     }
    8226           6 :     else if (dirent->tdir_type == TIFF_SLONG8)
    8227             :     {
    8228             :         /* Non conformant but used by some images as in */
    8229             :         /* https://github.com/OSGeo/gdal/issues/2165 */
    8230           0 :         sizeofval = sizeof(int64_t);
    8231             :     }
    8232             :     else
    8233             :     {
    8234           6 :         TIFFErrorExtR(tif, module,
    8235             :                       "Invalid type for [Strip|Tile][Offset/ByteCount] tag");
    8236           6 :         panVals[strile] = 0;
    8237           6 :         return 0;
    8238             :     }
    8239        6651 :     sizeofvalint = (int)(sizeofval);
    8240             : 
    8241        6651 :     if (tif->tif_flags & TIFF_BIGTIFF)
    8242             :     {
    8243          66 :         uint64_t offset = dirent->tdir_offset.toff_long8;
    8244          66 :         if (bSwab)
    8245           0 :             TIFFSwabLong8(&offset);
    8246          66 :         nBaseOffset = offset;
    8247             :     }
    8248             :     else
    8249             :     {
    8250        6585 :         uint32_t offset = dirent->tdir_offset.toff_long;
    8251        6585 :         if (bSwab)
    8252          88 :             TIFFSwabLong(&offset);
    8253        6585 :         nBaseOffset = offset;
    8254             :     }
    8255             :     /* To avoid later unsigned integer overflows */
    8256        6651 :     if (nBaseOffset > (uint64_t)INT64_MAX)
    8257             :     {
    8258           0 :         TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
    8259             :                       strile);
    8260           0 :         panVals[strile] = 0;
    8261           0 :         return 0;
    8262             :     }
    8263        6651 :     nOffset = nBaseOffset + (uint64_t)sizeofval * (uint64_t)strile;
    8264        6651 :     nOffsetStartPage = (nOffset / IO_CACHE_PAGE_SIZE) * IO_CACHE_PAGE_SIZE;
    8265        6651 :     nOffsetEndPage = nOffsetStartPage + IO_CACHE_PAGE_SIZE;
    8266             : 
    8267        6651 :     if (nOffset + sizeofval > nOffsetEndPage)
    8268        1978 :         nOffsetEndPage += IO_CACHE_PAGE_SIZE;
    8269             : #undef IO_CACHE_PAGE_SIZE
    8270             : 
    8271        6651 :     nLastStripOffset = nBaseOffset + (uint64_t)arraySize * sizeofval;
    8272        6651 :     if (nLastStripOffset < nOffsetEndPage)
    8273        3603 :         nOffsetEndPage = nLastStripOffset;
    8274        6651 :     if (nOffsetStartPage >= nOffsetEndPage)
    8275             :     {
    8276           0 :         TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
    8277             :                       strile);
    8278           0 :         panVals[strile] = 0;
    8279           0 :         return 0;
    8280             :     }
    8281        6651 :     if (!SeekOK(tif, nOffsetStartPage))
    8282             :     {
    8283           0 :         panVals[strile] = 0;
    8284           0 :         return 0;
    8285             :     }
    8286             : 
    8287        6651 :     nToRead = (tmsize_t)(nOffsetEndPage - nOffsetStartPage);
    8288        6651 :     nRead = TIFFReadFile(tif, buffer, nToRead);
    8289        6652 :     if (nRead < nToRead)
    8290             :     {
    8291           3 :         TIFFErrorExtR(tif, module,
    8292             :                       "Cannot read offset/size for strile around ~%d", strile);
    8293           2 :         return 0;
    8294             :     }
    8295        6649 :     iStartBefore = -(int)((nOffset - nOffsetStartPage) / sizeofval);
    8296        6649 :     if (strile + iStartBefore < 0)
    8297        3630 :         iStartBefore = -strile;
    8298        6649 :     for (i = iStartBefore;
    8299    12686500 :          (uint32_t)(strile + i) < arraySize &&
    8300     6341640 :          _TIFFUnsanitizedAddUInt64AndInt(nOffset, (i + 1) * sizeofvalint) <=
    8301             :              nOffsetEndPage;
    8302     6338310 :          ++i)
    8303             :     {
    8304     6338530 :         if (dirent->tdir_type == TIFF_SHORT)
    8305             :         {
    8306             :             uint16_t val;
    8307     2133900 :             memcpy(&val,
    8308     2133900 :                    buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
    8309             :                    sizeof(val));
    8310     2133900 :             if (bSwab)
    8311        5220 :                 TIFFSwabShort(&val);
    8312     2133900 :             panVals[strile + i] = val;
    8313             :         }
    8314     4204630 :         else if (dirent->tdir_type == TIFF_LONG)
    8315             :         {
    8316             :             uint32_t val;
    8317     4203200 :             memcpy(&val,
    8318     4203200 :                    buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
    8319             :                    sizeof(val));
    8320     4203200 :             if (bSwab)
    8321        5528 :                 TIFFSwabLong(&val);
    8322     4202950 :             panVals[strile + i] = val;
    8323             :         }
    8324        1430 :         else if (dirent->tdir_type == TIFF_LONG8)
    8325             :         {
    8326             :             uint64_t val;
    8327        1458 :             memcpy(&val,
    8328        1458 :                    buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
    8329             :                    sizeof(val));
    8330        1458 :             if (bSwab)
    8331           0 :                 TIFFSwabLong8(&val);
    8332        1458 :             panVals[strile + i] = val;
    8333             :         }
    8334             :         else /* if( dirent->tdir_type == TIFF_SLONG8 ) */
    8335             :         {
    8336             :             /* Non conformant data type */
    8337             :             int64_t val;
    8338           0 :             memcpy(&val,
    8339           0 :                    buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
    8340             :                    sizeof(val));
    8341           0 :             if (bSwab)
    8342           0 :                 TIFFSwabLong8((uint64_t *)&val);
    8343           0 :             panVals[strile + i] = (uint64_t)val;
    8344             :         }
    8345             :     }
    8346        6321 :     return 1;
    8347             : }
    8348             : 
    8349     6395840 : static int _TIFFFetchStrileValue(TIFF *tif, uint32_t strile,
    8350             :                                  TIFFDirEntry *dirent, uint64_t **parray)
    8351             : {
    8352             :     static const char module[] = "_TIFFFetchStrileValue";
    8353     6395840 :     TIFFDirectory *td = &tif->tif_dir;
    8354     6395840 :     if (strile >= dirent->tdir_count)
    8355             :     {
    8356           0 :         return 0;
    8357             :     }
    8358     6395840 :     if (strile >= td->td_stripoffsetbyteallocsize)
    8359             :     {
    8360        1823 :         uint32_t nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;
    8361             :         uint32_t nStripArrayAllocNew;
    8362             :         uint64_t nArraySize64;
    8363             :         size_t nArraySize;
    8364             :         uint64_t *offsetArray;
    8365             :         uint64_t *bytecountArray;
    8366             : 
    8367        1823 :         if (strile > 1000000)
    8368             :         {
    8369           3 :             uint64_t filesize = TIFFGetFileSize(tif);
    8370             :             /* Avoid excessive memory allocation attempt */
    8371             :             /* For such a big blockid we need at least a TIFF_LONG per strile */
    8372             :             /* for the offset array. */
    8373           3 :             if (strile > filesize / sizeof(uint32_t))
    8374             :             {
    8375           2 :                 TIFFErrorExtR(tif, module, "File too short");
    8376           2 :                 return 0;
    8377             :             }
    8378             :         }
    8379             : 
    8380        1821 :         if (td->td_stripoffsetbyteallocsize == 0 &&
    8381        1820 :             td->td_nstrips < 1024 * 1024)
    8382             :         {
    8383        1819 :             nStripArrayAllocNew = td->td_nstrips;
    8384             :         }
    8385             :         else
    8386             :         {
    8387             : #define TIFF_MAX(a, b) (((a) > (b)) ? (a) : (b))
    8388             : #define TIFF_MIN(a, b) (((a) < (b)) ? (a) : (b))
    8389           2 :             nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U);
    8390           2 :             if (nStripArrayAllocNew < 0xFFFFFFFFU / 2)
    8391           2 :                 nStripArrayAllocNew *= 2;
    8392           2 :             nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
    8393             :         }
    8394        1821 :         assert(strile < nStripArrayAllocNew);
    8395        1821 :         nArraySize64 = (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew;
    8396        1821 :         nArraySize = (size_t)(nArraySize64);
    8397             : #if SIZEOF_SIZE_T == 4
    8398             :         if (nArraySize != nArraySize64)
    8399             :         {
    8400             :             TIFFErrorExtR(tif, module,
    8401             :                           "Cannot allocate strip offset and bytecount arrays");
    8402             :             return 0;
    8403             :         }
    8404             : #endif
    8405        1821 :         offsetArray = (uint64_t *)(_TIFFreallocExt(tif, td->td_stripoffset_p,
    8406             :                                                    (tmsize_t)nArraySize));
    8407        1821 :         bytecountArray = (uint64_t *)(_TIFFreallocExt(
    8408        1821 :             tif, td->td_stripbytecount_p, (tmsize_t)nArraySize));
    8409        1821 :         if (offsetArray)
    8410        1821 :             td->td_stripoffset_p = offsetArray;
    8411        1821 :         if (bytecountArray)
    8412        1822 :             td->td_stripbytecount_p = bytecountArray;
    8413        1821 :         if (offsetArray && bytecountArray)
    8414             :         {
    8415        1821 :             td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
    8416             :             /* Initialize new entries to ~0 / -1 */
    8417             :             /* coverity[overrun-buffer-arg] */
    8418        1821 :             memset(td->td_stripoffset_p + nStripArrayAllocBefore, 0xFF,
    8419        1821 :                    (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
    8420             :                        sizeof(uint64_t));
    8421             :             /* coverity[overrun-buffer-arg] */
    8422        1821 :             memset(td->td_stripbytecount_p + nStripArrayAllocBefore, 0xFF,
    8423        1821 :                    (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
    8424             :                        sizeof(uint64_t));
    8425             :         }
    8426             :         else
    8427             :         {
    8428           0 :             TIFFErrorExtR(tif, module,
    8429             :                           "Cannot allocate strip offset and bytecount arrays");
    8430           0 :             _TIFFfreeExt(tif, td->td_stripoffset_p);
    8431           0 :             td->td_stripoffset_p = NULL;
    8432           0 :             _TIFFfreeExt(tif, td->td_stripbytecount_p);
    8433           0 :             td->td_stripbytecount_p = NULL;
    8434           0 :             td->td_stripoffsetbyteallocsize = 0;
    8435             :         }
    8436             :     }
    8437     6395840 :     if (*parray == NULL || strile >= td->td_stripoffsetbyteallocsize)
    8438           0 :         return 0;
    8439             : 
    8440     6395840 :     if (~((*parray)[strile]) == 0)
    8441             :     {
    8442        6656 :         if (!_TIFFPartialReadStripArray(tif, dirent, (int)strile, *parray))
    8443             :         {
    8444           8 :             (*parray)[strile] = 0;
    8445           8 :             return 0;
    8446             :         }
    8447             :     }
    8448             : 
    8449     6395830 :     return 1;
    8450             : }
    8451             : 
    8452     6902400 : static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile,
    8453             :                                                      TIFFDirEntry *dirent,
    8454             :                                                      uint64_t **parray,
    8455             :                                                      int *pbErr)
    8456             : {
    8457     6902400 :     TIFFDirectory *td = &tif->tif_dir;
    8458     6902400 :     if (pbErr)
    8459     4328340 :         *pbErr = 0;
    8460             : 
    8461     6902400 :     if (strile >= td->td_nstrips)
    8462             :     {
    8463           0 :         if (pbErr)
    8464           0 :             *pbErr = 1;
    8465           0 :         return 0;
    8466             :     }
    8467             : 
    8468             :     /* Avoid the "dirent->tdir_count <= 4" code path for one of
    8469             :      * StripOffsets/StripByteCounts, and the other code path for the other one,
    8470             :      * which will lead to inconsistencies and potential out-of-bounds reads.
    8471             :      */
    8472     6902400 :     if ((td->td_stripoffset_entry.tdir_count <= 4) !=
    8473     6902400 :         (td->td_stripbytecount_entry.tdir_count <= 4))
    8474             :     {
    8475          12 :         TIFFErrorExtR(tif, "_TIFFGetStrileOffsetOrByteCountValue",
    8476             :                       "Inconsistent directory count between StripOffsets and "
    8477             :                       "StripByteCounts");
    8478          12 :         if (pbErr)
    8479           0 :             *pbErr = 1;
    8480          12 :         return 0;
    8481             :     }
    8482             : 
    8483     6902380 :     if ((tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
    8484     6732460 :         !(tif->tif_flags & TIFF_CHOPPEDUPARRAYS))
    8485             :     {
    8486     6725940 :         if (!(tif->tif_flags & TIFF_LAZYSTRILELOAD_ASKED) ||
    8487             :             /* If the values may fit in the toff_long/toff_long8 member */
    8488             :             /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */
    8489     6715570 :             dirent->tdir_count <= 4)
    8490             :         {
    8491      330099 :             if (!_TIFFFillStriles(tif))
    8492             :             {
    8493           2 :                 if (pbErr)
    8494           2 :                     *pbErr = 1;
    8495             :                 /* Do not return, as we want this function to always */
    8496             :                 /* return the same value if called several times with */
    8497             :                 /* the same arguments */
    8498             :             }
    8499             :         }
    8500             :         else
    8501             :         {
    8502     6395840 :             if (!_TIFFFetchStrileValue(tif, strile, dirent, parray))
    8503             :             {
    8504          10 :                 if (pbErr)
    8505           4 :                     *pbErr = 1;
    8506          10 :                 return 0;
    8507             :             }
    8508             :         }
    8509             :     }
    8510     6902370 :     if (*parray == NULL)
    8511             :     {
    8512           2 :         if (pbErr)
    8513           2 :             *pbErr = 1;
    8514           2 :         return 0;
    8515             :     }
    8516     6902370 :     return (*parray)[strile];
    8517             : }
    8518             : 
    8519             : /* Return the value of the TileOffsets/StripOffsets array for the specified
    8520             :  * tile/strile */
    8521     2189520 : uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile)
    8522             : {
    8523     2189520 :     return TIFFGetStrileOffsetWithErr(tif, strile, NULL);
    8524             : }
    8525             : 
    8526             : /* Return the value of the TileOffsets/StripOffsets array for the specified
    8527             :  * tile/strile */
    8528     4331800 : uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
    8529             : {
    8530     4331800 :     TIFFDirectory *td = &tif->tif_dir;
    8531     4331800 :     return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
    8532             :                                                 &(td->td_stripoffset_entry),
    8533             :                                                 &(td->td_stripoffset_p), pbErr);
    8534             : }
    8535             : 
    8536             : /* Return the value of the TileByteCounts/StripByteCounts array for the
    8537             :  * specified tile/strile */
    8538      384569 : uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
    8539             : {
    8540      384569 :     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
    8541             : }
    8542             : 
    8543             : /* Return the value of the TileByteCounts/StripByteCounts array for the
    8544             :  * specified tile/strile */
    8545     2570620 : uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
    8546             : {
    8547     2570620 :     TIFFDirectory *td = &tif->tif_dir;
    8548     2570620 :     return _TIFFGetStrileOffsetOrByteCountValue(
    8549             :         tif, strile, &(td->td_stripbytecount_entry), &(td->td_stripbytecount_p),
    8550             :         pbErr);
    8551             : }
    8552             : 
    8553      740428 : int _TIFFFillStriles(TIFF *tif) { return _TIFFFillStrilesInternal(tif, 1); }
    8554             : 
    8555      740465 : static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount)
    8556             : {
    8557      740465 :     TIFFDirectory *td = &tif->tif_dir;
    8558      740465 :     int return_value = 1;
    8559             : 
    8560             :     /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
    8561      740465 :     if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) ||
    8562      356771 :         (tif->tif_flags & TIFF_CHOPPEDUPARRAYS) != 0)
    8563      383735 :         return 1;
    8564             : 
    8565      356730 :     if ((tif->tif_flags & TIFF_LAZYSTRILELOAD_ASKED) &&
    8566      323267 :         !(tif->tif_flags & TIFF_LAZYSTRILELOAD_DONE))
    8567             :     {
    8568             :         /* In case of lazy loading, reload completely the arrays */
    8569       23833 :         _TIFFfreeExt(tif, td->td_stripoffset_p);
    8570       23813 :         _TIFFfreeExt(tif, td->td_stripbytecount_p);
    8571       23864 :         td->td_stripoffset_p = NULL;
    8572       23864 :         td->td_stripbytecount_p = NULL;
    8573       23864 :         td->td_stripoffsetbyteallocsize = 0;
    8574       23864 :         tif->tif_flags |= TIFF_LAZYSTRILELOAD_DONE;
    8575             :     }
    8576             : 
    8577             :     /* If stripoffset array is already loaded, exit with success */
    8578      356761 :     if (td->td_stripoffset_p != NULL)
    8579      331413 :         return 1;
    8580             : 
    8581             :     /* If tdir_count was canceled, then we already got there, but in error */
    8582       25348 :     if (td->td_stripoffset_entry.tdir_count == 0)
    8583         132 :         return 0;
    8584             : 
    8585       25216 :     if (!TIFFFetchStripThing(tif, &(td->td_stripoffset_entry), td->td_nstrips,
    8586             :                              &td->td_stripoffset_p))
    8587             :     {
    8588           1 :         return_value = 0;
    8589             :     }
    8590             : 
    8591       50423 :     if (loadStripByteCount &&
    8592       25193 :         !TIFFFetchStripThing(tif, &(td->td_stripbytecount_entry),
    8593             :                              td->td_nstrips, &td->td_stripbytecount_p))
    8594             :     {
    8595           1 :         return_value = 0;
    8596             :     }
    8597             : 
    8598       25230 :     _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
    8599       25238 :     _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
    8600             : 
    8601             : #ifdef STRIPBYTECOUNTSORTED_UNUSED
    8602             :     if (tif->tif_dir.td_nstrips > 1 && return_value == 1)
    8603             :     {
    8604             :         uint32_t strip;
    8605             : 
    8606             :         tif->tif_dir.td_stripbytecountsorted = 1;
    8607             :         for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
    8608             :         {
    8609             :             if (tif->tif_dir.td_stripoffset_p[strip - 1] >
    8610             :                 tif->tif_dir.td_stripoffset_p[strip])
    8611             :             {
    8612             :                 tif->tif_dir.td_stripbytecountsorted = 0;
    8613             :                 break;
    8614             :             }
    8615             :         }
    8616             :     }
    8617             : #endif
    8618             : 
    8619       25227 :     return return_value;
    8620             : }

Generated by: LCOV version 1.14