MCode机器码生成器的一些示例记录

; 原文:https://www.autohotkey.com/boards/viewtopic.php?f=23&t=49554
; https://github.com/joedf/MCode4GCC

;stringlen := MCode("2,x86:i0wkBDPAOAF0B0CAPAgAdfnD,x64:M8A4AXQKSP/B/8CAOQB19vPD") ;equivalent to line below
stringlen := MCode("2,x86:i1QkBDHAgDoAdBCQjXQmAIPAAYA8AgB19/PD88M=,x64:gDkAdBpIg8EBMcAPH0QAAEiDwQGDwAGAef8AdfPzwzHAww==")
stringlen2 := MCode("2,x86:i1QkBDHAZoM6AHQQjXQmAIPAAWaDPEIAdfbzw/PD,x64:ZoM5AHQaSIPBAjHADx9AAEiDwQKDwAFmg3n+AHXy88MxwMM=")
;vText := "abcdefghijklmnopqrstuvwxyz"
;I use Chr(257) aka Chr(0x101), because it gives a stream of 1-bytes, with no null bytes
vText := ""
Loop, 26
  vText .= Chr(0x101)
MsgBox, % DllCall(stringlen, "AStr",vText, "Cdecl") ;26
MsgBox, % DllCall(stringlen, "WStr",vText, "Cdecl") ;1 ;expected 52
MsgBox, % DllCall(stringlen, "Str",vText, "Cdecl") ;1 ;expected 52
MsgBox, % DllCall(stringlen, "Ptr",&vText, "Cdecl") ;1 ;expected 52

MsgBox, % DllCall(stringlen2, "AStr",vText, "Cdecl") ;13
MsgBox, % DllCall(stringlen2, "WStr",vText, "Cdecl") ;26
MsgBox, % DllCall(stringlen2, "Str",vText, "Cdecl") ;26
MsgBox, % DllCall(stringlen2, "Ptr",&vText, "Cdecl") ;26
return

;==================================================

;GitHub - joedf/MCode4GCC: MCode4GCC is an MCode generator using the GCC Compiler.
;https://github.com/joedf/MCode4GCC

MCode(mcode) {
  static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"
  if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
    return
  if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))
    return
  p := DllCall("GlobalAlloc", "uint", 0, "ptr", s, "ptr")
  if (c="x64")
    DllCall("VirtualProtect", "ptr", p, "ptr", s, "uint", 0x40, "uint*", op)
  if (DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", p, "uint*", s, "ptr", 0, "ptr", 0))
    return p
  DllCall("GlobalFree", "ptr", p)
}

;==================================================

/*
source code
[stringlen:]
MCode Tutorial (Compiled Code in AHK) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=32
[stringlen2: a slight variant using 'short' instead of 'char', not present at the link]

int stringlen(char *str)
{
  int i=0;
  for (; str[i]!=0; i++);
  return i;
}

int stringlen2(short *str)
{
  int i=0;
  for (; str[i]!=0; i++);
  return i;
}

note: for 'stringlen' the tutorial states the machine code as:
2,x86:i0wkBDPAOAF0B0CAPAgAdfnD,x64:M8A4AXQKSP/B/8CAOQB19vPD
my script gave instead:
2,x86:i1QkBDHAgDoAdBCQjXQmAIPAAYA8AgB19/PD88M=,x64:gDkAdBpIg8EBMcAPH0QAAEiDwQGDwAGAef8AdfPzwzHAww==
both appear to function identically
*/

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。