Password protected files patch

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Password protected files patch

Alexander Kurtakov
The patch.

Index: src/java/org/apache/poi/hssf/record/RecordFactory.java
===================================================================
RCS file: /home/cvspublic/jakarta-poi/src/java/org/apache/poi/hssf/record/RecordFactory.java,v
retrieving revision 1.22
diff -u -r1.22 RecordFactory.java
--- src/java/org/apache/poi/hssf/record/RecordFactory.java 5 May 2005 13:39:14 -0000 1.22
+++ src/java/org/apache/poi/hssf/record/RecordFactory.java 25 Jul 2005 14:26:43 -0000
@@ -19,6 +19,7 @@
 package org.apache.poi.hssf.record;
 
 import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.PasswordProtectedException;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -226,7 +227,10 @@
     {
         Record   retval;
         Record[] realretval = null;
-
+//        if (rectype==49 && size ==26) {
+//         // Encrypted document found
+//            throw new PasswordProtectedException("The document is encrypted");
+//        }
         try
         {
             Constructor constructor =
@@ -246,9 +250,11 @@
         }
         catch (Exception introspectionException)
         {
-            introspectionException.printStackTrace();
-            throw new RecordFormatException(
-                "Unable to construct record instance, the following exception occured: " + introspectionException.getMessage());
+//         System.out.println("wrong: "+rectype+"size:"+size);
+//            introspectionException.printStackTrace();
+            throw new PasswordProtectedException("The document is encrypted");
+//            throw new RecordFormatException(
+//                "Unable to construct record instance, the following exception occured: " + introspectionException.getMessage());
         }
         if (retval instanceof RKRecord)
         {
Index: src/scratchpad/src/org/apache/poi/hdf/extractor/WordDocument.java
===================================================================
RCS file: /home/cvspublic/jakarta-poi/src/scratchpad/src/org/apache/poi/hdf/extractor/WordDocument.java,v
retrieving revision 1.8
diff -u -r1.8 WordDocument.java
--- src/scratchpad/src/org/apache/poi/hdf/extractor/WordDocument.java 9 Apr 2004 13:05:30 -0000 1.8
+++ src/scratchpad/src/org/apache/poi/hdf/extractor/WordDocument.java 25 Jul 2005 14:26:48 -0000
@@ -20,19 +20,26 @@
 package org.apache.poi.hdf.extractor;
 
 
-import org.apache.poi.hdf.extractor.util.*;
-import org.apache.poi.hdf.extractor.data.*;
-import java.util.*;
-import java.io.*;
-import javax.swing.*;
-
-import java.awt.*;
-
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
-import org.apache.poi.poifs.filesystem.POIFSDocument;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import org.apache.poi.hdf.extractor.data.DOP;
+import org.apache.poi.hdf.extractor.data.LVL;
+import org.apache.poi.hdf.extractor.data.ListTables;
+import org.apache.poi.hdf.extractor.util.BTreeSet;
+import org.apache.poi.hdf.extractor.util.ChpxNode;
+import org.apache.poi.hdf.extractor.util.NumberFormatter;
+import org.apache.poi.hdf.extractor.util.PapxNode;
+import org.apache.poi.hdf.extractor.util.PropertyNode;
+import org.apache.poi.hdf.extractor.util.SepxNode;
 import org.apache.poi.poifs.filesystem.DocumentEntry;
-
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.PasswordProtectedException;
 
 /**
  * This class contains the main functionality for the Word file "reader". Much
@@ -242,7 +249,12 @@
       int parPLC = LittleEndian.getInt(_header, 0x102);
       int parPlcSize = LittleEndian.getInt(_header, 0x106);
       boolean useTable1 = (info & 0x200) != 0;
-
+      //Check to see if it's encrypted
+      // Looking for bit with 0x0100 in short at 0xa (returns 256 if set)
+      if((info & 0x0100) == 256) {
+      // Encrypted document found
+          throw new PasswordProtectedException("The document is encrypted");
+      }
       //process the text and formatting properties
       processComplexFile(useTable1, charPLC, charPlcSize, parPLC, parPlcSize);
   }
Index: src/scratchpad/src/org/apache/poi/hslf/record/Record.java
===================================================================
RCS file: /home/cvspublic/jakarta-poi/src/scratchpad/src/org/apache/poi/hslf/record/Record.java,v
retrieving revision 1.5
diff -u -r1.5 Record.java
--- src/scratchpad/src/org/apache/poi/hslf/record/Record.java 8 Jul 2005 17:38:55 -0000 1.5
+++ src/scratchpad/src/org/apache/poi/hslf/record/Record.java 25 Jul 2005 14:26:49 -0000
@@ -24,6 +24,7 @@
 import java.io.ByteArrayOutputStream;
 import java.util.Vector;
 import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.PasswordProtectedException;
 
 
 /**
@@ -83,7 +84,7 @@
  }
 
  /**
- * Default method for finding child records of a given record
+ * Default method for finding child records of a given record.
  */
  public static Record[] findChildRecords(byte[] b, int start, int len) {
  Vector children = new Vector(5);
@@ -92,6 +93,10 @@
  int pos = start;
  while(pos <= (start+len-8)) {
  long type = LittleEndian.getUShort(b,pos+2);
+ if (type == 51432) {
+// Encrypted document found
+          throw new PasswordProtectedException("The document is encrypted");
+ }
  long rlen = LittleEndian.getUInt(b,pos+4);
 
  // Sanity check the length
Index: src/java/org/apache/poi/util/PasswordProtectedException.java
===================================================================
RCS file: src/java/org/apache/poi/util/PasswordProtectedException.java
diff -N src/java/org/apache/poi/util/PasswordProtectedException.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/java/org/apache/poi/util/PasswordProtectedException.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,18 @@
+package org.apache.poi.util;
+
+
+/**
+ * Exeption indicating a password protected document.
+ * @author Alexander Kurtakov
+ * @version (CVS $Revision$)
+ */
+public class PasswordProtectedException extends RuntimeException {
+
+ /**
+ * Creates the exeption with the given file name.
+ * @param message The name of the password protected file.
+ */
+ public PasswordProtectedException(String message) {
+ super(message);
+ }
+}

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
Mailing List:    http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta POI Project: http://jakarta.apache.org/poi/