Repository navigation
Expand file tree
/
Copy pathMarshalType.cs
More file actions
224 lines (213 loc) · 9.71 KB
/
Copy pathMarshalType.cs
File metadata and controls
224 lines (213 loc) · 9.71 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Runtime.InteropServices;
using System.Text;
using Process.NET.Memory;
// ReSharper disable All
namespace Process.NET.Marshaling
{
/// <summary>
/// Static class providing tools for extracting information related to types.
/// </summary>
/// <typeparam name="T">Type to analyse.</typeparam>
public static class MarshalType<T>
{
/// <summary>
/// Initializes static information related to the specified type.
/// </summary>
static MarshalType()
{
// Gather information related to the provided type
IsIntPtr = typeof (T) == typeof (IntPtr);
RealType = typeof (T);
Size = TypeCode == TypeCode.Boolean ? 1 : Marshal.SizeOf(RealType);
TypeCode = Type.GetTypeCode(RealType);
// Check if the type can be stored in registers
if (IntPtr.Size == 8)
CanBeStoredInRegisters = Test();
else
CanBeStoredInRegisters =
IsIntPtr ||
TypeCode == TypeCode.Boolean ||
TypeCode == TypeCode.Byte ||
TypeCode == TypeCode.Char ||
TypeCode == TypeCode.Int16 ||
TypeCode == TypeCode.Int32 ||
TypeCode == TypeCode.Int64 ||
TypeCode == TypeCode.SByte ||
TypeCode == TypeCode.Single ||
TypeCode == TypeCode.UInt16 ||
TypeCode == TypeCode.UInt32;
}
/// <summary>
/// Gets if the type can be stored in a registers (for example ACX, ECX, ...).
/// </summary>
public static bool CanBeStoredInRegisters { get; }
/// <summary>
/// State if the type is <see cref="IntPtr" />.
/// </summary>
public static bool IsIntPtr { get; }
/// <summary>
/// The real type.
/// </summary>
public static Type RealType { get; }
/// <summary>
/// The size of the type.
/// </summary>
public static int Size { get; }
/// <summary>
/// The typecode of the type.
/// </summary>
public static TypeCode TypeCode { get; }
private static bool Test()
{
return IsIntPtr ||
TypeCode == TypeCode.Int64 ||
TypeCode == TypeCode.UInt64 ||
TypeCode == TypeCode.Boolean ||
TypeCode == TypeCode.Byte ||
TypeCode == TypeCode.Char ||
TypeCode == TypeCode.Int16 ||
TypeCode == TypeCode.Int32 ||
TypeCode == TypeCode.Int64 ||
TypeCode == TypeCode.SByte ||
TypeCode == TypeCode.Single ||
TypeCode == TypeCode.UInt16 ||
TypeCode == TypeCode.UInt32;
}
/// <summary>
/// Marshals a managed object to an array of bytes.
/// </summary>
/// <param name="obj">The object to marshal.</param>
/// <returns>A array of bytes corresponding to the managed object.</returns>
public static byte[] ObjectToByteArray(T obj)
{
// We'll tried to avoid marshalling as it really slows the process
// First, check if the type can be converted without marhsalling
switch (TypeCode)
{
case TypeCode.Object:
if (IsIntPtr)
switch (Size)
{
case 4:
return BitConverter.GetBytes(((IntPtr) (object) obj).ToInt32());
case 8:
return BitConverter.GetBytes(((IntPtr) (object) obj).ToInt64());
}
break;
case TypeCode.Boolean:
return BitConverter.GetBytes((bool) (object) obj);
case TypeCode.Char:
return Encoding.UTF8.GetBytes(new[] {(char) (object) obj});
case TypeCode.Double:
return BitConverter.GetBytes((double) (object) obj);
case TypeCode.Int16:
return BitConverter.GetBytes((short) (object) obj);
case TypeCode.Int32:
return BitConverter.GetBytes((int) (object) obj);
case TypeCode.Int64:
return BitConverter.GetBytes((long) (object) obj);
case TypeCode.Single:
return BitConverter.GetBytes((float) (object) obj);
case TypeCode.String:
throw new InvalidCastException("This method doesn't support string conversion.");
case TypeCode.UInt16:
return BitConverter.GetBytes((ushort) (object) obj);
case TypeCode.UInt32:
return BitConverter.GetBytes((uint) (object) obj);
case TypeCode.UInt64:
return BitConverter.GetBytes((ulong) (object) obj);
}
// Check if it's not a common type
// Allocate a block of unmanaged memory
using (var unmanaged = new LocalUnmanagedMemory(Size))
{
// Write the object inside the unmanaged memory
unmanaged.Write(obj);
// Return the content of the block of unmanaged memory
return unmanaged.Read();
}
}
/// <summary>
/// Marshals an array of byte to a managed object.
/// </summary>
/// <param name="byteArray">The array of bytes corresponding to a managed object.</param>
/// <returns>A managed object.</returns>
public static T ByteArrayToObject(byte[] byteArray)
{
// We'll tried to avoid marshalling as it really slows the process
// First, check if the type can be converted without marhsalling
switch (TypeCode)
{
case TypeCode.Object:
if (IsIntPtr)
switch (byteArray.Length)
{
case 1:
return
(T)
(object)
new IntPtr(BitConverter.ToInt32(new byte[] {byteArray[0], 0x0, 0x0, 0x0}, 0));
case 2:
return
(T)
(object)
new IntPtr(
BitConverter.ToInt32(new byte[] {byteArray[0], byteArray[1], 0x0, 0x0},
0));
case 4:
return (T) (object) new IntPtr(BitConverter.ToInt32(byteArray, 0));
case 8:
return (T) (object) new IntPtr(BitConverter.ToInt64(byteArray, 0));
}
break;
case TypeCode.Boolean:
return (T) (object) BitConverter.ToBoolean(byteArray, 0);
case TypeCode.Byte:
return (T) (object) byteArray[0];
case TypeCode.Char:
return (T) (object) Encoding.UTF8.GetChars(byteArray)[0]; //BitConverter.ToChar(byteArray, 0);
case TypeCode.Double:
return (T) (object) BitConverter.ToDouble(byteArray, 0);
case TypeCode.Int16:
return (T) (object) BitConverter.ToInt16(byteArray, 0);
case TypeCode.Int32:
return (T) (object) BitConverter.ToInt32(byteArray, 0);
case TypeCode.Int64:
return (T) (object) BitConverter.ToInt64(byteArray, 0);
case TypeCode.Single:
return (T) (object) BitConverter.ToSingle(byteArray, 0);
case TypeCode.String:
throw new InvalidCastException("This method doesn't support string conversion.");
case TypeCode.UInt16:
return (T) (object) BitConverter.ToUInt16(byteArray, 0);
case TypeCode.UInt32:
return (T) (object) BitConverter.ToUInt32(byteArray, 0);
case TypeCode.UInt64:
return (T) (object) BitConverter.ToUInt64(byteArray, 0);
}
// Check if it's not a common type
// Allocate a block of unmanaged memory
using (var unmanaged = new LocalUnmanagedMemory(Size))
{
// Write the array of bytes inside the unmanaged memory
unmanaged.Write(byteArray);
// Return a managed object created from the block of unmanaged memory
return unmanaged.Read<T>();
}
}
/// <summary>
/// Converts a pointer to a given type. This function converts the value of the pointer or the pointed value,
/// according if the data type is primitive or reference.
/// </summary>
/// <param name="memorySharp">The concerned process.</param>
/// <param name="pointer">The pointer to convert.</param>
/// <returns>The return value is the pointer converted to the given data type.</returns>
public static T PtrToObject(IProcess memorySharp, IntPtr pointer)
{
return ByteArrayToObject(CanBeStoredInRegisters
? BitConverter.GetBytes(pointer.ToInt64())
: memorySharp.Memory.Read<byte>(pointer, Size));
}
}
}