From 50610894c45344a47a7d5b3f7ac2c26690d6a338 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 23 Apr 2007 10:45:49 +0000 Subject: [PATCH] Detect, and report a meaningful error, if we come across an Office 2007 XML document git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@531419 13f79535-47bb-0310-9956-ffa450edef68 --- .../filesystem/OfficeXmlFileException.java | 36 +++++++++++++ .../poi/poifs/storage/HeaderBlockReader.java | 8 +++ .../org/apache/poi/hssf/data/sample.xlsx | Bin 0 -> 8836 bytes .../TestOffice2007XMLException.java | 50 ++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 src/java/org/apache/poi/poifs/filesystem/OfficeXmlFileException.java create mode 100644 src/testcases/org/apache/poi/hssf/data/sample.xlsx create mode 100644 src/testcases/org/apache/poi/poifs/filesystem/TestOffice2007XMLException.java diff --git a/src/java/org/apache/poi/poifs/filesystem/OfficeXmlFileException.java b/src/java/org/apache/poi/poifs/filesystem/OfficeXmlFileException.java new file mode 100644 index 0000000000..b83ccd0f27 --- /dev/null +++ b/src/java/org/apache/poi/poifs/filesystem/OfficeXmlFileException.java @@ -0,0 +1,36 @@ + +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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.poifs.filesystem; + +/** + * This exception is thrown when we try to open a file that's actually + * an Office 2007+ XML file, rather than an OLE2 file (which is what + * POI works with) + * + * @author Nick Burch + */ + +public class OfficeXmlFileException extends IllegalArgumentException +{ + public OfficeXmlFileException(String s) { + super(s); + } +} diff --git a/src/java/org/apache/poi/poifs/storage/HeaderBlockReader.java b/src/java/org/apache/poi/poifs/storage/HeaderBlockReader.java index 70e1c301b7..16c94e2c23 100644 --- a/src/java/org/apache/poi/poifs/storage/HeaderBlockReader.java +++ b/src/java/org/apache/poi/poifs/storage/HeaderBlockReader.java @@ -24,6 +24,7 @@ import java.io.*; import java.util.*; import org.apache.poi.poifs.common.POIFSConstants; +import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.util.IOUtils; import org.apache.poi.util.IntegerField; import org.apache.poi.util.LittleEndian; @@ -89,6 +90,13 @@ public class HeaderBlockReader if (signature.get() != _signature) { + // Is it one of the usual suspects? + if(_data[0] == 0x50 && _data[1] == 0x4b && _data[2] == 0x03 && + _data[3] == 0x04) { + throw new OfficeXmlFileException("The supplied data appears to be in the Office 2007+ XML. POI only supports OLE2 Office documents"); + } + + // Give a generic error throw new IOException("Invalid header signature; read " + signature.get() + ", expected " + _signature); diff --git a/src/testcases/org/apache/poi/hssf/data/sample.xlsx b/src/testcases/org/apache/poi/hssf/data/sample.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..2eb36ee2a0a1c3d1ce59617161d522cf86f0564b GIT binary patch literal 8836 zcmeHMXH-*Lw++4bUW0Um5Rl&6rFW$x(xrut^e#=h5I_Nu-jNPM=wN6ngx*EEND+~u zfV}9v@9IV07;n5E@6UJ7$T=q&d#{yq_MUUCx!2ZFM?)tEU;?lK24!A?Ij*OJK_~zK z6D9ya2Eax&Rd99jvUTw?*Yk6;^)%!4b%rqIqoZ=>0#E_}9{|G72Ip>T zOU)tpV2q+gp?;)s(a!p3>5jW3n3mAdDcPapP>8gIOqTRMgT)_{n77iSmt91aA1iS2 z)e_=g3|4{_bG18Hv<4cDP@cvs8QwM_>xvN7qlE|u6#_bRh(VD=MER`1j&aFkTEP)_ zd4C=g!N@e9p$N~`WM&H@^}3IEoB`)6$r z{LZwH)oYRIaoKk_`{+#;&A)`Te8|lSF9G>|ymw#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cmV?d00001 diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestOffice2007XMLException.java b/src/testcases/org/apache/poi/poifs/filesystem/TestOffice2007XMLException.java new file mode 100644 index 0000000000..db8607d37e --- /dev/null +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestOffice2007XMLException.java @@ -0,0 +1,50 @@ + +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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.poifs.filesystem; + +import junit.framework.TestCase; +import java.io.*; + +/** + * Class to test that POIFS complains when given an Office 2007 XML document + * + * @author Marc Johnson + */ + +public class TestOffice2007XMLException extends TestCase +{ + public String dirname; + + public void setUp() { + dirname = System.getProperty("HSSF.testdata.path"); + } + + public void testXMLException() throws IOException + { + FileInputStream in = new FileInputStream(dirname + "/sample.xlsx"); + + try { + new POIFSFileSystem(in); + fail(); + } catch(OfficeXmlFileException e) { + // Good + } + } +}