Пример #1
0
 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)(Utility.URShift(writtenFileSize, 32)));
         rarVM.execute(Prg);
     }
 }
Пример #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];
                }
            }
        }
 internal UnpackFilter()
 {
     Program = new VMPreparedProgram();
 }
Пример #4
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);
            }
        }
Пример #5
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;
                }
            }
        }
Пример #6
0
 private void ExecuteCode(VMPreparedProgram Prg)
 {
     if (Prg.GlobalData.Count > 0)
     {
         Prg.InitR[6] = (int) this.writtenFileSize;
         this.rarVM.SetLowEndianValue(Prg.GlobalData, 0x24, (int) this.writtenFileSize);
         this.rarVM.SetLowEndianValue(Prg.GlobalData, 40, (int) Utility.URShift(this.writtenFileSize, 0x20));
         this.rarVM.execute(Prg);
     }
 }
Пример #7
0
        private void optimize(VMPreparedProgram prg)
        {
            List<VMPreparedCommand> commands = prg.Commands;
            foreach (VMPreparedCommand command in commands)
            {
                bool flag;
                switch (command.OpCode)
                {
                    case VMCommands.VM_MOV:
                    {
                        command.OpCode = command.IsByteMode ? VMCommands.VM_MOVB : VMCommands.VM_MOVD;
                        continue;
                    }
                    case VMCommands.VM_CMP:
                    {
                        command.OpCode = command.IsByteMode ? VMCommands.VM_CMPB : VMCommands.VM_CMPD;
                        continue;
                    }
                    default:
                        if ((VMCmdFlags.VM_CmdFlags[(int) command.OpCode] & 0x40) == 0)
                        {
                            continue;
                        }
                        flag = false;
                        for (int i = commands.IndexOf(command) + 1; i < commands.Count; i++)
                        {
                            int num2 = VMCmdFlags.VM_CmdFlags[(int) commands[i].OpCode];
                            if ((num2 & 0x38) != 0)
                            {
                                flag = true;
                                break;
                            }
                            if ((num2 & 0x40) != 0)
                            {
                                break;
                            }
                        }
                        break;
                }
                if (!flag)
                {
                    switch (command.OpCode)
                    {
                        case VMCommands.VM_ADD:
                            command.OpCode = command.IsByteMode ? VMCommands.VM_ADDB : VMCommands.VM_ADDD;
                            break;

                        case VMCommands.VM_SUB:
                            command.OpCode = command.IsByteMode ? VMCommands.VM_SUBB : VMCommands.VM_SUBD;
                            break;

                        case VMCommands.VM_INC:
                            command.OpCode = command.IsByteMode ? VMCommands.VM_INCB : VMCommands.VM_INCD;
                            break;

                        case VMCommands.VM_DEC:
                            command.OpCode = command.IsByteMode ? VMCommands.VM_DECB : VMCommands.VM_DECD;
                            break;

                        case VMCommands.VM_NEG:
                            command.OpCode = command.IsByteMode ? VMCommands.VM_NEGB : VMCommands.VM_NEGD;
                            break;
                    }
                }
            }
        }
Пример #8
0
 public void prepare(byte[] code, int codeSize, VMPreparedProgram prg)
 {
     int num3;
     base.InitBitInput();
     int count = Math.Min(0x8000, codeSize);
     Buffer.BlockCopy(code, 0, base.InBuf, 0, count);
     byte num2 = 0;
     for (num3 = 1; num3 < codeSize; num3++)
     {
         num2 = (byte) (num2 ^ code[num3]);
     }
     base.AddBits(8);
     prg.CommandCount = 0;
     if (num2 == code[0])
     {
         VMPreparedCommand command;
         VMStandardFilters filters = this.IsStandardFilter(code, codeSize);
         if (filters != VMStandardFilters.VMSF_NONE)
         {
             command = new VMPreparedCommand();
             command.OpCode = VMCommands.VM_STANDARD;
             command.Op1.Data = (int) filters;
             command.Op1.Type = VMOpType.VM_OPNONE;
             command.Op2.Type = VMOpType.VM_OPNONE;
             codeSize = 0;
             prg.Commands.Add(command);
             prg.CommandCount++;
         }
         int bits = base.GetBits();
         base.AddBits(1);
         if ((bits & 0x8000) != 0)
         {
             long num5 = ReadData(this) & 0x100000000L;
             for (num3 = 0; (base.inAddr < codeSize) && (num3 < num5); num3++)
             {
                 prg.StaticData.Add((byte) (base.GetBits() >> 8));
                 base.AddBits(8);
             }
         }
         while (base.inAddr < codeSize)
         {
             command = new VMPreparedCommand();
             int num6 = base.GetBits();
             if ((num6 & 0x8000) == 0)
             {
                 command.OpCode = (VMCommands) (num6 >> 12);
                 base.AddBits(4);
             }
             else
             {
                 command.OpCode = (VMCommands) ((num6 >> 10) - 0x18);
                 base.AddBits(6);
             }
             if ((VMCmdFlags.VM_CmdFlags[(int) command.OpCode] & 4) != 0)
             {
                 command.IsByteMode = (base.GetBits() >> 15) == 1;
                 base.AddBits(1);
             }
             else
             {
                 command.IsByteMode = false;
             }
             command.Op1.Type = VMOpType.VM_OPNONE;
             command.Op2.Type = VMOpType.VM_OPNONE;
             int num7 = VMCmdFlags.VM_CmdFlags[(int) command.OpCode] & 3;
             if (num7 > 0)
             {
                 this.decodeArg(command.Op1, command.IsByteMode);
                 if (num7 == 2)
                 {
                     this.decodeArg(command.Op2, command.IsByteMode);
                 }
                 else if ((command.Op1.Type == VMOpType.VM_OPINT) && ((VMCmdFlags.VM_CmdFlags[(int) command.OpCode] & 0x18) != 0))
                 {
                     int data = command.Op1.Data;
                     if (data >= 0x100)
                     {
                         data -= 0x100;
                     }
                     else
                     {
                         if (data >= 0x88)
                         {
                             data -= 0x108;
                         }
                         else if (data >= 0x10)
                         {
                             data -= 8;
                         }
                         else if (data >= 8)
                         {
                             data -= 0x10;
                         }
                         data += prg.CommandCount;
                     }
                     command.Op1.Data = data;
                 }
             }
             prg.CommandCount++;
             prg.Commands.Add(command);
         }
     }
     VMPreparedCommand item = new VMPreparedCommand();
     item.OpCode = VMCommands.VM_RET;
     item.Op1.Type = VMOpType.VM_OPNONE;
     item.Op2.Type = VMOpType.VM_OPNONE;
     prg.Commands.Add(item);
     prg.CommandCount++;
     if (codeSize != 0)
     {
         this.optimize(prg);
     }
 }