libtiff: Merge branch 'WIN32' into 'master'

From 651b14ffa91722bd1815e367c8e7708925057430 Mon Sep 17 00:00:00 2001
From: Even Rouault <[EMAIL REDACTED]>
Date: Sun, 7 Jan 2024 23:14:24 +0100
Subject: [PATCH 1/5] Use #ifdef _WIN32 to test for Windows, and tiffio.h:
 remove definition of __WIN32__

---
 libtiff/tif_dirread.c |  8 ++++----
 libtiff/tif_jpeg.c    |  2 +-
 libtiff/tif_ojpeg.c   |  2 +-
 libtiff/tif_unix.c    |  2 +-
 libtiff/tiffio.h      | 14 +++++---------
 libtiff/tiffiop.h     |  2 +-
 test/raw_decode.c     |  2 +-
 tools/tiffset.c       |  2 +-
 8 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
index 242912f3..cea569d5 100644
--- a/libtiff/tif_dirread.c
+++ b/libtiff/tif_dirread.c
@@ -1016,7 +1016,7 @@ TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value)
             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
             if (err != TIFFReadDirEntryErrOk)
                 return (err);
-#if defined(__WIN32__) && (_MSC_VER < 1500)
+#if defined(_WIN32) && (_MSC_VER < 1500)
             /*
              * XXX: MSVC 6.0 does not support conversion
              * of 64-bit integers into floating point
@@ -1130,7 +1130,7 @@ TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
             if (err != TIFFReadDirEntryErrOk)
                 return (err);
-#if defined(__WIN32__) && (_MSC_VER < 1500)
+#if defined(_WIN32) && (_MSC_VER < 1500)
             /*
              * XXX: MSVC 6.0 does not support conversion
              * of 64-bit integers into floating point
@@ -2904,7 +2904,7 @@ TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value)
             {
                 if (tif->tif_flags & TIFF_SWAB)
                     TIFFSwabLong8(ma);
-#if defined(__WIN32__) && (_MSC_VER < 1500)
+#if defined(_WIN32) && (_MSC_VER < 1500)
                 /*
                  * XXX: MSVC 6.0 does not support
                  * conversion of 64-bit integers into
@@ -3149,7 +3149,7 @@ TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
             {
                 if (tif->tif_flags & TIFF_SWAB)
                     TIFFSwabLong8(ma);
-#if defined(__WIN32__) && (_MSC_VER < 1500)
+#if defined(_WIN32) && (_MSC_VER < 1500)
                 /*
                  * XXX: MSVC 6.0 does not support
                  * conversion of 64-bit integers into
diff --git a/libtiff/tif_jpeg.c b/libtiff/tif_jpeg.c
index bcab8dd0..da4b53f0 100644
--- a/libtiff/tif_jpeg.c
+++ b/libtiff/tif_jpeg.c
@@ -103,7 +103,7 @@ int TIFFJPEGIsFullStripRequired_12(TIFF *tif);
 */
 
 /* Define "boolean" as unsigned char, not int, per Windows custom. */
-#if defined(__WIN32__) && !defined(__MINGW32__)
+#if defined(_WIN32) && !defined(__MINGW32__)
 #ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
 typedef unsigned char boolean;
 #endif
diff --git a/libtiff/tif_ojpeg.c b/libtiff/tif_ojpeg.c
index ec0da992..57adcff8 100644
--- a/libtiff/tif_ojpeg.c
+++ b/libtiff/tif_ojpeg.c
@@ -224,7 +224,7 @@ static const TIFFField ojpegFields[] = {
 #endif
 
 /* Define "boolean" as unsigned char, not int, per Windows custom. */
-#if defined(__WIN32__) && !defined(__MINGW32__)
+#if defined(_WIN32) && !defined(__MINGW32__)
 #ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
 typedef unsigned char boolean;
 #endif
diff --git a/libtiff/tif_unix.c b/libtiff/tif_unix.c
index 2cfbbe8d..d9254d45 100644
--- a/libtiff/tif_unix.c
+++ b/libtiff/tif_unix.c
@@ -262,7 +262,7 @@ TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
     return tif;
 }
 
