diff --git a/sources/OpenMcdf.Extensions/OLEProperties/Common.cs b/sources/OpenMcdf.Extensions/OLEProperties/Common.cs index ba703681..b61ddcc9 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/Common.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/Common.cs @@ -1,6 +1,4 @@ -using System; - -namespace OpenMcdf.Extensions.OLEProperties +namespace OpenMcdf.Extensions.OLEProperties { public enum Behavior { diff --git a/sources/OpenMcdf.Extensions/OLEProperties/DictionaryEntry.cs b/sources/OpenMcdf.Extensions/OLEProperties/DictionaryEntry.cs index bea8e286..3c691307 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/DictionaryEntry.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/DictionaryEntry.cs @@ -1,5 +1,4 @@ -using System; -using System.IO; +using System.IO; using System.Text; namespace OpenMcdf.Extensions.OLEProperties diff --git a/sources/OpenMcdf.Extensions/OLEProperties/DictionaryProperty.cs b/sources/OpenMcdf.Extensions/OLEProperties/DictionaryProperty.cs index 34021665..44cccfd3 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/DictionaryProperty.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/DictionaryProperty.cs @@ -21,8 +21,8 @@ public DictionaryProperty(int codePage) public object Value { - get { return entries; } - set { entries = (Dictionary)value; } + get => entries; + set => entries = (Dictionary)value; } public void Read(BinaryReader br) diff --git a/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs b/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs index a0558475..b565daf6 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs @@ -8,7 +8,7 @@ namespace OpenMcdf.Extensions.OLEProperties { public class OLEPropertiesContainer { - public Dictionary PropertyNames = null; + public Dictionary PropertyNames; public OLEPropertiesContainer UserDefinedProperties { get; private set; } @@ -19,7 +19,7 @@ public class OLEPropertiesContainer public PropertyContext Context { get; } - private readonly List properties = new List(); + private readonly List properties = new(); internal CFStream cfStream; /* @@ -98,7 +98,7 @@ internal OLEPropertiesContainer(CFStream cfStream) FmtID0 = pStream.FMTID0; PropertyNames = (Dictionary)pStream.PropertySet0.Properties - .Where(p => p.PropertyType == PropertyType.DictionaryProperty).FirstOrDefault()?.Value; + .FirstOrDefault(p => p.PropertyType == PropertyType.DictionaryProperty)?.Value; Context = new PropertyContext() { @@ -149,7 +149,7 @@ internal OLEPropertiesContainer(CFStream cfStream) } var existingPropertyNames = (Dictionary)pStream.PropertySet1.Properties - .Where(p => p.PropertyType == PropertyType.DictionaryProperty).FirstOrDefault()?.Value; + .FirstOrDefault(p => p.PropertyType == PropertyType.DictionaryProperty)?.Value; UserDefinedProperties.PropertyNames = existingPropertyNames ?? new Dictionary(); } @@ -224,7 +224,7 @@ public OLEProperty AddUserDefinedProperty(VTPropertyType vtPropertyType, string public void RemoveProperty(uint propertyIdentifier) { //throw new NotImplementedException("API Unstable - Work in progress - Milestone 2.3.0.0"); - var toRemove = properties.Where(o => o.PropertyIdentifier == propertyIdentifier).FirstOrDefault(); + var toRemove = properties.FirstOrDefault(o => o.PropertyIdentifier == propertyIdentifier); if (toRemove != null) properties.Remove(toRemove); diff --git a/sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs b/sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs index 9bfa0921..85048f31 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs @@ -1,7 +1,4 @@ - -using System; - -namespace OpenMcdf.Extensions.OLEProperties +namespace OpenMcdf.Extensions.OLEProperties { public class OLEProperty { @@ -47,10 +44,7 @@ public object Value return value; } - set - { - this.value = value; - } + set => this.value = value; } public override bool Equals(object obj) diff --git a/sources/OpenMcdf.Extensions/OLEProperties/PropertyFactory.cs b/sources/OpenMcdf.Extensions/OLEProperties/PropertyFactory.cs index e66aa532..a787b8ae 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/PropertyFactory.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/PropertyFactory.cs @@ -54,7 +54,7 @@ protected virtual ITypedPropertyValue CreateLpstrProperty(VTPropertyType vType, #region Property implementations - private class VT_EMPTY_Property : TypedPropertyValue + private sealed class VT_EMPTY_Property : TypedPropertyValue { public VT_EMPTY_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -70,7 +70,7 @@ public override void WriteScalarValue(BinaryWriter bw, object pValue) } } - private class VT_I1_Property : TypedPropertyValue + private sealed class VT_I1_Property : TypedPropertyValue { public VT_I1_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -88,7 +88,7 @@ public override void WriteScalarValue(BinaryWriter bw, sbyte pValue) } } - private class VT_UI1_Property : TypedPropertyValue + private sealed class VT_UI1_Property : TypedPropertyValue { public VT_UI1_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -106,7 +106,7 @@ public override void WriteScalarValue(BinaryWriter bw, byte pValue) } } - private class VT_UI4_Property : TypedPropertyValue + private sealed class VT_UI4_Property : TypedPropertyValue { public VT_UI4_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -124,7 +124,7 @@ public override void WriteScalarValue(BinaryWriter bw, uint pValue) } } - private class VT_UI8_Property : TypedPropertyValue + private sealed class VT_UI8_Property : TypedPropertyValue { public VT_UI8_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -142,7 +142,7 @@ public override void WriteScalarValue(BinaryWriter bw, ulong pValue) } } - private class VT_I2_Property : TypedPropertyValue + private sealed class VT_I2_Property : TypedPropertyValue { public VT_I2_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -160,7 +160,7 @@ public override void WriteScalarValue(BinaryWriter bw, short pValue) } } - private class VT_UI2_Property : TypedPropertyValue + private sealed class VT_UI2_Property : TypedPropertyValue { public VT_UI2_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -178,7 +178,7 @@ public override void WriteScalarValue(BinaryWriter bw, ushort pValue) } } - private class VT_I4_Property : TypedPropertyValue + private sealed class VT_I4_Property : TypedPropertyValue { public VT_I4_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -196,7 +196,7 @@ public override void WriteScalarValue(BinaryWriter bw, int pValue) } } - private class VT_I8_Property : TypedPropertyValue + private sealed class VT_I8_Property : TypedPropertyValue { public VT_I8_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -214,7 +214,7 @@ public override void WriteScalarValue(BinaryWriter bw, long pValue) } } - private class VT_INT_Property : TypedPropertyValue + private sealed class VT_INT_Property : TypedPropertyValue { public VT_INT_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -232,7 +232,7 @@ public override void WriteScalarValue(BinaryWriter bw, int pValue) } } - private class VT_UINT_Property : TypedPropertyValue + private sealed class VT_UINT_Property : TypedPropertyValue { public VT_UINT_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -250,7 +250,7 @@ public override void WriteScalarValue(BinaryWriter bw, uint pValue) } } - private class VT_R4_Property : TypedPropertyValue + private sealed class VT_R4_Property : TypedPropertyValue { public VT_R4_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -268,7 +268,7 @@ public override void WriteScalarValue(BinaryWriter bw, float pValue) } } - private class VT_R8_Property : TypedPropertyValue + private sealed class VT_R8_Property : TypedPropertyValue { public VT_R8_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -286,7 +286,7 @@ public override void WriteScalarValue(BinaryWriter bw, double pValue) } } - private class VT_CY_Property : TypedPropertyValue + private sealed class VT_CY_Property : TypedPropertyValue { public VT_CY_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -307,7 +307,7 @@ public override void WriteScalarValue(BinaryWriter bw, long pValue) } } - private class VT_DATE_Property : TypedPropertyValue + private sealed class VT_DATE_Property : TypedPropertyValue { public VT_DATE_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -402,8 +402,6 @@ public override void WriteScalarValue(BinaryWriter bw, string pValue) //if (addNullTerminator) dataLength += 1; // null terminator \u+0000 - var mod = dataLength % 4; // pad to multiple of 4 bytes - bw.Write(dataLength); // datalength of string + null char (unicode) bw.Write(data); // string @@ -418,7 +416,7 @@ public override void WriteScalarValue(BinaryWriter bw, string pValue) } } - protected class VT_Unaligned_LPSTR_Property : VT_LPSTR_Property + protected sealed class VT_Unaligned_LPSTR_Property : VT_LPSTR_Property { public VT_Unaligned_LPSTR_Property(VTPropertyType vType, int codePage, bool isVariant) : base(vType, codePage, isVariant) { @@ -426,7 +424,7 @@ public VT_Unaligned_LPSTR_Property(VTPropertyType vType, int codePage, bool isVa } } - private class VT_LPWSTR_Property : TypedPropertyValue + private sealed class VT_LPWSTR_Property : TypedPropertyValue { private byte[] data; private readonly int codePage; @@ -476,7 +474,7 @@ public override void WriteScalarValue(BinaryWriter bw, string pValue) } } - private class VT_FILETIME_Property : TypedPropertyValue + private sealed class VT_FILETIME_Property : TypedPropertyValue { public VT_FILETIME_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -495,7 +493,7 @@ public override void WriteScalarValue(BinaryWriter bw, DateTime pValue) } } - private class VT_DECIMAL_Property : TypedPropertyValue + private sealed class VT_DECIMAL_Property : TypedPropertyValue { public VT_DECIMAL_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -538,7 +536,7 @@ public override void WriteScalarValue(BinaryWriter bw, decimal pValue) } } - private class VT_BOOL_Property : TypedPropertyValue + private sealed class VT_BOOL_Property : TypedPropertyValue { public VT_BOOL_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -546,7 +544,7 @@ public VT_BOOL_Property(VTPropertyType vType, bool isVariant) : base(vType, isVa public override bool ReadScalarValue(BinaryReader br) { - propertyValue = br.ReadUInt16() == 0xFFFF ? true : false; + propertyValue = br.ReadUInt16() == 0xFFFF; return (bool)propertyValue; //br.ReadUInt16();//padding } @@ -557,7 +555,7 @@ public override void WriteScalarValue(BinaryWriter bw, bool pValue) } } - private class VT_CF_Property : TypedPropertyValue + private sealed class VT_CF_Property : TypedPropertyValue { public VT_CF_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -585,7 +583,7 @@ public override void WriteScalarValue(BinaryWriter bw, object pValue) } } - private class VT_BLOB_Property : TypedPropertyValue + private sealed class VT_BLOB_Property : TypedPropertyValue { public VT_BLOB_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -612,7 +610,7 @@ public override void WriteScalarValue(BinaryWriter bw, object pValue) } } - private class VT_CLSID_Property : TypedPropertyValue + private sealed class VT_CLSID_Property : TypedPropertyValue { public VT_CLSID_Property(VTPropertyType vType, bool isVariant) : base(vType, isVariant) { @@ -631,7 +629,7 @@ public override void WriteScalarValue(BinaryWriter bw, object pValue) } } - private class VT_VariantVector : TypedPropertyValue + private sealed class VT_VariantVector : TypedPropertyValue { private readonly int codePage; private readonly PropertyFactory factory; @@ -681,7 +679,7 @@ internal sealed class DocumentSummaryInfoPropertyFactory : PropertyFactory protected override ITypedPropertyValue CreateLpstrProperty(VTPropertyType vType, int codePage, uint propertyIdentifier, bool isVariant) { // PIDDSI_HEADINGPAIR and PIDDSI_DOCPARTS use unaligned (unpadded) strings - the others are padded - if (propertyIdentifier == 0x0000000C || propertyIdentifier == 0x0000000D) + if (propertyIdentifier is 0x0000000C or 0x0000000D) { return new VT_Unaligned_LPSTR_Property(vType, codePage, isVariant); } diff --git a/sources/OpenMcdf.Extensions/OLEProperties/PropertySet.cs b/sources/OpenMcdf.Extensions/OLEProperties/PropertySet.cs index 15bc2b5d..98aa9893 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/PropertySet.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/PropertySet.cs @@ -25,7 +25,7 @@ public void LoadContext(int propertySetOffset, BinaryReader br) var currPos = br.BaseStream.Position; PropertyContext = new PropertyContext(); - var codePageOffset = (int)(propertySetOffset + PropertyIdentifierAndOffsets.Where(pio => pio.PropertyIdentifier == 1).First().Offset); + var codePageOffset = (int)(propertySetOffset + PropertyIdentifierAndOffsets.First(pio => pio.PropertyIdentifier == 1).Offset); br.BaseStream.Seek(codePageOffset, SeekOrigin.Begin); VTPropertyType vType = (VTPropertyType)br.ReadUInt16(); diff --git a/sources/OpenMcdf.Extensions/OLEProperties/PropertySetStream.cs b/sources/OpenMcdf.Extensions/OLEProperties/PropertySetStream.cs index 0d3e316d..af0dc066 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/PropertySetStream.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/PropertySetStream.cs @@ -102,7 +102,7 @@ public void Read(BinaryReader br) } } - private class OffsetContainer + private sealed class OffsetContainer { public int OffsetPS { get; set; } diff --git a/sources/OpenMcdf.Extensions/OLEProperties/ProperyIdentifiers.cs b/sources/OpenMcdf.Extensions/OLEProperties/ProperyIdentifiers.cs index 01db834f..658c5f7c 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/ProperyIdentifiers.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/ProperyIdentifiers.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; namespace OpenMcdf.Extensions.OLEProperties { @@ -16,7 +15,7 @@ public enum ContainerType public static class CommonIdentifiers { - public static Dictionary PropertyIdentifiersSummaryInfo = new Dictionary() + public static Dictionary PropertyIdentifiersSummaryInfo = new() { {0x00000001, "CodePageString" }, {0x00000002, "PIDSI_TITLE" }, @@ -38,7 +37,7 @@ public static class CommonIdentifiers {0x00000013, "PIDSI_DOC_SECURITY" } }; - public static Dictionary PropertyIdentifiersDocumentSummaryInfo = new Dictionary() + public static Dictionary PropertyIdentifiersDocumentSummaryInfo = new() { {0x00000001, "CodePageString" }, {0x00000002, "PIDDSI_CATEGORY" }, diff --git a/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs b/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs index f043b93c..febcdfd9 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/TypedPropertyValue.cs @@ -12,7 +12,7 @@ internal abstract class TypedPropertyValue : ITypedPropertyValue public VTPropertyType VTType => _VTType; - protected object propertyValue = null; + protected object propertyValue; public TypedPropertyValue(VTPropertyType vtType, bool isVariant = false) { @@ -23,7 +23,7 @@ public TypedPropertyValue(VTPropertyType vtType, bool isVariant = false) public PropertyDimensions PropertyDimensions { get; } = PropertyDimensions.IsScalar; - public bool IsVariant { get; } = false; + public bool IsVariant { get; } protected virtual bool NeedsPadding { get; set; } = true; @@ -39,15 +39,9 @@ private static PropertyDimensions CheckPropertyDimensions(VTPropertyType vtType) public virtual object Value { - get - { - return propertyValue; - } + get => propertyValue; - set - { - propertyValue = value; - } + set => propertyValue = value; } public abstract T ReadScalarValue(BinaryReader br); diff --git a/sources/OpenMcdf.Extensions/StreamDecorator.cs b/sources/OpenMcdf.Extensions/StreamDecorator.cs index 01b6fe3f..646a4931 100644 --- a/sources/OpenMcdf.Extensions/StreamDecorator.cs +++ b/sources/OpenMcdf.Extensions/StreamDecorator.cs @@ -9,7 +9,7 @@ namespace OpenMcdf.Extensions public class StreamDecorator : Stream { private readonly CFStream cfStream; - private long position = 0; + private long position; /// /// Create a new for the specified . diff --git a/sources/OpenMcdf/CFItem.cs b/sources/OpenMcdf/CFItem.cs index 5fd4fe58..85203a93 100644 --- a/sources/OpenMcdf/CFItem.cs +++ b/sources/OpenMcdf/CFItem.cs @@ -163,14 +163,11 @@ public string Name /// public DateTime CreationDate { - get - { - return DateTime.FromFileTimeUtc(BitConverter.ToInt64(DirEntry.CreationDate, 0)); - } + get => DateTime.FromFileTimeUtc(BitConverter.ToInt64(DirEntry.CreationDate, 0)); set { - if (DirEntry.StgType != StgType.StgStream && DirEntry.StgType != StgType.StgRoot) + if (DirEntry.StgType is not StgType.StgStream and not StgType.StgRoot) DirEntry.CreationDate = BitConverter.GetBytes(value.ToFileTimeUtc()); else throw new CFException("Creation Date can only be set on storage entries"); @@ -182,14 +179,11 @@ public DateTime CreationDate /// public DateTime ModifyDate { - get - { - return DateTime.FromFileTimeUtc(BitConverter.ToInt64(DirEntry.ModifyDate, 0)); - } + get => DateTime.FromFileTimeUtc(BitConverter.ToInt64(DirEntry.ModifyDate, 0)); set { - if (DirEntry.StgType != StgType.StgStream && DirEntry.StgType != StgType.StgRoot) + if (DirEntry.StgType is not StgType.StgStream and not StgType.StgRoot) DirEntry.ModifyDate = BitConverter.GetBytes(value.ToFileTimeUtc()); else throw new CFException("Modify Date can only be set on storage entries"); @@ -201,10 +195,7 @@ public DateTime ModifyDate /// public Guid CLSID { - get - { - return DirEntry.StorageCLSID; - } + get => DirEntry.StorageCLSID; set { if (DirEntry.StgType != StgType.StgStream) diff --git a/sources/OpenMcdf/CFStream.cs b/sources/OpenMcdf/CFStream.cs index c8ed35bc..abf00f2f 100644 --- a/sources/OpenMcdf/CFStream.cs +++ b/sources/OpenMcdf/CFStream.cs @@ -6,7 +6,6 @@ * * The Initial Developer of the Original Code is Federico Blaseotto.*/ -using System; using System.IO; namespace OpenMcdf diff --git a/sources/OpenMcdf/CompoundFile.cs b/sources/OpenMcdf/CompoundFile.cs index e1fee2eb..9d4e2de1 100644 --- a/sources/OpenMcdf/CompoundFile.cs +++ b/sources/OpenMcdf/CompoundFile.cs @@ -11,7 +11,6 @@ using RedBlackTree; using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; namespace OpenMcdf @@ -152,17 +151,16 @@ public class CompoundFile : IDisposable /// /// Flag for sector recycling. /// - private bool sectorRecycle = false; + private bool sectorRecycle; /// /// Flag for unallocated sector zeroing out. /// - private bool eraseFreeSectors = false; + private bool eraseFreeSectors; public bool ValidationExceptionEnabled { get; private set; } = true; private readonly CFSUpdateMode updateMode = CFSUpdateMode.ReadOnly; - private string fileName = string.Empty; /// /// Initial capacity of the flushing queue used @@ -176,7 +174,7 @@ public class CompoundFile : IDisposable /// private const int FLUSHING_BUFFER_MAX_SIZE = 1024 * 1024 * 16; - private SectorCollection sectors = new SectorCollection(); + private SectorCollection sectors = new(); /// /// CompoundFile header @@ -186,7 +184,7 @@ public class CompoundFile : IDisposable /// /// Compound underlying stream. Null when new CF has been created. /// - internal Stream sourceStream = null; + internal Stream sourceStream; /// /// Create a blank, version 3 compound file. @@ -644,8 +642,6 @@ private void Load(Stream stream) private void LoadFile(string fileName) { - this.fileName = fileName; - FileAccess access = updateMode == CFSUpdateMode.ReadOnly ? FileAccess.Read : FileAccess.ReadWrite; FileShare share = updateMode == CFSUpdateMode.ReadOnly ? FileShare.ReadWrite : FileShare.Read; FileStream fs = new(fileName, FileMode.Open, access, share); @@ -952,9 +948,9 @@ private void AllocateSectorChain(List sectorChain) AllocateFATSectorChain(sectorChain); } - internal bool _transactionLockAdded = false; + internal bool _transactionLockAdded; internal int _lockSectorId = -1; - internal bool _transactionLockAllocated = false; + internal bool _transactionLockAllocated; /// /// Check for transaction lock sector addition and mark it in the FAT. @@ -1084,7 +1080,7 @@ private void AllocateDIFATSectorChain(List FATsectorChain) s.Type = SectorType.DIFAT; s.Id = sectors.Count - 1; difatSectors.Add(s); - + } } @@ -1212,7 +1208,7 @@ List result // a specification point of view: // only ENDOFCHAIN should break DIFAT chain but // a lot of existing compound files use FREESECT as DIFAT chain termination - if (nextSecID == Sector.FREESECT || nextSecID == Sector.ENDOFCHAIN) break; + if (nextSecID is Sector.FREESECT or Sector.ENDOFCHAIN) break; validationCount--; @@ -1427,10 +1423,11 @@ List result if (nextSecID == Sector.ENDOFCHAIN) break; - Sector ms = new Sector(Sector.MINISECTOR_SIZE, sourceStream); - - ms.Id = nextSecID; - ms.Type = SectorType.Mini; + Sector ms = new Sector(Sector.MINISECTOR_SIZE, sourceStream) + { + Id = nextSecID, + Type = SectorType.Mini + }; miniStreamView.Seek(nextSecID * Sector.MINISECTOR_SIZE, SeekOrigin.Begin); miniStreamView.ReadExactly(ms.GetData(), 0, Sector.MINISECTOR_SIZE); @@ -1783,20 +1780,15 @@ public void Save(Stream stream) { Sector s = sectors[i]; - if (s == null) + // Load source (unmodified) sectors + // Here we have to ignore "Dirty flag" of + // sectors because we are NOT modifying the source + // in a differential way but ALL sectors need to be + // persisted on the destination stream + s ??= new Sector(sSize, sourceStream) { - // Load source (unmodified) sectors - // Here we have to ignore "Dirty flag" of - // sectors because we are NOT modifying the source - // in a differential way but ALL sectors need to be - // persisted on the destination stream - s = new Sector(sSize, sourceStream) - { - Id = i - }; - - //sectors[i] = s; - } + Id = i + }; stream.Write(s.GetData(), 0, sSize); @@ -2320,7 +2312,7 @@ public void Dispose() #endregion - private readonly object lockObject = new object(); + private readonly object lockObject = new(); /// /// When called from user code, release all resources, otherwise, in the case runtime called it, @@ -2349,7 +2341,6 @@ protected virtual void Dispose(bool disposing) header = null; directoryEntries.Clear(); directoryEntries = null; - fileName = null; //this.lockObject = null; #if !FLAT_WRITE this.buffer = null; @@ -2370,7 +2361,7 @@ protected virtual void Dispose(bool disposing) internal bool IsClosed { get; private set; } private List directoryEntries - = new List(); + = new(); internal IList Directories => directoryEntries; @@ -2554,23 +2545,22 @@ public static void ShrinkCompoundFile(string fileName) /// Current cloned destination storage private static void DoCompression(CFStorage currSrcStorage, CFStorage currDstStorage) { - Action va = - delegate (CFItem item) + void va(CFItem item) + { + if (item.IsStream) { - if (item.IsStream) - { - CFStream itemAsStream = item as CFStream; - CFStream st = currDstStorage.AddStream(itemAsStream.Name); - st.SetData(itemAsStream.GetData()); - } - else if (item.IsStorage) - { - CFStorage itemAsStorage = item as CFStorage; - CFStorage strg = currDstStorage.AddStorage(itemAsStorage.Name); - strg.CLSID = itemAsStorage.CLSID; - DoCompression(itemAsStorage, strg); // recursion, one level deeper - } - }; + CFStream itemAsStream = item as CFStream; + CFStream st = currDstStorage.AddStream(itemAsStream.Name); + st.SetData(itemAsStream.GetData()); + } + else if (item.IsStorage) + { + CFStorage itemAsStorage = item as CFStorage; + CFStorage strg = currDstStorage.AddStorage(itemAsStorage.Name); + strg.CLSID = itemAsStorage.CLSID; + DoCompression(itemAsStorage, strg); // recursion, one level deeper + } + } currSrcStorage.VisitEntries(va, false); } diff --git a/sources/OpenMcdf/DirectoryEntry.cs b/sources/OpenMcdf/DirectoryEntry.cs index 5a4e1b32..3cea8928 100644 --- a/sources/OpenMcdf/DirectoryEntry.cs +++ b/sources/OpenMcdf/DirectoryEntry.cs @@ -102,16 +102,11 @@ public void SetEntryName(string entryName) } private ushort nameLength; + public ushort NameLength { - get - { - return nameLength; - } - set - { - throw new NotImplementedException(); - } + get => nameLength; + set => throw new NotImplementedException(); } public StgType StgType { get; set; } = StgType.StgInvalid; @@ -129,14 +124,8 @@ private Guid storageCLSID public Guid StorageCLSID { - get - { - return storageCLSID; - } - set - { - storageCLSID = value; - } + get => storageCLSID; + set => storageCLSID = value; } public int StateBits { get; set; } @@ -333,28 +322,16 @@ public RedBlackTree.IRBNode Right public RedBlackTree.Color Color { - get - { - return (RedBlackTree.Color)StgColor; - } - set - { - StgColor = (StgColor)value; - } + get => (RedBlackTree.Color)StgColor; + set => StgColor = (StgColor)value; } - private IDirectoryEntry parent = null; + private IDirectoryEntry parent; public RedBlackTree.IRBNode Parent { - get - { - return parent; - } - set - { - parent = value as IDirectoryEntry; - } + get => parent; + set => parent = value as IDirectoryEntry; } public RedBlackTree.IRBNode Grandparent() diff --git a/sources/OpenMcdf/Header.cs b/sources/OpenMcdf/Header.cs index a20626a2..a67dd655 100644 --- a/sources/OpenMcdf/Header.cs +++ b/sources/OpenMcdf/Header.cs @@ -147,7 +147,7 @@ public void Read(Stream stream) private void CheckVersion() { - if (MajorVersion != 3 && MajorVersion != 4) + if (MajorVersion is not 3 and not 4) throw new CFFileFormatException("Unsupported Binary File Format version: OpenMcdf only supports Compound Files with major version equal to 3 or 4 "); } diff --git a/sources/OpenMcdf/Sector.cs b/sources/OpenMcdf/Sector.cs index 324e4270..838fa79d 100644 --- a/sources/OpenMcdf/Sector.cs +++ b/sources/OpenMcdf/Sector.cs @@ -30,9 +30,9 @@ internal sealed class Sector public const int FATSECT = unchecked((int)0xFFFFFFFD); public const int DIFSECT = unchecked((int)0xFFFFFFFC); - public bool DirtyFlag { get; set; } = false; + public bool DirtyFlag { get; set; } - public bool IsStreamed => (stream != null && Size != MINISECTOR_SIZE) ? (Id * Size) + Size < stream.Length : false; + public bool IsStreamed => (stream != null && Size != MINISECTOR_SIZE) && (Id * Size) + Size < stream.Length; private readonly Stream stream; @@ -53,7 +53,7 @@ public Sector(int size) public int Id { get; set; } = -1; - public int Size { get; private set; } = 0; + public int Size { get; private set; } private byte[] data; diff --git a/sources/OpenMcdf/SectorCollection.cs b/sources/OpenMcdf/SectorCollection.cs index 82a4e5a9..a1f3e2f9 100644 --- a/sources/OpenMcdf/SectorCollection.cs +++ b/sources/OpenMcdf/SectorCollection.cs @@ -31,7 +31,7 @@ internal sealed class SectorCollection : IList public event Ver3SizeLimitReached OnVer3SizeLimitReached; private readonly List> largeArraySlices = new(); - private bool sizeLimitReached = false; + private bool sizeLimitReached; public SectorCollection() { @@ -125,7 +125,7 @@ public void CopyTo(Sector[] array, int arrayIndex) throw new NotImplementedException(); } - public int Count { get; private set; } = 0; + public int Count { get; private set; } public bool IsReadOnly => false; diff --git a/sources/OpenMcdf/StreamView.cs b/sources/OpenMcdf/StreamView.cs index ca31b912..8a303c8f 100644 --- a/sources/OpenMcdf/StreamView.cs +++ b/sources/OpenMcdf/StreamView.cs @@ -21,8 +21,8 @@ internal sealed class StreamView : Stream private long position; private readonly Stream stream; - private readonly bool isFatStream = false; - private readonly List freeSectors = new List(); + private readonly bool isFatStream; + private readonly List freeSectors = new(); public IEnumerable FreeSectors => freeSectors; public StreamView(List sectorChain, int sectorSize, Stream stream) @@ -85,7 +85,7 @@ public override int Read(byte[] buffer, int offset, int count) count = Math.Min((int)intMax, count); // Ensure read request greater then stream length, when position is not 0, return only the limited and correct number of bytes - count = (int)Math.Min(length - position, count); + count = (int)Math.Min(length - position, count); if (BaseSectorChain != null && BaseSectorChain.Count > 0) { @@ -325,4 +325,4 @@ public override void Write(byte[] buffer, int offset, int count) } } } -} \ No newline at end of file +} diff --git a/sources/Structured Storage Explorer/MainForm.cs b/sources/Structured Storage Explorer/MainForm.cs index 83776c17..718d04cf 100644 --- a/sources/Structured Storage Explorer/MainForm.cs +++ b/sources/Structured Storage Explorer/MainForm.cs @@ -11,7 +11,6 @@ using System.Drawing; using System.Globalization; using System.IO; -using System.Linq; using System.Windows.Forms; // Author Federico Blaseotto @@ -86,7 +85,7 @@ private void CloseCurrentFile() #endif } - private bool canUpdate = false; + private bool canUpdate; private void CreateNewFile() { @@ -341,7 +340,7 @@ private void openFileMenuItem_Click(object sender, EventArgs e) { OpenFile(); } - catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException || ex is CFException) + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or CFException) { MessageBox.Show($"Cannot open file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } @@ -391,7 +390,7 @@ private void treeView1_MouseUp(object sender, MouseEventArgs e) dgvUserDefinedProperties.DataSource = null; dgvOLEProps.DataSource = null; - if (stream.Name == "\u0005SummaryInformation" || stream.Name == "\u0005DocumentSummaryInformation") + if (stream.Name is "\u0005SummaryInformation" or "\u0005DocumentSummaryInformation") { OLEPropertiesContainer c = stream.AsOLEPropertiesContainer(); @@ -402,7 +401,7 @@ private void treeView1_MouseUp(object sender, MouseEventArgs e) foreach (OLEProperty p in c.Properties) { - if (p.Value is not byte[] && p.Value is IList list) + if (p.Value is not byte[] and IList list) { for (int h = 0; h < list.Count; h++) { @@ -431,7 +430,7 @@ private void treeView1_MouseUp(object sender, MouseEventArgs e) foreach (OLEProperty p in c.UserDefinedProperties.Properties) { - if (p.Value is not byte[] && p.Value is IList list) + if (p.Value is not byte[] and IList list) { for (int h = 0; h < list.Count; h++) { diff --git a/sources/Structured Storage Explorer/Utils.cs b/sources/Structured Storage Explorer/Utils.cs index f61f0371..0a33f1fd 100644 --- a/sources/Structured Storage Explorer/Utils.cs +++ b/sources/Structured Storage Explorer/Utils.cs @@ -5,7 +5,7 @@ namespace StructuredStorageExplorer { - class Utils + static class Utils { public static DialogResult InputBox(string title, string promptText, ref string value) { @@ -30,7 +30,7 @@ public static DialogResult InputBox(string title, string promptText, ref string buttonCancel.SetBounds(309, 72, 75, 23); label.AutoSize = true; - textBox.Anchor = textBox.Anchor | AnchorStyles.Right; + textBox.Anchor |= AnchorStyles.Right; buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; diff --git a/sources/Test/OpenMcdf.Benchmark/InMemory.cs b/sources/Test/OpenMcdf.Benchmark/InMemory.cs index 73ca8fdd..c5de3a11 100644 --- a/sources/Test/OpenMcdf.Benchmark/InMemory.cs +++ b/sources/Test/OpenMcdf.Benchmark/InMemory.cs @@ -1,6 +1,5 @@ using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Order; -using BenchmarkDotNet.Jobs; using System; using System.IO; @@ -22,7 +21,7 @@ public class InMemory : IDisposable private byte[] _readBuffer; - private Stream _stream; + private MemoryStream _stream; [Params(Kb / 2, Kb, 4 * Kb, 128 * Kb, 256 * Kb, 512 * Kb, Kb * Kb)] public int BufferSize { get; set; } diff --git a/sources/Test/OpenMcdf.Benchmark/Program.cs b/sources/Test/OpenMcdf.Benchmark/Program.cs index a6459efa..34aa8734 100644 --- a/sources/Test/OpenMcdf.Benchmark/Program.cs +++ b/sources/Test/OpenMcdf.Benchmark/Program.cs @@ -2,11 +2,11 @@ namespace OpenMcdf.Benchmark { - internal class Program + internal static class Program { private static void Main(string[] args) { BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); } } -} \ No newline at end of file +} diff --git a/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs b/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs index a63931e7..d09a5182 100644 --- a/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs +++ b/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs @@ -1,7 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; using System.IO; -using System.Linq; namespace OpenMcdf.Extensions.Test { diff --git a/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs b/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs index 98f73f97..5b72e304 100644 --- a/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs +++ b/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs @@ -545,7 +545,7 @@ public void Test_FIX_CRASH() bufferedStream.CopyTo(memoryStream); Assert.AreEqual(memoryStream.Length, cfStream.Size); - + } } } diff --git a/sources/Test/OpenMcdf.MemTest/Program.cs b/sources/Test/OpenMcdf.MemTest/Program.cs index be7d44b5..0d8ff744 100644 --- a/sources/Test/OpenMcdf.MemTest/Program.cs +++ b/sources/Test/OpenMcdf.MemTest/Program.cs @@ -5,7 +5,7 @@ namespace OpenMcdf.MemTest { - class Program + static class Program { static void Main(string[] args) { @@ -130,7 +130,7 @@ private static void StressMemory() byte[] cmp = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 }; CompoundFile cf = new CompoundFile(CFSVersion.Ver_4, CFSConfiguration.Default); - CFStream st = cf.RootStorage.AddStream("MySuperLargeStream"); + _ = cf.RootStorage.AddStream("MySuperLargeStream"); cf.SaveAs("LARGE.cfs"); cf.Close(); @@ -207,7 +207,7 @@ private static void DummyFile() private static void AddNodes(string depth, CFStorage cfs) { - Action va = delegate (CFItem target) + void va(CFItem target) { string temp = target.Name + (target is CFStorage ? "" : " (" + target.Size + " bytes )"); @@ -222,7 +222,7 @@ private static void AddNodes(string depth, CFStorage cfs) //Recursion into the storage AddNodes(newDepth, (CFStorage)target); } - }; + } //Visit NON-recursively (first level only) cfs.VisitEntries(va, false); diff --git a/sources/Test/OpenMcdf.PerfTest/Program.cs b/sources/Test/OpenMcdf.PerfTest/Program.cs index 136659b5..f002f3a7 100644 --- a/sources/Test/OpenMcdf.PerfTest/Program.cs +++ b/sources/Test/OpenMcdf.PerfTest/Program.cs @@ -4,7 +4,7 @@ namespace OpenMcdf.PerfTest { - class Program + static class Program { const int MAX_STREAM_COUNT = 5000; const string fileName = "PerfLoad.cfs"; @@ -19,7 +19,7 @@ static void Main(string[] args) CompoundFile cf = new CompoundFile(fileName); var stopwatch = Stopwatch.StartNew(); - CFStream s = cf.RootStorage.GetStream("Test1"); + _ = cf.RootStorage.GetStream("Test1"); Console.WriteLine($"Elapsed: {stopwatch.Elapsed}"); Console.Read(); } diff --git a/sources/Test/OpenMcdf.Test/CFStorageTest.cs b/sources/Test/OpenMcdf.Test/CFStorageTest.cs index e382cdc8..861208e0 100644 --- a/sources/Test/OpenMcdf.Test/CFStorageTest.cs +++ b/sources/Test/OpenMcdf.Test/CFStorageTest.cs @@ -82,10 +82,10 @@ public void Test_VISIT_ENTRIES() using FileStream output = new("LogEntries.txt", FileMode.Create); using StreamWriter tw = new(output); - Action va = delegate (CFItem item) + void va(CFItem item) { tw.WriteLine(item.Name); - }; + } cf.RootStorage.VisitEntries(va, true); } @@ -182,10 +182,10 @@ public void Test_VISIT_STORAGE() Console.SetOut(sw); - Action va = delegate (CFItem target) + void va(CFItem target) { sw.WriteLine(target.Name); - }; + } cf.RootStorage.VisitEntries(va, true); } @@ -212,7 +212,7 @@ public void Test_DELETE_MINISTREAM_STREAM() using CompoundFile cf = new(FILENAME); CFStorage found = null; - Action action = delegate (CFItem item) { if (item.Name == "AnotherStorage") found = item as CFStorage; }; + void action(CFItem item) { if (item.Name == "AnotherStorage") found = item as CFStorage; } cf.RootStorage.VisitEntries(action, true); Assert.IsNotNull(found); @@ -229,11 +229,11 @@ public void Test_DELETE_STREAM() using CompoundFile cf = new(FILENAME); CFStorage found = null; - Action action = delegate (CFItem item) + void action(CFItem item) { if (item.Name == "AnotherStorage") found = item as CFStorage; - }; + } cf.RootStorage.VisitEntries(action, true); diff --git a/sources/Test/OpenMcdf.Test/CFStreamTest.cs b/sources/Test/OpenMcdf.Test/CFStreamTest.cs index 0f4bfa4d..4f76a617 100644 --- a/sources/Test/OpenMcdf.Test/CFStreamTest.cs +++ b/sources/Test/OpenMcdf.Test/CFStreamTest.cs @@ -507,7 +507,7 @@ static IEnumerable FuzzyBufferLengths foreach (CFSVersion version in Enum.GetValues(typeof(CFSVersion))) { - for (int i = r.Next(1, 100); i < 1024 * 1024 * 70; i = i << 1) + for (int i = r.Next(1, 100); i < 1024 * 1024 * 70; i <<= 1) { yield return new object[] { version, i + r.Next(0, 3) }; } diff --git a/sources/Test/OpenMcdf.Test/CompoundFileTest.cs b/sources/Test/OpenMcdf.Test/CompoundFileTest.cs index 00e28224..dd8963eb 100644 --- a/sources/Test/OpenMcdf.Test/CompoundFileTest.cs +++ b/sources/Test/OpenMcdf.Test/CompoundFileTest.cs @@ -363,7 +363,7 @@ public void Test_FUNCTIONAL_BEHAVIOUR() using (CompoundFile cfTest = new("6_Streams_Shrinked.cfs")) { - Action va = delegate (CFItem item) + static void va(CFItem item) { if (item.IsStream) { @@ -375,7 +375,7 @@ public void Test_FUNCTIONAL_BEHAVIOUR() Assert.IsTrue(d.Length > 0); Assert.AreEqual(ia.Size, d.Length); } - }; + } cfTest.RootStorage.VisitEntries(va, true); } @@ -776,7 +776,7 @@ public void Test_FIX_BUG_GH_14() byte[] testBuffer = new byte[100]; byte t = 0x0A; - for (int streamId = 0; streamId g == t)); @@ -1011,7 +1011,7 @@ public void Test_FIX_BUG_90_CompoundFile_Delete_Storages() // check if all storages can be found in to copied CompundFile foreach (var storageName in storageNames) { - Assert.IsTrue(cf.RootStorage.TryGetStorage(storageName, out var storage)); //<- we will see some missing storages here + Assert.IsTrue(cf.RootStorage.TryGetStorage(storageName, out _)); //<- we will see some missing storages here } } @@ -1089,6 +1089,6 @@ public void Test_FIX_BUG_96_CompoundFile_SaveOverwrite() File.Delete(filename2); } - + } } diff --git a/sources/Test/OpenMcdf.Test/RBTreeTest.cs b/sources/Test/OpenMcdf.Test/RBTreeTest.cs index 77f48476..c88bc143 100644 --- a/sources/Test/OpenMcdf.Test/RBTreeTest.cs +++ b/sources/Test/OpenMcdf.Test/RBTreeTest.cs @@ -1,6 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using RedBlackTree; -using System; using System.Collections.Generic; namespace OpenMcdf.Test @@ -43,12 +42,12 @@ public RBTreeTest() // #endregion - internal IList GetDirectoryRepository(int count) + internal static IList GetDirectoryRepository(int count) { List repo = new List(); for (int i = 0; i < count; i++) { - IDirectoryEntry de = DirectoryEntry.New(i.ToString(), StgType.StgInvalid, repo); + _ = DirectoryEntry.New(i.ToString(), StgType.StgInvalid, repo); } return repo; @@ -85,9 +84,9 @@ public void Test_RBTREE_DELETE() rbTree.Insert(item); } - rbTree.Delete(DirectoryEntry.Mock("5", StgType.StgInvalid), out IRBNode n); - rbTree.Delete(DirectoryEntry.Mock("24", StgType.StgInvalid), out n); - rbTree.Delete(DirectoryEntry.Mock("7", StgType.StgInvalid), out n); + rbTree.Delete(DirectoryEntry.Mock("5", StgType.StgInvalid), out _); + rbTree.Delete(DirectoryEntry.Mock("24", StgType.StgInvalid), out _); + rbTree.Delete(DirectoryEntry.Mock("7", StgType.StgInvalid), out _); // CFItem c; // bool s = rbTree.TryLookup(new CFMock("7", StgType.StgStream), out c); @@ -120,7 +119,7 @@ private static Color NodeColor(IRBNode n) private static void VerifyProperty1(IRBNode n) { - Assert.IsTrue(NodeColor(n) == Color.RED || NodeColor(n) == Color.BLACK); + Assert.IsTrue(NodeColor(n) is Color.RED or Color.BLACK); if (n == null) return; VerifyProperty1(n.Left); diff --git a/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs b/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs index 05d9bba9..ab5f32a2 100644 --- a/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs +++ b/sources/Test/OpenMcdf.Test/SectorCollectionTest.cs @@ -1,7 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; using System.Collections.Generic; -using System.Linq; namespace OpenMcdf.Test {