Example #1
0
        public void TestCorruptExtRstDetection()
        {
            byte[] data = new byte[] {
                0x79, 0x79, 0x11, 0x11,
                0x22, 0x22, 0x33, 0x33,
            };
            Assert.AreEqual(8, data.Length);

            LittleEndianInputStream inp = new LittleEndianInputStream(
                new MemoryStream(data)
                );

            UnicodeString.ExtRst ext = new UnicodeString.ExtRst(inp, data.Length);

            // Will be empty
            Assert.AreEqual(ext, new UnicodeString.ExtRst());

            // If written, will be the usual size
            Assert.AreEqual(10, ext.DataSize); // Excludes 4 byte header

            // Is empty
            Assert.AreEqual(0, ext.NumberOfRuns);
            Assert.AreEqual(0, ext.FormattingOptions);
            Assert.AreEqual(0, ext.FormattingFontIndex);
            Assert.AreEqual("", ext.PhoneticText);
            Assert.AreEqual(0, ext.PhRuns.Length);
        }
Example #2
0
        public void TestFormatRun()
        {
            UnicodeString.FormatRun fr = new UnicodeString.FormatRun((short)4, (short)0x15c);
            Assert.AreEqual(4, fr.CharacterPos);
            Assert.AreEqual(0x15c, fr.FontIndex);

            MemoryStream             baos = new MemoryStream();
            LittleEndianOutputStream out1 = new LittleEndianOutputStream(baos);

            fr.Serialize(out1);

            byte[] b = baos.ToArray();
            Assert.AreEqual(4, b.Length);
            Assert.AreEqual(4, b[0]);
            Assert.AreEqual(0, b[1]);
            Assert.AreEqual(0x5c, b[2]);
            Assert.AreEqual(0x01, b[3]);

            LittleEndianInputStream inp = new LittleEndianInputStream(
                new MemoryStream(b)
                );

            fr = new UnicodeString.FormatRun(inp);
            Assert.AreEqual(4, fr.CharacterPos);
            Assert.AreEqual(0x15c, fr.FontIndex);
        }
