FileRead & binary data Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
newbieforever
Posts: 232
Joined: 24 Aug 2016, 03:34

FileRead & binary data

Post by newbieforever » 22 May 2018, 01:30

I would like to use FileRead, Str, c* MyFile.exeto read the content of an exe file, and then replace all binary null characters by @ to have an readable Str which I can use for further processing. How this replacing could be done?
User avatar
jeeswg
Posts: 5117
Joined: 19 Dec 2016, 01:58
Location: UK

Re: FileRead & binary data

Post by jeeswg » 22 May 2018, 23:56

- Some ideas are:
- Loop through each byte using NumGet, use NumPut on any null bytes.
- Binary data to hex string, edit hex string (use RegEx to put a separator character between every pair of hex digits, replace any 00s, remove the separator character), hex string to binary data.
- Write a machine code function to search for null characters and replace them using NumPut.
InBuf function currently 32-bit only (machine code binary buffer searching) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=28393
- Write a machine code function to replace null characters with a specified character.
- (Then, for all of the approaches, to finish, use StrGet to retrieve an ANSI string.)

- Here's an example to loop through each byte using NumGet, and use NumPut on any null bytes.

Code: Select all

q:: ;read binary file, replace null bytes with a character
vChar := "@"
vPath := A_AhkPath

;AHK v1
;FileGetSize, vSize, % vPath
;FileRead, vData, % "*c " vPath

;AHK v1/v2 two-way compatible
oFile := FileOpen(vPath, "r")
oFile.Pos := 0
oFile.RawRead(vData, vSize := oFile.Length)
oFile.Close()

vOrd := Ord(vChar)
Loop, % vSize
	if !NumGet(&vData, A_Index-1, "UChar")
		NumPut(vOrd, &vData, A_Index-1, "UChar")
Clipboard := StrGet(&vData, vSize, "CP0")
MsgBox, % "done"
return
User avatar
nnnik
Posts: 3320
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: FileRead & binary data

Post by nnnik » 23 May 2018, 00:20

newbieforever wrote:I would like to use FileRead, Str, c* MyFile.exeto read the content of an exe file, and then replace all binary null characters by @ to have an readable Str which I can use for further processing. How this replacing could be done?
I think it might be worth mentioning that you wont get a working string after you have done this.
Depending on your encoding you still need to do several different things.
I would not reccomend doing this.
Recommends AHK Studio
newbieforever
Posts: 232
Joined: 24 Aug 2016, 03:34

Re: FileRead & binary data  Topic is solved

Post by newbieforever » 23 May 2018, 01:22

I am using now this:

Code: Select all

; Purpose: Read binary data from a file (e.g. an exe file) and convert it 
; to a readable string which can be processed with AHK string commands. 
; To enable this the binary null is replaced by a character.
; The string can be used e.g. to view/explore/extract program resources.

NullRepl := "5F"		; Replace binary null by _

FileSelectFile, File, , %A_ScriptDir%, Select a binary file, *.exe

Gui Wait: Show, w500 h0, Just a second ...
VarCap := VarSetCapacity(Content,0)
FileRead Content, %File%
VarCap := VarSetCapacity(Content)
SetFormat Integer, H

Loop %VarCap%
  {
    If (Mod(A_Index, 2) <> 0)
      {
        Byte := SubStr(NumGet(Content, A_Index-1, "UChar"), 3) " "
        If (Byte = 0x0)
            Byte := NullRepl
        String .= Chr("0x" . Byte)
      }
  }

FileAppend, %String%, %A_ScriptDir%\String.txt

ExitApp
Post Reply

Return to “Ask For Help”