Example #1
0
        // VB initializes the handlers in the property setter, where it first removes the handler
        // from the previous control, and then adds the handler to the new control.
        void initVbEventHandlers(FieldDefinitionAndDeclaringTypeDict <FieldDef> ourFields, MethodDefinitionAndDeclaringTypeDict <MethodDef> ourMethods)
        {
            var checker = NameChecker;

            foreach (var propDef in type.AllProperties)
            {
                var setterDef = propDef.SetMethod;
                if (setterDef == null)
                {
                    continue;
                }

                string eventName;
                var    handler = getVbHandler(setterDef.MethodDefinition, out eventName);
                if (handler == null)
                {
                    continue;
                }
                var handlerDef = ourMethods.find(handler);
                if (handlerDef == null)
                {
                    continue;
                }

                if (!checker.isValidEventName(eventName))
                {
                    continue;
                }

                memberInfos.method(handlerDef).suggestedName = string.Format("{0}_{1}", memberInfos.prop(propDef).newName, eventName);
            }
        }
Example #2
0
        static short[] findKey(MethodDefinition initMethod, FieldDefinition keyField)
        {
            var fields = new FieldDefinitionAndDeclaringTypeDict <bool>();

            fields.add(keyField, true);
            return(findKey(initMethod, fields));
        }
Example #3
0
        static internal IEnumerable <FieldDefinition> getFields(TypeDefinition type)
        {
            var typeFields = new FieldDefinitionAndDeclaringTypeDict <FieldDefinition>();

            foreach (var field in type.Fields)
            {
                typeFields.add(field, field);
            }
            var realFields = new Dictionary <FieldDefinition, bool>();

            foreach (var method in type.Methods)
            {
                if (method.Body == null)
                {
                    continue;
                }
                foreach (var instr in method.Body.Instructions)
                {
                    var fieldRef = instr.Operand as FieldReference;
                    if (fieldRef == null)
                    {
                        continue;
                    }
                    var field = typeFields.find(fieldRef);
                    if (field == null)
                    {
                        continue;
                    }
                    realFields[field] = true;
                }
            }
            return(realFields.Keys);
        }
Example #4
0
        public void initialize()
        {
            foreach (var kv in getMovedTypes())
            {
                var structType = kv.Key;
                structToOwners.add(structType, kv.Value);

                foreach (var ownerType in kv.Value)
                {
                    foreach (var ownerField in ownerType.Fields)
                    {
                        if (DotNetUtils.getType(module, ownerField.FieldType) != structType)
                        {
                            continue;
                        }
                        structFieldsToFix.add(ownerField, true);
                        break;
                    }

                    var fieldsDict = new FieldDefinitionAndDeclaringTypeDict <FieldDefinition>();
                    typeToFieldsDict.add(ownerType, fieldsDict);
                    foreach (var structField in structType.Fields)
                    {
                        var newField = DotNetUtils.createFieldDefinition(structField.Name, structField.Attributes, structField.FieldType);
                        ownerType.Fields.Add(newField);
                        fieldsDict.add(structField, newField);
                    }
                }
            }
        }
Example #5
0
        static short[] findKey(MethodDefinition initMethod, FieldDefinitionAndDeclaringTypeDict <bool> fields)
        {
            var instrs = initMethod.Body.Instructions;

            for (int i = 0; i < instrs.Count - 2; i++)
            {
                var ldci4 = instrs[i];
                if (!DotNetUtils.isLdcI4(ldci4))
                {
                    continue;
                }
                var newarr = instrs[i + 1];
                if (newarr.OpCode.Code != Code.Newarr)
                {
                    continue;
                }
                if (newarr.Operand.ToString() != "System.Char")
                {
                    continue;
                }

                var stloc = instrs[i + 2];
                if (!DotNetUtils.isStloc(stloc))
                {
                    continue;
                }
                var local = DotNetUtils.getLocalVar(initMethod.Body.Variables, stloc);

                int startInitIndex = i;
                i++;
                var array = ArrayFinder.getInitializedInt16Array(DotNetUtils.getLdcI4Value(ldci4), initMethod, ref i);
                if (array == null)
                {
                    continue;
                }

                var field = getStoreField(initMethod, startInitIndex, local);
                if (field == null)
                {
                    continue;
                }
                if (fields.find(field))
                {
                    return(array);
                }
            }

            return(null);
        }
