Exemplo n.º 1
0
        void Clear_works()
        {
            InstructionList list;

            list = new InstructionList();
            Assert.Equal(0, list.Count);
            Assert.Equal(0, list.Capacity);
            list.Clear();
            Assert.Equal(0, list.Count);
            Assert.Equal(0, list.Capacity);

            var instructions = GetInstructions();

            list = new InstructionList(instructions);
            Assert.Equal(instructions.Length, list.Count);
            Assert.Equal(instructions.Length, list.Capacity);
            list.Clear();
            Assert.Equal(0, list.Count);
            Assert.Equal(instructions.Length, list.Capacity);
        }
        public LadderProgram ReadExecutable(string content, string projectName)
        {
            if (content.IndexOf("@laddermic.com") == -1)
            {
                throw new Exception("Executable has no Ladder content!");
            }

            OperationCode   opCode      = OperationCode.None;
            int             countOfEnds = 0;
            int             lineIndex   = 0;
            Address         address;
            AddressTypeEnum addressType;
            int             index = 0;


            LadderProgram program = new LadderProgram();

            program.Name   = projectName;
            program.device = DeviceFactory.CreateNewDevice();
            addressingServices.AlocateIOAddressing(program.device);
            addressingServices.AlocateAddressingMemoryAndTimerAndCounter(program, program.addressing.ListMemoryAddress, AddressTypeEnum.DigitalMemory, 10);
            addressingServices.AlocateAddressingMemoryAndTimerAndCounter(program, program.addressing.ListTimerAddress, AddressTypeEnum.DigitalMemoryTimer, 10);
            addressingServices.AlocateAddressingMemoryAndTimerAndCounter(program, program.addressing.ListCounterAddress, AddressTypeEnum.DigitalMemoryCounter, 10);
            lineIndex = program.InsertLineAtEnd(new Line());

            for (int position = content.IndexOf("@laddermic.com") + 15; position < content.Length; position++)
            {
                opCode = (OperationCode)Convert.ToChar(content.Substring(position, 1));

                switch (opCode)
                {
                case OperationCode.None:
                    countOfEnds++;
                    break;

                case OperationCode.LineEnd:
                    countOfEnds++;
                    if ((OperationCode)Convert.ToChar(content.Substring(position + 1, 1)) != OperationCode.None)
                    {
                        lineIndex = program.InsertLineAtEnd(new Line());
                    }
                    break;

                case OperationCode.NormallyOpenContact:
                case OperationCode.NormallyClosedContact:
                    countOfEnds = 0;
                    //Instruction instruction = new Instruction((OperationCode)opCode);
                    Instruction instruction = InstructionFactory.createInstruction(opCode);

                    addressType = (AddressTypeEnum)Convert.ToChar(content.Substring(position + 1, 1));
                    index       = (Int32)Convert.ToChar(content.Substring(position + 2, 1));
                    address     = addressingServices.Find(addressType, index);
                    if (address == null)
                    {
                        program.device.Pins[index - 1].Type = addressType;
                        addressingServices.RealocatePinAddressesByPinTypesFromDevice(program.device);
                        addressingServices.AlocateIOAddressing(program.device);
                        address = addressingServices.Find(addressType, index);
                    }
                    instruction.SetOperand(0, address);

                    position += 2;
                    program.Lines[lineIndex].Instructions.Add(instruction);
                    break;

                case OperationCode.OutputCoil:
                case OperationCode.Reset:
                    countOfEnds = 0;
                    {
                        InstructionList instructions = new InstructionList();
                        instructions.Add(InstructionFactory.createInstruction(opCode));
                        addressType = (AddressTypeEnum)Convert.ToChar(content.Substring(position + 1, 1));
                        index       = (Int32)Convert.ToChar(content.Substring(position + 2, 1));
                        address     = addressingServices.Find(addressType, index);
                        if (address == null)
                        {
                            program.device.Pins[index - 1].Type = addressType;
                            addressingServices.RealocatePinAddressesByPinTypesFromDevice(program.device);
                            addressingServices.AlocateIOAddressing(program.device);
                            address = addressingServices.Find(addressType, index);
                        }
                        instructions[instructions.Count - 1].SetOperand(0, address);
                        position += 2;
                        lineServices.InsertToOutputs(program.Lines[lineIndex], instructions);
                        instructions.Clear();
                    }
                    break;

                case OperationCode.ParallelBranchBegin:
                case OperationCode.ParallelBranchEnd:
                case OperationCode.ParallelBranchNext:
                    countOfEnds = 0;
                    program.Lines[lineIndex].Instructions.Add(InstructionFactory.createInstruction(opCode));
                    break;

                case OperationCode.Counter:
                    countOfEnds = 0;
                    {
                        InstructionList    instructions = new InstructionList();
                        CounterInstruction counter      = (CounterInstruction)InstructionFactory.createInstruction(opCode);
                        counter.SetAddress(addressingServices.Find(AddressTypeEnum.DigitalMemoryCounter, (Int32)Convert.ToChar(content.Substring(position + 1, 1))));
                        counter.setBoxType((int)Convert.ToChar(content.Substring(position + 2, 1)));
                        counter.setPreset((int)Convert.ToChar(content.Substring(position + 3, 1)));
                        instructions.Add(counter);
                        //instructions.Add(InstructionFactory.createInstruction(opCode));
                        //instructions[instructions.Count - 1].SetOperand(0, addressingServices.Find(AddressTypeEnum.DigitalMemoryCounter, (Int32)Convert.ToChar(content.Substring(position + 1, 1))));
                        //((Address)instructions[instructions.Count - 1].GetOperand(0)).Counter.Type = (Int32)Convert.ToChar(content.Substring(position + 2, 1));
                        //((Address)instructions[instructions.Count - 1].GetOperand(0)).Counter.Preset = (Int32)Convert.ToChar(content.Substring(position + 3, 1));

                        //instructions[instructions.Count - 1].SetOperand(1, ((Address)instructions[instructions.Count - 1].GetOperand(0)).Counter.Type);
                        //instructions[instructions.Count - 1].SetOperand(2, ((Address)instructions[instructions.Count - 1].GetOperand(0)).Counter.Preset);
                        position += 3;
                        lineServices.InsertToOutputs(program.Lines[lineIndex], instructions);
                        instructions.Clear();
                    }
                    break;

                case OperationCode.Timer:
                    countOfEnds = 0;
                    {
                        InstructionList instructions = new InstructionList();
                        instructions.Add(InstructionFactory.createInstruction(opCode));
                        instructions[instructions.Count - 1].SetOperand(0, addressingServices.Find(AddressTypeEnum.DigitalMemoryTimer, (Int32)Convert.ToChar(content.Substring(position + 1, 1))));
                        ((Address)instructions[instructions.Count - 1].GetOperand(0)).Timer.Type     = (Int32)Convert.ToChar(content.Substring(position + 2, 1));
                        ((Address)instructions[instructions.Count - 1].GetOperand(0)).Timer.TimeBase = (Int32)Convert.ToChar(content.Substring(position + 3, 1));
                        ((Address)instructions[instructions.Count - 1].GetOperand(0)).Timer.Preset   = (Int32)Convert.ToChar(content.Substring(position + 4, 1));

                        instructions[instructions.Count - 1].SetOperand(1, ((Address)instructions[instructions.Count - 1].GetOperand(0)).Timer.Type);
                        instructions[instructions.Count - 1].SetOperand(2, ((Address)instructions[instructions.Count - 1].GetOperand(0)).Timer.Preset);
                        instructions[instructions.Count - 1].SetOperand(4, ((Address)instructions[instructions.Count - 1].GetOperand(0)).Timer.TimeBase);

                        position += 4;
                        lineServices.InsertToOutputs(program.Lines[lineIndex], instructions);
                        instructions.Clear();
                    }
                    break;
                }

                /// end of codes
                if (countOfEnds >= 2)
                {
                    MicIntegrationServices p = new MicIntegrationServices();
                    //p.CreateFile("opcode.txt", content.Substring(content.IndexOf("@laddermic.com"), i - content.IndexOf("@laddermic.com") + 1));
                    //position = content.Length;
                    break;
                }
            }
            return(program);
        }
