Example #1
0
        /// <summary>
        /// Returns mini FAT table
        /// </summary>
        public XlsFat GetMiniFAT(XlsRootDirectory rootDir)
        {
            if (m_minifat != null)
            {
                return(m_minifat);
            }

            if (MiniFatSectorCount == 0 || (long)MiniSectorSize == (long)0xFFFFFFFE)
            {
                return(null);
            }

            //uint value;

            int         miniSectorSize = MiniSectorSize;
            List <uint> sectors        = new List <uint>(MiniFatSectorCount);

            //find the sector where the minifat starts
            var miniFatStartSector = BitConverter.ToUInt32(m_bytes, 0x3c);

            sectors.Add(miniFatStartSector);
            //lock (m_file)
            //{
            //	//work out the file location of minifat then read each sector
            //	var miniFatStartOffset = (miniFatStartSector + 1)*SectorSize;
            //	var miniFatSize = MiniFatSectorCount * SectorSize;
            //	m_file.Seek(miniFatStartOffset, SeekOrigin.Begin);

            //	byte[] sectorBuff = new byte[SectorSize];

            //	for (var i = 0; i < MiniFatSectorCount; i += SectorSize)
            //	{
            //		m_file.Read(sectorBuff, 0, 4);
            //		var secId = BitConverter.ToUInt32(sectorBuff, 0);
            //		sectors.Add(secId);
            //	}
            //}

            m_minifat = new XlsFat(this, sectors, this.MiniSectorSize, true, rootDir);
            return(m_minifat);
        }
        /// <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_isMini          = isMini;
            m_rootDir         = rootDir;
            m_hdr             = hdr;
            m_sectors_for_fat = sectors.Count;
            sizeOfSector      = hdr.SectorSize;
            uint sector = 0, 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;

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

            using (MemoryStream ms = new MemoryStream(sizeOfSector * m_sectors_for_fat))
            {
                lock (file)
                {
                    for (int i = 0; i < sectors.Count; i++)
                    {
                        sector = sectors[i];
                        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);
                BinaryReader 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();
            }
        }
 public void CalculateMiniFat(XlsRootDirectory rootDir)
 {
     m_minifat = m_hdr.GetMiniFAT(rootDir);
 }
Example #4
0
        private void readWorkBookGlobals()
        {
            //Read Header
            try
            {
                m_hdr = XlsHeader.ReadHeader(m_file);
            }
            catch (HeaderException ex)
            {
                fail(ex.Message);
                return;
            }
            catch (FormatException ex)
            {
                fail(ex.Message);
                return;
            }

            XlsRootDirectory  dir           = new XlsRootDirectory(m_hdr);
            XlsDirectoryEntry workbookEntry = dir.FindEntry(WORKBOOK) ?? dir.FindEntry(BOOK);

            if (workbookEntry == null)
            {
                fail(Errors.ErrorStreamWorkbookNotFound); return;
            }

            if (workbookEntry.EntryType != STGTY.STGTY_STREAM)
            {
                fail(Errors.ErrorWorkbookIsNotStream); return;
            }

            m_stream = new XlsBiffStream(m_hdr, workbookEntry.StreamFirstSector, workbookEntry.IsEntryMiniStream, dir);

            m_globals = new XlsWorkbookGlobals();

            m_stream.Seek(0, SeekOrigin.Begin);

            XlsBiffRecord rec = m_stream.Read();
            XlsBiffBOF    bof = rec as XlsBiffBOF;

            if (bof == null || bof.Type != BIFFTYPE.WorkbookGlobals)
            {
                fail(Errors.ErrorWorkbookGlobalsInvalidData); return;
            }

            bool sst = false;

            m_version = bof.Version;
            m_sheets  = new List <XlsWorksheet>();

            while (null != (rec = m_stream.Read()))
            {
                switch (rec.ID)
                {
                case BIFFRECORDTYPE.INTERFACEHDR:
                    m_globals.InterfaceHdr = (XlsBiffInterfaceHdr)rec;
                    break;

                case BIFFRECORDTYPE.BOUNDSHEET:
                    XlsBiffBoundSheet sheet = (XlsBiffBoundSheet)rec;

                    if (sheet.Type != XlsBiffBoundSheet.SheetType.Worksheet)
                    {
                        break;
                    }

                    sheet.IsV8        = isV8();
                    sheet.UseEncoding = m_encoding;

                    m_sheets.Add(new XlsWorksheet(m_globals.Sheets.Count, sheet));
                    m_globals.Sheets.Add(sheet);

                    break;

                case BIFFRECORDTYPE.MMS:
                    m_globals.MMS = rec;
                    break;

                case BIFFRECORDTYPE.COUNTRY:
                    m_globals.Country = rec;
                    break;

                case BIFFRECORDTYPE.CODEPAGE:

                    m_globals.CodePage = (XlsBiffSimpleValueRecord)rec;

                    try
                    {
                        m_encoding = Encoding.GetEncoding(m_globals.CodePage.Value);
                    }
                    catch (ArgumentException)
                    {
                        // Warning - Password protection
                    }

                    break;

                case BIFFRECORDTYPE.FONT:
                case BIFFRECORDTYPE.FONT_V34:
                    m_globals.Fonts.Add(rec);
                    break;

                case BIFFRECORDTYPE.FORMAT_V23:
                {
                    var fmt = (XlsBiffFormatString)rec;
                    fmt.UseEncoding = m_encoding;
                    m_globals.Formats.Add((ushort)m_globals.Formats.Count, fmt);
                }
                break;

                case BIFFRECORDTYPE.FORMAT:
                {
                    var fmt = (XlsBiffFormatString)rec;
                    m_globals.Formats.Add(fmt.Index, fmt);
                }
                break;

                case BIFFRECORDTYPE.XF:
                case BIFFRECORDTYPE.XF_V4:
                case BIFFRECORDTYPE.XF_V3:
                case BIFFRECORDTYPE.XF_V2:
                    m_globals.ExtendedFormats.Add(rec);
                    break;

                case BIFFRECORDTYPE.SST:
                    m_globals.SST = (XlsBiffSST)rec;
                    sst           = true;
                    break;

                case BIFFRECORDTYPE.CONTINUE:
                    if (!sst)
                    {
                        break;
                    }
                    XlsBiffContinue contSST = (XlsBiffContinue)rec;
                    m_globals.SST.Append(contSST);
                    break;

                case BIFFRECORDTYPE.EXTSST:
                    m_globals.ExtSST = rec;
                    sst = false;
                    break;

                case BIFFRECORDTYPE.PROTECT:
                case BIFFRECORDTYPE.PASSWORD:
                case BIFFRECORDTYPE.PROT4REVPASSWORD:
                    //IsProtected
                    break;

                case BIFFRECORDTYPE.EOF:
                    if (m_globals.SST != null)
                    {
                        m_globals.SST.ReadStrings();
                    }
                    return;

                default:
                    continue;
                }
            }
        }