Example #6
0
        // Find the string decrypter string offset value or null if none found
        int?findOffsetValue(MethodDefinition method)
        {
            var fieldDict = new FieldDefinitionAndDeclaringTypeDict <FieldReference>();

            foreach (var field in method.DeclaringType.Fields)
            {
                fieldDict.add(field, field);
            }

            var offsetField = findOffsetField(method);

            if (offsetField == null)
            {
                return(null);
            }

            return(findOffsetValue(method, (FieldDefinition)fieldDict.find(offsetField), fieldDict));
        }
Example #7
0
        public void initializeEventHandlerNames()
        {
            var ourFields = new FieldDefinitionAndDeclaringTypeDict <FieldDef>();

            foreach (var fieldDef in type.AllFields)
            {
                ourFields.add(fieldDef.FieldDefinition, fieldDef);
            }
            var ourMethods = new MethodDefinitionAndDeclaringTypeDict <MethodDef>();

            foreach (var methodDef in type.AllMethods)
            {
                ourMethods.add(methodDef.MethodDefinition, methodDef);
            }

            initVbEventHandlers(ourFields, ourMethods);
            initFieldEventHandlers(ourFields, ourMethods);
            initTypeEventHandlers(ourFields, ourMethods);
        }
Example #8
0
        int?findOffsetValue(MethodDefinition method, FieldDefinition offsetField, FieldDefinitionAndDeclaringTypeDict <FieldReference> fields)
        {
            var instructions = method.Body.Instructions;

            for (int i = 0; i <= instructions.Count - 2; i++)
            {
                var ldstr = instructions[i];
                if (ldstr.OpCode.Code != Code.Ldstr)
                {
                    continue;
                }
                var stringVal = ldstr.Operand as string;
                if (stringVal == null)
                {
                    continue;
                }

                var stsfld = instructions[i + 1];
                if (stsfld.OpCode.Code != Code.Stsfld)
                {
                    continue;
                }
                var field = stsfld.Operand as FieldReference;
                if (field == null || fields.find(field) != offsetField)
                {
                    continue;
                }

                int value;
                if (!int.TryParse(stringVal, System.Globalization.NumberStyles.Integer, null, out value))
                {
                    continue;
                }

                return(value);
            }

            return(null);
        }
Example #9
0
            static FieldDefinitionAndDeclaringTypeDict <bool> getFields(MethodDefinition method)
            {
                var fields = new FieldDefinitionAndDeclaringTypeDict <bool>();

                foreach (var instr in method.Body.Instructions)
                {
                    if (instr.OpCode.Code != Code.Ldsfld && instr.OpCode.Code != Code.Stsfld)
                    {
                        continue;
                    }
                    var field = instr.Operand as FieldDefinition;
                    if (field == null)
                    {
                        continue;
                    }
                    if (field.DeclaringType != method.DeclaringType)
                    {
                        continue;
                    }
                    fields.add(field, true);
                }
                return(fields);
            }
Example #10
0
            public bool initialize()
            {
                if (!findMagic(Method, out arg1, out arg2, out magic))
                {
                    return(false);
                }

                fields = getFields(Method);
                if (fields == null)
                {
                    return(false);
                }

                arrayInfo = getArrayInfo(cctor);
                if (arrayInfo == null)
                {
                    return(false);
                }

                if (arrayInfo.initField.InitialValue.Length % 2 == 1)
                {
                    return(false);
                }
                encryptedData = new ushort[arrayInfo.initField.InitialValue.Length / 2];
                Buffer.BlockCopy(arrayInfo.initField.InitialValue, 0, encryptedData, 0, arrayInfo.initField.InitialValue.Length);

                isTrial  = !DeobUtils.hasInteger(Method, 0xFFF0);
                keyShift = findKeyShift(cctor);
                key      = findKey();
                if (key == null || key.Length == 0)
                {
                    return(false);
                }

                return(true);
            }
