mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 20:56:02 +00:00
* Update TestService * Update WebListener * Update Controllers * Update ExpTest * Update MyApp * Update Logic * Update Logic * Update MyApp * Update Microsoft.Management.Infrastructure.CimCmdlets * Update Microsoft.PowerShell.Commands.Diagnostics * Update Microsoft.PowerShell.ScheduledJob * Update Microsoft.WSMan.Management * Update Microsoft.WSMan.Runtime * Update ResGen * Update TypeCatalogGen * Update commands * Update Eventing * Update Reader * Update utility * Update ShowCommand * Update trace * Update WebCmdlet * Update Common * Update CoreCLR * Update common * Update format-object * Update format-wide * Update out-file * Update out-printer * Update out-string * Update OutGridView * Update LocalAccounts * Update Commands * Update security * Update CoreCLR * Update DscSupport * Update engine * Update help * Update logging * Update namespaces * Update security * Update utils * Update config * Update perfCounters * Update tracing * Update cmdletization * Update other * Update cim * Update xml * Update CoreCLR * Update common * Update DefaultFormatters * Update out-console * Update out-textInterface * Update DisplayDatabase * Update Utilities * Update COM * Update ComInterop * Update CommandCompletion * Update debugger * Update ExperimentalFeature * Update hostifaces * Update interpreter * Update lang * Update Modules * Update parser * Update runtime * Update client * Update commands * Update common * Update fanin * Update server * Update WireDataFormat * Update Binding * Update Operations * Update interface * Update cmdletization * Update cim * Update management * Update WindowsTaskbarJumpList * Update msh
132 lines
4.3 KiB
C#
132 lines
4.3 KiB
C#
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
|
|
// TODO/FIXME: move this to Microsoft.PowerShell.Cim namespace (and move in source depot folder as well)
|
|
|
|
namespace Microsoft.PowerShell.Cmdletization.Cim
|
|
{
|
|
using System.Management.Automation;
|
|
using System.Text;
|
|
|
|
/// <summary>
|
|
/// Translates a <see cref="WildcardPattern"/> into a like-operand for WQL.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Documentation on MSDN (http://msdn.microsoft.com/library/aa392263(VS.85).aspx) is
|
|
/// 1) rather slim / incomplete
|
|
/// 2) sometimes incorrect (i.e. says that '=' is used for character ranges, when it should have said '-')
|
|
///
|
|
/// The code below is therefore mainly based on reverse engineering of admin\wmi\wbem\winmgmt\wbecomn\like.cpp
|
|
/// </remarks>
|
|
internal class WildcardPatternToCimQueryParser : WildcardPatternParser
|
|
{
|
|
private readonly StringBuilder _result = new StringBuilder();
|
|
private bool _needClientSideFiltering;
|
|
|
|
protected override void AppendLiteralCharacter(char c)
|
|
{
|
|
switch (c)
|
|
{
|
|
case '%':
|
|
case '_':
|
|
case '[': // no need to escape ']' character
|
|
this.BeginBracketExpression();
|
|
this.AppendLiteralCharacterToBracketExpression(c);
|
|
this.EndBracketExpression();
|
|
break;
|
|
default:
|
|
_result.Append(c);
|
|
break;
|
|
}
|
|
}
|
|
|
|
protected override void AppendAsterix()
|
|
{
|
|
_result.Append('%');
|
|
}
|
|
|
|
protected override void AppendQuestionMark()
|
|
{
|
|
_result.Append('_');
|
|
}
|
|
|
|
protected override void BeginBracketExpression()
|
|
{
|
|
_result.Append('[');
|
|
}
|
|
|
|
protected override void AppendLiteralCharacterToBracketExpression(char c)
|
|
{
|
|
switch (c)
|
|
{
|
|
case '^':
|
|
case ']':
|
|
case '-':
|
|
case '\\':
|
|
this.AppendCharacterRangeToBracketExpression(c, c);
|
|
break;
|
|
default:
|
|
_result.Append(c);
|
|
break;
|
|
}
|
|
}
|
|
|
|
protected override void AppendCharacterRangeToBracketExpression(char startOfCharacterRange, char endOfCharacterRange)
|
|
{
|
|
// 90 = Z
|
|
// 91 = [
|
|
// 92 = \
|
|
// 93 = ]
|
|
// 94 = ^
|
|
// 95 = _
|
|
if ((91 <= startOfCharacterRange) && (startOfCharacterRange <= 94))
|
|
{
|
|
startOfCharacterRange = (char)90;
|
|
_needClientSideFiltering = true;
|
|
}
|
|
|
|
if ((91 <= endOfCharacterRange) && (endOfCharacterRange <= 94))
|
|
{
|
|
endOfCharacterRange = (char)95;
|
|
_needClientSideFiltering = true;
|
|
}
|
|
|
|
// 44 = ,
|
|
// 45 = -
|
|
// 46 = .
|
|
if (startOfCharacterRange == 45)
|
|
{
|
|
startOfCharacterRange = (char)44;
|
|
_needClientSideFiltering = true;
|
|
}
|
|
|
|
if (endOfCharacterRange == 45)
|
|
{
|
|
endOfCharacterRange = (char)46;
|
|
_needClientSideFiltering = true;
|
|
}
|
|
|
|
_result.Append(startOfCharacterRange);
|
|
_result.Append('-');
|
|
_result.Append(endOfCharacterRange);
|
|
}
|
|
|
|
protected override void EndBracketExpression()
|
|
{
|
|
_result.Append(']');
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts <paramref name="wildcardPattern"/> into a value of a right-hand-side operand of LIKE operator of a WQL query.
|
|
/// Return value still has to be string-escaped (i.e. by doubling '\'' character), before embedding it into a query.
|
|
/// </summary>
|
|
internal static string Parse(WildcardPattern wildcardPattern, out bool needsClientSideFiltering)
|
|
{
|
|
var parser = new WildcardPatternToCimQueryParser();
|
|
WildcardPatternParser.Parse(wildcardPattern, parser);
|
|
needsClientSideFiltering = parser._needClientSideFiltering;
|
|
return parser._result.ToString();
|
|
}
|
|
}
|
|
}
|