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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Harvesters
{
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using WixToolset.Data;
using WixToolset.Harvesters.Data;
using WixToolset.Harvesters.Extensibility;
using Wix = WixToolset.Harvesters.Serialize;
/// <summary>
/// Harvest WiX authoring for a reg file.
/// </summary>
public sealed class RegFileHarvester : BaseHarvesterExtension
{
private static readonly string ComponentPrefix = "cmp";
/// <summary>
/// Current line in the reg file being processed.
/// </summary>
private int currentLineNumber = 0;
/// <summary>
/// Flag indicating whether this is a unicode registry file.
/// </summary>
private bool unicodeRegistry;
/// <summary>
/// Harvest a file.
/// </summary>
/// <param name="argument">The path of the file.</param>
/// <returns>A harvested file.</returns>
public override Wix.Fragment[] Harvest(string argument)
{
if (null == argument)
{
throw new ArgumentNullException("argument");
}
// Harvest the keys from the registry file
Wix.Fragment fragment = this.HarvestRegFile(argument);
return new Wix.Fragment[] { fragment };
}
/// <summary>
/// Harvest a reg file.
/// </summary>
/// <param name="path">The path of the file.</param>
/// <returns>A harvested registy file.</returns>
public Wix.Fragment HarvestRegFile(string path)
{
if (null == path)
{
throw new ArgumentNullException("path");
}
if (!File.Exists(path))
{
throw new WixException(HarvesterErrors.FileNotFound(path));
}
var directory = DirectoryHelper.CreateDirectory("TARGETDIR");
// Use absolute paths
path = Path.GetFullPath(path);
FileInfo file = new FileInfo(path);
using (StreamReader sr = file.OpenText())
{
string line;
this.currentLineNumber = 0;
while (null != (line = this.GetNextLine(sr)))
{
if (line.StartsWith(@"Windows Registry Editor Version 5.00"))
{
this.unicodeRegistry = true;
}
else if (line.StartsWith(@"REGEDIT4"))
{
this.unicodeRegistry = false;
}
else if (line.StartsWith(@"[HKEY_CLASSES_ROOT\"))
{
this.ConvertKey(sr, ref directory, Wix.RegistryRootType.HKCR, line.Substring(19, line.Length - 20));
}
else if (line.StartsWith(@"[HKEY_CURRENT_USER\"))
{
this.ConvertKey(sr, ref directory, Wix.RegistryRootType.HKCU, line.Substring(19, line.Length - 20));
}
else if (line.StartsWith(@"[HKEY_LOCAL_MACHINE\"))
{
this.ConvertKey(sr, ref directory, Wix.RegistryRootType.HKLM, line.Substring(20, line.Length - 21));
}
else if (line.StartsWith(@"[HKEY_USERS\"))
{
this.ConvertKey(sr, ref directory, Wix.RegistryRootType.HKU, line.Substring(12, line.Length - 13));
}
}
}
Console.WriteLine("Processing complete");
Wix.Fragment fragment = new Wix.Fragment();
fragment.AddChild(directory);
return fragment;
}
/// <summary>
/// Converts the registry key to a WiX component element.
/// </summary>
/// <param name="sr">The registry file stream.</param>
/// <param name="directory">A WiX directory reference.</param>
/// <param name="root">The root key.</param>
/// <param name="line">The current line.</param>
private void ConvertKey(StreamReader sr, ref Wix.DirectoryBase directory, Wix.RegistryRootType root, string line)
{
Wix.Component component = new Wix.Component();
component.Id = this.Core.GenerateIdentifier(ComponentPrefix, line);
component.KeyPath = Wix.YesNoType.yes;
this.ConvertValues(sr, ref component, root, line);
directory.AddChild(component);
}
/// <summary>
/// Converts the registry values to WiX regisry key element.
/// </summary>
/// <param name="sr">The registry file stream.</param>
/// <param name="component">A WiX component reference.</param>
/// <param name="root">The root key.</param>
/// <param name="line">The current line.</param>
private void ConvertValues(StreamReader sr, ref Wix.Component component, Wix.RegistryRootType root, string line)
{
string name = null;
string value = null;
var registryKey = new Wix.RegistryKey
{
Root = root,
Key = line
};
while (this.GetValue(sr, ref name, ref value, out var type))
{
Wix.RegistryValue registryValue = new Wix.RegistryValue();
ArrayList charArray;
// Don't specifiy name for default attribute
if (!String.IsNullOrEmpty(name))
{
registryValue.Name = name;
}
registryValue.Type = type;
switch (type)
{
case Wix.RegistryValue.TypeType.binary:
registryValue.Value = value.Replace(",", String.Empty).ToUpper();
break;
case Wix.RegistryValue.TypeType.integer:
registryValue.Value = Int32.Parse(value, NumberStyles.HexNumber).ToString();
break;
case Wix.RegistryValue.TypeType.expandable:
charArray = this.ConvertCharList(value);
value = String.Empty;
// create the string, remove the terminating null
for (int i = 0; i < charArray.Count; i++)
{
if ('\0' != (char)charArray[i])
{
value += charArray[i];
}
}
registryValue.Value = value;
break;
case Wix.RegistryValue.TypeType.multiString:
charArray = this.ConvertCharList(value);
value = String.Empty;
// Convert the character array to a string so we can simply split it at the nulls, ignore the final null null.
for (int i = 0; i < (charArray.Count - 2); i++)
{
value += charArray[i];
}
// Although the value can use [~] the preffered way is to use MultiStringValue
string[] parts = value.Split("\0".ToCharArray());
foreach (string part in parts)
{
Wix.MultiStringValue multiStringValue = new Wix.MultiStringValue();
multiStringValue.Value = part;
registryValue.AddChild(multiStringValue);
}
break;
case Wix.RegistryValue.TypeType.@string:
// Remove \\ and \"
value = value.ToString().Replace("\\\"", "\"");
value = value.ToString().Replace(@"\\", @"\");
// Escape [ and ]
value = value.ToString().Replace(@"[", @"[\[]");
value = value.ToString().Replace(@"]", @"[\]]");
// This undoes the duplicate escaping caused by the second replace
value = value.ToString().Replace(@"[\[[\]]", @"[\[]");
// Escape $
value = value.ToString().Replace(@"$", @"$$");
registryValue.Value = value;
break;
default:
throw new ApplicationException(String.Format("Did not recognize the type of reg value on line {0}", this.currentLineNumber));
}
registryKey.AddChild(registryValue);
}
// Make sure empty keys are created
if (null == value)
{
registryKey.ForceCreateOnInstall = Wix.YesNoType.yes;
}
component.AddChild(registryKey);
}
/// <summary>
/// Parse a value from a line.
/// </summary>
/// <param name="sr">Reader for the reg file.</param>
/// <param name="name">Name of the value.</param>
/// <param name="value">Value of the value.</param>
/// <param name="type">Type of the value.</param>
/// <returns>true if the value can be parsed, false otherwise.</returns>
private bool GetValue(StreamReader sr, ref string name, ref string value, out Wix.RegistryValue.TypeType type)
{
string line = this.GetNextLine(sr);
if (null == line || 0 == line.Length)
{
type = 0;
return false;
}
string[] parts;
if (line.StartsWith("@"))
{
// Special case for default value
parts = line.Trim().Split("=".ToCharArray(), 2);
name = null;
}
else
{
parts = line.Trim().Split("=".ToCharArray());
// It is valid to have an '=' in the name or the data. This is probably a string so the separator will be '"="'.
if (2 != parts.Length)
{
string[] stringSeparator = new string[] { "\"=\"" };
parts = line.Trim().Split(stringSeparator, StringSplitOptions.None);
if (2 != parts.Length)
{
// Line still no parsed correctly
throw new ApplicationException(String.Format("Cannot parse value: {0} at line {1}.", line, this.currentLineNumber));
}
// Put back quotes stripped by Split()
parts[0] += "\"";
parts[1] = "\"" + parts[1];
}
name = parts[0].Substring(1, parts[0].Length - 2);
}
if (parts[1].StartsWith("hex:"))
{
// binary
value = parts[1].Substring(4);
type = Wix.RegistryValue.TypeType.binary;
}
else if (parts[1].StartsWith("dword:"))
{
// dword
value = parts[1].Substring(6);
type = Wix.RegistryValue.TypeType.integer;
}
else if (parts[1].StartsWith("hex(2):"))
{
// expandable string
value = parts[1].Substring(7);
type = Wix.RegistryValue.TypeType.expandable;
}
else if (parts[1].StartsWith("hex(7):"))
{
// multi-string
value = parts[1].Substring(7);
type = Wix.RegistryValue.TypeType.multiString;
}
else if (parts[1].StartsWith("hex("))
{
// Give a better error when we find something that isn't supported
// by specifying the type that isn't supported.
string unsupportedType = "";
if (parts[1].StartsWith("hex(0")) { unsupportedType = "REG_NONE"; }
else if (parts[1].StartsWith("hex(6")) { unsupportedType = "REG_LINK"; }
else if (parts[1].StartsWith("hex(8")) { unsupportedType = "REG_RESOURCE_LIST"; }
else if (parts[1].StartsWith("hex(9")) { unsupportedType = "REG_FULL_RESOURCE_DESCRIPTOR"; }
else if (parts[1].StartsWith("hex(a")) { unsupportedType = "REG_RESOURCE_REQUIREMENTS_LIST"; }
else if (parts[1].StartsWith("hex(b")) { unsupportedType = "REG_QWORD"; }
// REG_NONE(0), REG_LINK(6), REG_RESOURCE_LIST(8), REG_FULL_RESOURCE_DESCRIPTOR(9), REG_RESOURCE_REQUIREMENTS_LIST(a), REG_QWORD(b)
this.Core.Messaging.Write(HarvesterWarnings.UnsupportedRegistryType(parts[0], this.currentLineNumber, unsupportedType));
type = 0;
return false;
}
else if (parts[1].StartsWith("\""))
{
// string
value = parts[1].Substring(1, parts[1].Length - 2);
type = Wix.RegistryValue.TypeType.@string;
}
else
{
// unsupported value
throw new ApplicationException(String.Format("Unsupported registry value {0} at line {1}.", line, this.currentLineNumber));
}
return true;
}
/// <summary>
/// Get the next line from the reg file input stream.
/// </summary>
/// <param name="sr">Reader for the reg file.</param>
/// <returns>The next line.</returns>
private string GetNextLine(StreamReader sr)
{
string line;
string totalLine = null;
while (null != (line = sr.ReadLine()))
{
bool stop = true;
this.currentLineNumber++;
line = line.Trim();
if (line.EndsWith("\\"))
{
stop = false;
line = line.Substring(0, line.Length - 1);
}
if (null == totalLine)
{
// first line
totalLine = line;
}
else
{
// other lines
totalLine += line;
}
// break if there is no more info for this line
if (stop)
{
break;
}
}
return totalLine;
}
/// <summary>
/// Convert a character list into the proper WiX format for either unicode or ansi lists.
/// </summary>
/// <param name="charList">List of characters.</param>
/// <returns>Array of characters.</returns>
private ArrayList ConvertCharList(string charList)
{
if (String.IsNullOrEmpty(charList))
{
return new ArrayList();
}
string[] strChars = charList.Split(",".ToCharArray());
ArrayList charArray = new ArrayList();
if (this.unicodeRegistry)
{
if (0 != strChars.Length % 2)
{
throw new ApplicationException(String.Format("Problem parsing Expandable string data at line {0}, its probably not Unicode.", this.currentLineNumber));
}
for (int i = 0; i < strChars.Length; i += 2)
{
string chars = strChars[i + 1] + strChars[i];
int unicodeInt = Int32.Parse(chars, NumberStyles.HexNumber);
char unicodeChar = (char)unicodeInt;
charArray.Add(unicodeChar);
}
}
else
{
for (int i = 0; i < strChars.Length; i++)
{
char charValue = (char)Int32.Parse(strChars[i], NumberStyles.HexNumber);
charArray.Add(charValue);
}
}
return charArray;
}
}
}
|