2022-12-23  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* contrib/iptcutil/iptcutil.c [_O_BINARY]:  Define _O_BINARY as O_BINARY,
	_fileno as _fileno and _setmode as setmode.
	[__DJGPP__]:  Include <sys/exceptn.h> for __djgpp_set_ctrl_c declaration
	and <unistd.h> for isatty declaration.
	Define SET_BINARY macro.  For POSIX like OS this is a no-op.  For Win32
	it calls setmode.
	(main):  For DJGPP, call SET_BINARY to reenable SIGINT and SIGQUIT
	signals if stdin and/or stdout is connected to console and has been
	switched to binary mode.

	* libtiff/tif_jpeg.c (JPEGCleanup, JPEGPrintDir):  Assertion did not
	hold true, resulting in the client application calling abort().

	* tools/fax2ps.c [_O_BINARY]:  Define _O_BINARY as O_BINARY, _fileno as
	_fileno and _setmode as setmode.
	[__DJGPP__]:  Define SET_BINARY to have interruptible terminal reads
	and writes; include <unistd.h> for isatty declaration and
	<sys/exceptn.h> for __djgpp_set_ctrl_c declaration.  For POSIX like OS
	this is a no-op.  For Win32 it calls setmode.
	(main):  Use SET_BINARY instead of setmode() to reenable SIGINT and
	SIGQUIT if stdin and/or stdout is connected to console and has been
	switched to binary mode.

	* tools/ppm2tiff.c [_O_BINARY]:  Define _O_BINARY as O_BINARY, _fileno as
	_fileno and _setmode as setmode.
	[__DJGPP__]: Define SET_BINARY to have interruptible terminale reads
	and writes; include <unistd.h> for isatty declaration and
	<sys/exceptn.h> for __djgpp_set_ctrl_c declaration.  For POSIX like OS
	this is a no-op.  For Win32 it calls setmode.
	(main):  Use SET_BINARY instead of setmode() to reenable SIGINT and
	SIGQUIT if stdin and/or stdout is connected to console and has been
	switched to binary mode.






diff -aprNU5 tiff-4.5.0.orig/contrib/iptcutil/iptcutil.c tiff-4.5.0/contrib/iptcutil/iptcutil.c
--- tiff-4.5.0.orig/contrib/iptcutil/iptcutil.c	2022-12-12 15:04:32 +0000
+++ tiff-4.5.0/contrib/iptcutil/iptcutil.c	2022-12-23 15:25:22 +0000
@@ -9,10 +9,50 @@
 #include <strings.h>
 #endif
 
 #ifdef HAVE_IO_H
 #include <io.h>
