diff --git a/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java b/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java index 0dce7cb56..4dac22aa6 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java +++ b/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java @@ -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 index 000000000..66b4e50c0 Binary files /dev/null and b/src/scratchpad/testcases/org/apache/poi/hslf/data/PictureTypeZero.ppt differ diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java index 91f4f1f50..d61245e23 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java @@ -373,5 +373,22 @@ public class TestPictures extends TestCase{ } + /** + * Test that on a party corrupt powerpoint document, which has + * crazy pictures of type 0, we do our best. + */ + public void testZeroPictureType() throws Exception { + HSLFSlideShow hslf = new HSLFSlideShow(new File(cwd, "PictureTypeZero.ppt").getPath()); + // Should still have 2 real pictures + assertEquals(2, hslf.getPictures().length); + // Both are real pictures, both WMF + assertEquals(Picture.WMF, hslf.getPictures()[0].getType()); + assertEquals(Picture.WMF, hslf.getPictures()[1].getType()); + + // TODO: DISABLED: Pending bug #41176 + + // Now test what happens when we use the SlideShow interface + //SlideShow ppt = new SlideShow(hslf); + } }