-#ifdef __WIN32__
+#ifdef _WIN32
 #include <windows.h>
 /*
  * Open a TIFF file with a Unicode filename, for read/writing.
diff --git a/libtiff/tiffio.h b/libtiff/tiffio.h
index 26e1d089..225f3c1b 100644
--- a/libtiff/tiffio.h
+++ b/libtiff/tiffio.h
@@ -77,10 +77,6 @@ typedef tstrile_t ttile_t;  /* tile number */
 typedef tmsize_t tsize_t;   /* i/o size in bytes */
 typedef void *tdata_t;      /* image data ref */
 
-#if !defined(__WIN32__) && (defined(_WIN32) || defined(WIN32))
-#define __WIN32__
-#endif
-
 /*
  * On windows you should define USE_WIN32_FILEIO if you are using tif_win32.c
  * or AVOID_WIN32_FILEIO if you are using something else (like tif_unix.c).
@@ -88,7 +84,7 @@ typedef void *tdata_t;      /* image data ref */
  * By default tif_unix.c is assumed.
  */
 
-#if defined(_WINDOWS) || defined(__WIN32__) || defined(_Windows)
+#if defined(_WIN32)
 #if !defined(__CYGWIN) && !defined(AVOID_WIN32_FILEIO) &&                      \
     !defined(USE_WIN32_FILEIO)
 #define AVOID_WIN32_FILEIO
@@ -98,11 +94,11 @@ typedef void *tdata_t;      /* image data ref */
 #if defined(USE_WIN32_FILEIO)
 #define VC_EXTRALEAN
 #include <windows.h>
-#ifdef __WIN32__
+#ifdef _WIN32
 DECLARE_HANDLE(thandle_t); /* Win32 file handle */
 #else
 typedef HFILE thandle_t; /* client data handle */
-#endif /* __WIN32__ */
+#endif /* _WIN32 */
 #else
 typedef void *thandle_t; /* client data handle */
 #endif /* USE_WIN32_FILEIO */
@@ -522,11 +518,11 @@ extern int TIFFReadRGBAImageOriented(TIFF *, uint32_t, uint32_t, uint32_t *,
 
     extern TIFF *TIFFOpen(const char *, const char *);
     extern TIFF *TIFFOpenExt(const char *, const char *, TIFFOpenOptions *opts);
-#ifdef __WIN32__
+#ifdef _WIN32
     extern TIFF *TIFFOpenW(const wchar_t *, const char *);
     extern TIFF *TIFFOpenWExt(const wchar_t *, const char *,
                               TIFFOpenOptions *opts);
-#endif /* __WIN32__ */
+#endif /* _WIN32 */
     extern TIFF *TIFFFdOpen(int, const char *, const char *);
     extern TIFF *TIFFFdOpenExt(int, const char *, const char *,
                                TIFFOpenOptions *opts);
diff --git a/libtiff/tiffiop.h b/libtiff/tiffiop.h
index da6a0f91..96e67435 100644
--- a/libtiff/tiffiop.h
+++ b/libtiff/tiffiop.h
@@ -334,7 +334,7 @@ struct TIFFOpenOptions
 #define ftell(stream, offset, whence) ftello(stream, offset, whence)
 #endif
 #endif
-#if defined(__WIN32__) && !(defined(_MSC_VER) && _MSC_VER < 1400) &&           \
+#if defined(_WIN32) && !(defined(_MSC_VER) && _MSC_VER < 1400) &&              \
     !(defined(__MSVCRT_VERSION__) && __MSVCRT_VERSION__ < 0x800)
 typedef unsigned int TIFFIOSize_t;
 #define _TIFF_lseek_f(fildes, offset, whence)                                  \
diff --git a/test/raw_decode.c b/test/raw_decode.c
index cf6ef00e..e09f2e17 100644
--- a/test/raw_decode.c
+++ b/test/raw_decode.c
@@ -64,7 +64,7 @@
 */
 
 /* Define "boolean" as unsigned char, not int, per Windows custom. */
-#if defined(__WIN32__) && !defined(__MINGW32__)
+#if defined(_WIN32) && !defined(__MINGW32__)
 #ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
 typedef unsigned char boolean;
 #endif
diff --git a/tools/tiffset.c b/tools/tiffset.c
index c7f9c5a2..c768d989 100644
--- a/tools/tiffset.c
+++ b/tools/tiffset.c
@@ -55,7 +55,7 @@
   does not distribute the CRT (it is supplied by Microsoft) so the correct CRT
   must be available on the target computer in order for the program to run.
 */
-#if defined(__WIN32__) && !(defined(_MSC_VER) && _MSC_VER < 1400) &&           \
+#if defined(_WIN32) && !(defined(_MSC_VER) && _MSC_VER < 1400) &&              \
     !(defined(__MSVCRT_VERSION__) && __MSVCRT_VERSION__ < 0x800)
 #define TIFFfseek(stream, offset, whence)                                      \
     _fseeki64(stream, /* __int64 */ offset, whence)

From 97e9225fa51a761e94a7493618a471647741a403 Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Sun, 7 Jan 2024 23:42:49 +0100
Subject: [PATCH 2/5] Remove support for _MSC_VER < 1500

---
 libtiff/tif_dirread.c | 36 ------------------------------------
 libtiff/tiffiop.h     |  2 +-
 2 files changed, 1 insertion(+), 37 deletions(-)

diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
index cea569d5..a6e65998 100644
--- a/libtiff/tif_dirread.c
+++ b/libtiff/tif_dirread.c
@@ -1016,16 +1016,7 @@ TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value)
             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
             if (err != TIFFReadDirEntryErrOk)
                 return (err);
