diff --git a/sources/OpenMcdf.Extensions/OLEProperties/PropertySetStream.cs b/sources/OpenMcdf.Extensions/OLEProperties/PropertySetStream.cs index cd7264f2..ffc7bfd4 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/PropertySetStream.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/PropertySetStream.cs @@ -211,7 +211,7 @@ public void Write(System.IO.BinaryWriter bw) } } - private IProperty ReadProperty(uint propertyIdentifier, int codePage, BinaryReader br, PropertyFactory factory) + private static IProperty ReadProperty(uint propertyIdentifier, int codePage, BinaryReader br, PropertyFactory factory) { if (propertyIdentifier != 0) { @@ -225,7 +225,7 @@ private IProperty ReadProperty(uint propertyIdentifier, int codePage, BinaryRead } else { - IDictionaryProperty dictionaryProperty = new DictionaryProperty(codePage); + DictionaryProperty dictionaryProperty = new DictionaryProperty(codePage); dictionaryProperty.Read(br); return dictionaryProperty; } diff --git a/sources/OpenMcdf/CFStorage.cs b/sources/OpenMcdf/CFStorage.cs index fe84e7ed..b79c8e51 100644 --- a/sources/OpenMcdf/CFStorage.cs +++ b/sources/OpenMcdf/CFStorage.cs @@ -59,10 +59,7 @@ internal RBTree Children children = LoadChildren(this.DirEntry.SID); //} //else - if (children == null) - { - children = this.CompoundFile.CreateNewTree(); - } + children ??= new RBTree(); } return children; @@ -635,10 +632,7 @@ public void RenameItem(string oldItemName, string newItemName) children = null; children = LoadChildren(this.DirEntry.SID); //Rethread - if (children == null) - { - children = this.CompoundFile.CreateNewTree(); - } + children ??= new RBTree(); } } } diff --git a/sources/OpenMcdf/CompoundFile.cs b/sources/OpenMcdf/CompoundFile.cs index 1faf25b9..1576fcc5 100644 --- a/sources/OpenMcdf/CompoundFile.cs +++ b/sources/OpenMcdf/CompoundFile.cs @@ -615,7 +615,7 @@ private void Load(Stream stream) if (!Configuration.HasFlag(CFSConfiguration.NoValidationException)) { - ValidateHeader(header); + header.ThrowIfInvalid(); } int n_sector = Ceiling((stream.Length - GetSectorSize()) / (double)GetSectorSize()); @@ -644,34 +644,6 @@ private void Load(Stream stream) } } - /// - /// Validate header values specified in [MS-CFB] document - /// - /// The Header sector of file to validate - /// If one of the validation checks fails a CFCorruptedFileException exception will be thrown - private void ValidateHeader(Header header) - { - if (header.MiniSectorShift != 6) - { - throw new CFCorruptedFileException("Mini sector Shift MUST be 0x06"); - } - - if ((header.MajorVersion == 0x0003 && header.SectorShift != 9) || (header.MajorVersion == 0x0004 && header.SectorShift != 0x000c)) - { - throw new CFCorruptedFileException("Sector Shift MUST be 0x0009 for Major Version 3 and 0x000c for Major Version 4"); - } - - if (header.MinSizeStandardStream != 4096) - { - throw new CFCorruptedFileException("Mini Stream Cut off size MUST be 4096 byte"); - } - - if (header.ByteOrder != 0xFFFE) - { - throw new CFCorruptedFileException("Byte order MUST be little endian (0xFFFE)"); - } - } - private void LoadFile(string fileName) { this.fileName = fileName; @@ -1563,16 +1535,6 @@ internal void ResetDirectoryEntry(int sid) // } //} - internal RBTree CreateNewTree() - { - RBTree bst = new RBTree(); - //bst.NodeInserted += OnNodeInsert; - //bst.NodeOperation += OnNodeOperation; - //bst.NodeDeleted += new Action>(OnNodeDeleted); - // bst.ValueAssignedAction += new Action, CFItem>(OnValueAssigned); - return bst; - } - //void OnValueAssigned(RBNode node, CFItem from) //{ // if (from.DirEntry != null && from.DirEntry.LeftSibling != DirectoryEntry.NOSTREAM) @@ -1623,7 +1585,7 @@ private void DoLoadChildren(RBTree bst, IDirectoryEntry de) } } - private void NullifyChildNodes(IDirectoryEntry de) + private static void NullifyChildNodes(IDirectoryEntry de) { de.Parent = null; de.Left = null; @@ -1827,18 +1789,18 @@ public void Save(string fileName) { bool raiseSaveFileEx = false; - if (this.HasSourceStream && this.sourceStream != null && this.sourceStream is FileStream) + if (this.HasSourceStream && this.sourceStream != null && this.sourceStream is FileStream stream) { if (Path.IsPathRooted(fileName)) { - if (((FileStream)this.sourceStream).Name == fileName) + if (stream.Name == fileName) { raiseSaveFileEx = true; } } else { - if (((FileStream)this.sourceStream).Name == (Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + fileName)) + if (stream.Name == (Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + fileName)) { raiseSaveFileEx = true; } @@ -1903,9 +1865,9 @@ public void Save(Stream stream) try { - if (this.HasSourceStream && this.sourceStream != null && this.sourceStream is FileStream && stream is FileStream) + if (this.HasSourceStream && this.sourceStream != null && this.sourceStream is FileStream && stream is FileStream otherStream) { - if (((FileStream)this.sourceStream).Name == ((FileStream)stream).Name) + if (((FileStream)this.sourceStream).Name == otherStream.Name) { throw new CFInvalidOperation("Cannot overwrite current backing file. Compound File should be opened in UpdateMode and Commit() method should be called to persist changes"); } @@ -2600,13 +2562,13 @@ internal IList GetDirectories() internal IDirectoryEntry RootEntry => directoryEntries[0]; - private IList FindDirectoryEntries(string entryName) + private List FindDirectoryEntries(string entryName) { List result = new List(); foreach (IDirectoryEntry d in directoryEntries) { - if (d.GetEntryName() == entryName && d.StgType != StgType.StgInvalid) + if (d.StgType != StgType.StgInvalid && d.GetEntryName() == entryName) result.Add(d); } @@ -2630,9 +2592,9 @@ public IList GetAllNamedEntries(string entryName) foreach (IDirectoryEntry id in r) { - if (id.GetEntryName() == entryName && id.StgType != StgType.StgInvalid) + if (id.StgType != StgType.StgInvalid && id.GetEntryName() == entryName) { - CFItem i = id.StgType == StgType.StgStorage ? new CFStorage(this, id) : (CFItem)new CFStream(this, id); + CFItem i = id.StgType == StgType.StgStorage ? new CFStorage(this, id) : new CFStream(this, id); result.Add(i); } } diff --git a/sources/OpenMcdf/Header.cs b/sources/OpenMcdf/Header.cs index 309e9320..6b4da8a7 100644 --- a/sources/OpenMcdf/Header.cs +++ b/sources/OpenMcdf/Header.cs @@ -167,5 +167,32 @@ private void CheckSignature() throw new CFFileFormatException("Invalid OLE structured storage file"); } } + + /// + /// Validate header values specified in [MS-CFB] document + /// + /// If one of the validation checks fails a CFCorruptedFileException exception will be thrown + internal void ThrowIfInvalid() + { + if (MiniSectorShift != 6) + { + throw new CFCorruptedFileException("Mini sector Shift MUST be 0x06"); + } + + if ((MajorVersion == 0x0003 && SectorShift != 9) || (MajorVersion == 0x0004 && SectorShift != 0x000c)) + { + throw new CFCorruptedFileException("Sector Shift MUST be 0x0009 for Major Version 3 and 0x000c for Major Version 4"); + } + + if (MinSizeStandardStream != 4096) + { + throw new CFCorruptedFileException("Mini Stream Cut off size MUST be 4096 byte"); + } + + if (ByteOrder != 0xFFFE) + { + throw new CFCorruptedFileException("Byte order MUST be little endian (0xFFFE)"); + } + } } } diff --git a/sources/Structured Storage Explorer/MainForm.cs b/sources/Structured Storage Explorer/MainForm.cs index f28a3787..1acf3aa1 100644 --- a/sources/Structured Storage Explorer/MainForm.cs +++ b/sources/Structured Storage Explorer/MainForm.cs @@ -162,7 +162,7 @@ private void LoadFile(string fileName, bool enableCommit) /// /// Current TreeNode /// Current storage associated with node - private void AddNodes(TreeNode node, CFStorage cfs) + private static void AddNodes(TreeNode node, CFStorage cfs) { Action va = delegate (CFItem target) { diff --git a/sources/Test/OpenMcdf.Test/CFSStreamTest.cs b/sources/Test/OpenMcdf.Test/CFSStreamTest.cs index bc50b94f..5ceefcb3 100644 --- a/sources/Test/OpenMcdf.Test/CFSStreamTest.cs +++ b/sources/Test/OpenMcdf.Test/CFSStreamTest.cs @@ -596,7 +596,7 @@ public void Test_DELETE_ZERO_LENGTH_STREAM() //} - private void SingleTransactedChange(int size) + public static void SingleTransactedChange(int size) { string filename = "INCREMENTAL_SIZE_MULTIPLE_WRITE_AND_READ_CFS.cfs"; @@ -624,7 +624,7 @@ private void SingleTransactedChange(int size) cf2.Close(); } - private void SingleWriteReadMatching(int size) + private static void SingleWriteReadMatching(int size) { string filename = "INCREMENTAL_SIZE_MULTIPLE_WRITE_AND_READ_CFS.cfs"; @@ -652,7 +652,7 @@ private void SingleWriteReadMatching(int size) cf2.Close(); } - private void SingleWriteReadMatchingSTREAMED(int size) + private static void SingleWriteReadMatchingSTREAMED(int size) { MemoryStream ms = new MemoryStream(size); diff --git a/sources/Test/OpenMcdf.Test/CFSTorageTest.cs b/sources/Test/OpenMcdf.Test/CFSTorageTest.cs index 8508ae2a..9e7c2f01 100644 --- a/sources/Test/OpenMcdf.Test/CFSTorageTest.cs +++ b/sources/Test/OpenMcdf.Test/CFSTorageTest.cs @@ -81,7 +81,7 @@ public void Test_VISIT_ENTRIES() CompoundFile cf = new CompoundFile(STORAGE_NAME); FileStream output = new FileStream("LogEntries.txt", FileMode.Create); - TextWriter tw = new StreamWriter(output); + StreamWriter tw = new StreamWriter(output); Action va = delegate (CFItem item) { @@ -179,7 +179,7 @@ public void Test_VISIT_ENTRIES_CORRUPTED_FILE_VALIDATION_ON() { output = new FileStream("LogEntriesCorrupted_1.txt", FileMode.Create); - using (TextWriter tw = new StreamWriter(output)) + using (StreamWriter tw = new StreamWriter(output)) { Action va = delegate (CFItem item) { @@ -223,7 +223,7 @@ public void Test_VISIT_ENTRIES_CORRUPTED_FILE_VALIDATION_OFF_BUT_CAN_LOAD() { output = new FileStream("LogEntriesCorrupted_2.txt", FileMode.Create); - using (TextWriter tw = new StreamWriter(output)) + using (StreamWriter tw = new StreamWriter(output)) { Action va = delegate (CFItem item) { @@ -275,7 +275,7 @@ public void Test_VISIT_STORAGE() CompoundFile cf = new CompoundFile(FILENAME); FileStream output = new FileStream("reportVisit.txt", FileMode.Create); - TextWriter sw = new StreamWriter(output); + StreamWriter sw = new StreamWriter(output); Console.SetOut(sw); diff --git a/sources/Test/OpenMcdf.Test/CompoundFileTest.cs b/sources/Test/OpenMcdf.Test/CompoundFileTest.cs index f874df74..c4c062ba 100644 --- a/sources/Test/OpenMcdf.Test/CompoundFileTest.cs +++ b/sources/Test/OpenMcdf.Test/CompoundFileTest.cs @@ -959,7 +959,7 @@ public void Test_PR_GH_18() var st = f.RootStorage.GetStorage("MyStorage").GetStorage("AnotherStorage").GetStream("MyStream"); st.Write(Helpers.GetBuffer(100, 0x02), 100); f.Commit(true); - Assert.IsTrue(st.GetData().Count() == 31220); + Assert.AreEqual(31220, st.GetData().Length); f.Close(); } catch (Exception)