Example #11
0
        void initTypeEventHandlers(FieldDefinitionAndDeclaringTypeDict<FieldDef> ourFields, MethodDefinitionAndDeclaringTypeDict<MethodDef> ourMethods)
        {
            var checker = NameChecker;

            foreach (var methodDef in type.AllMethods) {
                if (methodDef.MethodDefinition.Body == null)
                    continue;
                if (methodDef.MethodDefinition.IsStatic)
                    continue;
                var method = methodDef.MethodDefinition;
                var instructions = method.Body.Instructions;
                for (int i = 0; i < instructions.Count - 5; i++) {
                    // ldarg.0
                    // ldarg.0 / dup
                    // ldarg.0 / dup
                    // ldvirtftn handler
                    // newobj event handler ctor
                    // call add_Xyz

                    if (DotNetUtils.getArgIndex(instructions[i]) != 0)
                        continue;
                    int index = i + 1;

                    if (!isThisOrDup(instructions[index++]))
                        continue;
                    MethodReference handler;
                    if (instructions[index].OpCode.Code == Code.Ldftn) {
                        handler = instructions[index++].Operand as MethodReference;
                    }
                    else {
                        if (!isThisOrDup(instructions[index++]))
                            continue;
                        var instr = instructions[index++];
                        if (instr.OpCode.Code != Code.Ldvirtftn)
                            continue;
                        handler = instr.Operand as MethodReference;
                    }
                    if (handler == null)
                        continue;
                    var handlerDef = ourMethods.find(handler);
                    if (handlerDef == null)
                        continue;

                    var newobj = instructions[index++];
                    if (newobj.OpCode.Code != Code.Newobj)
                        continue;
                    if (!isEventHandlerCtor(newobj.Operand as MethodReference))
                        continue;

                    var call = instructions[index++];
                    if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt)
                        continue;
                    var addMethod = call.Operand as MethodReference;
                    if (addMethod == null)
                        continue;
                    if (!Utils.StartsWith(addMethod.Name, "add_", StringComparison.Ordinal))
                        continue;

                    var eventName = addMethod.Name.Substring(4);
                    if (!checker.isValidEventName(eventName))
                        continue;

                    memberInfos.method(handlerDef).suggestedName = string.Format("{0}_{1}", newName, eventName);
                }
            }
        }
Example #12
0
        void initializeWindowsFormsFieldsAndProps()
        {
            var checker = NameChecker;

            var ourFields = new FieldDefinitionAndDeclaringTypeDict<FieldDef>();
            foreach (var fieldDef in type.AllFields)
                ourFields.add(fieldDef.FieldDefinition, fieldDef);
            var ourMethods = new MethodDefinitionAndDeclaringTypeDict<MethodDef>();
            foreach (var methodDef in type.AllMethods)
                ourMethods.add(methodDef.MethodDefinition, methodDef);

            foreach (var methodDef in type.AllMethods) {
                if (methodDef.MethodDefinition.Body == null)
                    continue;
                if (methodDef.MethodDefinition.IsStatic || methodDef.MethodDefinition.IsVirtual)
                    continue;
                var instructions = methodDef.MethodDefinition.Body.Instructions;
                for (int i = 2; i < instructions.Count; i++) {
                    var call = instructions[i];
                    if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt)
                        continue;
                    if (!isWindowsFormsSetNameMethod(call.Operand as MethodReference))
                        continue;

                    var ldstr = instructions[i - 1];
                    if (ldstr.OpCode.Code != Code.Ldstr)
                        continue;
                    var fieldName = ldstr.Operand as string;
                    if (fieldName == null || !checker.isValidFieldName(fieldName))
                        continue;

                    var instr = instructions[i - 2];
                    FieldReference fieldRef = null;
                    if (instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt) {
                        var calledMethod = instr.Operand as MethodReference;
                        if (calledMethod == null)
                            continue;
                        var calledMethodDef = ourMethods.find(calledMethod);
                        if (calledMethodDef == null)
                            continue;
                        fieldRef = getFieldReference(calledMethodDef.MethodDefinition);

                        var propDef = calledMethodDef.Property;
                        if (propDef == null)
                            continue;

                        memberInfos.prop(propDef).suggestedName = fieldName;
                        fieldName = "_" + fieldName;
                    }
                    else if (instr.OpCode.Code == Code.Ldfld) {
                        fieldRef = instr.Operand as FieldReference;
                    }

                    if (fieldRef == null)
                        continue;
                    var fieldDef = ourFields.find(fieldRef);
                    if (fieldDef == null)
                        continue;
                    var fieldInfo = memberInfos.field(fieldDef);

                    if (fieldInfo.renamed)
                        continue;

                    fieldInfo.suggestedName = variableNameState.getNewFieldName(fieldInfo.oldName, new NameCreator2(fieldName));
                }
            }
        }
