2017-12-25  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/tiffiop.h [HAVE_SEARCH_H]:  New guard HAVE_LFIND added.
	Do not assume that <search.h> provides lfind().

	* 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.0.9.orig/contrib/iptcutil/iptcutil.c tiff-4.0.9/contrib/iptcutil/iptcutil.c
--- tiff-4.0.9.orig/contrib/iptcutil/iptcutil.c	2015-06-08 15:01:52 +0000
+++ tiff-4.0.9/contrib/iptcutil/iptcutil.c	2017-12-25 13:23:50 +0000
@@ -11,10 +11,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
@@ -414,21 +454,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.0.9.orig/libtiff/tiffiop.h tiff-4.0.9/libtiff/tiffiop.h
--- tiff-4.0.9.orig/libtiff/tiffiop.h	2017-09-04 09:00:40 +0000
+++ tiff-4.0.9/libtiff/tiffiop.h	2017-12-25 13:23:50 +0000
@@ -48,11 +48,11 @@
 # include <assert.h>
 #else
 # define assert(x) 
 #endif
 
-#ifdef HAVE_SEARCH_H
+#if defined HAVE_SEARCH_H && defined HAVE_LFIND
 # include <search.h>
 #else
 extern void *lfind(const void *, const void *, size_t *, size_t,
 		   int (*)(const void *, const void *));
 #endif
