Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java?rev=1798722&r1=1798721&r2=1798722&view=diff ============================================================================== --- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java (original) +++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java Wed Jun 14 17:21:50 2017 @@ -18,7 +18,6 @@ package org.apache.poi.hwpf; import java.io.ByteArrayOutputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.PushbackInputStream; @@ -26,7 +25,6 @@ import java.security.GeneralSecurityExce import org.apache.poi.EncryptedDocumentException; import org.apache.poi.POIDocument; -import org.apache.poi.hpsf.PropertySet; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hwpf.model.CHPBinTable; import org.apache.poi.hwpf.model.FibBase; @@ -44,6 +42,7 @@ import org.apache.poi.poifs.crypt.Chunke import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.crypt.EncryptionMode; +import org.apache.poi.poifs.crypt.Encryptor; import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DocumentEntry; @@ -67,7 +66,17 @@ public abstract class HWPFDocumentCore e protected static final String STREAM_TABLE_0 = "0Table"; protected static final String STREAM_TABLE_1 = "1Table"; - private static final int FIB_BASE_LEN = 68; + /** + * Size of the not encrypted part of the FIB + */ + protected static final int FIB_BASE_LEN = 68; + + /** + * [MS-DOC] 2.2.6.2/3 Office Binary Document ... Encryption: + * "... The block number MUST be set to zero at the beginning of the stream and + * MUST be incremented at each 512 byte boundary. ..." + */ + protected static final int RC4_REKEYING_INTERVAL = 512; /** Holds OLE2 objects */ protected ObjectPoolImpl _objectPool; @@ -171,35 +180,85 @@ public abstract class HWPFDocumentCore e } _objectPool = new ObjectPoolImpl(objectPoolEntry); } + /** + * Returns the range which covers the whole of the document, but excludes + * any headers and footers. + */ + public abstract Range getRange(); /** - * For a given named property entry, either return it or null if - * if it wasn't found - * - * @param setName The property to read - * @return The value of the given property or null if it wasn't found. + * Returns the range that covers all text in the file, including main text, + * footnotes, headers and comments */ - @Override - protected PropertySet getPropertySet(String setName) { - EncryptionInfo ei; - try { - ei = getEncryptionInfo(); - } catch (IOException e) { - throw new RuntimeException(e); - } - return (ei == null) - ? super.getPropertySet(setName) - : super.getPropertySet(setName, ei); + public abstract Range getOverallRange(); + + /** + * Returns document text, i.e. text information from all text pieces, + * including OLE descriptions and field codes + */ + public String getDocumentText() { + return getText().toString(); + } + + /** + * Internal method to access document text + */ + @Internal + public abstract StringBuilder getText(); + + public CHPBinTable getCharacterTable() { + return _cbt; } - protected EncryptionInfo getEncryptionInfo() throws IOException { + public PAPBinTable getParagraphTable() { + return _pbt; + } + + public SectionTable getSectionTable() { + return _st; + } + + public StyleSheet getStyleSheet() { + return _ss; + } + + public ListTables getListTables() { + return _lt; + } + + public FontTable getFontTable() { + return _ft; + } + + public FileInformationBlock getFileInformationBlock() { + return _fib; + } + + public ObjectsPool getObjectsPool() { + return _objectPool; + } + + public abstract TextPieceTable getTextTable(); + + @Internal + public byte[] getMainStream() { + return _mainStream; + } + + @Override + public EncryptionInfo getEncryptionInfo() throws IOException { if (_encryptionInfo != null) { return _encryptionInfo; } // Create our FIB, and check for the doc being encrypted - byte[] fibBaseBytes = (_mainStream != null) ? _mainStream : getDocumentEntryBytes(STREAM_WORD_DOCUMENT, -1, FIB_BASE_LEN); - FibBase fibBase = new FibBase( fibBaseBytes, 0 ); + FibBase fibBase; + if (_fib != null && _fib.getFibBase() != null) { + fibBase = _fib.getFibBase(); + } else { + byte[] fibBaseBytes = (_mainStream != null) ? _mainStream : getDocumentEntryBytes(STREAM_WORD_DOCUMENT, -1, FIB_BASE_LEN); + fibBase = new FibBase( fibBaseBytes, 0 ); + } if (!fibBase.isFEncrypted()) { return null; } @@ -210,7 +269,7 @@ public abstract class HWPFDocumentCore e EncryptionMode em = fibBase.isFObfuscated() ? EncryptionMode.xor : null; EncryptionInfo ei = new EncryptionInfo(leis, em); Decryptor dec = ei.getDecryptor(); - dec.setChunkSize(512); + dec.setChunkSize(RC4_REKEYING_INTERVAL); try { String pass = Biff8EncryptionKey.getCurrentUserPassword(); if (pass == null) { @@ -226,6 +285,35 @@ public abstract class HWPFDocumentCore e return ei; } + protected void updateEncryptionInfo() { + // make sure, that we've read all the streams ... + readProperties(); + // now check for the password + String password = Biff8EncryptionKey.getCurrentUserPassword(); + FibBase fBase = _fib.getFibBase(); + if (password == null) { + fBase.setLKey(0); + fBase.setFEncrypted(false); + fBase.setFObfuscated(false); + _encryptionInfo = null; + } else { + // create password record + if (_encryptionInfo == null) { + _encryptionInfo = new EncryptionInfo(EncryptionMode.cryptoAPI); + fBase.setFEncrypted(true); + fBase.setFObfuscated(false); + } + Encryptor enc = _encryptionInfo.getEncryptor(); + byte salt[] = _encryptionInfo.getVerifier().getSalt(); + if (salt == null) { + enc.confirmPassword(password); + } else { + byte verifier[] = _encryptionInfo.getDecryptor().getVerifier(); + enc.confirmPassword(password, null, null, verifier, salt, null); + } + } + } + /** * Reads OLE Stream into byte array - if an {@link EncryptionInfo} is available, * decrypt the bytes starting at encryptionOffset. If encryptionOffset = -1, then do not try @@ -273,70 +361,4 @@ public abstract class HWPFDocumentCore e IOUtils.closeQuietly(dis); } } - - - /** - * Returns the range which covers the whole of the document, but excludes - * any headers and footers. - */ - public abstract Range getRange(); - - /** - * Returns the range that covers all text in the file, including main text, - * footnotes, headers and comments - */ - public abstract Range getOverallRange(); - - /** - * Returns document text, i.e. text information from all text pieces, - * including OLE descriptions and field codes - */ - public String getDocumentText() { - return getText().toString(); - } - - /** - * Internal method to access document text - */ - @Internal - public abstract StringBuilder getText(); - - public CHPBinTable getCharacterTable() { - return _cbt; - } - - public PAPBinTable getParagraphTable() { - return _pbt; - } - - public SectionTable getSectionTable() { - return _st; - } - - public StyleSheet getStyleSheet() { - return _ss; - } - - public ListTables getListTables() { - return _lt; - } - - public FontTable getFontTable() { - return _ft; - } - - public FileInformationBlock getFileInformationBlock() { - return _fib; - } - - public ObjectsPool getObjectsPool() { - return _objectPool; - } - - public abstract TextPieceTable getTextTable(); - - @Internal - public byte[] getMainStream() { - return _mainStream; - } } \ No newline at end of file
Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/io/HWPFFileSystem.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/io/HWPFFileSystem.java?rev=1798722&r1=1798721&r2=1798722&view=diff ============================================================================== --- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/io/HWPFFileSystem.java (original) +++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/io/HWPFFileSystem.java Wed Jun 14 17:21:50 2017 @@ -31,9 +31,9 @@ public final class HWPFFileSystem public HWPFFileSystem() { - _streams.put("WordDocument", new ByteArrayOutputStream()); - _streams.put("1Table", new ByteArrayOutputStream()); - _streams.put("Data", new ByteArrayOutputStream()); + _streams.put("WordDocument", new ByteArrayOutputStream(100000)); + _streams.put("1Table", new ByteArrayOutputStream(100000)); + _streams.put("Data", new ByteArrayOutputStream(100000)); } public ByteArrayOutputStream getStream(String name) --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
