InBuf function currently 32-bit only (machine code binary buffer searching) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 19#p151519
Code: Select all
vText := "abcd", vNeedle := "cd"
vSize := StrLen(vText)*(A_IsUnicode?2:1)
vSizeNeedle := StrLen(vNeedle)*(A_IsUnicode?2:1)
MsgBox, % inBufStep(&vText, &vNeedle, vSize, vSizeNeedle)
inBufStep(hay,ned,haylen,nedlen,haystep:=1,nedstep:=1,bufPos:=true){
; Input:
; - hay, haystack buffer to be searched for first occurrence of ned.
; - ned, needle buffer to search for first occurrence in hay.
; - haylen, length of hay, in bytes
; - nedlen, length of ned, in bytes
; - haystep, size of steps to take in hay
; - nedstep, size of steps to take in ned
; - bufpos, return buffer position, set to false to return string position.
; Return:
; - position of ned in hay according to bufPos parameter, if no match, return -1 if nedlen>haylen, else -2.
; Url:
; - https://msdn.microsoft.com/en-us/library/windows/desktop/aa366887(v=vs.85).aspx (VirtualAlloc function)
; - https://msdn.microsoft.com/en-us/library/windows/desktop/aa366786(v=vs.85).aspx (Memory Protection Constants)
;
; Due to:
; - https://autohotkey.com/boards/viewtopic.php?f=5&t=28393
local k, i, raw
static flProtect:=0x40, flAllocationType:=0x1000 ; PAGE_EXECUTE_READWRITE , ; MEM_COMMIT
static raw32:=[1398167381,2332159107,2333680748,2334205004,2334729340,253043828,687883702,689775692,604276985,3054485553,506995740,3380944757,4203294324,3054441451,939529564,225777180,479054393,2300146746,1992964570,608437225,611582757,608451365,3100341789,4294967294,1526842499,3277676382]
static raw64:=[1398167381,1344566411,824882703,612142016,3358147912,1106323777,943899273,846532908,1959363909,4069081401,258352107,17439,270306630,265521476,1109007542,1964579896,3510191377,370969926,2303005812,3510191578,4160872051,1992309060,4294883518,1583087615,12803423]
static bin
if !bin {
bin:=DllCall("Kernel32.dll\VirtualAlloc", "Uptr",0, "Ptr", (raw:=A_PtrSize==4?raw32:raw64).length()*4, "Uint", flAllocationType, "Uint", flProtect, "Ptr")
for k, i in raw
NumPut(i,bin+(k-1)*4,"Int")
raw32:="",raw64:=""
}
if (nedlen>haylen)
return -1
p:=DllCall(bin, "Ptr", hay, "Ptr", ned, "Uint", haylen, "Uint", nedlen, "Uint", haystep, "Uint", nedstep)
return p>=0 ? (bufPos ? p : (p+(A_IsUnicode?2:1)) // (A_IsUnicode?2:1)) : p
}