diff --git a/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs b/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs index 4c240890..e1ea0d55 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; using OpenMcdf.Extensions.OLEProperties.Interfaces; namespace OpenMcdf.Extensions.OLEProperties @@ -16,7 +15,15 @@ public class OLEPropertiesContainer public bool HasUserDefinedProperties { get; private set; } - public ContainerType ContainerType { get; internal set; } + /// + /// Gets the type of the container. + /// + public ContainerType ContainerType { get; } + + /// + /// Gets the FMTID of the properties container. + /// + public Guid FMTID0 { get; } public PropertyContext Context { get; private set; } @@ -69,6 +76,11 @@ public static OLEPropertiesContainer CreateNewSummaryInfo(SummaryInfoProperties return null; } + /// + /// Create a new instance of with the specified code page and container type. + /// + /// The code page to use for the new container. + /// The type of the new container. public OLEPropertiesContainer(int codePage, ContainerType containerType) { Context = new PropertyContext @@ -78,6 +90,7 @@ public OLEPropertiesContainer(int codePage, ContainerType containerType) }; this.ContainerType = containerType; + this.FMTID0 = FmtIdFromContainerType(containerType); } internal OLEPropertiesContainer(CFStream cfStream) @@ -87,19 +100,8 @@ internal OLEPropertiesContainer(CFStream cfStream) this.cfStream = cfStream; pStream.Read(new BinaryReader(new StreamDecorator(cfStream))); - switch (pStream.FMTID0.ToString("B").ToUpperInvariant()) - { - case WellKnownFMTID.FMTID_SummaryInformation: - this.ContainerType = ContainerType.SummaryInfo; - break; - case WellKnownFMTID.FMTID_DocSummaryInformation: - this.ContainerType = ContainerType.DocumentSummaryInfo; - break; - default: - this.ContainerType = ContainerType.AppSpecific; - break; - } - + this.FMTID0 = pStream.FMTID0; + this.ContainerType = ContainerTypeFromFmtId(pStream.FMTID0); this.PropertyNames = (Dictionary)pStream.PropertySet0.Properties .Where(p => p.PropertyType == PropertyType.DictionaryProperty).FirstOrDefault()?.Value; @@ -134,8 +136,6 @@ internal OLEPropertiesContainer(CFStream cfStream) UserDefinedProperties = new OLEPropertiesContainer(pStream.PropertySet1.PropertyContext.CodePage, ContainerType.UserDefinedProperties); this.HasUserDefinedProperties = true; - UserDefinedProperties.ContainerType = ContainerType.UserDefinedProperties; - for (int i = 0; i < pStream.PropertySet1.Properties.Count; i++) { if (pStream.PropertySet1.PropertyIdentifierAndOffsets[i].PropertyIdentifier == 0) continue; @@ -247,7 +247,7 @@ public void Save(CFStream cfStream) NumPropertySets = 1, - FMTID0 = this.ContainerType == ContainerType.SummaryInfo ? new Guid(WellKnownFMTID.FMTID_SummaryInformation) : new Guid(WellKnownFMTID.FMTID_DocSummaryInformation), + FMTID0 = this.FMTID0, Offset0 = 0, FMTID1 = Guid.Empty, @@ -311,5 +311,41 @@ public void Save(CFStream cfStream) ps.Write(bw); } + + // Determine the type of the container from the FMTID0 property. + private static ContainerType ContainerTypeFromFmtId(Guid fmtId0) + { + if (fmtId0 == Guid.Parse(WellKnownFMTID.FMTID_SummaryInformation)) + return ContainerType.SummaryInfo; + else if (fmtId0 == Guid.Parse(WellKnownFMTID.FMTID_DocSummaryInformation)) + return ContainerType.DocumentSummaryInfo; + else if (fmtId0 == Guid.Parse(WellKnownFMTID.FMTID_GlobalInfo)) + return ContainerType.GlobalInfo; + else if (fmtId0 == Guid.Parse(WellKnownFMTID.FMTID_ImageInfo)) + return ContainerType.ImageInfo; + else if (fmtId0 == Guid.Parse(WellKnownFMTID.FMTID_ImageContents)) + return ContainerType.ImageContents; + + return ContainerType.AppSpecific; + } + + // Determine the FMTID property from the container type. + // Note: Uses FMTID_DocSummaryInformation by default to match the previous behavior. + private static Guid FmtIdFromContainerType(ContainerType containerType) + { + switch (containerType) + { + case ContainerType.SummaryInfo: + return Guid.Parse(WellKnownFMTID.FMTID_SummaryInformation); + case ContainerType.GlobalInfo: + return Guid.Parse(WellKnownFMTID.FMTID_GlobalInfo); + case ContainerType.ImageContents: + return Guid.Parse(WellKnownFMTID.FMTID_ImageContents); + case ContainerType.ImageInfo: + return Guid.Parse(WellKnownFMTID.FMTID_ImageInfo); + default: + return Guid.Parse(WellKnownFMTID.FMTID_DocSummaryInformation); + } + } } }