-
Notifications
You must be signed in to change notification settings - Fork 222
/
KnownAssembly.Predicates.cs
74 lines (58 loc) · 3.32 KB
/
KnownAssembly.Predicates.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
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2023 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace SonarAnalyzer.Helpers;
public sealed partial class KnownAssembly
{
private const StringComparison AssemblyNameComparission = StringComparison.OrdinalIgnoreCase;
internal static class Predicates
{
internal static Func<AssemblyIdentity, bool> NameIs(string name) =>
x => x.Name.Equals(name, AssemblyNameComparission);
internal static Func<AssemblyIdentity, bool> StartsWith(string name) =>
x => x.Name.StartsWith(name, AssemblyNameComparission);
internal static Func<AssemblyIdentity, bool> EndsWith(string name) =>
x => x.Name.EndsWith(name, AssemblyNameComparission);
internal static Func<AssemblyIdentity, bool> Contains(string name) =>
x => x.Name.IndexOf(name, 0, AssemblyNameComparission) >= 0;
internal static Func<AssemblyIdentity, bool> VersionLowerThen(string version) =>
VersionLowerThen(Version.Parse(version));
internal static Func<AssemblyIdentity, bool> VersionLowerThen(Version version) =>
x => x.Version < version;
internal static Func<AssemblyIdentity, bool> VersionGreaterOrEqual(string version) =>
VersionGreaterOrEqual(Version.Parse(version));
internal static Func<AssemblyIdentity, bool> VersionGreaterOrEqual(Version version) =>
x => x.Version >= version;
internal static Func<AssemblyIdentity, bool> VersionBetween(string from, string to) =>
VersionBetween(Version.Parse(from), Version.Parse(to));
internal static Func<AssemblyIdentity, bool> VersionBetween(Version from, Version to) =>
x => x.Version >= from && x.Version <= to;
internal static Func<AssemblyIdentity, bool> OptionalPublicKeyTokenIs(string key) =>
x => !x.HasPublicKey || PublicKeyEqualHex(x, key);
internal static Func<AssemblyIdentity, bool> PublicKeyTokenIs(string key) =>
x => x.HasPublicKey && PublicKeyEqualHex(x, key);
private static bool PublicKeyEqualHex(AssemblyIdentity identity, string hexString)
{
var normalizedHexString = hexString.Replace("-", string.Empty);
return ArraysEqual(identity.PublicKeyToken.ToArray(), normalizedHexString) || ArraysEqual(identity.PublicKey.ToArray(), normalizedHexString);
static bool ArraysEqual(byte[] key, string hexString) =>
BitConverter.ToString(key).Replace("-", string.Empty).Equals(hexString, StringComparison.OrdinalIgnoreCase);
}
}
}