Skip to content

Commit 55ad75a

Browse files
committed
KodiBridgeABI: decode Python messages as utf-8
1 parent abc7bc0 commit 55ad75a

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

KodiInterop/KodiInterop/KodiBridgeABI.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Reflection;
55
using System.Runtime.InteropServices;
6+
using System.Text;
67

78
namespace Smx.KodiInterop
89
{
@@ -18,16 +19,23 @@ public delegate IntPtr PySendStringDelegate(
1819

1920
private readonly PySendStringDelegate _delegate;
2021

22+
private static unsafe int strlen(IntPtr ptr){
23+
int l = 0;
24+
byte* dptr = (byte *)ptr.ToPointer();
25+
while(*dptr++ != 0) ++l;
26+
return l;
27+
}
28+
2129

2230
/// <summary>
2331
/// Wrapper to PySendMessageDelegate that does *not* free the string, like in MarshalAs (causing a python crash when it tries to free the string again)
2432
/// </summary>
2533
/// <param name="messageData"></param>
2634
/// <returns></returns>
27-
public string PySendMessage(string messageData) {
35+
public unsafe string PySendMessage(string messageData) {
2836
IntPtr pyStr = _delegate(messageData);
2937
if (pyStr == IntPtr.Zero) return "";
30-
return Marshal.PtrToStringAnsi(pyStr);
38+
return Encoding.UTF8.GetString((byte *)pyStr.ToPointer(), strlen(pyStr));
3139
}
3240

3341
public KodiBridgeABI(IntPtr pySendStringFuncPtr) {

KodiInterop/KodiInterop/KodiInterop.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
88
<LangVersion>latest</LangVersion>
99
<Nullable>enable</Nullable>
10+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1011
</PropertyGroup>
1112
<ItemGroup>
1213
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />

0 commit comments

Comments
 (0)