示例#1
0
 public XlsBiffStream(XlsHeader hdr, uint streamStart, bool isMini, XlsRootDirectory rootDir,
                      ExcelBinaryReader reader)
     : base(hdr, streamStart, isMini, rootDir)
 {
     this.reader = reader;
     bytes       = base.ReadStream();
     m_size      = bytes.Length;
     m_offset    = 0;
 }
示例#2
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="bytes">byte array representing current object</param>
 /// <param name="header"></param>
 public XlsDirectoryEntry(byte[] bytes, XlsHeader header)
 {
     if (bytes.Length < Length)
     {
         throw new ArgumentException(Errors.ErrorDirectoryEntryArray);
     }
     m_bytes  = bytes;
     m_header = header;
 }
示例#3
0
        public XlsStream(XlsHeader hdr, uint startSector, bool isMini, XlsRootDirectory rootDir)
        {
            m_fileStream  = hdr.FileStream;
            m_fat         = hdr.FAT;
            m_hdr         = hdr;
            m_startSector = startSector;
            m_isMini      = isMini;
            m_rootDir     = rootDir;

            CalculateMiniFat(rootDir);
        }
示例#4
0
        /// <summary>
        /// Constructs FAT table from list of sectors
        /// </summary>
        /// <param name="hdr">XlsHeader</param>
        /// <param name="sectors">Sectors list</param>
        /// <param name="sizeOfSector"></param>
        /// <param name="isMini"></param>
        /// <param name="rootDir"></param>
        public XlsFat(XlsHeader hdr, List <uint> sectors, int sizeOfSector, bool isMini, XlsRootDirectory rootDir)
        {
            m_sectorSize      = sizeOfSector;
            m_isMini          = isMini;
            m_rootDir         = rootDir;
            m_hdr             = hdr;
            m_sectors_for_fat = sectors.Count;
            sizeOfSector      = hdr.SectorSize;
            uint prevSector = 0;

            //calc offset of stream . If mini stream then find mini stream container stream
            //long offset = 0;
            //if (rootDir != null)
            //	offset = isMini ? (hdr.MiniFatFirstSector + 1) * hdr.SectorSize : 0;

            var    buff = new byte[sizeOfSector];
            Stream file = hdr.FileStream;

            using (var ms = new MemoryStream(sizeOfSector * m_sectors_for_fat)) {
                lock (file) {
                    foreach (uint sector in sectors)
                    {
                        if (prevSector == 0 || (sector - prevSector) != 1)
                        {
                            file.Seek((sector + 1) * sizeOfSector, SeekOrigin.Begin);
                        }
                        prevSector = sector;
                        file.Read(buff, 0, sizeOfSector);
                        ms.Write(buff, 0, sizeOfSector);
                    }
                }

                ms.Seek(0, SeekOrigin.Begin);
                var rd = new BinaryReader(ms);
                m_sectors = (int)ms.Length / 4;

                m_fat = new List <uint>(m_sectors);
                for (int i = 0; i < m_sectors; i++)
                {
                    m_fat.Add(rd.ReadUInt32());
                }

                rd.Close();
                ms.Close();
            }
        }
示例#5
0
        /// <summary>
        /// Reads Excel header from Stream
        /// </summary>
        /// <param name="file">Stream with Excel file</param>
        /// <returns>XlsHeader representing specified file</returns>
        public static XlsHeader ReadHeader(Stream file)
        {
            var hdr = new XlsHeader(file);

            lock (file) {
                file.Seek(0, SeekOrigin.Begin);
                file.Read(hdr.m_bytes, 0, 512);
            }
            if (!hdr.IsSignatureValid)
            {
                throw new ArgumentException(Errors.ErrorHeaderSignature);
            }
            if (hdr.ByteOrder != 0xFFFE)
            {
                throw new FormatException(Errors.ErrorHeaderOrder);
            }
            return(hdr);
        }
示例#6
0
        /// <summary>
        /// Creates Root Directory catalog from XlsHeader
        /// </summary>
        /// <param name="hdr">XlsHeader object</param>
        public XlsRootDirectory(XlsHeader hdr)
        {
            var stream  = new XlsStream(hdr, hdr.RootDirectoryEntryStart, false, null);
            var array   = stream.ReadStream();
            var entries = new List <XlsDirectoryEntry>();

            for (int i = 0; i < array.Length; i += XlsDirectoryEntry.Length)
            {
                var tmp = new byte[XlsDirectoryEntry.Length];
                Buffer.BlockCopy(array, i, tmp, 0, tmp.Length);
                entries.Add(new XlsDirectoryEntry(tmp, hdr));
            }
            m_entries = entries;
            foreach (var entry in entries)
            {
                if (m_root == null && entry.EntryType == STGTY.STGTY_ROOT)
                {
                    m_root = entry;
                }

                if (entry.ChildSid != (uint)FATMARKERS.FAT_FreeSpace)
                {
                    entry.Child = entries[(int)entry.ChildSid];
                }

                if (entry.LeftSiblingSid != (uint)FATMARKERS.FAT_FreeSpace)
                {
                    entry.LeftSibling = entries[(int)entry.LeftSiblingSid];
                }

                if (entry.RightSiblingSid != (uint)FATMARKERS.FAT_FreeSpace)
                {
                    entry.RightSibling = entries[(int)entry.RightSiblingSid];
                }
            }
            stream.CalculateMiniFat(this);
        }