Author: damjan
Date: Sat Jul 16 09:11:03 2011
New Revision: 1147386

URL: http://svn.apache.org/viewvc?rev=1147386&view=rev
Log:
Make JPEG parsing a lot more resilient:
skip all bytes between the end of a segment
and the next 0xFF followed by non-0xFF.
Add a thorough test for this.

Fixes one of the problems with SANSELAN-13.

JIRA-Key: SANSELAN-13


Added:
    commons/proper/sanselan/trunk/src/test/data/images/jpg/4/
    
commons/proper/sanselan/trunk/src/test/data/images/jpg/4/0x00-to-0xFF-between-segments.jpg
   (with props)
    commons/proper/sanselan/trunk/src/test/data/images/jpg/4/info.txt
Modified:
    
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/jpeg/JpegUtils.java

Modified: 
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/jpeg/JpegUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/jpeg/JpegUtils.java?rev=1147386&r1=1147385&r2=1147386&view=diff
==============================================================================
--- 
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/jpeg/JpegUtils.java
 (original)
+++ 
commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/jpeg/JpegUtils.java
 Sat Jul 16 09:11:03 2011
@@ -65,10 +65,12 @@ public class JpegUtils extends BinaryFil
 
             for (int markerCount = 0; true; markerCount++)
             {
-                byte markerBytes[] = readByteArray("markerBytes", 2, is,
-                        "markerBytes");
-                int marker = convertByteArrayToShort("marker", markerBytes,
-                        byteOrder);
+                byte[] markerBytes = new byte[2];
+                do {
+                    markerBytes[0] = markerBytes[1];
+                    markerBytes[1] = readByte("marker", is, "Could not read 
marker");
+                } while ((0xff & markerBytes[0]) != 0xff || (0xff & 
markerBytes[1]) == 0xff);
+                int marker = ((0xff & markerBytes[0]) << 8) | (0xff & 
markerBytes[1]);
 
 //                Debug.debug("marker", marker + " (0x" + 
Integer.toHexString(marker) + ")");
 //                Debug.debug("markerBytes", markerBytes);

Added: 
commons/proper/sanselan/trunk/src/test/data/images/jpg/4/0x00-to-0xFF-between-segments.jpg
URL: 
http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/data/images/jpg/4/0x00-to-0xFF-between-segments.jpg?rev=1147386&view=auto
==============================================================================
Binary file - no diff available.

Propchange: 
commons/proper/sanselan/trunk/src/test/data/images/jpg/4/0x00-to-0xFF-between-segments.jpg
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: commons/proper/sanselan/trunk/src/test/data/images/jpg/4/info.txt
URL: 
http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/data/images/jpg/4/info.txt?rev=1147386&view=auto
==============================================================================
--- commons/proper/sanselan/trunk/src/test/data/images/jpg/4/info.txt (added)
+++ commons/proper/sanselan/trunk/src/test/data/images/jpg/4/info.txt Sat Jul 
16 09:11:03 2011
@@ -0,0 +1,7 @@
+Image contributed by Damjan Jovanovic.
+
+This is a badly corrupted JPEG: 0x00 0x01 ... 0xFF
+is written between each segment. A resilient JPEG
+parser must skip this data, and scan forward
+looking for 0xFF followed by a non-0xFF byte
+which marks the next segment.


Reply via email to