Ejemplo n.º 1
0
 static object AskForDef(string title, ITokenResolver resolver) => MsgBox.Instance.Ask(dnSpy_Resources.GoToToken_Label, null, title, s => {
     string error;
     uint token    = SimpleTypeConverter.ParseUInt32(s, uint.MinValue, uint.MaxValue, out error);
     var memberRef = resolver.ResolveToken(token);
     var member    = ResolveDef(memberRef);
     return(member);
 }, s => {
     string error;
     uint token = SimpleTypeConverter.ParseUInt32(s, uint.MinValue, uint.MaxValue, out error);
     if (!string.IsNullOrEmpty(error))
     {
         return(error);
     }
     var memberRef = resolver.ResolveToken(token);
     var member    = ResolveDef(memberRef);
     if (memberRef == null)
     {
         return(string.Format(dnSpy_Resources.GoToToken_InvalidToken, token));
     }
     else if (member == null)
     {
         return(string.Format(dnSpy_Resources.GoToToken_CouldNotResolve, token));
     }
     return(string.Empty);
 });
Ejemplo n.º 2
0
        public string GetString(out string s)
        {
            string error;

            s = SimpleTypeConverter.ParseString(text, true, out error);
            return(error);
        }
Ejemplo n.º 3
0
        public override void Execute(DnHexBox dnHexBox)
        {
            var sel = dnHexBox.Selection;

            if (sel == null)
            {
                return;
            }

            var res = messageBoxService.Ask <byte?>(dnSpy_Contracts_DnSpy_Resources.FillSelection_Label, "0xFF", dnSpy_Contracts_DnSpy_Resources.FillSelection_Title, s => {
                string error;
                byte b = SimpleTypeConverter.ParseByte(s, byte.MinValue, byte.MaxValue, out error);
                return(string.IsNullOrEmpty(error) ? b : (byte?)null);
            }, s => {
                string error;
                byte b = SimpleTypeConverter.ParseByte(s, byte.MinValue, byte.MaxValue, out error);
                return(error);
            });

            if (res == null)
            {
                return;
            }

            dnHexBox.FillBytes(sel.Value.StartOffset, sel.Value.EndOffset, res.Value);
            dnHexBox.Selection = null;
        }
Ejemplo n.º 4
0
 public bool TryGetCode(out int code, out string error)
 {
     code = SimpleTypeConverter.ParseInt32(StringValue, int.MinValue, int.MaxValue, out error);
     if (error != null)
     {
         code = (int)SimpleTypeConverter.ParseUInt32(StringValue, uint.MinValue, uint.MaxValue, out error);
     }
     return(error == null);
 }
Ejemplo n.º 5
0
 public bool TryGetCode(out int code, [NotNullWhen(false)] out string?error)
 {
     code = SimpleTypeConverter.ParseInt32(StringValue, int.MinValue, int.MaxValue, out error);
     if (!(error is null))
     {
         code = (int)SimpleTypeConverter.ParseUInt32(StringValue, uint.MinValue, uint.MaxValue, out error);
     }
     return(error is null);
 }
Ejemplo n.º 6
0
 protected override string ConvertToValue(out uint value)
 {
     value = SimpleTypeConverter.ParseUInt32(StringValue, Min, Max, out string error);
     if (error != null)
     {
         return(error);
     }
     return(CheckOffsetToken(value) ? null : dnSpy_Resources.GoToMetadataInvalidOffsetOrToken);
 }
Ejemplo n.º 7
0
        public override void FillSelection()
        {
            if (HexView.Selection.IsEmpty)
            {
                return;
            }

            var res = messageBoxService.Ask <byte?>(dnSpy_Resources.FillSelection_Label, "0xFF", dnSpy_Resources.FillSelection_Title, s => {
                string error;
                byte b = SimpleTypeConverter.ParseByte(s, byte.MinValue, byte.MaxValue, out error);
                return(string.IsNullOrEmpty(error) ? b : (byte?)null);
            }, s => {
                string error;
                byte b = SimpleTypeConverter.ParseByte(s, byte.MinValue, byte.MaxValue, out error);
                return(error);
            });

            if (res == null)
            {
                return;
            }

            try {
                var  span = HexView.Selection.StreamSelectionSpan;
                var  data = new byte[span.IsFull ? ulong.MaxValue : span.Length.ToUInt64()];
                byte b    = res.Value;
                if (b != 0)
                {
                    if (data.LongLength <= int.MaxValue)
                    {
                        for (int i = 0; i < data.Length; i++)
                        {
                            data[i] = b;
                        }
                    }
                    else
                    {
                        for (long i = 0; i < data.LongLength; i++)
                        {
                            data[i] = b;
                        }
                    }
                }
                HexView.Buffer.Replace(span.Start, data);
            }
            catch (ArithmeticException) {
                messageBoxService.Show("Out of memory");
            }
            catch (OutOfMemoryException) {
                messageBoxService.Show("Out of memory");
            }
            HexView.Selection.Clear();
        }