Example #13
0
        void initFieldEventHandlers(FieldDefinitionAndDeclaringTypeDict<FieldDef> ourFields, MethodDefinitionAndDeclaringTypeDict<MethodDef> ourMethods)
        {
            var checker = NameChecker;

            foreach (var methodDef in type.AllMethods) {
                if (methodDef.MethodDefinition.Body == null)
                    continue;
                if (methodDef.MethodDefinition.IsStatic)
                    continue;
                var instructions = methodDef.MethodDefinition.Body.Instructions;
                for (int i = 0; i < instructions.Count - 6; i++) {
                    // We're looking for this code pattern:
                    //	ldarg.0
                    //	ldfld field
                    //	ldarg.0
                    //	ldftn method / ldarg.0 + ldvirtftn
                    //	newobj event_handler_ctor
                    //	callvirt add_SomeEvent

                    if (DotNetUtils.getArgIndex(instructions[i]) != 0)
                        continue;
                    int index = i + 1;

                    var ldfld = instructions[index++];
                    if (ldfld.OpCode.Code != Code.Ldfld)
                        continue;
                    var fieldRef = ldfld.Operand as FieldReference;
                    if (fieldRef == null)
                        continue;
                    var fieldDef = ourFields.find(fieldRef);
                    if (fieldDef == null)
                        continue;

                    if (DotNetUtils.getArgIndex(instructions[index++]) != 0)
                        continue;

                    MethodReference methodRef;
                    var instr = instructions[index + 1];
                    if (instr.OpCode.Code == Code.Ldvirtftn) {
                        if (!isThisOrDup(instructions[index++]))
                            continue;
                        var ldvirtftn = instructions[index++];
                        methodRef = ldvirtftn.Operand as MethodReference;
                    }
                    else {
                        var ldftn = instructions[index++];
                        if (ldftn.OpCode.Code != Code.Ldftn)
                            continue;
                        methodRef = ldftn.Operand as MethodReference;
                    }
                    if (methodRef == null)
                        continue;
                    var handlerMethod = ourMethods.find(methodRef);
                    if (handlerMethod == null)
                        continue;

                    var newobj = instructions[index++];
                    if (newobj.OpCode.Code != Code.Newobj)
                        continue;
                    if (!isEventHandlerCtor(newobj.Operand as MethodReference))
                        continue;

                    var call = instructions[index++];
                    if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt)
                        continue;
                    var addHandler = call.Operand as MethodReference;
                    if (addHandler == null)
                        continue;
                    if (!Utils.StartsWith(addHandler.Name, "add_", StringComparison.Ordinal))
                        continue;

                    var eventName = addHandler.Name.Substring(4);
                    if (!checker.isValidEventName(eventName))
                        continue;

                    memberInfos.method(handlerMethod).suggestedName = string.Format("{0}_{1}", memberInfos.field(fieldDef).newName, eventName);
                }
            }
        }
