Jump to content

Replacing string in Binary


Ultratuga
 Share

Recommended Posts

Hi,

I´m don´t know if this is a simple or complicated question.

I build this script, copy pasting from posts of this forum.

I´m trying to replace a string in a binary file. The string I want to replace is "SMT_MAIN.DRW". The script replaces the string well.

If I replace the string for one with the same number of characters, everything works great.

But if the replacement string is longer or shorter, binary file becomes invalid.

Can this be easily solved ?

I attached the binary file toosmt_main.7z

#include <String.au3>

$sInFile = @ScriptDir & "\SMT_MAIN.drw" ; Original in file

$Orig = "SMT_MAIN.PRT"
$sFind = _StringToHex($Orig)
$Insert = "SMT_MAI4.PRT"
$sReplace = _StringToHex($Insert)
$sOutFile = @ScriptDir & "\SMT_MAIN1.DRW" ; New edited out file

_BinaryReplace($sInFile, $sFind, $sReplace, $sOutFile)

Func _BinaryReplace($sInFile, $sFind, $sReplace, $sOutFile)
    Local $FO, $FR
    $FO = FileOpen($sInFile, 16) ; Open the file in binary read mode
    $FR = FileRead($FO) ; Read the opened file into a variable
    FileClose($FO) ; Close the opened file now we've read it
    $FR = StringReplace($FR, $sFind, $sReplace, 1); find and replace the hex string
    $FO = FileOpen($sOutFile, 18) ; Open a new empty file in binary write mode
    FileWrite($FO, $FR) ; Write the data to the new opened file
    FileClose($FO) ; Close the newly created file.
EndFunc
Link to comment
Share on other sites

Maybe you´re right, and this can´t be done.

I was thinking in trying to split the file in three parts, the string, the code before and the code after. Then create a new file with

code before string &replacement String &code after string, but I doubt this will work...

Link to comment
Share on other sites

Maybe you´re right, and this can´t be done.

I was thinking in trying to split the file in three parts, the string, the code before and the code after. Then create a new file with

code before string &replacement String &code after string, but I doubt this will work...

 

there is no need to do that, the file is already shrinked or expanded according to the size of the replaced string (there isn't overlay of bytes),

the problem that arise when the replaced string has different length maybe is due to the fact that previous data, no longer has the same previous offset location within the file, but it's at a shifted location, and this perhaps will render some pointers within the file to point to an invalidated location.

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

If i look at the file its always on this line ending with a continuation character in the binary and only seems to occur 1 time in the file

#- CMNM 00csmt_main.drw                                              \

So I would suggest to padd to equal length replacement string by adding spaces

Link to comment
Share on other sites

If i look at the file its always on this line ending with a continuation character in the binary and only seems to occur 1 time in the file

#- CMNM 00csmt_main.drw                                              \

So I would suggest to padd to equal length replacement string by adding spaces

 

Good idea if replacing string is shorter.

Problem remains if the replacing string is longer than the replaced.

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...