Skip to content

Commit

Permalink
Explicitly turn off BOM when marshaling strings to/from Steamworks, j…
Browse files Browse the repository at this point in the history
…ust to be safe
  • Loading branch information
Rohansi committed Feb 4, 2025
1 parent b4d3a65 commit 0595c9c
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions Facepunch.Steamworks/Utility/Utf8String.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;

namespace Steamworks
{
internal unsafe class Utf8StringToNative : ICustomMarshaler
{
internal static readonly Encoding Utf8NoBom = new UTF8Encoding( false, false );

public IntPtr MarshalManagedToNative(object managedObj)
{
if ( managedObj == null )
Expand All @@ -19,10 +17,10 @@ public IntPtr MarshalManagedToNative(object managedObj)
{
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;

Expand Down Expand Up @@ -64,7 +62,7 @@ public unsafe static implicit operator string( Utf8StringPointer p )
dataLen++;
}

return Encoding.UTF8.GetString( bytes, dataLen );
return Utf8StringToNative.Utf8NoBom.GetString( bytes, dataLen );
}
}
}

0 comments on commit 0595c9c

Please sign in to comment.