Example #1
0
        /// <summary>
        /// Read from an InputStream and Process the documents we Get
        /// </summary>
        /// <param name="stream">the InputStream from which to Read the data</param>
        /// <returns>POIFSDocument list</returns>
        public List<DocumentDescriptor> Read(Stream stream)
        {

            // Read the header block from the stream
            HeaderBlockReader header_block_Reader = new HeaderBlockReader(stream);

            // Read the rest of the stream into blocks
            RawDataBlockList data_blocks = new RawDataBlockList(stream, header_block_Reader.BigBlockSize);

            // Set up the block allocation table (necessary for the
            // data_blocks to be manageable
            new BlockAllocationTableReader(header_block_Reader.BATCount,
                                           header_block_Reader.BATArray,
                                           header_block_Reader.XBATCount,
                                           header_block_Reader.XBATIndex,
                                           data_blocks);

            // Get property table from the document
            PropertyTable properties =
                new PropertyTable(header_block_Reader.PropertyStart,
                                  data_blocks);

            // Process documents
            return ProcessProperties(SmallBlockTableReader
                .GetSmallDocumentBlocks(data_blocks, properties
                    .Root, header_block_Reader
                        .SBATStart), data_blocks, properties.Root
                            .Children, new POIFSDocumentPath());
        }
        public void TestConstructors()
        {
            string[] hexData = 
        {
                "D0 CF 11 E0 A1 B1 1A E1 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 3B 00 03 00 FE FF 09 00",
                "06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FE FF FF FF 00 00 00 00 00 10 00 00 FE FF FF FF",
                "01 00 00 00 FE FF FF FF 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
                "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF",
        };

            byte[] content = RawDataUtil.Decode(hexData);
            HeaderBlockReader block = new HeaderBlockReader(new MemoryStream(content));

            Assert.AreEqual(-2, block.PropertyStart);

            // verify we can't Read a short block
            byte[] shortblock = new byte[511];

            Array.Copy(content, 0, shortblock, 0, 511);
            try
            {
                block = new HeaderBlockReader(new MemoryStream(shortblock));
                Assert.Fail("Should have caught IOException Reading a short block");
            }
            catch (IOException)
            {

                // as expected
            }

            // try various forms of corruption
            for (int index = 0; index < 8; index++)
            {
                content[index] = (byte)(content[index] - 1);
                try
                {
                    block = new HeaderBlockReader(new MemoryStream(content));
                    Assert.Fail("Should have caught IOException corrupting byte " + index);
                }
                catch (IOException)
                {

                    // as expected
                }

                // restore byte value
                content[index] = (byte)(content[index] + 1);
            }
        }
Example #3
0
        /// <summary>
        /// Create a POIFSFileSystem from an Stream. Normally the stream is Read until
        /// EOF.  The stream is always Closed.  In the unlikely case that the caller has such a stream and
        /// needs to use it after this constructor completes, a work around is to wrap the
        /// stream in order to trap the Close() call.  
        /// </summary>
        /// <param name="stream">the Streamfrom which to Read the data</param>
        public POIFSFileSystem(Stream stream):this()
        {
            bool success = false;

            HeaderBlockReader header_block_Reader;
            RawDataBlockList data_blocks;
            try {
                // Read the header block from the stream
                header_block_Reader = new HeaderBlockReader(stream);
                bigBlockSize = header_block_Reader.BigBlockSize;
                
                // Read the rest of the stream into blocks
                data_blocks = new RawDataBlockList(stream, bigBlockSize);
                success = true;
            } finally {
                CloseInputStream(stream, success);
            }
            

            // Set up the block allocation table (necessary for the
            // data_blocks to be manageable
            new BlockAllocationTableReader(header_block_Reader.BATCount,
                                           header_block_Reader.BATArray,
                                           header_block_Reader.XBATCount,
                                           header_block_Reader.XBATIndex,
                                           data_blocks);

            // Get property table from the document
            PropertyTable properties =
                new PropertyTable(header_block_Reader.PropertyStart,data_blocks);

            // init documents
            ProcessProperties(SmallBlockTableReader.GetSmallDocumentBlocks(data_blocks, properties
                    .Root, header_block_Reader.SBATStart), data_blocks, properties.Root
                            .Children, null);
        }