libtiff: Make TIFF_MAX_DIR_COUNT a autoconf/CMake setting

From a1be870b1fb77d5e6120425947943334c1504264 Mon Sep 17 00:00:00 2001
From: Even Rouault <[EMAIL REDACTED]>
Date: Tue, 13 Dec 2022 13:12:49 +0100
Subject: [PATCH] Make TIFF_MAX_DIR_COUNT a autoconf/CMake setting

---
 cmake/LibraryFeatures.cmake   |  2 ++
 configure.ac                  | 14 ++++++++++++++
 libtiff/tif_config.h.cmake.in |  3 +++
 libtiff/tif_config.h.in       |  3 +++
 libtiff/tiffio.h              |  5 -----
 libtiff/tiffiop.h             |  4 ++++
 6 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/cmake/LibraryFeatures.cmake b/cmake/LibraryFeatures.cmake
index 929ed241..89635d64 100644
--- a/cmake/LibraryFeatures.cmake
+++ b/cmake/LibraryFeatures.cmake
@@ -35,6 +35,8 @@ if(strip-chopping)
     endif()
 endif()
 
+set(TIFF_MAX_DIR_COUNT 1048576 CACHE STRING "Maximum number of TIFF directories that libtiff can browse through")
+
 # Defer loading of strip/tile offsets
 option(defer-strile-load "enable deferred strip/tile offset/size loading (also available at runtime with the 'D' flag of TIFFOpen())" OFF)
 set(DEFER_STRILE_LOAD ${defer-strile-load})
diff --git a/configure.ac b/configure.ac
index 9833955c..292bb20e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1080,6 +1080,20 @@ if test "$HAVE_STRIPCHOP" = "yes" \
 
 fi
 
+dnl ---------------------------------------------------------------------------
+dnl Maximum number of TIFF directories that libtiff can browse through
+dnl ---------------------------------------------------------------------------
+
+AC_ARG_WITH(max-dir-count,
+    AS_HELP_STRING([--with-max-dir-count=SIZE],
+       [Maximum number of TIFF directories that libtiff can browse through [[default=1048576]]]),,)
+
+if test "x$with_max_dir_count" = "x" -o "x$with_max_dir_count" = "xyes"; then
+    with_max_dir_count="1048576"
+fi
+
+AC_DEFINE_UNQUOTED(TIFF_MAX_DIR_COUNT,$with_max_dir_count,[Maximum number of TIFF directories that libtiff can browse through])
+
 dnl ---------------------------------------------------------------------------
 dnl Should we try to defer loading of strip/tile offsets and sizes to 
 dnl optimize directory scanning?
diff --git a/libtiff/tif_config.h.cmake.in b/libtiff/tif_config.h.cmake.in
index 20e44630..570e4e1b 100644
--- a/libtiff/tif_config.h.cmake.in
+++ b/libtiff/tif_config.h.cmake.in
@@ -112,6 +112,9 @@
 /* Default size of the strip in bytes (when strip chopping enabled) */
 #define STRIP_SIZE_DEFAULT @STRIP_SIZE_DEFAULT@
 
+/** Maximum number of TIFF IFDs that libtiff can iterate through in a file. */
+#define TIFF_MAX_DIR_COUNT @TIFF_MAX_DIR_COUNT@
+
 /* define to use win32 IO system */
 #cmakedefine USE_WIN32_FILEIO 1
 
diff --git a/libtiff/tif_config.h.in b/libtiff/tif_config.h.in
index 66eef23b..8fb78c38 100644
--- a/libtiff/tif_config.h.in
+++ b/libtiff/tif_config.h.in
@@ -115,6 +115,9 @@
 /* Default size of the strip in bytes (when strip chopping enabled) */
 #undef STRIP_SIZE_DEFAULT
 
+/* Maximum number of TIFF IFDs that libtiff can iterate through in a file. */
+#undef TIFF_MAX_DIR_COUNT
+
 /* define to use win32 IO system */
 #undef USE_WIN32_FILEIO
 
diff --git a/libtiff/tiffio.h b/libtiff/tiffio.h
index c6cb7779..4df57665 100644
--- a/libtiff/tiffio.h
+++ b/libtiff/tiffio.h
@@ -277,11 +277,6 @@ typedef struct
 #define TIFF_ATTRIBUTE(x) /*nothing*/
 #endif
 
-/** Maximum number of TIFF IFDs that libtiff can iterate through in a file. */
-#ifndef TIFF_MAX_DIR_COUNT
-#define TIFF_MAX_DIR_COUNT 1048576
-#endif
-
 #if defined(c_plusplus) || defined(__cplusplus)
 extern "C"
 {
diff --git a/libtiff/tiffiop.h b/libtiff/tiffiop.h
index 1d4add0a..fe677071 100644
--- a/libtiff/tiffiop.h
+++ b/libtiff/tiffiop.h
@@ -55,6 +55,10 @@
 #define STRIP_SIZE_DEFAULT 8192
 #endif
 
+#ifndef TIFF_MAX_DIR_COUNT
+#define TIFF_MAX_DIR_COUNT 1048576
+#endif
+
 #define streq(a, b) (strcmp(a, b) == 0)
 #define strneq(a, b, n) (strncmp(a, b, n) == 0)