libtiff: Merge branch 'fax2tiff_191' into 'master'

From dfd5e8fda06410bd2b5cddb742372d72f5e3d4f0 Mon Sep 17 00:00:00 2001
From: Lee Howard <[EMAIL REDACTED]>
Date: Fri, 15 Mar 2024 13:26:50 -0700
Subject: [PATCH] Fix fax2tiff issue #191 - EOFB interpretation

---
 libtiff/tif_fax3.c |  3 ++-
 tools/fax2tiff.c   | 10 ++++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/libtiff/tif_fax3.c b/libtiff/tif_fax3.c
index 668e96a3..c70e1245 100644
--- a/libtiff/tif_fax3.c
+++ b/libtiff/tif_fax3.c
@@ -1556,6 +1556,7 @@ static int Fax4Decode(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
         return (-1);
     }
     CACHE_STATE(tif, sp);
+    int start = sp->line;
     while (occ > 0)
     {
         a0 = 0;
@@ -1604,7 +1605,7 @@ static int Fax4Decode(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
         }
         (*sp->fill)(buf, thisrun, pa, lastx);
         UNCACHE_STATE(tif, sp);
-        return (sp->line ? 1 : -1); /* don't error on badly-terminated strips */
+        return (sp->line != start ? 1 : -1); /* don't error on badly-terminated strips */
     }
     UNCACHE_STATE(tif, sp);
     return (1);
diff --git a/tools/fax2tiff.c b/tools/fax2tiff.c
index 27c88c34..ab8a65e1 100644
--- a/tools/fax2tiff.c
+++ b/tools/fax2tiff.c
@@ -64,6 +64,7 @@ int verbose;
 int stretch;
 uint16_t badfaxrun;
 uint32_t badfaxlines;
+int compression_in;
 
 int copyFaxFile(TIFF *tifin, TIFF *tifout);
 static void usage(int code);
@@ -84,7 +85,7 @@ int main(int argc, char *argv[])
     TIFF *out = NULL;
     FAX_Client_Data client_data;
     TIFFErrorHandler whandler = NULL;
-    int compression_in = COMPRESSION_CCITTFAX3;
+    compression_in = COMPRESSION_CCITTFAX3;
     int compression_out = COMPRESSION_CCITTFAX3;
     int fillorder_in = FILLORDER_LSB2MSB;
     int fillorder_out = FILLORDER_LSB2MSB;
@@ -401,8 +402,13 @@ int copyFaxFile(TIFF *tifin, TIFF *tifout)
     while (tifin->tif_rawcc > 0)
     {
         ok = (*tifin->tif_decoderow)(tifin, (tdata_t)rowbuf, linesize, 0);
-        if (!ok)
+        if (ok < 1)
         {
+            if (compression_in == COMPRESSION_CCITTFAX4)
+            {
+                /* This is proably EOFB, but if it's corrupt data, then we can't continue, anyway. */
+                break;
+            }
             badfaxlines++;
             badrun++;
             /* regenerate line from previous good line */