Browse Source

Escape/Unescape invalid characters on save/load to avoid exceptions in XDocument.Load/Save. fixes #54

pull/320/merge
Siegfried Pammer 14 years ago
parent
commit
4de3aba67b
  1. 13
      ILSpy/ILSpySettings.cs
  2. 26
      ILSpy/SessionSettings.cs

13
ILSpy/ILSpySettings.cs

@ -70,11 +70,7 @@ namespace ICSharpCode.ILSpy @@ -70,11 +70,7 @@ namespace ICSharpCode.ILSpy
static XDocument LoadWithoutCheckingCharacters(string fileName)
{
// XDocument.Load(fileName) validates that no invalid characters appear (not even in escaped form),
// but we need those characters for some obfuscated assemblies.
using (XmlTextReader r = new XmlTextReader(fileName)) {
return XDocument.Load(r);
}
return XDocument.Load(fileName, LoadOptions.None);
}
/// <summary>
@ -113,12 +109,7 @@ namespace ICSharpCode.ILSpy @@ -113,12 +109,7 @@ namespace ICSharpCode.ILSpy
}
doc.Root.SetAttributeValue("version", RevisionClass.Major + "." + RevisionClass.Minor + "." + RevisionClass.Build + "." + RevisionClass.Revision);
action(doc.Root);
// We can't use XDocument.Save(filename) because that checks for invalid characters, but those can appear
// in obfuscated assemblies.
using (XmlTextWriter writer = new XmlTextWriter(config, Encoding.UTF8)) {
writer.Formatting = Formatting.Indented;
doc.Save(writer);
}
doc.Save(config, SaveOptions.None);
}
}

26
ILSpy/SessionSettings.cs

@ -18,7 +18,10 @@ @@ -18,7 +18,10 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Xml.Linq;
@ -43,7 +46,7 @@ namespace ICSharpCode.ILSpy @@ -43,7 +46,7 @@ namespace ICSharpCode.ILSpy
XElement activeTreeViewPath = doc.Element("ActiveTreeViewPath");
if (activeTreeViewPath != null) {
this.ActiveTreeViewPath = activeTreeViewPath.Elements().Select(e => (string)e).ToArray();
this.ActiveTreeViewPath = activeTreeViewPath.Elements().Select(e => Unescape((string)e)).ToArray();
}
this.WindowState = FromString((string)doc.Element("WindowState"), WindowState.Normal);
@ -84,7 +87,7 @@ namespace ICSharpCode.ILSpy @@ -84,7 +87,7 @@ namespace ICSharpCode.ILSpy
doc.Add(new XElement("ActiveAssemblyList", this.ActiveAssemblyList));
}
if (this.ActiveTreeViewPath != null) {
doc.Add(new XElement("ActiveTreeViewPath", ActiveTreeViewPath.Select(p => new XElement("Node", p))));
doc.Add(new XElement("ActiveTreeViewPath", ActiveTreeViewPath.Select(p => new XElement("Node", Escape(p)))));
}
doc.Add(new XElement("WindowState", ToString(this.WindowState)));
doc.Add(new XElement("WindowBounds", ToString(this.WindowBounds)));
@ -95,6 +98,25 @@ namespace ICSharpCode.ILSpy @@ -95,6 +98,25 @@ namespace ICSharpCode.ILSpy
ILSpySettings.SaveSettings(doc);
}
static Regex regex = new Regex("\\\\x(?<num>[0-9A-f]{4})");
static string Escape(string p)
{
StringBuilder sb = new StringBuilder();
foreach (char ch in p) {
if (char.IsLetterOrDigit(ch))
sb.Append(ch);
else
sb.AppendFormat("\\x{0:X4}", (int)ch);
}
return sb.ToString();
}
static string Unescape(string p)
{
return regex.Replace(p, m => ((char)int.Parse(m.Groups["num"].Value, NumberStyles.HexNumber)).ToString());
}
static T FromString<T>(string s, T defaultValue)
{
if (s == null)

Loading…
Cancel
Save