Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.1 KiB

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