Page 1 of 3

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 3:12 pm
by netmaestro
I didn't test it but this looks reasonable:

Code: Select all

length = 32 ; or whatever it is

*searchdata  = AllocateMemory(length)
*replacedata = AllocateMemory(length)
*testdata    = AllocateMemory(length)

;///////////////////////////////////////////////////////////
; Here place your search and replace data in the mem blocks 
;///////////////////////////////////////////////////////////

If OpenFile(0, "myfile.bin")
  readpos = 0
  thiseof = Lof(0) - length
  While (Not Eof(0)) And readpos <= thiseof
    FileSeek(0, readpos)
    ReadData(0, *testdata, length)
    If CompareMemory(*testdata, *searchdata, length)
      WriteData(readpos, *replacedata, length)
    EndIf
    readpos + 1
  Wend
  CloseFile(0)
EndIf

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 3:27 pm
by wallgod
Thank you, netmaestro. Much appreciated! However, I'm still a little confused on how to use this method. I understand the logic, but specifying what to look for and what to replace it with, is where I'm confused now. Would you mind giving me an example of how an actual search replace would look? I do have the HEXerTools library installed, if that helps.

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 3:31 pm
by netmaestro
Note that I fixed a typo which would cause it to fail. Afaik an actual example would look like this one here:

http://www.purebasic.fr/english/viewtop ... 76&start=3

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 3:32 pm
by wallgod
netmaestro wrote:Note that I fixed a typo which would cause it to fail. Afaik an actual example would look like this one here:

