y_writemem.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**--------------------------------------------------------------------------**\
  2. ===========================
  3. Y Sever Includes - Writemem
  4. ===========================
  5. Description:
  6. Write to any absolute address in the SA:MP server in pure PAWN with embedded
  7. assembly (i.e. a new native). Calls "VirtualProtect" to make writes safe.
  8. Legal:
  9. Version: MPL 1.1
  10. The contents of this file are subject to the Mozilla Public License Version
  11. 1.1 (the "License"); you may not use this file except in compliance with
  12. the License. You may obtain a copy of the License at
  13. http://www.mozilla.org/MPL/
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the
  17. License.
  18. The Original Code is the YSI ini include.
  19. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  20. Portions created by the Initial Developer are Copyright (C) 2011
  21. the Initial Developer. All Rights Reserved.
  22. Contributors:
  23. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  24. Thanks:
  25. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  26. ZeeX - Very productive conversations.
  27. koolk - IsPlayerinAreaEx code.
  28. TheAlpha - Danish translation.
  29. breadfish - German translation.
  30. Fireburn - Dutch translation.
  31. yom - French translation.
  32. 50p - Polish translation.
  33. Zamaroht - Spanish translation.
  34. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  35. for me to strive to better.
  36. Pixels^ - Running XScripters where the idea was born.
  37. Matite - Pestering me to release it and using it.
  38. Very special thanks to:
  39. Thiadmer - PAWN, whose limits continue to amaze me!
  40. Kye/Kalcor - SA:MP.
  41. SA:MP Team past, present and future - SA:MP.
  42. Version:
  43. 1.0
  44. Changelog:
  45. 01/02/12:
  46. Zeex_: Changed to use SYSREQ.D call not SYSREQ.C.
  47. Zeex_: Modified "VirtualProtect" pointer to SA:MP server one.
  48. Added "VirtualProtect" calls to the code.
  49. First version.
  50. Functions:
  51. Public:
  52. -
  53. Core:
  54. -
  55. Stock:
  56. -
  57. Static:
  58. -
  59. Inline:
  60. -
  61. API:
  62. WriteMem - Write data to an address.
  63. Callbacks:
  64. -
  65. Definitions:
  66. asm - Convert a stream of assembly to a cell.
  67. Enums:
  68. -
  69. Macros:
  70. -
  71. Tags:
  72. -
  73. Variables:
  74. Global:
  75. -
  76. Static:
  77. YSI_g_sWriteMem - The assembly.
  78. Commands:
  79. -
  80. Compile options:
  81. -
  82. Operators:
  83. -
  84. </remarks>
  85. \**--------------------------------------------------------------------------**/
  86. #include <a_samp>
  87. #include "y_amx"
  88. #include "y_hooks"
  89. #include "y_utils"
  90. forward WriteMem(addr, value);
  91. #define asm(%0,%1,%2,%3) ((0x%0<<0)|(0x%1<<8)|(0x%2<<16)|(0x%3<<24))
  92. /*
  93. cell AMX_NATIVE_CALL
  94. n_WriteMem(AMX * amx, cell * params)
  95. {
  96. if (params[0] == 8)
  97. {
  98. DWORD
  99. oldp;
  100. VirtualProtect((cell *)params[1], 4, PAGE_EXECUTE_READWRITE, &oldp);
  101. *((cell *)params[1]) = params[2];
  102. }
  103. return 0;
  104. }
  105. BECOMES:
  106. align 16
  107. push ebp
  108. mov ebp, esp
  109. push esi
  110. mov esi, [ebp+12]
  111. cmp dword ptr [esi], 8
  112. jnz short loc_ret
  113. mov ecx, [esi+4]
  114. lea eax, [ebp+12]
  115. push eax
  116. push 40h
  117. push 4
  118. push ecx
  119. call ds:__imp__VirtualProtect@16
  120. mov edx, [esi+4]
  121. mov eax, [esi+8]
  122. mov [edx], eax
  123. loc_ret:
  124. xor eax, eax
  125. pop esi
  126. pop ebp
  127. retn
  128. */
  129. static
  130. YSI_g_sWriteMem[] =
  131. {
  132. asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC),
  133. asm(55,8B,EC,56), asm(8B,75,0C,83), asm(3E,08,75,1A), asm(8B,4E,04,8D),
  134. asm(45,0C,50,6A), asm(40,6A,04,51), asm(FF,15,8C,11), asm(4A,00,8B,56),
  135. asm(04,8B,46,08), asm(89,02,33,C0), asm(5E,5D,C3,CC)
  136. };
  137. static
  138. YSI_g_iWriteMemAddr = 0;
  139. stock
  140. WriteMem(addr, value)
  141. {
  142. static ptr = -1;
  143. // Push addr and value.
  144. #emit PUSH.S value
  145. #emit PUSH.S addr
  146. #emit PUSH.C 8
  147. if (ptr == -1)
  148. {
  149. // ptr = COD + CIP - DAT + <distance to nop #1>
  150. // Modified from code by Zeex_.
  151. #emit LCTRL 6 // CIP
  152. #emit LOAD.alt AMX_HEADER_COD
  153. #emit ADD
  154. #emit ADD.C 84
  155. #emit STOR.S.pri ptr
  156. // NOP #1 = SYSREQ.D
  157. #emit CONST.pri 135
  158. #emit SREF.S.pri ptr
  159. // ptr += 4
  160. #emit LOAD.S.pri ptr
  161. #emit ADD.C 4
  162. #emit STOR.S.pri ptr
  163. // NOP #2 = address
  164. #emit LOAD.pri YSI_g_iWriteMemAddr
  165. #emit SREF.S.pri ptr
  166. }
  167. // Reserve space for SYSREQ.D WriteMem.
  168. #emit NOP
  169. #emit NOP
  170. // Pop native arguments.
  171. #emit STACK 12
  172. }
  173. static
  174. WM_Shift(from, to, data[], len = sizeof (data))
  175. {
  176. if (FALSE)
  177. {
  178. WriteMem(0, 0);
  179. }
  180. while (from < len)
  181. {
  182. data[to++] = data[from++];
  183. }
  184. }
  185. hook OnScriptInit()
  186. {
  187. new
  188. addr = AMX_GetGlobalAddress(YSI_g_sWriteMem);
  189. // Align the code to a 16-byte boundary.
  190. switch (addr & 15)
  191. {
  192. case 0:
  193. {
  194. WM_Shift(4, 0, YSI_g_sWriteMem);
  195. }
  196. case 4:
  197. {
  198. WM_Shift(4, 3, YSI_g_sWriteMem);
  199. addr += 12;
  200. }
  201. case 8:
  202. {
  203. WM_Shift(4, 2, YSI_g_sWriteMem);
  204. addr += 8;
  205. }
  206. case 12:
  207. {
  208. WM_Shift(4, 1, YSI_g_sWriteMem);
  209. addr += 4;
  210. }
  211. default:
  212. {
  213. P:E("Cannot relocate YSI_g_sWriteMem");
  214. }
  215. }
  216. YSI_g_iWriteMemAddr = addr;
  217. return 1;
  218. }