-#if defined(_WIN32) && (_MSC_VER < 1500)
-            /*
-             * XXX: MSVC 6.0 does not support conversion
-             * of 64-bit integers into floating point
-             * values.
-             */
-            *value = _TIFFUInt64ToFloat(m);
-#else
             *value = (float)m;
-#endif
             return (TIFFReadDirEntryErrOk);
         }
         case TIFF_SLONG8:
@@ -1130,16 +1121,7 @@ TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
             if (err != TIFFReadDirEntryErrOk)
                 return (err);
-#if defined(_WIN32) && (_MSC_VER < 1500)
-            /*
-             * XXX: MSVC 6.0 does not support conversion
-             * of 64-bit integers into floating point
-             * values.
-             */
-            *value = _TIFFUInt64ToDouble(m);
-#else
             *value = (double)m;
-#endif
             return (TIFFReadDirEntryErrOk);
         }
         case TIFF_SLONG8:
@@ -2904,16 +2886,7 @@ TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value)
             {
                 if (tif->tif_flags & TIFF_SWAB)
                     TIFFSwabLong8(ma);
-#if defined(_WIN32) && (_MSC_VER < 1500)
-                /*
-                 * XXX: MSVC 6.0 does not support
-                 * conversion of 64-bit integers into
-                 * floating point values.
-                 */
-                *mb++ = _TIFFUInt64ToFloat(*ma++);
-#else
                 *mb++ = (float)(*ma++);
-#endif
             }
         }
         break;
@@ -3149,16 +3122,7 @@ TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
             {
                 if (tif->tif_flags & TIFF_SWAB)
                     TIFFSwabLong8(ma);
-#if defined(_WIN32) && (_MSC_VER < 1500)
-                /*
-                 * XXX: MSVC 6.0 does not support
-                 * conversion of 64-bit integers into
-                 * floating point values.
-                 */
-                *mb++ = _TIFFUInt64ToDouble(*ma++);
-#else
                 *mb++ = (double)(*ma++);
-#endif
             }
         }
         break;
diff --git a/libtiff/tiffiop.h b/libtiff/tiffiop.h
index 96e67435..7f25ae14 100644
--- a/libtiff/tiffiop.h
+++ b/libtiff/tiffiop.h
@@ -334,7 +334,7 @@ struct TIFFOpenOptions
 #define ftell(stream, offset, whence) ftello(stream, offset, whence)
 #endif
 #endif
