-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRegistryComparison.cs
More file actions
338 lines (310 loc) · 13.5 KB
/
RegistryComparison.cs
File metadata and controls
338 lines (310 loc) · 13.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//-----------------------------------------------------------------------
// <copyright company="CoApp Project">
// Copyright (c) 2013 Tim Rogers and CoApp Contributors.
// Contributors can be discovered using the 'git log' command.
// All rights reserved.
// </copyright>
// <license>
// The software is licensed under the Apache 2.0 License (the "License")
// You may not use the software except in compliance with the License.
// </license>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DiscUtils.Registry;
namespace DiffVHD
{
public class RegistryComparison
{
public enum Side { A, B }
public RegistryHive HiveA { get; private set; }
public RegistryHive HiveB { get; private set; }
public Dictionary<string, Data> Output { get; private set; }
private bool HavePreference = false;
private Side Preference;
public RegistryComparison(string HiveFileA, string HiveFileB)
: this(File.OpenRead(HiveFileA), File.OpenRead(HiveFileB))
{}
public RegistryComparison(Stream HiveFileA, Stream HiveFileB)
{
Output = new Dictionary<string, Data>();
HiveA = new RegistryHive(HiveFileA);
HiveB = new RegistryHive(HiveFileB);
}
public RegistryComparison(string HiveFileA, string HiveFileB, Side PreferSide)
: this(File.OpenRead(HiveFileA), File.OpenRead(HiveFileB), PreferSide)
{}
public RegistryComparison(Stream HiveFileA, Stream HiveFileB, Side PreferSide)
: this(HiveFileA, HiveFileB)
{
HavePreference = true;
Preference = PreferSide;
}
public class Data
{
public RegistryValueType TypeA { get; private set; }
public object ValueA { get; private set; }
public RegistryValueType TypeB { get; private set; }
public object ValueB { get; private set; }
public bool Same { get; private set; }
public Data()
{
TypeA = RegistryValueType.None;
TypeB = RegistryValueType.None;
ValueA = null;
ValueB = null;
Same = false;
}
public Data(object aVal, RegistryValueType aType, object bVal, RegistryValueType bType)
{
TypeA = aType;
TypeB = bType;
ValueA = aVal;
ValueB = bVal;
CheckSame();
}
public void SetA(object aVal, RegistryValueType aType)
{
TypeA = aType;
ValueA = aVal;
CheckSame();
}
public void SetB(object bVal, RegistryValueType bType)
{
TypeA = bType;
ValueA = bVal;
CheckSame();
}
public void CheckSame()
{
Same = ValueA != null && ValueB != null && (TypeA != TypeB && ValueA.Equals(ValueB));
}
}
public bool DoCompare()
{
var t = HavePreference
? Preference == Side.A
? AOnlyInnerCompare(HiveA.Root, HiveB.Root, @"\")
: BOnlyInnerCompare(HiveA.Root, HiveB.Root, @"\")
: InnerCompare(HiveA.Root, HiveB.Root, @"\");
t.Wait();
return t.Result;
}
private Task<bool> InnerCompare(RegistryKey A, RegistryKey B, string root) // TODO: Adjust to match the BOnly comparison (below)
{
List<Task<bool>> tasks = new List<Task<bool>>();
try
{
if (A != null)
{
// Process A
string[] aVals;
lock (HiveA)
aVals = A.GetValueNames();
foreach (var Name in aVals)
{
string EntryName;
lock (HiveA)
EntryName = root + A.Name + "::" + Name;
var dat = new Data();
lock (HiveA)
dat.SetA(A.GetValue(Name), A.GetValueType(Name));
Output.Add(EntryName, dat);
}
string[] ASubKeys;
lock (HiveA)
ASubKeys = A.GetSubKeyNames();
string[] BSubKeys = new string[0];
if (B != null)
lock (HiveB)
BSubKeys = B.GetSubKeyNames();
tasks.AddRange(ASubKeys.AsParallel().Select(keyName =>
{
RegistryKey aSub, bSub;
lock (HiveA)
aSub = A.OpenSubKey(keyName);
lock (HiveB)
bSub = B == null
? null
: BSubKeys.Contains(keyName, StringComparer.CurrentCultureIgnoreCase)
? B.OpenSubKey(keyName)
: null;
return InnerCompare(aSub, bSub, root + keyName + @"\");
}));
}
if (B != null)
{
// Process B
string[] bVals;
lock (HiveB)
bVals = B.GetValueNames();
foreach (var Name in bVals)
{
string EntryName;
lock (HiveB)
EntryName = root + B.Name + "::" + Name;
Data dat = Output.ContainsKey(EntryName) ? Output[EntryName] : new Data();
lock (HiveB)
dat.SetB(B.GetValue(Name), B.GetValueType(Name));
Output[EntryName] = dat;
}
string[] BSubKeys;
lock (HiveB)
BSubKeys = B.GetSubKeyNames();
tasks.AddRange(BSubKeys.AsParallel().Select(keyName =>
{
RegistryKey bSub;
lock (HiveB)
bSub = B.OpenSubKey(keyName);
return InnerCompare(null, bSub, root + keyName + @"\");
}));
}
return Task.Factory.StartNew(() =>
tasks.Aggregate(true, (ret, task) =>
{
task.Wait();
return ret && task.Result;
}), TaskCreationOptions.AttachedToParent);
}
catch (Exception e)
{
throw;
}
}
private Task<bool> AOnlyInnerCompare(RegistryKey A, RegistryKey B, string root) // TODO: Adjust to match the BOnly comparison (below)
{
List<Task<bool>> tasks = new List<Task<bool>>();
try
{
if (A != null)
{
// Process A
string[] aVals;
lock (HiveA)
aVals = A.GetValueNames();
string[] bVals = new string[0];
if (B != null)
lock (HiveB)
bVals = B.GetValueNames();
foreach (var Name in aVals)
{
string EntryName;
lock (HiveA)
EntryName = root + A.Name + "::" + Name;
var dat = new Data();
lock (HiveA)
dat.SetA(A.GetValue(Name), A.GetValueType(Name));
if (bVals.Contains(Name, StringComparer.CurrentCultureIgnoreCase))
lock (HiveB)
dat.SetB(B.GetValue(Name), B.GetValueType(Name));
Output.Add(EntryName, dat);
}
string[] ASubKeys;
lock (HiveA)
ASubKeys = A.GetSubKeyNames();
string[] BSubKeys = new string[0];
if (B != null)
lock (HiveB)
BSubKeys = B.GetSubKeyNames();
tasks.AddRange(ASubKeys.AsParallel().Select(keyName =>
{
RegistryKey aSub, bSub;
lock (HiveA)
aSub = A.OpenSubKey(keyName);
lock (HiveB)
bSub = B == null
? null
: BSubKeys.Contains(keyName, StringComparer.CurrentCultureIgnoreCase)
? B.OpenSubKey(keyName)
: null;
return AOnlyInnerCompare(aSub, bSub, root + keyName + @"\");
}));
}
return Task.Factory.StartNew(() =>
tasks.Aggregate(true, (ret, task) =>
{
task.Wait();
return ret && task.Result;
}), TaskCreationOptions.AttachedToParent);
}
catch (Exception e)
{
throw;
}
}
private async Task<bool> BOnlyInnerCompare(RegistryKey A, RegistryKey B, string root)
{
List<Task<bool>> tasks = new List<Task<bool>>();
List<bool> bools = new List<bool>();
try
{
if (B != null)
{
// Process A
string[] bVals;
lock (HiveB)
bVals = B.GetValueNames();
string[] aVals = new string[0];
if (A != null)
lock (HiveA)
aVals = A.GetValueNames();
foreach (var Name in bVals)
{
string EntryName;
lock (HiveB)
EntryName = root + B.Name + "::" + Name;
var dat = new Data();
lock (HiveB)
dat.SetB(B.GetValue(Name), B.GetValueType(Name));
if (aVals.Contains(Name, StringComparer.CurrentCultureIgnoreCase))
lock (HiveA)
dat.SetA(A.GetValue(Name), A.GetValueType(Name));
lock(Output)
Output.Add(EntryName, dat);
}
string[] BSubKeys;
lock (HiveB)
BSubKeys = B.GetSubKeyNames();
string[] ASubKeys = new string[0];
if (A != null)
lock (HiveA)
ASubKeys = A.GetSubKeyNames();
tasks.AddRange(BSubKeys.Select(async keyName =>
{
RegistryKey aSub, bSub;
lock (HiveB)
bSub = B.OpenSubKey(keyName);
lock (HiveA)
aSub = A == null
? null
: ASubKeys.Contains(keyName, StringComparer.CurrentCultureIgnoreCase)
? A.OpenSubKey(keyName)
: null;
return await BOnlyInnerCompare(aSub, bSub, root + keyName + @"\");
}));
}
/*
return Task.Factory.StartNew(() =>
tasks.AsParallel().Aggregate(true, (ret, task) =>
{
task.Wait();
return ret && task.Result;
}), TaskCreationOptions.AttachedToParent);
*/
return tasks.AsParallel().Aggregate(true, (ret, task) =>
{
task.Wait();
return ret && task.Result;
});
}
catch (Exception e)
{
throw;
}
}
}
}