/// <summary>
 /// Initializes a new instance of the <see cref="BlockAllocationTableWriter"/> class.
 /// </summary>
 public BlockAllocationTableWriter(POIFSBigBlockSize bigBlockSize)
 {
     _start_block = POIFSConstants.END_OF_CHAIN;
     _entries = new List<int>(_default_size);
     _blocks      = new BATBlock[ 0 ];
     _bigBlockSize = bigBlockSize;
 }
        /// <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 documents,
                                     RootProperty root)
        {
            _sbat = new BlockAllocationTableWriter(bigBlockSize);
            _small_blocks = new ArrayList();
            _root         = root;
            IEnumerator iter = documents.GetEnumerator();

            while (iter.MoveNext())
            {
                POIFSDocument   doc    = ( POIFSDocument ) iter.Current;
                BlockWritable[] 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);
        }
Beispiel #3
0
        private static List<Property> BuildProperties(IEnumerator<ByteBuffer> dataSource, POIFSBigBlockSize bigBlockSize)
        {
            List<Property> properties = new List<Property>();

            while(dataSource.MoveNext())
            {
                ByteBuffer bb = dataSource.Current;

                // Turn it into an array
                byte[] data;
                if (bb.HasBuffer && bb.Offset == 0 &&
                    bb.Buffer.Length == bigBlockSize.GetBigBlockSize())
                {
                    data = bb.Buffer;
                }
                else
                {
                    data = new byte[bigBlockSize.GetBigBlockSize()];
                    int toRead = data.Length;
                    if (bb.Remaining() < bigBlockSize.GetBigBlockSize())
                    {
                        // Looks to be a truncated block
                        // This isn't allowed, but some third party created files
                        //  sometimes do this, and we can normally read anyway

                        toRead = bb.Remaining();
                    }
                    bb.Read(data, 0, toRead);
                }

                PropertyFactory.ConvertToProperties(data, properties);
            }
            return properties;

        }
Beispiel #4
0
        /// <summary>
        /// convert a single long array into an array of SmallDocumentBlock
        /// instances
        /// </summary>
        /// <param name="bigBlockSize">the poifs bigBlockSize</param>
        /// <param name="array">the byte array to be converted</param>
        /// <param name="size">the intended size of the array (which may be smaller)</param>
        /// <returns>an array of SmallDocumentBlock instances, filled from
        /// the array</returns>
        public static SmallDocumentBlock [] Convert(POIFSBigBlockSize bigBlockSize,
                                                    byte [] array,
                                                    int size)
        {
            SmallDocumentBlock[] rval = new SmallDocumentBlock[ (size + _block_size - 1) / _block_size ];
            int                  offset = 0;

            for (int k = 0; k < rval.Length; k++)
            {
                rval[ k ] = new SmallDocumentBlock(bigBlockSize);
                if (offset < array.Length)
                {
                    int length = Math.Min(_block_size, array.Length - offset);

                    Array.Copy(array, offset, rval[ k ]._data, 0, length);
                    if (length != _block_size)
                    {
                        for (int i = length; i < _block_size; i++)
                            rval[k]._data[i] = _default_fill;
                    }
                }
                else
                {
                    for (int j = 0; j < rval[k]._data.Length; j++)
                    {
                        rval[k]._data[j] = _default_fill;
                    }

                }
                offset += _block_size;
            }
            return rval;
        }
Beispiel #5
0
 public NPropertyTable(HeaderBlock headerBlock, NPOIFSFileSystem fileSystem)
     :base(headerBlock, 
             BuildProperties( (new NPOIFSStream(fileSystem, headerBlock.PropertyStart)).GetEnumerator(),headerBlock.BigBlockSize)
     )
 {
     _bigBigBlockSize = headerBlock.BigBlockSize;
 }
Beispiel #6
0
 /// <summary>
 /// Create a single instance initialized with default values
 /// </summary>
 /// <param name="bigBlockSize"></param>
 /// <param name="properties">the properties to be inserted</param>
 /// <param name="offset">the offset into the properties array</param>
 protected PropertyBlock(POIFSBigBlockSize bigBlockSize, Property[] properties, int offset) : base(bigBlockSize)
 {
     _properties = new Property[ bigBlockSize.GetPropertiesPerBlock() ];
     for (int j = 0; j < _properties.Length; j++)
     {
         _properties[ j ] = properties[ j + offset ];
     }
 }
