Пример #1
0
        private static void InsertVmBody(object sender, ModuleWriterEventArgs e)
        {
            var mainSection = new PESection(".Nasha0", 0x60000020);
            var references  = new PESection(".Nasha1", 0x60000020);
            var opcodesList = new PESection(".Nasha2", 0x60000020);

            var writer = (ModuleWriterBase)sender;

            TokenGetter.Writer = writer;
            if (e.Event != ModuleWriterEvent.MDMemberDefRidsAllocated)
            {
                return;
            }

            var translated     = Settings.Translated;
            var bufferedLength = 0;
            var nasha0         = new byte[0];

            for (int i = 0; i < translated.Count; ++i)
            {
                var methodBytes = Settings.Serialize(translated[i]);
                Array.Resize(ref nasha0, nasha0.Length + methodBytes.Count);
                methodBytes.CopyTo(nasha0, bufferedLength);
                Settings.Translated[i].Method.Body.Instructions.Last(x => x.OpCode == OpCodes.Ldc_I4).Operand = bufferedLength;
                bufferedLength += methodBytes.Count;
            }

            mainSection.Add(new ByteArrayChunk(Compress(nasha0)), 1);
            references.Add(new ByteArrayChunk(Compress(Settings.TranslateReference().ToArray())), 1);
            opcodesList.Add(new ByteArrayChunk(NashaSettings.TranslateOpcodes().ToArray()), 1);

            NashaSections.Add(mainSection);
            NashaSections.Add(references);
            NashaSections.Add(opcodesList);
        }
Пример #2
0
        public byte[] Serializer(NashaSettings settings, NashaInstruction instruction)
        {
            var buf = new byte[5];

            buf[0] = (byte)NashaOpcode.Stloc;
            Array.Copy(BitConverter.GetBytes((int)instruction.Operand), 0, buf, 1, 4);
            return(buf);
        }
Пример #3
0
        public byte[] Serializer(NashaSettings settings, NashaInstruction instruction)
        {
            var buf = new byte[9];

            buf[0] = (byte)NashaOpcodes.LdcR8.ShuffledID;
            Array.Copy(BitConverter.GetBytes((double)instruction.Operand), 0, buf, 1, 8);
            return(buf);
        }
Пример #4
0
        public byte[] Serializer(NashaSettings settings, NashaInstruction instruction)
        {
            var buf = new byte[7];

            buf[0] = (byte)NashaOpcodes.Newobj.ShuffledID;
            var(referenceId, method) = (Tuple <short, IMethod>)instruction.Operand;
            Array.Copy(BitConverter.GetBytes(referenceId), 0, buf, 1, 2);
            Array.Copy(BitConverter.GetBytes(TokenGetter.GetMdToken(method)), 0, buf, 3, 4);
            return(buf);
        }
Пример #5
0
        public byte[] Serializer(NashaSettings settings, NashaInstruction instruction)
        {
            var buf = new byte[7];

            buf[0] = (byte)NashaOpcodes.Newarr.ShuffledID;
            var(referenceId, type) = (Tuple <short, ITypeDefOrRef>)instruction.Operand;
            Array.Copy(BitConverter.GetBytes(referenceId), 0, buf, 1, 2);
            Array.Copy(BitConverter.GetBytes(TokenGetter.GetTypeToken(type)), 0, buf, 3, 4);
            return(buf);
        }
Пример #6
0
        public byte[] Serializer(NashaSettings settings, NashaInstruction instruction)
        {
            var buf = new byte[7];

            buf[0] = (byte)NashaOpcode.Stsfld;
            var(referenceId, field) = (Tuple <short, IField>)instruction.Operand;
            Array.Copy(BitConverter.GetBytes(referenceId), 0, buf, 1, 2);
            Array.Copy(BitConverter.GetBytes(TokenGetter.GetFieldToken(field)), 0, buf, 3, 4);
            return(buf);
        }
Пример #7
0
        public NashaInstruction Translation(NashaSettings settings, MethodDef method, int index)
        {
            var operand = ((IMethod)method.Body.Instructions[index].Operand);
            var asmName = operand.Module.Assembly.FullName;

            if (!settings.References.Contains(asmName))
            {
                settings.References.Add(asmName);
            }
            return(new NashaInstruction(NashaOpcodes.Newobj, new Tuple <short, IMethod>((short)settings.References.IndexOf(asmName), operand)));
        }
