libtiff: Avoid trusting samplesperpixel's default of 1 for purposes of trimming tags. This is to get some super crappy OJPEG files...

https://github.com/libsdl-org/libtiff/commit/f7c1776fa7f6d6fb3f038f573380c1fddb150394

From f7c1776fa7f6d6fb3f038f573380c1fddb150394 Mon Sep 17 00:00:00 2001
From: Frank Warmerdam <[EMAIL REDACTED]>
Date: Tue, 5 Jun 2012 01:48:30 +0000
Subject: [PATCH]  Avoid trusting samplesperpixel's default of 1 for purposes
 of trimming tags.  This is to get some super crappy OJPEG files to work
 again.  Grr. (#2348)

---
 ChangeLog             |  7 +++++++
 libtiff/tif_dirread.c | 24 ++++++++++++------------
 2 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 58b5804a..63a8d0ac 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2012-06-04  Frank Warmerdam  <warmerdam@google.com>
+
+	* libtiff/tif_dirread.c: Avoid trusting samplesperpixel's default
+	of 1 for purposes of trimming tags.  This is to get some super
+	crappy OJPEG files to work again.  Grr. 
+	http://bugzilla.maptools.org/show_bug.cgi?id=2348
+
 2012-06-01  Frank Warmerdam  <warmerdam@google.com>
 
 	* libtiff/tif_dir.c: Avoid generic handling of TIFFTAG_WHITELEVEL.
diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
index 667a5d96..14681f72 100644
--- a/libtiff/tif_dirread.c
+++ b/libtiff/tif_dirread.c
@@ -1,4 +1,4 @@
-/* $Id: tif_dirread.c,v 1.92.2.15 2010-12-31 16:12:40 olivier Exp $ */
+/* $Id: tif_dirread.c,v 1.92.2.16 2012-06-05 01:48:30 fwarmerdam Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -46,7 +46,7 @@ static  TIFFDirEntry* TIFFReadDirectoryFind(TIFFDirEntry* dir,
 static	int EstimateStripByteCounts(TIFF*, TIFFDirEntry*, uint16);
 static	void MissingRequired(TIFF*, const char*);
 static	int TIFFCheckDirOffset(TIFF*, toff_t);
-static	int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
+static	int CheckDirCount(TIFF*, TIFFDirEntry*, uint32, int);
 static	uint16 TIFFFetchDirectory(TIFF*, toff_t, TIFFDirEntry**, toff_t *);
 static	tsize_t TIFFFetchData(TIFF*, TIFFDirEntry*, char*);
 static	tsize_t TIFFFetchString(TIFF*, TIFFDirEntry*, char*);
@@ -238,7 +238,7 @@ TIFFReadDirectory(TIFF* tif)
 			uint32 expected = (fip->field_readcount == TIFF_SPP) ?
 			    (uint32) td->td_samplesperpixel :
 			    (uint32) fip->field_readcount;
-			if (!CheckDirCount(tif, dp, expected))
+			if (!CheckDirCount(tif, dp, expected, TRUE))
 				goto ignore;
 		}
 
@@ -519,7 +519,7 @@ TIFFReadDirectory(TIFF* tif)
 				v = 1L<<td->td_bitspersample;
 				if (dp->tdir_tag == TIFFTAG_COLORMAP ||
 				    dp->tdir_count != v) {
-					if (!CheckDirCount(tif, dp, 3 * v))
+					if (!CheckDirCount(tif, dp, 3 * v, TRUE))
 						break;
 				}
 				v *= sizeof(uint16);
@@ -941,7 +941,7 @@ TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
 			uint32 expected = (fip->field_readcount == TIFF_SPP) ?
 			    (uint32) td->td_samplesperpixel :
 			    (uint32) fip->field_readcount;
-			if (!CheckDirCount(tif, dp, expected))
+			if (!CheckDirCount(tif, dp, expected, TRUE))
 				goto ignore;
 		}
 
@@ -1109,7 +1109,7 @@ TIFFCheckDirOffset(TIFF* tif, toff_t diroff)
  * caller is expected to skip/ignore the tag if there is a mismatch.
  */
 static int
-CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
+CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count, int do_trim )
 {
 	if (count > dir->tdir_count) {
 		TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
@@ -1117,7 +1117,7 @@ CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
 		    _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
 		    dir->tdir_count, count);
 		return (0);
-	} else if (count < dir->tdir_count) {
+	} else if (count < dir->tdir_count && do_trim) {
 		TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
 	"incorrect count for field \"%s\" (%u, expecting %u); tag trimmed",
 		    _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
@@ -1700,7 +1700,7 @@ TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp)
 		}
 		if (cp != NULL)
 			_TIFFfree(cp);
-	} else if (CheckDirCount(tif, dp, 1)) {	/* singleton value */
+	} else if (CheckDirCount(tif, dp, 1, TRUE)) {	/* singleton value */
 		switch (dp->tdir_type) {
 		case TIFF_BYTE:
 		case TIFF_SBYTE:
@@ -1787,7 +1787,7 @@ TIFFFetchPerSampleShorts(TIFF* tif, TIFFDirEntry* dir, uint16* pl)
     uint16 samples = tif->tif_dir.td_samplesperpixel;
     int status = 0;
 
-    if (CheckDirCount(tif, dir, (uint32) samples)) {
+    if (CheckDirCount(tif, dir, (uint32) samples, TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))) {
         uint16 buf[10];
         uint16* v = buf;
 
@@ -1828,7 +1828,7 @@ TIFFFetchPerSampleLongs(TIFF* tif, TIFFDirEntry* dir, uint32* pl)
     uint16 samples = tif->tif_dir.td_samplesperpixel;
     int status = 0;
 
-    if (CheckDirCount(tif, dir, (uint32) samples)) {
+    if (CheckDirCount(tif, dir, (uint32) samples, TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))) {
         uint32 buf[10];
         uint32* v = buf;
 
@@ -1868,7 +1868,7 @@ TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* minv, double* maxv)
     uint16 samples = tif->tif_dir.td_samplesperpixel;
     int status = 0;
 
-    if (CheckDirCount(tif, dir, (uint32) samples)) {
+    if (CheckDirCount(tif, dir, (uint32) samples, TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))) {
         double buf[10];
         double* v = buf;
 
@@ -1908,7 +1908,7 @@ TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, long nstrips, uint32** lpp)
 	register uint32* lp;
 	int status;
 
-        CheckDirCount(tif, dir, (uint32) nstrips);
+        CheckDirCount(tif, dir, (uint32) nstrips, TRUE);
 
 	/*
 	 * Allocate space for strip information.