libtiff: Merge branch 'fix_558' into 'master'

From 6fd4237b0b8f03eaa51f28c9efcf89600d005bf7 Mon Sep 17 00:00:00 2001
From: Even Rouault <[EMAIL REDACTED]>
Date: Mon, 8 May 2023 23:17:57 +0200
Subject: [PATCH 1/2] tif_lerc.c: use WORDS_BIGENDIAN instead of HOST_BIGENDIAN

---
 libtiff/tif_lerc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libtiff/tif_lerc.c b/libtiff/tif_lerc.c
index 99de713c..4f357a60 100644
--- a/libtiff/tif_lerc.c
+++ b/libtiff/tif_lerc.c
@@ -517,7 +517,7 @@ static int LERCPreDecode(TIFF *tif, uint16_t s)
     {
         const unsigned nb_pixels = sp->segment_width * sp->segment_height;
         unsigned i;
-#if HOST_BIGENDIAN
+#if WORDS_BIGENDIAN
         const unsigned char nan_bytes[] = {0x7f, 0xc0, 0, 0};
 #else
         const unsigned char nan_bytes[] = {0, 0, 0xc0, 0x7f};

From 0dabdfe85732147d1c8d6016e8c3e40522104568 Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Mon, 8 May 2023 23:17:15 +0200
Subject: [PATCH 2/2] Hardcode HOST_FILLORDER to FILLORDER_LSB2MSB, and make
 'H' flag of TIFFOpen() to warn and an alias of FILLORDER_MSB2LSB

---
 cmake/ProcessorChecks.cmake | 11 -----------
 configure.ac                | 16 ----------------
 doc/internals.rst           |  8 +++-----
 libtiff/tif_open.c          |  6 +++++-
 libtiff/tiffconf.h.cmake.in | 13 +++++++++++--
 libtiff/tiffconf.h.in       | 13 +++++++++++--
 6 files changed, 30 insertions(+), 37 deletions(-)

diff --git a/cmake/ProcessorChecks.cmake b/cmake/ProcessorChecks.cmake
index 8d8c568a..08e89604 100644
--- a/cmake/ProcessorChecks.cmake
+++ b/cmake/ProcessorChecks.cmake
@@ -26,17 +26,6 @@
 
 include(TestBigEndian)
 
-
-# CPU bit order
-set(HOST_FILLORDER FILLORDER_MSB2LSB)
-if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "i.*86.*" OR
-        CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "amd64.*" OR
-        # AMD64 on Windows
-        CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "AMD64" OR
-        CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64.*")
-    set(HOST_FILLORDER FILLORDER_LSB2MSB)
-endif()
-
 # CPU endianness
 test_big_endian(HOST_BIG_ENDIAN)
 if(HOST_BIG_ENDIAN)
diff --git a/configure.ac b/configure.ac
index 292bb20e..8d1a52d1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -254,22 +254,6 @@ AC_CHECK_FUNCS([mmap setmode])
 dnl Will use local replacements for unavailable functions
 AC_REPLACE_FUNCS(getopt)
 
-dnl ---------------------------------------------------------------------------
-dnl Check the native cpu bit order.
-dnl ---------------------------------------------------------------------------
-AC_MSG_CHECKING([native cpu bit order])
-case "$host_cpu" in
-    i*86*|x86_64*)
-        HOST_FILLORDER=FILLORDER_LSB2MSB
-	AC_MSG_RESULT([lsb2msb])
-	;;
-    *)
-	HOST_FILLORDER=FILLORDER_MSB2LSB
-	AC_MSG_RESULT([msb2lsb])
-        ;;
-esac
-AC_DEFINE_UNQUOTED(HOST_FILLORDER, $HOST_FILLORDER, [Set the native cpu bit order (FILLORDER_LSB2MSB or FILLORDER_MSB2LSB)])
-
 dnl ---------------------------------------------------------------------------
 dnl Configure legacy tifconf.h HOST_BIGENDIAN.
 dnl ---------------------------------------------------------------------------
