Skip to content

Commit

Permalink
Fix for inbound byte attributes getting corrupted by UTF8 Encoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
desukuran committed Apr 23, 2024
1 parent a723411 commit 9dcf3a5
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/PFire.Core/Protocol/MessageSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,26 @@ public static IMessage Deserialize(BinaryReader reader, IMessage messageBase)

var value = XFireAttributeFactory.Instance.GetAttribute(attributeType).ReadValue(reader);

var field = fieldInfo.Where(a => a.GetCustomAttribute<XMessageField>() != null)
.FirstOrDefault(a => a.GetCustomAttribute<XMessageField>()?.Name == attributeName);
var field = fieldInfo
.Where(a => a.GetCustomAttribute<XMessageField>() != null)
.FirstOrDefault(a =>
{
var attribute = a.GetCustomAttribute<XMessageField>();
if (attribute != null)
{
if (attributeName.Length == 1)
{
// If attributeName is a single byte, compare with the attribute's NameAsBytes
return attribute.NameAsBytes.SequenceEqual(attributeName);
}
else
{
// If attributeName is a byte array with length > 1, compare with the attribute's Name converted to string
return Encoding.UTF8.GetString(attributeName) == attribute.Name;
}
}
return false;
});

if (field != null)
{
Expand All @@ -59,7 +77,7 @@ public static IMessage Deserialize(BinaryReader reader, IMessage messageBase)
return messageBase;
}

private static string GetAttributeName(BinaryReader reader, Type messageType)
private static byte[] GetAttributeName(BinaryReader reader, Type messageType)
{
HashSet<Type> messageTypeSet = new HashSet<Type>
{
Expand All @@ -75,9 +93,19 @@ private static string GetAttributeName(BinaryReader reader, Type messageType)

byte count = messageTypeSet.Contains(messageType) ? (byte)1 : reader.ReadByte();

// Check if count is 1, indicating a single byte
if (count == 1)
{
// Read the single byte and treat it as a numeric value
byte numericValue = reader.ReadByte();
return [numericValue];
}
else
{
// Read the bytes for the attribute name
var readBytes = reader.ReadBytes(count);
return Encoding.UTF8.GetString(readBytes);
return readBytes;
}
}

public static byte[] Serialize(IMessage message)
Expand Down

0 comments on commit 9dcf3a5

Please sign in to comment.