Author: damjan Date: Tue Dec 4 04:39:10 2012 New Revision: 1416772 URL: http://svn.apache.org/viewvc?rev=1416772&view=rev Log: Deal with JPEG images with bad block sizes.
Jira issue key: IMAGING-99 Submitted by: st.h <sth at lavabit dot com> Modified: commons/proper/imaging/trunk/src/changes/changes.xml commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java Modified: commons/proper/imaging/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/changes/changes.xml?rev=1416772&r1=1416771&r2=1416772&view=diff ============================================================================== --- commons/proper/imaging/trunk/src/changes/changes.xml (original) +++ commons/proper/imaging/trunk/src/changes/changes.xml Tue Dec 4 04:39:10 2012 @@ -245,6 +245,9 @@ The <action> type attribute can be add,u <action issue="IMAGING-90" dev="damjan" type="fix"> Allow non-1 TIFF field lengths when parsing offset fields in non-strict mode. </action> + <action issue="IMAGING-99" dev="damjan" type="fix" due-to="st.h"> + java.io.IOException: Could not read block + </action> </release> </body> Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java?rev=1416772&r1=1416771&r2=1416772&view=diff ============================================================================== --- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java (original) +++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java Tue Dec 4 04:39:10 2012 @@ -28,6 +28,7 @@ import org.apache.commons.imaging.common import org.apache.commons.imaging.common.ByteOrder; import org.apache.commons.imaging.common.bytesource.ByteSource; import org.apache.commons.imaging.common.bytesource.ByteSourceFile; +import org.apache.commons.imaging.formats.jpeg.JpegConstants; import org.apache.commons.imaging.formats.tiff.TiffDirectory.ImageDataElement; import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants; import org.apache.commons.imaging.formats.tiff.constants.TiffConstants; @@ -505,11 +506,16 @@ public class TiffReader extends BinaryFi ImageDataElement element = directory.getJpegRawImageDataElement(); int offset = element.offset; int length = element.length; - // Sony DCR-PC110 has an off-by-one error. - if (offset + length == byteSource.getLength() + 1) { - length--; + // In case the length is not correct, adjust it and check if the last read byte actually is the end of the image + if (offset + length > byteSource.getLength()) { + length = (int) byteSource.getLength() - offset; } byte data[] = byteSource.getBlock(offset, length); + // check if the last read byte is actually the end of the image data + if (length < 2 || + (((data[data.length - 2] & 0xff) << 8) | (data[data.length - 1] & 0xff)) != JpegConstants.EOIMarker) { + throw new ImageReadException("JPEG EOI marker could not be found at expected location"); + } return new JpegImageData(offset, length, data); }