-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathLibPcapSafeNativeMethods.Encoding.cs
156 lines (139 loc) · 5.12 KB
/
LibPcapSafeNativeMethods.Encoding.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
This file is part of SharpPcap.
SharpPcap is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SharpPcap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with SharpPcap. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright 2005 Tamir Gal <[email protected]>
* Copyright 2008-2009 Phillip Lemon <[email protected]>
* Copyright 2008-2011 Chris Morgan <[email protected]>
* Copyright 2021 Ayoub Kaanich <[email protected]>
*/
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace SharpPcap.LibPcap
{
internal static partial class LibPcapSafeNativeMethods
{
/// <summary>
/// This defaul is good enough for .NET Framework and .NET Core on non Windows with Libpcap default config
/// </summary>
private static readonly Encoding StringEncoding = Encoding.Default;
private static Encoding ConfigureStringEncoding()
{
if (!
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
)
{
// libpcap always use UTF-8 when not on Windows
return Encoding.UTF8;
}
try
{
// Try to change Libpcap to UTF-8 mode
var errorBuffer = new StringBuilder(Pcap.PCAP_ERRBUF_SIZE);
const uint PCAP_CHAR_ENC_UTF_8 = 1;
var res = pcap_init(PCAP_CHAR_ENC_UTF_8, errorBuffer);
if (res == 0)
{
// We made it
return Encoding.UTF8;
}
}
catch (TypeLoadException)
{
// pcap_init not supported, using old Libpcap
}
// Needed especially in .NET Core, to make sure codepage 0 returns the system default non-unicode code page
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// In windows by default, system code page is used
return Encoding.GetEncoding(0);
}
/// <summary>
/// Helper class to marshall string depending on encoding used by Libpcap
/// </summary>
private class PcapStringMarshaler : ICustomMarshaler
{
public static ICustomMarshaler GetInstance(string cookie)
{
return new PcapStringMarshaler(cookie);
}
private readonly bool FreeOnClean;
public PcapStringMarshaler(string cookie)
{
// If the string was not allocated by us, don't free it
FreeOnClean = !cookie.Contains("no_free");
}
public void CleanUpManagedData(object managedObj)
{
// Nothing to clean
}
public void CleanUpNativeData(IntPtr nativeData)
{
if (FreeOnClean)
{
Marshal.FreeHGlobal(nativeData);
}
}
public int GetNativeDataSize()
{
return -1;
}
public IntPtr MarshalManagedToNative(object managedObj)
{
if (managedObj is null)
{
return IntPtr.Zero;
}
byte[] bytes = null;
var byteCount = 0;
if (managedObj is string str)
{
bytes = StringEncoding.GetBytes(str);
byteCount = bytes.Length + 1;
}
if (managedObj is StringBuilder builder)
{
bytes = StringEncoding.GetBytes(builder.ToString());
byteCount = StringEncoding.GetMaxByteCount(builder.Capacity) + 1;
}
if (bytes is null)
{
throw new ArgumentException("The input argument is not a supported type.");
}
var ptr = Marshal.AllocHGlobal(byteCount);
Marshal.Copy(bytes, 0, ptr, bytes.Length);
// Put zero string termination
Marshal.WriteByte(ptr + bytes.Length, 0);
return ptr;
}
public unsafe object MarshalNativeToManaged(IntPtr nativeData)
{
if (nativeData == IntPtr.Zero)
{
return null;
}
var bytes = (byte*)nativeData;
var nbBytes = 0;
while (*(bytes + nbBytes) != 0)
{
nbBytes++;
}
return StringEncoding.GetString(bytes, nbBytes);
}
}
}
}