-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathProgram.cs
More file actions
316 lines (300 loc) · 12.5 KB
/
Program.cs
File metadata and controls
316 lines (300 loc) · 12.5 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
namespace Midl2Bytes
{
//https://github.com/microsoft/WindowsProtocolTestSuites/blob/main/ProtoSDK/MS-RPCE/Stub/RpceStubHelper.cs
internal class Program
{
private static void Main(string[] args)
{
string[] content;
if (args.Length < 1)
{
Console.WriteLine("Midl2Bytes.exe <path to midl output .c file>");
Console.WriteLine("Midl2Bytes.exe .\\x64\\ms-rprn_c.c");
return;
}
if (File.Exists(args[0]))
{
content = File.ReadAllLines(args[0]);
}
else
{
Console.WriteLine("Could not find file");
Console.WriteLine("Midl2Bytes <path to midl output>");
return;
}
bool c = false;
string architecture = "64";
string typeFormatString = "";
string procFormatString = "";
foreach (string line in content)
{
if (line.Contains("_MIDL_TypeFormatString ="))
{
c = true;
}
if (c)
{
typeFormatString += $"{line}\n";
}
if (line.Contains(" };"))
{
c = false;
}
if (line.Contains(" x86 Stack size/offset "))
{
architecture = "86";
}
}
foreach (string line in content)
{
if (line.Contains("_MIDL_ProcFormatString ="))
{
c = true;
}
if (c)
{
procFormatString += $"{line}\n";
}
if (line.Contains(" };"))
{
c = false;
}
if (line.Contains(" x86 Stack size/offset "))
{
architecture = "86";
}
}
try
{
//Console.WriteLine(typeFormatString);
byte[] typeFormatStringBytes = RpceStubHelper.CreateFormatStringByteArray(typeFormatString);
Console.WriteLine($"private static byte[] MIDL_TypeFormatStringx{architecture} = new byte[] {{" + PrintHexBytes(typeFormatStringBytes).TrimEnd(new char[] { ' ', ',' }) + "};");
//Console.WriteLine(typeFormatString);
byte[] ProcFormatStringBytes = RpceStubHelper.CreateFormatStringByteArray(procFormatString);
Console.WriteLine($"private static byte[] MIDL_ProcFormatStringx{architecture} = new byte[] {{" + PrintHexBytes(ProcFormatStringBytes).TrimEnd(new char[] { ' ', ',' }) + "};");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public static string PrintHexBytes(byte[] byteArray)
{
var res = new StringBuilder(byteArray.Length * 3);
for (var i = 0; i < byteArray.Length; i++)
res.AppendFormat(NumberFormatInfo.InvariantInfo, "0x{0:x2}, ", byteArray[i]);
return res.ToString();
}
}
/// <summary>
/// Helper class for encode/decode RPC stub.<para/>
/// </summary>
public static class RpceStubHelper
{
/// <summary>
/// Get a byte array from the format string generated by midl.exe.<para/>
/// Example: Regarding following code generated by midl.exe<para/>
/// static const foo_MIDL_TYPE_FORMAT_STRING foo__MIDL_TypeFormatString =<para/>
/// {<para/>
/// 0,<para/>
/// {<para/>
/// NdrFcShort( 0x0 ), /* 0 */<para/>
/// /* 2 */<para/>
/// 0x11, 0x0, /* FC_RP */<para/>
/// /* 4 */<para/>
/// NdrFcShort( 0x10 ), /* Offset= 16 (20) */<para/>
/// /* 6 */<para/>
/// 0x1c, /* FC_CVARRAY */<para/>
/// ...<para/>
/// }<para/>
/// };<para/>
/// The formatString parameter should be a substring
/// start from "NdrFcShort(0x0)" and end at first"}".
/// </summary>
/// <param name="formatString">
/// A format string generated by midl.exe. See method help for details.
/// </param>
/// <returns>A byte array of the format string passed to the method.</returns>
public static byte[] CreateFormatStringByteArray(string formatString)
{
int index = 0;
List<byte> formatStringBuffer = new List<byte>();
Regex regex = new Regex(@"
0x (?<byte>[0-9a-fA-F]{1,2}) \s* ,
|
NdrFcShort \( \s* 0x (?<short>[0-9a-fA-F]{1,4}) \s* \)
|
NdrFcLong \( \s* 0x (?<long>[0-9a-fA-F]+) \s* \)
|
\n \/\* \s* (?<offset>\d+)* \s* \*\/
|
\/\* .* \*\/
",
RegexOptions.IgnorePatternWhitespace);
while (true)
{
Match match = regex.Match(formatString, index);
if (!match.Success)
{
break;
}
else if (match.Groups["byte"].Success)
{
byte value = Convert.ToByte(match.Groups["byte"].Value, 16);
formatStringBuffer.Add(value);
}
else if (match.Groups["short"].Success)
{
ushort value = Convert.ToUInt16(match.Groups["short"].Value, 16);
formatStringBuffer.Add((byte)(value & 0xff));
formatStringBuffer.Add((byte)(value >> 8));
}
else if (match.Groups["long"].Success)
{
uint value = Convert.ToUInt32(match.Groups["long"].Value, 16);
formatStringBuffer.Add((byte)(value & 0xff));
formatStringBuffer.Add((byte)((value >> 8) & 0xff));
formatStringBuffer.Add((byte)((value >> 16) & 0xff));
formatStringBuffer.Add((byte)(value >> 24));
}
else if (match.Groups["offset"].Success)
{
int offset = Convert.ToInt32(match.Groups["offset"].Value);
if (formatStringBuffer.Count != offset)
throw new InvalidOperationException();
}
index = match.Index + match.Length;
}
return formatStringBuffer.ToArray();
}
/// <summary>
/// Parse procedure header a from byte array to structure.
/// </summary>
/// <param name="procFormatString">Proc format string.</param>
/// <param name="offset">Offset of a procedure in procFormatString.</param>
/// <returns>The RpceProcedureHeaderDescriptor.</returns>
public static RpceProcedureHeaderDescriptor ParseProcedureHeaderDescriptor(
byte[] procFormatString,
ref int offset)
{
//handle_type<1>
//Oi_flags<1>
//[rpc_flags<4>]
//proc_num<2>
//stack_size<2>
//[explicit_handle_description<>]
//constant_client_buffer_size<2>
//constant_server_buffer_size<2>
//INTERPRETER_OPT_FLAGS<1>
//number_of_params<1>
//extension_size<1>
//[extension]
const byte FC_BIND_PRIMITIVE = 0x32;
const int PRIMITIVE_HANDLE_LENGTH = 4;
const int OTHER_HANDLE_LENGTH = 6;
const byte OI_HAS_RPCFLAGS = 0x08;
const int EXTENSION_SIZE = 8;
RpceProcedureHeaderDescriptor procHeaderDesc = new RpceProcedureHeaderDescriptor();
procHeaderDesc.HandleType = procFormatString[offset];
offset += Marshal.SizeOf(procHeaderDesc.HandleType);
procHeaderDesc.OiFlags = procFormatString[offset];
offset += Marshal.SizeOf(procHeaderDesc.OiFlags);
if ((procHeaderDesc.OiFlags & OI_HAS_RPCFLAGS) != 0)
{
procHeaderDesc.RpcFlags = BitConverter.ToUInt32(procFormatString, offset);
offset += Marshal.SizeOf(procHeaderDesc.RpcFlags.Value);
}
procHeaderDesc.ProcNum = BitConverter.ToUInt16(procFormatString, offset);
offset += Marshal.SizeOf(procHeaderDesc.ProcNum);
procHeaderDesc.StackSize = BitConverter.ToUInt16(procFormatString, offset);
offset += Marshal.SizeOf(procHeaderDesc.StackSize);
if (procHeaderDesc.HandleType == 0) // explicit handle
{
int length = (procFormatString[offset] == FC_BIND_PRIMITIVE)
? PRIMITIVE_HANDLE_LENGTH
: OTHER_HANDLE_LENGTH;
procHeaderDesc.ExplicitHandleDescription = ArrayUtility.SubArray(procFormatString, offset, length);
offset += length;
}
procHeaderDesc.ClientBufferSize = BitConverter.ToUInt16(procFormatString, offset);
offset += Marshal.SizeOf(procHeaderDesc.ClientBufferSize);
procHeaderDesc.ServerBufferSize = BitConverter.ToUInt16(procFormatString, offset);
offset += Marshal.SizeOf(procHeaderDesc.ServerBufferSize);
procHeaderDesc.InterpreterOptFlags = procFormatString[offset];
offset += Marshal.SizeOf(procHeaderDesc.InterpreterOptFlags);
procHeaderDesc.NumberOfParams = procFormatString[offset];
offset += Marshal.SizeOf(procHeaderDesc.NumberOfParams);
byte extensionSize = procFormatString[offset];
if (extensionSize >= EXTENSION_SIZE)
{
730F
procHeaderDesc.ExtensionSize = extensionSize;
offset += Marshal.SizeOf(procHeaderDesc.ExtensionSize.Value);
procHeaderDesc.ExtensionFlags2 = (RpceInterpreterOptFlags2)procFormatString[offset];
offset += Marshal.SizeOf((byte)procHeaderDesc.ExtensionFlags2);
procHeaderDesc.ExtensionClientCorrHint = BitConverter.ToUInt16(procFormatString, offset);
offset += Marshal.SizeOf(procHeaderDesc.ExtensionClientCorrHint.Value);
procHeaderDesc.ExtensionServerCorrHint = BitConverter.ToUInt16(procFormatString, offset);
offset += Marshal.SizeOf(procHeaderDesc.ExtensionServerCorrHint.Value);
procHeaderDesc.ExtensionNotifyIndex = BitConverter.ToUInt16(procFormatString, offset);
offset += Marshal.SizeOf(procHeaderDesc.ExtensionNotifyIndex.Value);
offset += extensionSize - EXTENSION_SIZE;
}
else
{
offset += extensionSize;
}
return procHeaderDesc;
}
public static class ArrayUtility
{
/// <summary>
/// Compares two arrays
/// </summary>
/// <typeparam name="T">
/// Type of array, must implement IComparable<T> interface
/// </typeparam>
/// <param name="array1">The first array</param>
/// <param name="array2">The second array</param>
/// <returns>True if the two arrays are equal, false otherwise</returns>
public static bool CompareArrays<T>(T[] array1, T[] array2) where T : IComparable<T>
{
// Reference equal
if (array1 == array2)
{
return true;
}
// one of the arrays is null
if (array1 == null || array2 == null)
{
return false;
}
// The two arrays have different size
if (array1.Length != array2.Length)
{
return false;
}
for (int i = 0; i < array1.Length; i++)
{
if (array1[i].CompareTo(array2[i]) != 0)
{
return false;
}
}
return true;
}
public static T[] SubArray<T>(T[] array, int startIndex, int length)
{
T[] subArray = new T[length];
Array.Copy(array, startIndex, subArray, 0, length);
return subArray;
}
}
}
}