internal CompoundDocument(Stream stream, FileHeader header) { this.FileStorage = stream; this.Reader = new BinaryReader(this.FileStorage); // added this "if" to prevent exception when file is read only if( this.FileStorage.CanWrite ) this.Writer = new BinaryWriter(this.FileStorage, Encoding.Unicode); this.Header = header; this.SectorSize = (int)Math.Pow(2, Header.SectorSizeInPot); // prevent errors by limiting sector size to a most used value of 2^9 // compare it to zero because this integer can overflow if (this.SectorSize < 0) this.SectorSize = 512; this.ShortSectorSize = (int)Math.Pow(2, Header.ShortSectorSizeInPot); // prevent errors by limiting sector size to a most used value of 2^6 // compare it to zero because this integer can overflow if (this.ShortSectorSize < 0) this.ShortSectorSize = 64; this.TotalSectors = stream.Length == 0 ? 0 : (int)(stream.Length - 512) / this.SectorSize; this.MasterSectorAllocation = new MasterSectorAllocation(this); this.SectorAllocation = new SectorAllocation(this); this.ShortSectorAllocation = new ShortSectorAllocation(this); }
internal CompoundDocument(Stream stream, FileHeader header) { this.FileStorage = stream; this.Reader = new BinaryReader(this.FileStorage); this.Writer = new BinaryWriter(this.FileStorage, Encoding.Unicode); this.Header = header; this.SectorSize = (int)Math.Pow(2, Header.SectorSizeInPot); this.ShortSectorSize = (int)Math.Pow(2, Header.ShortSectorSizeInPot); this.TotalSectors = stream.Length == 0 ? 0 : (int)(stream.Length - 512) / this.SectorSize; this.MasterSectorAllocation = new MasterSectorAllocation(this); this.SectorAllocation = new SectorAllocation(this); this.ShortSectorAllocation = new ShortSectorAllocation(this); }
private static void WriteHeader(BinaryWriter writer, FileHeader header) { // header size is 512 bytes writer.Write(header.FileTypeIdentifier); writer.Write(header.FileIdentifier.ToByteArray()); writer.Write(header.RevisionNumber); writer.Write(header.VersionNumber); writer.Write(header.ByteOrderMark); writer.Write(header.SectorSizeInPot); writer.Write(header.ShortSectorSizeInPot); writer.Write(header.UnUsed10); writer.Write(header.NumberOfSATSectors); writer.Write(header.FirstSectorIDofDirectoryStream); writer.Write(header.UnUsed4); writer.Write(header.MinimumStreamSize); writer.Write(header.FirstSectorIDofShortSectorAllocationTable); writer.Write(header.NumberOfShortSectors); writer.Write(header.FirstSectorIDofMasterSectorAllocationTable); writer.Write(header.NumberOfMasterSectors); WriteArrayOfInt32(writer, header.MasterSectorAllocationTable); }
private static FileHeader ReadHeader(BinaryReader reader) { FileHeader header = new FileHeader(); header.FileTypeIdentifier = reader.ReadBytes(8); header.FileIdentifier = new Guid(reader.ReadBytes(16)); header.RevisionNumber = reader.ReadUInt16(); header.VersionNumber = reader.ReadUInt16(); header.ByteOrderMark = reader.ReadBytes(2); header.SectorSizeInPot = reader.ReadUInt16(); header.ShortSectorSizeInPot = reader.ReadUInt16(); header.UnUsed10 = reader.ReadBytes(10); header.NumberOfSATSectors = reader.ReadInt32(); header.FirstSectorIDofDirectoryStream = reader.ReadInt32(); header.UnUsed4 = reader.ReadBytes(4); header.MinimumStreamSize = reader.ReadInt32(); header.FirstSectorIDofShortSectorAllocationTable = reader.ReadInt32(); header.NumberOfShortSectors = reader.ReadInt32(); header.FirstSectorIDofMasterSectorAllocationTable = reader.ReadInt32(); header.NumberOfMasterSectors = reader.ReadInt32(); header.MasterSectorAllocationTable = ReadArrayOfInt32(reader, 109); return header; }