-
Notifications
You must be signed in to change notification settings - Fork 2
/
RegDiff.cs
164 lines (144 loc) · 5.71 KB
/
RegDiff.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
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
//-----------------------------------------------------------------------
// <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.Spatial;
using System.Text;
using System.Text.RegularExpressions;
using ClrPlus.Core.Collections;
using DiscUtils.Registry;
namespace DiffVHD
{
public class RegDiff
{
public class ValueObject
{
public RegistryValueType Type { get; private set; }
public object Value { get; private set; }
public ValueObject(RegistryValueType Kind, object Object)
{
Type = Kind;
Value = Object;
}
}
private readonly XDictionary<string, XDictionary<string, ValueObject>> Data;
public RegDiff()
{
Data = new XDictionary<string, XDictionary<string, ValueObject>>();
}
public RegDiff(RegistryComparison Source, RegistryComparison.Side Side)
: this()
{
var origin = Source.Output;
foreach (var data in origin)
{
int tmpIndex = data.Key.IndexOf("::");
string path = data.Key.Substring(0, tmpIndex);
string name = data.Key.Substring(tmpIndex + 2);
if (!data.Value.Same)
if (Side == RegistryComparison.Side.A &&
!(data.Value.TypeA == RegistryValueType.None && data.Value.ValueA == null))
Data[path][name] = new ValueObject(data.Value.TypeA, data.Value.ValueA);
else if (!(data.Value.TypeB == RegistryValueType.None && data.Value.ValueB == null)) // Side == B
Data[path][name] = new ValueObject(data.Value.TypeB, data.Value.ValueB);
}
}
public static RegDiff ReadFromStream(Stream Input)
{
try { return ReadFromHive(new RegistryHive(Input, DiscUtils.Ownership.None)); }
catch (Exception e) { return new RegDiff(); }
}
public static RegDiff ReadFromHive(RegistryHive Input)
{
var Out = new RegDiff();
var Root = Input.Root;
foreach (var name in Root.GetValueNames())
{
Out.Data[String.Empty][name] = new ValueObject(Root.GetValueType(name), Root.GetValue(name));
}
foreach (var sub in Root.GetSubKeyNames())
{
InnerRead(Root.OpenSubKey(sub), Out.Data);
}
return Out;
}
private static void InnerRead(RegistryKey key, XDictionary<string, XDictionary<string, ValueObject>> data)
{
foreach (var name in key.GetValueNames())
{
data[key.Name][name] = new ValueObject(key.GetValueType(name), key.GetValue(name));
}
foreach (var sub in key.GetSubKeyNames())
{
InnerRead(key.OpenSubKey(sub), data);
}
}
public void WriteToStream(Stream Output)
{
RegistryHive Out;
try { Out = new RegistryHive(Output, DiscUtils.Ownership.None); }
catch (Exception e) { Out = RegistryHive.Create(Output, DiscUtils.Ownership.None); }
var root = Out.Root;
foreach (var path in Data)
{
var currentKey = root.CreateSubKey(path.Key);
foreach (var val in path.Value)
currentKey.SetValue(val.Key, val.Value.Value, val.Value.Type);
}
}
public bool ApplyTo(RegistryKey Root, Action<string> Log = null)
{
if (Root == null)
throw new ArgumentNullException("Root");
bool status = true;
try
{
foreach (var path in Data)
{
try
{
RegistryKey currentPath = path.Key.Split('\\')
.Aggregate(Root, (current, sub) => current.CreateSubKey(sub));
foreach (var item in path.Value)
{
try
{
currentPath.SetValue(item.Key, item.Value.Value, item.Value.Type);
}
catch (Exception e)
{
if (Log != null)
Log(e.Message + '\n' + e.StackTrace);
status = false;
}
}
}
catch (Exception e)
{
if (Log != null)
Log(e.Message + '\n' + e.StackTrace);
status = false;
}
}
}
catch (Exception e)
{
if (Log != null)
Log(e.Message + '\n' + e.StackTrace);
status = false;
}
return status;
}
}
}