d00bc4
diff -Naur tiff-4.0.3.orig/tools/tiff2pdf.c tiff-4.0.3/tools/tiff2pdf.c
d00bc4
--- tiff-4.0.3.orig/tools/tiff2pdf.c	2012-07-25 22:56:43.000000000 -0400
d00bc4
+++ tiff-4.0.3/tools/tiff2pdf.c	2013-05-02 12:04:49.057090227 -0400
d00bc4
@@ -3341,33 +3341,56 @@
d00bc4
 	uint32 height){
d00bc4
 
d00bc4
 	tsize_t i=0;
d00bc4
-	uint16 ri =0;
d00bc4
-	uint16 v_samp=1;
d00bc4
-	uint16 h_samp=1;
d00bc4
-	int j=0;
d00bc4
-	
d00bc4
-	i++;
d00bc4
-	
d00bc4
-	while(i<(*striplength)){
d00bc4
+
d00bc4
+	while (i < *striplength) {
d00bc4
+		tsize_t datalen;
d00bc4
+		uint16 ri;
d00bc4
+		uint16 v_samp;
d00bc4
+		uint16 h_samp;
d00bc4
+		int j;
d00bc4
+		int ncomp;
d00bc4
+
d00bc4
+		/* marker header: one or more FFs */
d00bc4
+		if (strip[i] != 0xff)
d00bc4
+			return(0);
d00bc4
+		i++;
d00bc4
+		while (i < *striplength && strip[i] == 0xff)
d00bc4
+			i++;
d00bc4
+		if (i >= *striplength)
d00bc4
+			return(0);
d00bc4
+		/* SOI is the only pre-SOS marker without a length word */
d00bc4
+		if (strip[i] == 0xd8)
d00bc4
+			datalen = 0;
d00bc4
+		else {
d00bc4
+			if ((*striplength - i) <= 2)
d00bc4
+				return(0);
d00bc4
+			datalen = (strip[i+1] << 8) | strip[i+2];
d00bc4
+			if (datalen < 2 || datalen >= (*striplength - i))
d00bc4
+				return(0);
d00bc4
+		}
d00bc4
 		switch( strip[i] ){
d00bc4
-			case 0xd8:
d00bc4
-				/* SOI - start of image */
d00bc4
+			case 0xd8:	/* SOI - start of image */
d00bc4
 				_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), 2);
d00bc4
 				*bufferoffset+=2;
d00bc4
-				i+=2;
d00bc4
 				break;
d00bc4
-			case 0xc0:
d00bc4
-			case 0xc1:
d00bc4
-			case 0xc3:
d00bc4
-			case 0xc9:
d00bc4
-			case 0xca:
d00bc4
+			case 0xc0:	/* SOF0 */
d00bc4
+			case 0xc1:	/* SOF1 */
d00bc4
+			case 0xc3:	/* SOF3 */
d00bc4
+			case 0xc9:	/* SOF9 */
d00bc4
+			case 0xca:	/* SOF10 */
d00bc4
 				if(no==0){
d00bc4
-					_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), strip[i+2]+2);
d00bc4
-					for(j=0;j
d00bc4
-						if( (buffer[*bufferoffset+11+(2*j)]>>4) > h_samp) 
d00bc4
-							h_samp = (buffer[*bufferoffset+11+(2*j)]>>4);
d00bc4
-						if( (buffer[*bufferoffset+11+(2*j)] & 0x0f) > v_samp) 
d00bc4
-							v_samp = (buffer[*bufferoffset+11+(2*j)] & 0x0f);
d00bc4
+					_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), datalen+2);
d00bc4
+					ncomp = buffer[*bufferoffset+9];
d00bc4
+					if (ncomp < 1 || ncomp > 4)
d00bc4
+						return(0);
d00bc4
+					v_samp=1;
d00bc4
+					h_samp=1;
d00bc4
+					for(j=0;j
d00bc4
+						uint16 samp = buffer[*bufferoffset+11+(3*j)];
d00bc4
+						if( (samp>>4) > h_samp) 
d00bc4
+							h_samp = (samp>>4);
d00bc4
+						if( (samp & 0x0f) > v_samp) 
d00bc4
+							v_samp = (samp & 0x0f);
d00bc4
 					}
d00bc4
 					v_samp*=8;
d00bc4
 					h_samp*=8;
d00bc4
@@ -3381,45 +3404,43 @@
d00bc4
                                           (unsigned char) ((height>>8) & 0xff);
d00bc4
 					buffer[*bufferoffset+6]=
d00bc4
                                             (unsigned char) (height & 0xff);
d00bc4
-					*bufferoffset+=strip[i+2]+2;
d00bc4
-					i+=strip[i+2]+2;
d00bc4
-
d00bc4
+					*bufferoffset+=datalen+2;
d00bc4
+					/* insert a DRI marker */
d00bc4
 					buffer[(*bufferoffset)++]=0xff;
d00bc4
 					buffer[(*bufferoffset)++]=0xdd;
d00bc4
 					buffer[(*bufferoffset)++]=0x00;
d00bc4
 					buffer[(*bufferoffset)++]=0x04;
d00bc4
 					buffer[(*bufferoffset)++]=(ri >> 8) & 0xff;
d00bc4
 					buffer[(*bufferoffset)++]= ri & 0xff;
d00bc4
-				} else {
d00bc4
-					i+=strip[i+2]+2;
d00bc4
 				}
d00bc4
 				break;
d00bc4
-			case 0xc4:
d00bc4
-			case 0xdb:
d00bc4
-				_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), strip[i+2]+2);
d00bc4
-				*bufferoffset+=strip[i+2]+2;
d00bc4
-				i+=strip[i+2]+2;
d00bc4
+			case 0xc4: /* DHT */
d00bc4
+			case 0xdb: /* DQT */
d00bc4
+				_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), datalen+2);
d00bc4
+				*bufferoffset+=datalen+2;
d00bc4
 				break;
d00bc4
-			case 0xda:
d00bc4
+			case 0xda: /* SOS */
d00bc4
 				if(no==0){
d00bc4
-					_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), strip[i+2]+2);
d00bc4
-					*bufferoffset+=strip[i+2]+2;
d00bc4
-					i+=strip[i+2]+2;
d00bc4
+					_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), datalen+2);
d00bc4
+					*bufferoffset+=datalen+2;
d00bc4
 				} else {
d00bc4
 					buffer[(*bufferoffset)++]=0xff;
d00bc4
 					buffer[(*bufferoffset)++]=
d00bc4
                                             (unsigned char)(0xd0 | ((no-1)%8));
d00bc4
-					i+=strip[i+2]+2;
d00bc4
 				}
d00bc4
-				_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i-1]), (*striplength)-i-1);
d00bc4
-				*bufferoffset+=(*striplength)-i-1;
d00bc4
+				i += datalen + 1;
d00bc4
+				/* copy remainder of strip */
d00bc4
+				_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i]), *striplength - i);
d00bc4
+				*bufferoffset+= *striplength - i;
d00bc4
 				return(1);
d00bc4
 			default:
d00bc4
-				i+=strip[i+2]+2;
d00bc4
+				/* ignore any other marker */
d00bc4
+				break;
d00bc4
 		}
d00bc4
+		i += datalen + 1;
d00bc4
 	}
d00bc4
-	
d00bc4
 
d00bc4
+	/* failed to find SOS marker */
d00bc4
 	return(0);
d00bc4
 }
d00bc4
 #endif