Example #14
0
        public void initialize()
        {
            foreach (var kv in getMovedTypes()) {
                var structType = kv.Key;
                structToOwners.add(structType, kv.Value);

                foreach (var ownerType in kv.Value) {
                    foreach (var ownerField in ownerType.Fields) {
                        if (DotNetUtils.getType(module, ownerField.FieldType) != structType)
                            continue;
                        structFieldsToFix.add(ownerField, true);
                        break;
                    }

                    var fieldsDict = new FieldDefinitionAndDeclaringTypeDict<FieldDefinition>();
                    typeToFieldsDict.add(ownerType, fieldsDict);
                    foreach (var structField in structType.Fields) {
                        var newField = DotNetUtils.createFieldDefinition(structField.Name, structField.Attributes, structField.FieldType);
                        ownerType.Fields.Add(newField);
                        fieldsDict.add(structField, newField);
                    }
                }
            }
        }
Example #15
0
 static FieldDefinitionAndDeclaringTypeDict<bool> getFields(MethodDefinition method)
 {
     var fields = new FieldDefinitionAndDeclaringTypeDict<bool>();
     foreach (var instr in method.Body.Instructions) {
         if (instr.OpCode.Code != Code.Ldsfld && instr.OpCode.Code != Code.Stsfld)
             continue;
         var field = instr.Operand as FieldDefinition;
         if (field == null)
             continue;
         if (field.DeclaringType != method.DeclaringType)
             continue;
         fields.add(field, true);
     }
     return fields;
 }
Example #16
0
            public bool initialize()
            {
                if (!findMagic(Method, out arg1, out arg2, out magic))
                    return false;

                fields = getFields(Method);
                if (fields == null)
                    return false;

                arrayInfo = getArrayInfo(cctor);
                if (arrayInfo == null)
                    return false;

                if (arrayInfo.initField.InitialValue.Length % 2 == 1)
                    return false;
                encryptedData = new ushort[arrayInfo.initField.InitialValue.Length / 2];
                Buffer.BlockCopy(arrayInfo.initField.InitialValue, 0, encryptedData, 0, arrayInfo.initField.InitialValue.Length);

                isTrial = !DeobUtils.hasInteger(Method, 0xFFF0);
                keyShift = findKeyShift(cctor);
                key = findKey();
                if (key == null || key.Length == 0)
                    return false;

                return true;
            }
Example #17
0
 static IEnumerable<FieldDefinition> getFields(TypeDefinition type)
 {
     var typeFields = new FieldDefinitionAndDeclaringTypeDict<FieldDefinition>();
     foreach (var field in type.Fields)
         typeFields.add(field, field);
     var realFields = new Dictionary<FieldDefinition, bool>();
     foreach (var method in type.Methods) {
         if (method.Body == null)
             continue;
         foreach (var instr in method.Body.Instructions) {
             var fieldRef = instr.Operand as FieldReference;
             if (fieldRef == null)
                 continue;
             var field = typeFields.find(fieldRef);
             if (field == null)
                 continue;
             realFields[field] = true;
         }
     }
     return realFields.Keys;
 }
Example #18
0
 static short[] findKey(MethodDefinition initMethod, FieldDefinition keyField)
 {
     var fields = new FieldDefinitionAndDeclaringTypeDict<bool>();
     fields.add(keyField, true);
     return findKey(initMethod, fields);
 }
