Add super basic UIX decompiler

This commit is contained in:
Joshua Askharoun
2021-04-17 21:52:36 -05:00
parent 64f78f42b8
commit 1b199adc28
10 changed files with 1226 additions and 47 deletions
File diff suppressed because it is too large Load Diff
@@ -18,6 +18,8 @@ namespace Microsoft.Iris.Markup
private ParameterContext _parameterContext;
private Map<object, object> _scopedLocals;
private static Stack s_cache = new Stack();
public static bool UseDecompile = false;
public static System.Xml.XmlDocument DecompileResult;
private InterpreterContext()
{
@@ -0,0 +1,379 @@
using Microsoft.Iris.Data;
using Microsoft.Iris.Markup;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Xml;
namespace Microsoft.Iris.Markup
{
internal struct ManagedXmlReader : IDisposable
{
private XmlReader _reader;
private XmlNodeType _prevNodeType;
private XmlNodeType _curNodeType;
private int _lineNumber;
private int _linePosition;
private bool _beforeFirstAttribute;
public ManagedXmlReader(Resource resource) : this(false) => _reader = XmlReader.Create(resource.Uri);
public ManagedXmlReader(string content, bool isFragment) : this(false) => _reader = XmlReader.Create(new StringReader(content));
private ManagedXmlReader(bool placeHolder)
{
_lineNumber = -1;
_linePosition = -1;
_beforeFirstAttribute = true;
_reader = null;
_curNodeType = XmlNodeType.None;
_prevNodeType = XmlNodeType.None;
}
public void Dispose()
{
_reader.Close();
_reader = null;
}
public bool Read(out XmlNodeType nodeType)
{
_beforeFirstAttribute = true;
_lineNumber = InternalLineNumber;
_linePosition = InternalLinePosition;
bool flag = _reader.Read();
nodeType = _reader.NodeType;
_prevNodeType = _curNodeType;
_curNodeType = nodeType;
return flag;
}
public bool ReadAttribute()
{
if (_beforeFirstAttribute)
return MoveToFirstAttribute();
bool nextAttribute = MoveToNextAttribute();
if (!nextAttribute)
_beforeFirstAttribute = true;
return nextAttribute;
}
private bool MoveToFirstAttribute()
{
_beforeFirstAttribute = false;
_lineNumber = InternalLineNumber;
_linePosition = InternalLinePosition;
return _reader.MoveToFirstAttribute();
}
private bool MoveToNextAttribute()
{
_lineNumber = InternalLineNumber;
_linePosition = InternalLinePosition;
return _reader.MoveToNextAttribute();
}
public bool IsEmptyElement => _reader.IsEmptyElement;
public string Name => _reader.Name;
public string LocalName => _reader.LocalName;
public string Prefix => _reader.Prefix;
public string Value => _reader.Value;
public unsafe bool IsInlineExpression
{
get
{
string value = _reader.Value;
return value.StartsWith("{") && value.EndsWith("}");
}
}
public SSLexUnicodeBufferConsumer LexConsumerForValueWithPrefix(string prefix)
{
char[] buffer = _reader.Value.ToCharArray();
SSLexUnicodeBufferConsumer unicodeBufferConsumer = new SSLexUnicodeBufferConsumer(buffer, (uint)buffer.Length, prefix);
int lineNumber = _lineNumber;
int column = _linePosition - prefix.Length + 1;
if (_curNodeType == XmlNodeType.CDATA && _prevNodeType != XmlNodeType.Whitespace)
column += 9;
unicodeBufferConsumer.SetDocumentOffset(lineNumber, column);
return unicodeBufferConsumer;
}
public int LineNumber => _lineNumber;
public int LinePosition => _linePosition;
private int InternalLineNumber => ((IXmlLineInfo)_reader).LineNumber;
private int InternalLinePosition => ((IXmlLineInfo)_reader).LinePosition;
private void IFC(uint hr) => SUCCEEDED(hr);
private bool SUCCEEDED(uint hr)
{
if (hr == 0U)
return true;
if (hr == 1U)
return false;
ThrowXmlException(hr);
return false;
}
private void ThrowXmlException(uint hr)
{
string message = null;
switch (hr)
{
case 3222069277:
message = "Character in character entity is not a decimal digit as was expected.";
break;
case 3222069278:
message = "Character in character entity is not a hexadecimal digit as was expected.";
break;
case 3222069279:
message = "Character entity has invalid Unicode value.";
break;
case 3222072833:
message = "Unexpected end of input";
break;
case 3222072834:
message = "Unrecognized encoding";
break;
case 3222072835:
message = "Unable to switch the encoding";
break;
case 3222072836:
message = "Unrecognized input signature";
break;
case 3222072865:
message = "Whitespace expected";
break;
case 3222072866:
message = "Semicolon expected";
break;
case 3222072867:
message = "'>' expected";
break;
case 3222072868:
message = "Quote expected";
break;
case 3222072869:
message = "Equal expected";
break;
case 3222072870:
message = "Well-formedness constraint: no '<' in attribute value";
break;
case 3222072871:
message = "Hexadecimal digit expected";
break;
case 3222072872:
message = "Decimal digit expected";
break;
case 3222072873:
message = "'[' expected";
break;
case 3222072874:
message = "'(' expected";
break;
case 3222072875:
message = "Illegal xml character";
break;
case 3222072876:
message = "Illegal name character";
break;
case 3222072877:
message = "Incorrect document syntax";
break;
case 3222072878:
message = "Incorrect CDATA section syntax";
break;
case 3222072879:
message = "Incorrect comment syntax";
break;
case 3222072880:
message = "Incorrect conditional section syntax";
break;
case 3222072881:
message = "Incorrect ATTLIST declaration syntax";
break;
case 3222072882:
message = "Incorrect DOCTYPE declaration syntax";
break;
case 3222072883:
message = "Incorrect ELEMENT declaration syntax";
break;
case 3222072884:
message = "Incorrect ENTITY declaration syntax";
break;
case 3222072885:
message = "Incorrect NOTATION declaration syntax";
break;
case 3222072886:
message = "NDATA expected";
break;
case 3222072887:
message = "PUBLIC expected";
break;
case 3222072888:
message = "SYSTEM expected";
break;
case 3222072889:
message = "Name expected";
break;
case 3222072890:
message = "One root element";
break;
case 3222072891:
message = "Well-formedness constraint: element type match";
break;
case 3222072892:
message = "Well-formedness constraint: unique attribute spec";
break;
case 3222072893:
message = "Text/xmldecl not at the beginning of input";
break;
case 3222072894:
message = "Leading \"xml\"";
break;
case 3222072895:
message = "Incorrect text declaration syntax";
break;
case 3222072896:
message = "Incorrect xml declaration syntax";
break;
case 3222072897:
message = "Incorrect encoding name syntax";
break;
case 3222072898:
message = "Incorrect public identifier syntax";
break;
case 3222072899:
message = "Well-formedness constraint: pes in internal subset";
break;
case 3222072900:
message = "Well-formedness constraint: pes between declarations";
break;
case 3222072901:
message = "Well-formedness constraint: no recursion";
break;
case 3222072902:
message = "Entity content not well formed";
break;
case 3222072903:
message = "Well-formedness constraint: undeclared entity ";
break;
case 3222072904:
message = "Well-formedness constraint: parsed entity";
break;
case 3222072905:
message = "Well-formedness constraint: no external entity references";
break;
case 3222072906:
message = "Incorrect processing instruction syntax";
break;
case 3222072907:
message = "Incorrect system identifier syntax";
break;
case 3222072908:
message = "'?' expected";
break;
case 3222072909:
message = "No ']]>' in element content";
break;
case 3222072910:
message = "Not all chunks of value have been read";
break;
case 3222072911:
message = "DTD was found but is prohibited";
break;
case 3222072912:
message = "xml:space attribute with invalid value";
break;
case 3222072929:
message = "Illegal qualified name character";
break;
case 3222072930:
message = "Multiple colons in qualified name";
break;
case 3222072931:
message = "Colon in name";
break;
case 3222072932:
message = "Declared prefix";
break;
case 3222072933:
message = "Undeclared prefix";
break;
case 3222072934:
message = "Non default namespace with empty uri";
break;
case 3222072935:
message = "\"xml\" prefix is reserved and must have the http://www.w3.org/XML/1998/namespace URI";
break;
case 3222072936:
message = "\"xmlns\" prefix is reserved for use by XML";
break;
case 3222072937:
message = "xml namespace URI (http://www.w3.org/XML/1998/namespace) must be assigned only to prefix \"xml\"";
break;
case 3222072938:
message = "xmlns namespace URI (http://www.w3.org/2000/xmlns/) is reserved and must not be used";
break;
case 3222072961:
message = "Element depth exceeds limit in XmlReaderProperty_MaxElementDepth";
break;
case 3222072962:
message = "Entity expansion exceeds limit in XmlReaderProperty_MaxEntityExpansion";
break;
case 3222073089:
message = "Writer: specified string is not whitespace";
break;
case 3222073090:
message = "Writer: namespace prefix is already declared with a different namespace";
break;
case 3222073091:
message = "Writer: It is not allowed to declare a namespace prefix with empty URI (for example xmlns:p=””).";
break;
case 3222073092:
message = "Writer: duplicate attribute";
break;
case 3222073093:
message = "Writer: can not redefine the xmlns prefix";
break;
case 3222073094:
message = "Writer: xml prefix must have the http://www.w3.org/XML/1998/namespace URI";
break;
case 3222073095:
message = "Writer: xml namespace URI (http://www.w3.org/XML/1998/namespace) must be assigned only to prefix \"xml\"";
break;
case 3222073096:
message = "Writer: xmlns namespace URI (http://www.w3.org/2000/xmlns/) is reserved and must not be used";
break;
case 3222073097:
message = "Writer: namespace is not declared";
break;
case 3222073098:
message = "Writer: invalid value of xml:space attribute (allowed values are \"default\" and \"preserve\")";
break;
case 3222073099:
message = "Writer: performing the requested action would result in invalid XML document";
break;
case 3222073100:
message = "Writer: input contains invalid or incomplete surrogate pair";
break;
}
if (message != null)
{
int lineNumber = _reader == null ? -1 : InternalLineNumber;
int linePosition = _reader == null? -1 : InternalLinePosition;
throw new XmlException(message, new Exception(hr.ToString()), lineNumber, linePosition);
}
throw new XmlException("Unknown error", new Exception(hr.ToString()), -1, -1);
}
}
}
@@ -142,7 +142,7 @@ namespace Microsoft.Iris.Markup
uint num = scriptId >> 27;
while ((int)num != (int)markupTypeSchema._typeDepth)
markupTypeSchema = markupTypeSchema._baseType;
object obj = markupTypeSchema.RunAtOffset(markupType, scriptId & 134217727U, parameterContext);
object obj = markupTypeSchema.RunAtOffset(markupType, scriptId & 0x07FFFFFFU, parameterContext);
ErrorManager.ExitContext();
return obj;
}
+14 -13
View File
@@ -10,6 +10,7 @@ using Microsoft.Iris.OS;
using Microsoft.Iris.Session;
using SSVParseLib;
using System.Collections;
using System.Xml;
namespace Microsoft.Iris.Markup
{
@@ -28,17 +29,17 @@ namespace Microsoft.Iris.Markup
Stack parseStack = new Stack();
bool flag = false;
bool additionalMetadata = MarkupSystem.TrackAdditionalMetadata;
using (NativeXmlReader xmlReader = new NativeXmlReader(resource))
using (ManagedXmlReader xmlReader = new ManagedXmlReader(resource))
{
try
{
string str1 = string.Empty;
NativeXmlNodeType nodeType;
XmlNodeType nodeType;
while (xmlReader.Read(out nodeType))
{
if (parseResult.Root == null || parseStack.Count == 0 && nodeType == NativeXmlNodeType.EndElement)
if (parseResult.Root == null || parseStack.Count == 0 && nodeType == XmlNodeType.EndElement)
{
if (nodeType == NativeXmlNodeType.Element)
if (nodeType == XmlNodeType.Element)
{
parseResult.Root = xmlReader.Name;
while (xmlReader.ReadAttribute())
@@ -64,7 +65,7 @@ namespace Microsoft.Iris.Markup
{
switch (nodeType)
{
case NativeXmlNodeType.Element:
case XmlNodeType.Element:
if (flag)
{
ReportError(xmlReader, "Script tag may not contain XML elements, found: '{0}'", xmlReader.Name);
@@ -141,8 +142,8 @@ namespace Microsoft.Iris.Markup
continue;
}
continue;
case NativeXmlNodeType.Text:
case NativeXmlNodeType.CDATA:
case XmlNodeType.Text:
case XmlNodeType.CDATA:
if (parseStack.Count == 0)
{
ReportError(xmlReader, "Text/CDATA is not allowed under root tag ('{0}')", xmlReader.Value.Trim());
@@ -175,7 +176,7 @@ namespace Microsoft.Iris.Markup
ReportError(xmlReader, "Object tag may not contain Text/CDATA: '{0}'", xmlReader.Value.Trim());
continue;
}
case NativeXmlNodeType.Comment:
case XmlNodeType.Comment:
string str2 = null;
if (additionalMetadata)
{
@@ -190,7 +191,7 @@ namespace Microsoft.Iris.Markup
continue;
}
continue;
case NativeXmlNodeType.EndElement:
case XmlNodeType.EndElement:
flag = false;
HandleEndElement(owner, parseResult, parseStack);
continue;
@@ -291,7 +292,7 @@ namespace Microsoft.Iris.Markup
private static ValidateObject CreateValidateObjectFromString(
SourceMarkupLoader owner,
NativeXmlReader xmlReader)
ManagedXmlReader xmlReader)
{
if (xmlReader.IsInlineExpression)
return (ValidateObject)ParseCode(owner, xmlReader, CodeType.InlineExpression);
@@ -307,7 +308,7 @@ namespace Microsoft.Iris.Markup
private static Validate ParseCode(
SourceMarkupLoader owner,
NativeXmlReader xmlReader,
ManagedXmlReader xmlReader,
Parser.CodeType codeType)
{
Validate validate = null;
@@ -348,9 +349,9 @@ namespace Microsoft.Iris.Markup
return validate;
}
private static void ReportError(NativeXmlReader xmlReader, string message) => ErrorManager.ReportError(xmlReader.LineNumber, xmlReader.LinePosition, message);
private static void ReportError(ManagedXmlReader xmlReader, string message) => ErrorManager.ReportError(xmlReader.LineNumber, xmlReader.LinePosition, message);
private static void ReportError(NativeXmlReader xmlReader, string message, object param) => ErrorManager.ReportError(xmlReader.LineNumber, xmlReader.LinePosition, message, param);
private static void ReportError(ManagedXmlReader xmlReader, string message, object param) => ErrorManager.ReportError(xmlReader.LineNumber, xmlReader.LinePosition, message, param);
private enum CodeType
{
@@ -12,25 +12,51 @@ namespace Microsoft.Iris.Markup
{
internal class SSLexUnicodeBufferConsumer : SSLexConsumer
{
private unsafe char* _buffer;
private char[] _buffer;
private uint _length;
private string _prefix;
public SSLexUnicodeBufferConsumer(IntPtr buffer, uint length)
public SSLexUnicodeBufferConsumer(char[] buffer, uint length)
: this(buffer, length, "")
{
}
public unsafe SSLexUnicodeBufferConsumer(IntPtr buffer, uint length, string prefix)
public SSLexUnicodeBufferConsumer(IntPtr intPtr, uint length)
: this(intPtr, length, "")
{
_buffer = (char*)buffer.ToPointer();
}
public unsafe SSLexUnicodeBufferConsumer(char[] buffer, uint length, string prefix)
{
_buffer = buffer;
_length = length;
_prefix = prefix;
}
public unsafe SSLexUnicodeBufferConsumer(IntPtr intPtr, uint length, string prefix)
{
_buffer = new char[length];
System.Runtime.InteropServices.Marshal.Copy(intPtr, _buffer, 0, (int)length);
_length = length;
_prefix = prefix;
}
public override bool getNext() => GetCharAt(m_index, out m_current);
public override unsafe string getSubstring(int start, int length) => start + length <= _prefix.Length ? _prefix.Substring(start, length) : NativeApi.PtrToStringUni(new IntPtr(_buffer + start - _prefix.Length), length);
public override unsafe string getSubstring(int start, int length)
{
if (start + length <= _prefix.Length)
{
return _prefix.Substring(start, length);
}
else
{
fixed (char* ptr = _buffer)
{
return NativeApi.PtrToStringUni(new IntPtr(ptr + start - _prefix.Length), length);
}
}
}
public unsafe bool GetCharAt(int position, out char ch)
{