Skip to content

Commit

Permalink
Added structure alignment types.
Browse files Browse the repository at this point in the history
  • Loading branch information
tyranid committed Aug 19, 2024
1 parent 280826a commit 590cbca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
4 changes: 4 additions & 0 deletions NtApiDotNet/Ndr/NdrSimpleTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,10 @@ internal static NdrBaseTypeReference Read(NdrParseContext context, BinaryReader
case NdrFormatCharacter.FC_STRUCTPAD6:
case NdrFormatCharacter.FC_STRUCTPAD7:
return new NdrStructurePaddingTypeReference(format);
case NdrFormatCharacter.FC_ALIGNM2:
case NdrFormatCharacter.FC_ALIGNM4:
case NdrFormatCharacter.FC_ALIGNM8:
return new NdrStructureAlignTypeReference(format);
case NdrFormatCharacter.FC_IGNORE:
return new NdrIgnoreTypeReference();
case NdrFormatCharacter.FC_SYSTEM_HANDLE:
Expand Down
41 changes: 38 additions & 3 deletions NtApiDotNet/Ndr/NdrStructureTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,18 @@ protected virtual List<NdrStructureMember> PopulateMembers()
int current_offset = 0;
foreach (var type in _base_members)
{
if (!(type is NdrStructurePaddingTypeReference))
if (type is NdrStructureAlignTypeReference align)
{
members.Add(new NdrStructureMember(type, current_offset, $"Member{current_offset:X}"));
current_offset = align.Align(current_offset);
}
else
{
if (!(type is NdrStructurePaddingTypeReference))
{
members.Add(new NdrStructureMember(type, current_offset, $"Member{current_offset:X}"));
}
current_offset += type.GetSize();
}
current_offset += type.GetSize();
}
return members;
}
Expand Down Expand Up @@ -311,6 +318,34 @@ public override int GetSize()
}
}

[Serializable]
public sealed class NdrStructureAlignTypeReference : NdrBaseTypeReference
{
internal NdrStructureAlignTypeReference(NdrFormatCharacter format) : base(format)
{
}

internal int Align(int offset)
{
switch (Format)
{
case NdrFormatCharacter.FC_ALIGNM2:
return (offset + 1) & ~1;
case NdrFormatCharacter.FC_ALIGNM4:
return (offset + 3) & ~3;
case NdrFormatCharacter.FC_ALIGNM8:
return (offset + 7) & ~7;
default:
throw new InvalidOperationException("Format must be an alignment.");
}
}

public override int GetSize()
{
return 0;
}
}

[Serializable]
public sealed class NdrPointerInfoInstance
{
Expand Down

0 comments on commit 590cbca

Please sign in to comment.