Example #1
0
        public unsafe static uint SizeofMin5Byte(void *code)
        {
            uint Result = 0U;

            LDasm.ldasm_data data = default(LDasm.ldasm_data);
            bool             is64 = IntPtr.Size == 8;
            uint             Length;

            do
            {
                Length = LDasm.ldasm(code, data, is64);
                byte *pOpcode = (byte *)code + data.opcd_offset;
                Result += Length;
                if (Result >= 5U)
                {
                    break;
                }
                if (Length == 1U && *pOpcode == 204)
                {
                    break;
                }
                code = (void *)((byte *)code + (ulong)Length);
            }while (Length > 0U);
            return(Result);
        }
Example #2
0
        private unsafe static uint ldasm(void *code, LDasm.ldasm_data ld, bool is64)
        {
            byte *p = (byte *)code;
            byte  pr_67;
            byte  pr_66;
            byte  s;
            byte  rexw = s = (pr_66 = (pr_67 = 0));
            uint  result;

            if ((int)code == 0)
            {
                result = 0U;
            }
            else
            {
                while ((LDasm.cflags(*p) & 128) != 0)
                {
                    if (*p == 102)
                    {
                        pr_66 = 1;
                    }
                    if (*p == 103)
                    {
                        pr_67 = 1;
                    }
                    p++;
                    s        += 1;
                    ld.flags |= 2;
                    if (s == 15)
                    {
                        ld.flags |= 1;
                        return((uint)s);
                    }
                }
                if (is64 && *p >> 4 == 4)
                {
                    ld.rex    = *p;
                    rexw      = (byte)(ld.rex >> 3 & 1);
                    ld.flags |= 4;
                    p++;
                    s += 1;
                }
                if (is64 && *p >> 4 == 4)
                {
                    ld.flags |= 1;
                    s        += 1;
                    result    = (uint)s;
                }
                else
                {
                    ld.opcd_offset = (byte)((long)((byte *)p - (byte *)code));
                    ld.opcd_size   = 1;
                    byte op = *(p++);
                    s += 1;
                    byte f;
                    if (op == 15)
                    {
                        op            = *(p++);
                        s            += 1;
                        ld.opcd_size += 1;
                        f             = LDasm.cflags_ex(op);
                        if ((f & 128) != 0)
                        {
                            ld.flags |= 1;
                            return((uint)s);
                        }
                        if ((f & 16) != 0)
                        {
                            op            = *(p++);
                            s            += 1;
                            ld.opcd_size += 1;
                        }
                    }
                    else
                    {
                        f = LDasm.cflags(op);
                        if (op >= 160 && op <= 163)
                        {
                            pr_66 = pr_67;
                        }
                    }
                    if ((f & 64) != 0)
                    {
                        byte mod = (byte)(*p >> 6);
                        byte ro  = (byte)((*p & 56) >> 3);
                        byte rm  = (byte)(*p & 7);
                        ld.modrm  = *(p++);
                        s        += 1;
                        ld.flags |= 8;
                        if (op == 246 && (ro == 0 || ro == 1))
                        {
                            f |= 1;
                        }
                        if (op == 247 && (ro == 0 || ro == 1))
                        {
                            f |= 8;
                        }
                        if (mod != 3 && rm == 4 && (is64 || pr_67 == 0))
                        {
                            ld.sib    = *(p++);
                            s        += 1;
                            ld.flags |= 16;
                            if ((ld.sib & 7) == 5 && mod == 0)
                            {
                                ld.disp_size = 4;
                            }
                        }
                        switch (mod)
                        {
                        case 0:
                            if (is64)
                            {
                                if (rm == 5)
                                {
                                    ld.disp_size = 4;
                                    if (is64)
                                    {
                                        ld.flags |= 128;
                                    }
                                }
                            }
                            else if (pr_67 != 0)
                            {
                                if (rm == 6)
                                {
                                    ld.disp_size = 2;
                                }
                            }
                            else if (rm == 5)
                            {
                                ld.disp_size = 4;
                            }
                            break;

                        case 1:
                            ld.disp_size = 1;
                            break;

                        case 2:
                            if (is64)
                            {
                                ld.disp_size = 4;
                            }
                            else if (pr_67 != 0)
                            {
                                ld.disp_size = 2;
                            }
                            else
                            {
                                ld.disp_size = 4;
                            }
                            break;
                        }
                        if (ld.disp_size > 0)
                        {
                            ld.disp_offset = (byte)((long)((byte *)p - (byte *)code));
                            p        += ld.disp_size;
                            s        += ld.disp_size;
                            ld.flags |= 32;
                        }
                    }
                    if (rexw != 0 && (f & 8) != 0)
                    {
                        ld.imm_size = 8;
                    }
                    else if ((f & 4) != 0 || (f & 8) != 0)
                    {
                        ld.imm_size = (byte)(4 - ((int)pr_66 << 1));
                    }
                    ld.imm_size += (byte)(f & 3);
                    if (ld.imm_size != 0)
                    {
                        s            += ld.imm_size;
                        ld.imm_offset = (byte)((long)((byte *)p - (byte *)code));
                        ld.flags     |= 64;
                        if ((f & 32) != 0)
                        {
                            ld.flags |= 128;
                        }
                    }
                    if (s > 15)
                    {
                        ld.flags |= 1;
                    }
                    result = (uint)s;
                }
            }
            return(result);
        }