InBuf function currently 32-bit only (machine code binary buffer searching)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: InBuf function currently 32-bit only (machine code binary buffer searching)

16 Oct 2018, 15:37

It was relatively easy to alter the function so that it didn't call itself. Although, it seemed difficult at the time. I guess it was just one of those random things I wasn't too familiar with, or, I thought there must be some special reason for the function to call itself. Cheers.

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
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: InBuf function currently 32-bit only (machine code binary buffer searching)

17 Oct 2018, 02:43

Since nested functions, this is beter,

Code: Select all

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
	static bin := init()
	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
	init(){
		local
		flProtect:=0x40, flAllocationType:=0x1000 ; PAGE_EXECUTE_READWRITE ,	; MEM_COMMIT
		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]
		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]
		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")
		return bin
		
	}
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: InBuf function currently 32-bit only (machine code binary buffer searching)

24 Aug 2019, 13:18

One of the key things I'd want to do with binary searching is to find the next null byte or null short (i.e. 2 null bytes in a row).
This is possible using msvcrt\strlen and msvcrt\wcslen:

Code: Select all

q:: ;find first null byte, find first null short
VarSetCapacity(vData, 100, 1)
NumPut(0, &vData, 20, "UChar")
NumPut(0, &vData, 60, "UShort")
NumPut(0, &vData, 81, "UShort")
MsgBox, % vLenAnsi := DllCall("msvcrt\strlen", "Ptr",&vData, "Cdecl UPtr") ;20
MsgBox, % vLenUTF16Even := DllCall("msvcrt\wcslen", "Ptr",&vData, "Cdecl UPtr") ;30
MsgBox, % vLenUTF16Odd := DllCall("msvcrt\wcslen", "Ptr",&vData+1, "Cdecl UPtr") ;40
vOffsetNullByte := vLenAnsi ;20
vOffsetNullShortEven := vLenUTF16Even*2 ;30*2 = 60
vOffsetNullShortOdd := vLenUTF16Odd*2+1 ;40*2+1 = 81
vOffsetNullShort := Min(vOffsetNullShortEven, vOffsetNullShortOdd) ;Min(60, 81) = 60
MsgBox, % vOffsetNullByte " " vOffsetNullShortEven " " vOffsetNullShortOdd ;20 60 81 (the 3 offsets at the beginning)
MsgBox, % vOffsetNullByte " " vOffsetNullShort ;20 60 (offsets of first null byte and first null short)
return
One use is to repeatedly find the next null character, to replace it with a character of your choosing, and therefore to display binary data as a string.
Null characters are typically used to mark the end of a string.
See READ BINARY DATA AND REPLACE NULLS, here:
jeeswg's File object mini-tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=74&t=66674
Last edited by jeeswg on 24 Sep 2019, 13:38, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: InBuf function currently 32-bit only (machine code binary buffer searching)

25 Aug 2019, 05:02

for general use I'd recommend to use strnlen to avoid reading outside the buffer.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: LepG, mapcarter, OrangeCat and 266 guests