From 09c6c307d9faa52f5210ecd25697dda1949efcd1 Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Wed, 11 Sep 2024 11:22:52 +1200 Subject: [PATCH 1/6] Align braces (IDE0055) --- sources/OpenMcdf/RBTree/RBTree.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sources/OpenMcdf/RBTree/RBTree.cs b/sources/OpenMcdf/RBTree/RBTree.cs index 3b340444..cb128114 100644 --- a/sources/OpenMcdf/RBTree/RBTree.cs +++ b/sources/OpenMcdf/RBTree/RBTree.cs @@ -33,8 +33,11 @@ public RBTreeDuplicatedItemException(string msg) } } - public enum Color { RED = 0, - BLACK = 1 } + public enum Color + { + RED = 0, + BLACK = 1 + } /// /// Red Black Node interface From 15a9d88fddb18de4437d8721ffa796a7afb2b064 Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Wed, 11 Sep 2024 11:27:51 +1200 Subject: [PATCH 2/6] Use coalesce expression (IDE0029) --- sources/OpenMcdf/RBTree/RBTree.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/OpenMcdf/RBTree/RBTree.cs b/sources/OpenMcdf/RBTree/RBTree.cs index cb128114..7d849575 100644 --- a/sources/OpenMcdf/RBTree/RBTree.cs +++ b/sources/OpenMcdf/RBTree/RBTree.cs @@ -356,7 +356,7 @@ public void Delete(IRBNode template, out IRBNode deletedAlt) } //assert n.left == null || n.right == null; - IRBNode child = (n.Right == null) ? n.Left : n.Right; + IRBNode child = n.Right ?? n.Left; if (NodeColor(n) == Color.BLACK) { n.Color = NodeColor(child); From 776a9885bf52548ad2a26768ff6ebef7fd9dfe10 Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Wed, 11 Sep 2024 11:30:07 +1200 Subject: [PATCH 3/6] Use conditional delegate call (IDE1005) --- sources/OpenMcdf/RBTree/RBTree.cs | 14 ++++---------- .../StreamDataProvider.cs | 6 ++---- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/sources/OpenMcdf/RBTree/RBTree.cs b/sources/OpenMcdf/RBTree/RBTree.cs index 7d849575..41db381c 100644 --- a/sources/OpenMcdf/RBTree/RBTree.cs +++ b/sources/OpenMcdf/RBTree/RBTree.cs @@ -252,10 +252,7 @@ public void Insert(IRBNode newNode) InsertCase1(insertedNode); - if (NodeInserted != null) - { - NodeInserted(insertedNode); - } + NodeInserted?.Invoke(insertedNode); //Trace.WriteLine(" "); //Print(); @@ -482,8 +479,7 @@ private void DoVisitTree(Action action, IRBNode walker) DoVisitTree(action, walker.Left); } - if (action != null) - action(walker); + action?.Invoke(walker); if (walker.Right != null) { @@ -507,8 +503,7 @@ private void DoVisitTreeNodes(Action action, IRBNode walker) DoVisitTreeNodes(action, walker.Left); } - if (action != null) - action(walker); + action?.Invoke(walker); if (walker.Right != null) { @@ -586,8 +581,7 @@ private static void PrintHelper(IRBNode n, int indent) internal void FireNodeOperation(IRBNode node, NodeOperation operation) { - if (NodeOperation != null) - NodeOperation(node, operation); + NodeOperation?.Invoke(node, operation); } //internal void FireValueAssigned(RBNode node, V value) diff --git a/sources/Structured Storage Explorer/StreamDataProvider.cs b/sources/Structured Storage Explorer/StreamDataProvider.cs index 646a5973..8df3c05b 100644 --- a/sources/Structured Storage Explorer/StreamDataProvider.cs +++ b/sources/Structured Storage Explorer/StreamDataProvider.cs @@ -38,8 +38,7 @@ void OnChanged(EventArgs e) { _hasChanges = true; - if (Changed != null) - Changed(this, e); + Changed?.Invoke(this, e); } /// @@ -47,8 +46,7 @@ void OnChanged(EventArgs e) /// void OnLengthChanged(EventArgs e) { - if (LengthChanged != null) - LengthChanged(this, e); + LengthChanged?.Invoke(this, e); } /// From 80d2be7458f3e7633dbfbb5ae5609b0927369441 Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Thu, 5 Sep 2024 16:00:23 +1200 Subject: [PATCH 4/6] Use auto properties (IDE0032) --- .../OLEProperties/PropertySet.cs | 22 +- .../OLEProperties/TypedPropertyValue.cs | 11 +- sources/OpenMcdf/CFItem.cs | 55 ++--- sources/OpenMcdf/CompoundFile.cs | 81 +++---- sources/OpenMcdf/DirectoryEntry.cs | 227 +++++------------- sources/OpenMcdf/Header.cs | 224 +++++------------ sources/OpenMcdf/Sector.cs | 60 ++--- sources/OpenMcdf/SectorCollection.cs | 20 +- sources/OpenMcdf/StreamView.cs | 40 ++- .../StreamDataProvider.cs | 21 +- .../CFSStreamExtensionsTest.cs | 14 +- .../OLEPropertiesExtensionsTest.cs | 14 +- sources/Test/OpenMcdf.Test/CFSStreamTest.cs | 14 +- sources/Test/OpenMcdf.Test/CFSTorageTest.cs | 14 +- .../Test/OpenMcdf.Test/CompoundFileTest.cs | 14 +- sources/Test/OpenMcdf.Test/RBTreeTest.cs | 14 +- .../OpenMcdf.Test/SectorCollectionTest.cs | 14 +- 17 files changed, 257 insertions(+), 602 deletions(-) diff --git a/sources/OpenMcdf.Extensions/OLEProperties/PropertySet.cs b/sources/OpenMcdf.Extensions/OLEProperties/PropertySet.cs index ae1d4fea..15bc2b5d 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/PropertySet.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/PropertySet.cs @@ -16,27 +16,9 @@ public PropertyContext PropertyContext public uint NumProperties { get; set; } - List propertyIdentifierAndOffsets - = new List(); + public List PropertyIdentifierAndOffsets { get; set; } = new List(); - public List PropertyIdentifierAndOffsets - { - get { return propertyIdentifierAndOffsets; } - set { propertyIdentifierAndOffsets = value; } - } - - List properties = new List(); - public List Properties - { - get - { - return properties; - } - set - { - properties = value; - } - } + public List Properties { get; set; } = new List(); public void LoadContext(int propertySetOffset, BinaryReader br) { diff --git a/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs b/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs index 7d15ffde..ff10480d 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs @@ -6,9 +6,6 @@ namespace OpenMcdf.Extensions.OLEProperties { internal abstract class TypedPropertyValue : ITypedPropertyValue { - private bool isVariant = false; - private PropertyDimensions dim = PropertyDimensions.IsScalar; - private VTPropertyType _VTType; public PropertyType PropertyType => PropertyType.TypedPropertyValue; @@ -20,13 +17,13 @@ internal abstract class TypedPropertyValue : ITypedPropertyValue public TypedPropertyValue(VTPropertyType vtType, bool isVariant = false) { this._VTType = vtType; - dim = CheckPropertyDimensions(vtType); - this.isVariant = isVariant; + PropertyDimensions = CheckPropertyDimensions(vtType); + this.IsVariant = isVariant; } - public PropertyDimensions PropertyDimensions => dim; + public PropertyDimensions PropertyDimensions { get; } = PropertyDimensions.IsScalar; - public bool IsVariant => isVariant; + public bool IsVariant { get; } = false; protected virtual bool NeedsPadding { get; set; } = true; diff --git a/sources/OpenMcdf/CFItem.cs b/sources/OpenMcdf/CFItem.cs index b8c2b40e..cd813baf 100644 --- a/sources/OpenMcdf/CFItem.cs +++ b/sources/OpenMcdf/CFItem.cs @@ -37,13 +37,11 @@ namespace OpenMcdf /// public abstract class CFItem : IComparable { - private CompoundFile compoundFile; - - protected CompoundFile CompoundFile => compoundFile; + protected CompoundFile CompoundFile { get; } protected void CheckDisposed() { - if (compoundFile.IsClosed) + if (CompoundFile.IsClosed) throw new CFDisposedException("Owner Compound file has been closed and owned items have been invalidated"); } @@ -53,22 +51,17 @@ protected CFItem() protected CFItem(CompoundFile compoundFile) { - this.compoundFile = compoundFile; + this.CompoundFile = compoundFile; } #region IDirectoryEntry Members - private IDirectoryEntry dirEntry; - internal IDirectoryEntry DirEntry - { - get { return dirEntry; } - set { dirEntry = value; } - } + internal IDirectoryEntry DirEntry { get; set; } internal int CompareTo(CFItem other) { - return this.dirEntry.CompareTo(other.DirEntry); + return this.DirEntry.CompareTo(other.DirEntry); } #endregion @@ -77,7 +70,7 @@ internal int CompareTo(CFItem other) public int CompareTo(object obj) { - return this.dirEntry.CompareTo(((CFItem)obj).DirEntry); + return this.DirEntry.CompareTo(((CFItem)obj).DirEntry); } #endregion @@ -112,7 +105,7 @@ public override bool Equals(object obj) public override int GetHashCode() { - return this.dirEntry.GetEntryName().GetHashCode(); + return this.DirEntry.GetEntryName().GetHashCode(); } /// @@ -122,7 +115,7 @@ public string Name { get { - string n = this.dirEntry.GetEntryName(); + string n = this.DirEntry.GetEntryName(); if (n != null && n.Length > 0) { return n.TrimEnd('\0'); @@ -136,7 +129,7 @@ public string Name /// Size in bytes of the item. It has a valid value /// only if entity is a stream, otherwise it is set to zero. /// - public long Size => this.dirEntry.Size; + public long Size => this.DirEntry.Size; /// /// Return true if item is Storage @@ -145,7 +138,7 @@ public string Name /// This check doesn't use reflection or runtime type information /// and doesn't suffer related performance penalties. /// - public bool IsStorage => this.dirEntry.StgType == StgType.StgStorage; + public bool IsStorage => this.DirEntry.StgType == StgType.StgStorage; /// /// Return true if item is a Stream @@ -154,7 +147,7 @@ public string Name /// This check doesn't use reflection or runtime type information /// and doesn't suffer related performance penalties. /// - public bool IsStream => this.dirEntry.StgType == StgType.StgStream; + public bool IsStream => this.DirEntry.StgType == StgType.StgStream; /// /// Return true if item is the Root Storage @@ -163,7 +156,7 @@ public string Name /// This check doesn't use reflection or runtime type information /// and doesn't suffer related performance penalties. /// - public bool IsRoot => this.dirEntry.StgType == StgType.StgRoot; + public bool IsRoot => this.DirEntry.StgType == StgType.StgRoot; /// /// Get/Set the Creation Date of the current item @@ -172,13 +165,13 @@ public DateTime CreationDate { get { - return DateTime.FromFileTime(BitConverter.ToInt64(this.dirEntry.CreationDate, 0)); + return DateTime.FromFileTime(BitConverter.ToInt64(this.DirEntry.CreationDate, 0)); } set { - if (this.dirEntry.StgType != StgType.StgStream && this.dirEntry.StgType != StgType.StgRoot) - this.dirEntry.CreationDate = BitConverter.GetBytes(value.ToFileTime()); + if (this.DirEntry.StgType != StgType.StgStream && this.DirEntry.StgType != StgType.StgRoot) + this.DirEntry.CreationDate = BitConverter.GetBytes(value.ToFileTime()); else throw new CFException("Creation Date can only be set on storage entries"); } @@ -191,13 +184,13 @@ public DateTime ModifyDate { get { - return DateTime.FromFileTime(BitConverter.ToInt64(this.dirEntry.ModifyDate, 0)); + return DateTime.FromFileTime(BitConverter.ToInt64(this.DirEntry.ModifyDate, 0)); } set { - if (this.dirEntry.StgType != StgType.StgStream && this.dirEntry.StgType != StgType.StgRoot) - this.dirEntry.ModifyDate = BitConverter.GetBytes(value.ToFileTime()); + if (this.DirEntry.StgType != StgType.StgStream && this.DirEntry.StgType != StgType.StgRoot) + this.DirEntry.ModifyDate = BitConverter.GetBytes(value.ToFileTime()); else throw new CFException("Modify Date can only be set on storage entries"); } @@ -210,13 +203,13 @@ public Guid CLSID { get { - return this.dirEntry.StorageCLSID; + return this.DirEntry.StorageCLSID; } set { - if (this.dirEntry.StgType != StgType.StgStream) + if (this.DirEntry.StgType != StgType.StgStream) { - this.dirEntry.StorageCLSID = value; + this.DirEntry.StorageCLSID = value; } else throw new CFException("Object class GUID can only be set on Root and Storage entries"); @@ -225,13 +218,13 @@ public Guid CLSID int IComparable.CompareTo(CFItem other) { - return this.dirEntry.CompareTo(other.DirEntry); + return this.DirEntry.CompareTo(other.DirEntry); } public override string ToString() { - if (this.dirEntry != null) - return "[" + this.dirEntry.LeftSibling + "," + this.dirEntry.SID + "," + this.dirEntry.RightSibling + "]" + " " + this.dirEntry.GetEntryName(); + if (this.DirEntry != null) + return "[" + this.DirEntry.LeftSibling + "," + this.DirEntry.SID + "," + this.DirEntry.RightSibling + "]" + " " + this.DirEntry.GetEntryName(); else return string.Empty; } diff --git a/sources/OpenMcdf/CompoundFile.cs b/sources/OpenMcdf/CompoundFile.cs index b1cc2eda..178ad993 100644 --- a/sources/OpenMcdf/CompoundFile.cs +++ b/sources/OpenMcdf/CompoundFile.cs @@ -117,12 +117,10 @@ public enum CFSUpdateMode /// public class CompoundFile : IDisposable { - private CFSConfiguration configuration; - /// /// Get the configuration parameters of the CompoundFile object. /// - public CFSConfiguration Configuration => configuration; + public CFSConfiguration Configuration { get; private set; } /// /// Returns the size of standard sectors switching on CFS version (3 or 4) @@ -163,9 +161,7 @@ internal int GetSectorSize() /// private bool eraseFreeSectors = false; - private bool validationExceptionEnabled = true; - - public bool ValidationExceptionEnabled => validationExceptionEnabled; + public bool ValidationExceptionEnabled { get; private set; } = true; private CFSUpdateMode updateMode = CFSUpdateMode.ReadOnly; private string fileName = string.Empty; @@ -272,7 +268,7 @@ public CompoundFile(CFSVersion cfsVersion, CFSConfiguration configFlags) rootDir.StgColor = StgColor.Black; //this.InsertNewDirectoryEntry(rootDir); - rootStorage = new CFStorage(this, rootDir); + RootStorage = new CFStorage(this, rootDir); } /// @@ -432,7 +428,7 @@ public void Commit() /// public void Commit(bool releaseMemory) { - if (_disposed) + if (IsClosed) throw new CFDisposedException("Compound File closed: cannot commit data"); if (updateMode != CFSUpdateMode.Update) @@ -595,8 +591,8 @@ public void Commit(bool releaseMemory) /// private void SetConfigurationOptions(CFSConfiguration configParameters) { - this.configuration = configParameters; - this.validationExceptionEnabled = !configParameters.HasFlag(CFSConfiguration.NoValidationException); + this.Configuration = configParameters; + this.ValidationExceptionEnabled = !configParameters.HasFlag(CFSConfiguration.NoValidationException); this.sectorRecycle = configParameters.HasFlag(CFSConfiguration.SectorRecycle); this.eraseFreeSectors = configParameters.HasFlag(CFSConfiguration.EraseFreeSectors); this.closeStream = !configParameters.HasFlag(CFSConfiguration.LeaveOpen); @@ -617,7 +613,7 @@ private void Load(Stream stream) header.Read(stream); - if (!configuration.HasFlag(CFSConfiguration.NoValidationException)) + if (!Configuration.HasFlag(CFSConfiguration.NoValidationException)) { ValidateHeader(header); } @@ -636,7 +632,7 @@ private void Load(Stream stream) LoadDirectories(); - this.rootStorage + this.RootStorage = new CFStorage(this, directoryEntries[0]); } catch (Exception) @@ -732,7 +728,7 @@ StreamView miniStreamView = new StreamView( miniStream, GetSectorSize(), - this.rootStorage.Size, + this.RootStorage.Size, null, sourceStream); @@ -775,7 +771,7 @@ StreamView miniStreamView = new StreamView( miniStream, GetSectorSize(), - this.rootStorage.Size, + this.RootStorage.Size, null, sourceStream); @@ -790,11 +786,11 @@ StreamView miniStreamView // Allocate, position ministream at the end of already allocated // ministream's sectors - miniStreamView.Seek(this.rootStorage.Size + Sector.MINISECTOR_SIZE, SeekOrigin.Begin); + miniStreamView.Seek(this.RootStorage.Size + Sector.MINISECTOR_SIZE, SeekOrigin.Begin); //miniStreamView.Write(s.GetData(), 0, Sector.MINISECTOR_SIZE); s.Id = (int)(miniStreamView.Position - Sector.MINISECTOR_SIZE) / Sector.MINISECTOR_SIZE; - this.rootStorage.DirEntry.Size = miniStreamView.Length; + this.RootStorage.DirEntry.Size = miniStreamView.Length; } } @@ -819,7 +815,7 @@ StreamView miniStreamView //Update HEADER and root storage when ministream changes if (miniFAT.Count > 0) { - this.rootStorage.DirEntry.StartSetc = miniStream[0].Id; + this.RootStorage.DirEntry.StartSetc = miniStream[0].Id; header.MiniFATSectorsNumber = (uint)miniFAT.Count; header.FirstMiniFATSectorID = miniFAT[0].Id; } @@ -909,7 +905,7 @@ StreamView miniFATView = new StreamView(miniFAT, GetSectorSize(), header.MiniFATSectorsNumber * Sector.MINISECTOR_SIZE, null, sourceStream); StreamView miniStreamView - = new StreamView(miniStream, GetSectorSize(), this.rootStorage.Size, null, sourceStream); + = new StreamView(miniStream, GetSectorSize(), this.RootStorage.Size, null, sourceStream); // Set updated/new sectors within the ministream ---------- if (zeroSector) @@ -954,7 +950,7 @@ StreamView miniStreamView //Update HEADER and root storage when ministream changes if (miniFAT.Count > 0) { - this.rootStorage.DirEntry.StartSetc = miniStream[0].Id; + this.RootStorage.DirEntry.StartSetc = miniStream[0].Id; header.MiniFATSectorsNumber = (uint)miniFAT.Count; header.FirstMiniFATSectorID = miniFAT[0].Id; } @@ -1260,7 +1256,7 @@ int nextSecID if (this.closeStream) this.Close(); - if (this.validationExceptionEnabled) + if (this.ValidationExceptionEnabled) throw new CFCorruptedFileException("DIFAT sectors count mismatched. Corrupted compound file"); } @@ -1282,7 +1278,7 @@ int nextSecID private void EnsureUniqueSectorIndex(int nextSecID, HashSet processedSectors) { - if (!this.validationExceptionEnabled) + if (!this.ValidationExceptionEnabled) { return; } @@ -1459,7 +1455,7 @@ StreamView miniFATView = new StreamView(miniFAT, GetSectorSize(), header.MiniFATSectorsNumber * Sector.MINISECTOR_SIZE, null, sourceStream); StreamView miniStreamView = - new StreamView(miniStream, GetSectorSize(), rootStorage.Size, null, sourceStream); + new StreamView(miniStream, GetSectorSize(), RootStorage.Size, null, sourceStream); BinaryReader miniFATReader = new BinaryReader(miniFATView); @@ -1522,8 +1518,6 @@ internal List GetSectorChain(int secID, SectorType chainType) } } - private CFStorage rootStorage; - /// /// The entry point object that represents the /// root of the structures tree to get or set storage or @@ -1549,7 +1543,7 @@ internal List GetSectorChain(int secID, SectorType chainType) /// ncf.Close(); /// /// - public CFStorage RootStorage => rootStorage; + public CFStorage RootStorage { get; private set; } public CFSVersion Version => (CFSVersion)this.header.MajorVersion; @@ -1703,7 +1697,7 @@ private bool ValidateSibling(int sid) // if this siblings id does not overflow current list if (sid >= directoryEntries.Count) { - if (this.validationExceptionEnabled) + if (this.ValidationExceptionEnabled) { //this.Close(); throw new CFCorruptedFileException("A Directory Entry references the non-existent sid number " + sid.ToString()); @@ -1715,7 +1709,7 @@ private bool ValidateSibling(int sid) //if this sibling is valid... if (directoryEntries[sid].StgType == StgType.StgInvalid) { - if (this.validationExceptionEnabled) + if (this.ValidationExceptionEnabled) { //this.Close(); throw new CFCorruptedFileException("A Directory Entry has a valid reference to an Invalid Storage Type directory [" + sid + "]"); @@ -1726,7 +1720,7 @@ private bool ValidateSibling(int sid) if (!Enum.IsDefined(typeof(StgType), directoryEntries[sid].StgType)) { - if (this.validationExceptionEnabled) + if (this.ValidationExceptionEnabled) { //this.Close(); throw new CFCorruptedFileException("A Directory Entry has an invalid Storage Type"); @@ -1837,7 +1831,7 @@ public void SaveAs(string fileName) [Obsolete("Use SaveAs method")] public void Save(string fileName) { - if (_disposed) + if (IsClosed) throw new CFException("Compound File closed: cannot save data"); FileStream fs = null; @@ -1911,7 +1905,7 @@ public void Save(string fileName) /// public void Save(Stream stream) { - if (_disposed) + if (IsClosed) throw new CFDisposedException("Compound File closed: cannot save data"); if (!stream.CanSeek) @@ -2013,7 +2007,7 @@ List miniStream = GetSectorChain(RootEntry.StartSetc, SectorType.Normal); StreamView miniStreamView - = new StreamView(miniStream, GetSectorSize(), rootStorage.Size, null, sourceStream); + = new StreamView(miniStream, GetSectorSize(), RootStorage.Size, null, sourceStream); int idx = 0; @@ -2371,7 +2365,7 @@ internal int ReadData(CFStream cFStream, long position, byte[] buffer, int offse internal byte[] GetData(CFStream cFStream) { - if (_disposed) + if (IsClosed) throw new CFDisposedException("Compound File closed: cannot access data"); byte[] result = null; @@ -2405,7 +2399,7 @@ StreamView sView public byte[] GetDataBySID(int sid) { - if (_disposed) + if (IsClosed) throw new CFDisposedException("Compound File closed: cannot access data"); if (sid < 0) return null; @@ -2439,7 +2433,7 @@ StreamView sView public Guid getGuidBySID(int sid) { - if (_disposed) + if (IsClosed) throw new CFDisposedException("Compound File closed: cannot access data"); if (sid < 0) throw new CFException("Invalid SID"); @@ -2449,7 +2443,7 @@ public Guid getGuidBySID(int sid) public Guid getGuidForStream(int sid) { - if (_disposed) + if (IsClosed) throw new CFDisposedException("Compound File closed: cannot access data"); if (sid < 0) throw new CFException("Invalid SID"); @@ -2550,7 +2544,6 @@ public void Close(bool closeStream) #region IDisposable Members - private bool _disposed; //false void IDisposable.Dispose() { @@ -2571,7 +2564,7 @@ protected virtual void Dispose(bool disposing) { try { - if (!_disposed) + if (!IsClosed) { lock (lockObject) { @@ -2585,7 +2578,7 @@ protected virtual void Dispose(bool disposing) sectors = null; } - this.rootStorage = null; // Some problem releasing resources... + this.RootStorage = null; // Some problem releasing resources... this.header = null; this.directoryEntries.Clear(); this.directoryEntries = null; @@ -2596,18 +2589,18 @@ protected virtual void Dispose(bool disposing) #endif } - if (this.sourceStream != null && closeStream && !configuration.HasFlag(CFSConfiguration.LeaveOpen)) + if (this.sourceStream != null && closeStream && !Configuration.HasFlag(CFSConfiguration.LeaveOpen)) this.sourceStream.Close(); } } } finally { - _disposed = true; + IsClosed = true; } } - internal bool IsClosed => _disposed; + internal bool IsClosed { get; private set; } private List directoryEntries = new List(); @@ -2666,14 +2659,14 @@ public IList GetAllNamedEntries(string entryName) public int GetNumDirectories() { - if (_disposed) + if (IsClosed) throw new CFDisposedException("Compound File closed: cannot access data"); return directoryEntries.Count; } public string GetNameDirEntry(int id) { - if (_disposed) + if (IsClosed) throw new CFDisposedException("Compound File closed: cannot access data"); if (id < 0) throw new CFException("Invalid Storage ID"); @@ -2682,7 +2675,7 @@ public string GetNameDirEntry(int id) public StgType GetStorageType(int id) { - if (_disposed) + if (IsClosed) throw new CFDisposedException("Compound File closed: cannot access data"); if (id < 0) throw new CFException("Invalid Storage ID"); diff --git a/sources/OpenMcdf/DirectoryEntry.cs b/sources/OpenMcdf/DirectoryEntry.cs index f979eb2d..f2e1154b 100644 --- a/sources/OpenMcdf/DirectoryEntry.cs +++ b/sources/OpenMcdf/DirectoryEntry.cs @@ -35,12 +35,7 @@ internal sealed class DirectoryEntry : IDirectoryEntry internal const int OTHER_IS_GREATER = -1; private IList dirRepository; - private int sid = -1; - public int SID - { - get { return sid; } - set { sid = value; } - } + public int SID { get; set; } = -1; internal static int NOSTREAM = unchecked((int)0xFFFFFFFF); @@ -52,11 +47,11 @@ private DirectoryEntry(string name, StgType stgType, IList dirR { this.dirRepository = dirRepository; - this.stgType = stgType; + this.StgType = stgType; if (stgType == StgType.StgStorage) { - this.creationDate = BitConverter.GetBytes(DateTime.Now.ToFileTime()); + this.CreationDate = BitConverter.GetBytes(DateTime.Now.ToFileTime()); this.StartSetc = ZERO; } @@ -71,15 +66,13 @@ private DirectoryEntry(string name, StgType stgType, IList dirR } } - private byte[] entryName = new byte[64]; - - public byte[] EntryName => entryName; + public byte[] EntryName { get; private set; } = new byte[64]; public string GetEntryName() { - if (entryName != null && entryName.Length > 0) + if (EntryName != null && EntryName.Length > 0) { - return Encoding.Unicode.GetString(entryName).Remove((this.nameLength - 1) / 2); + return Encoding.Unicode.GetString(EntryName).Remove((this.nameLength - 1) / 2); } else return string.Empty; @@ -89,7 +82,7 @@ public void SetEntryName(string entryName) { if (entryName == string.Empty) { - this.entryName = new byte[64]; + this.EntryName = new byte[64]; this.nameLength = 0; } else @@ -111,7 +104,7 @@ public void SetEntryName(string entryName) newName[temp.Length] = 0x00; newName[temp.Length + 1] = 0x00; - this.entryName = newName; + this.EntryName = newName; this.nameLength = (ushort)(temp.Length + 2); } } @@ -129,53 +122,15 @@ public ushort NameLength } } - private StgType stgType = StgType.StgInvalid; - public StgType StgType - { - get - { - return stgType; - } - set - { - stgType = value; - } - } - - private StgColor stgColor = StgColor.Red; + public StgType StgType { get; set; } = StgType.StgInvalid; - public StgColor StgColor - { - get - { - return stgColor; - } - set - { - stgColor = value; - } - } + public StgColor StgColor { get; set; } = StgColor.Red; - private int leftSibling = NOSTREAM; - public int LeftSibling - { - get { return leftSibling; } - set { leftSibling = value; } - } + public int LeftSibling { get; set; } = NOSTREAM; - private int rightSibling = NOSTREAM; - public int RightSibling - { - get { return rightSibling; } - set { rightSibling = value; } - } + public int RightSibling { get; set; } = NOSTREAM; - private int child = NOSTREAM; - public int Child - { - get { return child; } - set { child = value; } - } + public int Child { get; set; } = NOSTREAM; private Guid storageCLSID = Guid.Empty; @@ -192,67 +147,15 @@ public Guid StorageCLSID } } - private int stateBits; - - public int StateBits - { - get { return stateBits; } - set { stateBits = value; } - } - - private byte[] creationDate = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - - public byte[] CreationDate - { - get - { - return creationDate; - } - set - { - creationDate = value; - } - } + public int StateBits { get; set; } - private byte[] modifyDate = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + public byte[] CreationDate { get; set; } = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - public byte[] ModifyDate - { - get - { - return modifyDate; - } - set - { - modifyDate = value; - } - } + public byte[] ModifyDate { get; set; } = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - private int startSetc = Sector.ENDOFCHAIN; - public int StartSetc - { - get - { - return startSetc; - } - set - { - startSetc = value; - } - } + public int StartSetc { get; set; } = Sector.ENDOFCHAIN; - private long size; - public long Size - { - get - { - return size; - } - set - { - size = value; - } - } + public long Size { get; set; } public int CompareTo(object obj) { @@ -314,26 +217,26 @@ private static ulong fnv_hash(byte[] buffer) public override int GetHashCode() { - return (int)fnv_hash(this.entryName); + return (int)fnv_hash(this.EntryName); } public void Write(Stream stream) { StreamRW rw = new StreamRW(stream); - rw.Write(entryName); + rw.Write(EntryName); rw.Write(nameLength); - rw.Write((byte)stgType); - rw.Write((byte)stgColor); - rw.Write(leftSibling); - rw.Write(rightSibling); - rw.Write(child); + rw.Write((byte)StgType); + rw.Write((byte)StgColor); + rw.Write(LeftSibling); + rw.Write(RightSibling); + rw.Write(Child); rw.Write(storageCLSID.ToByteArray()); - rw.Write(stateBits); - rw.Write(creationDate); - rw.Write(modifyDate); - rw.Write(startSetc); - rw.Write(size); + rw.Write(StateBits); + rw.Write(CreationDate); + rw.Write(ModifyDate); + rw.Write(StartSetc); + rw.Write(Size); rw.Close(); } @@ -369,40 +272,40 @@ public void Read(Stream stream, CFSVersion ver = CFSVersion.Ver_3) { StreamRW rw = new StreamRW(stream); - entryName = rw.ReadBytes(64); + EntryName = rw.ReadBytes(64); nameLength = rw.ReadUInt16(); - stgType = (StgType)rw.ReadByte(); + StgType = (StgType)rw.ReadByte(); //rw.ReadByte();//Ignore color, only black tree - stgColor = (StgColor)rw.ReadByte(); - leftSibling = rw.ReadInt32(); - rightSibling = rw.ReadInt32(); - child = rw.ReadInt32(); + StgColor = (StgColor)rw.ReadByte(); + LeftSibling = rw.ReadInt32(); + RightSibling = rw.ReadInt32(); + Child = rw.ReadInt32(); // Thanks to bugaccount (BugTrack id 3519554) - if (stgType == StgType.StgInvalid) + if (StgType == StgType.StgInvalid) { - leftSibling = NOSTREAM; - rightSibling = NOSTREAM; - child = NOSTREAM; + 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(); + 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(); + Size = rw.ReadInt32(); rw.ReadBytes(4); //discard most significant 4 (possibly) dirty bytes } else { - size = rw.ReadInt64(); + Size = rw.ReadInt64(); } } @@ -412,17 +315,17 @@ public RedBlackTree.IRBNode Left { get { - if (leftSibling == NOSTREAM) + if (LeftSibling == NOSTREAM) return null; - return dirRepository[leftSibling]; + return dirRepository[LeftSibling]; } set { - leftSibling = value != null ? ((IDirectoryEntry)value).SID : NOSTREAM; + LeftSibling = value != null ? ((IDirectoryEntry)value).SID : NOSTREAM; - if (leftSibling != NOSTREAM) - dirRepository[leftSibling].Parent = this; + if (LeftSibling != NOSTREAM) + dirRepository[LeftSibling].Parent = this; } } @@ -430,17 +333,17 @@ public RedBlackTree.IRBNode Right { get { - if (rightSibling == NOSTREAM) + if (RightSibling == NOSTREAM) return null; - return dirRepository[rightSibling]; + return dirRepository[RightSibling]; } set { - rightSibling = value != null ? ((IDirectoryEntry)value).SID : NOSTREAM; + RightSibling = value != null ? ((IDirectoryEntry)value).SID : NOSTREAM; - if (rightSibling != NOSTREAM) - dirRepository[rightSibling].Parent = this; + if (RightSibling != NOSTREAM) + dirRepository[RightSibling].Parent = this; } } @@ -540,7 +443,7 @@ internal static IDirectoryEntry TryNew(string name, StgType stgType, IList headerSignature; + public byte[] CLSID { get; set; } = new byte[16]; - //8 16 Unique identifier (UID) of this file (not of interest in the following, may be all 0) - private byte[] clsid = new byte[16]; + public ushort MinorVersion { get; private set; } = 0x003E; - public byte[] CLSID - { - get { return clsid; } - set { clsid = value; } - } - - //24 2 Revision number of the file format (most used is 003EH) - private ushort minorVersion = 0x003E; - - public ushort MinorVersion => minorVersion; - - //26 2 Version number of the file format (most used is 0003H) - private ushort majorVersion = 0x0003; - - public ushort MajorVersion => majorVersion; - - //28 2 Byte order identifier (➜4.2): FEH FFH = Little-Endian FFH FEH = Big-Endian - private ushort byteOrder = 0xFFFE; - - public ushort ByteOrder => byteOrder; - - //30 2 Size of a sector in the compound document file (➜3.1) in power-of-two (ssz), real sector - //size is sec_size = 2ssz bytes (minimum value is 7 which means 128 bytes, most used - //value is 9 which means 512 bytes) - private ushort sectorShift = 9; + public ushort MajorVersion { get; private set; } = 0x0003; - public ushort SectorShift => sectorShift; + public ushort ByteOrder { get; private set; } = 0xFFFE; - //32 2 Size of a short-sector in the short-stream container stream (➜6.1) in power-of-two (sssz), - //real short-sector size is short_sec_size = 2sssz bytes (maximum value is sector size - //ssz, see above, most used value is 6 which means 64 bytes) - private ushort miniSectorShift = 6; - public ushort MiniSectorShift => miniSectorShift; + public ushort SectorShift { get; private set; } = 9; - //34 10 Not used - private byte[] unUsed = new byte[6]; + public ushort MiniSectorShift { get; private set; } = 6; - public byte[] UnUsed => unUsed; + public byte[] UnUsed { get; private set; } = new byte[6]; - //44 4 Total number of sectors used Directory (➜5.2) - private int directorySectorsNumber; + public int DirectorySectorsNumber { get; set; } - public int DirectorySectorsNumber - { - get { return directorySectorsNumber; } - set { directorySectorsNumber = value; } - } - - //44 4 Total number of sectors used for the sector allocation table (➜5.2) - private int fatSectorsNumber; + public int FATSectorsNumber { get; set; } - public int FATSectorsNumber - { - get { return fatSectorsNumber; } - set { fatSectorsNumber = value; } - } - - //48 4 SecID of first sector of the directory stream (➜7) - private int firstDirectorySectorID = Sector.ENDOFCHAIN; - - public int FirstDirectorySectorID - { - get { return firstDirectorySectorID; } - set { firstDirectorySectorID = value; } - } + public int FirstDirectorySectorID { get; set; } = Sector.ENDOFCHAIN; - //52 4 Not used - private uint unUsed2; + public uint UnUsed2 { get; private set; } - public uint UnUsed2 => unUsed2; - - //56 4 Minimum size of a standard stream (in bytes, minimum allowed and most used size is 4096 - //bytes), streams with an actual size smaller than (and not equal to) this value are stored as - //short-streams (➜6) - private uint minSizeStandardStream = 4096; - - public uint MinSizeStandardStream - { - get { return minSizeStandardStream; } - set { minSizeStandardStream = value; } - } - - //60 4 SecID of first sector of the short-sector allocation table (➜6.2), or –2 (End Of Chain - //SecID, ➜3.1) if not extant - private int firstMiniFATSectorID = unchecked((int)0xFFFFFFFE); + public uint MinSizeStandardStream { get; set; } = 4096; /// /// This integer field contains the starting sector number for the mini FAT /// - public int FirstMiniFATSectorID - { - get { return firstMiniFATSectorID; } - set { firstMiniFATSectorID = value; } - } + public int FirstMiniFATSectorID { get; set; } = unchecked((int)0xFFFFFFFE); - //64 4 Total number of sectors used for the short-sector allocation table (➜6.2) - private uint miniFATSectorsNumber; + public uint MiniFATSectorsNumber { get; set; } - public uint MiniFATSectorsNumber - { - get { return miniFATSectorsNumber; } - set { miniFATSectorsNumber = value; } - } - - //68 4 SecID of first sector of the master sector allocation table (➜5.1), or –2 (End Of Chain - //SecID, ➜3.1) if no additional sectors used - private int firstDIFATSectorID = Sector.ENDOFCHAIN; - - public int FirstDIFATSectorID - { - get { return firstDIFATSectorID; } - set { firstDIFATSectorID = value; } - } - - //72 4 Total number of sectors used for the master sector allocation table (➜5.1) - private uint difatSectorsNumber; - - public uint DIFATSectorsNumber - { - get { return difatSectorsNumber; } - set { difatSectorsNumber = value; } - } + public int FirstDIFATSectorID { get; set; } = Sector.ENDOFCHAIN; - //76 436 First part of the master sector allocation table (➜5.1) containing 109 SecIDs - private int[] difat = new int[109]; + public uint DIFATSectorsNumber { get; set; } - public int[] DIFAT => difat; + public int[] DIFAT { get; } = new int[109]; public Header() : this(3) @@ -159,13 +61,13 @@ public Header(ushort version) switch (version) { case 3: - this.majorVersion = 3; - this.sectorShift = 0x0009; + this.MajorVersion = 3; + this.SectorShift = 0x0009; break; case 4: - this.majorVersion = 4; - this.sectorShift = 0x000C; + this.MajorVersion = 4; + this.SectorShift = 0x000C; break; default: @@ -174,7 +76,7 @@ public Header(ushort version) for (int i = 0; i < 109; i++) { - difat[i] = Sector.FREESECT; + DIFAT[i] = Sector.FREESECT; } } @@ -182,30 +84,30 @@ public void Write(Stream stream) { StreamRW rw = new StreamRW(stream); - rw.Write(headerSignature); - rw.Write(clsid); - rw.Write(minorVersion); - rw.Write(majorVersion); - rw.Write(byteOrder); - rw.Write(sectorShift); - rw.Write(miniSectorShift); - rw.Write(unUsed); - rw.Write(directorySectorsNumber); - rw.Write(fatSectorsNumber); - rw.Write(firstDirectorySectorID); - rw.Write(unUsed2); - rw.Write(minSizeStandardStream); - rw.Write(firstMiniFATSectorID); - rw.Write(miniFATSectorsNumber); - rw.Write(firstDIFATSectorID); - rw.Write(difatSectorsNumber); - - foreach (int i in difat) + rw.Write(HeaderSignature); + rw.Write(CLSID); + rw.Write(MinorVersion); + rw.Write(MajorVersion); + rw.Write(ByteOrder); + rw.Write(SectorShift); + rw.Write(MiniSectorShift); + rw.Write(UnUsed); + rw.Write(DirectorySectorsNumber); + rw.Write(FATSectorsNumber); + rw.Write(FirstDirectorySectorID); + rw.Write(UnUsed2); + rw.Write(MinSizeStandardStream); + rw.Write(FirstMiniFATSectorID); + rw.Write(MiniFATSectorsNumber); + rw.Write(FirstDIFATSectorID); + rw.Write(DIFATSectorsNumber); + + foreach (int i in DIFAT) { rw.Write(i); } - if (majorVersion == 4) + if (MajorVersion == 4) { byte[] zeroHead = new byte[3584]; rw.Write(zeroHead); @@ -218,25 +120,25 @@ public void Read(Stream stream) { StreamRW rw = new StreamRW(stream); - headerSignature = rw.ReadBytes(8); + HeaderSignature = rw.ReadBytes(8); CheckSignature(); - clsid = rw.ReadBytes(16); - minorVersion = rw.ReadUInt16(); - majorVersion = rw.ReadUInt16(); + CLSID = rw.ReadBytes(16); + MinorVersion = rw.ReadUInt16(); + MajorVersion = rw.ReadUInt16(); CheckVersion(); - byteOrder = rw.ReadUInt16(); - sectorShift = rw.ReadUInt16(); - miniSectorShift = rw.ReadUInt16(); - unUsed = rw.ReadBytes(6); - directorySectorsNumber = rw.ReadInt32(); - fatSectorsNumber = rw.ReadInt32(); - firstDirectorySectorID = rw.ReadInt32(); - unUsed2 = rw.ReadUInt32(); - minSizeStandardStream = rw.ReadUInt32(); - firstMiniFATSectorID = rw.ReadInt32(); - miniFATSectorsNumber = rw.ReadUInt32(); - firstDIFATSectorID = rw.ReadInt32(); - difatSectorsNumber = rw.ReadUInt32(); + ByteOrder = rw.ReadUInt16(); + SectorShift = rw.ReadUInt16(); + MiniSectorShift = rw.ReadUInt16(); + UnUsed = rw.ReadBytes(6); + DirectorySectorsNumber = rw.ReadInt32(); + FATSectorsNumber = rw.ReadInt32(); + FirstDirectorySectorID = rw.ReadInt32(); + UnUsed2 = rw.ReadUInt32(); + MinSizeStandardStream = rw.ReadUInt32(); + FirstMiniFATSectorID = rw.ReadInt32(); + MiniFATSectorsNumber = rw.ReadUInt32(); + FirstDIFATSectorID = rw.ReadInt32(); + DIFATSectorsNumber = rw.ReadUInt32(); for (int i = 0; i < 109; i++) { @@ -248,7 +150,7 @@ public void Read(Stream stream) private void CheckVersion() { - if (this.majorVersion != 3 && this.majorVersion != 4) + if (this.MajorVersion != 3 && this.MajorVersion != 4) throw new CFFileFormatException("Unsupported Binary File Format version: OpenMcdf only supports Compound Files with major version equal to 3 or 4 "); } @@ -259,9 +161,9 @@ private void CheckVersion() private void CheckSignature() { - for (int i = 0; i < headerSignature.Length; i++) + for (int i = 0; i < HeaderSignature.Length; i++) { - if (headerSignature[i] != OLE_CFS_SIGNATURE[i]) + if (HeaderSignature[i] != OLE_CFS_SIGNATURE[i]) throw new CFFileFormatException("Invalid OLE structured storage file"); } } diff --git a/sources/OpenMcdf/Sector.cs b/sources/OpenMcdf/Sector.cs index 30f73b3b..c5976dfd 100644 --- a/sources/OpenMcdf/Sector.cs +++ b/sources/OpenMcdf/Sector.cs @@ -30,59 +30,37 @@ internal sealed class Sector : IDisposable public const int FATSECT = unchecked((int)0xFFFFFFFD); public const int DIFSECT = unchecked((int)0xFFFFFFFC); - private bool dirtyFlag = false; + public bool DirtyFlag { get; set; } = false; - public bool DirtyFlag - { - get { return dirtyFlag; } - set { dirtyFlag = value; } - } - - public bool IsStreamed => (stream != null && size != MINISECTOR_SIZE) ? (this.id * size) + size < stream.Length : false; + public bool IsStreamed => (stream != null && Size != MINISECTOR_SIZE) ? (this.Id * Size) + Size < stream.Length : false; - private int size = 0; private Stream stream; public Sector(int size, Stream stream) { - this.size = size; + this.Size = size; this.stream = stream; } public Sector(int size, byte[] data) { - this.size = size; + this.Size = size; this.data = data; this.stream = null; } public Sector(int size) { - this.size = size; + this.Size = size; this.data = null; this.stream = null; } - private SectorType type; - - internal SectorType Type - { - get { return type; } - set { type = value; } - } + internal SectorType Type { get; set; } - private int id = -1; - - public int Id - { - get { return id; } - set - { - id = value; - } - } + public int Id { get; set; } = -1; - public int Size => size; + public int Size { get; private set; } = 0; private byte[] data; @@ -90,12 +68,12 @@ public byte[] GetData() { if (this.data == null) { - data = new byte[size]; + data = new byte[Size]; if (IsStreamed) { - stream.Seek(size + id * (long)size, SeekOrigin.Begin); - stream.Read(data, 0, size); + stream.Seek(Size + Id * (long)Size, SeekOrigin.Begin); + stream.Read(data, 0, Size); } } @@ -120,18 +98,18 @@ public byte[] GetData() public void ZeroData() { - data = new byte[size]; - dirtyFlag = true; + data = new byte[Size]; + DirtyFlag = true; } public void InitFATData() { - data = new byte[size]; + data = new byte[Size]; - for (int i = 0; i < size; i++) + for (int i = 0; i < Size; i++) data[i] = 0xFF; - dirtyFlag = true; + DirtyFlag = true; } internal void ReleaseData() @@ -154,9 +132,9 @@ void IDisposable.Dispose() lock (lockObject) { this.data = null; - this.dirtyFlag = false; - this.id = ENDOFCHAIN; - this.size = 0; + this.DirtyFlag = false; + this.Id = ENDOFCHAIN; + this.Size = 0; } } } diff --git a/sources/OpenMcdf/SectorCollection.cs b/sources/OpenMcdf/SectorCollection.cs index 9322ad3f..40f3a391 100644 --- a/sources/OpenMcdf/SectorCollection.cs +++ b/sources/OpenMcdf/SectorCollection.cs @@ -28,8 +28,6 @@ internal sealed class SectorCollection : IList private const int MAX_SECTOR_V4_COUNT_LOCK_RANGE = 524287; //0x7FFFFF00 for Version 4 private const int SLICE_SIZE = 4096; - private int count = 0; - public event Ver3SizeLimitReached OnVer3SizeLimitReached; private List largeArraySlices = new List(); @@ -41,7 +39,7 @@ public SectorCollection() private bool sizeLimitReached = false; private void DoCheckSizeLimitReached() { - if (OnVer3SizeLimitReached != null && !sizeLimitReached && (count - 1 > MAX_SECTOR_V4_COUNT_LOCK_RANGE)) + if (OnVer3SizeLimitReached != null && !sizeLimitReached && (Count - 1 > MAX_SECTOR_V4_COUNT_LOCK_RANGE)) { sizeLimitReached = true; OnVer3SizeLimitReached(); @@ -72,7 +70,7 @@ public Sector this[int index] int itemIndex = index / SLICE_SIZE; int itemOffset = index % SLICE_SIZE; - if ((index > -1) && (index < count)) + if ((index > -1) && (index < Count)) { return (Sector)largeArraySlices[itemIndex][itemOffset]; } @@ -85,7 +83,7 @@ public Sector this[int index] int itemIndex = index / SLICE_SIZE; int itemOffset = index % SLICE_SIZE; - if (index > -1 && index < count) + if (index > -1 && index < Count) { largeArraySlices[itemIndex][itemOffset] = value; } @@ -100,22 +98,22 @@ public Sector this[int index] private int add(Sector item) { - int itemIndex = count / SLICE_SIZE; + int itemIndex = Count / SLICE_SIZE; if (itemIndex < largeArraySlices.Count) { largeArraySlices[itemIndex].Add(item); - count++; + Count++; } else { ArrayList ar = new ArrayList(SLICE_SIZE); ar.Add(item); largeArraySlices.Add(ar); - count++; + Count++; } - return count - 1; + return Count - 1; } public void Add(Sector item) @@ -134,7 +132,7 @@ public void Clear() largeArraySlices.Clear(); - count = 0; + Count = 0; } public bool Contains(Sector item) @@ -147,7 +145,7 @@ public void CopyTo(Sector[] array, int arrayIndex) throw new NotImplementedException(); } - public int Count => count; + public int Count { get; private set; } = 0; public bool IsReadOnly => false; diff --git a/sources/OpenMcdf/StreamView.cs b/sources/OpenMcdf/StreamView.cs index 1ae23bba..60332546 100644 --- a/sources/OpenMcdf/StreamView.cs +++ b/sources/OpenMcdf/StreamView.cs @@ -20,8 +20,6 @@ internal sealed class StreamView : Stream private int sectorSize; private long position; - - private List sectorChain; private Stream stream; private bool isFatStream = false; private List freeSectors = new List(); @@ -35,7 +33,7 @@ public StreamView(List sectorChain, int sectorSize, Stream stream) if (sectorSize <= 0) throw new CFException("Sector size must be greater than zero"); - this.sectorChain = sectorChain; + this.BaseSectorChain = sectorChain; this.sectorSize = sectorSize; this.stream = stream; } @@ -47,7 +45,7 @@ public StreamView(List sectorChain, int sectorSize, long length, Queue BaseSectorChain => sectorChain; + public List BaseSectorChain { get; } public override bool CanRead => true; @@ -101,7 +99,7 @@ public override int Read(byte[] buffer, int offset, int count) long intMax = Math.Min(int.MaxValue, this.length); count = Math.Min((int)intMax, count); - if (sectorChain != null && sectorChain.Count > 0) + if (BaseSectorChain != null && BaseSectorChain.Count > 0) { // First sector int secIndex = (int)(position / sectorSize); @@ -110,13 +108,13 @@ public override int Read(byte[] buffer, int offset, int count) // and sector border nToRead = Math.Min( - sectorChain[0].Size - ((int)position % sectorSize), + BaseSectorChain[0].Size - ((int)position % sectorSize), count); - if (secIndex < sectorChain.Count) + if (secIndex < BaseSectorChain.Count) { Buffer.BlockCopy( - sectorChain[secIndex].GetData(), + BaseSectorChain[secIndex].GetData(), (int)(position % sectorSize), buffer, offset, @@ -133,7 +131,7 @@ public override int Read(byte[] buffer, int offset, int count) nToRead = sectorSize; Buffer.BlockCopy( - sectorChain[secIndex].GetData(), + BaseSectorChain[secIndex].GetData(), 0, buffer, offset + nRead, @@ -148,10 +146,10 @@ public override int Read(byte[] buffer, int offset, int count) if (nToRead != 0) { - if (secIndex > sectorChain.Count) throw new CFCorruptedFileException("The file is probably corrupted."); + if (secIndex > BaseSectorChain.Count) throw new CFCorruptedFileException("The file is probably corrupted."); Buffer.BlockCopy( - sectorChain[secIndex].GetData(), + BaseSectorChain[secIndex].GetData(), 0, buffer, offset + nRead, @@ -200,7 +198,7 @@ private void AdjustLength(long value, Queue availableSectors) { this.length = value; - long delta = value - (sectorChain.Count * (long)sectorSize); + long delta = value - (BaseSectorChain.Count * (long)sectorSize); if (delta > 0) { @@ -229,7 +227,7 @@ private void AdjustLength(long value, Queue availableSectors) t.InitFATData(); } - sectorChain.Add(t); + BaseSectorChain.Add(t); nSec--; } @@ -280,7 +278,7 @@ public override void Write(byte[] buffer, int offset, int count) if ((position + count) > length) AdjustLength(position + count); - if (sectorChain != null) + if (BaseSectorChain != null) { // First sector int secOffset = (int)(position / sectorSize); @@ -288,16 +286,16 @@ public override void Write(byte[] buffer, int offset, int count) roundByteWritten = Math.Min(sectorSize - (int)(position % sectorSize), count); - if (secOffset < sectorChain.Count) + if (secOffset < BaseSectorChain.Count) { Buffer.BlockCopy( buffer, offset, - sectorChain[secOffset].GetData(), + BaseSectorChain[secOffset].GetData(), secShift, roundByteWritten); - sectorChain[secOffset].DirtyFlag = true; + BaseSectorChain[secOffset].DirtyFlag = true; } byteWritten += roundByteWritten; @@ -312,11 +310,11 @@ public override void Write(byte[] buffer, int offset, int count) Buffer.BlockCopy( buffer, offset, - sectorChain[secOffset].GetData(), + BaseSectorChain[secOffset].GetData(), 0, roundByteWritten); - sectorChain[secOffset].DirtyFlag = true; + BaseSectorChain[secOffset].DirtyFlag = true; byteWritten += roundByteWritten; offset += roundByteWritten; @@ -331,11 +329,11 @@ public override void Write(byte[] buffer, int offset, int count) Buffer.BlockCopy( buffer, offset, - sectorChain[secOffset].GetData(), + BaseSectorChain[secOffset].GetData(), 0, roundByteWritten); - sectorChain[secOffset].DirtyFlag = true; + BaseSectorChain[secOffset].DirtyFlag = true; offset += roundByteWritten; byteWritten += roundByteWritten; diff --git a/sources/Structured Storage Explorer/StreamDataProvider.cs b/sources/Structured Storage Explorer/StreamDataProvider.cs index 8df3c05b..9183935c 100644 --- a/sources/Structured Storage Explorer/StreamDataProvider.cs +++ b/sources/Structured Storage Explorer/StreamDataProvider.cs @@ -16,18 +16,13 @@ public class StreamDataProvider : IByteProvider /// bool _hasChanges; - /// - /// Contains a byte collection. - /// - ByteCollection _bytes; - /// /// Initializes a new instance of the DynamicByteProvider class. /// /// public StreamDataProvider(CFStream modifiedStream) { - _bytes = new ByteCollection(modifiedStream.GetData()); + Bytes = new ByteCollection(modifiedStream.GetData()); _modifiedStream = modifiedStream; } @@ -52,7 +47,7 @@ void OnLengthChanged(EventArgs e) /// /// Gets the byte collection. /// - public ByteCollection Bytes => _bytes; + public ByteCollection Bytes { get; } #region IByteProvider Members /// @@ -70,7 +65,7 @@ public void ApplyChanges() { _hasChanges = false; - _modifiedStream.SetData(this._bytes.ToArray()); + _modifiedStream.SetData(this.Bytes.ToArray()); } /// @@ -89,7 +84,7 @@ public void ApplyChanges() /// the index of the byte to read /// the byte public byte ReadByte(long index) - { return _bytes[(int)index]; } + { return Bytes[(int)index]; } /// /// Write a byte into the byte collection. @@ -98,7 +93,7 @@ public byte ReadByte(long index) /// the byte public void WriteByte(long index, byte value) { - _bytes[(int)index] = value; + Bytes[(int)index] = value; OnChanged(EventArgs.Empty); } @@ -111,7 +106,7 @@ public void DeleteBytes(long index, long length) { int internal_index = (int)Math.Max(0, index); int internal_length = (int)Math.Min((int)Length, length); - _bytes.RemoveRange(internal_index, internal_length); + Bytes.RemoveRange(internal_index, internal_length); OnLengthChanged(EventArgs.Empty); OnChanged(EventArgs.Empty); @@ -124,7 +119,7 @@ public void DeleteBytes(long index, long length) /// the byte array to insert public void InsertBytes(long index, byte[] bs) { - _bytes.InsertRange((int)index, bs); + Bytes.InsertRange((int)index, bs); OnLengthChanged(EventArgs.Empty); OnChanged(EventArgs.Empty); @@ -133,7 +128,7 @@ public void InsertBytes(long index, byte[] bs) /// /// Gets the length of the bytes in the byte collection. /// - public long Length => _bytes.Count; + public long Length => Bytes.Count; /// /// Returns true diff --git a/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs b/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs index bcd5cc91..e1eb7c0d 100644 --- a/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs +++ b/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs @@ -15,23 +15,11 @@ public CFSStreamExtensionsTest() { } - private TestContext testContextInstance; - /// /// Gets or sets the test context which provides /// information about and functionality for the current test run. /// - public TestContext TestContext - { - get - { - return testContextInstance; - } - set - { - testContextInstance = value; - } - } + public TestContext TestContext { get; set; } #region Additional test attributes // diff --git a/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs b/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs index 5f9c2769..bce9c9f1 100644 --- a/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs +++ b/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs @@ -18,23 +18,11 @@ public OLEPropertiesExtensionsTest() { } - private TestContext testContextInstance; - /// /// Gets or sets the test context which provides /// information about and functionality for the current test run. /// - public TestContext TestContext - { - get - { - return testContextInstance; - } - set - { - testContextInstance = value; - } - } + public TestContext TestContext { get; set; } #region Additional test attributes // diff --git a/sources/Test/OpenMcdf.Test/CFSStreamTest.cs b/sources/Test/OpenMcdf.Test/CFSStreamTest.cs index 00cbf028..cd88a87a 100644 --- a/sources/Test/OpenMcdf.Test/CFSStreamTest.cs +++ b/sources/Test/OpenMcdf.Test/CFSStreamTest.cs @@ -18,23 +18,11 @@ public CFSStreamTest() { } - private TestContext testContextInstance; - /// /// Gets or sets the test context which provides /// information about and functionality for the current test run. /// - public TestContext TestContext - { - get - { - return testContextInstance; - } - set - { - testContextInstance = value; - } - } + public TestContext TestContext { get; set; } #region Additional test attributes // diff --git a/sources/Test/OpenMcdf.Test/CFSTorageTest.cs b/sources/Test/OpenMcdf.Test/CFSTorageTest.cs index 8f96f0c6..8508ae2a 100644 --- a/sources/Test/OpenMcdf.Test/CFSTorageTest.cs +++ b/sources/Test/OpenMcdf.Test/CFSTorageTest.cs @@ -17,23 +17,11 @@ public CFSTorageTest() { } - private TestContext testContextInstance; - /// /// Gets or sets the test context which provides /// information about and functionality for the current test run. /// - public TestContext TestContext - { - get - { - return testContextInstance; - } - set - { - testContextInstance = value; - } - } + public TestContext TestContext { get; set; } #region Additional test attributes // diff --git a/sources/Test/OpenMcdf.Test/CompoundFileTest.cs b/sources/Test/OpenMcdf.Test/CompoundFileTest.cs index 05a81957..4b185a02 100644 --- a/sources/Test/OpenMcdf.Test/CompoundFileTest.cs +++ b/sources/Test/OpenMcdf.Test/CompoundFileTest.cs @@ -17,23 +17,11 @@ public CompoundFileTest() { } - private TestContext testContextInstance; - /// /// Gets or sets the test context which provides /// information about and functionality for the current test run. /// - public TestContext TestContext - { - get - { - return testContextInstance; - } - set - { - testContextInstance = value; - } - } + public TestContext TestContext { get; set; } #region Additional test attributes // diff --git a/sources/Test/OpenMcdf.Test/RBTreeTest.cs b/sources/Test/OpenMcdf.Test/RBTreeTest.cs index 3cf86d71..7a8e9a93 100644 --- a/sources/Test/OpenMcdf.Test/RBTreeTest.cs +++ b/sources/Test/OpenMcdf.Test/RBTreeTest.cs @@ -15,23 +15,11 @@ public RBTreeTest() { } - private TestContext testContextInstance; - /// /// Gets or sets the test context which provides /// information about and functionality for the current test run. /// - public TestContext TestContext - { - get - { - return testContextInstance; - } - set - { - testContextInstance = value; - } - } + public TestContext TestContext { get; set; } #region Additional test attributes // diff --git a/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs b/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs index 74e2492d..19b2134a 100644 --- a/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs +++ b/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs @@ -10,23 +10,11 @@ namespace OpenMcdf.Test [TestClass()] public class SectorCollectionTest { - private TestContext testContextInstance; - /// /// Gets or sets the test context which provides /// information about and functionality for the current test run. /// - public TestContext TestContext - { - get - { - return testContextInstance; - } - set - { - testContextInstance = value; - } - } + public TestContext TestContext { get; set; } #region Additional test attributes // From 96560124f771b8dff33814333be1cf2393d81e53 Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Wed, 11 Sep 2024 11:29:22 +1200 Subject: [PATCH 5/6] Remove unnecessary value assignment (IDE0059) --- .../OLEProperties/PropertyFactory.cs | 3 +- .../OLEProperties/TypedPropertyValue.cs | 5 +-- sources/OpenMcdf/CFStorage.cs | 3 +- sources/OpenMcdf/CompoundFile.cs | 41 ++++++------------- sources/OpenMcdf/DirectoryEntry.cs | 6 +-- sources/OpenMcdf/RBTree/RBTree.cs | 1 - sources/OpenMcdf/StreamView.cs | 12 ++---- .../Structured Storage Explorer/MainForm.cs | 7 +--- sources/Test/OpenMcdf.Test/CFSStreamTest.cs | 25 ++++------- .../Test/OpenMcdf.Test/CompoundFileTest.cs | 4 +- .../OpenMcdf.Test/SectorCollectionTest.cs | 3 -- 11 files changed, 30 insertions(+), 80 deletions(-) diff --git a/sources/OpenMcdf.Extensions/OLEProperties/PropertyFactory.cs b/sources/OpenMcdf.Extensions/OLEProperties/PropertyFactory.cs index fb770821..24320ff3 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/PropertyFactory.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/PropertyFactory.cs @@ -16,8 +16,7 @@ static PropertyFactory() public ITypedPropertyValue NewProperty(VTPropertyType vType, int codePage, uint propertyIdentifier, bool isVariant = false) { - ITypedPropertyValue pr = null; - + ITypedPropertyValue pr; switch ((VTPropertyType)((ushort)vType & 0x00FF)) { case VTPropertyType.VT_I1: diff --git a/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs b/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs index ff10480d..70682667 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs @@ -110,9 +110,8 @@ public void Read(System.IO.BinaryReader br) public void Write(BinaryWriter bw) { long currentPos = bw.BaseStream.Position; - int size = 0; - int m = 0; - + int size; + int m; switch (this.PropertyDimensions) { case PropertyDimensions.IsScalar: diff --git a/sources/OpenMcdf/CFStorage.cs b/sources/OpenMcdf/CFStorage.cs index 150c340d..fe84e7ed 100644 --- a/sources/OpenMcdf/CFStorage.cs +++ b/sources/OpenMcdf/CFStorage.cs @@ -442,7 +442,6 @@ IDirectoryEntry cfo catch (RBTreeDuplicatedItemException) { CompoundFile.ResetDirectoryEntry(cfo.SID); - cfo = null; throw new CFDuplicatedItemException("An entry with name '" + storageName + "' is already present in storage '" + this.Name + "' "); } @@ -547,7 +546,7 @@ public void Delete(string entryName) if (((IDirectoryEntry)foundObj).StgType == StgType.StgRoot) throw new CFException("Root storage cannot be removed"); - IRBNode altDel = null; + IRBNode altDel; switch (((IDirectoryEntry)foundObj).StgType) { case StgType.StgStorage: diff --git a/sources/OpenMcdf/CompoundFile.cs b/sources/OpenMcdf/CompoundFile.cs index 178ad993..6cb85051 100644 --- a/sources/OpenMcdf/CompoundFile.cs +++ b/sources/OpenMcdf/CompoundFile.cs @@ -826,8 +826,7 @@ internal void FreeData(CFStream stream) if (stream.Size == 0) return; - List sectorChain = null; - + List sectorChain; if (stream.Size < header.MinSizeStandardStream) { sectorChain = GetSectorChain(stream.DirEntry.StartSetc, SectorType.Mini); @@ -1212,20 +1211,13 @@ StreamView difatStream /// A list of DIFAT sectors private List GetDifatSectorChain() { - int validationCount = 0; - List result = new List(); - - int nextSecID - = Sector.ENDOFCHAIN; - HashSet processedSectors = new HashSet(); if (header.DIFATSectorsNumber != 0) { - validationCount = (int)header.DIFATSectorsNumber; - + int validationCount = (int)header.DIFATSectorsNumber; Sector s = sectors[header.FirstDIFATSectorID]; if (s == null) //Lazy loading @@ -1240,7 +1232,7 @@ int nextSecID while (true && validationCount >= 0) { - nextSecID = BitConverter.ToInt32(s.GetData(), GetSectorSize() - 4); + int nextSecID = BitConverter.ToInt32(s.GetData(), GetSectorSize() - 4); EnsureUniqueSectorIndex(nextSecID, processedSectors); // Strictly speaking, the following condition is not correct from @@ -1299,14 +1291,12 @@ private List GetFatSectorChain() List result = new List(); - - int nextSecID - = Sector.ENDOFCHAIN; - List difatSectors = GetDifatSectorChain(); int idx = 0; + + int nextSecID; // Read FAT entries from the header Fat entry array (max 109 entries) while (idx < header.FATSectorsNumber && idx < N_HEADER_FAT_ENTRY) { @@ -1446,8 +1436,6 @@ List result if (secID != Sector.ENDOFCHAIN) { - int nextSecID = secID; - List miniFAT = GetNormalSectorChain(header.FirstMiniFATSectorID); List miniStream = GetNormalSectorChain(RootEntry.StartSetc); @@ -1459,8 +1447,7 @@ StreamView miniFATView BinaryReader miniFATReader = new BinaryReader(miniFATView); - nextSecID = secID; - + int nextSecID = secID; HashSet processedSectors = new HashSet(); while (true) @@ -2113,8 +2100,7 @@ internal void SetStreamLength(CFItem cfItem, long length) } Queue freeList = null; - StreamView sv = null; - + StreamView sv; if (!transitionToMini && !transitionToNormal) //############ NO TRANSITION { if (delta > 0) // Enlarging stream... @@ -2321,8 +2307,7 @@ internal int ReadData(CFStream cFStream, long position, byte[] buffer, int count count = (int)Math.Min(de.Size - position, count); - StreamView sView = null; - + StreamView sView; if (de.Size < header.MinSizeStandardStream) { sView @@ -2345,8 +2330,7 @@ internal int ReadData(CFStream cFStream, long position, byte[] buffer, int offse count = (int)Math.Min(buffer.Length - offset, (long)count); - StreamView sView = null; - + StreamView sView; if (de.Size < header.MinSizeStandardStream) { sView @@ -2367,11 +2351,10 @@ internal byte[] GetData(CFStream cFStream) { if (IsClosed) throw new CFDisposedException("Compound File closed: cannot access data"); - - byte[] result = null; - IDirectoryEntry de = cFStream.DirEntry; + + byte[] result; //IDirectoryEntry root = directoryEntries[0]; if (de.Size < header.MinSizeStandardStream) @@ -2403,7 +2386,7 @@ public byte[] GetDataBySID(int sid) throw new CFDisposedException("Compound File closed: cannot access data"); if (sid < 0) return null; - byte[] result = null; + byte[] result; try { IDirectoryEntry de = directoryEntries[sid]; diff --git a/sources/OpenMcdf/DirectoryEntry.cs b/sources/OpenMcdf/DirectoryEntry.cs index f2e1154b..cd5f1c1a 100644 --- a/sources/OpenMcdf/DirectoryEntry.cs +++ b/sources/OpenMcdf/DirectoryEntry.cs @@ -96,10 +96,8 @@ public void SetEntryName(string entryName) if (entryName.Length > 31) throw new CFException("Entry name MUST NOT exceed 31 characters"); - - byte[] newName = null; byte[] temp = Encoding.Unicode.GetBytes(entryName); - newName = new byte[64]; + byte[] newName = new byte[64]; Buffer.BlockCopy(temp, 0, newName, 0, temp.Length); newName[temp.Length] = 0x00; newName[temp.Length + 1] = 0x00; @@ -393,7 +391,7 @@ public RedBlackTree.IRBNode Uncle() internal static IDirectoryEntry New(string name, StgType stgType, IList dirRepository) { - DirectoryEntry de = null; + DirectoryEntry de; if (dirRepository != null) { de = new DirectoryEntry(name, stgType, dirRepository); diff --git a/sources/OpenMcdf/RBTree/RBTree.cs b/sources/OpenMcdf/RBTree/RBTree.cs index 41db381c..08512cc4 100644 --- a/sources/OpenMcdf/RBTree/RBTree.cs +++ b/sources/OpenMcdf/RBTree/RBTree.cs @@ -340,7 +340,6 @@ public void Delete(IRBNode template, out IRBNode deletedAlt) { deletedAlt = null; IRBNode n = LookupNode(template); - template = n; if (n == null) return; // Key not found, do nothing if (n.Left != null && n.Right != null) diff --git a/sources/OpenMcdf/StreamView.cs b/sources/OpenMcdf/StreamView.cs index 60332546..6f83c8b0 100644 --- a/sources/OpenMcdf/StreamView.cs +++ b/sources/OpenMcdf/StreamView.cs @@ -93,7 +93,6 @@ public int ReadInt32() public override int Read(byte[] buffer, int offset, int count) { int nRead = 0; - int nToRead = 0; // Don't try to read more bytes than this stream contains. long intMax = Math.Min(int.MaxValue, this.length); @@ -107,7 +106,7 @@ public override int Read(byte[] buffer, int offset, int count) // Bytes to read count is the min between request count // and sector border - nToRead = Math.Min( + int nToRead = Math.Min( BaseSectorChain[0].Size - ((int)position % sectorSize), count); @@ -208,8 +207,7 @@ private void AdjustLength(long value, Queue availableSectors) while (nSec > 0) { - Sector t = null; - + Sector t; if (availableSectors == null || availableSectors.Count == 0) { t = new Sector(sectorSize, stream); @@ -272,7 +270,6 @@ public void WriteInt32(int val) public override void Write(byte[] buffer, int offset, int count) { int byteWritten = 0; - int roundByteWritten = 0; // Assure length if ((position + count) > length) @@ -284,7 +281,7 @@ public override void Write(byte[] buffer, int offset, int count) int secOffset = (int)(position / sectorSize); int secShift = (int)(position % sectorSize); - roundByteWritten = Math.Min(sectorSize - (int)(position % sectorSize), count); + int roundByteWritten = Math.Min(sectorSize - (int)(position % sectorSize), count); if (secOffset < BaseSectorChain.Count) { @@ -334,9 +331,6 @@ public override void Write(byte[] buffer, int offset, int count) roundByteWritten); BaseSectorChain[secOffset].DirtyFlag = true; - - offset += roundByteWritten; - byteWritten += roundByteWritten; } position += count; diff --git a/sources/Structured Storage Explorer/MainForm.cs b/sources/Structured Storage Explorer/MainForm.cs index 0047d23f..f28a3787 100644 --- a/sources/Structured Storage Explorer/MainForm.cs +++ b/sources/Structured Storage Explorer/MainForm.cs @@ -104,9 +104,7 @@ private void CreateNewFile() private void RefreshTree() { treeView1.Nodes.Clear(); - - TreeNode root = null; - root = treeView1.Nodes.Add("Root Entry", "Root"); + TreeNode root = treeView1.Nodes.Add("Root Entry", "Root"); root.ImageIndex = 0; root.Tag = cf.RootStorage; @@ -249,7 +247,6 @@ private void exportDataToolStripMenuItem_Click(object sender, EventArgs e) { fs.Flush(); fs.Close(); - fs = null; } } } @@ -334,8 +331,6 @@ private void addStorageStripMenuItem1_Click(object sender, EventArgs e) private void importDataStripMenuItem1_Click(object sender, EventArgs e) { - string fileName = string.Empty; - if (openDataFileDialog.ShowDialog() == DialogResult.OK) { CFStream s = treeView1.SelectedNode.Tag as CFStream; diff --git a/sources/Test/OpenMcdf.Test/CFSStreamTest.cs b/sources/Test/OpenMcdf.Test/CFSStreamTest.cs index cd88a87a..bc50b94f 100644 --- a/sources/Test/OpenMcdf.Test/CFSStreamTest.cs +++ b/sources/Test/OpenMcdf.Test/CFSStreamTest.cs @@ -759,12 +759,10 @@ public void Test_RESIZE_STREAM_NO_TRANSITION() { int INITIAL_SIZE = 1024 * 1024 * 2; int DELTA_SIZE = 300; - - CompoundFile cf = null; //CFStream st = null; byte[] b = Helpers.GetBuffer(INITIAL_SIZE); //2MB buffer - cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.Default); + CompoundFile cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.Default); cf.RootStorage.AddStream("AStream").SetData(b); cf.Save("$Test_RESIZE_STREAM.cfs"); cf.Close(); @@ -791,8 +789,6 @@ public void Test_RESIZE_STREAM_NO_TRANSITION() public void Test_RESIZE_STREAM_TRANSITION_TO_MINI() { string FILE_NAME = "$Test_RESIZE_STREAM_TRANSITION_TO_MINI.cfs"; - CompoundFile cf = null; - byte[] b = Helpers.GetBuffer(1024 * 1024 * 2); //2MB buffer byte[] b100 = new byte[100]; @@ -801,7 +797,7 @@ public void Test_RESIZE_STREAM_TRANSITION_TO_MINI() b100[i] = b[i]; } - cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.Default); + CompoundFile cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.Default); cf.RootStorage.AddStream("AStream").SetData(b); cf.Save(FILE_NAME); cf.Close(); @@ -823,10 +819,9 @@ public void Test_RESIZE_STREAM_TRANSITION_TO_MINI() [TestMethod] public void Test_RESIZE_STREAM_TRANSITION_TO_NORMAL() { - CompoundFile cf = null; byte[] b = Helpers.GetBuffer(1024 * 2, 0xAA); //2MB buffer - cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.Default); + CompoundFile cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.Default); cf.RootStorage.AddStream("AStream").SetData(b); cf.Save("$Test_RESIZE_STREAM_TRANSITION_TO_NORMAL.cfs"); cf.Save("$Test_RESIZE_STREAM_TRANSITION_TO_NORMAL2.cfs"); @@ -851,13 +846,12 @@ public void Test_RESIZE_STREAM_TRANSITION_TO_NORMAL() [TestMethod] public void Test_RESIZE_MINISTREAM_NO_TRANSITION_MOD() { - CompoundFile cf = null; int INITIAL_SIZE = 1024 * 2; int SIZE_DELTA = 148; byte[] b = Helpers.GetBuffer(INITIAL_SIZE); - cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.Default); + CompoundFile cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.Default); cf.RootStorage.AddStream("MiniStream").SetData(b); cf.Save("$Test_RESIZE_MINISTREAM.cfs"); cf.Close(); @@ -886,11 +880,9 @@ public void Test_RESIZE_MINISTREAM_NO_TRANSITION_MOD() [TestMethod] public void Test_RESIZE_MINISTREAM_SECTOR_RECYCLE() { - CompoundFile cf = null; - byte[] b = Helpers.GetBuffer(1024 * 2); - cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.Default); + CompoundFile cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.Default); cf.RootStorage.AddStream("MiniStream").SetData(b); cf.Save("$Test_RESIZE_MINISTREAM_RECYCLE.cfs"); cf.Close(); @@ -916,14 +908,11 @@ public void Test_RESIZE_MINISTREAM_SECTOR_RECYCLE() [TestMethod] public void Test_DELETE_STREAM_SECTOR_REUSE() { - CompoundFile cf = null; - CFStream st = null; - byte[] b = Helpers.GetBuffer(1024 * 1024 * 2); //2MB buffer byte[] cmp = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 }; - cf = new CompoundFile(CFSVersion.Ver_4, CFSConfiguration.Default); - st = cf.RootStorage.AddStream("AStream"); + CompoundFile cf = new CompoundFile(CFSVersion.Ver_4, CFSConfiguration.Default); + CFStream st = cf.RootStorage.AddStream("AStream"); st.Append(b); cf.Save("SectorRecycle.cfs"); cf.Close(); diff --git a/sources/Test/OpenMcdf.Test/CompoundFileTest.cs b/sources/Test/OpenMcdf.Test/CompoundFileTest.cs index 4b185a02..f874df74 100644 --- a/sources/Test/OpenMcdf.Test/CompoundFileTest.cs +++ b/sources/Test/OpenMcdf.Test/CompoundFileTest.cs @@ -625,12 +625,10 @@ public void Test_DIFAT_CHECK() public void Test_ADD_LARGE_NUMBER_OF_ITEMS() { int ITEM_NUMBER = 10000; - - CompoundFile f = null; byte[] buffer = Helpers.GetBuffer(10, 0x0A); try { - f = new CompoundFile(); + CompoundFile f = new CompoundFile(); for (int i = 0; i < ITEM_NUMBER; i++) { diff --git a/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs b/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs index 19b2134a..6761fc49 100644 --- a/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs +++ b/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs @@ -91,9 +91,6 @@ public void ItemTest() Assert.AreEqual(expected, actual); Assert.IsNotNull(actual); Assert.IsTrue(actual.Id == expected.Id); - - actual = null; - try { actual = target[count + 100]; From 1c3bf3b29eeaff6f9a38e2dd7e85b72c30da9b7f Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Wed, 11 Sep 2024 11:32:31 +1200 Subject: [PATCH 6/6] Add readonly modifier (IDE0044) --- .../OLEProperties/DictionaryEntry.cs | 2 +- .../OLEProperties/DictionaryProperty.cs | 2 +- .../OLEProperties/OLEPropertiesContainer.cs | 2 +- .../OpenMcdf.Extensions/OLEProperties/OLEProperty.cs | 2 +- .../OLEProperties/PropertyFactory.cs | 4 ++-- .../OLEProperties/TypedPropertyValue.cs | 2 +- sources/OpenMcdf.Extensions/StreamDecorator.cs | 2 +- sources/OpenMcdf/CompoundFile.cs | 6 +++--- sources/OpenMcdf/DirectoryEntry.cs | 2 +- sources/OpenMcdf/Header.cs | 2 +- sources/OpenMcdf/RBTree/RBTree.cs | 4 ++-- sources/OpenMcdf/Sector.cs | 4 ++-- sources/OpenMcdf/SectorCollection.cs | 2 +- sources/OpenMcdf/StreamRW.cs | 4 ++-- sources/OpenMcdf/StreamView.cs | 10 +++++----- .../Structured Storage Explorer/StreamDataProvider.cs | 2 +- sources/Test/OpenMcdf.PerfTest/Program.cs | 4 ++-- 17 files changed, 28 insertions(+), 28 deletions(-) diff --git a/sources/OpenMcdf.Extensions/OLEProperties/DictionaryEntry.cs b/sources/OpenMcdf.Extensions/OLEProperties/DictionaryEntry.cs index d7221904..bce3b45b 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/DictionaryEntry.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/DictionaryEntry.cs @@ -6,7 +6,7 @@ namespace OpenMcdf.Extensions.OLEProperties { public class DictionaryEntry { - int codePage; + readonly int codePage; public DictionaryEntry(int codePage) { diff --git a/sources/OpenMcdf.Extensions/OLEProperties/DictionaryProperty.cs b/sources/OpenMcdf.Extensions/OLEProperties/DictionaryProperty.cs index cba65f83..f32872f3 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/DictionaryProperty.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/DictionaryProperty.cs @@ -7,7 +7,7 @@ namespace OpenMcdf.Extensions.OLEProperties { public class DictionaryProperty : IDictionaryProperty { - private int codePage; + private readonly int codePage; public DictionaryProperty(int codePage) { diff --git a/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs b/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs index c2b390d7..18d48f1a 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs @@ -19,7 +19,7 @@ public class OLEPropertiesContainer public PropertyContext Context { get; private set; } - private List properties = new List(); + private readonly List properties = new List(); internal CFStream cfStream; /* diff --git a/sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs b/sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs index fce0d709..c0071e08 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs @@ -5,7 +5,7 @@ namespace OpenMcdf.Extensions.OLEProperties { public class OLEProperty { - private OLEPropertiesContainer container; + private readonly OLEPropertiesContainer container; internal OLEProperty(OLEPropertiesContainer container) { diff --git a/sources/OpenMcdf.Extensions/OLEProperties/PropertyFactory.cs b/sources/OpenMcdf.Extensions/OLEProperties/PropertyFactory.cs index 24320ff3..afd60cdf 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/PropertyFactory.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/PropertyFactory.cs @@ -381,7 +381,7 @@ public override void WriteScalarValue(System.IO.BinaryWriter bw, DateTime pValue protected class VT_LPSTR_Property : TypedPropertyValue { private byte[] data; - private int codePage; + private readonly int codePage; public VT_LPSTR_Property(VTPropertyType vType, int codePage, bool isVariant) : base(vType, isVariant) { @@ -481,7 +481,7 @@ public VT_Unaligned_LPSTR_Property(VTPropertyType vType, int codePage, bool isVa private class VT_LPWSTR_Property : TypedPropertyValue { private byte[] data; - private int codePage; + private readonly int codePage; public VT_LPWSTR_Property(VTPropertyType vType, int codePage, bool isVariant) : base(vType, isVariant) { diff --git a/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs b/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs index 70682667..cefdeca0 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs @@ -6,7 +6,7 @@ namespace OpenMcdf.Extensions.OLEProperties { internal abstract class TypedPropertyValue : ITypedPropertyValue { - private VTPropertyType _VTType; + private readonly VTPropertyType _VTType; public PropertyType PropertyType => PropertyType.TypedPropertyValue; diff --git a/sources/OpenMcdf.Extensions/StreamDecorator.cs b/sources/OpenMcdf.Extensions/StreamDecorator.cs index eeddeb89..10ed6631 100644 --- a/sources/OpenMcdf.Extensions/StreamDecorator.cs +++ b/sources/OpenMcdf.Extensions/StreamDecorator.cs @@ -8,7 +8,7 @@ namespace OpenMcdf.Extensions /// public class StreamDecorator : Stream { - private CFStream cfStream; + private readonly CFStream cfStream; private long position = 0; /// diff --git a/sources/OpenMcdf/CompoundFile.cs b/sources/OpenMcdf/CompoundFile.cs index 6cb85051..1faf25b9 100644 --- a/sources/OpenMcdf/CompoundFile.cs +++ b/sources/OpenMcdf/CompoundFile.cs @@ -163,7 +163,7 @@ internal int GetSectorSize() public bool ValidationExceptionEnabled { get; private set; } = true; - private CFSUpdateMode updateMode = CFSUpdateMode.ReadOnly; + private readonly CFSUpdateMode updateMode = CFSUpdateMode.ReadOnly; private string fileName = string.Empty; /// @@ -1630,7 +1630,7 @@ private void NullifyChildNodes(IDirectoryEntry de) de.Right = null; } - private List levelSIDs = new List(); + private readonly List levelSIDs = new List(); // Doubling methods allows iterative behavior while avoiding // to insert duplicate items @@ -2536,7 +2536,7 @@ void IDisposable.Dispose() #endregion - private object lockObject = new object(); + private readonly object lockObject = new object(); /// /// When called from user code, release all resources, otherwise, in the case runtime called it, diff --git a/sources/OpenMcdf/DirectoryEntry.cs b/sources/OpenMcdf/DirectoryEntry.cs index cd5f1c1a..2f23f203 100644 --- a/sources/OpenMcdf/DirectoryEntry.cs +++ b/sources/OpenMcdf/DirectoryEntry.cs @@ -33,7 +33,7 @@ internal sealed class DirectoryEntry : IDirectoryEntry { internal const int THIS_IS_GREATER = 1; internal const int OTHER_IS_GREATER = -1; - private IList dirRepository; + private readonly IList dirRepository; public int SID { get; set; } = -1; diff --git a/sources/OpenMcdf/Header.cs b/sources/OpenMcdf/Header.cs index 4d55b67f..309e9320 100644 --- a/sources/OpenMcdf/Header.cs +++ b/sources/OpenMcdf/Header.cs @@ -157,7 +157,7 @@ private void CheckVersion() /// /// Structured Storage signature /// - private byte[] OLE_CFS_SIGNATURE = new byte[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 }; + private readonly byte[] OLE_CFS_SIGNATURE = new byte[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 }; private void CheckSignature() { diff --git a/sources/OpenMcdf/RBTree/RBTree.cs b/sources/OpenMcdf/RBTree/RBTree.cs index 08512cc4..30e22f37 100644 --- a/sources/OpenMcdf/RBTree/RBTree.cs +++ b/sources/OpenMcdf/RBTree/RBTree.cs @@ -513,7 +513,7 @@ private void DoVisitTreeNodes(Action action, IRBNode walker) public class RBTreeEnumerator : IEnumerator { int position = -1; - private Queue heap = new Queue(); + private readonly Queue heap = new Queue(); internal RBTreeEnumerator(RBTree tree) { @@ -545,7 +545,7 @@ public RBTreeEnumerator GetEnumerator() return new RBTreeEnumerator(this); } - private static int INDENT_STEP = 15; + private static readonly int INDENT_STEP = 15; public void Print() { diff --git a/sources/OpenMcdf/Sector.cs b/sources/OpenMcdf/Sector.cs index c5976dfd..5cfeb30d 100644 --- a/sources/OpenMcdf/Sector.cs +++ b/sources/OpenMcdf/Sector.cs @@ -34,7 +34,7 @@ internal sealed class Sector : IDisposable public bool IsStreamed => (stream != null && Size != MINISECTOR_SIZE) ? (this.Id * Size) + Size < stream.Length : false; - private Stream stream; + private readonly Stream stream; public Sector(int size, Stream stream) { @@ -117,7 +117,7 @@ internal void ReleaseData() this.data = null; } - private object lockObject = new object(); + private readonly object lockObject = new object(); #region IDisposable Members diff --git a/sources/OpenMcdf/SectorCollection.cs b/sources/OpenMcdf/SectorCollection.cs index 40f3a391..68491d48 100644 --- a/sources/OpenMcdf/SectorCollection.cs +++ b/sources/OpenMcdf/SectorCollection.cs @@ -30,7 +30,7 @@ internal sealed class SectorCollection : IList public event Ver3SizeLimitReached OnVer3SizeLimitReached; - private List largeArraySlices = new List(); + private readonly List largeArraySlices = new List(); public SectorCollection() { diff --git a/sources/OpenMcdf/StreamRW.cs b/sources/OpenMcdf/StreamRW.cs index 75cf7b08..a96954b3 100644 --- a/sources/OpenMcdf/StreamRW.cs +++ b/sources/OpenMcdf/StreamRW.cs @@ -12,8 +12,8 @@ namespace OpenMcdf { internal sealed class StreamRW { - private byte[] buffer = new byte[8]; - private Stream stream; + private readonly byte[] buffer = new byte[8]; + private readonly Stream stream; public StreamRW(Stream stream) { diff --git a/sources/OpenMcdf/StreamView.cs b/sources/OpenMcdf/StreamView.cs index 6f83c8b0..36491341 100644 --- a/sources/OpenMcdf/StreamView.cs +++ b/sources/OpenMcdf/StreamView.cs @@ -17,12 +17,12 @@ namespace OpenMcdf /// internal sealed class StreamView : Stream { - private int sectorSize; + private readonly int sectorSize; private long position; - private Stream stream; - private bool isFatStream = false; - private List freeSectors = new List(); + private readonly Stream stream; + private readonly bool isFatStream = false; + private readonly List freeSectors = new List(); public IEnumerable FreeSectors => freeSectors; public StreamView(List sectorChain, int sectorSize, Stream stream) @@ -82,7 +82,7 @@ public override void Close() base.Close(); } - private byte[] buf = new byte[4]; + private readonly byte[] buf = new byte[4]; public int ReadInt32() { diff --git a/sources/Structured Storage Explorer/StreamDataProvider.cs b/sources/Structured Storage Explorer/StreamDataProvider.cs index 9183935c..e670cb56 100644 --- a/sources/Structured Storage Explorer/StreamDataProvider.cs +++ b/sources/Structured Storage Explorer/StreamDataProvider.cs @@ -9,7 +9,7 @@ public class StreamDataProvider : IByteProvider /// /// Modifying stream /// - CFStream _modifiedStream; + readonly CFStream _modifiedStream; /// /// Contains information about changes. diff --git a/sources/Test/OpenMcdf.PerfTest/Program.cs b/sources/Test/OpenMcdf.PerfTest/Program.cs index 462b6c94..565ad00c 100644 --- a/sources/Test/OpenMcdf.PerfTest/Program.cs +++ b/sources/Test/OpenMcdf.PerfTest/Program.cs @@ -5,8 +5,8 @@ namespace OpenMcdf.PerfTest { class Program { - static int MAX_STREAM_COUNT = 5000; - static string fileName = "PerfLoad.cfs"; + static readonly int MAX_STREAM_COUNT = 5000; + static readonly string fileName = "PerfLoad.cfs"; static void Main(string[] args) {