Page 1 of 1

FileRead & binary data

Posted: 22 May 2018, 01:30
by newbieforever
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?

Re: FileRead & binary data

Posted: 22 May 2018, 23:56
by jeeswg
- 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

Re: FileRead & binary data

Posted: 23 May 2018, 00:20
by nnnik
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.

Re: FileRead & binary data  Topic is solved

Posted: 23 May 2018, 01:22
by newbieforever
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

Re: FileRead & binary data

Posted: 20 Sep 2022, 09:54
by b0dhimind
Is there a version of this that works at a string level instead of byte level?
I copied rich text from OneNote, which is basically binary when we load ClipboardAll to a file, and want to do some mass replace based on the strings searched and then have it automatically load back to the clipboard for pasting.

I did see some things related to Binread but couldn't make sense of it and not sure if it would work with string.