diff --git a/doc/internals.rst b/doc/internals.rst
index 86014d9f..7ea1b5e5 100644
--- a/doc/internals.rst
+++ b/doc/internals.rst
@@ -189,11 +189,9 @@ as possible in two files: :file:`tiffcomp.h` and
 operating system-specific routines to do I/O and I/O-related operations.
 The UNIX (:file:`tif_unix.c`) code has had the most use.
 
-Native CPU byte order is determined on the fly by
-the library and does not need to be specified.
-The :c:macro:`HOST_FILLORDER` and :c:macro:`HOST_BIGENDIAN`
-definitions are not currently used, but may be employed by
-codecs for optimization purposes.
+Native CPU byte order is determined through the :c:macro:`WORDS_BIGENDIAN`
+definition, which must be set to the appropriate value in :file:`tif_config.h`
+by the build system.
 
 The following defines control general portability:
 
diff --git a/libtiff/tif_open.c b/libtiff/tif_open.c
index aa16a64f..23fcf81c 100644
--- a/libtiff/tif_open.c
+++ b/libtiff/tif_open.c
@@ -365,8 +365,12 @@ TIFF *TIFFClientOpenExt(const char *name, const char *mode,
                     (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_LSB2MSB;
                 break;
             case 'H':
+                TIFFWarningExtR(tif, name,
+                                "H(ost) mode is deprecated. Since "
+                                "libtiff 4.5.1, it is an alias of 'B' / "
+                                "FILLORDER_MSB2LSB.");
                 tif->tif_flags =
-                    (tif->tif_flags & ~TIFF_FILLORDER) | HOST_FILLORDER;
+                    (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
                 break;
             case 'M':
                 if (m == O_RDONLY)
diff --git a/libtiff/tiffconf.h.cmake.in b/libtiff/tiffconf.h.cmake.in
index e3a830d4..306874f5 100644
--- a/libtiff/tiffconf.h.cmake.in
+++ b/libtiff/tiffconf.h.cmake.in
@@ -52,8 +52,17 @@
    machine */
 #cmakedefine HAVE_IEEEFP 1
 
-/* Set the native cpu bit order (FILLORDER_LSB2MSB or FILLORDER_MSB2LSB) */
-#define HOST_FILLORDER @HOST_FILLORDER@
+/* The concept of HOST_FILLORDER is broken. Since libtiff 4.5.1
+ * this macro will always be hardcoded to FILLORDER_LSB2MSB on all
+ * architectures, to reflect past long behavior of doing so on x86 architecture.
+ * Note however that the default FillOrder used by libtiff is FILLORDER_MSB2LSB,
+ * as mandated per the TIFF specification.
+ * The influence of HOST_FILLORDER is only when passing the 'H' mode in
+ * TIFFOpen().
+ * You should NOT rely on this macro to decide the CPU endianness!
+ * This macro will be removed in libtiff 4.6
+ */
+#define HOST_FILLORDER FILLORDER_LSB2MSB
 
 /* Native cpu byte order: 1 if big-endian (Motorola) or 0 if little-endian
    (Intel) */
diff --git a/libtiff/tiffconf.h.in b/libtiff/tiffconf.h.in
index 2a8b03e9..ab017357 100644
--- a/libtiff/tiffconf.h.in
+++ b/libtiff/tiffconf.h.in
@@ -52,8 +52,17 @@
    machine */
 #undef HAVE_IEEEFP
 
-/* Set the native cpu bit order (FILLORDER_LSB2MSB or FILLORDER_MSB2LSB) */
-#undef HOST_FILLORDER
+/* The concept of HOST_FILLORDER is broken. Since libtiff 4.5.1
+ * this macro will always be hardcoded to FILLORDER_LSB2MSB on all
+ * architectures, to reflect past long behavior of doing so on x86 architecture.
+ * Note however that the default FillOrder used by libtiff is FILLORDER_MSB2LSB,
+ * as mandated per the TIFF specification.
+ * The influence of HOST_FILLORDER is only when passing the 'H' mode in
+ * TIFFOpen().
+ * You should NOT rely on this macro to decide the CPU endianness!
+ * This macro will be removed in libtiff 4.6
+ */
+#define HOST_FILLORDER FILLORDER_LSB2MSB
 
 /* Native cpu byte order: 1 if big-endian (Motorola) or 0 if little-endian
    (Intel) */