Beispiel #7
0
        public SmallDocumentBlock(POIFSBigBlockSize bigBlockSize, byte[] data, int index)
        {
            _bigBlockSize = bigBlockSize;
            _blocks_per_big_block = GetBlocksPerBigBlock(bigBlockSize);
            _data = new byte[_block_size];

            System.Array.Copy(data, index*_block_size, _data, 0, _block_size);
        }
Beispiel #8
0
        /**
         * reading constructor (used when we've read in a file and we want
         * to extract the property table from it). Populates the
         * properties thoroughly
         *
         * @param startBlock the first block of the property table
         * @param blockList the list of blocks
         *
         * @exception IOException if anything goes wrong (which should be
         *            a result of the input being NFG)
         */
        public PropertyTable(HeaderBlock headerBlock, 
                             RawDataBlockList blockList)
            : base(headerBlock, 
                    PropertyFactory.ConvertToProperties( blockList.FetchBlocks(headerBlock.PropertyStart, -1) ) )
        {
            _bigBigBlockSize = headerBlock.BigBlockSize;
            _blocks      = null;

        }
Beispiel #9
0
        /// <summary>
        /// fetch the small document block list from an existing file
        /// </summary>
        /// <param name="blockList">the raw data from which the small block table will be extracted</param>
        /// <param name="root">the root property (which contains the start block and small block table size)</param>
        /// <param name="sbatStart">the start block of the SBAT</param>
        /// <returns>the small document block list</returns>
        public static BlockList GetSmallDocumentBlocks(POIFSBigBlockSize bigBlockSize,
                RawDataBlockList blockList, RootProperty root,
                int sbatStart)
        {
            BlockList list =
                new SmallDocumentBlockList(
                    SmallDocumentBlock.Extract(bigBlockSize, blockList.FetchBlocks(root.StartBlock, -1)));

            new BlockAllocationTableReader(bigBlockSize, blockList.FetchBlocks(sbatStart, -1), list);
            return list;
        }
Beispiel #10
0
        public POIFSDocument(string name, RawDataBlock[] blocks, int length)
        {
            _size = length;
            if (blocks.Length == 0)
                _bigBigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
            else
            {
                _bigBigBlockSize = (blocks[0].BigBlockSize == POIFSConstants.SMALLER_BIG_BLOCK_SIZE ?
                    POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS : POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS);
            }

            _big_store = new BigBlockStore(_bigBigBlockSize, ConvertRawBlocksToBigBlocks(blocks));
            _property = new DocumentProperty(name, _size);
            _small_store = new SmallBlockStore(_bigBigBlockSize, EMPTY_SMALL_BLOCK_ARRAY);
            _property.Document = this;
        }
Beispiel #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RawDataBlockList"/> class.
        /// </summary>
        /// <param name="stream">the InputStream from which the data will be read</param>
        /// <param name="bigBlockSize">The big block size, either 512 bytes or 4096 bytes</param>
        public RawDataBlockList(Stream stream, POIFSBigBlockSize bigBlockSize)
        {
            List<RawDataBlock> blocks = new List<RawDataBlock>();

            while (true)
            {
                RawDataBlock block = new RawDataBlock(stream, bigBlockSize.GetBigBlockSize());
                
                // If there was data, add the block to the list
                if(block.HasData) {
            	    blocks.Add(block);
                }

                // If the stream is now at the End Of File, we're done
                if (block.EOF) {
                    break;
                }
            }
             SetBlocks((ListManagedBlock[])blocks.ToArray());
        }
