From 0595c9c79660678a7fa6a35ce8479f34be51cc60 Mon Sep 17 00:00:00 2001 From: Rohan Singh Date: Mon, 3 Feb 2025 22:13:32 -0500 Subject: [PATCH] Explicitly turn off BOM when marshaling strings to/from Steamworks, just to be safe --- Facepunch.Steamworks/Utility/Utf8String.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Facepunch.Steamworks/Utility/Utf8String.cs b/Facepunch.Steamworks/Utility/Utf8String.cs index 3599753..74b101e 100644 --- a/Facepunch.Steamworks/Utility/Utf8String.cs +++ b/Facepunch.Steamworks/Utility/Utf8String.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; using System.Runtime.InteropServices; using System.Text; @@ -10,6 +6,8 @@ namespace Steamworks { internal unsafe class Utf8StringToNative : ICustomMarshaler { + internal static readonly Encoding Utf8NoBom = new UTF8Encoding( false, false ); + public IntPtr MarshalManagedToNative(object managedObj) { if ( managedObj == null ) @@ -19,10 +17,10 @@ namespace Steamworks { fixed ( char* strPtr = str ) { - int len = Encoding.UTF8.GetByteCount( str ); + int len = Utf8NoBom.GetByteCount( str ); var mem = Marshal.AllocHGlobal( len + 1 ); - var wlen = System.Text.Encoding.UTF8.GetBytes( strPtr, str.Length, (byte*)mem, len + 1 ); + var wlen = Utf8NoBom.GetBytes( strPtr, str.Length, (byte*)mem, len + 1 ); ( (byte*)mem )[wlen] = 0; @@ -64,7 +62,7 @@ namespace Steamworks dataLen++; } - return Encoding.UTF8.GetString( bytes, dataLen ); + return Utf8StringToNative.Utf8NoBom.GetString( bytes, dataLen ); } } }