Jump to content
Sign in to follow this  
AndyS01

StringReplace() does not work on binary data

Recommended Posts

AndyS01

I'm trying to replace all occurrences of one string in a binary file with another string (e.g. all "C:\" with "D:\"), but StringReplace() does nothing.

I opened my files in Binary mode and set the data read in to binary(), but nothing works.

My test script reads a compiled version of the script, does the replacements, then writes the results to an output file.

Prior to calling StringReplace(), I call StringInStr() to see if my 'from' string is found. The StringInStr() reports that it found the string, but the StringReplace() reports 0 replacements. Just to be sure, I did a binary compare (BeyondCompare) and the input and output files were exactly the same.

Here is my test code:

#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
Opt('MustDeclareVars', 1)

_Main()

Func _Main()
Local $infn, $infh, $outfn, $outfh, $cnt, $str_from, $str_to
Local $buffer, $offset = 0, $bufsize, $mcnt, $matches = 0, $ret

$infn = StringTrimRight(@ScriptName, 4) & ".exe"
$outfn = "test2_out.exe"

$infh = FileOpen($infn, 16 + 0)
If ($infh <= 0) Then Exit (1)

$outfh = FileOpen($outfn, 16 + 2) ; 2 = Write mode (erase previous contents)
If ($infh <= 0) Then Exit (1)

$str_from = "D:\"
$str_to = "C:\"
$bufsize = 2000

While (1)
FileSetPos($infh, $offset, 0)

$buffer = FileRead($infh, $bufsize)
If (@error == -1) Then ExitLoop
$cnt = @extended

$buffer = Binary($buffer)

$ret = StringInStr($buffer, $str_from)
If ($ret >= 0) Then ConsoleWrite("+++: 1 or more strings are in the buffer" & @CRLF)

$buffer = StringReplace( _
$buffer, _
$str_from, _
$str_to _
)

$mcnt = @extended
$matches += $mcnt
ConsoleWrite("+++: " & $mcnt & " replacement(s) made" & @CRLF)

FileWrite($outfh, $buffer)

$offset += $cnt
WEnd

FileClose($infh)
FileClose($outfh)

ConsoleWrite("+++: matches: " & $matches & @CRLF)
ConsoleWrite("+++: processed " & $offset & " bytes." & @CRLF)
EndFunc ;==>_Main

Share this post


Link to post
Share on other sites
BrewManNH

You're opening the file in binary mode, then converting what you read from the file to binary, then you're looking for an ASCII string in it, you might want to rething what you're looking for and what the contents of $buffer actually looks like.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Melba23

AndyS01,

As I read that code, you trying to use this to rewrite a compiled AutoIt file:

$infn = StringTrimRight(@ScriptName, 4) & ".exe" ; Remove .au3/.exe and replace it with .exe
$outfn = "test2_out.exe"

$infh = FileOpen($infn, 16 + 0) ; Open the .exe file

If that is indeed the case then you need to know that the script part of the compiled .exe is compressed before being added to the interpreter stub and so is very unlikely to hold the literal strings you seem to want to replace:

$str_from = "D:"
$str_to = "C:"

even if you use the correct format for the strings as BrewManNH has suggested. So I am not at all sure that you will ever succeed. ;)

