Exemple #1
0
        private void optimize(VMPreparedProgram prg)
        {
            //UPGRADE_NOTE: There is an untranslated Statement.  Please refer to original code. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1153'"

            List<VMPreparedCommand> commands = prg.Commands;
            //UPGRADE_ISSUE: The following fragment of code could not be parsed and was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1156'"
            foreach (VMPreparedCommand cmd in commands)
            {
                switch (cmd.OpCode)
                {

                    case VMCommands.VM_MOV:
                        cmd.OpCode = cmd.IsByteMode ? VMCommands.VM_MOVB : VMCommands.VM_MOVD;
                        continue;

                    case VMCommands.VM_CMP:
                        cmd.OpCode = cmd.IsByteMode ? VMCommands.VM_CMPB : VMCommands.VM_CMPD;
                        continue;
                }
                if ((VMCmdFlags.VM_CmdFlags[(int)cmd.OpCode] & VMCmdFlags.VMCF_CHFLAGS) == 0)
                {
                    continue;
                }
                bool flagsRequired = false;

                for (int i = commands.IndexOf(cmd) + 1; i < commands.Count; i++)
                {
                    int flags = VMCmdFlags.VM_CmdFlags[(int)commands[i].OpCode];
                    if ((flags & (VMCmdFlags.VMCF_JUMP | VMCmdFlags.VMCF_PROC | VMCmdFlags.VMCF_USEFLAGS)) != 0)
                    {
                        flagsRequired = true;
                        break;
                    }
                    if ((flags & VMCmdFlags.VMCF_CHFLAGS) != 0)
                    {
                        break;
                    }
                }
                if (flagsRequired)
                {
                    continue;
                }
                switch (cmd.OpCode)
                {

                    case VMCommands.VM_ADD:
                        cmd.OpCode = cmd.IsByteMode ? VMCommands.VM_ADDB : VMCommands.VM_ADDD;
                        continue;

                    case VMCommands.VM_SUB:
                        cmd.OpCode = cmd.IsByteMode ? VMCommands.VM_SUBB : VMCommands.VM_SUBD;
                        continue;

                    case VMCommands.VM_INC:
                        cmd.OpCode = cmd.IsByteMode ? VMCommands.VM_INCB : VMCommands.VM_INCD;
                        continue;

                    case VMCommands.VM_DEC:
                        cmd.OpCode = cmd.IsByteMode ? VMCommands.VM_DECB : VMCommands.VM_DECD;
                        continue;

                    case VMCommands.VM_NEG:
                        cmd.OpCode = cmd.IsByteMode ? VMCommands.VM_NEGB : VMCommands.VM_NEGD;
                        continue;
                }
            }
        }
