[Help] Search for data in binary file menu

User Tag List

Results 1 to 6 of 6
  1. #1
    Fenryr's Avatar Member
    Reputation
    58
    Join Date
    Mar 2007
    Posts
    114
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Help] Search for data in binary file

    Basically what i want to do is take a file, and be able to find 4 consecutive hexadecimal values (For example, 44 66 34 25) and have the program tell me where in the file that sequence appears. Any help would be appreciated, or tell me if i didn't explain the situation well enough. I want to be able to do this in VB 2008 or VB 6, preferably 2008.
    Last edited by Fenryr; 11-07-2008 at 03:52 PM.

    [Help] Search for data in binary file
  2. #2
    Fenryr's Avatar Member
    Reputation
    58
    Join Date
    Mar 2007
    Posts
    114
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, i kinda figured out the answer to my question, but it could be very inefficient when working with bigger files. Basically i just have the program check to see if the current value is the target amount, and if it is not, then it adds one to the current value. Here's some code to explain:

    Code:
            Dim fileContents As Byte()
            fileContents = My.Computer.FileSystem.ReadAllBytes("C:\Test.txt")
            Dim h As Integer = 0
    
            If fileContents(h) <> &HB5 And fileContents(h + 1) <> &HF6 Then
                Do Until fileContents(h) = &HB5 And fileContents(h + 1) = &HF6
                    h = h + 1
                Loop
    
            End If
            If fileContents(h) = &HB5 And fileContents(h + 1) = &HF6 Then
                MsgBox("yay, success " & Hex(h))
            End If
    It works, but barely and only for the first time the sequence appears in a file, and if it dosent appear at all, the program crashes from an overflow error. Can anyone give advice, or help me refine it?

  3. #3
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    ''' <summary>
    ''' Gets the offsets of a specified byte array in a file.
    ''' </summary>
    ''' <param name="file">The file to search in.</param>
    ''' <param name="bytes">The bytes to search for.</param>
    ''' <returns>A list containing the offsets for the byte pattern specified.</returns>
    Public Shared Function GetOffsets(ByVal file As String, ByVal bytes As Byte()) As List(Of Long)
        Dim ret As New List(Of Long)()
        Using s As Stream = New FileStream(file, FileMode.Open)
            Using br As New BinaryReader(s)
                While True
                    Try
                        ' Save the current position so we can add it
                        ' if this offset is the spot we want.
                        ' Also so we can go back to here, if it's not.
                        Dim pos As Long = br.BaseStream.Position
                        
                        ' Read in the same amount of bytes that we have
                        ' in the bytes param.
                        Dim ba As Byte() = br.ReadBytes(bytes.Length)
                        
                        Dim ok As Boolean = True
                        ' Small error check here.
                        If bytes.Length = ba.Length Then
                            For i As Integer = 0 To bytes.Length - 1
                                If ba(i) <> bytes(i) Then
                                    ok = False
                                    ' Jump out immediately.
                                    ' It's not what we want, so there's no need to check more.
                                    Exit For
                                End If
                            Next
                        End If
                        If ok Then
                            ' Add the position we got these 4 bytes from.
                            ret.Add(pos)
                        End If
                        ' Since the BinaryReader has jumped from pos to (pos + bytes.Length)
                        ' we need to go back to where we were, plus 1 offset to read again.
                        br.BaseStream.Position = pos + 1
                    Catch generatedExceptionName As EndOfStreamException
                        ' End of file
                        Exit Try
                    End Try
                End While
            End Using
        End Using
        Return ret
    End Function
    You can now just use

    Code:
    Public Sub test()
        Dim bytes As Byte() = New Byte() {44, 66, 34, 25}
        Dim offsets As List(Of Long) = GetOffsets("C:\MyFileToCheck.extension", bytes)
        ' Do whatever with the offsets here.
    End Sub
    Let me know if you have any issues.

    Of course, you need to import the proper namespaces. (System.IO, and System.Collections.Generic, probably System as well.)

  4. #4
    Fenryr's Avatar Member
    Reputation
    58
    Join Date
    Mar 2007
    Posts
    114
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, it seems to work, but how can i now use the offsets, like if i wanted to view them?

    EDIT: it's actually not working, when i use it, the form freezes, then goes white and the title bar says not responding. but it may just be because i'm not using it correctly.
    Last edited by Fenryr; 11-08-2008 at 01:33 AM.

  5. #5
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Large files can do that. (I suggest running the method in a separate thread.)

    And using the offsets is up to you. Look at how I opened the BinaryReader object, and set the br.BaseStream.Position property to the offset. (Same idea applies.)

  6. #6
    Fenryr's Avatar Member
    Reputation
    58
    Join Date
    Mar 2007
    Posts
    114
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But i mean just using that code on a small 20kb text file where the sequence appears it still crashes. But i have made my own code based on what you gave me and it seems to work on small files, but with larger ones it just opens a message box with nothing in it, rather than the offsett.

    I would put it in a code box but it's too wide at some parts






    Dim h As Integer = &H0
    Dim offset(255) As String
    Function searchfile()

    Dim fileContents As Byte()
    fileContents = My.Computer.FileSystem.ReadAllBytes("C:\test.txt")



    If fileContents(h) <> &H4B And fileContents(h + 1) <> &H4E And fileContents(h + 2) <> &H43 And fileContents(h + 3) <> &H4D Then
    Do Until fileContents(h) = &H4B And fileContents(h + 1) = &H4E And fileContents(h + 2) = &H43 And fileContents(h + 3) = &H4D
    h = h + 1
    Loop

    End If

    If fileContents(h) = &H4B And fileContents(h + 1) = &H4E And fileContents(h + 2) = &H43 And fileContents(h + 3) = &H4D Then

    offset(0) = Hex(h)
    End If


    End Function





    then to use:
    Code:
        
    
      searchfile()
    
            MsgBox(offset(0))
    its a little messy but it seems to work very well on small files( finds 4b 4e 43 4d in a file)

Similar Threads

  1. [Hack] Searching for a hack that Works for 1.12.1 HELP OUT LADS :D
    By elamericano in forum World of Warcraft Bots and Programs
    Replies: 3
    Last Post: 07-06-2011, 12:03 PM
  2. [Help] Looking For 2 Files
    By merfed in forum WoW ME Questions and Requests
    Replies: 2
    Last Post: 03-02-2008, 09:22 PM
  3. HELP: [Leggings of the Ursa] blp file!
    By mmowns in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 01-14-2007, 05:04 PM
  4. Searching for Voice Actors/In-Game Actors
    By Örpheus in forum World of Warcraft General
    Replies: 1
    Last Post: 08-01-2006, 10:03 AM
All times are GMT -5. The time now is 03:06 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search