Beispiel #12
0
//        private static List<Property> BuildProperties(IEnumerator<byte[]> dataSource, POIFSBigBlockSize bigBlockSize)
        private static List<Property> BuildProperties(IEnumerator<ByteBuffer> dataSource, POIFSBigBlockSize bigBlockSize)
        {
            try
            {
                List<Property> properties = new List<Property>();

                while(dataSource.MoveNext())
                {
                    ByteBuffer bb = dataSource.Current;
                    //byte[] bb = (byte[])dataSource.Current;

                    // Turn it into an array
                    byte[] data;
                    if (bb.HasBuffer && bb.Offset == 0 &&
                        bb.Buffer.Length == bigBlockSize.GetBigBlockSize())
                    {
                        data = bb.Buffer;
                    }
                    else
                    {
                        data = new byte[bigBlockSize.GetBigBlockSize()];
                        //bb.get(data, 0, data.length);
                        bb.Read(data, 0, data.Length);
                    }
                    //}

                    //data = new byte[bigBlockSize.GetBigBlockSize()];
                    //System.Array.Copy(bb, data, bb.Length);

                    PropertyFactory.ConvertToProperties(data, properties);
                }
                return properties;

            }
            catch(System.IO.IOException ex)
            {
                throw ex;
            }

         
        }
Beispiel #13
0
        /// <summary>
        /// Create an array of PropertyBlocks from an array of Property
        /// instances, creating empty Property instances to make up any
        /// shortfall
        /// </summary>
        /// <param name="properties">the Property instances to be converted into PropertyBlocks, in a java List</param>
        /// <returns>the array of newly created PropertyBlock instances</returns>
        public static BlockWritable [] CreatePropertyBlockArray( POIFSBigBlockSize bigBlockSize,
                                        List<Property> properties)
            {
            int _properties_per_block = bigBlockSize.GetPropertiesPerBlock();

            int blockCount = (properties.Count + _properties_per_block - 1) / _properties_per_block;

            Property[] toBeWritten = new Property[blockCount * _properties_per_block];

            System.Array.Copy(properties.ToArray(), 0, toBeWritten, 0, properties.Count);

            for (int i = properties.Count; i < toBeWritten.Length; i++)
            {
                toBeWritten[i] = new AnonymousProperty();
            }

            BlockWritable[] rvalue = new BlockWritable[blockCount];

            for (int i = 0; i < blockCount; i++)
                rvalue[i] = new PropertyBlock(bigBlockSize, toBeWritten, i * _properties_per_block);

            return rvalue;
        }
        /// <summary>
        /// create a BlockAllocationTableReader for an existing filesystem. Side
        /// effect: when this method finishes, the BAT blocks will have
        /// been Removed from the raw block list, and any blocks labeled as
        /// 'unused' in the block allocation table will also have been
        /// Removed from the raw block list. </summary>
        /// <param name="bigBlockSizse">the poifs bigBlockSize</param>
        /// <param name="block_count">the number of BAT blocks making up the block allocation table</param>
        /// <param name="block_array">the array of BAT block indices from the
        /// filesystem's header</param>
        /// <param name="xbat_count">the number of XBAT blocks</param>
        /// <param name="xbat_index">the index of the first XBAT block</param>
        /// <param name="raw_block_list">the list of RawDataBlocks</param>
        public BlockAllocationTableReader(POIFSBigBlockSize bigBlockSizse,
                                          int block_count,
                                          int[] block_array,
                                          int xbat_count,
                                          int xbat_index,
                                          BlockList raw_block_list)
            : this(bigBlockSizse)
        {
            SanityCheckBlockCount(block_count);

            RawDataBlock[] blocks = new RawDataBlock[block_count];
            int limit = Math.Min(block_count, block_array.Length);
            int block_index;

            for (block_index = 0; block_index < limit; block_index++)
            {
                int nextOffset = block_array[block_index];
                if (nextOffset > raw_block_list.BlockCount())
                {
                    throw new IOException("Your file contains " + raw_block_list.BlockCount() +
                                           " sectors, but the initial DIFAT array at index " + block_index +
                                           " referenced block # " + nextOffset + ". This isn't allowed and " +
                                           " your file is corrupt");
                }

                blocks[block_index] = (RawDataBlock)raw_block_list.Remove(nextOffset);
            }
            if (block_index < block_count)
            {

                // must have extended blocks
                if (xbat_index < 0)
                {
                    throw new IOException(
                        "BAT count exceeds limit, yet XBAT index indicates no valid entries");
                }
                int chain_index = xbat_index;
                int max_entries_per_block = BATBlock.EntriesPerXBATBlock;
                int chain_index_offset = BATBlock.XBATChainOffset;

                // Each XBAT block contains either:
                //  (maximum number of sector indexes) + index of next XBAT
                //  some sector indexes + FREE sectors to max # + EndOfChain
                for (int j = 0; j < xbat_count; j++)
                {
                    limit = Math.Min(block_count - block_index,
                                     max_entries_per_block);
                    byte[] data = raw_block_list.Remove(chain_index).Data;
                    int offset = 0;

                    for (int k = 0; k < limit; k++)
                    {
                        blocks[block_index++] =
                            (RawDataBlock)raw_block_list.Remove(LittleEndian.GetInt(data, offset));
                        offset += LittleEndianConsts.INT_SIZE;
                    }
                    chain_index = LittleEndian.GetInt(data, chain_index_offset);
                    if (chain_index == POIFSConstants.END_OF_CHAIN)
                    {
                        break;
                    }
                }
            }
            if (block_index != block_count)
            {
                throw new IOException("Could not find all blocks");
            }

            // now that we have all of the raw data blocks, go through and
            // create the indices
            SetEntries((ListManagedBlock[])blocks, raw_block_list);
        }