Exemple #2
0
        public void execute(VMPreparedProgram prg)
        {
            for (int i = 0; i < prg.InitR.Length; i++)
            // memcpy(R,Prg->InitR,sizeof(Prg->InitR));
            {
                R[i] = prg.InitR[i];
            }

            long globalSize = Math.Min(prg.GlobalData.Count, VM_GLOBALMEMSIZE) & unchecked((int)0xffFFffFF);
            if (globalSize != 0)
            {
                for (int i = 0; i < globalSize; i++)
                // memcpy(Mem+VM_GLOBALMEMADDR,&Prg->GlobalData[0],GlobalSize);
                {
                    Mem[VM_GLOBALMEMADDR + i] = prg.GlobalData[i];
                }
            }
            long staticSize = Math.Min(prg.StaticData.Count, VM_GLOBALMEMSIZE - globalSize) & unchecked((int)0xffFFffFF);
            if (staticSize != 0)
            {
                for (int i = 0; i < staticSize; i++)
                // memcpy(Mem+VM_GLOBALMEMADDR+GlobalSize,&Prg->StaticData[0],StaticSize);
                {
                    Mem[VM_GLOBALMEMADDR + (int)globalSize + i] = prg.StaticData[i];
                }
            }
            R[7] = VM_MEMSIZE;
            flags = 0;

            //UPGRADE_NOTE: There is an untranslated Statement.  Please refer to original code. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1153'"
            List<VMPreparedCommand> preparedCode = prg.AltCommands.Count != 0 ? prg
                .AltCommands
                : prg.Commands;

            if (!ExecuteCode(preparedCode, prg.CommandCount))
            {
                preparedCode[0].OpCode = VMCommands.VM_RET;
            }
            int newBlockPos = GetValue(false, Mem, VM_GLOBALMEMADDR + 0x20) & VM_MEMMASK;
            int newBlockSize = GetValue(false, Mem, VM_GLOBALMEMADDR + 0x1c) & VM_MEMMASK;
            if ((newBlockPos + newBlockSize) >= VM_MEMSIZE)
            {
                newBlockPos = 0;
                newBlockSize = 0;
            }

            prg.FilteredDataOffset = newBlockPos;
            prg.FilteredDataSize = newBlockSize;

            prg.GlobalData.Clear();

            int dataSize = System.Math.Min(GetValue(false, Mem, VM_GLOBALMEMADDR + 0x30), VM_GLOBALMEMSIZE - VM_FIXEDGLOBALSIZE);
            if (dataSize != 0)
            {
                //prg.GlobalData.Clear();
                // ->GlobalData.Add(dataSize+VM_FIXEDGLOBALSIZE);

                for (int i = 0; i < dataSize + VM_FIXEDGLOBALSIZE; i++)
                // memcpy(&Prg->GlobalData[0],&Mem[VM_GLOBALMEMADDR],DataSize+VM_FIXEDGLOBALSIZE);
                {
                    prg.GlobalData[i] = Mem[VM_GLOBALMEMADDR + i];
                }
            }
        }
