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

Generated by: LCOV version 1.14