This is an automated email from the ASF dual-hosted git repository.

fanningpj pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 9051507311 support opening hwpf docs by passing passwords (#1017)
9051507311 is described below

commit 905150731100f5f45d56b34f17fcfdb97c487d9e
Author: PJ Fanning <[email protected]>
AuthorDate: Thu Mar 5 01:27:01 2026 +0100

    support opening hwpf docs by passing passwords (#1017)
    
    * support opening hwpf docs by passing passwords
    
    * Update poi-integration-exceptions.csv
---
 .../java/org/apache/poi/hwpf/HWPFDocument.java     | 51 +++++++++++++++++++-
 .../java/org/apache/poi/hwpf/HWPFDocumentCore.java | 54 ++++++++++++++++++++--
 test-data/poi-integration-exceptions.csv           |  4 +-
 3 files changed, 103 insertions(+), 6 deletions(-)

diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java
index a4fd555a91..67fc4b4414 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java
@@ -223,6 +223,23 @@ public final class HWPFDocument extends HWPFDocumentCore {
         this(verifyAndBuildPOIFS(istream));
     }
 
+    /**
+     * This constructor loads a Word document from an InputStream.
+     *
+     * @param istream The InputStream that contains the Word document.
+     * @param password in char array format (can be null)
+     * @throws IOException If there is an unexpected IOException from the 
passed
+     *                     in InputStream.
+     * @throws org.apache.poi.EmptyFileException If the given stream is empty
+     * @throws IllegalStateException a number of other runtime exceptions can 
be thrown, especially if there are problems with the
+     * input format
+     * @since 6.0.0
+     */
+    public HWPFDocument(InputStream istream, final char[] password) throws 
IOException {
+        //do Ole stuff
+        this(verifyAndBuildPOIFS(istream), password);
+    }
+
     /**
      * This constructor loads a Word document from a POIFSFileSystem
      *
@@ -236,6 +253,21 @@ public final class HWPFDocument extends HWPFDocumentCore {
         this(pfilesystem.getRoot());
     }
 
+    /**
+     * This constructor loads a Word document from a POIFSFileSystem
+     *
+     * @param pfilesystem The POIFSFileSystem that contains the Word document.
+     * @throws IOException If there is an unexpected IOException from the 
passed
+     *                     in POIFSFileSystem.
+     * @param password in char array format (can be null)
+     * @throws IllegalStateException a number of runtime exceptions can be 
thrown, especially if there are problems with the
+     * input format
+     * @since 6.0.0
+     */
+    public HWPFDocument(POIFSFileSystem pfilesystem, final char[] password) 
throws IOException {
+        this(pfilesystem.getRoot(), password);
+    }
+
     /**
      * This constructor loads a Word document from a specific point
      * in a POIFSFileSystem, probably not the default.
@@ -248,9 +280,26 @@ public final class HWPFDocument extends HWPFDocumentCore {
      * input format
      */
     public HWPFDocument(DirectoryNode directory) throws IOException {
+        this(directory, null);
+    }
+
+    /**
+     * This constructor loads a Word document from a specific point
+     * in a POIFSFileSystem, probably not the default.
+     * Used typically to open embedded documents.
+     *
+     * @param directory The DirectoryNode that contains the Word document.
+     * @param password in char array format (can be null)
+     * @throws IOException If there is an unexpected IOException from the 
passed
+     *                     in POIFSFileSystem.
+     * @throws IllegalStateException a number of runtime exceptions can be 
thrown, especially if there are problems with the
+     * input format
+     * @since 6.0.0
+     */
+    public HWPFDocument(DirectoryNode directory, final char[] password) throws 
IOException {
         // Load the main stream and FIB
         // Also handles HPSF bits
-        super(directory);
+        super(directory, password);
 
         // Is this document too old for us?
         if (_fib.getFibBase().getNFib() < 106) {
diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocumentCore.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocumentCore.java
index 4fac6d2b0c..2ea1224633 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocumentCore.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocumentCore.java
@@ -124,6 +124,8 @@ public abstract class HWPFDocumentCore extends POIDocument {
     /** main document stream buffer*/
     protected byte[] _mainStream;
 
+    private char[] _password;
+
     private EncryptionInfo _encryptionInfo;
 
     protected HWPFDocumentCore() {
@@ -160,6 +162,20 @@ public abstract class HWPFDocumentCore extends POIDocument 
{
         this( verifyAndBuildPOIFS(istream) );
     }
 
+    /**
+     * This constructor loads a Word document from an InputStream.
+     *
+     * @param istream The InputStream that contains the Word document.
+     * @param password in char array format (can be null)
+     * @throws IOException If there is an unexpected IOException from the 
passed
+     *         in InputStream.
+     * @since 6.0.0
+     */
+    public HWPFDocumentCore(InputStream istream, final char[] password) throws 
IOException {
+        //do Ole stuff
+        this( verifyAndBuildPOIFS(istream), password );
+    }
+
     /**
      * This constructor loads a Word document from a POIFSFileSystem
      *
@@ -171,6 +187,19 @@ public abstract class HWPFDocumentCore extends POIDocument 
{
         this(pfilesystem.getRoot());
     }
 
+    /**
+     * This constructor loads a Word document from a POIFSFileSystem
+     *
+     * @param pfilesystem The POIFSFileSystem that contains the Word document.
+     * @param password in char array format (can be null)
+     * @throws IOException If there is an unexpected IOException from the 
passed
+     *         in POIFSFileSystem.
+     * @since 6.0.0
+     */
+    public HWPFDocumentCore(POIFSFileSystem pfilesystem, final char[] 
password) throws IOException {
+        this(pfilesystem.getRoot(), password);
+    }
+
     /**
      * This constructor loads a Word document from a specific point
      *  in a POIFSFileSystem, probably not the default.
@@ -181,8 +210,24 @@ public abstract class HWPFDocumentCore extends POIDocument 
{
      *         in POIFSFileSystem.
      */
     public HWPFDocumentCore(DirectoryNode directory) throws IOException {
+        this(directory, null);
+    }
+
+    /**
+     * This constructor loads a Word document from a specific point
+     *  in a POIFSFileSystem, probably not the default.
+     * Used typically to open embedded documents.
+     *
+     * @param directory The DirectoryNode that contains the Word document.
+     * @param password in char array format (can be null)
+     * @throws IOException If there is an unexpected IOException from the 
passed
+     *         in POIFSFileSystem.
+     * @since 6.0.0
+     */
+    public HWPFDocumentCore(DirectoryNode directory, final char[] password) 
throws IOException {
         // Sort out the hpsf properties
         super(directory);
+        _password = password;
 
         // read in the main stream.
         _mainStream = getDocumentEntryBytes(STREAM_WORD_DOCUMENT, 
FIB_BASE_LEN, Integer.MAX_VALUE);
@@ -198,6 +243,7 @@ public abstract class HWPFDocumentCore extends POIDocument {
         }
         _objectPool = new ObjectPoolImpl(objectPoolEntry);
     }
+
     /**
      * Returns the range which covers the whole of the document, but excludes
      * any headers and footers.
@@ -292,12 +338,13 @@ public abstract class HWPFDocumentCore extends 
POIDocument {
         }
         dec.setChunkSize(RC4_REKEYING_INTERVAL);
         try {
-            String pass = Biff8EncryptionKey.getCurrentUserPassword();
+            String pass = _password == null
+                ? Biff8EncryptionKey.getCurrentUserPassword() : new 
String(_password);
             if (pass == null) {
                 pass = Decryptor.DEFAULT_PASSWORD;
             }
             if (!dec.verifyPassword(pass)) {
-                throw new EncryptedDocumentException("document is encrypted, 
password is invalid - use Biff8EncryptionKey.setCurrentUserPassword() to set 
password before opening");
+                throw new EncryptedDocumentException("document is encrypted, 
password is invalid - use constructor to set the password");
             }
         } catch (GeneralSecurityException e) {
             throw new IOException(e.getMessage(), e);
@@ -310,7 +357,8 @@ public abstract class HWPFDocumentCore extends POIDocument {
         // make sure, that we've read all the streams ...
         readProperties();
         // now check for the password
-        String password = Biff8EncryptionKey.getCurrentUserPassword();
+        String password = _password == null
+                ? Biff8EncryptionKey.getCurrentUserPassword() : new 
String(_password);
         FibBase fBase = _fib.getFibBase();
         if (password == null) {
             fBase.setLKey(0);
diff --git a/test-data/poi-integration-exceptions.csv 
b/test-data/poi-integration-exceptions.csv
index 429f070de3..d2f0336e25 100644
--- a/test-data/poi-integration-exceptions.csv
+++ b/test-data/poi-integration-exceptions.csv
@@ -1,8 +1,8 @@
 File,Tests,Handler,Password,Exception Class,Exception Message,Comment
 
spreadsheet/51832.xls,handle,HSSF,,org.apache.poi.EncryptedDocumentException,Default
 password is invalid for salt/verifier/verifierHash,
 
spreadsheet/51832.xls,extract,"HSSF,HPSF",,org.apache.poi.EncryptedDocumentException,Default
 password is invalid for salt/verifier/verifierHash,
-document/PasswordProtected.doc,handle,HWPF,,org.apache.poi.EncryptedDocumentException,"document
 is encrypted, password is invalid - use 
Biff8EncryptionKey.setCurrentUserPassword() to set password before opening",
-document/PasswordProtected.doc,extract,"HWPF,HPSF",,org.apache.poi.EncryptedDocumentException,"document
 is encrypted, password is invalid - use 
Biff8EncryptionKey.setCurrentUserPassword() to set password before opening",
+document/PasswordProtected.doc,handle,HWPF,,org.apache.poi.EncryptedDocumentException,"document
 is encrypted, password is invalid - use constructor to set the password",
+document/PasswordProtected.doc,extract,"HWPF,HPSF",,org.apache.poi.EncryptedDocumentException,"document
 is encrypted, password is invalid - use constructor to set the password",
 
spreadsheet/CustomXMLMapping-singleattributenamespace.xlsx,handle,XSSF,,org.xml.sax.SAXParseException,access
 is not allowed,Oracle/OpenJDK and IBM-JDK error differs slightly
 
spreadsheet/55864.xlsx,handle,XSSF,,org.xml.sax.SAXParseException,cvc-complex-type.2.4.b:
 The content of element 'PersonData' is not complete. One of '{FirstName}' is 
expected.,
 
spreadsheet/57890.xlsx,handle,XSSF,,org.xml.sax.SAXParseException,cvc-complex-type.2.4.b:
 The content of element 'TestData' is not complete. One of '{Date}' is 
expected.,


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to