]> source.dussan.org Git - poi.git/commitdiff
If we have a picture of type 0, don't even bother trying to create a PictureData...
authorNick Burch <nick@apache.org>
Thu, 14 Dec 2006 11:49:56 +0000 (11:49 +0000)
committerNick Burch <nick@apache.org>
Thu, 14 Dec 2006 11:49:56 +0000 (11:49 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@487181 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java
src/scratchpad/testcases/org/apache/poi/hslf/data/PictureTypeZero.ppt [new file with mode: 0644]
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java

index 0dce7cb568bbf7b4fd08bf591af6b178c2c8779b..4dac22aa6843da70840838fa137ea5f8ee8a34fe 100644 (file)
@@ -255,30 +255,36 @@ public class HSLFSlideShow extends POIDocument
         List p = new ArrayList();
         int pos = 0;
 
-        while (pos < pictstream.length) {
+               // An empty picture record (length 0) will take up 8 bytes
+        while (pos <= (pictstream.length-8)) {
             int offset = pos;
 
-            //image signature
+            // Image signature
             int signature = LittleEndian.getUShort(pictstream, pos);
             pos += LittleEndian.SHORT_SIZE;
-            //image type + 0xF018
+            // Image type + 0xF018
             int type = LittleEndian.getUShort(pictstream, pos);
             pos += LittleEndian.SHORT_SIZE;
-            //image size
+            // Image size (excluding the 8 byte header)
             int imgsize = LittleEndian.getInt(pictstream, pos);
             pos += LittleEndian.INT_SIZE;
-
             byte[] imgdata = new byte[imgsize];
             System.arraycopy(pictstream, pos, imgdata, 0, imgdata.length);
 
-            try {
-               PictureData pict = PictureData.create(type - 0xF018);
-               pict.setRawData(imgdata);
-               pict.setOffset(offset);
-               p.add(pict);
-            } catch(IllegalArgumentException e) {
-               System.err.println("Problem reading picture: " + e + "\nYou document will probably become corrupted if you save it!");
-            }
+                       // If they type (including the bonus 0xF018) is 0, skip it
+                       if(type == 0) {
+                               System.err.println("Problem reading picture: Invalid image type 0, on picture with length" + imgsize + ".\nYou document will probably become corrupted if you save it!");
+                       } else {
+                               // Build the PictureData object from the data
+                               try {
+                                       PictureData pict = PictureData.create(type - 0xF018);
+                                       pict.setRawData(imgdata);
+                                       pict.setOffset(offset);
+                                       p.add(pict);
+                               } catch(IllegalArgumentException e) {
+                                       System.err.println("Problem reading picture: " + e + "\nYou document will probably become corrupted if you save it!");
+                               }
+                       }
             
             pos += imgsize;
         }
diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/data/PictureTypeZero.ppt b/src/scratchpad/testcases/org/apache/poi/hslf/data/PictureTypeZero.ppt
new file mode 100644 (file)
index 0000000..66b4e50
Binary files /dev/null and b/src/scratchpad/testcases/org/apache/poi/hslf/data/PictureTypeZero.ppt differ
index 91f4f1f509860a2c4630d6961ddfb15d5ae482d2..d61245e2312ece91a2fe7aae73992fb53a8ebb28 100644 (file)
@@ -373,5 +373,22 @@ public class TestPictures extends TestCase{
 \r
     }\r
 \r
-\r
+       /**\r
+        * Test that on a party corrupt powerpoint document, which has \r
+        *  crazy pictures of type 0, we do our best.\r
+        */\r
+       public void testZeroPictureType() throws Exception {\r
+               HSLFSlideShow hslf = new HSLFSlideShow(new File(cwd, "PictureTypeZero.ppt").getPath());\r
+\r
+               // Should still have 2 real pictures\r
+               assertEquals(2, hslf.getPictures().length);\r
+               // Both are real pictures, both WMF\r
+               assertEquals(Picture.WMF, hslf.getPictures()[0].getType());\r
+               assertEquals(Picture.WMF, hslf.getPictures()[1].getType());\r
+\r
+               // TODO: DISABLED: Pending bug #41176\r
+\r
+               // Now test what happens when we use the SlideShow interface\r
+               //SlideShow ppt = new SlideShow(hslf);\r
+       }\r
 }\r