public void Test_GET_COMPOUND_VERSION()
        {
            var        f   = new CompoundFile("report_name_fix.xls");
            CFSVersion ver = f.Version;

            Assert.IsTrue(ver == CFSVersion.Ver_3);

            f.Close();
        }
Exemple #2
0
        //public Byte[] ToByteArray()
        //{
        //    MemoryStream ms
        //        = new MemoryStream(128);

        //    BinaryWriter bw = new BinaryWriter(ms);

        //    byte[] paddedName = new byte[64];
        //    Array.Copy(entryName, paddedName, entryName.Length);

        //    bw.Write(paddedName);
        //    bw.Write(nameLength);
        //    bw.Write((byte)stgType);
        //    bw.Write((byte)stgColor);
        //    bw.Write(leftSibling);
        //    bw.Write(rightSibling);
        //    bw.Write(child);
        //    bw.Write(storageCLSID.ToByteArray());
        //    bw.Write(stateBits);
        //    bw.Write(creationDate);
        //    bw.Write(modifyDate);
        //    bw.Write(startSetc);
        //    bw.Write(size);

        //    return ms.ToArray();
        //}

        public void Read(Stream stream, CFSVersion ver = CFSVersion.Ver_3)
        {
            IStreamReader rw = stream.ToStreamReader();

            entryName  = rw.ReadBytes(64);
            nameLength = rw.ReadUInt16();
            stgType    = (StgType)rw.ReadByte();
            //rw.ReadByte();//Ignore color, only black tree
            stgColor     = (StgColor)rw.ReadByte();
            leftSibling  = rw.ReadInt32();
            rightSibling = rw.ReadInt32();
            child        = rw.ReadInt32();

            // Thanks to bugaccount (BugTrack id 3519554)
            if (stgType == StgType.StgInvalid)
            {
                leftSibling  = NOSTREAM;
                rightSibling = NOSTREAM;
                child        = NOSTREAM;
            }

            storageCLSID = new Guid(rw.ReadBytes(16));
            stateBits    = rw.ReadInt32();
            CreationDate = rw.ReadBytes(8);
            ModifyDate   = rw.ReadBytes(8);
            startSetc    = rw.ReadInt32();

            if (ver == CFSVersion.Ver_3)
            {
                // avoid dirty read for version 3 files (max size: 32bit integer)
                // where most significant bits are not initialized to zero

                size = rw.ReadInt32();
                rw.ReadInt32(); //rw.ReadBytes(4); //discard most significant 4 (possibly) dirty bytes
            }
            else
            {
                size = rw.ReadInt64();
            }
        }