Example #19
0
        void initFieldEventHandlers(FieldDefinitionAndDeclaringTypeDict <FieldDef> ourFields, MethodDefinitionAndDeclaringTypeDict <MethodDef> ourMethods)
        {
            var checker = NameChecker;

            foreach (var methodDef in type.AllMethods)
            {
                if (methodDef.MethodDefinition.Body == null)
                {
                    continue;
                }
                if (methodDef.MethodDefinition.IsStatic)
                {
                    continue;
                }
                var instructions = methodDef.MethodDefinition.Body.Instructions;
                for (int i = 0; i < instructions.Count - 6; i++)
                {
                    // We're looking for this code pattern:
                    //	ldarg.0
                    //	ldfld field
                    //	ldarg.0
                    //	ldftn method / ldarg.0 + ldvirtftn
                    //	newobj event_handler_ctor
                    //	callvirt add_SomeEvent

                    if (DotNetUtils.getArgIndex(instructions[i]) != 0)
                    {
                        continue;
                    }
                    int index = i + 1;

                    var ldfld = instructions[index++];
                    if (ldfld.OpCode.Code != Code.Ldfld)
                    {
                        continue;
                    }
                    var fieldRef = ldfld.Operand as FieldReference;
                    if (fieldRef == null)
                    {
                        continue;
                    }
                    var fieldDef = ourFields.find(fieldRef);
                    if (fieldDef == null)
                    {
                        continue;
                    }

                    if (DotNetUtils.getArgIndex(instructions[index++]) != 0)
                    {
                        continue;
                    }

                    MethodReference methodRef;
                    var             instr = instructions[index + 1];
                    if (instr.OpCode.Code == Code.Ldvirtftn)
                    {
                        if (!isThisOrDup(instructions[index++]))
                        {
                            continue;
                        }
                        var ldvirtftn = instructions[index++];
                        methodRef = ldvirtftn.Operand as MethodReference;
                    }
                    else
                    {
                        var ldftn = instructions[index++];
                        if (ldftn.OpCode.Code != Code.Ldftn)
                        {
                            continue;
                        }
                        methodRef = ldftn.Operand as MethodReference;
                    }
                    if (methodRef == null)
                    {
                        continue;
                    }
                    var handlerMethod = ourMethods.find(methodRef);
                    if (handlerMethod == null)
                    {
                        continue;
                    }

                    var newobj = instructions[index++];
                    if (newobj.OpCode.Code != Code.Newobj)
                    {
                        continue;
                    }
                    if (!isEventHandlerCtor(newobj.Operand as MethodReference))
                    {
                        continue;
                    }

                    var call = instructions[index++];
                    if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt)
                    {
                        continue;
                    }
                    var addHandler = call.Operand as MethodReference;
                    if (addHandler == null)
                    {
                        continue;
                    }
                    if (!Utils.StartsWith(addHandler.Name, "add_", StringComparison.Ordinal))
                    {
                        continue;
                    }

                    var eventName = addHandler.Name.Substring(4);
                    if (!checker.isValidEventName(eventName))
                    {
                        continue;
                    }

                    memberInfos.method(handlerMethod).suggestedName = string.Format("{0}_{1}", memberInfos.field(fieldDef).newName, eventName);
                }
            }
        }
Example #20
0
        // Find the string decrypter string offset value or null if none found
        int? findOffsetValue(MethodDefinition method)
        {
            var fieldDict = new FieldDefinitionAndDeclaringTypeDict<FieldReference>();
            foreach (var field in method.DeclaringType.Fields)
                fieldDict.add(field, field);

            var offsetField = findOffsetField(method);
            if (offsetField == null)
                return null;

            return findOffsetValue(method, (FieldDefinition)fieldDict.find(offsetField), fieldDict);
        }