+# ifdef _O_BINARY
+#  define _O_BINARY       O_BINARY
+#  define _fileno(f)      fileno(f)
+#  define _setmode(f, m)  setmode(f, m)
+# endif /* __O_BINARY */
+
+# ifdef __DJGPP__
+#  if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
+#    define __gnuc_extension__  __extension__
+#  else
+#    define __gnuc_extension__
+#  endif
+
+#  include <unistd.h>       /* to declare isatty() */
+#  include <sys/exceptn.h>  /* to declare __djgpp_set_ctrl_c() */
+   /* This is DJGPP-specific.  By default, switching console
+      to binary mode disables SIGINT and SIGQUIT.  But it
+      is preferable to have terminal reads and writes to
+      be interruptible.  */
+#  define SET_BINARY(f)                                          \
+   (__gnuc_extension__                                           \
+     ({                                                          \
+        int file_descriptor = fileno(f);                         \
+        int previous_mode = setmode(file_descriptor, O_BINARY);  \
+        if (isatty(file_descriptor))                             \
+          __djgpp_set_ctrl_c(1);                                 \
+        previous_mode;                                           \
+     })                                                          \
+   )
+# endif /* __DJGPP__ */
+# ifdef WIN32
+#  define SET_BINARY(f)   (setmode(fileno(f), O_BINARY))
+# endif
+# ifdef __CYGWIN__
+#  define SET_BINARY(f)   /* */
+# endif
+#endif
+
+#ifndef SET_BINARY
+# define SET_BINARY(f)   /* */
 #endif
 
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
@@ -384,21 +424,17 @@ int main(int argc, char *argv[])
             c = argv[i][1];
             switch (c)
             {
                 case 't':
                     mode = 1;
-#ifdef _WIN32
                     /* Set "stdout" to binary mode: */
-                    _setmode(_fileno(ofile), _O_BINARY);
-#endif
+                    SET_BINARY(ofile);
                     break;
                 case 'b':
                     mode = 0;
-#ifdef _WIN32
                     /* Set "stdin" to binary mode: */
-                    _setmode(_fileno(ifile), _O_BINARY);
-#endif
+                    SET_BINARY(ifile);
                     break;
                 case 'i':
                     if (mode == 0)
                         ifile = fopen(argv[++i], "rb");
                     else
diff -aprNU5 tiff-4.5.0.orig/libtiff/tif_jpeg.c tiff-4.5.0/libtiff/tif_jpeg.c
--- tiff-4.5.0.orig/libtiff/tif_jpeg.c	2022-12-12 15:04:32 +0000
+++ tiff-4.5.0/libtiff/tif_jpeg.c	2022-12-23 15:30:06 +0000
@@ -2517,11 +2517,11 @@ static int JPEGPostEncode(TIFF *tif)
 
 static void JPEGCleanup(TIFF *tif)
 {
     JPEGState *sp = JState(tif);
 
-    assert(sp != 0);
+    /* assert(sp != 0); */
 
     tif->tif_tagmethods.vgetfield = sp->otherSettings.vgetparent;
     tif->tif_tagmethods.vsetfield = sp->otherSettings.vsetparent;
     tif->tif_tagmethods.printdir = sp->otherSettings.printdir;
     if (sp->cinfo_initialized)
@@ -2661,11 +2661,15 @@ static int JPEGVGetField(TIFF *tif, uint
 
 static void JPEGPrintDir(TIFF *tif, FILE *fd, long flags)
 {
     JPEGState *sp = JState(tif);
 
-    assert(sp != NULL);
+    /* assert(sp != NULL); */
+    if (sp == NULL) {
+        TIFFWarningExt(tif->tif_clientdata, "JPEGPrintDir", "Unknown JPEGState");
+        return;
+    }
     (void)flags;
 
     if (sp != NULL)
     {
         if (TIFFFieldSet(tif, FIELD_JPEGTABLES))
diff -aprNU5 tiff-4.5.0.orig/tools/fax2ps.c tiff-4.5.0/tools/fax2ps.c
--- tiff-4.5.0.orig/tools/fax2ps.c	2022-12-12 15:04:32 +0000
+++ tiff-4.5.0/tools/fax2ps.c	2022-12-23 15:30:06 +0000
@@ -38,10 +38,50 @@
 #include <fcntl.h>
 #endif
 
 #ifdef HAVE_IO_H
 #include <io.h>
+# ifdef _O_BINARY
+#  define _O_BINARY       O_BINARY
+#  define _fileno(f)      fileno(f)
+#  define _setmode(f, m)  setmode(f, m)
+# endif /* __O_BINARY */
+
+# ifdef __DJGPP__
+#  if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
+#    define __gnuc_extension__  __extension__
+#  else
+#    define __gnuc_extension__
+#  endif
+
+#  include <unistd.h>       /* to declare isatty() */
+#  include <sys/exceptn.h>  /* to declare __djgpp_set_ctrl_c() */
+   /* This is DJGPP-specific.  By default, switching console
+      to binary mode disables SIGINT and SIGQUIT.  But it
+      is preferable to have terminal reads and writes to
+      be interruptible.  */
+#  define SET_BINARY(f)                                          \
+   (__gnuc_extension__                                           \
+     ({                                                          \
+        int file_descriptor = fileno(f);                         \
+        int previous_mode = setmode(file_descriptor, O_BINARY);  \
+        if (isatty(file_descriptor))                             \
+          __djgpp_set_ctrl_c(1);                                 \
+        previous_mode;                                           \
+     })                                                          \
+   )
+# endif /* __DJGPP__ */
+# ifdef WIN32
+#  define SET_BINARY(f)   (setmode(fileno(f), O_BINARY))
+# endif
+# ifdef __CYGWIN__
+#  define SET_BINARY(f)   /* */
+# endif
+#endif
+
+#ifndef SET_BINARY
+# define SET_BINARY(f)   /* */
 #endif
 
 #include "tiffio.h"
 #include "tiffiop.h"
 
@@ -422,13 +462,11 @@ int main(int argc, char **argv)
         if (fd == NULL)
         {
             fprintf(stderr, "Could not obtain temporary file.\n");
             exit(EXIT_FAILURE);
         }
-#if defined(HAVE_SETMODE) && defined(O_BINARY)
-        setmode(fileno(stdin), O_BINARY);
-#endif
+        SET_BINARY(stdin);
         while ((n = read(fileno(stdin), buf, sizeof(buf))) > 0)
         {
             if (write(fileno(fd), buf, n) != n)
             {
                 fclose(fd);
diff -aprNU5 tiff-4.5.0.orig/tools/ppm2tiff.c tiff-4.5.0/tools/ppm2tiff.c
--- tiff-4.5.0.orig/tools/ppm2tiff.c	2022-12-12 15:04:32 +0000
+++ tiff-4.5.0/tools/ppm2tiff.c	2022-12-23 15:30:06 +0000
@@ -38,10 +38,50 @@
 #include <fcntl.h>
 #endif
 
 #ifdef HAVE_IO_H
 #include <io.h>
+# ifdef _O_BINARY
+#  define _O_BINARY       O_BINARY
+#  define _fileno(f)      fileno(f)
+#  define _setmode(f, m)  setmode(f, m)
+# endif /* __O_BINARY */
+
+# ifdef __DJGPP__
+#  if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
+#    define __gnuc_extension__  __extension__
+#  else
+#    define __gnuc_extension__
+#  endif
+
+#  include <unistd.h>       /* to declare isatty() */
+#  include <sys/exceptn.h>  /* to declare __djgpp_set_ctrl_c() */
+   /* This is DJGPP-specific.  By default, switching console
+      to binary mode disables SIGINT and SIGQUIT.  But it
+      is preferable to have terminal reads and writes to
+      be interruptible.  */
+#  define SET_BINARY(f)                                          \
+   (__gnuc_extension__                                           \
+     ({                                                          \
+        int file_descriptor = fileno(f);                         \
+        int previous_mode = setmode(file_descriptor, O_BINARY);  \
+        if (isatty(file_descriptor))                             \
+          __djgpp_set_ctrl_c(1);                                 \
+        previous_mode;                                           \
+     })                                                          \
+   )
+# endif /* __DJGPP__ */
+# ifdef WIN32
+#  define SET_BINARY(f)   (setmode(fileno(f), O_BINARY))
+# endif
+# ifdef __CYGWIN__
+#  define SET_BINARY(f)   /* */
+# endif
+#endif
+
+#ifndef SET_BINARY
+# define SET_BINARY(f)   /* */
 #endif
 
 #include "tiffio.h"
 
 #ifndef EXIT_SUCCESS
@@ -243,13 +283,11 @@ int main(int argc, char *argv[])
     }
     else
     {
         infile = "<stdin>";
         in = stdin;
-#if defined(HAVE_SETMODE) && defined(O_BINARY)
-        setmode(fileno(stdin), O_BINARY);
-#endif
+        SET_BINARY(stdin);
     }
 
     if (fgetc(in) != 'P')
         BadPPM(infile);
     switch (fgetc(in))




2022-12-24  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	Applied all patches taken from tiff_4.4.0-6.debian.tar.xz available at
	https://packages.debian.org/unstable/libs/libtiff5/.
	See the ChangeLog file for changes.









diff -aprNU5 tiff-4.5.0.orig/libtiff/tif_lzw.c tiff-4.5.0/libtiff/tif_lzw.c
--- tiff-4.5.0.orig/libtiff/tif_lzw.c	2022-12-12 15:04:32 +0000
+++ tiff-4.5.0/libtiff/tif_lzw.c	2022-12-24 23:45:28 +0000
@@ -345,12 +345,21 @@ static int LZWPreDecode(TIFF *tif, uint1
                (((uint64_t)bp[4]) << 24) | (((uint64_t)bp[5]) << 16) |         \
                (((uint64_t)bp[6]) << 8) | (((uint64_t)bp[7]))
 #endif
 #elif SIZEOF_WORDTYPE == 4
 #if defined(__GNUC__) && defined(__i386__)
+#if defined(__DJGPP__) && (__GNUC__ - 3) == 0 && (__GNUC_MINOR__ - 4) == 0
+/* gcc 3.4.6 seems not to provide __builtin_bswap32.  */
+#define __bswap_constant_32(x)                                                 \
+  ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8)                    \
+   | (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
+#define GetNextData(nextdata, bp)                                              \
+    nextdata = __bswap_constant_32(*(uint32_t *)(bp))
+#else
 #define GetNextData(nextdata, bp)                                              \
     nextdata = __builtin_bswap32(*(uint32_t *)(bp))
+#endif
 #elif defined(_M_X86)
 #define GetNextData(nextdata, bp)                                              \
     nextdata = _byteswap_ulong(*(unsigned long *)(bp))
 #elif defined(__GNUC__)
 #define GetNextData(nextdata, bp)                                              \




2022-12-25  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	Applied all patches taken from tiff_4.5.0-1.debian.tar.xz available at
	https://ftp.de.debian.org/debian/pool/main/t/tiff/.
	See the ChangeLog file for changes.









diff -aprNU5 tiff-4.5.0.orig/libtiff/tif_dir.c tiff-4.5.0/libtiff/tif_dir.c
--- tiff-4.5.0.orig/libtiff/tif_dir.c	2022-12-13 22:28:16 +0000
+++ tiff-4.5.0/libtiff/tif_dir.c	2022-12-25 02:21:40 +0000
@@ -2039,14 +2039,18 @@ int TIFFSetDirectory(TIFF *tif, tdir_t d
         return (0);
 
     tif->tif_nextdiroff = nextdiroff;
     /*
      * Set curdir to the actual directory index.  The
-     * -1 is because TIFFReadDirectory will increment
+     * -1 decrement is because TIFFReadDirectory will increment
      * tif_curdir after successfully reading the directory.
      */
-    tif->tif_curdir = (dirn - n) - 1;
+    tif->tif_curdir = (dirn - n);
+    if (tif->tif_curdir == 0)
+        tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
+    else
+        tif->tif_curdir--;
     return (TIFFReadDirectory(tif));
 }
 
 /*
  * Set the current directory to be the directory
diff -aprNU5 tiff-4.5.0.orig/libtiff/tif_dirwrite.c tiff-4.5.0/libtiff/tif_dirwrite.c
--- tiff-4.5.0.orig/libtiff/tif_dirwrite.c	2022-12-12 15:04:32 +0000
+++ tiff-4.5.0/libtiff/tif_dirwrite.c	2022-12-25 02:21:40 +0000
@@ -1112,11 +1112,16 @@ static int TIFFWriteDirectorySec(TIFF *t
             goto bad;
         }
         if (tif->tif_dataoff & 1)
             tif->tif_dataoff++;
         if (isimage)
-            tif->tif_curdir++;
+        {
+            if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER)
+                tif->tif_curdir = 0;
+            else
+                tif->tif_curdir++;
+        }
     }
     if (isimage)
     {
         if (TIFFFieldSet(tif, FIELD_SUBIFD) && (tif->tif_subifdoff == 0))
         {
diff -aprNU5 tiff-4.5.0.orig/libtiff/tif_hash_set.c tiff-4.5.0/libtiff/tif_hash_set.c
--- tiff-4.5.0.orig/libtiff/tif_hash_set.c	2022-12-13 19:05:40 +0000
+++ tiff-4.5.0/libtiff/tif_hash_set.c	2022-12-25 02:21:40 +0000
@@ -25,10 +25,11 @@
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
  ****************************************************************************/
 
 #include "tif_hash_set.h"
+#include "tif_config.h"
 
 #include <assert.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
