1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-26 05:58:47 +00:00

stackalloc sufficient length for byte span

This commit is contained in:
Oliver Booth 2022-12-04 13:57:29 +00:00
parent 0c0866f54e
commit 20e5eebf47
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634

View File

@ -5,6 +5,7 @@ namespace VpSharp.Internal;
internal sealed class Utf8StringToNative : ICustomMarshaler
{
private static readonly Encoding Utf8Encoding = Encoding.UTF8;
private static Utf8StringToNative? s_instance;
public static ICustomMarshaler GetInstance(string cookie)
@ -29,14 +30,15 @@ internal sealed class Utf8StringToNative : ICustomMarshaler
public unsafe nint MarshalManagedToNative(object managedObj)
{
var managedString = (string)managedObj;
Span<byte> utf8Bytes = stackalloc byte[managedString.Length];
Encoding.UTF8.GetBytes(managedString, utf8Bytes);
int byteCount = Utf8Encoding.GetByteCount(managedString);
Span<byte> utf8Bytes = stackalloc byte[byteCount];
Utf8Encoding.GetBytes(managedString, utf8Bytes);
fixed (byte* data = &MemoryMarshal.GetReference(utf8Bytes))
{
nint buffer = Marshal.AllocHGlobal(utf8Bytes.Length + 1);
Buffer.MemoryCopy(data, (void*)buffer, utf8Bytes.Length, utf8Bytes.Length);
Marshal.WriteByte(buffer, utf8Bytes.Length, 0);
nint buffer = Marshal.AllocHGlobal(byteCount + 1);
Buffer.MemoryCopy(data, (void*)buffer, byteCount, byteCount);
Marshal.WriteByte(buffer, byteCount, 0);
return buffer;
}
}