1/* BranchARMThumb.c */
2
3#include "BranchARMThumb.h"
4
5UInt32 ARMThumb_Convert(Byte *data, UInt32 size, UInt32 nowPos, int encoding)
6{
7  UInt32 i;
8  for (i = 0; i + 4 <= size; i += 2)
9  {
10    if ((data[i + 1] & 0xF8) == 0xF0 &&
11        (data[i + 3] & 0xF8) == 0xF8)
12    {
13      UInt32 dest;
14      UInt32 src =
15        ((data[i + 1] & 0x7) << 19) |
16        (data[i + 0] << 11) |
17        ((data[i + 3] & 0x7) << 8) |
18        (data[i + 2]);
19
20      src <<= 1;
21      if (encoding)
22        dest = nowPos + i + 4 + src;
23      else
24        dest = src - (nowPos + i + 4);
25      dest >>= 1;
26
27      data[i + 1] = (Byte)(0xF0 | ((dest >> 19) & 0x7));
28      data[i + 0] = (Byte)(dest >> 11);
29      data[i + 3] = (Byte)(0xF8 | ((dest >> 8) & 0x7));
30      data[i + 2] = (Byte)dest;
31      i += 2;
32    }
33  }
34  return i;
35}
36