Пример #8
0
        public byte[] Serializer(NashaSettings settings, NashaInstruction instruction)
        {
            var str = Encoding.UTF8.GetBytes(instruction.Operand.ToString());

            var buf = new byte[5 + str.Length];

            buf[0] = (byte)NashaOpcode.Ldstr;
            Array.Copy(BitConverter.GetBytes(str.Length), 0, buf, 1, 4);
            Array.Copy(str, 0, buf, 5, str.Length);
            return(buf);
        }
Пример #9
0
        public byte[] Serializer(NashaSettings settings, NashaInstruction instruction)
        {
            var buf = new byte[8];

            buf[0] = (byte)NashaOpcodes.Ldsfld.ShuffledID;

            var(referenceId, field, isGeneric) = (Tuple <short, IField, bool>)instruction.Operand;
            Array.Copy(BitConverter.GetBytes(isGeneric), 0, buf, 1, 1);
            Array.Copy(BitConverter.GetBytes(referenceId), 0, buf, 2, 2);
            Array.Copy(BitConverter.GetBytes(TokenGetter.GetFieldToken(field)), 0, buf, 4, 4);
            return(buf);
        }
Пример #10
0
        public NashaInstruction Translation(NashaSettings settings, MethodDef method, int index)
        {
            var field        = ((IField)method.Body.Instructions[index].Operand);
            var assemblyName = field.Module.Assembly.FullName;

            if (!settings.References.Contains(assemblyName))
            {
                settings.References.Add(assemblyName);
            }

            return(new NashaInstruction(NashaOpcodes.Ldsfld, new Tuple <short, IField, bool>((short)settings.References.IndexOf(assemblyName), field, field.FieldSig.ContainsGenericParameter)));
        }
Пример #11
0
 public NashaInstruction Translation(NashaSettings settings, MethodDef method, int index)
 {
     return(new NashaInstruction(NashaOpcode.Stloc, method.Body.Variables.IndexOf(method.Body.Instructions[index].GetLocal(method.Body.Variables))));
 }
Пример #12
0
 public byte[] Serializer(NashaSettings body, NashaInstruction instruction)
 {
     return(new[] { (byte)NashaOpcode.Ret });
 }
Пример #13
0
 public NashaInstruction Translation(NashaSettings settings, MethodDef method, int index)
 {
     return(new NashaInstruction(NashaOpcodes.Mul));
 }
Пример #14
0
 public byte[] Serializer(NashaSettings settings, NashaInstruction instruction)
 {
     return(new[] { (byte)NashaOpcode.Nop });
 }
Пример #15
0
        public NashaInstruction Translation(NashaSettings settings, MethodDef method, int index)
        {
            var arg = method.Body.Instructions[index].GetParameterIndex();

            return(new NashaInstruction(NashaOpcodes.Ldarg, arg));
        }
Пример #16
0
 public NashaInstruction Translation(NashaSettings settings, MethodDef method, int index)
 {
     return(new NashaInstruction(NashaOpcodes.LdcR8, (double)method.Body.Instructions[index].Operand));
 }
Пример #17
0
 public NashaInstruction Translation(NashaSettings settings, MethodDef method, int index)
 {
     return(new NashaInstruction(NashaOpcode.LdcI4, method.Body.Instructions[index].GetLdcI4Value()));
 }
Пример #18
0
 public NashaInstruction Translation(NashaSettings body, MethodDef method, int index)
 {
     return(new NashaInstruction(NashaOpcode.Ret));
 }
Пример #19
0
 public byte[] Serializer(NashaSettings settings, NashaInstruction instruction)
 {
     return(new[] { (byte)NashaOpcodes.Mul.ShuffledID });
 }
Пример #20
0
 public NashaInstruction Translation(NashaSettings settings, MethodDef method, int index)
 {
     return(new NashaInstruction(NashaOpcodes.Brtrue, OffsetHelper.Get(method.Body.Instructions.IndexOf((Instruction)method.Body.Instructions[index].Operand))));
 }