/// <summary> /// This constructor loads a Word document from a specific point in a POIFSFileSystem, probably not the default.Used typically to open embeded documents. /// </summary> /// <param name="directory">The POIFSFileSystem that Contains the Word document.</param> /// <param name="pfilesystem">If there is an unexpected IOException from the passed in POIFSFileSystem.</param> public HWPFDocumentCore(DirectoryNode directory) : base(directory) { // Sort out the hpsf properties // read in the main stream. DocumentEntry documentProps = (DocumentEntry) directory.GetEntry(STREAM_WORD_DOCUMENT); _mainStream = new byte[documentProps.Size]; directory.CreatePOIFSDocumentReader(STREAM_WORD_DOCUMENT).Read(_mainStream); // Create our FIB, and check for the doc being encrypted _fib = new FileInformationBlock(_mainStream); if (_fib.IsFEncrypted()) { throw new EncryptedDocumentException("Cannot process encrypted word files!"); } DirectoryEntry objectPoolEntry; try { objectPoolEntry = (DirectoryEntry)directory.GetEntry(STREAM_OBJECT_POOL); } catch (FileNotFoundException exc) { objectPoolEntry = null; } _objectPool = new ObjectPoolImpl(objectPoolEntry); }
public void TestNonEmptyConstructor() { DirectoryProperty property1 = new DirectoryProperty("parent"); DirectoryProperty property2 = new DirectoryProperty("child1"); property1.AddChild(property2); property1.AddChild(new DocumentProperty("child2", 2000)); property2.AddChild(new DocumentProperty("child3", 30000)); DirectoryNode node = new DirectoryNode(property1, new POIFSFileSystem(), null); // Verify that GetEntries behaves correctly int count = 0; IEnumerator iter = node.Entries; while (iter.MoveNext()) { count++; //iter.Current; } Assert.AreEqual(2, count); // Verify behavior of IsEmpty Assert.IsTrue(!node.IsEmpty); // Verify behavior of EntryCount Assert.AreEqual(2, node.EntryCount); // Verify behavior of Entry DirectoryNode child1 = (DirectoryNode)node.GetEntry("child1"); child1.GetEntry("child3"); node.GetEntry("child2"); try { node.GetEntry("child3"); Assert.Fail("Should have caught FileNotFoundException"); } catch (FileNotFoundException) { // as expected } // Verify behavior of isDirectoryEntry Assert.IsTrue(node.IsDirectoryEntry); // Verify behavior of GetName Assert.AreEqual(property1.Name, node.Name); // Verify behavior of isDocumentEntry Assert.IsTrue(!node.IsDocumentEntry); // Verify behavior of GetParent Assert.IsNull(node.Parent); }
/** * Decrypt the Document-/SummaryInformation and other optionally streams. * Opposed to other crypto modes, cryptoapi is record based and can't be used * to stream-decrypt a whole file * * @see <a href="http://msdn.microsoft.com/en-us/library/dd943321(v=office.12).aspx">2.3.5.4 RC4 CryptoAPI Encrypted Summary Stream</a> */ public override InputStream GetDataStream(DirectoryNode dir) { NPOIFSFileSystem fsOut = new NPOIFSFileSystem(); DocumentNode es = (DocumentNode)dir.GetEntry("EncryptedSummary"); DocumentInputStream dis = dir.CreateDocumentInputStream(es); MemoryStream bos = new MemoryStream(); IOUtils.Copy(dis, bos); dis.Close(); SeekableMemoryStream sbis = new SeekableMemoryStream(bos.ToArray()); LittleEndianInputStream leis = new LittleEndianInputStream(sbis); int streamDescriptorArrayOffset = (int)leis.ReadUInt(); int streamDescriptorArraySize = (int)leis.ReadUInt(); sbis.Seek(streamDescriptorArrayOffset - 8, SeekOrigin.Current);// sbis.Skip(streamDescriptorArrayOffset - 8); sbis.SetBlock(0); int encryptedStreamDescriptorCount = (int)leis.ReadUInt(); StreamDescriptorEntry[] entries = new StreamDescriptorEntry[encryptedStreamDescriptorCount]; for (int i = 0; i < encryptedStreamDescriptorCount; i++) { StreamDescriptorEntry entry = new StreamDescriptorEntry(); entries[i] = entry; entry.streamOffset = (int)leis.ReadUInt(); entry.streamSize = (int)leis.ReadUInt(); entry.block = leis.ReadUShort(); int nameSize = leis.ReadUByte(); entry.flags = leis.ReadUByte(); bool IsStream = StreamDescriptorEntry.flagStream.IsSet(entry.flags); entry.reserved2 = leis.ReadInt(); entry.streamName = StringUtil.ReadUnicodeLE(leis, nameSize); leis.ReadShort(); Debug.Assert(entry.streamName.Length == nameSize); } foreach (StreamDescriptorEntry entry in entries) { sbis.Seek(entry.streamOffset); sbis.SetBlock(entry.block); Stream is1 = new BufferedStream(sbis, entry.streamSize); fsOut.CreateDocument(is1, entry.streamName); } leis.Close(); sbis = null; bos.Seek(0, SeekOrigin.Begin); //bos.Reset(); fsOut.WriteFileSystem(bos); fsOut.Close(); _length = bos.Length; ByteArrayInputStream bis = new ByteArrayInputStream(bos.ToArray()); throw new NotImplementedException("ByteArrayInputStream should be derived from InputStream"); }
public void TestEmptyConstructor() { POIFSFileSystem fs = new POIFSFileSystem(); DirectoryProperty property1 = new DirectoryProperty("parent"); DirectoryProperty property2 = new DirectoryProperty("child"); DirectoryNode parent = new DirectoryNode(property1, fs, null); DirectoryNode node = new DirectoryNode(property2, fs, parent); Assert.AreEqual(0, parent.Path.Length); Assert.AreEqual(1, node.Path.Length); Assert.AreEqual("child", node.Path.GetComponent(0)); // Verify that GetEntries behaves correctly int count = 0; IEnumerator iter = node.Entries; while (iter.MoveNext()) { count++; //iter.Current; } Assert.AreEqual(0, count); // Verify behavior of IsEmpty Assert.IsTrue(node.IsEmpty); // Verify behavior of EntryCount Assert.AreEqual(0, node.EntryCount); // Verify behavior of Entry try { node.GetEntry("foo"); Assert.Fail("Should have caught FileNotFoundException"); } catch (FileNotFoundException) { // as expected } // Verify behavior of isDirectoryEntry Assert.IsTrue(node.IsDirectoryEntry); // Verify behavior of GetName Assert.AreEqual(property2.Name, node.Name); // Verify behavior of isDocumentEntry Assert.IsTrue(!node.IsDocumentEntry); // Verify behavior of GetParent Assert.AreEqual(parent, node.Parent); }
private void Open(DirectoryNode directory) { DocumentNode book; try { book = (DocumentNode)directory.GetEntry(InternalWorkbook.OLD_WORKBOOK_DIR_ENTRY_NAME); } catch (FileNotFoundException) { // some files have "Workbook" instead book = (DocumentNode)directory.GetEntry(InternalWorkbook.WORKBOOK_DIR_ENTRY_NAMES[0]); } if (book == null) { throw new IOException("No Excel 5/95 Book stream found"); } ris = new RecordInputStream(directory.CreateDocumentInputStream(book)); Prepare(); }
private bool HasEntry(DirectoryNode dirNode, String entryName) { try { dirNode.GetEntry(entryName); return(true); } catch (FileNotFoundException) { return(false); } }
private void Open(DirectoryNode directory) { DocumentNode book = (DocumentNode)directory.GetEntry("Book"); if (book == null) { throw new IOException("No Excel 5/95 Book stream found"); } ris = new RecordInputStream(directory.CreateDocumentInputStream(book)); Prepare(); }
public void TestReadMultipleTreeLevels() { POIDataSamples _samples = POIDataSamples.GetPublisherInstance(); FileStream sample = _samples.GetFile("Sample.pub"); DocumentInputStream stream; NPOIFSFileSystem npoifs = new NPOIFSFileSystem(sample); try { sample = _samples.GetFile("Sample.pub"); OPOIFSFileSystem opoifs = new OPOIFSFileSystem(sample); // Ensure we have what we expect on the root Assert.AreEqual(npoifs, npoifs.Root.NFileSystem); Assert.AreEqual(npoifs, npoifs.Root.FileSystem); Assert.AreEqual(null, npoifs.Root.OFileSystem); Assert.AreEqual(null, opoifs.Root.FileSystem); Assert.AreEqual(opoifs, opoifs.Root.OFileSystem); Assert.AreEqual(null, opoifs.Root.NFileSystem); // Check inside foreach (DirectoryNode root in new DirectoryNode[] { opoifs.Root, npoifs.Root }) { // Top Level Entry top = root.GetEntry("Contents"); Assert.AreEqual(true, top.IsDocumentEntry); stream = root.CreateDocumentInputStream(top); stream.Read(); // One Level Down DirectoryNode escher = (DirectoryNode)root.GetEntry("Escher"); Entry one = escher.GetEntry("EscherStm"); Assert.AreEqual(true, one.IsDocumentEntry); stream = escher.CreateDocumentInputStream(one); stream.Read(); // Two Levels Down DirectoryNode quill = (DirectoryNode)root.GetEntry("Quill"); DirectoryNode quillSub = (DirectoryNode)quill.GetEntry("QuillSub"); Entry two = quillSub.GetEntry("CONTENTS"); Assert.AreEqual(true, two.IsDocumentEntry); stream = quillSub.CreateDocumentInputStream(two); stream.Read(); } } finally { npoifs.Close(); } }
public void TestExtractFromEmbeded() { POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.GetSpreadSheetInstance().OpenResourceAsStream(filename3)); HWPFDocument doc; WordExtractor extractor3; DirectoryNode dirA = (DirectoryNode)fs.Root.GetEntry("MBD0000A3B7"); DirectoryNode dirB = (DirectoryNode)fs.Root.GetEntry("MBD0000A3B2"); // Should have WordDocument and 1Table Assert.IsNotNull(dirA.GetEntry("1Table")); Assert.IsNotNull(dirA.GetEntry("WordDocument")); Assert.IsNotNull(dirB.GetEntry("1Table")); Assert.IsNotNull(dirB.GetEntry("WordDocument")); // Check each in turn doc = new HWPFDocument(dirA, fs); extractor3 = new WordExtractor(doc); Assert.IsNotNull(extractor3.Text); Assert.IsTrue(extractor3.Text.Length > 20); Assert.AreEqual("I am a sample document\r\nNot much on me\r\nI am document 1\r\n", extractor3 .Text); Assert.AreEqual("Sample Doc 1", extractor3.SummaryInformation.Title); Assert.AreEqual("Sample Test", extractor3.SummaryInformation.Subject); doc = new HWPFDocument(dirB, fs); extractor3 = new WordExtractor(doc); Assert.IsNotNull(extractor3.Text); Assert.IsTrue(extractor3.Text.Length > 20); Assert.AreEqual("I am another sample document\r\nNot much on me\r\nI am document 2\r\n", extractor3.Text); Assert.AreEqual("Sample Doc 2", extractor3.SummaryInformation.Title); Assert.AreEqual("Another Sample Test", extractor3.SummaryInformation.Subject); }
public void TestWithEmbeded() { POIFSFileSystem fs = new POIFSFileSystem( POIDataSamples.GetDocumentInstance().OpenResourceAsStream("word_with_embeded.doc") ); DirectoryNode objPool = (DirectoryNode)fs.Root.GetEntry("ObjectPool"); DirectoryNode dirA = (DirectoryNode)objPool.GetEntry("_1269427460"); DirectoryNode dirB = (DirectoryNode)objPool.GetEntry("_1269427461"); HSSFWorkbook wbA = new HSSFWorkbook(dirA, fs, true); HSSFWorkbook wbB = new HSSFWorkbook(dirB, fs, true); ExcelExtractor exA = new ExcelExtractor(wbA); ExcelExtractor exB = new ExcelExtractor(wbB); Assert.AreEqual("Sheet1\nTest excel file\nThis is the first file\nSheet2\nSheet3\n", exA.Text); Assert.AreEqual("Sample Excel", exA.SummaryInformation.Title); Assert.AreEqual("Sheet1\nAnother excel file\nThis is the second file\nSheet2\nSheet3\n", exB.Text); Assert.AreEqual("Sample Excel 2", exB.SummaryInformation.Title); }
/// <summary> /// Creates an instance of this class from an embedded OLE Object. The OLE Object is expected /// to include a stream "{01}Ole10Native" which contains the actual /// data relevant for this class. /// </summary> /// <param name="directory">directory POI Filesystem object</param> /// <returns>Returns an instance of this class</returns> public static Ole10Native CreateFromEmbeddedOleObject(DirectoryNode directory) { DocumentEntry nativeEntry = (DocumentEntry)directory.GetEntry(OLE10_NATIVE); byte[] data = new byte[nativeEntry.Size]; directory.CreateDocumentInputStream(nativeEntry).Read(data); return new Ole10Native(data, 0); }
/// <summary> /// Initializes a new instance of the <see cref="HWPFDocument"/> class. /// </summary> /// <param name="directory">The directory.</param> public HWPFDocument(DirectoryNode directory) : base(directory) { _endnotes = new NotesImpl(_endnotesTables); _footnotes = new NotesImpl(_footnotesTables); // Load the main stream and FIB // Also handles HPSF bits // Do the CP Split _cpSplit = new CPSplitCalculator(_fib); // Is this document too old for us? if (_fib.GetNFib() < 106) { throw new OldWordFileFormatException("The document is too old - Word 95 or older. Try HWPFOldDocument instead?"); } // use the fib to determine the name of the table stream. String name = "0Table"; if (_fib.IsFWhichTblStm()) { name = "1Table"; } // Grab the table stream. DocumentEntry tableProps; try { tableProps = (DocumentEntry)directory.GetEntry(name); } catch (FileNotFoundException) { throw new InvalidOperationException("Table Stream '" + name + "' wasn't found - Either the document is corrupt, or is Word95 (or earlier)"); } // read in the table stream. _tableStream = new byte[tableProps.Size]; directory.CreatePOIFSDocumentReader(name).Read(_tableStream); _fib.FillVariableFields(_mainStream, _tableStream); // read in the data stream. try { DocumentEntry dataProps = (DocumentEntry)directory.GetEntry("Data"); _dataStream = new byte[dataProps.Size]; directory.CreatePOIFSDocumentReader("Data").Read(_dataStream); } catch (FileNotFoundException) { _dataStream = new byte[0]; } // Get the cp of the start of text in the main stream // The latest spec doc says this is always zero! int fcMin = 0; //fcMin = _fib.GetFcMin() // Start to load up our standard structures. _dop = new DocumentProperties(_tableStream, _fib.GetFcDop()); _cft = new ComplexFileTable(_mainStream, _tableStream, _fib.GetFcClx(), fcMin); TextPieceTable _tpt = _cft.GetTextPieceTable(); // Now load the rest of the properties, which need to be adjusted // for where text really begin _cbt = new CHPBinTable(_mainStream, _tableStream, _fib.GetFcPlcfbteChpx(), _fib.GetLcbPlcfbteChpx(), _tpt); _pbt = new PAPBinTable(_mainStream, _tableStream, _dataStream, _fib.GetFcPlcfbtePapx(), _fib.GetLcbPlcfbtePapx(), _tpt); _text = _tpt.Text; /* * in this mode we preserving PAPX/CHPX structure from file, so text may * miss from output, and text order may be corrupted */ bool preserveBinTables = false; try { preserveBinTables = Boolean.Parse( ConfigurationManager.AppSettings[PROPERTY_PRESERVE_BIN_TABLES]); } catch (Exception) { // ignore; } if (!preserveBinTables) { _cbt.Rebuild(_cft); _pbt.Rebuild(_text, _cft); } /* * Property to disable text rebuilding. In this mode changing the text * will lead to unpredictable behavior */ bool preserveTextTable = false; try { preserveTextTable = Boolean.Parse( ConfigurationManager.AppSettings[PROPERTY_PRESERVE_TEXT_TABLE]); } catch (Exception) { // ignore; } if (!preserveTextTable) { _cft = new ComplexFileTable(); _tpt = _cft.GetTextPieceTable(); TextPiece textPiece = new SinglentonTextPiece(_text); _tpt.Add(textPiece); _text = textPiece.GetStringBuilder(); } // Read FSPA and Escher information // _fspa = new FSPATable(_tableStream, _fib.getFcPlcspaMom(), // _fib.getLcbPlcspaMom(), getTextTable().getTextPieces()); _fspaHeaders = new FSPATable(_tableStream, _fib, FSPADocumentPart.HEADER); _fspaMain = new FSPATable(_tableStream, _fib, FSPADocumentPart.MAIN); if (_fib.GetFcDggInfo() != 0) { _dgg = new EscherRecordHolder(_tableStream, _fib.GetFcDggInfo(), _fib.GetLcbDggInfo()); } else { _dgg = new EscherRecordHolder(); } // read in the pictures stream _pictures = new PicturesTable(this, _dataStream, _mainStream, _fspa, _dgg); // And the art shapes stream _officeArts = new ShapesTable(_tableStream, _fib); // And escher pictures _officeDrawingsHeaders = new OfficeDrawingsImpl(_fspaHeaders, _dgg, _mainStream); _officeDrawingsMain = new OfficeDrawingsImpl(_fspaMain, _dgg, _mainStream); _st = new SectionTable(_mainStream, _tableStream, _fib.GetFcPlcfsed(), _fib.GetLcbPlcfsed(), fcMin, _tpt, _cpSplit); _ss = new StyleSheet(_tableStream, _fib.GetFcStshf()); _ft = new FontTable(_tableStream, _fib.GetFcSttbfffn(), _fib.GetLcbSttbfffn()); int listOffset = _fib.GetFcPlcfLst(); int lfoOffset = _fib.GetFcPlfLfo(); if (listOffset != 0 && _fib.GetLcbPlcfLst() != 0) { _lt = new ListTables(_tableStream, _fib.GetFcPlcfLst(), _fib.GetFcPlfLfo()); } int sbtOffset = _fib.GetFcSttbSavedBy(); int sbtLength = _fib.GetLcbSttbSavedBy(); if (sbtOffset != 0 && sbtLength != 0) { _sbt = new SavedByTable(_tableStream, sbtOffset, sbtLength); } int rmarkOffset = _fib.GetFcSttbfRMark(); int rmarkLength = _fib.GetLcbSttbfRMark(); if (rmarkOffset != 0 && rmarkLength != 0) { _rmat = new RevisionMarkAuthorTable(_tableStream, rmarkOffset, rmarkLength); } _bookmarksTables = new BookmarksTables(_tableStream, _fib); _bookmarks = new BookmarksImpl(_bookmarksTables); _endnotesTables = new NotesTables(NoteType.ENDNOTE, _tableStream, _fib); _endnotes = new NotesImpl(_endnotesTables); _footnotesTables = new NotesTables(NoteType.FOOTNOTE, _tableStream, _fib); _footnotes = new NotesImpl(_footnotesTables); _fieldsTables = new FieldsTables(_tableStream, _fib); _fields = new FieldsImpl(_fieldsTables); }
/** * Adds a new OLE Package Shape * * @param anchor the client anchor describes how this picture is * attached to the sheet. * @param storageId the storageId returned by {@Link HSSFWorkbook.AddOlePackage} * @param pictureIndex the index of the picture (used as preview image) in the * workbook collection of pictures. * * @return newly Created shape */ public HSSFObjectData CreateObjectData(HSSFClientAnchor anchor, int storageId, int pictureIndex) { ObjRecord obj = new ObjRecord(); CommonObjectDataSubRecord ftCmo = new CommonObjectDataSubRecord(); ftCmo.ObjectType = (/*setter*/ CommonObjectType.Picture); // ftCmo.ObjectId=(/*setter*/oleShape.ShapeId); ... will be Set by onCreate(...) ftCmo.IsLocked = (/*setter*/ true); ftCmo.IsPrintable = (/*setter*/ true); ftCmo.IsAutoFill = (/*setter*/ true); ftCmo.IsAutoline = (/*setter*/ true); ftCmo.Reserved1 = (/*setter*/ 0); ftCmo.Reserved2 = (/*setter*/ 0); ftCmo.Reserved3 = (/*setter*/ 0); obj.AddSubRecord(ftCmo); // FtCf (pictFormat) FtCfSubRecord ftCf = new FtCfSubRecord(); HSSFPictureData pictData = Sheet.Workbook.GetAllPictures()[(pictureIndex - 1)] as HSSFPictureData; switch ((PictureType)pictData.Format) { case PictureType.WMF: case PictureType.EMF: // this needs patch #49658 to be applied to actually work ftCf.Flags = (/*setter*/ FtCfSubRecord.METAFILE_BIT); break; case PictureType.DIB: case PictureType.PNG: case PictureType.JPEG: case PictureType.PICT: ftCf.Flags = (/*setter*/ FtCfSubRecord.BITMAP_BIT); break; } obj.AddSubRecord(ftCf); // FtPioGrbit (pictFlags) FtPioGrbitSubRecord ftPioGrbit = new FtPioGrbitSubRecord(); ftPioGrbit.SetFlagByBit(FtPioGrbitSubRecord.AUTO_PICT_BIT, true); obj.AddSubRecord(ftPioGrbit); EmbeddedObjectRefSubRecord ftPictFmla = new EmbeddedObjectRefSubRecord(); ftPictFmla.SetUnknownFormulaData(new byte[] { 2, 0, 0, 0, 0 }); ftPictFmla.OLEClassName = (/*setter*/ "Paket"); ftPictFmla.SetStorageId(storageId); obj.AddSubRecord(ftPictFmla); obj.AddSubRecord(new EndSubRecord()); String entryName = "MBD" + HexDump.ToHex(storageId); DirectoryEntry oleRoot; try { DirectoryNode dn = (_sheet.Workbook as HSSFWorkbook).RootDirectory; if (dn == null) { throw new FileNotFoundException(); } oleRoot = (DirectoryEntry)dn.GetEntry(entryName); } catch (FileNotFoundException e) { throw new InvalidOperationException("trying to add ole shape without actually Adding data first - use HSSFWorkbook.AddOlePackage first", e); } // create picture shape, which need to be minimal modified for oleshapes HSSFPicture shape = new HSSFPicture(null, anchor); shape.PictureIndex = (/*setter*/ pictureIndex); EscherContainerRecord spContainer = shape.GetEscherContainer(); EscherSpRecord spRecord = spContainer.GetChildById(EscherSpRecord.RECORD_ID) as EscherSpRecord; spRecord.Flags = (/*setter*/ spRecord.Flags | EscherSpRecord.FLAG_OLESHAPE); HSSFObjectData oleShape = new HSSFObjectData(spContainer, obj, oleRoot); AddShape(oleShape); OnCreate(oleShape); return(oleShape); }
public void TestBATandXBAT() { byte[] hugeStream = new byte[8 * 1024 * 1024]; OPOIFSFileSystem fs = new OPOIFSFileSystem(); fs.Root.CreateDocument("BIG", new MemoryStream(hugeStream)); MemoryStream baos = new MemoryStream(); fs.WriteFileSystem(baos); byte[] fsData = baos.ToArray(); // Check the header was written properly Stream inp = new MemoryStream(fsData); HeaderBlock header = new HeaderBlock(inp); Assert.AreEqual(109 + 21, header.BATCount); Assert.AreEqual(1, header.XBATCount); ByteBuffer xbatData = ByteBuffer.CreateBuffer(512); xbatData.Write(fsData, (1 + header.XBATIndex) * 512, 512); xbatData.Position = 0; BATBlock xbat = BATBlock.CreateBATBlock(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, xbatData); for (int i = 0; i < 21; i++) { Assert.IsTrue(xbat.GetValueAt(i) != POIFSConstants.UNUSED_BLOCK); } for (int i = 21; i < 127; i++) { Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, xbat.GetValueAt(i)); } Assert.AreEqual(POIFSConstants.END_OF_CHAIN, xbat.GetValueAt(127)); RawDataBlockList blockList = new RawDataBlockList(inp, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS); Assert.AreEqual(fsData.Length / 512, blockList.BlockCount() + 1); new BlockAllocationTableReader(header.BigBlockSize, header.BATCount, header.BATArray, header.XBATCount, header.XBATIndex, blockList); Assert.AreEqual(fsData.Length / 512, blockList.BlockCount() + 1); fs = null; fs = new OPOIFSFileSystem(new MemoryStream(fsData)); DirectoryNode root = fs.Root; Assert.AreEqual(1, root.EntryCount); DocumentNode big = (DocumentNode)root.GetEntry("BIG"); Assert.AreEqual(hugeStream.Length, big.Size); }
private static String GetWorkbookDirEntryName(DirectoryNode directory) { String[] potentialNames = WORKBOOK_DIR_ENTRY_NAMES; for (int i = 0; i < potentialNames.Length; i++) { String wbName = potentialNames[i]; try { directory.GetEntry(wbName); return wbName; } catch (FileNotFoundException) { // continue - to try other options } } // Check for previous version of file format try { directory.GetEntry("Book"); throw new OldExcelFormatException("The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. " + "POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)"); } catch (FileNotFoundException) { // fall through } throw new ArgumentException("The supplied POIFSFileSystem does not contain a BIFF8 'Workbook' entry. " + "Is it really an excel file?"); }
/// <summary> /// For a given named property entry, either return it or null if /// if it wasn't found /// </summary> /// <param name="setName">The property to read</param> /// <param name="encryptionInfo">the encryption descriptor in case of cryptoAPI encryption</param> /// <returns>The value of the given property or null if it wasn't found.</returns> /// <exception cref="IOException">If retrieving properties fails</exception> protected PropertySet GetPropertySet(string setName, EncryptionInfo encryptionInfo) { DirectoryNode dirNode = directory; NPOIFSFileSystem encPoifs = null; String step = "getting"; try { if (encryptionInfo != null) { step = "getting encrypted"; InputStream is1 = encryptionInfo.Decryptor.GetDataStream(directory); try { encPoifs = new NPOIFSFileSystem(is1); dirNode = encPoifs.Root; } finally { is1.Close(); } } //directory can be null when creating new documents if (dirNode == null || !dirNode.HasEntry(setName)) { return(null); } // Find the entry, and get an input stream for it step = "getting"; DocumentInputStream dis = dirNode.CreateDocumentInputStream(dirNode.GetEntry(setName)); try { // Create the Property Set step = "creating"; return(PropertySetFactory.Create(dis)); } finally { dis.Close(); } } catch (Exception e) { logger.Log(POILogger.WARN, "Error " + step + " property set with name " + setName, e); return(null); } finally { if (encPoifs != null) { try { encPoifs.Close(); } catch (IOException e) { logger.Log(POILogger.WARN, "Error closing encrypted property poifs", e); } } } }
/** * Encrypt the Document-/SummaryInformation and other optionally streams. * Opposed to other crypto modes, cryptoapi is record based and can't be used * to stream-encrypt a whole file * * @see <a href="http://msdn.microsoft.com/en-us/library/dd943321(v=office.12).aspx">2.3.5.4 RC4 CryptoAPI Encrypted Summary Stream</a> */ public override OutputStream GetDataStream(DirectoryNode dir) { CipherByteArrayOutputStream bos = new CipherByteArrayOutputStream(this); byte[] buf = new byte[8]; bos.Write(buf, 0, 8); // skip header String[] entryNames = { SummaryInformation.DEFAULT_STREAM_NAME, DocumentSummaryInformation.DEFAULT_STREAM_NAME }; List <CryptoAPIDecryptor.StreamDescriptorEntry> descList = new List <CryptoAPIDecryptor.StreamDescriptorEntry>(); int block = 0; foreach (String entryName in entryNames) { if (!dir.HasEntry(entryName)) { continue; } CryptoAPIDecryptor.StreamDescriptorEntry descEntry = new CryptoAPIDecryptor.StreamDescriptorEntry(); descEntry.block = block; descEntry.streamOffset = (int)bos.Length; descEntry.streamName = entryName; descEntry.flags = CryptoAPIDecryptor.StreamDescriptorEntry.flagStream.SetValue(0, 1); descEntry.reserved2 = 0; bos.SetBlock(block); DocumentInputStream dis = dir.CreateDocumentInputStream(entryName); IOUtils.Copy(dis, bos); dis.Close(); descEntry.streamSize = (int)(bos.Length - descEntry.streamOffset); descList.Add(descEntry); dir.GetEntry(entryName).Delete(); block++; } int streamDescriptorArrayOffset = (int)bos.Length; bos.SetBlock(0); LittleEndian.PutUInt(buf, 0, descList.Count); bos.Write(buf, 0, 4); foreach (CryptoAPIDecryptor.StreamDescriptorEntry sde in descList) { LittleEndian.PutUInt(buf, 0, sde.streamOffset); bos.Write(buf, 0, 4); LittleEndian.PutUInt(buf, 0, sde.streamSize); bos.Write(buf, 0, 4); LittleEndian.PutUShort(buf, 0, sde.block); bos.Write(buf, 0, 2); LittleEndian.PutUByte(buf, 0, (short)sde.streamName.Length); bos.Write(buf, 0, 1); LittleEndian.PutUByte(buf, 0, (short)sde.flags); bos.Write(buf, 0, 1); LittleEndian.PutUInt(buf, 0, sde.reserved2); bos.Write(buf, 0, 4); byte[] nameBytes = StringUtil.GetToUnicodeLE(sde.streamName); bos.Write(nameBytes, 0, nameBytes.Length); LittleEndian.PutShort(buf, 0, (short)0); // null-termination bos.Write(buf, 0, 2); } int savedSize = (int)bos.Length; int streamDescriptorArraySize = savedSize - streamDescriptorArrayOffset; LittleEndian.PutUInt(buf, 0, streamDescriptorArrayOffset); LittleEndian.PutUInt(buf, 4, streamDescriptorArraySize); bos.Reset(); bos.SetBlock(0); bos.Write(buf, 0, 8); bos.SetSize(savedSize); dir.CreateDocument("EncryptedSummary", new MemoryStream(bos.GetBuf(), 0, savedSize)); DocumentSummaryInformation dsi = PropertySetFactory.NewDocumentSummaryInformation(); try { dsi.Write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME); } catch (WritingNotSupportedException e) { throw new IOException(e.Message); } //return bos; throw new NotImplementedException("CipherByteArrayOutputStream should be derived from OutputStream"); }
public void TestEmptyConstructor() { POIFSFileSystem fs = new POIFSFileSystem(); DirectoryProperty property1 = new DirectoryProperty("parent"); DirectoryProperty property2 = new DirectoryProperty("child"); DirectoryNode parent = new DirectoryNode(property1, fs, null); DirectoryNode node = new DirectoryNode(property2, fs, parent); Assert.AreEqual(0, parent.Path.Length); Assert.AreEqual(1, node.Path.Length); Assert.AreEqual("child", node.Path.GetComponent(0)); // Verify that GetEntries behaves correctly int count = 0; IEnumerator iter = node.Entries; while (iter.MoveNext()) { count++; } Assert.AreEqual(0, count); // Verify behavior of IsEmpty Assert.IsTrue(node.IsEmpty); // Verify behavior of EntryCount Assert.AreEqual(0, node.EntryCount); // Verify behavior of Entry try { node.GetEntry("foo"); Assert.Fail("Should have caught FileNotFoundException"); } catch (FileNotFoundException ) { // as expected } // Verify behavior of isDirectoryEntry Assert.IsTrue(node.IsDirectoryEntry); // Verify behavior of GetName Assert.AreEqual(property2.Name, node.Name); // Verify behavior of isDocumentEntry Assert.IsTrue(!node.IsDocumentEntry); // Verify behavior of GetParent Assert.AreEqual(parent, node.Parent); }
public void TestNonEmptyConstructor() { DirectoryProperty property1 = new DirectoryProperty("parent"); DirectoryProperty property2 = new DirectoryProperty("child1"); property1.AddChild(property2); property1.AddChild(new DocumentProperty("child2", 2000)); property2.AddChild(new DocumentProperty("child3", 30000)); DirectoryNode node = new DirectoryNode(property1, new POIFSFileSystem(), null); // Verify that GetEntries behaves correctly int count = 0; IEnumerator iter = node.Entries; while (iter.MoveNext()) { count++; //iter.Current; } Assert.AreEqual(2, count); // Verify behavior of IsEmpty Assert.IsTrue(!node.IsEmpty); // Verify behavior of EntryCount Assert.AreEqual(2, node.EntryCount); // Verify behavior of Entry DirectoryNode child1 = (DirectoryNode)node.GetEntry("child1"); child1.GetEntry("child3"); node.GetEntry("child2"); try { node.GetEntry("child3"); Assert.Fail("Should have caught FileNotFoundException"); } catch (FileNotFoundException) { // as expected } // Verify behavior of isDirectoryEntry Assert.IsTrue(node.IsDirectoryEntry); // Verify behavior of GetName Assert.AreEqual(property1.Name, node.Name); // Verify behavior of isDocumentEntry Assert.IsTrue(!node.IsDocumentEntry); // Verify behavior of GetParent Assert.IsNull(node.Parent); }