Example #1
0
        /// <summary>
        /// Create a single instance initialized with default values
        /// </summary>
        protected BATBlock()
        {
            _data = new byte[POIFSConstants.BIG_BLOCK_SIZE];
            for (int i = 0; i < this._data.Length; i++)
            {
                this._data[i] = _default_value;
            }
            _fields = new IntegerField[_entries_per_block];
            int offset = 0;

            for (int j = 0; j < _entries_per_block; j++)
            {
                _fields[j] = new IntegerField(offset);
                offset += LittleEndianConsts.INT_SIZE;
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Property"/> class.
        /// </summary>
        protected Property()
        {
            _raw_data = new byte[POIFSConstants.PROPERTY_SIZE];
            for (int i = 0; i < this._raw_data.Length; i++)
            {
                this._raw_data[i] = _default_fill;
            }
            _name_size         = new ShortField(_name_size_offset);
            _property_type     =
                new ByteField(PropertyConstants.PROPERTY_TYPE_OFFSET);
            _node_color        = new ByteField(_node_color_offset);
            _previous_property = new IntegerField(_previous_property_offset,
                                                  _NO_INDEX, _raw_data);
            _next_property     = new IntegerField(_next_property_offset,
                                                  _NO_INDEX, _raw_data);
            _child_property    = new IntegerField(_child_property_offset,
                                                  _NO_INDEX, _raw_data);
            _storage_clsid     = new ClassID(_raw_data,_storage_clsid_offset);
            _user_flags        = new IntegerField(_user_flags_offset, 0, _raw_data);
            _seconds_1         = new IntegerField(_seconds_1_offset, 0,
                                                  _raw_data);
            _days_1            = new IntegerField(_days_1_offset, 0, _raw_data);
            _seconds_2         = new IntegerField(_seconds_2_offset, 0,
                                                  _raw_data);
            _days_2            = new IntegerField(_days_2_offset, 0, _raw_data);
            _start_block       = new IntegerField(_start_block_offset);
            _size              = new IntegerField(_size_offset, 0, _raw_data);
            _index             = _NO_INDEX;

            this.Name="";
            this.NextChild=null;
            this.PreviousChild=null;
        }
Example #3
0
        /// <summary>
        /// Constructor from byte data
        /// </summary>
        /// <param name="index">index number</param>
        /// <param name="array">byte data</param>
        /// <param name="offset">offset into byte data</param>
        protected Property(int index, byte [] array, int offset)
        {
            _raw_data = new byte[ POIFSConstants.PROPERTY_SIZE ];
            System.Array.Copy(array, offset, _raw_data, 0, POIFSConstants.PROPERTY_SIZE);
            _name_size         = new ShortField(_name_size_offset, _raw_data);
            _property_type     =
                new ByteField(PropertyConstants.PROPERTY_TYPE_OFFSET, _raw_data);
            _node_color        = new ByteField(_node_color_offset, _raw_data);
            _previous_property = new IntegerField(_previous_property_offset,
                                                  _raw_data);
            _next_property     = new IntegerField(_next_property_offset,
                                                  _raw_data);
            _child_property    = new IntegerField(_child_property_offset,
                                                  _raw_data);
            _storage_clsid     = new ClassID(_raw_data,_storage_clsid_offset);
            _user_flags        = new IntegerField(_user_flags_offset, 0, _raw_data);
            _seconds_1         = new IntegerField(_seconds_1_offset, _raw_data);
            _days_1            = new IntegerField(_days_1_offset, _raw_data);
            _seconds_2         = new IntegerField(_seconds_2_offset, _raw_data);
            _days_2            = new IntegerField(_days_2_offset, _raw_data);
            _start_block       = new IntegerField(_start_block_offset, _raw_data);
            _size              = new IntegerField(_size_offset, _raw_data);
            _index             = index;
            int name_length = (_name_size.Value / LittleEndianConsts.SHORT_SIZE)
                              - 1;

            if (name_length < 1)
            {
                _name = "";
            }
            else
            {
                char[] char_array  = new char[ name_length ];
                int    name_offset = 0;

                for (int j = 0; j < name_length; j++)
                {
                    char_array[ j ] = ( char ) new ShortField(name_offset,
                                                              _raw_data).Value;
                    name_offset     += LittleEndianConsts.SHORT_SIZE;
                }
                _name = new String(char_array, 0, name_length);
            }
            _next_child     = null;
            _previous_child = null;
        }
Example #4
0
        public void TestWriteToBytes()
        {
            IntegerField field = new IntegerField(0);
            byte[]       array = new byte[ 4 ];

            for (int j = 0; j < _test_array.Length; j++)
            {
                field.Value=_test_array[ j ];
                field.WriteToBytes(array);
                int val = array[ 3 ] << 24;

                val &= unchecked((int)0xFF000000);
                val += (array[ 2 ] << 16) & 0x00FF0000;
                val += (array[ 1 ] << 8) & 0x0000FF00;
                val += (array[ 0 ] & 0x000000FF);
                Assert.AreEqual(_test_array[j], val, "testing ");
            }
        }
Example #5
0
        public void TestConstructors()
        {
            try
            {
                new IntegerField(-1);
                Assert.Fail("Should have caught IndexOutOfRangeException");
            }
            catch (IndexOutOfRangeException)
            {

                // as expected
            }
            IntegerField field = new IntegerField(2);

            Assert.AreEqual(0, field.Value);
            try
            {
                new IntegerField(-1, 1);
                Assert.Fail("Should have caught IndexOutOfRangeException");
            }
            catch (IndexOutOfRangeException)
            {

                // as expected
            }
            field = new IntegerField(2, 0x12345678);
            Assert.AreEqual(0x12345678, field.Value);
            byte[] array = new byte[ 6 ];

            try
            {
                new IntegerField(-1, 1, array);
                Assert.Fail("Should have caught IndexOutOfRangeException");
            }
            catch (IndexOutOfRangeException)
            {

                // as expected
            }
            field = new IntegerField(2, 0x12345678, array);
            Assert.AreEqual(0x12345678, field.Value);
            Assert.AreEqual(( byte ) 0x78, array[ 2 ]);
            Assert.AreEqual(( byte ) 0x56, array[ 3 ]);
            Assert.AreEqual(( byte ) 0x34, array[ 4 ]);
            Assert.AreEqual(( byte ) 0x12, array[ 5 ]);
            array = new byte[ 5 ];
            try
            {
                new IntegerField(2, 5, array);
                Assert.Fail("should have gotten IndexOutOfRangeException");
            }
            catch (IndexOutOfRangeException)
            {

                // as expected
            }
            for (int j = 0; j < _test_array.Length; j++)
            {
                array = new byte[ 4 ];
                new IntegerField(0, _test_array[ j ], array);
                Assert.AreEqual(_test_array[ j ], new IntegerField(0, array).Value);
            }

            // same test as above, but using the static method
            for (int j = 0; j < _test_array.Length; j++)
            {
                array = new byte[ 4 ];
                IntegerField.Write(0, _test_array[j], array);
                Assert.AreEqual(_test_array[ j ], new IntegerField(0, array).Value);
            }
        }
Example #6
0
        public void TestReadFromStream()
        {
            IntegerField field  = new IntegerField(0);
            byte[]       buffer = new byte[ _test_array.Length * 4 ];

            for (int j = 0; j < _test_array.Length; j++)
            {
                buffer[ (j * 4) + 0 ] = ( byte ) (_test_array[ j ] % 256);
                buffer[ (j * 4) + 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256);
                buffer[ (j * 4) + 2 ] = ( byte ) ((_test_array[ j ] >> 16) % 256);
                buffer[ (j * 4) + 3 ] = ( byte ) ((_test_array[ j ] >> 24) % 256);
            }
            MemoryStream stream = new MemoryStream(buffer);

            for (int j = 0; j < buffer.Length / 4; j++)
            {
                field.ReadFromStream(stream);
                Assert.AreEqual(_test_array[j], field.Value, "Testing " + j);
            }
        }
Example #7
0
        public void TestReadFromBytes()
        {
            IntegerField field = new IntegerField(1);
            byte[]       array = new byte[ 4 ];

            try
            {
                field.ReadFromBytes(array);
                Assert.Fail("should have caught IndexOutOfRangeException");
            }
            catch (IndexOutOfRangeException)
            {

                // as expected
            }
            field = new IntegerField(0);
            for (int j = 0; j < _test_array.Length; j++)
            {
                array[ 0 ] = ( byte ) (_test_array[ j ] % 256);
                array[ 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256);
                array[ 2 ] = ( byte ) ((_test_array[ j ] >> 16) % 256);
                array[ 3 ] = ( byte ) ((_test_array[ j ] >> 24) % 256);
                field.ReadFromBytes(array);
                Assert.AreEqual(_test_array[j], field.Value, "testing " + j);
            }
        }
Example #8
0
        public void TestSet()
        {
            IntegerField field = new IntegerField(0);
            byte[]       array = new byte[ 4 ];

            for (int j = 0; j < _test_array.Length; j++)
            {
                field.Value=_test_array[ j ];
                Assert.AreEqual(_test_array[j], field.Value, "testing _1 " + j);
                field = new IntegerField(0);
                field.Set(_test_array[ j ], array);
                Assert.AreEqual(_test_array[ j ], field.Value,"testing _2 ");
                Assert.AreEqual((byte)(_test_array[j] % 256), array[0], "testing _3.0 " + _test_array[j]);
                Assert.AreEqual(( byte ) ((_test_array[ j ] >> 8) % 256),
                             array[ 1 ],"testing _3.1 " + _test_array[ j ]);
                Assert.AreEqual(( byte ) ((_test_array[ j ] >> 16) % 256),
                             array[2], "testing _3.2 " + _test_array[j]);
                Assert.AreEqual(( byte ) ((_test_array[ j ] >> 24) % 256),
                             array[3], "testing _3.3 " + _test_array[j]);
            }
        }
Example #9
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;

        }
Example #10
0
        /// <summary>
        /// create a new HeaderBlockReader from an Stream
        /// </summary>
        /// <param name="stream">the source Stream</param>
        public HeaderBlockReader(Stream stream)
        {
            stream.Position = 0;
            this._data = ReadFirst512(stream);

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

            if (signature != HeaderBlockConstants._signature)
            {
                // Is it one of the usual suspects?
                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. POI only supports OLE2 Office documents");
                }
                if ((signature & unchecked((long)0xFF8FFFFFFFFFFFFFL)) == 0x0010000200040009L)
                {
                    // BIFF2 raw stream starts with BOF (sid=0x0009, size=0x0004, data=0x00t0)
                    throw new ArgumentException("The supplied data appears to be in BIFF2 format.  "
                            + "POI only supports BIFF8 format");
                }

                // Give a generic error
                throw new IOException("Invalid header signature; Read "
                                      + LongToHex(signature) + ", expected "
                                      + LongToHex(HeaderBlockConstants._signature));
            }

            // Figure out our block size
            if (_data[30] == 12)
            {
                this.bigBlockSize = POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS;
            }
            else if (_data[30] == 9)
            {
                this.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;

            // Fetch the rest of the block if needed
            if (bigBlockSize.GetBigBlockSize() != 512)
            {
                int rest = bigBlockSize.GetBigBlockSize() - 512;
                byte[] tmp = new byte[rest];
                IOUtils.ReadFully(stream, tmp);
            }
        }