From 730cbcd0d3ef0fd03fd2464faa070c7bb793fd9d Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 3 Jan 2006 11:54:38 +0000 Subject: [PATCH] Add (and throw) an exception if the powerpoint file is corrupt in a way that some seem to be (first record is of type 0000, and length is 0xFFFF) git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@365615 13f79535-47bb-0310-9956-ffa450edef68 --- .../CorruptPowerPointFileException.java | 34 +++++++++++++++++++ .../org/apache/poi/hslf/record/Record.java | 11 ++++-- .../apache/poi/hslf/usermodel/SlideShow.java | 3 +- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/scratchpad/src/org/apache/poi/hslf/exceptions/CorruptPowerPointFileException.java diff --git a/src/scratchpad/src/org/apache/poi/hslf/exceptions/CorruptPowerPointFileException.java b/src/scratchpad/src/org/apache/poi/hslf/exceptions/CorruptPowerPointFileException.java new file mode 100644 index 000000000..98f1e2c5b --- /dev/null +++ b/src/scratchpad/src/org/apache/poi/hslf/exceptions/CorruptPowerPointFileException.java @@ -0,0 +1,34 @@ + +/* ==================================================================== + Copyright 2002-2004 Apache Software Foundation + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + + + +package org.apache.poi.hslf.exceptions; + +/** + * This exception is thrown when we try to open a PowerPoint file, and + * something is fundamentally broken about it. + * + * @author Nick Burch + */ + +public class CorruptPowerPointFileException extends IllegalStateException +{ + public CorruptPowerPointFileException(String s) { + super(s); + } +} diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/Record.java b/src/scratchpad/src/org/apache/poi/hslf/record/Record.java index b9dddf32b..1e6778a5d 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/Record.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/Record.java @@ -24,6 +24,7 @@ import java.io.OutputStream; import java.io.ByteArrayOutputStream; import java.util.Vector; import org.apache.poi.util.LittleEndian; +import org.apache.poi.hslf.exceptions.CorruptPowerPointFileException; /** @@ -83,7 +84,7 @@ public abstract class Record } /** - * Default method for finding child records of a given record + * Default method for finding child records of a container record */ public static Record[] findChildRecords(byte[] b, int start, int len) { Vector children = new Vector(5); @@ -98,6 +99,12 @@ public abstract class Record int rleni = (int)rlen; if(rleni < 0) { rleni = 0; } + // Abort if first record is of type 0000 and length FFFF, + // as that's a sign of a screwed up record + if(pos == 0 && type == 0l && rleni == 0xffff) { + throw new CorruptPowerPointFileException("Corrupt document - starts with record of type 0000 and length 0xFFFF"); + } + //System.out.println("Found a " + type + " at pos " + pos + " (" + Integer.toHexString(pos) + "), len " + rlen); Record r = createRecordForType(type,b,pos,8+rleni); children.add(r); @@ -148,7 +155,7 @@ public abstract class Record } catch(InstantiationException ie) { throw new RuntimeException("Couldn't instantiate the class for type with id " + type + " on class " + c + " : " + ie); } catch(java.lang.reflect.InvocationTargetException ite) { - throw new RuntimeException("Couldn't instantiate the class for type with id " + type + " on class " + c + " : " + ite); + throw new RuntimeException("Couldn't instantiate the class for type with id " + type + " on class " + c + " : " + ite + "\nCause was : " + ite.getCause()); } catch(IllegalAccessException iae) { throw new RuntimeException("Couldn't access the constructor for type with id " + type + " on class " + c + " : " + iae); } catch(NoSuchMethodException nsme) { diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java index 817bd0ce5..15b610ce0 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java @@ -36,6 +36,7 @@ import org.apache.poi.hslf.record.SlideListWithText; import org.apache.poi.hslf.record.SlideListWithText.*; import org.apache.poi.hslf.record.PersistPtrHolder; import org.apache.poi.hslf.record.PositionDependentRecord; +import org.apache.poi.hslf.exceptions.CorruptPowerPointFileException; /** * This class is a friendly wrapper on top of the more scary HSLFSlideShow. @@ -227,7 +228,7 @@ public class SlideShow // Ensure we really found a Document record // If we didn't, then the file is probably corrupt if(documentRecord == null) { - throw new IllegalStateException("The PowerPoint file didn't contain a Document Record in its PersistPtr blocks. It is probably corrupt."); + throw new CorruptPowerPointFileException("The PowerPoint file didn't contain a Document Record in its PersistPtr blocks. It is probably corrupt."); }