don't click here

VB6 help: Find a string in a binary file?

Discussion in 'Technical Discussion' started by Oerg866, Jan 18, 2010.

  1. I need to find the following string in a binary file.

    Code (Text):
    1. SEGA SEGAKATANA SEGA ENTERPRISES
    How do I do that using VB6? I've seen methods for text files using "Line Input" and then some other simple stuff, but this is not a text file.

    Halp plz?

    cheers :)
     
  2. Andlabs

    Andlabs

    「いっきまーす」 Wiki Sysop
    2,175
    2
    0
    Writing my own MD/Genesis sound driver :D
    Why use vb6 when you can use the strings tool? My strings has an -o option to get the offset into the file. I'm pretty sure you can pipe the output into vb6; if not, then just write the output to a text file and read it in.
     
  3. what is that "strings" thing I keep hearing about?
     
  4. nineko

    nineko

    I am the Holy Cat Tech Member
    6,421
    585
    93
    italy
    OPEN "WHATEVER" FOR BINARY AS #1
    A$="SEGA SEGAKATANA SEGA ENTERPRISES"
    B$=SPACE$(32)
    FOR I& = 1 TO LOF(1)
    GET #1,I&,B$
    IF A$=B$ THEN
    MSGBOX "YAY YOU FOUND THE STRING"
    EXIT FOR
    ENDIF
    NEXT
    IF I& = LOF(1)+1 THEN MSGBOX "YOU DIDN'T FIND THE STRING D:"
    CLOSE #1
     
  5. Oh god I'm so stupid. Thanks nineko.

    (slow solution though =P)
     
  6. Andlabs

    Andlabs

    「いっきまーす」 Wiki Sysop
    2,175
    2
    0
    Writing my own MD/Genesis sound driver :D
    A Unix program that finds all ASCII strings in a binary file. For example:

    Code (Text):
    1. $ strings /bin/stty | tail
    2. -aefg
    3. aef:g
    4. stdin isn't a terminal
    5. TIOCGETD
    6. TIOCGWINSZ
    7. illegal option -- %s
    8. tcsetattr
    9. TIOCGWINSZ: %s
    10. gfmt1
    11. stdout appears redirected, but stdin is the control descriptor
     
  7. nineko

    nineko

    I am the Holy Cat Tech Member
    6,421
    585
    93
    italy
    Yes.

    You're welcome.

    Yes. Very slow. A much faster alternative would be this, given that your file isn't too big:

    OPEN "WHATEVER" FOR BINARY AS #1
    A$="SEGA SEGAKATANA SEGA ENTERPRISES"
    B$=SPACE$(LOF(1)+32)
    GET #1,,B$
    FOR I&=1 TO LOF(1)
    IF MID$(B$,I&,32)=A$ THEN
    MSGBOX "YAY YOU FOUND THE STRING"
    EXIT FOR
    ENDIF
    NEXT
    IF I& = LOF(1)+1 THEN MSGBOX "YOU DIDN'T FIND THE STRING D:"
    CLOSE #1

    Note that you can't combine the two approaches like I once suggested you for another problem, because this time the string you are looking for might be between two blocks if you are particularly unlucky so no -- don't do that. Use either this or the other approach.
    And yes, I know that adding 32 to the LOF(1) makes the string bigger for no reason. But this gives you a much better FOR in return.
     
  8. Yeah well the files are ranging from 40MB (if lucky) to 1GB (if unlucky).... :S

    I'm trying the first solution on a 400MB file right now and I am doing my homework while it's doing that.. Expecting it to be done by then =P

    EDIT.EXE: I think you can fix the problem by dividing the file in chunks (like 1MB).

    Then you scan, and the next chunk will start at Current Offset + 1MB - 32 bytes.

    ???

    Profit?
     
  9. nineko

    nineko

    I am the Holy Cat Tech Member
    6,421
    585
    93
    italy
    Yes.
     
  10. Andlabs

    Andlabs

    「いっきまーす」 Wiki Sysop
    2,175
    2
    0
    Writing my own MD/Genesis sound driver :D
    What if the string crosses the 1MB boundary?
     
  11. nineko

    nineko

    I am the Holy Cat Tech Member
    6,421
    585
    93
    italy
    That was my original concern. Oerg came up with a solution that, while not elegant, works. The chunks will overlap by 32 bytes.

     
  12. EDIT: Shit was all wrong. This works:

    Dim ding As Long

    Open Dir1.Path + "\" + File1.FileName For Binary As #1
    A$ = "SEGA SEGAKATANA SEGA ENTERPRISES"
    B$ = Space$(65504)
    Calculators:
    parts% = Int((LOF(1) - 65504) / 65504)
    rest% = (LOF(1) - 65536) Mod 65504
    ding = 1
    While Not EOF(1)
    c$ = Space$(65536)
    Get #1, ding, c$
    For I = 1 To Len(c$) - 31
    If Mid$(c$, I, 32) = A$ Then
    r$ = "4DB7 GD-ROM1/1 JUE "
    Get #1, ding + I + 31, r$
    Put #1, , "0799A10"
    MsgBox "Done patching. Use at own risk! If you have a CRT and the signal is bad, you might damage it!"
    Close #1
    Exit Sub
    End If
    Next I
    ding = ding + 65504
    Wend

    MsgBox "Couldn't find SEGAKATANA identifyer string. Are you sure that this is a dreamcast image?"
    Close #1

    EDIT: Hahaha, this patched a 725MB image in like half a second :D - Thanks guys! :)
     
  13. Syniphas

    Syniphas

    also known as Svetlana Member
    28
    0
    0
    Brazil
    what are you even trying to do here oerg
     
  14. patch games to work with VGA
     
  15. Yuzu

    Yuzu

    Member
    2,554
    59
    28
    Damn it you're awesome, if only we had people in the PS2 scene who patched games to work with VGA.

    The only games that work on PS2 VGA are Tekken 4 and 2 built in Linux games. And there's some retail disc which supports like 7 games.
     
  16. Look at the code, it's just overwriting a string =P

    Most games have proper VGA init code and stuff but the string doesn't imply it's compatible with it, so the DC displays an error message. The patcher fixes it.