http://www.purebasic.fr/english/viewtop ... 76&start=3
LOL, okay okay... I'll play with it. Point taken. Thanks again.

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 3:43 pm
by wallgod
:(
I don't know how to place data into memory blocks.

Code: Select all

*searchdata = "hello"
*replacedata = "there"
returns the error "trying to write a string into a numerical variable"

Sigh... the embarrassment of being a n00b. :oops:

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 4:10 pm
by rsts
wallgod wrote::(
I don't know how to place data into memory blocks.

Code: Select all

*searchdata = "hello"
*replacedata = "there"
returns the error "trying to write a string into a numerical variable"

Sigh... the embarrassment of being a n00b. :oops:

Code: Select all

*searchdata = @"hello"
*replacedata = @"there"
cheers

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 4:22 pm
by wallgod
rsts wrote:
wallgod wrote::(
I don't know how to place data into memory blocks.

Code: Select all

*searchdata = "hello"
*replacedata = "there"
returns the error "trying to write a string into a numerical variable"

Sigh... the embarrassment of being a n00b. :oops:

Code: Select all

*searchdata = @"hello"
*replacedata = @"there"
cheers
Ahhh... merci! What does that @ actually mean?

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 4:27 pm
by rsts
When you set *searchdata = "hello" you're attempting to set the string "hello" as a memory address, thus the error.

setting *searchdata = @"hello" sets the memorypointer *searchdata to the location in memory of the string "hello" or @(the memory location of)"hello"

cheers

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 4:46 pm
by wallgod
rsts wrote:When you set *searchdata = "hello" you're attempting to set the string "hello" as a memory address, thus the error.

setting *searchdata = @"hello" sets the memorypointer *searchdata to the location in memory of the string "hello" or @(the memory location of)"hello"

cheers
Oh okay, so if i set *searchdata = @"hello", it jumps to the first position in the memory block containing that string?

BTW, I tested this and it went very slowly and ultimately did not change anything in the file.

Code: Select all

Procedure.s AsciiToHex(binData$)
    tdata$ = Space(Len(binData$)*2)
    BinToHex(@binData$,Len(binData$),@tdata$,#False)
    ProcedureReturn tdata$
EndProcedure

Procedure.s HexToAscii(hexdata$)
    hdata$ = Space(Len(hexdata$))
    HexToBin(@hexdata$,Len(hexdata$),@hdata$)
    ProcedureReturn hdata$
EndProcedure

length = 7 ; or whatever it is

*searchdata  = AllocateMemory(length)
*replacedata = AllocateMemory(length)
*testdata    = AllocateMemory(length)

;///////////////////////////////////////////////////////////
*searchdata = @HexToAscii("F606017415538D")
*replacedata = @HexToAscii("8026007415538D")
MessageRequester("","get here?",#MB_OK)
;///////////////////////////////////////////////////////////

If OpenFile(0, "test.dll")
  readpos = 0
  thiseof = Lof(0) - length
  While (Not Eof(0)) And readpos <= thiseof
    FileSeek(0, readpos)
    ReadData(0, *testdata, length)
    If CompareMemory(*testdata, *searchdata, length)
      WriteData(readpos, *replacedata, length)
    EndIf
    readpos + 1
  Wend
  CloseFile(0)
EndIf
Any ideas? I'm sure the search string (in hex) is in the file.
Image

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 5:06 pm
by rsts
Would need to see bin2hex and hex2bin as I'm not sure what's going on here.

May be some unnecessary or incorrect conversions occuring.

cheers

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 6:11 pm
by wallgod
I use HEXerTools for the hex stuff (bin2hex and hex2bin), and I've tested it to be working correctly, for normal strings at least. I'd be happy to use binary or some other approach. What do you folks suggest for this? Keep in mind the offset will not always be the same, so it would need to be a search/replace.

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 6:38 pm
by cas
Try this, i didn't tested it:

Code: Select all

ToFind$       = "F606017415538D"
ToReplace$    = "8026007415538D"

length        = Len(ToFind$)/2

*searchdata   = AllocateMemory(length)
*replacedata  = AllocateMemory(length)
*testdata     = AllocateMemory(length)

;///////////////////////////////////////////////////////////
; Here place your search and replace data in the mem blocks

For k=0 To length-1
  PokeB(*searchdata+k ,Val("$"+PeekS(@ToFind$+(k*2)   ,2)))
  PokeB(*replacedata+k,Val("$"+PeekS(@ToReplace$+(k*2),2)))
Next

;///////////////////////////////////////////////////////////

If OpenFile(0, "myfile.bin")
  readpos = 0
  thiseof = Lof(0) - length
  While (Not Eof(0)) And readpos <= thiseof
    FileSeek(0, readpos)
    ReadData(0, *testdata, length)
    If CompareMemory(*testdata, *searchdata, length)
      WriteData(0, *replacedata, length)
    EndIf
    readpos + 1
  Wend
  CloseFile(0)
EndIf

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 7:29 pm
by wallgod
cas wrote:Try this, i didn't tested it:

Code: Select all

ToFind$       = "F606017415538D"
ToReplace$    = "8026007415538D"

length        = Len(ToFind$)/2

*searchdata   = AllocateMemory(length)
*replacedata  = AllocateMemory(length)
*testdata     = AllocateMemory(length)

;///////////////////////////////////////////////////////////
; Here place your search and replace data in the mem blocks

For k=0 To length-1
  PokeB(*searchdata+k ,Val("$"+PeekS(@ToFind$+(k*2)   ,2)))
  PokeB(*replacedata+k,Val("$"+PeekS(@ToReplace$+(k*2),2)))
Next

;///////////////////////////////////////////////////////////

If OpenFile(0, "myfile.bin")
  readpos = 0
  thiseof = Lof(0) - length
  While (Not Eof(0)) And readpos <= thiseof
    FileSeek(0, readpos)
    ReadData(0, *testdata, length)
    If CompareMemory(*testdata, *searchdata, length)
      WriteData(0, *replacedata, length)
    EndIf
    readpos + 1
  Wend
  CloseFile(0)
EndIf
Well this code made a change in the file unlike the other one, but for some odd reason it didn't change the file where I expected. Instead of replacing:

[F606017415538D]
with
[8026007415538D]

...it ended up replacing:

[4E0CE884CB0000]
with
[8026007415538D]

I have no idea how that happened. Also, it still takes a long time; I assume this is because it's reading one byte at a time. Is there any way to perhaps read the entire chunk of the file in at once and then get the search string position(s) faster for replacing?

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 7:41 pm
by cas
Looks like we missed FileSeek(0, readpos) before WriteData(0, *replacedata, length). Try adding this line and post results. Yes, you are right, it is for small files and for larger ones it would be much faster to read file in memory.

Re: Search/replacing data in a binary file?

Posted: Wed Oct 06, 2010 7:52 pm
by wallgod
cas wrote:Looks like we missed FileSeek(0, readpos) before WriteData(0, *replacedata, length). Try adding this line and post results. Yes, you are right, it is for small files and for larger ones it would be much faster to read file in memory.
Yep, that did the trick. Thank you!

Code: Select all

ToFind$       = "F606017415538D"
ToReplace$    = "8026007415538D"

length        = Len(ToFind$)/2

*searchdata   = AllocateMemory(length)
*replacedata  = AllocateMemory(length)
*testdata     = AllocateMemory(length)

For k=0 To length-1
  PokeB(*searchdata+k ,Val("$"+PeekS(@ToFind$+(k*2)   ,2)))
  PokeB(*replacedata+k,Val("$"+PeekS(@ToReplace$+(k*2),2)))
Next

If OpenFile(0, "myfile.bin")
  readpos = 0
  thiseof = Lof(0) - length
  While (Not Eof(0)) And readpos <= thiseof
    FileSeek(0, readpos)
    ReadData(0, *testdata, length)
    If CompareMemory(*testdata, *searchdata, length)
      FileSeek(0, readpos)
      WriteData(0, *replacedata, length)
    EndIf
    readpos + 1
  Wend
  CloseFile(0)
EndIf
How do we read the entire file at once in memory to speed things up? It's my understanding that due to chr(0), it's not practical to use a string, so how would this work?