Skip to content

Commit

Permalink
Merge pull request #157 from Visionaid-International-Ltd/perf
Browse files Browse the repository at this point in the history
Initial performance improvements
  • Loading branch information
ironfede authored Sep 23, 2024
2 parents b95add4 + 7c24a9e commit 4008143
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand Down
10 changes: 2 additions & 8 deletions sources/OpenMcdf/CFStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
60 changes: 11 additions & 49 deletions sources/OpenMcdf/CompoundFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -644,34 +644,6 @@ private void Load(Stream stream)
}
}

/// <summary>
/// Validate header values specified in [MS-CFB] document
/// </summary>
/// <param name="header">The Header sector of file to validate</param>
/// <exception cref="CFCorruptedFileException">If one of the validation checks fails a <see cref="T:OpenMcdf.CFCorruptedFileException">CFCorruptedFileException</see> exception will be thrown</exception>
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;
Expand Down Expand Up @@ -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<RBNode<CFItem>>(OnNodeDeleted);
// bst.ValueAssignedAction += new Action<RBNode<CFItem>, CFItem>(OnValueAssigned);
return bst;
}

//void OnValueAssigned(RBNode<CFItem> node, CFItem from)
//{
// if (from.DirEntry != null && from.DirEntry.LeftSibling != DirectoryEntry.NOSTREAM)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -2600,13 +2562,13 @@ internal IList<IDirectoryEntry> GetDirectories()

internal IDirectoryEntry RootEntry => directoryEntries[0];

private IList<IDirectoryEntry> FindDirectoryEntries(string entryName)
private List<IDirectoryEntry> FindDirectoryEntries(string entryName)
{
List<IDirectoryEntry> result = new List<IDirectoryEntry>();

foreach (IDirectoryEntry d in directoryEntries)
{
if (d.GetEntryName() == entryName && d.StgType != StgType.StgInvalid)
if (d.StgType != StgType.StgInvalid && d.GetEntryName() == entryName)
result.Add(d);
}

Expand All @@ -2630,9 +2592,9 @@ public IList<CFItem> 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);
}
}
Expand Down
27 changes: 27 additions & 0 deletions sources/OpenMcdf/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,32 @@ private void CheckSignature()
throw new CFFileFormatException("Invalid OLE structured storage file");
}
}

/// <summary>
/// Validate header values specified in [MS-CFB] document
/// </summary>
/// <exception cref="CFCorruptedFileException">If one of the validation checks fails a <see cref="T:OpenMcdf.CFCorruptedFileException">CFCorruptedFileException</see> exception will be thrown</exception>
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)");
}
}
}
}
2 changes: 1 addition & 1 deletion sources/Structured Storage Explorer/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void LoadFile(string fileName, bool enableCommit)
/// </summary>
/// <param name="node">Current TreeNode</param>
/// <param name="cfs">Current storage associated with node</param>
private void AddNodes(TreeNode node, CFStorage cfs)
private static void AddNodes(TreeNode node, CFStorage cfs)
{
Action<CFItem> va = delegate (CFItem target)
{
Expand Down
6 changes: 3 additions & 3 deletions sources/Test/OpenMcdf.Test/CFSStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions sources/Test/OpenMcdf.Test/CFSTorageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CFItem> va = delegate (CFItem item)
{
Expand Down Expand Up @@ -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<CFItem> va = delegate (CFItem item)
{
Expand Down Expand Up @@ -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<CFItem> va = delegate (CFItem item)
{
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion sources/Test/OpenMcdf.Test/CompoundFileTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4008143

Please sign in to comment.