Beispiel #15
0
        public POIFSDocument(string name, POIFSBigBlockSize bigBlockSize, Stream stream)
        {
            List<DocumentBlock> blocks = new List<DocumentBlock>();

            _size = 0;
            _bigBigBlockSize = bigBlockSize;
            while (true)
            {
                DocumentBlock block = new DocumentBlock(stream, bigBlockSize);
                int blockSize = block.Size;

                if (blockSize > 0)
                {
                    blocks.Add(block);
                    _size += blockSize;
                }
                if (block.PartiallyRead)
                    break;
            }

            DocumentBlock[] bigBlocks = blocks.ToArray();
            _big_store = new BigBlockStore(bigBlockSize, bigBlocks);
            _property = new DocumentProperty(name, _size);
            _property.Document = this;

            if (_property.ShouldUseSmallBlocks)
            {
                _small_store = new SmallBlockStore(bigBlockSize, SmallDocumentBlock.Convert(bigBlockSize, bigBlocks, _size));
                _big_store = new BigBlockStore(bigBlockSize, new DocumentBlock[0]);
            }
            else
            {
                _small_store = new SmallBlockStore(bigBlockSize, EMPTY_SMALL_BLOCK_ARRAY);
        }

        }
 /// <summary>
 /// create a BlockAllocationTableReader from an array of raw data blocks
 /// </summary>
 /// <param name="bigBlockSize"></param>
 /// <param name="blocks">the raw data</param>
 /// <param name="raw_block_list">the list holding the managed blocks</param>
 public BlockAllocationTableReader(POIFSBigBlockSize bigBlockSize, ListManagedBlock[] blocks,  
                            BlockList raw_block_list)
     : this(bigBlockSize)
 {
     SetEntries(blocks, raw_block_list);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="BlockAllocationTableReader"/> class.
 /// </summary>
 public BlockAllocationTableReader(POIFSBigBlockSize bigBlockSize)
 {
     this.bigBlockSize = bigBlockSize;
     _entries = new List<int>();
 }
Beispiel #18
0
 /// <summary>
 /// Create a single instance initialized with data.
 /// </summary>
 /// <param name="stream">the InputStream delivering the data.</param>
 /// <param name="bigBlockSize">the poifs bigBlockSize</param>
 public DocumentBlock(Stream stream, POIFSBigBlockSize bigBlockSize)
     : this(bigBlockSize)
 {
     int count = IOUtils.ReadFully(stream, _data);
     _bytes_Read = (count == -1) ? 0: count;
 }
Beispiel #19
0
 public DocumentBlock(POIFSBigBlockSize bigBlockSize)
     :base(bigBlockSize)
 {
     _data = new byte[POIFSConstants.BIG_BLOCK_SIZE];
     Arrays.Fill(_data, _default_value);
 }
Beispiel #20
0
        public POIFSDocument(string name, SmallDocumentBlock[] blocks, int length)
        {
            _size = length;
            if(blocks.Length == 0)
                _bigBigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
            else
                _bigBigBlockSize = blocks[0].BigBlockSize;

            _big_store = new BigBlockStore(_bigBigBlockSize, EMPTY_BIG_BLOCK_ARRAY);
            _property = new DocumentProperty(name, _size);
            _small_store = new SmallBlockStore(_bigBigBlockSize, blocks);
            _property.Document = this;
        }
Beispiel #21
0
        /// <summary>
        /// convert a single long array into an array of DocumentBlock
        /// instances
        /// </summary>
        /// <param name="bigBlockSize">the poifs bigBlockSize</param>
        /// <param name="array">the byte array to be converted</param>
        /// <param name="size">the intended size of the array (which may be smaller)</param>
        /// <returns>an array of DocumentBlock instances, filled from the
        /// input array</returns>
        public static DocumentBlock[] Convert(POIFSBigBlockSize bigBlockSize,
                                                byte[] array,
                                               int size)
        {
            DocumentBlock[] rval =
                new DocumentBlock[(size + POIFSConstants.BIG_BLOCK_SIZE - 1) / POIFSConstants.BIG_BLOCK_SIZE];
            int offset = 0;

            for (int k = 0; k < rval.Length; k++)
            {
                rval[k] = new DocumentBlock(bigBlockSize);
                if (offset < array.Length)
                {
                    int length = Math.Min(POIFSConstants.BIG_BLOCK_SIZE,
                                          array.Length - offset);

                    Array.Copy(array, offset, rval[k]._data, 0, length);
                    if (length != POIFSConstants.BIG_BLOCK_SIZE)
                    {
                        for (int j = (length > 0) ? (length - 1) : length; j < POIFSConstants.BIG_BLOCK_SIZE; j++)
                        {
                            rval[k]._data[j] = _default_value;
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < rval[k]._data.Length; j++)
                    {
                        rval[k]._data[j] = _default_value;
                    }
                }
                offset += POIFSConstants.BIG_BLOCK_SIZE;
            }
            return rval;
        }
Beispiel #22
0
 internal BigBlockStore(POIFSBigBlockSize bigBlockSize, POIFSDocumentPath path, string name, int size, POIFSWriterListener writer)
 {
     this.bigBlockSize = bigBlockSize;
     this.bigBlocks = new DocumentBlock[0];
     this.path = path;
     this.name = name;
     this.size = size;
     this.writer = writer;
 }
Beispiel #23
0
        public void PrivateHeaderBlock(byte[] data)
        {
            _data = data;

            long signature = LittleEndian.GetLong(_data, _signature_offset);

            if (signature != _signature)
            {
                byte[] OOXML_FILE_HEADER = POIFSConstants.OOXML_FILE_HEADER;
                if (_data[0] == OOXML_FILE_HEADER[0]
                    && _data[1] == OOXML_FILE_HEADER[1]
                    && _data[2] == OOXML_FILE_HEADER[2]
                    && _data[3] == OOXML_FILE_HEADER[3])
                {
                    throw new OfficeXmlFileException("The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)");
                }
                if (_data[0] == 0x09 && _data[1] == 0x00 && // sid=0x0009
                _data[2] == 0x04 && _data[3] == 0x00 && // size=0x0004
                _data[4] == 0x00 && _data[5] == 0x00 && // unused
               (_data[6] == 0x10 || _data[6] == 0x20 || _data[6] == 0x40) &&
                _data[7] == 0x00)
                {
                    // BIFF2 raw stream
                    throw new OldExcelFormatException("The supplied data appears to be in BIFF2 format. " +
                            "HSSF only supports the BIFF8 format, try OldExcelExtractor");
                }
                if (_data[0] == 0x09 && _data[1] == 0x02 && // sid=0x0209
                    _data[2] == 0x06 && _data[3] == 0x00 && // size=0x0006
                    _data[4] == 0x00 && _data[5] == 0x00 && // unused
                   (_data[6] == 0x10 || _data[6] == 0x20 || _data[6] == 0x40) &&
                    _data[7] == 0x00)
                {
                    // BIFF3 raw stream
                    throw new OldExcelFormatException("The supplied data appears to be in BIFF3 format. " +
                            "HSSF only supports the BIFF8 format, try OldExcelExtractor");
                }
                if (_data[0] == 0x09 && _data[1] == 0x04 && // sid=0x0409
                    _data[2] == 0x06 && _data[3] == 0x00 && // size=0x0006
                    _data[4] == 0x00 && _data[5] == 0x00)
                { // unused
                    if (((_data[6] == 0x10 || _data[6] == 0x20 || _data[6] == 0x40) &&
                          _data[7] == 0x00) ||
                        (_data[6] == 0x00 && _data[7] == 0x01))
                    {
                        // BIFF4 raw stream
                        throw new OldExcelFormatException("The supplied data appears to be in BIFF4 format. " +
                                "HSSF only supports the BIFF8 format, try OldExcelExtractor");
                    }
                }

                // Give a generic error if the OLE2 signature isn't found
                throw new NotOLE2FileException("Invalid header signature; read "
                                    + LongToHex(signature) + ", expected "
                                    + LongToHex(_signature) + " - Your file appears "
                                    + "not to be a valid OLE2 document");
            }

            if (_data[30] == 12)
            {
                bigBlockSize = POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS;
            }
            else if (_data[30] == 9)
            {
                bigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
            }
            else
            {
                throw new IOException("Unsupported blocksize  (2^" + _data[30] + "). Expected 2^9 or 2^12.");
            }

            // Setup the fields to read and write the counts and starts
            _bat_count = new IntegerField(HeaderBlockConstants._bat_count_offset, _data).Value;
            _property_start = new IntegerField(HeaderBlockConstants._property_start_offset, _data).Value;
            _sbat_start = new IntegerField(HeaderBlockConstants._sbat_start_offset, _data).Value;
            _sbat_count = new IntegerField(HeaderBlockConstants._sbat_block_count_offset, _data).Value;
            _xbat_start = new IntegerField(HeaderBlockConstants._xbat_start_offset, _data).Value;
            _xbat_count = new IntegerField(HeaderBlockConstants._xbat_count_offset, _data).Value;

        }
Beispiel #24
0
 internal BigBlockStore(POIFSBigBlockSize bigBlockSize, DocumentBlock[] blocks)
 {
     this.bigBlockSize = bigBlockSize;
     bigBlocks = (DocumentBlock[])blocks.Clone();
     path = null;
     name = null;
     size = -1;
     writer = null;
 }
Beispiel #25
0
 internal SmallBlockStore(POIFSBigBlockSize bigBlockSize, SmallDocumentBlock[] blocks)
 {
     this.bigBlockSize = bigBlockSize;
     smallBlocks = (SmallDocumentBlock[])blocks.Clone();
     this.path = null;
     this.name = null;
     this.size = -1;
     this.writer = null;
 }
Beispiel #26
0
 public NPropertyTable(HeaderBlock headerBlock) : base(headerBlock)
 {
     _bigBigBlockSize = headerBlock.BigBlockSize;
 }
Beispiel #27
0
        public void PrivateHeaderBlock(byte[] data)
        {
            _data = data;

            long signature = LittleEndian.GetLong(_data, _signature_offset);

            if (signature != _signature)
            {
                byte[] OOXML_FILE_HEADER = POIFSConstants.OOXML_FILE_HEADER;
                if (_data[0] == OOXML_FILE_HEADER[0]
                    && _data[1] == OOXML_FILE_HEADER[1]
                    && _data[2] == OOXML_FILE_HEADER[2]
                    && _data[3] == OOXML_FILE_HEADER[3])
                {
                    throw new OfficeXmlFileException("The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)");
                }
                if ((signature & unchecked((long)0xFF8FFFFFFFFFFFFFL)) == 0x0010000200040009L)
                {
                    throw new ArgumentException("The supplied data appears to be in BIFF2 format.  "
                        + "POI only supports BIFF8 format");
                }

                throw new IOException("Invalid header signature; read "
                                    + LongToHex(signature) + ", expected "
                                    + LongToHex(_signature));
            }

            if (_data[30] == 12)
            {
                bigBlockSize = POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS;
            }
            else if (_data[30] == 9)
            {
                bigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
            }
            else
            {
                throw new IOException("Unsupported blocksize  (2^" + _data[30] + "). Expected 2^9 or 2^12.");
            }

            // Setup the fields to read and write the counts and starts
            _bat_count = new IntegerField(HeaderBlockConstants._bat_count_offset, _data).Value;
            _property_start = new IntegerField(HeaderBlockConstants._property_start_offset, _data).Value;
            _sbat_start = new IntegerField(HeaderBlockConstants._sbat_start_offset, _data).Value;
            _sbat_count = new IntegerField(HeaderBlockConstants._sbat_block_count_offset, _data).Value;
            _xbat_start = new IntegerField(HeaderBlockConstants._xbat_start_offset, _data).Value;
            _xbat_count = new IntegerField(HeaderBlockConstants._xbat_count_offset, _data).Value;

        }
Beispiel #28
0
        public POIFSDocument(string name, POIFSBigBlockSize bigBlockSize, ListManagedBlock[] blocks, int length)
        {
            _size = length;
            _bigBigBlockSize = bigBlockSize;
            _property = new DocumentProperty(name, _size);
            _property.Document = this;

            if (Property.IsSmall(_size))
            {
                _big_store = new BigBlockStore(bigBlockSize, EMPTY_BIG_BLOCK_ARRAY);
                _small_store = new SmallBlockStore(bigBlockSize, ConvertRawBlocksToSmallBlocks(blocks));
            }
            else
            {
                _big_store = new BigBlockStore(bigBlockSize, ConvertRawBlocksToBigBlocks(blocks));
                _small_store = new SmallBlockStore(bigBlockSize, EMPTY_SMALL_BLOCK_ARRAY);
            }
        }
Beispiel #29
0
        public HeaderBlock(POIFSBigBlockSize bigBlockSize)
        {
            this.bigBlockSize = bigBlockSize;

            _data = new byte[POIFSConstants.SMALLER_BIG_BLOCK_SIZE];

            //fill the array.
            for (int i = 0; i < _data.Length; i++)
                _data[i] = _default_value;

            new LongField(_signature_offset, _signature, _data);
            new IntegerField(0x08, 0, _data);
            new IntegerField(0x0c, 0, _data);
            new IntegerField(0x10, 0, _data);
            new IntegerField(0x14, 0, _data);

            new ShortField((int)0x18, (short)0x3b, ref _data);
            new ShortField((int)0x1a, (short)0x3, ref _data);
            new ShortField((int)0x1c, (short)-2, ref _data);

            new ShortField(0x1e, bigBlockSize.GetHeaderValue(), ref _data);
            new IntegerField(0x20, 0x6, _data);
            new IntegerField(0x24, 0, _data);
            new IntegerField(0x28, 0, _data);
            new IntegerField(0x34, 0, _data);
            new IntegerField(0x38, 0x1000, _data);

            _bat_count = 0;
            _sbat_count = 0;
            _xbat_count = 0;
            _property_start = POIFSConstants.END_OF_CHAIN;
            _sbat_start = POIFSConstants.END_OF_CHAIN;
            _xbat_start = POIFSConstants.END_OF_CHAIN;

        }
Beispiel #30
0
 public POIFSDocument(string name, int size, POIFSBigBlockSize bigBlockSize, POIFSDocumentPath path, POIFSWriterListener writer)
     {
     _size = size;
     _bigBigBlockSize = bigBlockSize;
     _property = new DocumentProperty(name, _size);
     _property.Document = this;
     if (_property.ShouldUseSmallBlocks)
     {
         _small_store = new SmallBlockStore(_bigBigBlockSize, path, name, size, writer);
         _big_store = new BigBlockStore(_bigBigBlockSize, EMPTY_BIG_BLOCK_ARRAY);
     }
     else
     {
         _small_store = new SmallBlockStore(_bigBigBlockSize, EMPTY_SMALL_BLOCK_ARRAY);
         _big_store = new BigBlockStore(_bigBigBlockSize, path, name, size, writer);
     }
 }