Exemple #3
0
        //public Byte[] ToByteArray()
        //{
        //    MemoryStream ms
        //        = new MemoryStream(128);

        //    BinaryWriter bw = new BinaryWriter(ms);

        //    byte[] paddedName = new byte[64];
        //    Array.Copy(entryName, paddedName, entryName.Length);

        //    bw.Write(paddedName);
        //    bw.Write(nameLength);
        //    bw.Write((byte)stgType);
        //    bw.Write((byte)stgColor);
        //    bw.Write(leftSibling);
        //    bw.Write(rightSibling);
        //    bw.Write(child);
        //    bw.Write(storageCLSID.ToByteArray());
        //    bw.Write(stateBits);
        //    bw.Write(creationDate);
        //    bw.Write(modifyDate);
        //    bw.Write(startSetc);
        //    bw.Write(size);

        //    return ms.ToArray();
        //}

        public void Read(Stream stream, CFSVersion version)
        {
            StreamRW rw = new StreamRW(stream);

            entryName  = rw.ReadBytes(64);
            nameLength = rw.ReadUInt16();
            stgType    = (StgType)rw.ReadByte();
            rw.ReadByte();//Ignore color, only black tree
            //stgColor = (StgColor)br.ReadByte();
            leftSibling  = rw.ReadInt32();
            rightSibling = rw.ReadInt32();
            child        = rw.ReadInt32();

            // Thank you to bugaccount (BugTrack id 3519554)
            if (stgType == StgType.StgInvalid)
            {
                leftSibling  = NOSTREAM;
                rightSibling = NOSTREAM;
                child        = NOSTREAM;
            }

            storageCLSID = new Guid(rw.ReadBytes(16));
            stateBits    = rw.ReadInt32();
            creationDate = rw.ReadBytes(8);
            modifyDate   = rw.ReadBytes(8);
            startSetc    = rw.ReadInt32();
            if (version == CFSVersion.Ver_3)
            {
                size = rw.ReadInt32();
                var rest = rw.ReadInt32();
            }
            else
            {
                size = rw.ReadInt64();
            }
        }
        /// <summary>
        ///     Create a new, blank, compound file.
        /// </summary>
        /// <param name="cfsVersion">Use a specific Compound File Version to set 512 or 4096 bytes sectors</param>
        /// <param name="sectorRecycle">If true, recycle unused sectors</param>
        /// <example>
        ///     <code>
        ///  
        ///      byte[] b = new byte[10000];
        ///      for (int i = 0; i &lt; 10000; i++)
        ///      {
        ///          b[i % 120] = (byte)i;
        ///      }
        /// 
        ///      CompoundFile cf = new CompoundFile(CFSVersion.Ver_4, true, true);
        ///      CFStream myStream = cf.RootStorage.AddStream("MyStream");
        /// 
        ///      Assert.IsNotNull(myStream);
        ///      myStream.SetData(b);
        ///      cf.Save("MyCompoundFile.cfs");
        ///      cf.Close();
        ///      
        ///  </code>
        /// </example>
        /// <remarks>
        ///     Sector recycling reduces data writing performances but avoids space wasting in scenarios with frequently
        ///     data manipulation of the same streams. The new compound file is open in Update mode.
        /// </remarks>
        public CompoundFile(CFSVersion cfsVersion, bool sectorRecycle)
        {
            _header = new Header((ushort) cfsVersion);
            _sectorRecycle = sectorRecycle;

            _difatSectorFATEntriesCount = (GetSectorSize()/4) - 1;
            _fatSectorEntriesCount = (GetSectorSize()/4);

            //Root --
            RootStorage = new CFStorage(this);

            RootStorage.DirEntry.SetEntryName("Root Entry");
            RootStorage.DirEntry.StgType = StgType.StgRoot;
        }
Exemple #5
0
        //public Byte[] ToByteArray()
        //{
        //    MemoryStream ms
        //        = new MemoryStream(128);

        //    BinaryWriter bw = new BinaryWriter(ms);

        //    byte[] paddedName = new byte[64];
        //    Array.Copy(entryName, paddedName, entryName.Length);

        //    bw.Write(paddedName);
        //    bw.Write(nameLength);
        //    bw.Write((byte)stgType);
        //    bw.Write((byte)stgColor);
        //    bw.Write(leftSibling);
        //    bw.Write(rightSibling);
        //    bw.Write(child);
        //    bw.Write(storageCLSID.ToByteArray());
        //    bw.Write(stateBits);
        //    bw.Write(creationDate);
        //    bw.Write(modifyDate);
        //    bw.Write(startSetc);
        //    bw.Write(size);

        //    return ms.ToArray();
        //}

        public void Read(Stream stream, CFSVersion version)
        {
            StreamRW rw = new StreamRW(stream);

            entryName = rw.ReadBytes(64);
            nameLength = rw.ReadUInt16();
            stgType = (StgType)rw.ReadByte();
            rw.ReadByte();//Ignore color, only black tree
            //stgColor = (StgColor)br.ReadByte();
            leftSibling = rw.ReadInt32();
            rightSibling = rw.ReadInt32();
            child = rw.ReadInt32();

            // Thank you to bugaccount (BugTrack id 3519554)
            if (stgType == StgType.StgInvalid)
            {
                leftSibling = NOSTREAM;
                rightSibling = NOSTREAM;
                child = NOSTREAM;
            }

            storageCLSID = new Guid(rw.ReadBytes(16));
            stateBits = rw.ReadInt32();
            creationDate = rw.ReadBytes(8);
            modifyDate = rw.ReadBytes(8);
            startSetc = rw.ReadInt32();
            if (version == CFSVersion.Ver_3)
            {
                size = rw.ReadInt32();
                var rest = rw.ReadInt32();
            }
            else
            {
                size = rw.ReadInt64();
            }
        }