Upgrade libtiff from 4.5.1 to 4.6.0

Change-Id: Icf1ec6c48ebe3726dd7c8b20d7b91cb8c73807d0
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/112491
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/third_party/libtiff/README.pdfium b/third_party/libtiff/README.pdfium
index 9eec6dc..fd575c7 100644
--- a/third_party/libtiff/README.pdfium
+++ b/third_party/libtiff/README.pdfium
@@ -1,7 +1,7 @@
 Name: LibTIFF
 URL: http://www.simplesystems.org/libtiff/
-Version: 4.5.1
-CPEPrefix: cpe:/a:libtiff:libtiff:4.5.1
+Version: 4.6.0
+CPEPrefix: cpe:/a:libtiff:libtiff:4.6.0
 Security Critical: yes
 Shipped: yes
 License: BSD
diff --git a/third_party/libtiff/tif_dirread.c b/third_party/libtiff/tif_dirread.c
index 838f64d..4a5015e 100644
--- a/third_party/libtiff/tif_dirread.c
+++ b/third_party/libtiff/tif_dirread.c
@@ -5046,7 +5046,7 @@
                                         uint16_t dircount)
 {
     static const char module[] = "TIFFReadDirectoryCheckOrder";
-    uint16_t m;
+    uint32_t m;
     uint16_t n;
     TIFFDirEntry *o;
     m = 0;
diff --git a/third_party/libtiff/tif_dirwrite.c b/third_party/libtiff/tif_dirwrite.c
index a6a485f..d8844bb 100644
--- a/third_party/libtiff/tif_dirwrite.c
+++ b/third_party/libtiff/tif_dirwrite.c
@@ -2073,34 +2073,41 @@
         (*ndir)++;
         return (1);
     }
+    /* TIFFTAG_TRANSFERFUNCTION expects (1 or 3) pointer to arrays with
+     *  (1 << BitsPerSample) * uint16_t values.
+     */
     m = (1 << tif->tif_dir.td_bitspersample);
-    n = tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples;
+    /* clang-format off */
+    n = (tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples) > 1 ? 3 : 1;
+    /* clang-format on */
+
+    /* Check for proper number of transferfunctions */
+    for (int i = 0; i < n; i++)
+    {
+        if (tif->tif_dir.td_transferfunction[i] == NULL)
+        {
+            TIFFWarningExtR(
+                tif, module,
+                "Too few TransferFunctions provided. Tag not written to file");
+            return (1); /* Not an error; only tag is not written. */
+        }
+    }
     /*
      * Check if the table can be written as a single column,
      * or if it must be written as 3 columns.  Note that we
      * write a 3-column tag if there are 2 samples/pixel and
      * a single column of data won't suffice--hmm.
      */
-    if (n > 3)
-        n = 3;
     if (n == 3)
     {
-        if (tif->tif_dir.td_transferfunction[2] == NULL ||
-            !_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],
+        if (!_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],
                          tif->tif_dir.td_transferfunction[2],
-                         m * sizeof(uint16_t)))
-            n = 2;
-    }
-    if (n == 2)
-    {
-        if (tif->tif_dir.td_transferfunction[1] == NULL ||
+                         m * sizeof(uint16_t)) &&
             !_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],
                          tif->tif_dir.td_transferfunction[1],
                          m * sizeof(uint16_t)))
             n = 1;
     }
-    if (n == 0)
-        n = 1;
     o = _TIFFmallocExt(tif, n * m * sizeof(uint16_t));
     if (o == NULL)
     {
diff --git a/third_party/libtiff/tiff.h b/third_party/libtiff/tiff.h
index b2d1186..d8da33d 100644
--- a/third_party/libtiff/tiff.h
+++ b/third_party/libtiff/tiff.h
@@ -646,7 +646,7 @@
 #define TIFFTAG_EP_EXPOSUREINDEX 37397            /* Exposure index */
 #define TIFFTAG_EP_STANDARDID 37398               /* TIFF/EP standard version, n.n.n.n */
 #define TIFFTAG_EP_SENSINGMETHOD 37399            /* Type of image sensor */
-/* 
+/*
  * TIFF/EP tags equivalent to EXIF tags
  *     Note that TIFF-EP and EXIF use nearly the same metadata tag set, but TIFF-EP stores the tags in IFD 0,
  *     while EXIF store the tags in a separate IFD. Either location is allowed by DNG, but the EXIF location is preferred.
@@ -761,6 +761,7 @@
 #define TIFFTAG_LERC_MAXZERROR 65567   /* LERC maximum error */
 #define TIFFTAG_WEBP_LEVEL 65568       /* WebP compression level */
 #define TIFFTAG_WEBP_LOSSLESS 65569    /* WebP lossless/lossy */
+#define TIFFTAG_WEBP_LOSSLESS_EXACT 65571  /* WebP lossless exact mode. Set-only mode. Default is 1. Can be set to 0 to increase compression rate, but R,G,B in areas where alpha = 0 will not be preserved */
 #define TIFFTAG_DEFLATE_SUBCODEC 65570 /* ZIP codec: to get/set the sub-codec to use. Will default to libdeflate when available */
 #define DEFLATE_SUBCODEC_ZLIB 0
 #define DEFLATE_SUBCODEC_LIBDEFLATE 1
diff --git a/third_party/libtiff/tiffio.h b/third_party/libtiff/tiffio.h
index d6bf0cc..2046054 100644
--- a/third_party/libtiff/tiffio.h
+++ b/third_party/libtiff/tiffio.h
@@ -277,7 +277,7 @@
 #define LOGLUV_PUBLIC 1
 #endif
 
-#if defined(__GNUC__) || defined(__attribute__)
+#if defined(__GNUC__) || defined(__clang__) || defined(__attribute__)
 #define TIFF_ATTRIBUTE(x) __attribute__(x)
 #else
 #define TIFF_ATTRIBUTE(x) /*nothing*/
diff --git a/third_party/libtiff/tiffvers.h b/third_party/libtiff/tiffvers.h
index ed84776..5741347 100644
--- a/third_party/libtiff/tiffvers.h
+++ b/third_party/libtiff/tiffvers.h
@@ -3,7 +3,7 @@
 /* clang-format disabled because FindTIFF.cmake is very sensitive to the
  * formatting of below line being a single line.
  */
-#define TIFFLIB_VERSION_STR "LIBTIFF, Version 4.5.1\nCopyright (c) 1988-1996 Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
+#define TIFFLIB_VERSION_STR "LIBTIFF, Version 4.6.0\nCopyright (c) 1988-1996 Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
 /*
  * This define can be used in code that requires
  * compilation-related definitions specific to a
@@ -11,12 +11,12 @@
  * version checking should be done based on the
  * string returned by TIFFGetVersion.
  */
-#define TIFFLIB_VERSION 20230609
+#define TIFFLIB_VERSION 20230908
 
 /* The following defines have been added in 4.5.0 */
 #define TIFFLIB_MAJOR_VERSION 4
-#define TIFFLIB_MINOR_VERSION 5
-#define TIFFLIB_MICRO_VERSION 1
+#define TIFFLIB_MINOR_VERSION 6
+#define TIFFLIB_MICRO_VERSION 0
 
 /* Macro added in 4.5.0. Returns TRUE if the current libtiff version is
  * greater or equal to major.minor.micro