Skip to content

Commit 06cb51a

Browse files
committed
Format expression using current context.
1 parent b508b14 commit 06cb51a

2 files changed

Lines changed: 54 additions & 73 deletions

File tree

NtApiDotNet/Ndr/IdlNdrFormatterInternal.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ private void ApplyCorrelation<T>(string name, NdrFormatterContextEntry<T> entry,
302302
{
303303
if (!descriptor.Expression.IsValid)
304304
return;
305-
expression = descriptor.Expression.ToString();
305+
expression = descriptor.Expression.ToString(this,
306+
ofs => GetLinkedEntry(entry, entries, descriptor.CorrelationType, ofs)?.Name);
306307
}
307308
else
308309
{

NtApiDotNet/Ndr/NdrExpression.cs

Lines changed: 52 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -76,34 +76,18 @@ public enum NdrExpressionOperator
7676
[Serializable]
7777
public class NdrExpression
7878
{
79-
#region Private Members
80-
81-
private static bool IsValidType(NdrExpressionType type)
82-
{
83-
switch (type)
84-
{
85-
case NdrExpressionType.FC_EXPR_OPER:
86-
case NdrExpressionType.FC_EXPR_CONST32:
87-
case NdrExpressionType.FC_EXPR_CONST64:
88-
case NdrExpressionType.FC_EXPR_VAR:
89-
return true;
90-
}
91-
return false;
92-
}
93-
79+
#region Public Members
9480
/// <summary>
9581
/// Overridden ToString method.
9682
/// </summary>
9783
/// <returns>The expression as a string.</returns>
98-
public override string ToString()
84+
public override sealed string ToString()
9985
{
100-
return string.Empty;
86+
return ToString(null, null);
10187
}
102-
10388
#endregion
10489

10590
#region Constructors
106-
10791
internal NdrExpression(NdrExpressionType type)
10892
{
10993
Type = type;
@@ -112,7 +96,6 @@ internal NdrExpression(NdrExpressionType type)
11296
internal NdrExpression() : this(0)
11397
{
11498
}
115-
11699
#endregion
117100

118101
#region Public Properties
@@ -165,6 +148,11 @@ internal static NdrExpression Read(NdrParseContext context, int index)
165148
BinaryReader reader = context.Reader.GetReader(context.ExprDesc.pFormatExpr + expr_ofs);
166149
return ReadExpression(reader);
167150
}
151+
152+
internal virtual string ToString(INdrFormatterInternal context, Func<int, string> get_variable_name)
153+
{
154+
return string.Empty;
155+
}
168156
#endregion
169157
}
170158

@@ -254,82 +242,80 @@ internal NdrOperatorExpression(BinaryReader reader)
254242
#endregion
255243

256244
#region Private Members
257-
private string FormatUnaryOperator(string op)
245+
private string FormatUnaryOperator(string op, INdrFormatterInternal context, Func<int, string> get_variable_name)
258246
{
259-
return $"{op}{Arguments[0]}";
247+
return $"{op}{Arguments[0].ToString(context, get_variable_name)}";
260248
}
261249

262-
private string FormatBinaryOperator(string op)
250+
private string FormatBinaryOperator(string op, INdrFormatterInternal context, Func<int, string> get_variable_name)
263251
{
264-
return $"({Arguments[0]} {op} {Arguments[1]})";
252+
return $"({Arguments[0].ToString(context, get_variable_name)} {op} {Arguments[1].ToString(context, get_variable_name)})";
265253
}
266254
#endregion
267255

268-
#region Public Methods
269-
/// <summary>
270-
/// Overridden ToString method.
271-
/// </summary>
272-
/// <returns>The expression as a string.</returns>
273-
public override string ToString()
256+
#region Internal Members
257+
internal override string ToString(INdrFormatterInternal context, Func<int, string> get_variable_name)
274258
{
275259
switch (Operator)
276260
{
277261
case NdrExpressionOperator.OP_UNARY_INDIRECTION:
278-
return FormatUnaryOperator("*");
262+
return FormatUnaryOperator("*", context, get_variable_name);
279263
case NdrExpressionOperator.OP_UNARY_MINUS:
280-
return FormatUnaryOperator("-");
264+
return FormatUnaryOperator("-", context, get_variable_name);
281265
case NdrExpressionOperator.OP_UNARY_PLUS:
282-
return FormatUnaryOperator("+");
266+
return FormatUnaryOperator("+", context, get_variable_name);
283267
case NdrExpressionOperator.OP_UNARY_CAST:
284-
return FormatUnaryOperator($"({Format})");
268+
return FormatUnaryOperator($"({context?.SimpleTypeToName(Format) ?? Format.ToString()})", context, get_variable_name);
285269
case NdrExpressionOperator.OP_UNARY_COMPLEMENT:
286-
return FormatUnaryOperator("~");
270+
return FormatUnaryOperator("~", context, get_variable_name);
287271
case NdrExpressionOperator.OP_UNARY_NOT:
288-
return FormatUnaryOperator("!");
272+
return FormatUnaryOperator("!", context, get_variable_name);
289273
case NdrExpressionOperator.OP_UNARY_SIZEOF:
290-
return FormatUnaryOperator("sizeof ");
274+
return FormatUnaryOperator("sizeof ", context, get_variable_name);
291275
case NdrExpressionOperator.OP_UNARY_ALIGNOF:
292-
return FormatUnaryOperator("alignof ");
276+
return FormatUnaryOperator("alignof ", context, get_variable_name);
293277
case NdrExpressionOperator.OP_UNARY_AND:
294-
return FormatUnaryOperator(string.Empty);
278+
return FormatUnaryOperator(string.Empty, context, get_variable_name);
295279
case NdrExpressionOperator.OP_MINUS:
296-
return FormatBinaryOperator("-");
280+
return FormatBinaryOperator("-", context, get_variable_name);
297281
case NdrExpressionOperator.OP_MOD:
298-
return FormatBinaryOperator("%");
282+
return FormatBinaryOperator("%", context, get_variable_name);
299283
case NdrExpressionOperator.OP_OR:
300-
return FormatBinaryOperator("|");
284+
return FormatBinaryOperator("|", context, get_variable_name);
301285
case NdrExpressionOperator.OP_PLUS:
302-
return FormatBinaryOperator("+");
286+
return FormatBinaryOperator("+", context, get_variable_name);
303287
case NdrExpressionOperator.OP_SLASH:
304-
return FormatBinaryOperator("/");
288+
return FormatBinaryOperator("/", context, get_variable_name);
305289
case NdrExpressionOperator.OP_STAR:
306-
return FormatBinaryOperator("*");
290+
return FormatBinaryOperator("*", context, get_variable_name);
307291
case NdrExpressionOperator.OP_XOR:
308-
return FormatBinaryOperator("^");
292+
return FormatBinaryOperator("^", context, get_variable_name);
309293
case NdrExpressionOperator.OP_AND:
310-
return FormatBinaryOperator("&");
294+
return FormatBinaryOperator("&", context, get_variable_name);
311295
case NdrExpressionOperator.OP_LEFT_SHIFT:
312-
return FormatBinaryOperator("<<");
296+
return FormatBinaryOperator("<<", context, get_variable_name);
313297
case NdrExpressionOperator.OP_RIGHT_SHIFT:
314-
return FormatBinaryOperator(">>");
298+
return FormatBinaryOperator(">>", context, get_variable_name);
315299
case NdrExpressionOperator.OP_EQUAL:
316-
return FormatBinaryOperator("==");
300+
return FormatBinaryOperator("==", context, get_variable_name);
317301
case NdrExpressionOperator.OP_GREATER:
318-
return FormatBinaryOperator(">");
302+
return FormatBinaryOperator(">", context, get_variable_name);
319303
case NdrExpressionOperator.OP_GREATER_EQUAL:
320-
return FormatBinaryOperator(">=");
304+
return FormatBinaryOperator(">=", context, get_variable_name);
321305
case NdrExpressionOperator.OP_LESS:
322-
return FormatBinaryOperator("<");
306+
return FormatBinaryOperator("<", context, get_variable_name);
323307
case NdrExpressionOperator.OP_LESS_EQUAL:
324-
return FormatBinaryOperator("<=");
308+
return FormatBinaryOperator("<=", context, get_variable_name);
325309
case NdrExpressionOperator.OP_LOGICAL_AND:
326-
return FormatBinaryOperator("&&");
310+
return FormatBinaryOperator("&&", context, get_variable_name);
327311
case NdrExpressionOperator.OP_LOGICAL_OR:
328-
return FormatBinaryOperator("||");
312+
return FormatBinaryOperator("||", context, get_variable_name);
329313
case NdrExpressionOperator.OP_NOT_EQUAL:
330-
return FormatBinaryOperator("!=");
314+
return FormatBinaryOperator("!=", context, get_variable_name);
331315
case NdrExpressionOperator.OP_EXPRESSION:
332-
return $"({Arguments[2]} ? {Arguments[0]} : {Arguments[1]})";
316+
return $"({Arguments[2].ToString(context, get_variable_name)} ? " +
317+
$"{Arguments[0].ToString(context, get_variable_name)} " +
318+
$": {Arguments[1].ToString(context, get_variable_name)})";
333319
default:
334320
break;
335321
}
@@ -368,14 +354,13 @@ internal NdrVariableExpression(BinaryReader reader)
368354

369355
#endregion
370356

371-
#region Public Methods
372-
/// <summary>
373-
/// Overridden ToString method.
374-
/// </summary>
375-
/// <returns>The expression as a string.</returns>
376-
public override string ToString()
357+
#region Internal Members
358+
internal override string ToString(INdrFormatterInternal context, Func<int, string> get_variable_name)
377359
{
378-
return $"VAR{{{Offset}}}";
360+
string name = get_variable_name?.Invoke(Offset);
361+
if (string.IsNullOrEmpty(name))
362+
name = $"VAR{{{Offset}}}";
363+
return name;
379364
}
380365
#endregion
381366
}
@@ -404,7 +389,6 @@ public sealed class NdrConstantExpression : NdrExpression
404389
#endregion
405390

406391
#region Constructors
407-
408392
internal NdrConstantExpression(NdrExpressionType type, BinaryReader reader)
409393
: base(type)
410394
{
@@ -422,12 +406,8 @@ internal NdrConstantExpression(NdrExpressionType type, BinaryReader reader)
422406
}
423407
#endregion
424408

425-
#region Public Methods
426-
/// <summary>
427-
/// Overridden ToString method.
428-
/// </summary>
429-
/// <returns>The expression as a string.</returns>
430-
public override string ToString()
409+
#region Internal Members
410+
internal override string ToString(INdrFormatterInternal context, Func<int, string> get_variable_name)
431411
{
432412
return Value.ToString();
433413
}

0 commit comments

Comments
 (0)