IDA PRO将C ++代码翻译成C代码__OFSUB__宏

时间:2014-04-19 10:56:45

标签: c++ c macros translate ida

我要求在C中使用这个C ++宏的等价物(不必是一个宏可能是一个函数)

以下是代码在C ++伪代码中的使用方式,它实际上可能与宏编译,但宏在C中不起作用。不能在C中使用模板。

我不太明白它是否像%模数一样工作?

  int v47; // ecx@78
  bool v48; // sf@78
  unsigned char v49; // of@78
  int v79; // [sp+18h] [bp-5438h]@68

  v47 = *(unsigned int *)(v2 + 65292);
  v49 = __OFSUB__(v79 + 1, v47);
  v48 = v79++ + 1 - v47 < 0;

  char *playerra; // [sp+24h] [bp-6D0h]@20
  int v26; // esi@32
  unsigned __int8 v28; // of@32

  v28 = __OFSUB__(playerra + 1, v26);
  v27 = (signed int)&(playerra++)[-v26 + 1] < 0;

  bool v10; // sf@24
  unsigned __int8 v11; // of@24
  int v54; // [sp+14h] [bp-508h]@19
  v11 = __OFSUB__(v54 + 1, 40);
  v10 = v54++ - 39 < 0;

对于C,它声明为

// For C, we just provide macros, they are not quite correct.
#define __OFSUB__(x, y) invalid_operation // Generate overflow flag for (x-y)

以下是宏的定义方式。

// overflow flag of subtraction (x-y)
template<class T, class U> int8 __OFSUB__(T x, U y)
{
  if ( sizeof(T) < sizeof(U) )
  {
    U x2 = x;
    int8 sx = __SETS__(x2);
    return (sx ^ __SETS__(y)) & (sx ^ __SETS__(x2-y));
  }
  else
  {
    T y2 = y;
    int8 sx = __SETS__(x);
    return (sx ^ __SETS__(y2)) & (sx ^ __SETS__(x-y2));
  }
}

使用此

// sign flag
template<class T> int8 __SETS__(T x)
{
  if ( sizeof(T) == 1 )
    return int8(x) < 0;
  if ( sizeof(T) == 2 )
    return int16(x) < 0;
  if ( sizeof(T) == 4 )
    return int32(x) < 0;
  return int64(x) < 0;
}

并使用此

typedef          char    int8;
typedef   signed char    sint8;
typedef unsigned char    uint8;
typedef          short   int16;
typedef   signed short   sint16;
typedef unsigned short   uint16;
typedef          int     int32;
typedef   signed int     sint32;
typedef unsigned int     uint32;
typedef __int64          int64;
typedef __int64          sint64;
typedef unsigned __int64 uint64;

1 个答案:

答案 0 :(得分:3)

由于您的问题中有一个特定的案例,似乎对模板没有任何需求。所以我在这里所做的就是用int替换类型名称,我也删除了一半的OFSUB,因为你的参数类型大小匹配。

// overflow flag of subtraction (x-y)
int8 __OFSUB__(int x, int y)
{
    int y2 = y;
    int8 sx = __SETS__(x);
    return (sx ^ __SETS__(y2)) & (sx ^ __SETS__(x-y2));
}

// sign flag
int8 __SETS__(int x)
{
  if ( sizeof(int) == 1 )
    return int8(x) < 0;
  if ( sizeof(int) == 2 )
    return int16(x) < 0;
  if ( sizeof(int) == 4 )
    return int32(x) < 0;
  return int64(x) < 0;
}