What I have done in the past as a coding exercise was to store the basic text script in the resources of the compiled exe (using #AutoIt3Wrapper_Res_SaveSource) and then when needed extract it, modify it and then recompile the whole thing. But you are now into FileInstalling Aut2Exe and AutoItSC.bin which seriously increases the size of the compiled file. Are you sure that you want to go to so much trouble? :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
guinness

Just a note, if you do decide to use #AutoIt3Wrapper_Res_SaveSource (as Melba23 suggest,) then look at _GetSavedSource in my signature.


UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
AndyS01

Thank you for your suggestions. You're right, a compiled AutoIT file will not have what I want. However, making the compiled script an input file was just a test.

What I'm going to use my script for, is to replace "C:" with "D:" in a very large binary file. I will actually be editing it 'in place'.

I used a 'strings' utility on several .exe files until I found relatively small one with an instance of "D:". I used a copy of "C:WindowsSystem32poqexec.exe" as a test file. This file has 1 occurrence of "D:" (at offset 0x0950).

StringReplace() still didn't do any substitutions.

I'n not sure what 'binary()' buys me. Perhaps I should to a StringReplace() using patterns enclosed with 'binary()'.

Share this post


Link to post
Share on other sites
AndyS01

Well, after much experientation, I got a lot farther. I found that mixing Binary...() functions and String...() functions doesn't really work.

My new code, below, successfully finds all occurrences of '$str_from'. However, I cannot figure out how to replace binary($buffer) data with new data ($str_to).

I would want to say "BinaryMid($buffer, $x1, 3) = StringToBinary($str_to)", but apparently "BinaryMid($buffer, $x1, 3)" cannot be on the left side of an assignment (I get syntax errors when I try to run with this line in).

How can I replace data in a binary($buffer)?

#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
Opt('MustDeclareVars', 1)

_Main()

Func _Main()
   Local $infn, $infh, $outfn, $outfh, $cnt, $str_from, $str_to, $x1
   Local $buffer, $offset = 0, $bufsize, $matches = 0

   $infn = "poqexec.exe"
   $outfn = "test2_out.exe"

   $infh = FileOpen($infn, 16 + 0)
   If ($infh <= 0) Then Exit (1)

   $outfh = FileOpen($outfn, 16 + 2) ; 2 = Write mode (erase previous contents)
   If ($infh <= 0) Then Exit (1)

   $str_from = "D:"
   $str_to = "C:"
   $bufsize = 0x1000

   While (1)
      FileSetPos($infh, $offset, 0)

      $buffer = FileRead($infh, $bufsize)
      If (@error == -1) Then ExitLoop
      $cnt = @extended
      $buffer = Binary($buffer)

      For $x1 = 0 To $cnt - 3
         If (BinaryToString(BinaryMid($buffer, $x1, 3), 1) = $str_from) Then
            ConsoleWrite("+++: found it at 0x" & Hex($offset + $x1) & @CRLF)
            $matches += 1
            ; The next line replaces the 'found' data with the 'repace with' data
            ;BinaryMid($buffer, $x1, 1) = StringToBinary($str_to)
         EndIf
      Next

      FileWrite($outfh, $buffer)
      $offset += $cnt
   WEnd

   FileClose($infh)
   FileClose($outfh)

   ConsoleWrite("+++: matches:  " & $matches & @CRLF)
   ConsoleWrite("+++: processed " & $offset & " bytes." & @CRLF)
EndFunc   ;==>_Main

Share this post


Link to post
Share on other sites
AZJIO

Look

If the binary data, the lines must also be hexadecimal numbers. You can replace the text in the EXE-file, as if it were a string. I replaced and comparing the result was correct.

screenshot (Change in binary data)

Edited by AZJIO

Share this post


Link to post
Share on other sites
DrLarch

Look

If the binary data, the lines must also be hexadecimal numbers. You can replace the text in the EXE-file, as if it were a string. I replaced and comparing the result was correct.

screenshot (Change in binary data)

AZJIO - your utility works great, but I need to be able to modify a binary file from within my script. I dug through your code, but it's obviously beyond me :wacko: . I can easily read in a binary file to a variable and dump it back out to another file:

$hFile = FileOpen(@ScriptDir & "binaryfile1.tmp", 16)
$hFileContents = FileRead($hFile)
FileClose($hFile)

$nFile = FileOpen(@ScriptDir & "binaryfile2.tmp", 17)
FileWrite($nFile, $hFileContents)
FileClose($nFile)

... but how to replace/insert data within $hFileContents?! Could one split the binary data at the position to edit (into "before" & "after" variables), convert the replace/insert data (with _StringToHex) and then:

FileWrite($nFile, $hFileContentsBefore & $EditData & $hFileContentsAfter)

?? :think: I apologize, I'm clueless on dealing with binary data - can anyone provide an example? :wacko:

Share this post


Link to post
Share on other sites
DrLarch

Never mind, I think I figured it out:

#include 
#include 
$Insert = "Blahblahblah"
$InsertHex = _StringToHex($Insert)

$nFile = FileOpen(@ScriptDir & "test.bin", 18)
FileWrite($nFile, "0x0B00000000" & $InsertHex & "000D0A000000")
FileClose($nFile)

I didn't understand the hex format and how AutoIt handles binary data. Now I know a little bit more via trial and error. :unsure:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • milos83
      By milos83
      Default keyword for optional parameter is interpreted wrongly.
      ConsoleWrite(StringReplace("aa", "a", "b", Default, 1) & @CRLF) StringReplace ( "string", "searchstring/start", "replacestring" [, occurrence = 0 [, casesense = 0]] ) The code above will output ab even thou the default value for the occurrence is 0 (replace all).
      Of course using zero instead of Default will work fine.
    • smartkey
      By smartkey
      Hi All,
           I have written a UDF for one of my requirement which replaces a single character in string with a sub string/another character. 
           I am using this for my requirement by calling below function as StrReplace("C:\Software\Autoit\Substr","\","\\") and gives result as C:\\Software\\Autoit\\Substr
           Please let me know if this can be improvised or any mistakes to correct.   
      ;===============================================================================
      ;
      ; Function Name:    StrReplace($INPUT_STRING)
      ; Description:      This function is to replace a character with another in a string.
      ; Parameter(s):     $INPUT_STRING     - Original String Value
      ;                    $STR_2_FIND     - Single Character to find the $INPUT_STRING
      ;                    $STR_2_REPLACE     - Substring/Multiple Characters to replace in place of     $STR_2_FIND value
      ; Requirement(s):   Replacing one single Character in a string with multiple Characters
      ; Return Value(s):  success - Output string after replacing a character with required character
      ;                    failure - 0
      ; Author(s):        smartkey
      ;
      ;===============================================================================
      Func StrReplace($INPUT_STRING, $STR_2_FIND, $STR_2_REPLACE)
          Local $OUTPUT_STRING = ""
          If StringLen($INPUT_STRING) > 0 Then
              If StringMid($INPUT_STRING,1,1) = $STR_2_FIND Then
                  $OUTPUT_STRING = $OUTPUT_STRING & $STR_2_REPLACE
              Else
                  $OUTPUT_STRING = StringMid($INPUT_STRING,1,1)
              EndIf
              For $i=2 to StringLen($INPUT_STRING)
                  If StringMid($INPUT_STRING,$i,1) = $STR_2_FIND Then
                      $OUTPUT_STRING= $OUTPUT_STRING & $STR_2_REPLACE
                  Else
                      $OUTPUT_STRING= $OUTPUT_STRING & StringMid($INPUT_STRING,$i,1)
                  EndIf
              Next
              Return $OUTPUT_STRING
          Else
              Return 0
          EndIf
      EndFunc
    • FMS
      By FMS
      Hello,
      I'm trying to read a binary file to an array but couln't get it to work.
      Also I coul not find any help in the forum around this subject whish was helpfull.
      Is there any way it could be done?
      I tried a lot of ways but maybe somebody know's the right way?
      #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <File.au3> #include <Array.au3> #include <AutoItConstants.au3> Local $in=FileOpen("TEST_labels.idx1-ubyte",16) ; 16+0=Read binary Local $data = FileRead($in) Local $FileArray = BinaryToString($data,4) ;~ $FileArray = StringSplit($BinarydData, @CRLF, 1+2) ;~ Local $FileArray = StringRegExp($BinarydData, "[^\r\n]+", 3) FileClose($in) _ArrayDisplay($FileArray,"$FileArray","",32) MsgBox($MB_SYSTEMMODAL, "", "$FileArray = " & $FileArray )  
      TEST_labels.idx1-ubyte
    • Amixg
      By Amixg
      Hi! I have another problem with AutoIT.  You see, I'm still the one who's just starting out with this great programming language. I have a problem with AutoIT, it seems not to recognize the "StringReplace" function even when it is by default. The code was made half in KODA and half of my own. It is a software programmed for the Italian language (I am Italian) and translate the SMS language into Italian correct. Would you help me? Thanks in advance.
       
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $hull = "hello" StringReplace($hull, "hello", "hellx") $Form1 = GUICreate("Linguaggio SMS A Italiano", 507, 498, 192, 124) $Edit1 = GUICtrlCreateEdit("", 24, 40, 465, 177) GUICtrlSetData(-1, "") $Label1 = GUICtrlCreateLabel("Inserisci qui il testo che non riesci a capire:", 24, 8, 205, 17) $Label2 = GUICtrlCreateLabel("Ecco il testo tradotto(non toccare questo campo):", 27, 237, 237, 17) $Edit2 = GUICtrlCreateEdit("", 24, 264, 465, 137) GUICtrlSetData(-1, "") $Button1 = GUICtrlCreateButton("Traduci", 104, 424, 249, 49) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 StringReplace(GUICtrlGetData($Label1), "qlc", "qualcuno") StringReplace(GUICtrlGetData($Label1), "qls", "qualcosa") StringReplace(GUICtrlGetData($Label1), "ke", "che") StringReplace(GUICtrlGetData($Label1), "x", "per") StringReplace(GUICtrlGetData($Label1), "pls", "per favore") StringReplace(GUICtrlGetData($Label1), "tu6", "tu sei") StringReplace(GUICtrlGetData($Label1), "zzz", "mi fai dormire") StringReplace(GUICtrlGetData($Label1), "hagn", "Buonanotte") StringReplace(GUICtrlGetData($Label1), "tvtb", "Ti voglio tanto bene") StringReplace(GUICtrlGetData($Label1), "tat", "Ti amo tanto") StringReplace(GUICtrlGetData($Label1), "lafs", "Amore a prima vista") StringReplace(GUICtrlGetData($Label1), "fli?", "Flirtiamo?") StringReplace(GUICtrlGetData($Label1), "msidt", "Mi sono innamorato di te") StringReplace(GUICtrlGetData($Label1), "ba", "Bacio") StringReplace(GUICtrlGetData($Label1), "midi", "Mi dispiace.") StringReplace(GUICtrlGetData($Label1), "ntm", "Non ti merito") StringReplace(GUICtrlGetData($Label1), "tdp", "Togliti dai piedi.") StringReplace(GUICtrlGetData($Label1), "amò", "Amore") StringReplace(GUICtrlGetData($Label1), "ap", "A presto!") StringReplace(GUICtrlGetData($Label1), "cmq", "comunque") StringReplace(GUICtrlGetData($Label1), "cvd", "Ci vediamo dopo") StringReplace(GUICtrlGetData($Label1), "Tvb", "Ti voglio bene") StringReplace(GUICtrlGetData($Label1), "nn", "non") StringReplace(GUICtrlGetData($Label1), "risp", "rispondimi") StringReplace(GUICtrlGetData($Label1), "cel", "cellulare") StringReplace(GUICtrlGetData($Label1), "dom", "qualcosa") StringReplace(GUICtrlGetData($Label1), "nm", "numero") StringReplace(GUICtrlGetData($Label1), "fv", "favore") StringReplace(GUICtrlGetData($Label1), "-male", "meno male") StringReplace(GUICtrlGetData($Label1), "disc", "discoteca") StringReplace(GUICtrlGetData($Label1), "se#", "settimana") StringReplace(GUICtrlGetData($Label1), "+ o -", "più o meno") StringReplace(GUICtrlGetData($Label1), "ts", "torno subito") StringReplace(GUICtrlGetData($Label1), "tvtbcoa", "Ti voglio tanto bene come amica") EndSwitch WEnd  
    • Dragonfighter
      By Dragonfighter
      I'm searching a way to do xor and shift and if possible also other operations. Thanks in advance for the replies.
×