Example #21
0
        void initializeWindowsFormsFieldsAndProps()
        {
            var checker = NameChecker;

            var ourFields = new FieldDefinitionAndDeclaringTypeDict <FieldDef>();

            foreach (var fieldDef in type.AllFields)
            {
                ourFields.add(fieldDef.FieldDefinition, fieldDef);
            }
            var ourMethods = new MethodDefinitionAndDeclaringTypeDict <MethodDef>();

            foreach (var methodDef in type.AllMethods)
            {
                ourMethods.add(methodDef.MethodDefinition, methodDef);
            }

            foreach (var methodDef in type.AllMethods)
            {
                if (methodDef.MethodDefinition.Body == null)
                {
                    continue;
                }
                if (methodDef.MethodDefinition.IsStatic || methodDef.MethodDefinition.IsVirtual)
                {
                    continue;
                }
                var instructions = methodDef.MethodDefinition.Body.Instructions;
                for (int i = 2; i < instructions.Count; i++)
                {
                    var call = instructions[i];
                    if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt)
                    {
                        continue;
                    }
                    if (!isWindowsFormsSetNameMethod(call.Operand as MethodReference))
                    {
                        continue;
                    }

                    var ldstr = instructions[i - 1];
                    if (ldstr.OpCode.Code != Code.Ldstr)
                    {
                        continue;
                    }
                    var fieldName = ldstr.Operand as string;
                    if (fieldName == null || !checker.isValidFieldName(fieldName))
                    {
                        continue;
                    }

                    var            instr    = instructions[i - 2];
                    FieldReference fieldRef = null;
                    if (instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt)
                    {
                        var calledMethod = instr.Operand as MethodReference;
                        if (calledMethod == null)
                        {
                            continue;
                        }
                        var calledMethodDef = ourMethods.find(calledMethod);
                        if (calledMethodDef == null)
                        {
                            continue;
                        }
                        fieldRef = getFieldReference(calledMethodDef.MethodDefinition);

                        var propDef = calledMethodDef.Property;
                        if (propDef == null)
                        {
                            continue;
                        }

                        memberInfos.prop(propDef).suggestedName = fieldName;
                        fieldName = "_" + fieldName;
                    }
                    else if (instr.OpCode.Code == Code.Ldfld)
                    {
                        fieldRef = instr.Operand as FieldReference;
                    }

                    if (fieldRef == null)
                    {
                        continue;
                    }
                    var fieldDef = ourFields.find(fieldRef);
                    if (fieldDef == null)
                    {
                        continue;
                    }
                    var fieldInfo = memberInfos.field(fieldDef);

                    if (fieldInfo.renamed)
                    {
                        continue;
                    }

                    fieldInfo.suggestedName = variableNameState.getNewFieldName(fieldInfo.oldName, new NameCreator2(fieldName));
                }
            }
        }
Example #22
0
        void initTypeEventHandlers(FieldDefinitionAndDeclaringTypeDict <FieldDef> ourFields, MethodDefinitionAndDeclaringTypeDict <MethodDef> ourMethods)
        {
            var checker = NameChecker;

            foreach (var methodDef in type.AllMethods)
            {
                if (methodDef.MethodDefinition.Body == null)
                {
                    continue;
                }
                if (methodDef.MethodDefinition.IsStatic)
                {
                    continue;
                }
                var method       = methodDef.MethodDefinition;
                var instructions = method.Body.Instructions;
                for (int i = 0; i < instructions.Count - 5; i++)
                {
                    // ldarg.0
                    // ldarg.0 / dup
                    // ldarg.0 / dup
                    // ldvirtftn handler
                    // newobj event handler ctor
                    // call add_Xyz

                    if (DotNetUtils.getArgIndex(instructions[i]) != 0)
                    {
                        continue;
                    }
                    int index = i + 1;

                    if (!isThisOrDup(instructions[index++]))
                    {
                        continue;
                    }
                    MethodReference handler;
                    if (instructions[index].OpCode.Code == Code.Ldftn)
                    {
                        handler = instructions[index++].Operand as MethodReference;
                    }
                    else
                    {
                        if (!isThisOrDup(instructions[index++]))
                        {
                            continue;
                        }
                        var instr = instructions[index++];
                        if (instr.OpCode.Code != Code.Ldvirtftn)
                        {
                            continue;
                        }
                        handler = instr.Operand as MethodReference;
                    }
                    if (handler == null)
                    {
                        continue;
                    }
                    var handlerDef = ourMethods.find(handler);
                    if (handlerDef == null)
                    {
                        continue;
                    }

                    var newobj = instructions[index++];
                    if (newobj.OpCode.Code != Code.Newobj)
                    {
                        continue;
                    }
                    if (!isEventHandlerCtor(newobj.Operand as MethodReference))
                    {
                        continue;
                    }

                    var call = instructions[index++];
                    if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt)
                    {
                        continue;
                    }
                    var addMethod = call.Operand as MethodReference;
                    if (addMethod == null)
                    {
                        continue;
                    }
                    if (!Utils.StartsWith(addMethod.Name, "add_", StringComparison.Ordinal))
                    {
                        continue;
                    }

                    var eventName = addMethod.Name.Substring(4);
                    if (!checker.isValidEventName(eventName))
                    {
                        continue;
                    }

                    memberInfos.method(handlerDef).suggestedName = string.Format("{0}_{1}", newName, eventName);
                }
            }
        }