Example #3
0
        /**
         * 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");
        }
Example #4
0
        private static Ptg ReadRefPtg(byte[] formulaRawBytes)
        {
            LittleEndianInput in1 = new LittleEndianInputStream(new MemoryStream(formulaRawBytes));
            byte ptgSid           = (byte)in1.ReadByte();

            switch (ptgSid)
            {
            case AreaPtg.sid: return(new AreaPtg(in1));

            case Area3DPtg.sid: return(new Area3DPtg(in1));

            case RefPtg.sid: return(new RefPtg(in1));

            case Ref3DPtg.sid: return(new Ref3DPtg(in1));
            }
            return(null);
        }
Example #5
0
        public void Test_LbsDropData()
        {
            byte[] data = HexRead.ReadFromString(
                //LbsDropData
                                     "0A, 00, " + //flags
                                     "14, 00, " + //the number of lines to be displayed in the dropdown
                                     "6C, 00, " + //the smallest width in pixels allowed for the dropdown window
                                     "00, 00, " +  //num chars
                                     "00, " +      //compression flag
                                     "00");        //pAdding byte

            LittleEndianInputStream in1 = new LittleEndianInputStream(new MemoryStream(data));

            LbsDropData lbs = new LbsDropData(in1);

            MemoryStream baos = new MemoryStream();
            lbs.Serialize(new LittleEndianOutputStream(baos));

            Assert.IsTrue(Arrays.Equals(data, baos.ToArray()));
        }
        public void TestRead()
        {
            MemoryStream        baos = new MemoryStream();
            ILittleEndianOutput leo  = new LittleEndianOutputStream(baos);

            leo.WriteInt(12345678);
            leo.WriteShort(12345);
            leo.WriteByte(123);
            leo.WriteShort(40000);
            leo.WriteByte(200);
            leo.WriteLong(1234567890123456789L);
            leo.WriteDouble(123.456);

            ILittleEndianInput lei = new LittleEndianInputStream(new MemoryStream(baos.ToArray()));

            Assert.AreEqual(12345678, lei.ReadInt());
            Assert.AreEqual(12345, lei.ReadShort());
            Assert.AreEqual(123, lei.ReadByte());
            Assert.AreEqual(40000, lei.ReadUShort());
            Assert.AreEqual(200, lei.ReadUByte());
            Assert.AreEqual(1234567890123456789L, lei.ReadLong());
            Assert.AreEqual(123.456, lei.ReadDouble(), 0.0);
        }
Example #7
0
        public void TestExtRstFromData()
        {
            byte[] data = new byte[] {
                01, 00, 0x0C, 00,
                00, 00, 0x37, 00,
                00, 00,
                00, 00, 00, 00,
                00, 00 // Cruft at the end, as found from real files
            };
            Assert.AreEqual(16, data.Length);

            LittleEndianInputStream inp = new LittleEndianInputStream(
                new MemoryStream(data)
                );

            UnicodeString.ExtRst ext = new UnicodeString.ExtRst(inp, data.Length);
            Assert.AreEqual(0x0c, ext.DataSize); // Excludes 4 byte header

            Assert.AreEqual(0, ext.NumberOfRuns);
            Assert.AreEqual(0x37, ext.FormattingOptions);
            Assert.AreEqual(0, ext.FormattingFontIndex);
            Assert.AreEqual("", ext.PhoneticText);
            Assert.AreEqual(0, ext.PhRuns.Length);
        }
Example #8
0
        /**
         * Constructs a OBJ record and Sets its fields appropriately.
         *
         * @param in the RecordInputstream to Read the record from
         */

        public ObjRecord(RecordInputStream in1)
        {
            // TODO - problems with OBJ sub-records stream
            // MS spec says first sub-record is always CommonObjectDataSubRecord,
            // and last is
            // always EndSubRecord. OOO spec does not mention ObjRecord(0x005D).
            // Existing POI test data seems to violate that rule. Some test data
            // seems to contain
            // garbage, and a crash is only averted by stopping at what looks like
            // the 'EndSubRecord'

            //Check if this can be continued, if so then the
            //following wont work properly
            //int subSize = 0;
            byte[] subRecordData = in1.ReadRemainder();

            if (LittleEndian.GetUShort(subRecordData, 0) != CommonObjectDataSubRecord.sid)
            {
                // seems to occur in just one junit on "OddStyleRecord.xls" (file created by CrystalReports)
                // Excel tolerates the funny ObjRecord, and replaces it with a corrected version
                // The exact logic/reasoning is not yet understood
                _uninterpretedData = subRecordData;
                subrecords         = null;
                return;
            }
            //if (subRecordData.Length % 2 != 0)
            //{
            //    String msg = "Unexpected length of subRecordData : " + HexDump.ToHex(subRecordData);
            //    throw new RecordFormatException(msg);
            //}
            subrecords = new List <SubRecord>();
            using (MemoryStream bais = new MemoryStream(subRecordData))
            {
                LittleEndianInputStream   subRecStream = new LittleEndianInputStream(bais);
                CommonObjectDataSubRecord cmo          = (CommonObjectDataSubRecord)SubRecord.CreateSubRecord(subRecStream, 0);
                subrecords.Add(cmo);
                while (true)
                {
                    SubRecord subRecord = SubRecord.CreateSubRecord(subRecStream, cmo.ObjectType);
                    subrecords.Add(subRecord);
                    if (subRecord.IsTerminating)
                    {
                        break;
                    }
                }
                int nRemainingBytes = subRecStream.Available();
                if (nRemainingBytes > 0)
                {
                    // At present (Oct-2008), most unit test samples have (subRecordData.length % 2 == 0)
                    _isPaddedToQuadByteMultiple = subRecordData.Length % MAX_PAD_ALIGNMENT == 0;
                    if (nRemainingBytes >= (_isPaddedToQuadByteMultiple ? MAX_PAD_ALIGNMENT : NORMAL_PAD_ALIGNMENT))
                    {
                        if (!CanPaddingBeDiscarded(subRecordData, nRemainingBytes))
                        {
                            String msg = "Leftover " + nRemainingBytes
                                         + " bytes in subrecord data " + HexDump.ToHex(subRecordData);
                            throw new RecordFormatException(msg);
                        }
                        _isPaddedToQuadByteMultiple = false;
                    }
                }
                else
                {
                    _isPaddedToQuadByteMultiple = false;
                }
                _uninterpretedData = null;
            }
        }