Exemple #3
0
        public void prepare(byte[] code, int codeSize, VMPreparedProgram prg)
        {
            InitBitInput();
            int cpLength = System.Math.Min(MAX_SIZE, codeSize);
            // memcpy(inBuf,Code,Min(CodeSize,BitInput::MAX_SIZE));
#if !PORTABLE
            Buffer.BlockCopy(code, 0, InBuf, 0, cpLength);
#else
            Array.Copy(code, 0, InBuf, 0, cpLength);
#endif
            byte xorSum = 0;
            for (int i = 1; i < codeSize; i++)
            {
                xorSum ^= code[i];
            }

            AddBits(8);

            prg.CommandCount = 0;
            if (xorSum == code[0])
            {
                VMStandardFilters filterType = IsStandardFilter(code, codeSize);
                if (filterType != VMStandardFilters.VMSF_NONE)
                {

                    VMPreparedCommand curCmd = new VMPreparedCommand();
                    curCmd.OpCode = VMCommands.VM_STANDARD;
                    curCmd.Op1.Data = (int)filterType;
                    curCmd.Op1.Type = VMOpType.VM_OPNONE;
                    curCmd.Op2.Type = VMOpType.VM_OPNONE;
                    codeSize = 0;
                    prg.Commands.Add(curCmd);
                    prg.CommandCount = prg.CommandCount + 1;
                    // TODO
                    // curCmd->Op1.Data=FilterType;
                    // >>>>>> CurCmd->Op1.Addr=&CurCmd->Op1.Data; <<<<<<<<<< not set
                    // do i need to ?
                    // >>>>>> CurCmd->Op2.Addr=&CurCmd->Op2.Data; <<<<<<<<<< "
                    // CurCmd->Op1.Type=CurCmd->Op2.Type=VM_OPNONE;
                    // CodeSize=0;
                }
                int dataFlag = GetBits();
                AddBits(1);

                // Read static data contained in DB operators. This data cannot be
                // changed,
                // it is a part of VM code, not a filter parameter.

                if ((dataFlag & 0x8000) != 0)
                {
                    long dataSize = (long)((long)ReadData(this) & 0xffFFffFFL + 1L);
                    for (int i = 0; inAddr < codeSize && i < dataSize; i++)
                    {
                        prg.StaticData.Add((byte)(GetBits() >> 8));
                        AddBits(8);
                    }
                }

                while (inAddr < codeSize)
                {
                    VMPreparedCommand curCmd = new VMPreparedCommand();
                    int data = GetBits();
                    if ((data & 0x8000) == 0)
                    {
                        curCmd.OpCode = (VMCommands)((data >> 12));
                        AddBits(4);
                    }
                    else
                    {
                        curCmd.OpCode = (VMCommands)((data >> 10) - 24);
                        AddBits(6);
                    }
                    if ((VMCmdFlags.VM_CmdFlags[(int)curCmd.OpCode] & VMCmdFlags.VMCF_BYTEMODE) != 0)
                    {
                        curCmd.IsByteMode = (GetBits() >> 15) == 1 ? true : false;
                        AddBits(1);
                    }
                    else
                    {
                        curCmd.IsByteMode = false;
                    }
                    curCmd.Op1.Type = VMOpType.VM_OPNONE;
                    curCmd.Op2.Type = VMOpType.VM_OPNONE;

                    int opNum = (VMCmdFlags.VM_CmdFlags[(int)curCmd.OpCode] & VMCmdFlags.VMCF_OPMASK);
                    // TODO >>> CurCmd->Op1.Addr=CurCmd->Op2.Addr=NULL; <<<???
                    if (opNum > 0)
                    {
                        decodeArg(curCmd.Op1, curCmd.IsByteMode);
                        if (opNum == 2)
                            decodeArg(curCmd.Op2, curCmd.IsByteMode);
                        else
                        {
                            if (curCmd.Op1.Type == VMOpType.VM_OPINT && (VMCmdFlags.VM_CmdFlags[(int)curCmd.OpCode] & (VMCmdFlags.VMCF_JUMP | VMCmdFlags.VMCF_PROC)) != 0)
                            {
                                int distance = curCmd.Op1.Data;
                                if (distance >= 256)
                                    distance -= 256;
                                else
                                {
                                    if (distance >= 136)
                                    {
                                        distance -= 264;
                                    }
                                    else
                                    {
                                        if (distance >= 16)
                                        {
                                            distance -= 8;
                                        }
                                        else
                                        {
                                            if (distance >= 8)
                                            {
                                                distance -= 16;
                                            }
                                        }
                                    }
                                    distance += prg.CommandCount;
                                }
                                curCmd.Op1.Data = distance;
                            }
                        }
                    }
                    prg.CommandCount = (prg.CommandCount + 1);
                    prg.Commands.Add(curCmd);
                }
            }
            VMPreparedCommand curCmd2 = new VMPreparedCommand();
            curCmd2.OpCode = VMCommands.VM_RET;
            // TODO CurCmd->Op1.Addr=&CurCmd->Op1.Data;
            // CurCmd->Op2.Addr=&CurCmd->Op2.Data;
            curCmd2.Op1.Type = VMOpType.VM_OPNONE;
            curCmd2.Op2.Type = VMOpType.VM_OPNONE;

            // for (int i=0;i<prg.CmdCount;i++)
            // {
            // VM_PreparedCommand *Cmd=&Prg->Cmd[I];
            // if (Cmd->Op1.Addr==NULL)
            // Cmd->Op1.Addr=&Cmd->Op1.Data;
            // if (Cmd->Op2.Addr==NULL)
            // Cmd->Op2.Addr=&Cmd->Op2.Data;
            // }

            prg.Commands.Add(curCmd2);
            prg.CommandCount = prg.CommandCount + 1;
            // #ifdef VM_OPTIMIZE
            if (codeSize != 0)
            {
                optimize(prg);
            }
        }
 private void ExecuteCode(VMPreparedProgram Prg)
 {
     if (Prg.GlobalData.Count > 0)
     {
         // Prg->InitR[6]=int64to32(WrittenFileSize);
         Prg.InitR[6] = (int)(writtenFileSize);
         // rarVM.SetLowEndianValue((uint
         // *)&Prg->GlobalData[0x24],int64to32(WrittenFileSize));
         rarVM.SetLowEndianValue(Prg.GlobalData, 0x24, (int)writtenFileSize);
         // rarVM.SetLowEndianValue((uint
         // *)&Prg->GlobalData[0x28],int64to32(WrittenFileSize>>32));
         rarVM.SetLowEndianValue(Prg.GlobalData, 0x28, (int)(Utilities.URShift(writtenFileSize, 32)));
         rarVM.execute(Prg);
     }
 }