Skip to content

Commit a7b8a27

Browse files
committed
change public parser to internel and private
1 parent 8ca1c2f commit a7b8a27

File tree

3 files changed

+65
-18
lines changed

3 files changed

+65
-18
lines changed

src/modules/registrypreview/RegistryPreview.FuzzTests/FuzzTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,30 @@ public static void FuzzStripFirstAndLast(ReadOnlySpan<byte> input)
142142

143143
// Fuzz test for the StripFirstAndLast method
144144
name = ParseHelper.StripFirstAndLast(name);
145+
146+
// Clean out any escaped characters in the value, only for the preview
147+
name = ParseHelper.StripEscapedCharacters(name);
148+
149+
// set the value
150+
string value = registryLine.Substring(equal + 1);
151+
152+
// trim the whitespace from the value
153+
value = value.Trim();
154+
155+
// if the first character is a " then this is a string value, so find the last most " which will avoid comments
156+
if (value.StartsWith('"'))
157+
{
158+
int last = value.LastIndexOf('"');
159+
if (last >= 0)
160+
{
161+
value = value.Substring(0, last + 1);
162+
}
163+
}
164+
165+
if (value.StartsWith('"') && value.EndsWith('"'))
166+
{
167+
value = ParseHelper.StripFirstAndLast(value);
168+
}
145169
}
146170
else
147171
{

src/modules/registrypreview/RegistryPreviewUILib/ParseHelper.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
namespace RegistryPreviewUILib
1212
{
13-
public class ParseHelper
13+
internal static class ParseHelper
1414
{
1515
private const string ERRORIMAGE = "ms-appx:///Assets/RegistryPreview/error32.png";
1616

1717
/// <summary>
1818
/// Checks a Key line for the closing bracket and treat it as an error if it cannot be found
1919
/// </summary>
20-
public static void CheckKeyLineForBrackets(ref string registryLine, ref string imageName)
20+
internal static void CheckKeyLineForBrackets(ref string registryLine, ref string imageName)
2121
{
2222
// following the current behavior of the registry editor, find the last ] and treat everything else as ignorable
2323
int lastBracket = registryLine.LastIndexOf(']');
@@ -81,7 +81,7 @@ private static bool CheckForKnownGoodBranches(string key)
8181
/// Rip the first and last character off a string,
8282
/// checking that the string is at least 2 characters long to avoid errors
8383
/// </summary>
84-
public static string StripFirstAndLast(string line)
84+
internal static string StripFirstAndLast(string line)
8585
{
8686
if (line.Length > 1)
8787
{
@@ -92,9 +92,19 @@ public static string StripFirstAndLast(string line)
9292
return line;
9393
}
9494

95+
/// <summary>
96+
/// Replace any escaped characters in the REG file with their counterparts, for the UX
97+
/// </summary>
98+
internal static string StripEscapedCharacters(string value)
99+
{
100+
value = value.Replace("\\\\", "\\"); // Replace \\ with \ in the UI
101+
value = value.Replace("\\\"", "\""); // Replace \" with " in the UI
102+
return value;
103+
}
104+
95105
// special case for when the registryLine begins with a @ - make some tweaks and
96106
// let the regular processing handle the rest.
97-
public static string ProcessRegistryLine(string registryLine)
107+
internal static string ProcessRegistryLine(string registryLine)
98108
{
99109
if (registryLine.StartsWith("@=-", StringComparison.InvariantCulture))
100110
{

src/modules/registrypreview/RegistryPreviewUILib/RegistryPreviewMainPage.Utilities.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,17 @@ private bool ParseRegistryFile(string filenameText)
225225
{
226226
// special case for when the registryLine begins with a @ - make some tweaks and
227227
// let the regular processing handle the rest.
228-
registryLine = ParseHelper.ProcessRegistryLine(registryLine);
228+
if (registryLine.StartsWith("@=-", StringComparison.InvariantCulture))
229+
{
230+
// REG file has a callout to delete the @ Value which won't work *but* the Registry Editor will
231+
// clear the value of the @ Value instead, so it's still a valid line.
232+
registryLine = registryLine.Replace("@=-", "\"(Default)\"=\"\"");
233+
}
234+
else if (registryLine.StartsWith("@=", StringComparison.InvariantCulture))
235+
{
236+
// This is the Value called "(Default)" so we tweak the line for the UX
237+
registryLine = registryLine.Replace("@=", "\"(Default)\"=");
238+
}
229239

230240
// continue until we have nothing left to read
231241
// switch logic, based off what the current line we're reading is
@@ -235,21 +245,21 @@ private bool ParseRegistryFile(string filenameText)
235245
registryLine = registryLine.Remove(1, 1);
236246

237247
string imageName = DELETEDKEYIMAGE;
238-
ParseHelper.CheckKeyLineForBrackets(ref registryLine, ref imageName);
248+
CheckKeyLineForBrackets(ref registryLine, ref imageName);
239249

240250
// this is a key, so remove the first [ and last ]
241-
registryLine = ParseHelper.StripFirstAndLast(registryLine);
251+
registryLine = StripFirstAndLast(registryLine);
242252

243253
// do not track the result of this node, since it should have no children
244254
AddTextToTree(registryLine, imageName);
245255
}
246256
else if (registryLine.StartsWith('['))
247257
{
248258
string imageName = KEYIMAGE;
249-
ParseHelper.CheckKeyLineForBrackets(ref registryLine, ref imageName);
259+
CheckKeyLineForBrackets(ref registryLine, ref imageName);
250260

251261
// this is a key, so remove the first [ and last ]
252-
registryLine = ParseHelper.StripFirstAndLast(registryLine);
262+
registryLine = StripFirstAndLast(registryLine);
253263

254264
treeViewNode = AddTextToTree(registryLine, imageName);
255265
lastKeyPath = registryLine;
@@ -260,7 +270,7 @@ private bool ParseRegistryFile(string filenameText)
260270
registryLine = registryLine.Replace("=-", string.Empty);
261271

262272
// remove the "'s without removing all of them
263-
registryLine = ParseHelper.StripFirstAndLast(registryLine);
273+
registryLine = StripFirstAndLast(registryLine);
264274

265275
// Create a new listview item that will be used to display the delete value and store it
266276
registryValue = new RegistryValue(registryLine, string.Empty, string.Empty, lastKeyPath);
@@ -290,7 +300,7 @@ private bool ParseRegistryFile(string filenameText)
290300

291301
// trim the whitespace and quotes from the name
292302
name = name.Trim();
293-
name = ParseHelper.StripFirstAndLast(name);
303+
name = StripFirstAndLast(name);
294304

295305
// Clean out any escaped characters in the value, only for the preview
296306
name = StripEscapedCharacters(name);
@@ -316,7 +326,7 @@ private bool ParseRegistryFile(string filenameText)
316326

317327
if (value.StartsWith('"') && value.EndsWith('"'))
318328
{
319-
value = ParseHelper.StripFirstAndLast(value);
329+
value = StripFirstAndLast(value);
320330
}
321331
else
322332
{
@@ -957,12 +967,7 @@ private void SaveFile()
957967
/// </summary>
958968
private string StripFirstAndLast(string line)
959969
{
960-
if (line.Length > 1)
961-
{
962-
line = line.Remove(line.Length - 1, 1);
963-
line = line.Remove(0, 1);
964-
}
965-
970+
line = ParseHelper.StripFirstAndLast(line);
966971
return line;
967972
}
968973

@@ -1025,6 +1030,14 @@ private void SetValueToolTip(RegistryValue registryValue)
10251030
registryValue.ToolTipText = value;
10261031
}
10271032

1033+
/// <summary>
1034+
/// Checks a Key line for the closing bracket and treat it as an error if it cannot be found
1035+
/// </summary>
1036+
private void CheckKeyLineForBrackets(ref string registryLine, ref string imageName)
1037+
{
1038+
ParseHelper.CheckKeyLineForBrackets(ref registryLine, ref imageName);
1039+
}
1040+
10281041
/// <summary>
10291042
/// Takes a binary registry value, sees if it has a ; and dumps the rest of the line - this does not work for REG_SZ values
10301043
/// </summary>

0 commit comments

Comments
 (0)