Example #1
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);
            }
        }
Example #2
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);
     }
 }