Ejemplo n.º 8
0
        /// <inheritdoc/>
        protected override string?ConvertToValue(out ulong value)
        {
            long v = SimpleTypeConverter.ParseInt64(StringValue, long.MinValue, long.MaxValue, out var error);

            if (error is null)
            {
                value = (ulong)v;
                return(null);
            }
            value = SimpleTypeConverter.ParseUInt64(StringValue, ulong.MinValue, ulong.MaxValue, out error);
            return(error);
        }
Ejemplo n.º 9
0
        string ReadStringsHeap(int index)
        {
            var mdt       = (MetaDataTableNode)TreeNode.Parent.Data;
            var tableInfo = mdt.TableInfo;
            var s         = SimpleTypeConverter.ToString(mdt.MetaDataTableVM.ReadStringsHeap(ReadFieldValue(mdt.Document, tableInfo.Columns[index])), false);

            Debug.Assert(s.Length >= 2);
            if (s.Length < 2)
            {
                return(s);
            }
            return(s.Substring(1, s.Length - 2));
        }
Ejemplo n.º 10
0
        bool SelectMember(IAppCommandLineArgs args)
        {
            if (string.IsNullOrEmpty(args.SelectMember))
            {
                return(false);
            }

            string error;
            uint   token = SimpleTypeConverter.ParseUInt32(args.SelectMember, uint.MinValue, uint.MaxValue, out error);

            if (string.IsNullOrEmpty(error))
            {
                var mod    = GetLoadedFiles(args).FirstOrDefault();
                var member = mod?.ResolveToken(token);
                if (member == null)
                {
                    return(false);
                }
                documentTabService.FollowReference(member);
                return(true);
            }

            foreach (var mod in GetLoadedFiles(args))
            {
                const string XMLDOC_NS_PREFIX = "N:";
                bool         isNamespace      = args.SelectMember.StartsWith(XMLDOC_NS_PREFIX);
                if (isNamespace)
                {
                    var ns      = args.SelectMember.Substring(XMLDOC_NS_PREFIX.Length);
                    var modNode = documentTabService.DocumentTreeView.FindNode(mod);
                    var nsNode  = modNode == null ? null : documentTabService.DocumentTreeView.FindNamespaceNode(modNode.Document, ns);
                    if (nsNode != null)
                    {
                        documentTabService.FollowReference(nsNode);
                        return(true);
                    }
                }
                else
                {
                    var member = XmlDocKeyProvider.FindMemberByKey(mod, args.SelectMember);
                    if (member != null)
                    {
                        documentTabService.FollowReference(member);
                        return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 11
0
 static MetaDataTableRecordVM Ask(string title, MDTableContext context)
 {
     return(MsgBox.Instance.Ask(dnSpy_AsmEditor_Resources.GoToMetaDataTableRow_RID, null, title, s => {
         string error;
         uint rid = SimpleTypeConverter.ParseUInt32(s, 1, context.MetaDataTableVM.Rows, out error);
         if (!string.IsNullOrEmpty(error))
         {
             return null;
         }
         return context.MetaDataTableVM.Get((int)(rid - 1));
     }, s => {
         string error;
         uint rid = SimpleTypeConverter.ParseUInt32(s, 1, context.MetaDataTableVM.Rows, out error);
         if (!string.IsNullOrEmpty(error))
         {
             return error;
         }
         if (rid == 0 || rid > context.MetaDataTableVM.Rows)
         {
             return string.Format(dnSpy_AsmEditor_Resources.GoToRowIdentifier_InvalidRowIdentifier, rid);
         }
         return string.Empty;
     }));
 }
Ejemplo n.º 12
0
        void WriteStringId(BamlContext ctx, ushort id)
        {
            string str;

            if (id > 0x7fff)
            {
                str = ctx.KnownThings.Strings((short)-id);
            }
            else if (ctx.StringIdMap.ContainsKey(id))
            {
                str = ctx.StringIdMap[id].Value;
            }
            else
            {
                str = null;
            }
            string reference = null;

            if (str is not null)
            {
                reference = SimpleTypeConverter.ToString(str, true);
            }
            output.Write($"0x{id:x4}", BamlToolTipReference.Create(reference), DecompilerReferenceFlags.Local, BoxedTextColor.Number);
        }
Ejemplo n.º 13
0
 void WriteQuotedString(ITextColorWriter output, string text, string s)
 {
     output.Write(BoxedTextColor.Text, text);
     output.WriteSpace();
     output.Write(BoxedTextColor.String, SimpleTypeConverter.ToString(s, true));
 }
Ejemplo n.º 14
0
        /// <inheritdoc/>
        public virtual void WriteShort(IDecompilerOutput output, IDecompiler decompiler, bool showOffset)
        {
            decompiler.WriteCommentBegin(output, true);
            output.WriteOffsetComment(this, showOffset);
            const string LTR = "\u200E";

            output.Write(NameUtilities.CleanName(Name) + LTR, this, DecompilerReferenceFlags.Local | DecompilerReferenceFlags.Definition, BoxedTextColor.Comment);
            string extra = null;

            switch (Resource.ResourceType)
            {
            case ResourceType.AssemblyLinked:
                extra = ((AssemblyLinkedResource)Resource).Assembly.FullName;
                break;

            case ResourceType.Linked:
                var file = ((LinkedResource)Resource).File;
                extra = string.Format("{0}, {1}, {2}", file.Name, file.ContainsNoMetaData ? "ContainsNoMetaData" : "ContainsMetaData", SimpleTypeConverter.ByteArrayToString(file.HashValue));
                break;

            case ResourceType.Embedded:
                extra = string.Format(dnSpy_Contracts_DnSpy_Resources.NumberOfBytes, ((EmbeddedResource)Resource).Data.Length);
                break;
            }
            output.Write(string.Format(" ({0}{1}, {2})", extra == null ? string.Empty : string.Format("{0}, ", extra), Resource.ResourceType, Resource.Attributes), BoxedTextColor.Comment);
            decompiler.WriteCommentEnd(output, true);
            output.WriteLine();
        }
Ejemplo n.º 15
0
        static ulong?ParseIntegerConstant(CorElementType etype, string c, out string error)
        {
            error = null;
            long  smin, smax;
            ulong max;

            switch (etype)
            {
            case CorElementType.Boolean:
                smin = 0;
                smax = 1;
                max  = 1;
                break;

            case CorElementType.I:
                if (IntPtr.Size == 4)
                {
                    goto case CorElementType.I4;
                }
                goto case CorElementType.I8;

            case CorElementType.U:
            case CorElementType.Ptr:
            case CorElementType.FnPtr:
                if (UIntPtr.Size == 4)
                {
                    goto case CorElementType.U4;
                }
                goto case CorElementType.U8;

            case CorElementType.I1:
            case CorElementType.U1:
                smin = sbyte.MinValue;
                smax = sbyte.MaxValue;
                max  = byte.MaxValue;
                break;

            case CorElementType.I2:
            case CorElementType.U2:
                smin = short.MinValue;
                smax = short.MaxValue;
                max  = ushort.MaxValue;
                break;

            case CorElementType.I4:
            case CorElementType.U4:
                smin = int.MinValue;
                smax = int.MaxValue;
                max  = uint.MaxValue;
                break;

            case CorElementType.I8:
            case CorElementType.U8:
                smin = long.MinValue;
                smax = long.MaxValue;
                max  = ulong.MaxValue;
                break;

            default:
                return(null);
            }
            ulong v = SimpleTypeConverter.ParseUInt64(c, 0, max, out error);

            if (string.IsNullOrEmpty(error))
            {
                return(v);
            }

            v = (ulong)SimpleTypeConverter.ParseInt64(c, smin, smax, out error);
            if (string.IsNullOrEmpty(error))
            {
                return(v);
            }

            return(null);
        }
Ejemplo n.º 16
0
 /// <inheritdoc/>
 protected override string OnNewValue(ulong value) => SimpleTypeConverter.ToString(value, ulong.MinValue, ulong.MaxValue, null);
Ejemplo n.º 17
0
        public string GetPrimitiveValue(CorType type, out byte[] bytes)
        {
            bytes = null;
            if (type == null)
            {
                return("Internal error: CorType is null");
            }

            if (type.IsEnum)
            {
                return(GetEnumValue(type, out bytes));
            }

            string error;
            var    etype = type.TryGetPrimitiveType();

            switch (etype)
            {
            case CorElementType.Boolean:
            {
                var value = SimpleTypeConverter.ParseBoolean(text, out error);
                if (!string.IsNullOrEmpty(error))
                {
                    return(error);
                }
                bytes = BitConverter.GetBytes(value);
                return(null);
            }

            case CorElementType.Char:
            {
                var value = SimpleTypeConverter.ParseChar(text, out error);
                if (!string.IsNullOrEmpty(error))
                {
                    return(error);
                }
                bytes = BitConverter.GetBytes(value);
                return(null);
            }

            case CorElementType.R4:
            {
                var value = SimpleTypeConverter.ParseSingle(text, out error);
                if (!string.IsNullOrEmpty(error))
                {
                    return(error);
                }
                bytes = BitConverter.GetBytes(value);
                return(null);
            }

            case CorElementType.R8:
            {
                var value = SimpleTypeConverter.ParseDouble(text, out error);
                if (!string.IsNullOrEmpty(error))
                {
                    return(error);
                }
                bytes = BitConverter.GetBytes(value);
                return(null);
            }

            case CorElementType.Class:
            case CorElementType.ValueType:
                if (type.IsSystemDecimal)
                {
                    var value = SimpleTypeConverter.ParseDecimal(text, out error);
                    if (!string.IsNullOrEmpty(error))
                    {
                        return(error);
                    }
                    bytes = GetBytes(value);
                    return(null);
                }
                return(null);

            case CorElementType.I:
            case CorElementType.U:
            case CorElementType.Ptr:
            case CorElementType.FnPtr:
                if (text.Trim() == "null")
                {
                    bytes = new byte[IntPtr.Size];
                    return(null);
                }
                break;
            }

            ulong?res = ParseIntegerConstant(etype, text, out error);

            if (res == null)
            {
                return(error ?? dnSpy_Debugger_Resources.LocalsEditValue_Error_InvalidNumber);
            }

            switch (etype)
            {
            case CorElementType.I1:
                bytes = new byte[1] {
                    (byte)res.Value
                };
                return(null);

            case CorElementType.U1:
                bytes = new byte[1] {
                    (byte)res.Value
                };
                return(null);

            case CorElementType.I2:
                bytes = BitConverter.GetBytes((short)res.Value);
                return(null);

            case CorElementType.U2:
                bytes = BitConverter.GetBytes((ushort)res.Value);
                return(null);

            case CorElementType.I4:
                bytes = BitConverter.GetBytes((int)res.Value);
                return(null);

            case CorElementType.U4:
                bytes = BitConverter.GetBytes((uint)res.Value);
                return(null);

            case CorElementType.I8:
                bytes = BitConverter.GetBytes((long)res.Value);
                return(null);

            case CorElementType.U8:
                bytes = BitConverter.GetBytes(res.Value);
                return(null);

            case CorElementType.I:
            case CorElementType.U:
            case CorElementType.Ptr:
            case CorElementType.FnPtr:
                if (IntPtr.Size == 4)
                {
                    goto case CorElementType.I4;
                }
                goto case CorElementType.I8;
            }

            return("Unknown number type");
        }
Ejemplo n.º 18
0
        public static string ValueToString(object value, bool addCast)
        {
            if (value == null)
            {
                return("null");
            }

            switch (ModelUtils.GetElementType(value.GetType()))
            {
            case ElementType.Boolean: return(SimpleTypeConverter.ToString((bool)value));

            case ElementType.Char:  return(SimpleTypeConverter.ToString((char)value));

            case ElementType.I1:    return(AddCast(SimpleTypeConverter.ToString((sbyte)value), addCast, value.GetType().FullName));

            case ElementType.U1:    return(AddCast(SimpleTypeConverter.ToString((byte)value), addCast, value.GetType().FullName));

            case ElementType.I2:    return(AddCast(SimpleTypeConverter.ToString((short)value), addCast, value.GetType().FullName));

            case ElementType.U2:    return(AddCast(SimpleTypeConverter.ToString((ushort)value), addCast, value.GetType().FullName));

            case ElementType.I4:    return(AddCast(SimpleTypeConverter.ToString((int)value), addCast, value.GetType().FullName));

            case ElementType.U4:    return(AddCast(SimpleTypeConverter.ToString((uint)value), addCast, value.GetType().FullName));

            case ElementType.I8:    return(AddCast(SimpleTypeConverter.ToString((long)value), addCast, value.GetType().FullName));

            case ElementType.U8:    return(AddCast(SimpleTypeConverter.ToString((ulong)value), addCast, value.GetType().FullName));

            case ElementType.R4:    return(AddCast(SimpleTypeConverter.ToString((float)value), addCast, value.GetType().FullName));

            case ElementType.R8:    return(AddCast(SimpleTypeConverter.ToString((double)value), addCast, value.GetType().FullName));

            case ElementType.String: return(SimpleTypeConverter.ToString((string)value, true));
            }
            if (value is TypeSig)
            {
                return($"typeof({value})");
            }

            var valueType = value.GetType();

            if (value is IList <bool> )
            {
                return(ArrayToString(value, typeof(bool)));
            }
            if (value is IList <char> )
            {
                return(ArrayToString(value, typeof(char)));
            }
            if (value is IList <sbyte> && valueType != typeof(byte[]))
            {
                return(ArrayToString(value, typeof(sbyte)));
            }
            if (value is IList <short> && valueType != typeof(ushort[]))
            {
                return(ArrayToString(value, typeof(short)));
            }
            if (value is IList <int> && valueType != typeof(uint[]))
            {
                return(ArrayToString(value, typeof(int)));
            }
            if (value is IList <long> && valueType != typeof(ulong[]))
            {
                return(ArrayToString(value, typeof(long)));
            }
            if (value is IList <byte> && valueType != typeof(sbyte[]))
            {
                return(ArrayToString(value, typeof(byte)));
            }
            if (value is IList <ushort> && valueType != typeof(short[]))
            {
                return(ArrayToString(value, typeof(ushort)));
            }
            if (value is IList <uint> && valueType != typeof(int[]))
            {
                return(ArrayToString(value, typeof(uint)));
            }
            if (value is IList <ulong> && valueType != typeof(long[]))
            {
                return(ArrayToString(value, typeof(ulong)));
            }
            if (value is IList <float> )
            {
                return(ArrayToString(value, typeof(float)));
            }
            if (value is IList <double> )
            {
                return(ArrayToString(value, typeof(double)));
            }
            if (value is IList <string> )
            {
                return(ArrayToString(value, typeof(string)));
            }
            if (value is IList <TypeSig> )
            {
                return(ArrayToString(value, typeof(Type)));
            }
            if (value is IList <object> )
            {
                return(ArrayToString(value, typeof(object)));
            }

            return(value.ToString());
        }
Ejemplo n.º 19
0
 protected override string OnNewValue(uint value) => SimpleTypeConverter.ToString(value, Min, Max, UseDecimal);
Ejemplo n.º 20
0
        void WriteDefinition(string value, string def = null)
        {
            string str = SimpleTypeConverter.ToString(value, true);

            output.Write(str, BamlToolTipReference.Create(def ?? IdentifierEscaper.Escape(value)), DecompilerReferenceFlags.Local | DecompilerReferenceFlags.Definition, BoxedTextColor.String);
        }
Ejemplo n.º 21
0
 void WriteQuotedString(IDbgTextWriter output, string text, string s)
 {
     output.Write(DbgTextColor.Text, text);
     output.Write(DbgTextColor.Text, " ");
     output.Write(DbgTextColor.String, SimpleTypeConverter.ToString(s, true));
 }
Ejemplo n.º 22
0
        void WriteString(string value)
        {
            string str = SimpleTypeConverter.ToString(value, true);

            output.Write(str, BoxedTextColor.String);
        }