internal static void ValidateHeader(CFHeader header) { if (header.Signature != CFHeader.DefaultSignature) throw new CompoundFileException("Invalid header: signature"); if(header.CLSID != Guid.Empty) throw new CompoundFileException("Invalid header: CLSID"); if(header.MinorVersion != 0x3E) throw new CompoundFileException("Invalid header: Minor version"); if (header.MajorVersion != 3 && header.MajorVersion != 4) throw new CompoundFileException("Invalid header: Major version"); if(header.ByteOrder != 0xFFFE) throw new CompoundFileException("Invalid header: Byte order"); if(header.MajorVersion == 3 && header.SectorShift != 0x0009) throw new CompoundFileException("Invalid header: Sector shirt for v3"); if (header.MajorVersion == 4 && header.SectorShift != 0x000c) throw new CompoundFileException("Invalid header: Sector shirt for v4"); if(header.MiniSectorShift != 0x0006) throw new CompoundFileException("Invalid header: Mini sector shirt"); if(header.Reserved[0] != 0 || header.Reserved[1] != 0 || header.Reserved[2] != 0 || header.Reserved[3] != 0 || header.Reserved[4] != 0 || header.Reserved[5] != 0) throw new CompoundFileException("Invalid header: Reserved"); if(header.MajorVersion == 3 && header.DirectorySectorsCount != 0) throw new CompoundFileException("Invalid header: Directory sectors for v3"); // TODO numbers and first sector if(header.MiniStreamCutoffSize != 0x1000) throw new CompoundFileException("Invalid header: Mini stream cutoff size"); // TODO mini numbers and first sector // TODO mini DIFAT and number DIFAT }
private void Initialize() { header = ReaderUtils.ReadHeader(BaseStream); ReaderUtils.ValidateHeader(header); }
internal static CFHeader ReadHeader(Stream s) { s.Position = 0; byte[] headerBytes = new byte[HeaderSize]; int headerRead = s.Read(headerBytes, 0, HeaderSize); if (headerRead != HeaderSize) { throw new CompoundFileException("Invalid header: eof of file"); } CFHeader header = new CFHeader(); header.Signature = BitConverter.ToUInt64(headerBytes, 0); header.CLSID = new Guid(GetByteArrayPortion(headerBytes, 8, 16)); header.MinorVersion = BitConverter.ToUInt16(headerBytes, 24); header.MajorVersion = BitConverter.ToUInt16(headerBytes, 26); header.ByteOrder = BitConverter.ToUInt16(headerBytes, 28); header.SectorShift = BitConverter.ToUInt16(headerBytes, 30); header.MiniSectorShift = BitConverter.ToUInt16(headerBytes, 32); header.Reserved = GetByteArrayPortion(headerBytes, 34, 6); header.DirectorySectorsCount = BitConverter.ToUInt32(headerBytes, 40); header.FATSectorsCount = BitConverter.ToUInt32(headerBytes, 44); header.FirstDirectorySectorLocation = BitConverter.ToUInt32(headerBytes, 48); header.TransactionSignatureNumber = BitConverter.ToUInt32(headerBytes, 52); header.MiniStreamCutoffSize = BitConverter.ToUInt32(headerBytes, 56); header.FirstMiniFATSectorLocation = BitConverter.ToUInt32(headerBytes, 60); header.MiniFATSectorsCount = BitConverter.ToUInt32(headerBytes, 64); header.FirstDIFATSectorLocation = BitConverter.ToUInt32(headerBytes, 68); header.DIFATSectorsCount = BitConverter.ToUInt32(headerBytes, 72); header.DIFAT = new uint[HeaderDIFATSectorsCount]; for (int i = 0; i < HeaderDIFATSectorsCount; i++) { header.DIFAT[i] = BitConverter.ToUInt32(headerBytes, 76 + i * 4); } // skip checking zeros after the header data return header; }