-#if defined(_WIN32) && !(defined(_MSC_VER) && _MSC_VER < 1400) &&              \
+#if defined(_WIN32) &&                                                         \
     !(defined(__MSVCRT_VERSION__) && __MSVCRT_VERSION__ < 0x800)
 typedef unsigned int TIFFIOSize_t;
 #define _TIFF_lseek_f(fildes, offset, whence)                                  \

From 19af4e4ab5f7828583c44718fdbb1a13d119f543 Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Mon, 8 Jan 2024 12:20:39 +0100
Subject: [PATCH 3/5] Remove _TIFFUInt64ToFloat() and _TIFFUInt64ToDouble()

---
 libtiff/tif_aux.c | 47 -----------------------------------------------
 libtiff/tiffiop.h |  3 ---
 2 files changed, 50 deletions(-)

diff --git a/libtiff/tif_aux.c b/libtiff/tif_aux.c
index 49855bb0..e9606a42 100644
--- a/libtiff/tif_aux.c
+++ b/libtiff/tif_aux.c
@@ -385,53 +385,6 @@ int TIFFGetFieldDefaulted(TIFF *tif, uint32_t tag, ...)
     return (ok);
 }
 
-struct _Int64Parts
-{
-    int32_t low, high;
-};
-
-typedef union
-{
-    struct _Int64Parts part;
-    int64_t value;
-} _Int64;
-
-float _TIFFUInt64ToFloat(uint64_t ui64)
-{
-    _Int64 i;
-
-    i.value = ui64;
-    if (i.part.high >= 0)
-    {
-        return (float)i.value;
-    }
-    else
-    {
-        long double df;
-        df = (long double)i.value;
-        df += 18446744073709551616.0; /* adding 2**64 */
-        return (float)df;
-    }
-}
-
-double _TIFFUInt64ToDouble(uint64_t ui64)
-{
-    _Int64 i;
-
-    i.value = ui64;
-    if (i.part.high >= 0)
-    {
-        return (double)i.value;
-    }
-    else
-    {
-        long double df;
-        df = (long double)i.value;
-        df += 18446744073709551616.0; /* adding 2**64 */
-        return (double)df;
-    }
-}
-
 float _TIFFClampDoubleToFloat(double val)
 {
     if (val > FLT_MAX)
diff --git a/libtiff/tiffiop.h b/libtiff/tiffiop.h
index 7f25ae14..08ba0dfe 100644
--- a/libtiff/tiffiop.h
+++ b/libtiff/tiffiop.h
@@ -440,9 +440,6 @@ extern "C"
     extern void *_TIFFCheckRealloc(TIFF *, void *, tmsize_t, tmsize_t,
                                    const char *);
 
-    extern double _TIFFUInt64ToDouble(uint64_t);
-    extern float _TIFFUInt64ToFloat(uint64_t);
-
     extern float _TIFFClampDoubleToFloat(double);
     extern uint32_t _TIFFClampDoubleToUInt32(double);
 

From 10fe25bddf035a371e1b83cf7bb026128145d4de Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Sun, 7 Jan 2024 23:44:07 +0100
Subject: [PATCH 4/5] tif_jpeg.c/tif_ojpeg.c: remove likely ifdef tricks
 related to old compilers or unusual setups

---
 libtiff/tif_jpeg.c  | 37 -------------------------------------
 libtiff/tif_ojpeg.c | 24 ------------------------
 2 files changed, 61 deletions(-)

diff --git a/libtiff/tif_jpeg.c b/libtiff/tif_jpeg.c
index da4b53f0..39bd8130 100644
--- a/libtiff/tif_jpeg.c
+++ b/libtiff/tif_jpeg.c
@@ -73,43 +73,6 @@ int TIFFReInitJPEG_12(TIFF *tif, const JPEGOtherSettings *otherSettings,
                       int scheme, int is_encode);
 int TIFFJPEGIsFullStripRequired_12(TIFF *tif);
 
-/* We undefine FAR to avoid conflict with JPEG definition */
-
-#ifdef FAR
-#undef FAR
-#endif
-
-/*
-  Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
-  not defined.  Unfortunately, the MinGW and Borland compilers include
-  a typedef for INT32, which causes a conflict.  MSVC does not include
-  a conflicting typedef given the headers which are included.
-*/
-#if defined(__BORLANDC__) || defined(__MINGW32__)
-#define XMD_H 1
-#endif
-
-/*
-   The windows RPCNDR.H file defines boolean, but defines it with the
-   unsigned char size.  You should compile JPEG library using appropriate
-   definitions in jconfig.h header, but many users compile library in wrong
-   way. That causes errors of the following type:
-
-   "JPEGLib: JPEG parameter struct mismatch: library thinks size is 432,
-   caller expects 464"
-
-   For such users we will fix the problem here. See install.doc file from
-   the JPEG library distribution for details.
-*/
-
-/* Define "boolean" as unsigned char, not int, per Windows custom. */
-#if defined(_WIN32) && !defined(__MINGW32__)
-#ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
-typedef unsigned char boolean;
-#endif
-#define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
-#endif
-
 #include "jerror.h"
 #include "jpeglib.h"
 
diff --git a/libtiff/tif_ojpeg.c b/libtiff/tif_ojpeg.c
index 57adcff8..e4547cf0 100644
--- a/libtiff/tif_ojpeg.c
+++ b/libtiff/tif_ojpeg.c
@@ -207,30 +207,6 @@ static const TIFFField ojpegFields[] = {
 #include <setjmp.h>
 #endif
 
-/* We undefine FAR to avoid conflict with JPEG definition */
-
-#ifdef FAR
-#undef FAR
-#endif
-
-/*
-  Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
-  not defined.  Unfortunately, the MinGW and Borland compilers include
-  a typedef for INT32, which causes a conflict.  MSVC does not include
-  a conflicting typedef given the headers which are included.
-*/
-#if defined(__BORLANDC__) || defined(__MINGW32__)
-#define XMD_H 1
-#endif
-
-/* Define "boolean" as unsigned char, not int, per Windows custom. */
-#if defined(_WIN32) && !defined(__MINGW32__)
-#ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
-typedef unsigned char boolean;
-#endif
-#define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
-#endif
-
 #include "jerror.h"
 #include "jpeglib.h"
 

From f9649538122ef9881269db6a56af16e3e1d15308 Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Mon, 8 Jan 2024 13:49:02 +0100
Subject: [PATCH 5/5] test/raw_decode.c: remove likely ifdef tricks related to
 old compilers or unusual setups

---
 test/raw_decode.c | 30 ------------------------------
 1 file changed, 30 deletions(-)

diff --git a/test/raw_decode.c b/test/raw_decode.c
index e09f2e17..05952bef 100644
--- a/test/raw_decode.c
+++ b/test/raw_decode.c
@@ -40,36 +40,6 @@
 
 #include "tiffio.h"
 
-/*
-  Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
-  not defined.  Unfortunately, the MinGW and Borland compilers include
-  a typedef for INT32, which causes a conflict.  MSVC does not include
-  a conficting typedef given the headers which are included.
-*/
-#if defined(__BORLANDC__) || defined(__MINGW32__)
-#define XMD_H 1
-#endif
-
-/*
-   The windows RPCNDR.H file defines boolean, but defines it with the
-   unsigned char size.  You should compile JPEG library using appropriate
-   definitions in jconfig.h header, but many users compile library in wrong
-   way. That causes errors of the following type:
-
-   "JPEGLib: JPEG parameter struct mismatch: library thinks size is 432,
-   caller expects 464"
-
-   For such users we will fix the problem here. See install.doc file from
-   the JPEG library distribution for details.
-*/
-
-/* Define "boolean" as unsigned char, not int, per Windows custom. */
-#if defined(_WIN32) && !defined(__MINGW32__)
-#ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
-typedef unsigned char boolean;
-#endif
-#define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
-#endif
 #include "jpeglib.h" /* Needed for JPEG_LIB_VERSION */
 
 static unsigned char cluster_0[] = {0, 0, 2, 0, 138, 139};