diff -aprNU5 tiff-4.0.9.orig/libtiff/tif_jpeg.c tiff-4.0.9/libtiff/tif_jpeg.c
--- tiff-4.0.9.orig/libtiff/tif_jpeg.c	2017-10-17 07:16:44 +0000
+++ tiff-4.0.9/libtiff/tif_jpeg.c	2017-12-25 13:23:50 +0000
@@ -2232,11 +2232,11 @@ JPEGPostEncode(TIFF* tif)
 static void
 JPEGCleanup(TIFF* tif)
 {
 	JPEGState *sp = JState(tif);
 	
-	assert(sp != 0);
+	/* assert(sp != 0); */
 
 	tif->tif_tagmethods.vgetfield = sp->vgetparent;
 	tif->tif_tagmethods.vsetfield = sp->vsetparent;
 	tif->tif_tagmethods.printdir = sp->printdir;
         if( sp->cinfo_initialized )
@@ -2369,11 +2369,15 @@ JPEGVGetField(TIFF* tif, uint32 tag, va_
 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))
 			fprintf(fd, "  JPEG Tables: (%lu bytes)\n",
diff -aprNU5 tiff-4.0.9.orig/tools/fax2ps.c tiff-4.0.9/tools/fax2ps.c
--- tiff-4.0.9.orig/tools/fax2ps.c	2015-08-25 08:17:10 +0000
+++ tiff-4.0.9/tools/fax2ps.c	2017-12-25 13:23:50 +0000
@@ -39,10 +39,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
 
 #ifdef NEED_LIBPORT
 # include "libport.h"
 #endif
@@ -393,13 +433,11 @@ main(int argc, char** argv)
 	fd = tmpfile();
 	if (fd == NULL) {
 	    fprintf(stderr, "Could not obtain temporary file.\n");
 	    exit(-2);
 	}
-#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);
                         fprintf(stderr,
                                 "Could not copy stdin to temporary file.\n");
diff -aprNU5 tiff-4.0.9.orig/tools/ppm2tiff.c tiff-4.0.9/tools/ppm2tiff.c
--- tiff-4.0.9.orig/tools/ppm2tiff.c	2015-08-16 12:09:52 +0000
+++ tiff-4.0.9/tools/ppm2tiff.c	2017-12-25 13:23:50 +0000
@@ -39,10 +39,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
 
 #ifdef NEED_LIBPORT
 # include "libport.h"
 #endif
@@ -142,13 +182,11 @@ main(int argc, char* argv[])
 			return (-1);
 		}
 	} 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)) {




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

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







diff -aprNU5 tiff-4.0.9.orig/libtiff/tif_dir.c tiff-4.0.9/libtiff/tif_dir.c
--- tiff-4.0.9.orig/libtiff/tif_dir.c	2017-07-05 03:24:20 +0000
+++ tiff-4.0.9/libtiff/tif_dir.c	2017-12-25 13:33:42 +0000
@@ -1065,10 +1065,13 @@ _TIFFVGetField(TIFF* tif, uint32 tag, va
 		case TIFFTAG_TRANSFERFUNCTION:
 			*va_arg(ap, uint16**) = td->td_transferfunction[0];
 			if (td->td_samplesperpixel - td->td_extrasamples > 1) {
 				*va_arg(ap, uint16**) = td->td_transferfunction[1];
 				*va_arg(ap, uint16**) = td->td_transferfunction[2];
+			} else {
+				*va_arg(ap, uint16**) = NULL;
+				*va_arg(ap, uint16**) = NULL;
 			}
 			break;
 		case TIFFTAG_REFERENCEBLACKWHITE:
 			*va_arg(ap, float**) = td->td_refblackwhite;
 			break;
diff -aprNU5 tiff-4.0.9.orig/tools/tiff2pdf.c tiff-4.0.9/tools/tiff2pdf.c
--- tiff-4.0.9.orig/tools/tiff2pdf.c	2017-10-17 08:43:24 +0000
+++ tiff-4.0.9/tools/tiff2pdf.c	2017-12-25 13:33:42 +0000
@@ -235,11 +235,11 @@ typedef struct {
 	uint32 pdf_ojpegiflength;
 #endif
 	float tiff_whitechromaticities[2];
 	float tiff_primarychromaticities[6];
 	float tiff_referenceblackwhite[2];
-	float* tiff_transferfunction[3];
+	uint16* tiff_transferfunction[3];
 	int pdf_image_interpolate;	/* 0 (default) : do not interpolate,
 					   1 : interpolate */
 	uint16 tiff_transferfunctioncount;
 	uint32 pdf_icccs;
 	uint32 tiff_iccprofilelength;
@@ -1045,10 +1045,12 @@ void t2p_read_tiff_init(T2P* t2p, TIFF*
 	tdir_t directorycount=0;
 	tdir_t i=0;
 	uint16 pagen=0;
 	uint16 paged=0;
 	uint16 xuint16=0;
+	uint16 tiff_transferfunctioncount=0;
+	uint16* tiff_transferfunction[3];
 
 	directorycount=TIFFNumberOfDirectories(input);
 	t2p->tiff_pages = (T2P_PAGE*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,directorycount,sizeof(T2P_PAGE)));
 	if(t2p->tiff_pages==NULL){
 		TIFFError(
@@ -1145,30 +1147,52 @@ void t2p_read_tiff_init(T2P* t2p, TIFF*
                                 if(t2p->pdf_minorversion<2){t2p->pdf_minorversion=2;}
                         }
                 }
 #endif
 		if (TIFFGetField(input, TIFFTAG_TRANSFERFUNCTION,
-                                 &(t2p->tiff_transferfunction[0]),
-                                 &(t2p->tiff_transferfunction[1]),
-                                 &(t2p->tiff_transferfunction[2]))) {
-			if((t2p->tiff_transferfunction[1] != (float*) NULL) &&
-                           (t2p->tiff_transferfunction[2] != (float*) NULL) &&
-                           (t2p->tiff_transferfunction[1] !=
-                            t2p->tiff_transferfunction[0])) {
-				t2p->tiff_transferfunctioncount = 3;
-				t2p->tiff_pages[i].page_extra += 4;
-				t2p->pdf_xrefcount += 4;
-			} else {
-				t2p->tiff_transferfunctioncount = 1;
-				t2p->tiff_pages[i].page_extra += 2;
-				t2p->pdf_xrefcount += 2;
-			}
-			if(t2p->pdf_minorversion < 2)
-				t2p->pdf_minorversion = 2;
+                                 &(tiff_transferfunction[0]),
+                                 &(tiff_transferfunction[1]),
+                                 &(tiff_transferfunction[2]))) {
+
+                        if((tiff_transferfunction[1] != (uint16*) NULL) &&
+                           (tiff_transferfunction[2] != (uint16*) NULL)
+                          ) {
+                            tiff_transferfunctioncount=3;
+                        } else {
+                            tiff_transferfunctioncount=1;
+                        }
                 } else {
-			t2p->tiff_transferfunctioncount=0;
+			tiff_transferfunctioncount=0;
 		}
+
+                if (i > 0){
+                    if (tiff_transferfunctioncount != t2p->tiff_transferfunctioncount){
+                        TIFFError(
+                            TIFF2PDF_MODULE,
+                            "Different transfer function on page %d",
+                            i);
+                        t2p->t2p_error = T2P_ERR_ERROR;
+                        return;
+                    }
+                }
+
+                t2p->tiff_transferfunctioncount = tiff_transferfunctioncount;
+                t2p->tiff_transferfunction[0] = tiff_transferfunction[0];
+                t2p->tiff_transferfunction[1] = tiff_transferfunction[1];
+                t2p->tiff_transferfunction[2] = tiff_transferfunction[2];
+                if(tiff_transferfunctioncount == 3){
+                        t2p->tiff_pages[i].page_extra += 4;
+                        t2p->pdf_xrefcount += 4;
+                        if(t2p->pdf_minorversion < 2)
+                                t2p->pdf_minorversion = 2;
+                } else if (tiff_transferfunctioncount == 1){
+                        t2p->tiff_pages[i].page_extra += 2;
+                        t2p->pdf_xrefcount += 2;
+                        if(t2p->pdf_minorversion < 2)
+                                t2p->pdf_minorversion = 2;
+                }
+
 		if( TIFFGetField(
 			input, 
 			TIFFTAG_ICCPROFILE, 
 			&(t2p->tiff_iccprofilelength), 
 			&(t2p->tiff_iccprofile)) != 0){
@@ -1825,14 +1849,13 @@ void t2p_read_tiff_data(T2P* t2p, TIFF*
 
 	if (TIFFGetField(input, TIFFTAG_TRANSFERFUNCTION,
 			 &(t2p->tiff_transferfunction[0]),
 			 &(t2p->tiff_transferfunction[1]),
 			 &(t2p->tiff_transferfunction[2]))) {
-		if((t2p->tiff_transferfunction[1] != (float*) NULL) &&
-                   (t2p->tiff_transferfunction[2] != (float*) NULL) &&
-                   (t2p->tiff_transferfunction[1] !=
-                    t2p->tiff_transferfunction[0])) {
+		if((t2p->tiff_transferfunction[1] != (uint16*) NULL) &&
+                   (t2p->tiff_transferfunction[2] != (uint16*) NULL)
+                  ) {
 			t2p->tiff_transferfunctioncount=3;
 		} else {
 			t2p->tiff_transferfunctioncount=1;
 		}
 	} else {
