Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CSTRMarshaler.cs
Go to the documentation of this file.
2
3namespace System.StubHelpers;
4
5internal static class CSTRMarshaler
6{
7 internal unsafe static IntPtr ConvertToNative(int flags, string strManaged, IntPtr pNativeBuffer)
8 {
9 if (strManaged == null)
10 {
11 return IntPtr.Zero;
12 }
13 byte* ptr = (byte*)(void*)pNativeBuffer;
14 int num;
15 if (ptr != null || Marshal.SystemMaxDBCSCharSize == 1)
16 {
17 num = checked((strManaged.Length + 1) * Marshal.SystemMaxDBCSCharSize + 1);
18 bool flag = false;
19 if (ptr == null)
20 {
21 ptr = (byte*)(void*)Marshal.AllocCoTaskMem(num);
22 flag = true;
23 }
24 try
25 {
26 num = Marshal.StringToAnsiString(strManaged, ptr, num, (flags & 0xFF) != 0, flags >> 8 != 0);
27 }
28 catch (Exception) when (flag)
29 {
31 throw;
32 }
33 }
34 else if (strManaged.Length == 0)
35 {
36 num = 0;
37 ptr = (byte*)(void*)Marshal.AllocCoTaskMem(2);
38 }
39 else
40 {
41 byte[] array = AnsiCharMarshaler.DoAnsiConversion(strManaged, (flags & 0xFF) != 0, flags >> 8 != 0, out num);
42 ptr = (byte*)(void*)Marshal.AllocCoTaskMem(num + 2);
43 Buffer.Memmove(ref *ptr, ref MemoryMarshal.GetArrayDataReference(array), (nuint)num);
44 }
45 ptr[num] = 0;
46 ptr[num + 1] = 0;
47 return (IntPtr)ptr;
48 }
49
50 internal unsafe static string ConvertToManaged(IntPtr cstr)
51 {
52 if (IntPtr.Zero == cstr)
53 {
54 return null;
55 }
56 return new string((sbyte*)(void*)cstr);
57 }
58
59 internal static void ClearNative(IntPtr pNative)
60 {
61 Marshal.FreeCoTaskMem(pNative);
62 }
63
64 internal unsafe static void ConvertFixedToNative(int flags, string strManaged, IntPtr pNativeBuffer, int length)
65 {
66 if (strManaged == null)
67 {
68 if (length > 0)
69 {
70 *(sbyte*)(void*)pNativeBuffer = 0;
71 }
72 return;
73 }
74 int num = strManaged.Length;
75 if (num >= length)
76 {
77 num = length - 1;
78 }
79 byte* ptr = (byte*)(void*)pNativeBuffer;
80 bool flag = flags >> 8 != 0;
81 bool flag2 = (flags & 0xFF) != 0;
82 uint num2 = 0u;
83 int num3;
84 fixed (char* lpWideCharStr = strManaged)
85 {
86 num3 = Interop.Kernel32.WideCharToMultiByte(0u, (!flag2) ? 1024u : 0u, lpWideCharStr, num, ptr, length, IntPtr.Zero, flag ? new IntPtr(&num2) : IntPtr.Zero);
87 }
88 if (num2 != 0)
89 {
91 }
92 if (num3 == length)
93 {
94 num3--;
95 }
96 ptr[num3] = 0;
97 }
98
99 internal unsafe static string ConvertFixedToManaged(IntPtr cstr, int length)
100 {
101 int num = SpanHelpers.IndexOf(ref *(byte*)(void*)cstr, 0, length);
102 if (num != -1)
103 {
104 length = num;
105 }
106 return new string((sbyte*)(void*)cstr, 0, length);
107 }
108}
static unsafe int WideCharToMultiByte(uint CodePage, uint dwFlags, char *lpWideCharStr, int cchWideChar, byte *lpMultiByteStr, int cbMultiByte, IntPtr lpDefaultChar, IntPtr lpUsedDefaultChar)
static void Memmove(ref byte dest, ref byte src, nuint len)
Definition Buffer.cs:215
static unsafe int StringToAnsiString(string s, byte *buffer, int bufferLength, bool bestFit=false, bool throwOnUnmappableChar=false)
Definition Marshal.cs:1608
static void FreeCoTaskMem(IntPtr ptr)
Definition Marshal.cs:1712
static IntPtr AllocCoTaskMem(int cb)
Definition Marshal.cs:1702
static readonly int SystemMaxDBCSCharSize
Definition Marshal.cs:19
static unsafe ref byte GetArrayDataReference(Array array)
static string Interop_Marshal_Unmappable_Char
Definition SR.cs:1374
Definition SR.cs:7
static int IndexOf(ref byte searchSpace, int searchSpaceLength, ref byte value, int valueLength)
static unsafe byte[] DoAnsiConversion(string str, bool fBestFit, bool fThrowOnUnmappableChar, out int cbLength)
static void ClearNative(IntPtr pNative)
static unsafe void ConvertFixedToNative(int flags, string strManaged, IntPtr pNativeBuffer, int length)
static unsafe string ConvertToManaged(IntPtr cstr)
static unsafe IntPtr ConvertToNative(int flags, string strManaged, IntPtr pNativeBuffer)
static unsafe string ConvertFixedToManaged(IntPtr cstr, int length)
static readonly IntPtr Zero
Definition IntPtr.cs:18