Skip to content

Commit 021d7b2

Browse files
committed
Cleanup code
1 parent 19ae8d9 commit 021d7b2

33 files changed

+1921
-1642
lines changed

Windows Build Identifier/Comparer.cs

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,70 @@ public static void CompareBuilds(string xml1, string xml2)
1616
FileItem[] build2 = Deserialize(xml2);
1717

1818
Console.WriteLine("Getting paths 1");
19-
var build1Paths = build1.Select(x => Sanitize(x.Location)).Where(x => !ExcludedFromChecks(x)).ToHashSet();
19+
HashSet<string> build1Paths = build1.Select(x => Sanitize(x.Location)).Where(x => !ExcludedFromChecks(x)).ToHashSet();
2020
Console.WriteLine("Getting paths 2");
21-
var build2Paths = build2.Select(x => Sanitize(x.Location)).Where(x => !ExcludedFromChecks(x)).ToHashSet();
21+
HashSet<string> build2Paths = build2.Select(x => Sanitize(x.Location)).Where(x => !ExcludedFromChecks(x)).ToHashSet();
2222

2323
Console.WriteLine("Getting common paths");
24-
var commonPaths = build1Paths.Intersect(build2Paths).ToHashSet();
24+
HashSet<string> commonPaths = build1Paths.Intersect(build2Paths).ToHashSet();
2525

2626
Console.WriteLine("Getting unique paths 1");
27-
var uniqueBuild1 = build1Paths.Where(x => !commonPaths.Contains(x)).ToHashSet();
27+
HashSet<string> uniqueBuild1 = build1Paths.Where(x => !commonPaths.Contains(x)).ToHashSet();
2828
Console.WriteLine("Getting unique paths 2");
29-
var uniqueBuild2 = build2Paths.Where(x => !commonPaths.Contains(x)).ToHashSet();
29+
HashSet<string> uniqueBuild2 = build2Paths.Where(x => !commonPaths.Contains(x)).ToHashSet();
3030

