/* Bra.c -- Converters for RISC code
2010-04-16 : Igor Pavlov : Public domain */

#include "Precomp.h"

#include "Bra.h"

SizeT ARM_Convert(Byte *data, SizeT size, UInt32_7z ip, int encoding)
{
  SizeT i;
  if (size < 4)
    return 0;
  size -= 4;
  ip += 8;
  for (i = 0; i <= size; i += 4)
  {
    if (data[i + 3] == 0xEB)
    {
      UInt32_7z dest;
      UInt32_7z src = ((UInt32_7z)data[i + 2] << 16) | ((UInt32_7z)data[i + 1] << 8) | (data[i + 0]);
      src <<= 2;
      if (encoding)
        dest = ip + (UInt32_7z)i + src;
      else
        dest = src - (ip + (UInt32_7z)i);
      dest >>= 2;
      data[i + 2] = (Byte)(dest >> 16);
      data[i + 1] = (Byte)(dest >> 8);
      data[i + 0] = (Byte)dest;
    }
  }
  return i;
}

SizeT ARMT_Convert(Byte *data, SizeT size, UInt32_7z ip, int encoding)
{
  SizeT i;
  if (size < 4)
    return 0;
  size -= 4;
  ip += 4;
  for (i = 0; i <= size; i += 2)
  {
    if ((data[i + 1] & 0xF8) == 0xF0 &&
        (data[i + 3] & 0xF8) == 0xF8)
    {
      UInt32_7z dest;
      UInt32_7z src =
        (((UInt32_7z)data[i + 1] & 0x7) << 19) |
        ((UInt32_7z)data[i + 0] << 11) |
        (((UInt32_7z)data[i + 3] & 0x7) << 8) |
        (data[i + 2]);
      
      src <<= 1;
      if (encoding)
        dest = ip + (UInt32_7z)i + src;
      else
        dest = src - (ip + (UInt32_7z)i);
      dest >>= 1;
      
      data[i + 1] = (Byte)(0xF0 | ((dest >> 19) & 0x7));
      data[i + 0] = (Byte)(dest >> 11);
      data[i + 3] = (Byte)(0xF8 | ((dest >> 8) & 0x7));
      data[i + 2] = (Byte)dest;
      i += 2;
    }
  }
  return i;
}

SizeT PPC_Convert(Byte *data, SizeT size, UInt32_7z ip, int encoding)
{
  SizeT i;
  if (size < 4)
    return 0;
  size -= 4;
  for (i = 0; i <= size; i += 4)
  {
    if ((data[i] >> 2) == 0x12 && (data[i + 3] & 3) == 1)
    {
      UInt32_7z src = ((UInt32_7z)(data[i + 0] & 3) << 24) |
        ((UInt32_7z)data[i + 1] << 16) |
        ((UInt32_7z)data[i + 2] << 8) |
        ((UInt32_7z)data[i + 3] & (~3));
      
      UInt32_7z dest;
      if (encoding)
        dest = ip + (UInt32_7z)i + src;
      else
        dest = src - (ip + (UInt32_7z)i);
      data[i + 0] = (Byte)(0x48 | ((dest >> 24) &  0x3));
      data[i + 1] = (Byte)(dest >> 16);
      data[i + 2] = (Byte)(dest >> 8);
      data[i + 3] &= 0x3;
      data[i + 3] |= dest;
    }
  }
  return i;
}

SizeT SPARC_Convert(Byte *data, SizeT size, UInt32_7z ip, int encoding)
{
  UInt32_7z i;
  if (size < 4)
    return 0;
  size -= 4;
  for (i = 0; i <= size; i += 4)
  {
    if ((data[i] == 0x40 && (data[i + 1] & 0xC0) == 0x00) ||
        (data[i] == 0x7F && (data[i + 1] & 0xC0) == 0xC0))
    {
      UInt32_7z src =
        ((UInt32_7z)data[i + 0] << 24) |
        ((UInt32_7z)data[i + 1] << 16) |
        ((UInt32_7z)data[i + 2] << 8) |
        ((UInt32_7z)data[i + 3]);
      UInt32_7z dest;
      
      src <<= 2;
      if (encoding)
        dest = ip + i + src;
      else
        dest = src - (ip + i);
      dest >>= 2;
      
      dest = (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF) | (dest & 0x3FFFFF) | 0x40000000;

      data[i + 0] = (Byte)(dest >> 24);
      data[i + 1] = (Byte)(dest >> 16);
      data[i + 2] = (Byte)(dest >> 8);
      data[i + 3] = (Byte)dest;
    }
  }
  return i;
}