Example #9
0
        /**
         * Constructs a OBJ record and Sets its fields appropriately.
         *
         * @param in the RecordInputstream to Read the record from
         */
        public ObjRecord(RecordInputStream in1)
        {
            // TODO - problems with OBJ sub-records stream
            // MS spec says first sub-record is always CommonObjectDataSubRecord,
            // and last is
            // always EndSubRecord. OOO spec does not mention ObjRecord(0x005D).
            // Existing POI test data seems to violate that rule. Some test data
            // seems to contain
            // garbage, and a crash is only averted by stopping at what looks like
            // the 'EndSubRecord'

            //Check if this can be continued, if so then the
            //following wont work properly
            int subSize = 0;
            byte[] subRecordData = in1.ReadRemainder();

            if (LittleEndian.GetUShort(subRecordData, 0) != CommonObjectDataSubRecord.sid)
            {
                // seems to occur in just one junit on "OddStyleRecord.xls" (file created by CrystalReports)
                // Excel tolerates the funny ObjRecord, and replaces it with a corrected version
                // The exact logic/reasoning is not yet understood
                _uninterpretedData = subRecordData;
                subrecords = null;
                return;
            }
            //if (subRecordData.Length % 2 != 0)
            //{
            //    String msg = "Unexpected length of subRecordData : " + HexDump.ToHex(subRecordData);
            //    throw new RecordFormatException(msg);
            //}
            subrecords = new List<SubRecord>();
            MemoryStream bais = new MemoryStream(subRecordData);
            LittleEndianInputStream subRecStream = new LittleEndianInputStream(bais);
            CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord)SubRecord.CreateSubRecord(subRecStream, 0);
            subrecords.Add(cmo);
            while (true)
            {
                SubRecord subRecord = SubRecord.CreateSubRecord(subRecStream, cmo.ObjectType);
                subrecords.Add(subRecord);
             	if (subRecord.IsTerminating) {
                    break;
                }
            }
            int nRemainingBytes = subRecStream.Available();
            if (nRemainingBytes > 0)
            {
                // At present (Oct-2008), most unit test samples have (subRecordData.length % 2 == 0)
                _isPaddedToQuadByteMultiple = subRecordData.Length % MAX_PAD_ALIGNMENT == 0;
                if (nRemainingBytes >= (_isPaddedToQuadByteMultiple ? MAX_PAD_ALIGNMENT : NORMAL_PAD_ALIGNMENT))
                {
                    if (!CanPaddingBeDiscarded(subRecordData, nRemainingBytes))
                    {
                        String msg = "Leftover " + nRemainingBytes
                            + " bytes in subrecord data " + HexDump.ToHex(subRecordData);
                        throw new RecordFormatException(msg);
                    }
                    _isPaddedToQuadByteMultiple = false;
                }
            }
            else
            {
                _isPaddedToQuadByteMultiple = false;
            }
            _uninterpretedData = null;
        }
        private static Ptg ReadRefPtg(byte[] formulaRawBytes)
        {

            LittleEndianInput in1 = new LittleEndianInputStream(new MemoryStream(formulaRawBytes));
            byte ptgSid = (byte)in1.ReadByte();
            switch (ptgSid)
            {
                case AreaPtg.sid: return new AreaPtg(in1);
                case Area3DPtg.sid: return new Area3DPtg(in1);
                case RefPtg.sid: return new RefPtg(in1);
                case Ref3DPtg.sid: return new Ref3DPtg(in1);
            }
            return null;
        }
Example #11
0
        public void TestExtRstFromEmpty()
        {
            UnicodeString.ExtRst ext = new UnicodeString.ExtRst();

            Assert.AreEqual(0, ext.NumberOfRuns);
            Assert.AreEqual(0, ext.FormattingFontIndex);
            Assert.AreEqual(0, ext.FormattingOptions);
            Assert.AreEqual("", ext.PhoneticText);
            Assert.AreEqual(0, ext.PhRuns.Length);
            Assert.AreEqual(10, ext.DataSize); // Excludes 4 byte header

            MemoryStream             baos = new MemoryStream();
            LittleEndianOutputStream out1 = new LittleEndianOutputStream(baos);
            ContinuableRecordOutput  cout = new ContinuableRecordOutput(out1, 0xffff);

            ext.Serialize(cout);
            cout.WriteContinue();

            byte[] b = baos.ToArray();
            Assert.AreEqual(20, b.Length);

            // First 4 bytes from the outputstream
            Assert.AreEqual(-1, (sbyte)b[0]);
            Assert.AreEqual(-1, (sbyte)b[1]);
            Assert.AreEqual(14, b[2]);
            Assert.AreEqual(00, b[3]);

            // Reserved
            Assert.AreEqual(1, b[4]);
            Assert.AreEqual(0, b[5]);
            // Data size
            Assert.AreEqual(10, b[6]);
            Assert.AreEqual(00, b[7]);
            // Font*2
            Assert.AreEqual(0, b[8]);
            Assert.AreEqual(0, b[9]);
            Assert.AreEqual(0, b[10]);
            Assert.AreEqual(0, b[11]);
            // 0 Runs
            Assert.AreEqual(0, b[12]);
            Assert.AreEqual(0, b[13]);
            // Size=0, *2
            Assert.AreEqual(0, b[14]);
            Assert.AreEqual(0, b[15]);
            Assert.AreEqual(0, b[16]);
            Assert.AreEqual(0, b[17]);

            // Last 2 bytes from the outputstream
            Assert.AreEqual(ContinueRecord.sid, b[18]);
            Assert.AreEqual(0, b[19]);


            // Load in again and re-test
            byte[] data = new byte[14];
            Array.Copy(b, 4, data, 0, data.Length);
            LittleEndianInputStream inp = new LittleEndianInputStream(
                new MemoryStream(data)
                );

            ext = new UnicodeString.ExtRst(inp, data.Length);

            Assert.AreEqual(0, ext.NumberOfRuns);
            Assert.AreEqual(0, ext.FormattingFontIndex);
            Assert.AreEqual(0, ext.FormattingOptions);
            Assert.AreEqual("", ext.PhoneticText);
            Assert.AreEqual(0, ext.PhRuns.Length);
        }