libtiff: Merge branch '4_5_0rc2' into 'master'

From 35f53f7a0ba0eb04077536d85267245ba546f594 Mon Sep 17 00:00:00 2001
From: Even Rouault <[EMAIL REDACTED]>
Date: Tue, 13 Dec 2022 16:26:51 +0100
Subject: [PATCH 1/3] Prepare libtiff v4.5.0rc2

---
 ChangeLog          | 86 +++++++++++++++++++++++++++++++++++++++++++++-
 RELEASE-DATE       |  2 +-
 libtiff/tiffvers.h |  2 +-
 3 files changed, 87 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 52f78081..9ae250a8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,90 @@
+2022-12-14  Even Rouault  <even.rouault@spatialys.com>
+
+	libtiff v4.5.0rc2 preparation
+
+2022-12-13  Even Rouault  <even.rouault@spatialys.com>
+
+	Merge branch 'fix_502' into 'master'
+	Make TIFFSetDirectory(tiff, 65534) work again (fixes #502)
+
+	Closes #502
+
+	See merge request libtiff/libtiff!436
+
+2022-12-13  Even Rouault  <even.rouault@spatialys.com>
+
+	Add tests for IFD loop detection.
+
+	Fix IFD loop detection.
+
+	Use UINT_MAX.
+
+	Make TIFF_MAX_DIR_COUNT a autoconf/CMake setting.
+
+2022-12-13  Even Rouault  <even.rouault@spatialys.com>
+
+	Merge branch 'build-shared-by-default' into 'master'
+	Restore shared libraries by default
+
+	See merge request libtiff/libtiff!437
+
+2022-12-13  shaun walbridge  <shaun.walbridge@gmail.com>
+
+	CMake: restore shared libraries by default for top-level build.
+
+2022-12-12  Even Rouault  <even.rouault@spatialys.com>
+
+	Add a TIFF_MAX_DIR_COUNT public #define.
+
+	TIFFCurrentDirectory(), TIFFNumberOfDirectories(), TIFFSetDirectory(), TIFFUnlinkDirectory(): use tdir_t that is now a uint32_t, and raise limit of IFDs to 1048576
+
+	IFD loop checking: use hashmap to avoid quadratic performance.
+
+	Add a hashset/hashmap implementation (ported from GDAL's CPLHashSet)
+
+	Make TIFFSetDirectory(tiff, 65534) work again (fixes #502)
+
+2022-12-11  Even Rouault  <even.rouault@spatialys.com>
+
+	Merge branch 'do_not_format_tiffvers_h' into 'master'
+	Revert formatting of tiffvers.h and add TIFFLIB_MAJOR_VERSION, TIFFLIB_MINOR_VERSION, TIFFLIB_MICRO_VERSION defines
+
+	See merge request libtiff/libtiff!434
+
+2022-12-11  Even Rouault  <even.rouault@spatialys.com>
+
+	Merge branch 'tiffcrop_exclude_some_comment_from_clang-format' into 'master'
+	tiffcrop: Exclude some comments from clang-format
+
+	See merge request libtiff/libtiff!435
+
+2022-12-11  Su Laus  <sulau@freenet.de>
+
+	tiffcrop: Exclude some comments from clang-format.
+
+2022-12-11  Even Rouault  <even.rouault@spatialys.com>
+
+	tiffvers.h.in: add clang-format off/on.
+
+2022-12-10  Even Rouault  <even.rouault@spatialys.com>
+
+	tiffvers.h: add TIFFLIB_MAJOR_VERSION, TIFFLIB_MINOR_VERSION, TIFFLIB_MICRO_VERSION defines
+	Also add a TIFFLIB_AT_LEAST() macro
+
+	tiffvers.h: revert formatting.
+
+	Exclude reformatting of tiffvers.h which breaks version detection for FindTIFF.cmake
+
+2022-12-09  Even Rouault  <even.rouault@spatialys.com>
+
+	Merge branch 'release_4_5_0' into 'master'
+	Prepare v4.5.0 release
+
+	See merge request libtiff/libtiff!433
+
 2022-12-09  Even Rouault  <even.rouault@spatialys.com>
 
-	libtiff v4.5.0 released
+	libtiff v4.5.0rc1 preparation
 
 2022-12-08  Even Rouault  <even.rouault@spatialys.com>
 
diff --git a/RELEASE-DATE b/RELEASE-DATE
index 039ae27f..349ad3e1 100644
--- a/RELEASE-DATE
+++ b/RELEASE-DATE
@@ -1 +1 @@
-20221210
+20221213
diff --git a/libtiff/tiffvers.h b/libtiff/tiffvers.h
index c90b5c1c..552e8857 100644
--- a/libtiff/tiffvers.h
+++ b/libtiff/tiffvers.h
@@ -11,7 +11,7 @@
  * version checking should be done based on the
  * string returned by TIFFGetVersion.
  */
-#define TIFFLIB_VERSION 20221210
+#define TIFFLIB_VERSION 20221213
 
 /* The following defines have been added in 4.5.0 */
 #define TIFFLIB_MAJOR_VERSION 4

From 749edaed71dff51ce9987c6c879c51d6d709a466 Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Tue, 13 Dec 2022 16:32:52 +0100
Subject: [PATCH 2/3] _TIFFCheckDirNumberAndOffset: fix a previous commit

---
 libtiff/tif_dirread.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
index b9606f58..1be962ce 100644
--- a/libtiff/tif_dirread.c
+++ b/libtiff/tif_dirread.c
@@ -5398,8 +5398,8 @@ int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
         if (foundEntry->offset != diroff)
         {
             TIFFOffsetAndDirNumber entryOld;
-            entryOld.offset = diroff;
-            entryOld.dirNumber = foundEntry->offset;
+            entryOld.offset = foundEntry->offset;
+            entryOld.dirNumber = dirn;
             TIFFOffsetAndDirNumber *foundEntryOld =
                 (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
                     tif->tif_map_dir_offset_to_number, &entryOld);

From 6e83cb0ea9bed0ba4071f4b7df7cfa944cd2fb8d Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Tue, 13 Dec 2022 16:37:10 +0100
Subject: [PATCH 3/3] Update v4.5.0rst

---
 doc/releases/v4.5.0.rst | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/doc/releases/v4.5.0.rst b/doc/releases/v4.5.0.rst
index 7183f3cb..6a83070d 100644
--- a/doc/releases/v4.5.0.rst
+++ b/doc/releases/v4.5.0.rst
@@ -62,6 +62,16 @@ New/improved functionalities:
   to specify per-TIFF handle re-entrant error and warning callbacks
   with :c:func:`TIFFOpenOptionsSetErrorHandlerExtR` and
   :c:func:`TIFFOpenOptionsSetWarningHandlerExtR`
+* Related to IFD-Loop detection refactoring, the number of IFDs that libtiff
+  can browse through has been extended from 65535 to 1048576. This value is
+  a build-time setting that can be configured with CMake's TIFF_MAX_DIR_COUNT
+  variable or autoconf's --with-max-dir-count option.
+
+API/ABI breaks:
+
+* tdir_t type updated to uint32_t. This type is now used for the return value of
+  :c:func:`TIFFCurrentDirectory` and :c:func:`TIFFNumberOfDirectories`, and
+  as the argument of :c:func:`TIFFSetDirectory` and :c:func:`TIFFUnlinkDirectory`
 
 Bug fixes:
 
@@ -82,7 +92,7 @@ Bug fixes:
   :issue:`269`,:issue:`398`, CVE-2022-3599, :issue:`456`)
 * TIFFAdvanceDirectory(): fix unsigned-integer-overflow in mapped case
   (oss-fuzz #52309)
-* Improved/fixes IFD-Loop Handling (:issue:`455`)
+* Improved/fixes IFD-Loop Handling (:issue:`455`).
 * Update getimage to support large raster images.
 * Presetting of default tag values extended (e.g. PlanarConfig). (:issue:`449`)
 * Deal with RichTIFFIPTC tag written with LONG type (:issue:`225`)