diff --git a/OpenMcdf.Ole/DictionaryEntry.cs b/OpenMcdf.Ole/DictionaryEntry.cs deleted file mode 100644 index 9fa05739..00000000 --- a/OpenMcdf.Ole/DictionaryEntry.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System.Text; - -namespace OpenMcdf.Ole; - -public class DictionaryEntry -{ - readonly int codePage; - private byte[]? nameBytes; - - public DictionaryEntry(int codePage) - { - this.codePage = codePage; - } - - public uint PropertyIdentifier { get; set; } - - public int Length { get; set; } - - public string Name - { - get - { - if (nameBytes is null) - return string.Empty; - - string result = Encoding.GetEncoding(codePage).GetString(nameBytes); - result = result.Trim('\0'); - return result; - } - } - - public void Read(BinaryReader br) - { - PropertyIdentifier = br.ReadUInt32(); - Length = br.ReadInt32(); - - if (codePage == CodePages.WinUnicode) - { - nameBytes = br.ReadBytes(Length << 1); - - int m = Length * 2 % 4; - if (m > 0) - br.ReadBytes(m); - } - else - { - nameBytes = br.ReadBytes(Length); - } - } - - public void Write(BinaryWriter bw) - { - bw.Write(PropertyIdentifier); - bw.Write(Length); - bw.Write(nameBytes!); - } -} diff --git a/OpenMcdf.Ole/DictionaryProperty.cs b/OpenMcdf.Ole/DictionaryProperty.cs index 7c8a0759..3a0d0dc6 100644 --- a/OpenMcdf.Ole/DictionaryProperty.cs +++ b/OpenMcdf.Ole/DictionaryProperty.cs @@ -2,7 +2,7 @@ namespace OpenMcdf.Ole; -public class DictionaryProperty : IProperty +public sealed class DictionaryProperty : IProperty { private readonly int codePage; private Dictionary? entries = new(); @@ -28,10 +28,7 @@ public void Read(BinaryReader br) for (uint i = 0; i < numEntries; i++) { - DictionaryEntry de = new(codePage); - - de.Read(br); - entries!.Add(de.PropertyIdentifier, de.Name); + ReadEntry(br); } int m = (int)(br.BaseStream.Position - curPos) % 4; @@ -45,6 +42,31 @@ public void Read(BinaryReader br) } } + // Read a single dictionary entry + private void ReadEntry(BinaryReader br) + { + uint propertyIdentifier = br.ReadUInt32(); + int length = br.ReadInt32(); + + byte[] nameBytes; + + if (codePage == CodePages.WinUnicode) + { + nameBytes = br.ReadBytes(length << 1); + + int m = length * 2 % 4; + if (m > 0) + br.ReadBytes(m); + } + else + { + nameBytes = br.ReadBytes(length); + } + + string entryName = Encoding.GetEncoding(codePage).GetString(nameBytes).Trim('\0'); + entries!.Add(propertyIdentifier, entryName); + } + /// /// Write the dictionary and all its values into the specified . ///