Bra.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Bra.c -- Converters for RISC code
  2. 2008-10-04 : Igor Pavlov : Public domain */
  3. #include "Bra.h"
  4. SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
  5. {
  6. SizeT i;
  7. if (size < 4)
  8. return 0;
  9. size -= 4;
  10. ip += 8;
  11. for (i = 0; i <= size; i += 4)
  12. {
  13. if (data[i + 3] == 0xEB)
  14. {
  15. UInt32 dest;
  16. UInt32 src = ((UInt32)data[i + 2] << 16) | ((UInt32)data[i + 1] << 8) | (data[i + 0]);
  17. src <<= 2;
  18. if (encoding)
  19. dest = ip + (UInt32)i + src;
  20. else
  21. dest = src - (ip + (UInt32)i);
  22. dest >>= 2;
  23. data[i + 2] = (Byte)(dest >> 16);
  24. data[i + 1] = (Byte)(dest >> 8);
  25. data[i + 0] = (Byte)dest;
  26. }
  27. }
  28. return i;
  29. }
  30. SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
  31. {
  32. SizeT i;
  33. if (size < 4)
  34. return 0;
  35. size -= 4;
  36. ip += 4;
  37. for (i = 0; i <= size; i += 2)
  38. {
  39. if ((data[i + 1] & 0xF8) == 0xF0 &&
  40. (data[i + 3] & 0xF8) == 0xF8)
  41. {
  42. UInt32 dest;
  43. UInt32 src =
  44. (((UInt32)data[i + 1] & 0x7) << 19) |
  45. ((UInt32)data[i + 0] << 11) |
  46. (((UInt32)data[i + 3] & 0x7) << 8) |
  47. (data[i + 2]);
  48. src <<= 1;
  49. if (encoding)
  50. dest = ip + (UInt32)i + src;
  51. else
  52. dest = src - (ip + (UInt32)i);
  53. dest >>= 1;
  54. data[i + 1] = (Byte)(0xF0 | ((dest >> 19) & 0x7));
  55. data[i + 0] = (Byte)(dest >> 11);
  56. data[i + 3] = (Byte)(0xF8 | ((dest >> 8) & 0x7));
  57. data[i + 2] = (Byte)dest;
  58. i += 2;
  59. }
  60. }
  61. return i;
  62. }
  63. SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
  64. {
  65. SizeT i;
  66. if (size < 4)
  67. return 0;
  68. size -= 4;
  69. for (i = 0; i <= size; i += 4)
  70. {
  71. if ((data[i] >> 2) == 0x12 && (data[i + 3] & 3) == 1)
  72. {
  73. UInt32 src = ((UInt32)(data[i + 0] & 3) << 24) |
  74. ((UInt32)data[i + 1] << 16) |
  75. ((UInt32)data[i + 2] << 8) |
  76. ((UInt32)data[i + 3] & (~3));
  77. UInt32 dest;
  78. if (encoding)
  79. dest = ip + (UInt32)i + src;
  80. else
  81. dest = src - (ip + (UInt32)i);
  82. data[i + 0] = (Byte)(0x48 | ((dest >> 24) & 0x3));
  83. data[i + 1] = (Byte)(dest >> 16);
  84. data[i + 2] = (Byte)(dest >> 8);
  85. data[i + 3] &= 0x3;
  86. data[i + 3] |= dest;
  87. }
  88. }
  89. return i;
  90. }
  91. SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
  92. {
  93. UInt32 i;
  94. if (size < 4)
  95. return 0;
  96. size -= 4;
  97. for (i = 0; i <= size; i += 4)
  98. {
  99. if ((data[i] == 0x40 && (data[i + 1] & 0xC0) == 0x00) ||
  100. (data[i] == 0x7F && (data[i + 1] & 0xC0) == 0xC0))
  101. {
  102. UInt32 src =
  103. ((UInt32)data[i + 0] << 24) |
  104. ((UInt32)data[i + 1] << 16) |
  105. ((UInt32)data[i + 2] << 8) |
  106. ((UInt32)data[i + 3]);
  107. UInt32 dest;
  108. src <<= 2;
  109. if (encoding)
  110. dest = ip + i + src;
  111. else
  112. dest = src - (ip + i);
  113. dest >>= 2;
  114. dest = (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF) | (dest & 0x3FFFFF) | 0x40000000;
  115. data[i + 0] = (Byte)(dest >> 24);
  116. data[i + 1] = (Byte)(dest >> 16);
  117. data[i + 2] = (Byte)(dest >> 8);
  118. data[i + 3] = (Byte)dest;
  119. }
  120. }
  121. return i;
  122. }