]> source.dussan.org Git - poi.git/commitdiff
Fix bug #51832 - handle XLS files where the WRITEPROTECT record preceeds the FILEPASS...
authorNick Burch <nick@apache.org>
Mon, 19 Sep 2011 11:43:09 +0000 (11:43 +0000)
committerNick Burch <nick@apache.org>
Mon, 19 Sep 2011 11:43:09 +0000 (11:43 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1172575 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/record/RecordFactoryInputStream.java
src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
test-data/spreadsheet/51832.xls [new file with mode: 0644]

index be005a1d78fc2c966a3679c3c28df0c6b114bb44..69291355a91fcf5b6af2b8b4f0209ac4133799d8 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta5" date="2011-??-??">
+           <action dev="poi-developers" type="fix">51832 - handle XLS files where the WRITEPROTECT record preceeds the FILEPASS one, rather than following as normal</action>
            <action dev="poi-developers" type="fix">51809 - correct GTE handling in COUNTIF</action>
            <action dev="poi-developers" type="add">Add HWPF API to update range text and delete bookmarks</action>
            <action dev="poi-developers" type="add">HWPF Bookmarks tables are correctly updated on text updates</action>
index 479169a5c29140fc31ec2ce1d164f9a61caedc85..344ad07ef3c4f7c9d6b6b308869590d206c731ac 100644 (file)
@@ -57,11 +57,26 @@ public final class RecordFactoryInputStream {
                        FilePassRecord fpr = null;
                        if (rec instanceof BOFRecord) {
                                _hasBOFRecord = true;
+                               
+                               // Fetch the next record, and see if it indicates whether
+                               //  the document is encrypted or not
                                if (rs.hasNextRecord()) {
                                        rs.nextRecord();
                                        rec = RecordFactory.createSingleRecord(rs);
                                        recSize += rec.getRecordSize();
                                        outputRecs.add(rec);
+                                       
+                                       // Encrypted is normally BOF then FILEPASS
+                                       // May sometimes be BOF, WRITEPROTECT, FILEPASS
+                                       if (rec instanceof WriteProtectRecord && rs.hasNextRecord()) {
+                      rs.nextRecord();
+                      rec = RecordFactory.createSingleRecord(rs);
+                      recSize += rec.getRecordSize();
+                      outputRecs.add(rec);
+                                       }
+                                       
+                                       // If it's a FILEPASS, track it specifically but
+                                       //  don't include it in the main stream
                                        if (rec instanceof FilePassRecord) {
                                                fpr = (FilePassRecord) rec;
                                                outputRecs.remove(outputRecs.size()-1);
index e64cd7240bd0fa0e93b0193d1eaae17b348bd300..12171ccb0cd993345a4296d5ee3591ea8a412d70 100644 (file)
@@ -18,6 +18,8 @@
 package org.apache.poi.hssf.usermodel;
 
 import junit.framework.AssertionFailedError;
+
+import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.hssf.HSSFITestDataProvider;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.hssf.OldExcelFormatException;
@@ -2179,4 +2181,16 @@ if(1==2) {
         writeOutAndReadBack(wb);
     }
 
+    /**
+     * Normally encrypted files have BOF then FILEPASS, but
+     *  some may squeeze a WRITEPROTECT in the middle
+     */
+    public void test51832() {
+       try {
+          openSample("51832.xls");
+          fail("Encrypted file");
+       } catch(EncryptedDocumentException e) {
+          // Good
+       }
+    }
 }
diff --git a/test-data/spreadsheet/51832.xls b/test-data/spreadsheet/51832.xls
new file mode 100644 (file)
index 0000000..8b38bff
Binary files /dev/null and b/test-data/spreadsheet/51832.xls differ