y_writemem.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. \*----------------------------------------------------------------------------*/
  85. #include <a_samp>
  86. #include "y_amx"
  87. #include "y_hooks"
  88. #include "y_utils"
  89. forward WriteMem(addr, value);
  90. #define asm(%0,%1,%2,%3) ((0x%0<<0)|(0x%1<<8)|(0x%2<<16)|(0x%3<<24))
  91. /*
  92. cell AMX_NATIVE_CALL
  93. n_WriteMem(AMX * amx, cell * params)
  94. {
  95. if (params[0] == 8)
  96. {
  97. DWORD
  98. oldp;
  99. VirtualProtect((cell *)params[1], 4, PAGE_EXECUTE_READWRITE, &oldp);
  100. *((cell *)params[1]) = params[2];
  101. }
  102. return 0;
  103. }
  104. BECOMES:
  105. align 16
  106. push ebp
  107. mov ebp, esp
  108. push esi
  109. mov esi, [ebp+12]
  110. cmp dword ptr [esi], 8
  111. jnz short loc_ret
  112. mov ecx, [esi+4]
  113. lea eax, [ebp+12]
  114. push eax
  115. push 40h
  116. push 4
  117. push ecx
  118. call ds:__imp__VirtualProtect@16
  119. mov edx, [esi+4]
  120. mov eax, [esi+8]
  121. mov [edx], eax
  122. loc_ret:
  123. xor eax, eax
  124. pop esi
  125. pop ebp
  126. retn
  127. */
  128. static
  129. YSI_g_sWriteMem[] =
  130. {
  131. asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC),
  132. asm(55,8B,EC,56), asm(8B,75,0C,83), asm(3E,08,75,1A), asm(8B,4E,04,8D),
  133. asm(45,0C,50,6A), asm(40,6A,04,51), asm(FF,15,8C,11), asm(4A,00,8B,56),
  134. asm(04,8B,46,08), asm(89,02,33,C0), asm(5E,5D,C3,CC)
  135. };
  136. static
  137. YSI_g_iWriteMemAddr = 0;
  138. stock
  139. WriteMem(addr, value)
  140. {
  141. static ptr = -1;
  142. // Push addr and value.
  143. #emit PUSH.S value
  144. #emit PUSH.S addr
  145. #emit PUSH.C 8
  146. if (ptr == -1)
  147. {
  148. // ptr = COD + CIP - DAT + <distance to nop #1>
  149. // Modified from code by Zeex_.
  150. #emit LCTRL 6 // CIP
  151. #emit LOAD.alt AMX_HEADER_COD
  152. #emit ADD
  153. #emit ADD.C 84
  154. #emit STOR.S.pri ptr
  155. // NOP #1 = SYSREQ.D
  156. #emit CONST.pri 135
  157. #emit SREF.S.pri ptr
  158. // ptr += 4
  159. #emit LOAD.S.pri ptr
  160. #emit ADD.C 4
  161. #emit STOR.S.pri ptr
  162. // NOP #2 = address
  163. #emit LOAD.pri YSI_g_iWriteMemAddr
  164. #emit SREF.S.pri ptr
  165. }
  166. // Reserve space for SYSREQ.D WriteMem.
  167. #emit NOP
  168. #emit NOP
  169. // Pop native arguments.
  170. #emit STACK 12
  171. }
  172. static
  173. WM_Shift(from, to, data[], len = sizeof (data))
  174. {
  175. if (FALSE)
  176. {
  177. WriteMem(0, 0);
  178. }
  179. while (from < len)
  180. {
  181. data[to++] = data[from++];
  182. }
  183. }
  184. hook OnScriptInit()
  185. {
  186. new
  187. addr = AMX_GetGlobalAddress(YSI_g_sWriteMem);
  188. // Align the code to a 16-byte boundary.
  189. switch (addr & 15)
  190. {
  191. case 0:
  192. {
  193. WM_Shift(4, 0, YSI_g_sWriteMem);
  194. }
  195. case 4:
  196. {
  197. WM_Shift(4, 3, YSI_g_sWriteMem);
  198. addr += 12;
  199. }
  200. case 8:
  201. {
  202. WM_Shift(4, 2, YSI_g_sWriteMem);
  203. addr += 8;
  204. }
  205. case 12:
  206. {
  207. WM_Shift(4, 1, YSI_g_sWriteMem);
  208. addr += 4;
  209. }
  210. default:
  211. {
  212. P:E("Cannot relocate YSI_g_sWriteMem");
  213. }
  214. }
  215. YSI_g_iWriteMemAddr = addr;
  216. return 1;
  217. }