3131
Console.WriteLine("Getting common resources");
32-
var commonResources = commonPaths.Where(x => x.Contains(@"\.rsrc\")).ToHashSet();
32+
HashSet<string> commonResources = commonPaths.Where(x => x.Contains(@"\.rsrc\")).ToHashSet();
3333

34-
Console.WriteLine($"{commonPaths.Count()} common paths");
35-
Console.WriteLine($"{commonResources.Count()} common resources");
36-
Console.WriteLine($"{uniqueBuild1.Count()} unique 1");
37-
Console.WriteLine($"{uniqueBuild2.Count()} unique 2");
34+
Console.WriteLine($"{commonPaths.Count} common paths");
35+
Console.WriteLine($"{commonResources.Count} common resources");
36+
Console.WriteLine($"{uniqueBuild1.Count} unique 1");
37+
Console.WriteLine($"{uniqueBuild2.Count} unique 2");
3838

3939
Console.WriteLine("Processing common resources");
40-
HashSet<string> modifiedResources = new HashSet<string>();
40+
HashSet<string> modifiedResources = new();
4141

4242
long total = commonResources.Count;
4343
long counter = 0;
4444

45-
foreach (var resource in commonResources)
45+
foreach (string resource in commonResources)
4646
{
4747
Console.Title = $"Processing {counter}/{total}";
48-
var item1 = GetFileItem(build1, resource);
49-
var item2 = GetFileItem(build2, resource);
48+
FileItem item1 = GetFileItem(build1, resource);
49+
FileItem item2 = GetFileItem(build2, resource);
5050

5151
if (item1.Hash != null)
5252
{
53-
if (item1.Hash.SHA1 != item2.Hash.SHA1)
53+
if (item1.Hash.Sha1 != item2.Hash.Sha1)
5454
{
5555
Console.WriteLine("modified");
5656
modifiedResources.Add(item1.Location);
5757
}
5858
}
59+
5960
counter++;
6061
}
6162

6263
Console.WriteLine();
6364
Console.WriteLine("Files unique to build 1");
6465
Console.WriteLine();
65-
foreach (var file in uniqueBuild1)
66+
foreach (string file in uniqueBuild1)
6667
{
6768
Console.WriteLine(file);
6869
}
6970

7071
Console.WriteLine();
7172
Console.WriteLine("Files unique to build 2");
7273
Console.WriteLine();
73-
foreach (var file in uniqueBuild2)
74+
foreach (string file in uniqueBuild2)
7475
{
7576
Console.WriteLine(file);
7677
}
7778

7879
Console.WriteLine();
7980
Console.WriteLine("Modified resources between both builds");
8081
Console.WriteLine();
81-
foreach (var file in modifiedResources)
82+
foreach (string file in modifiedResources)
8283
{
8384
Console.WriteLine(file);
8485
}
@@ -88,77 +89,77 @@ private static string Sanitize(string path1)
8889
{
8990
if (path1.Contains(@"installedrepository\", StringComparison.InvariantCultureIgnoreCase))
9091
{
91-
var fold1t = path1.ToLower().Split(@"installedrepository\")[0];
92-
var fold1 = path1.ToLower().Split(@"installedrepository\")[1];
92+
string fold1T = path1.ToLower().Split(@"installedrepository\")[0];
93+
string fold1 = path1.ToLower().Split(@"installedrepository\")[1];
9394

94-
var number = fold1.Count(x => x == '_');
95+
int number = fold1.Count(x => x == '_');
9596
if (number < 1)
9697
{
9798
return path1;
9899
}
99100

100-
var fsplit1 = fold1.Split(@"\");
101+
string[] fsplit1 = fold1.Split(@"\");
101102

102-
var san1 = string.Join("_", fsplit1[0].Split("_")[0..^1]);
103+
string san1 = string.Join("_", fsplit1[0].Split("_")[..^1]);
103104

104105
if (fsplit1.Length < 2)
105106
{
106107
return san1;
107108
}
108109

109-
var f1 = fold1t + "installedrepository\\" + san1 + "\\" + string.Join("\\", fsplit1[1..^0]);
110+
string f1 = fold1T + "installedrepository\\" + san1 + "\\" + string.Join("\\", fsplit1[1..]);
110111
return f1;
111112
}
112-
else if (path1.Contains(@"build\filerepository\", StringComparison.InvariantCultureIgnoreCase))
113+
114+
if (path1.Contains(@"build\filerepository\", StringComparison.InvariantCultureIgnoreCase))
113115
{
114-
var fold1t = path1.ToLower().Split(@"build\filerepository\")[0];
115-
var fold1 = path1.ToLower().Split(@"build\filerepository\")[1];
116+
string fold1T = path1.ToLower().Split(@"build\filerepository\")[0];
117+
string fold1 = path1.ToLower().Split(@"build\filerepository\")[1];
116118

117-
var number = fold1.Count(x => x == '_');
119+
int number = fold1.Count(x => x == '_');
118120
if (number < 1)
119121
{
120122
return path1;
121123
}
122124

123-
var fsplit1 = fold1.Split(@"\");
125+
string[] fsplit1 = fold1.Split(@"\");
124126

125-
var san1 = string.Join("_", fsplit1[0].Split("_")[0..^1]);
127+
string san1 = string.Join("_", fsplit1[0].Split("_")[..^1]);
126128

127129
if (fsplit1.Length < 2)
128130
{
129131
return san1;
130132
}
131133

132-
var f1 = fold1t + "build\\filerepository\\" + san1 + "\\" + string.Join("\\", fsplit1[1..^0]);
134+
string f1 = fold1T + "build\\filerepository\\" + san1 + "\\" + string.Join("\\", fsplit1[1..]);
133135
return f1;
134136
}
135-
else if (path1.Contains(@"Windows\winsxs\", StringComparison.InvariantCultureIgnoreCase))
137+
138+
if (path1.Contains(@"Windows\winsxs\", StringComparison.InvariantCultureIgnoreCase))
136139
{
137-
var fold1t = path1.ToLower().Split(@"windows\winsxs\")[0];
138-
var fold1 = path1.ToLower().Split(@"windows\winsxs\")[1];
140+
string fold1T = path1.ToLower().Split(@"windows\winsxs\")[0];
141+
string fold1 = path1.ToLower().Split(@"windows\winsxs\")[1];
139142

140-
var number = fold1.Count(x => x == '_');
143+
int number = fold1.Count(x => x == '_');
141144
if (number < 1)
142145
{
143146
return path1;
144147
}
145148

146-
var fsplit1 = fold1.Split(@"\");
149+
string[] fsplit1 = fold1.Split(@"\");
147150

148-
var san1 = string.Join("_", fsplit1[0].Split("_")[0..^1]);
151+
string san1 = string.Join("_", fsplit1[0].Split("_")[..^1]);
149152

150153
if (fsplit1.Length < 2)
151154
{
152155
return san1;
153156
}
154157

155-
var f1 = fold1t + "windows\\winsxs\\" + san1 + "\\" + string.Join("\\", fsplit1[1..^0]);
158+
string f1 = fold1T + "windows\\winsxs\\" + san1 + "\\" + string.Join("\\", fsplit1[1..]);
156159
return f1;
157160
}
158-
else
159-
{
160-
return path1;
161-
}
161+
162+
return path1;
162163
}
163164

164165
private static FileItem GetFileItem(FileItem[] array, string path)
@@ -171,7 +172,9 @@ private static bool ExcludedFromChecks(string path)
171172
string index = @"install.wim\3\";
172173

173174
if (!path.Contains(index, StringComparison.InvariantCultureIgnoreCase))
175+
{
174176
return true;
177+
}
175178

176179
if (path.Contains(@"\.rsrc\"))
177180
{
@@ -186,13 +189,13 @@ private static bool ExcludedFromChecks(string path)
186189

187190
private static FileItem[] Deserialize(string path)
188191
{
189-
XmlSerializer deserializer = new XmlSerializer(typeof(FileItem[]));
192+
XmlSerializer deserializer = new(typeof(FileItem[]));
190193
TextReader reader = new StreamReader(path);
191194
object obj = deserializer.Deserialize(reader);
192-
FileItem[] XmlData = (FileItem[])obj;
195+
FileItem[] xmlData = (FileItem[])obj;
193196
reader.Close();
194197

195-
return XmlData;
198+
return xmlData;
196199
}
197200
}
198-
}
201+
}

Windows Build Identifier/Crc32.cs

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,111 +9,129 @@
99
namespace WindowsBuildIdentifier
1010
{
1111
/// <summary>
12-
/// Implements a 32-bit CRC hash algorithm compatible with Zip etc.
12+
/// Implements a 32-bit CRC hash algorithm compatible with Zip etc.
1313
/// </summary>
1414
/// <remarks>
15-
/// Crc32 should only be used for backward compatibility with older file formats
16-
/// and algorithms. It is not secure enough for new applications.
17-
/// If you need to call multiple times for the same data either use the HashAlgorithm
18-
/// interface or remember that the result of one Compute call needs to be ~ (XOR) before
19-
/// being passed in as the seed for the next Compute call.
15+
/// Crc32 should only be used for backward compatibility with older file formats
16+
/// and algorithms. It is not secure enough for new applications.
17+
/// If you need to call multiple times for the same data either use the HashAlgorithm
18+
/// interface or remember that the result of one Compute call needs to be ~ (XOR) before
19+
/// being passed in as the seed for the next Compute call.
2020
/// </remarks>
2121
public sealed class Crc32 : HashAlgorithm
2222
{
23-
public const UInt32 DefaultPolynomial = 0xedb88320u;
24-
public const UInt32 DefaultSeed = 0xffffffffu;
23+
public const uint DefaultPolynomial = 0xedb88320u;
24+
public const uint DefaultSeed = 0xffffffffu;
2525

26-
static UInt32[] defaultTable;
26+
private static uint[] _defaultTable;
2727

28-
readonly UInt32 seed;
29-
readonly UInt32[] table;
30-
UInt32 hash;
28+
private readonly uint _seed;
29+
private readonly uint[] _table;
30+
private uint _hash;
3131

3232
public Crc32()
3333
: this(DefaultPolynomial, DefaultSeed)
3434
{
3535
}
3636

37-
public Crc32(UInt32 polynomial, UInt32 seed)
37+
public Crc32(uint polynomial, uint seed)
3838
{
3939
if (!BitConverter.IsLittleEndian)
40+
{
4041
throw new PlatformNotSupportedException("Not supported on Big Endian processors");
42+
}
4143

42-
table = InitializeTable(polynomial);
43-
this.seed = hash = seed;
44+
_table = InitializeTable(polynomial);
45+
_seed = _hash = seed;
4446
}
4547

48+
public override int HashSize => 32;
49+
4650
public override void Initialize()
4751
{
48-
hash = seed;
52+
_hash = _seed;
4953
}
5054

5155
protected override void HashCore(byte[] array, int ibStart, int cbSize)
5256
{
53-
hash = CalculateHash(table, hash, array, ibStart, cbSize);
57+
_hash = CalculateHash(_table, _hash, array, ibStart, cbSize);
5458
}
5559

5660
protected override byte[] HashFinal()
5761
{
58-
var hashBuffer = UInt32ToBigEndianBytes(~hash);
62+
byte[] hashBuffer = UInt32ToBigEndianBytes(~_hash);
5963
HashValue = hashBuffer;
6064
return hashBuffer;
6165
}
6266

63-
public override int HashSize { get { return 32; } }
64-
65-
public static UInt32 Compute(byte[] buffer)
67+
public static uint Compute(byte[] buffer)
6668
{
6769
return Compute(DefaultSeed, buffer);
6870
}
6971

70-
public static UInt32 Compute(UInt32 seed, byte[] buffer)
72+
public static uint Compute(uint seed, byte[] buffer)
7173
{
7274
return Compute(DefaultPolynomial, seed, buffer);
7375
}
7476

75-
public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
77+
public static uint Compute(uint polynomial, uint seed, byte[] buffer)
7678
{
7779
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
7880
}
7981

80-
static UInt32[] InitializeTable(UInt32 polynomial)
82+
private static uint[] InitializeTable(uint polynomial)
8183
{
82-
if (polynomial == DefaultPolynomial && defaultTable != null)
83-
return defaultTable;
84+
if (polynomial == DefaultPolynomial && _defaultTable != null)
85+
{
86+
return _defaultTable;
87+
}
8488

85-
var createTable = new UInt32[256];
86-
for (var i = 0; i < 256; i++)
89+
uint[] createTable = new uint[256];
90+
for (int i = 0; i < 256; i++)
8791
{
88-
var entry = (UInt32)i;
89-
for (var j = 0; j < 8; j++)
92+
uint entry = (uint)i;
93+
for (int j = 0; j < 8; j++)
94+
{
9095
if ((entry & 1) == 1)
96+
{
9197
entry = (entry >> 1) ^ polynomial;
98+
}
9299
else
100+
{
93101
entry >>= 1;
102+
}
103+
}
104+
94105
createTable[i] = entry;
95106
}
96107

97108
if (polynomial == DefaultPolynomial)
98-
defaultTable = createTable;
109+
{
110+
_defaultTable = createTable;
111+
}
99112

100113
return createTable;
101114
}
102115

103-
static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList<byte> buffer, int start, int size)
116+
private static uint CalculateHash(uint[] table, uint seed, IList<byte> buffer, int start, int size)
104117
{
105-
var hash = seed;
106-
for (var i = start; i < start + size; i++)
107-
hash = (hash >> 8) ^ table[buffer[i] ^ hash & 0xff];
118+
uint hash = seed;
119+
for (int i = start; i < start + size; i++)
120+
{
121+
hash = (hash >> 8) ^ table[buffer[i] ^ (hash & 0xff)];
122+
}
123+
108124
return hash;
109125
}
110126

111-
static byte[] UInt32ToBigEndianBytes(UInt32 uint32)
127+
private static byte[] UInt32ToBigEndianBytes(uint uint32)
112128
{
113-
var result = BitConverter.GetBytes(uint32);
129+
byte[] result = BitConverter.GetBytes(uint32);
114130

115131
if (BitConverter.IsLittleEndian)
132+
{
116133
Array.Reverse(result);
134+
}
117135

118136
return result;
119137
}

0 commit comments

Comments
 (0)