Exemplo n.º 3
0
        //Alternative (Less Stable) Key Find Method
        void OnCreateMutexCalled(uint Caller)
        {
            var  pCaller        = (void *)Caller;
            bool FromMainModule = Info.Value.AddressIsContained(pCaller);

            Log.Trace($"CreateMutex Called At: 0x{Caller:X8} ({(FromMainModule ? "Main Module" : "Secundary Module")})");
            if (!FromMainModule)
            {
                return;
            }

            MInterceptor.Uninstall();

            Helper = new DiasmHelper(pCaller);

            var List = new InstructionList();

            bool InMissmatch = false;
            bool InEnd       = false;
            int  MovCount    = 0;
            int  Tries       = 0;

            while (Tries <= 500)
            {
                Tries++;
                if (MovCount > 7)
                {
                    InEnd = true;
                }

                var Instruction = Helper.Diassembly();

                bool IsMov = Instruction.Code == Code.Mov_rm32_imm32;

                if (IsMov)
                {
                    Log.Trace($"{Instruction} at ({Instruction.IP:X8})");
                }

                if (IsMov)
                {
                    MovCount++;
                }

                if (!IsMov)
                {
                    if (InMissmatch)
                    {
                        MovCount    = 0;
                        InMissmatch = false;
                    }
                    else
                    {
                        InMissmatch = true;
                    }
                }
                else
                {
                    InMissmatch = false;
                }

                if (InEnd && !IsMov)
                {
                    break;
                }

                if (!InEnd && IsMov)
                {
                    List.Add(Instruction);
                }
                else if (!InEnd && !InMissmatch)
                {
                    List.Clear();
                }
            }

            if (Tries >= 500)
            {
                return;
            }

            byte[] KBuffer = new byte[0x20];
            for (int i = 0; i < List.Count; i++)
            {
                BitConverter.GetBytes(List[i].Immediate32).CopyTo(KBuffer, i * 4);
            }

            OnKeyIntercepted(KBuffer);
        }