Ejemplo n.º 1
0
        private void checkDocument(OPOIFSDocument document,
                                   byte[] input)
        {
            int big_blocks   = 0;
            int small_blocks = 0;
            int total_output = 0;

            if (input.Length >= 4096)
            {
                big_blocks   = (input.Length + 511) / 512;
                total_output = big_blocks * 512;
            }
            else
            {
                small_blocks = (input.Length + 63) / 64;
                total_output = 0;
            }
            checkValues(
                big_blocks, small_blocks, total_output,
                makeCopy(
                    document, input,
                    checkValues(
                        big_blocks, small_blocks, total_output, document,
                        input)), input);
        }
Ejemplo n.º 2
0
        private OPOIFSDocument makeCopy(OPOIFSDocument document, byte[] input,
                                        byte[] data)
        {
            OPOIFSDocument copy = null;

            if (input.Length >= 4096)
            {
                RawDataBlock[] blocks =
                    new RawDataBlock[(input.Length + 511) / 512];
                MemoryStream stream = new MemoryStream(data);
                int          index  = 0;

                while (true)
                {
                    RawDataBlock block = new RawDataBlock(stream);

                    if (block.EOF)
                    {
                        break;
                    }
                    blocks[index++] = block;
                }
                copy = new OPOIFSDocument("test" + input.Length, blocks,
                                          input.Length);
            }
            else
            {
                copy = new OPOIFSDocument(
                    "test" + input.Length,
                    (SmallDocumentBlock[])document.SmallBlocks,
                    input.Length);
            }
            return(copy);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SmallBlockTableWriter"/> class.
        /// </summary>
        /// <param name="bigBlockSize">the poifs bigBlockSize</param>
        /// <param name="documents">a IList of POIFSDocument instances</param>
        /// <param name="root">the Filesystem's root property</param>
        public SmallBlockTableWriter(POIFSBigBlockSize bigBlockSize,
                                     IList <OPOIFSDocument> documents,
                                     RootProperty root)
        {
            _sbat         = new BlockAllocationTableWriter(bigBlockSize);
            _small_blocks = new List <SmallDocumentBlock>();
            _root         = root;
            IEnumerator iter = documents.GetEnumerator();

            while (iter.MoveNext())
            {
                OPOIFSDocument       doc    = ( OPOIFSDocument )iter.Current;
                SmallDocumentBlock[] blocks = doc.SmallBlocks;

                if (blocks.Length != 0)
                {
                    doc.StartBlock = _sbat.AllocateSpace(blocks.Length);
                    for (int j = 0; j < blocks.Length; j++)
                    {
                        _small_blocks.Add(blocks[j]);
                    }
                }
                else
                {
                    doc.StartBlock = POIFSConstants.END_OF_CHAIN;
                }
            }
            _sbat.SimpleCreateBlocks();
            _root.Size       = _small_blocks.Count;
            _big_block_count = SmallDocumentBlock.Fill(bigBlockSize, _small_blocks);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DocumentProperty"/> class.
        /// </summary>
        /// <param name="name">POIFSDocument name</param>
        /// <param name="size">POIFSDocument size</param>
        public DocumentProperty(String name, int size)
            : base()
        {
            _document = null;

            this.Name         = name;
            this.Size         = size;
            this.NodeColor    = _NODE_BLACK; // simplification
            this.PropertyType = PropertyConstants.DOCUMENT_TYPE;
        }
Ejemplo n.º 5
0
        public TestDocumentInputStream()
        {
            int blocks = (_workbook_size + 511) / 512;

            _workbook_data = new byte[512 * blocks];
            Arrays.Fill(_workbook_data, unchecked ((byte)-1));
            for (int j = 0; j < _workbook_size; j++)
            {
                _workbook_data[j] = (byte)(j * j);
            }

            // Create the Old POIFS Version
            RawDataBlock[] rawBlocks = new RawDataBlock[blocks];
            MemoryStream   stream    =
                new MemoryStream(_workbook_data);

            for (int j = 0; j < blocks; j++)
            {
                rawBlocks[j] = new RawDataBlock(stream);
            }
            OPOIFSDocument document = new OPOIFSDocument("Workbook", rawBlocks,
                                                         _workbook_size);

            _workbook_o = new DocumentNode(
                document.DocumentProperty,
                new DirectoryNode(
                    new DirectoryProperty("Root Entry"), (POIFSFileSystem)null, null));

            // Now create the NPOIFS Version
            byte[] _workbook_data_only = new byte[_workbook_size];
            Array.Copy(_workbook_data, 0, _workbook_data_only, 0, _workbook_size);

            NPOIFSFileSystem npoifs = new NPOIFSFileSystem();

            // Make it easy when debugging to see what isn't the doc
            byte[] minus1 = new byte[512];
            Arrays.Fill(minus1, unchecked ((byte)-1));
            npoifs.GetBlockAt(-1).Write(minus1);
            npoifs.GetBlockAt(0).Write(minus1);
            npoifs.GetBlockAt(1).Write(minus1);

            // Create the NPOIFS document
            _workbook_n = (DocumentNode)npoifs.CreateDocument(
                new MemoryStream(_workbook_data_only),
                "Workbook"
                );
        }
Ejemplo n.º 6
0
        private byte[] checkValues(int big_blocks, int small_blocks,
                                   int total_output, OPOIFSDocument document,
                                   byte[] input)
        {
            Assert.AreEqual(document, document.DocumentProperty.Document);
            int increment = (int)Math.Sqrt(input.Length);

            for (int j = 1; j <= input.Length; j += increment)
            {
                byte[] buffer = new byte[j];
                int    offset = 0;

                for (int k = 0; k < (input.Length / j); k++)
                {
                    document.Read(buffer, offset);
                    for (int n = 0; n < buffer.Length; n++)
                    {
                        Assert.AreEqual(input[(k * j) + n], buffer[n]
                                        , "checking byte " + (k * j) + n);
                    }
                    offset += j;
                }
            }
            Assert.AreEqual(big_blocks, document.CountBlocks);
            Assert.AreEqual(small_blocks, document.SmallBlocks.Length);
            MemoryStream stream = new MemoryStream();

            document.WriteBlocks(stream);
            byte[] output = stream.ToArray();

            Assert.AreEqual(total_output, output.Length);
            int limit = Math.Min(total_output, input.Length);

            for (int j = 0; j < limit; j++)
            {
                Assert.AreEqual(input[j],
                                output[j], "Checking document offset " + j);
            }
            for (int j = limit; j < output.Length; j++)
            {
                Assert.AreEqual(unchecked ((byte)-1),
                                output[j], "Checking document offset " + j);
            }
            return(output);
        }
Ejemplo n.º 7
0
        public void TestConstructor()
        {
            DirectoryProperty property1 = new DirectoryProperty("directory");

            RawDataBlock[] rawBlocks = new RawDataBlock[4];
            MemoryStream   stream    =
                new MemoryStream(new byte[2048]);

            for (int j = 0; j < 4; j++)
            {
                rawBlocks[j] = new RawDataBlock(stream);
            }
            OPOIFSDocument document = new OPOIFSDocument("document", rawBlocks,
                                                         2000);
            DocumentProperty property2 = document.DocumentProperty;
            DirectoryNode    parent    = new DirectoryNode(property1, (POIFSFileSystem)null, null);
            DocumentNode     node      = new DocumentNode(property2, parent);

            // Verify we can retrieve the document
            Assert.AreEqual(property2.Document, node.Document);

            // Verify we can Get the size
            Assert.AreEqual(property2.Size, node.Size);

            // Verify isDocumentEntry returns true
            Assert.IsTrue(node.IsDocumentEntry);

            // Verify isDirectoryEntry returns false
            Assert.IsTrue(!node.IsDirectoryEntry);

            // Verify GetName behaves correctly
            Assert.AreEqual(property2.Name, node.Name);

            // Verify GetParent behaves correctly
            Assert.AreEqual(parent, node.Parent);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Processes the properties.
        /// </summary>
        /// <param name="small_blocks">The small_blocks.</param>
        /// <param name="big_blocks">The big_blocks.</param>
        /// <param name="properties">The properties.</param>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        private List <DocumentDescriptor> ProcessProperties(BlockList small_blocks,
                                                            BlockList big_blocks,
                                                            IEnumerator properties,
                                                            POIFSDocumentPath path)
        {
            List <DocumentDescriptor> documents =
                new List <DocumentDescriptor>();

            while (properties.MoveNext())
            {
                Property property = (Property)properties.Current;
                String   name     = property.Name;

                if (property.IsDirectory)
                {
                    POIFSDocumentPath new_path = new POIFSDocumentPath(path,
                                                                       new String[]
                    {
                        name
                    });

                    ProcessProperties(
                        small_blocks, big_blocks,
                        ((DirectoryProperty)property).Children, new_path);
                }
                else
                {
                    int            startBlock = property.StartBlock;
                    IEnumerator    listeners  = registry.GetListeners(path, name);
                    OPOIFSDocument document   = null;
                    if (listeners.MoveNext())
                    {
                        listeners.Reset();
                        int size = property.Size;


                        if (property.ShouldUseSmallBlocks)
                        {
                            document =
                                new OPOIFSDocument(name, small_blocks
                                                   .FetchBlocks(startBlock, -1), size);
                        }
                        else
                        {
                            document =
                                new OPOIFSDocument(name, big_blocks
                                                   .FetchBlocks(startBlock, -1), size);
                        }
                        //POIFSReaderListener listener =
                        //        (POIFSReaderListener)listeners.Current;
                        //listener.ProcessPOIFSReaderEvent(
                        //        new POIFSReaderEvent(
                        //            new DocumentInputStream(document), path,
                        //            name));
                        while (listeners.MoveNext())
                        {
                            POIFSReaderListener listener =
                                (POIFSReaderListener)listeners.Current;
                            listener.ProcessPOIFSReaderEvent(
                                new POIFSReaderEvent(
                                    new DocumentInputStream(document), path,
                                    name));
                        }
                    }
                    else
                    {
                        // consume the document's data and discard it
                        if (property.ShouldUseSmallBlocks)
                        {
                            small_blocks.FetchBlocks(startBlock, -1);
                        }
                        else
                        {
                            big_blocks.FetchBlocks(startBlock, -1);
                        }
                        //documents.Add(
                        //        new DocumentDescriptor(path, name));
                        //fire event
                        //OnStreamReaded(new POIFSReaderEventArgs(name, path, document));
                    }
                }
            }
            return(documents);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DocumentProperty"/> class.
 /// </summary>
 /// <param name="index">index number</param>
 /// <param name="array">byte data</param>
 /// <param name="offset">offset into byte data</param>
 public DocumentProperty(int index, byte [] array, int offset) : base(index, array, offset)
 {
     _document = null;
 }
Ejemplo n.º 10
0
        public void TestOPOIFSDocument()
        {
            // Verify correct number of blocks Get Created for document
            // that is exact multituple of block size
            OPOIFSDocument document;

            byte[] array = new byte[4096];

            for (int j = 0; j < array.Length; j++)
            {
                array[j] = (byte)j;
            }
            document = new OPOIFSDocument("foo", new SlowInputStream(new MemoryStream(array)));
            checkDocument(document, array);

            // Verify correct number of blocks Get Created for document
            // that is not an exact multiple of block size
            array = new byte[4097];
            for (int j = 0; j < array.Length; j++)
            {
                array[j] = (byte)j;
            }
            document = new OPOIFSDocument("bar", new MemoryStream(array));
            checkDocument(document, array);

            // Verify correct number of blocks Get Created for document
            // that is small
            array = new byte[4095];
            for (int j = 0; j < array.Length; j++)
            {
                array[j] = (byte)j;
            }
            document = new OPOIFSDocument("_bar", new MemoryStream(array));
            checkDocument(document, array);

            // Verify correct number of blocks Get Created for document
            // that is rather small
            array = new byte[199];
            for (int j = 0; j < array.Length; j++)
            {
                array[j] = (byte)j;
            }
            document = new OPOIFSDocument("_bar2",
                                          new MemoryStream(array));
            checkDocument(document, array);

            // Verify that output is correct
            array = new byte[4097];
            for (int j = 0; j < array.Length; j++)
            {
                array[j] = (byte)j;
            }
            document = new OPOIFSDocument("foobar",
                                          new MemoryStream(array));
            checkDocument(document, array);
            document.StartBlock = 0x12345678;   // what a big file!!
            DocumentProperty property = document.DocumentProperty;
            MemoryStream     stream   = new MemoryStream();

            property.WriteData(stream);
            byte[] output = stream.ToArray();
            byte[] array2 =
            {
                ( byte )'f',            ( byte )0,              ( byte )'o',            ( byte )0,              ( byte )'o',
                ( byte )0,              ( byte )'b',            ( byte )0,              ( byte )'a',            ( byte )0,
                ( byte )'r',            ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )14,
                ( byte )0,              ( byte )2,              ( byte )1,              unchecked (( byte )-1), unchecked (( byte )-1),
                unchecked (( byte )-1), unchecked (( byte )-1), unchecked (( byte )-1), unchecked (( byte )-1), unchecked (( byte )-1),
                unchecked (( byte )-1), unchecked (( byte )-1), unchecked (( byte )-1), unchecked (( byte )-1), unchecked (( byte )-1),
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0x78,           ( byte )0x56,           ( byte )0x34,
                ( byte )0x12,           ( byte )1,              ( byte )16,             ( byte )0,              ( byte )0,
                ( byte )0,              ( byte )0,              ( byte )0,              ( byte )0
            };

            Assert.AreEqual(array2.Length, output.Length);
            for (int j = 0; j < output.Length; j++)
            {
                Assert.AreEqual(array2[j],
                                output[j], "Checking property offset " + j);
            }
        }
Ejemplo n.º 11
0
 public POIFSReaderEventArgs(string name, POIFSDocumentPath path, OPOIFSDocument document)
 {
     this.name     = name;
     this.path     = path;
     this.document = document;
 }