Example #23
0
        // VB initializes the handlers in the property setter, where it first removes the handler
        // from the previous control, and then adds the handler to the new control.
        void initVbEventHandlers(FieldDefinitionAndDeclaringTypeDict<FieldDef> ourFields, MethodDefinitionAndDeclaringTypeDict<MethodDef> ourMethods)
        {
            var checker = NameChecker;

            foreach (var propDef in type.AllProperties) {
                var setterDef = propDef.SetMethod;
                if (setterDef == null)
                    continue;

                string eventName;
                var handler = getVbHandler(setterDef.MethodDefinition, out eventName);
                if (handler == null)
                    continue;
                var handlerDef = ourMethods.find(handler);
                if (handlerDef == null)
                    continue;

                if (!checker.isValidEventName(eventName))
                    continue;

                memberInfos.method(handlerDef).suggestedName = string.Format("{0}_{1}", memberInfos.prop(propDef).newName, eventName);
            }
        }
Example #24
0
        static short[] findKey(MethodDefinition initMethod, FieldDefinitionAndDeclaringTypeDict<bool> fields)
        {
            var instrs = initMethod.Body.Instructions;
            for (int i = 0; i < instrs.Count - 2; i++) {
                var ldci4 = instrs[i];
                if (!DotNetUtils.isLdcI4(ldci4))
                    continue;
                var newarr = instrs[i + 1];
                if (newarr.OpCode.Code != Code.Newarr)
                    continue;
                if (newarr.Operand.ToString() != "System.Char")
                    continue;

                var stloc = instrs[i + 2];
                if (!DotNetUtils.isStloc(stloc))
                    continue;
                var local = DotNetUtils.getLocalVar(initMethod.Body.Variables, stloc);

                int startInitIndex = i;
                i++;
                var array = ArrayFinder.getInitializedInt16Array(DotNetUtils.getLdcI4Value(ldci4), initMethod, ref i);
                if (array == null)
                    continue;

                var field = getStoreField(initMethod, startInitIndex, local);
                if (field == null)
                    continue;
                if (fields.find(field))
                    return array;
            }

            return null;
        }
Example #25
0
        public void initializeEventHandlerNames()
        {
            var ourFields = new FieldDefinitionAndDeclaringTypeDict<FieldDef>();
            foreach (var fieldDef in type.AllFields)
                ourFields.add(fieldDef.FieldDefinition, fieldDef);
            var ourMethods = new MethodDefinitionAndDeclaringTypeDict<MethodDef>();
            foreach (var methodDef in type.AllMethods)
                ourMethods.add(methodDef.MethodDefinition, methodDef);

            initVbEventHandlers(ourFields, ourMethods);
            initFieldEventHandlers(ourFields, ourMethods);
            initTypeEventHandlers(ourFields, ourMethods);
        }
Example #26
0
        int? findOffsetValue(MethodDefinition method, FieldDefinition offsetField, FieldDefinitionAndDeclaringTypeDict<FieldReference> fields)
        {
            var instructions = method.Body.Instructions;
            for (int i = 0; i <= instructions.Count - 2; i++) {
                var ldstr = instructions[i];
                if (ldstr.OpCode.Code != Code.Ldstr)
                    continue;
                var stringVal = ldstr.Operand as string;
                if (stringVal == null)
                    continue;

                var stsfld = instructions[i + 1];
                if (stsfld.OpCode.Code != Code.Stsfld)
                    continue;
                var field = stsfld.Operand as FieldReference;
                if (field == null || fields.find(field) != offsetField)
                    continue;

                int value;
                if (!int.TryParse(stringVal, System.Globalization.NumberStyles.Integer, null, out value))
                    continue;

                return value;
            }

            return null;
        }