Skip to content

Commit

Permalink
U/sgriffin/nspi (#82)
Browse files Browse the repository at this point in the history
* Clean up handling of MAPIStringAddressBook

* Use annotaed data and remove special case

* parse address book prop tag names
  • Loading branch information
stephenegriffin committed Feb 12, 2024
1 parent e0cb1b9 commit 21e0433
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 89 deletions.
78 changes: 1 addition & 77 deletions MAPIInspector/Source/Parsers/BaseStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static TreeNode AddNodesForTree(string nodeName, object obj, int startInd
{
offset = ad.Size;
res.Text = ad.ToString();
foreach(var parsedValue in ad.parsedValues)
foreach (var parsedValue in ad.parsedValues)
{
var alternateParsingNode = new TreeNode($"{parsedValue.Key}:{parsedValue.Value}");
alternateParsingNode.Tag = new Position(current, offset);
Expand All @@ -162,82 +162,6 @@ public static TreeNode AddNodesForTree(string nodeName, object obj, int startInd

return res;
}
else if (t.Name == "MAPIStringAddressBook")
{
FieldInfo[] infoString = t.GetFields();

// MagicByte node
if (infoString[1].GetValue(obj) != null)
{
TreeNode nodeMagic = new TreeNode(string.Format("{0}:{1}", infoString[1].Name, infoString[1].GetValue(obj)));
Position positionStringMagic = new Position(current, 1);
nodeMagic.Tag = positionStringMagic;
res.Nodes.Add(nodeMagic);
current += 1;
}

// value node
string terminator = (string)infoString[3].GetValue(obj);
int os = 0;
TreeNode node = new TreeNode(string.Format("{0}:{1}", infoString[0].Name, infoString[0].GetValue(obj)));

// If the Encoding is Unicode.
if (infoString[2].GetValue(obj).ToString() == "System.Text.UnicodeEncoding")
{
// If the StringLength is not equal 0, the StringLength will be OS value.
if (infoString[4].GetValue(obj).ToString() != "0")
{
os = ((int)infoString[4].GetValue(obj)) * 2;
}
else
{
if (infoString[0].GetValue(obj) != null)
{
os = ((string)infoString[0].GetValue(obj)).Length * 2;
}

if (infoString[5].GetValue(obj).ToString() != "False")
{
os -= 1;
}

os += terminator.Length * 2;
}
}
else
{
// If the Encoding is ASCII.
if (infoString[4].GetValue(obj).ToString() != "0")
{
// If the StringLength is not equal 0, the StringLength will be OS value.
os = (int)infoString[4].GetValue(obj);
}
else
{
if (infoString[0].GetValue(obj) != null)
{
os = ((string)infoString[0].GetValue(obj)).Length;
}

os += terminator.Length;
}
}

Position positionString = new Position(current, os);
node.Tag = positionString;
res.Nodes.Add(node);

if (infoString[1].GetValue(obj) != null)
{
offset = os + 1;
}
else
{
offset = os;
}

return res;
}

// Check whether the data type is simple type
if (Enum.IsDefined(typeof(DataType), t.Name))
Expand Down
69 changes: 59 additions & 10 deletions MAPIInspector/Source/Parsers/MSOXCDATA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4098,7 +4098,7 @@ public override int Size
/// <summary>
/// The MAPIString class to record the related attributes of string.
/// </summary>
public class MAPIStringAddressBook : BaseStructure
public class MAPIStringAddressBook : AnnotatedData
{
/// <summary>
/// The string value
Expand All @@ -4113,7 +4113,7 @@ public class MAPIStringAddressBook : BaseStructure
/// <summary>
/// The string Encoding : ASCII or Unicode
/// </summary>
private Encoding Encode;
public Encoding Encode;

/// <summary>
/// The string Terminator. Default is "\0".
Expand All @@ -4130,20 +4130,13 @@ public class MAPIStringAddressBook : BaseStructure
/// </summary>
public bool ReducedUnicode;

/// <summary>
/// Initializes a new instance of the MAPIStringAddressBook class without parameters.
/// </summary>
public MAPIStringAddressBook()
{
}

/// <summary>
/// Initializes a new instance of the MAPIStringAddressBook class with parameters.
/// </summary>
/// <param name="encode">The encoding type</param>
/// <param name="terminator">The string terminator</param>
/// <param name="stringLength">The string length</param>
/// <param name="reducedUnicode">INdicate whether the terminator is reduced</param>
/// <param name="reducedUnicode">Indicate whether the terminator is reduced</param>
public MAPIStringAddressBook(Encoding encode, string terminator = "\0", int stringLength = 0, bool reducedUnicode = false)
{
this.Encode = encode;
Expand All @@ -4170,6 +4163,62 @@ public override void Parse(Stream s)

this.Value = this.ReadString(this.Encode, this.Terminator, this.StringLength, this.ReducedUnicode);
}
public override string ToString() => Value;
public override int Size
{
get
{
var len = 0;

if (Encode == Encoding.Unicode)
{
// If the StringLength is not equal 0, the StringLength will be basis for size
if (StringLength != 0)
{
len = StringLength * 2;
}
else
{
if (Value != null)
{
len = Value.Length * 2;
}

if (ReducedUnicode)
{
len -= 1;
}

len += Terminator.Length * 2;
}
}
else
{
// If the Encoding is ASCII.
if (StringLength != 0)
{
// If the StringLength is not equal 0, the StringLength will be basis for size
len = StringLength;
}
else
{
if (Value != null)
{
len = Value.Length;
}

len += Terminator.Length;
}
}

if (HasValue != null)
{
len += 1;
}

return len;
}
}
}

#region 2.1 AddressList Structures
Expand Down
35 changes: 33 additions & 2 deletions MAPIInspector/Source/Parsers/MSOXCMAPIHTTP.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace MAPIInspector.Parsers
{
using MapiInspector;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -4307,6 +4308,11 @@ public class AddressBookPropertyValue : BaseStructure
/// </summary>
private CountWideEnum countWide;

/// <summary>
/// Source property tag information
/// </summary>
public AnnotatedComment PropertyTag;

/// <summary>
/// Initializes a new instance of the AddressBookPropertyValue class.
/// </summary>
Expand Down Expand Up @@ -4367,13 +4373,18 @@ public class AddressBookTaggedPropertyValue : BaseStructure
/// <summary>
/// An unsigned integer that identifies the property.
/// </summary>
public ushort PropertyId;
public PidTagPropertyEnum PropertyId;

/// <summary>
/// An AddressBookPropertyValue structure
/// </summary>
public AddressBookPropertyValue PropertyValue;

/// <summary>
/// Source property tag information
/// </summary>
public AnnotatedComment PropertyTag;

/// <summary>
/// Parse the AddressBookTaggedPropertyValue structure.
/// </summary>
Expand All @@ -4382,10 +4393,11 @@ public override void Parse(Stream s)
{
base.Parse(s);
this.PropertyType = (PropertyDataType)this.ReadUshort();
this.PropertyId = this.ReadUshort();
this.PropertyId = (PidTagPropertyEnum)this.ReadUshort();
AddressBookPropertyValue addressBookValue = new AddressBookPropertyValue(this.PropertyType);
addressBookValue.Parse(s);
this.PropertyValue = addressBookValue;
this.PropertyTag = $"{PropertyType}:{Utilities.EnumToString(PropertyId)}";
}
}
#endregion
Expand Down Expand Up @@ -4445,6 +4457,11 @@ public class AddressBookTypedPropertyValue : BaseStructure
/// </summary>
public AddressBookPropertyValue PropertyValue;

/// <summary>
/// Source property tag information
/// </summary>
public AnnotatedComment PropertyTag;

/// <summary>
/// Parse the AddressBookTypedPropertyValue structure.
/// </summary>
Expand Down Expand Up @@ -4481,6 +4498,11 @@ public class AddressBookFlaggedPropertyValue : BaseStructure
/// </summary>
private PropertyDataType propertyDataType;

/// <summary>
/// Source property tag information
/// </summary>
public AnnotatedComment PropertyTag;

/// <summary>
/// Initializes a new instance of the AddressBookFlaggedPropertyValue class.
/// </summary>
Expand Down Expand Up @@ -4540,6 +4562,11 @@ public class AddressBookFlaggedPropertyValueWithType : BaseStructure
/// </summary>
public AddressBookPropertyValue PropertyValue;

/// <summary>
/// Source property tag information
/// </summary>
public AnnotatedComment PropertyTag;

/// <summary>
/// Parse the AddressBookFlaggedPropertyValueWithType structure.
/// </summary>
Expand Down Expand Up @@ -4628,12 +4655,14 @@ public override void Parse(Stream s)
{
AddressBookPropertyValue propValue = new AddressBookPropertyValue(propTag.PropertyType, this.ptypMultiCountSize);
propValue.Parse(s);
propValue.PropertyTag = $"{propTag.PropertyType}:{Utilities.EnumToString(propTag.PropertyId)}";
addrRowValue = propValue;
}
else
{
AddressBookTypedPropertyValue typePropValue = new AddressBookTypedPropertyValue();
typePropValue.Parse(s);
typePropValue.PropertyTag = $"{propTag.PropertyType}:{Utilities.EnumToString(propTag.PropertyId)}";
addrRowValue = typePropValue;
}
}
Expand All @@ -4643,12 +4672,14 @@ public override void Parse(Stream s)
{
AddressBookFlaggedPropertyValue flagPropValue = new AddressBookFlaggedPropertyValue(propTag.PropertyType);
flagPropValue.Parse(s);
flagPropValue.PropertyTag = $"{propTag.PropertyType}:{Utilities.EnumToString(propTag.PropertyId)}";
addrRowValue = flagPropValue;
}
else
{
AddressBookFlaggedPropertyValueWithType flagPropValue = new AddressBookFlaggedPropertyValueWithType();
flagPropValue.Parse(s);
flagPropValue.PropertyTag = $"{propTag.PropertyType}:{Utilities.EnumToString(propTag.PropertyId)}";
addrRowValue = flagPropValue;
}
}
Expand Down

0 comments on commit 21e0433

Please sign in to comment.