The main proc is:
Declare InMem(StartPos.l, *MainMem, MainLen.l, *FindMem, FindLen.l)
It works like the FindString function or the InStr function of other basics but you can seach chr(0) and other naughty little bytes. Since it works with pointers it needs the lengths passed too unfortunately
This just shows a binary file being opened, and a text search done on it even though there are nulls all the way though. I'm sure ASM guru's could make it faster but it seems okay speed wise as it's all just pointers.
Time permitting I want to do Mid() Trim(), stringfield() and Replace()
(I'm easily distracted though and these thing mysteriously turn into vapour

One thing I can think of is turn the LONGs into QUADS so that it can read files larger than 2gb but it'd slow it down a little I guess
Code: Select all
Declare ProcMain()
Declare InMem(StartPos.l, *MainMem, MainLen.l, *FindMem, FindLen.l)
Structure MemoryArray
Byte.b[0]
EndStructure
;=========================================================================
ProcMain()
;=========================================================================
Procedure.l ProcMain()
Infile.s = "C:\WINDOWS\notepad.exe" ;choose a binary file
hFile.l = ReadFile(#PB_Any,Infile)
FileLen.l = Lof(hfile)
*MainMemPtr = AllocateMemory(FileLen) ;Allocate Some Memory
*FindMemPtr = AllocateMemory(2) ;Allocate Some Memory
; Find arbitrary bytes
;PokeB(*FindMemPtr, 0)
;PokeB(*FindMemPtr + 1, Asc("l"))
;Find a string
FindStr.s = "ext"
FindLen.l = Len(FindStr)
*FindMemPtr = @FindStr
ReadData(hfile, *MainMemPtr, FileLen)
Debug "Found at " + Str(inMem(1, *MainMemPtr, FileLen, *FindMemPtr, FindLen))
Debug "Found at " + Str(inMem(500, *MainMemPtr, FileLen, *FindMemPtr, FindLen))
Debug "Found at " + Str(inMem(29000, *MainMemPtr, FileLen, *FindMemPtr, FindLen))
EndProcedure
;=========================================================================
Procedure.l InMem(StartPos.l, *MainMem, MainLen.l, *FindMem, FindLen.l)
*MainByteArray.MemoryArray = *MainMem
*FindByteArray.MemoryArray = *FindMem
If StartPos < 1 : StartPos = 1 : EndIf
FoundPos.l = 0
For MainArrayLoop.l = StartPos - 1 To MainLen -1
For FindArrayLoop.l = 0 To FindLen -1
If MainArrayLoop + FindArrayLoop = MainLen
;End reached
Break
EndIf
If *MainByteArray\byte[MainArrayLoop + FindArrayLoop] = *FindByteArray\byte[FindArrayLoop]
; Keep Going
Else
Break
EndIf
If FindArrayLoop = FindLen -1 ;we reached the end of the search
FoundPos.l = MainArrayLoop + 1
Break 2
EndIf
Next
Next
ProcedureReturn FoundPos
EndProcedure
;=========================================================================