Read magic numbers

Peter Volz 1,295 Reputation points
2023-06-07T11:59:39.03+00:00

Hello all

I can't figure out how to make it done, to read 64 bytes of the file and check it against a list of magic numbers.

The key is that each signature/magic number has different length. but I wanna read the first 64 bytes only once.

    Public Function DetectMagicNo(ByVal InputFile As String) As String
        Using MyFileStream As New FileStream(InputFile, FileMode.Open, FileAccess.Read)
            Dim MyBytes(63) As Byte
            Dim OGG As Byte() = {&H4F, &H67, &H67, &H53}
            Dim PSD As Byte() = {&H38, &H42, &H50, &H53}
            Dim MP3 As Byte() = {&HFF, &HFB, &HFF, &HF3, &HFF, &HF2}
            '''full list
            MyFileStream.Read(MyBytes, 0, 64)
            Select Case True
                Case MyBytes.SequenceEqual(OGG)
                    DetectMagicNo = "OGG"
                Case MyBytes.SequenceEqual(PSD)
                    DetectMagicNo = "PSD"
                Case MyBytes.SequenceEqual(MP3)
                    'full case
            End Select
        End Using
    End Function

Anyone can help please :)

Developer technologies | VB
Developer technologies | C#
{count} votes

Accepted answer
  1. Dewayne Basnett 1,381 Reputation points
    2023-06-07T13:23:20.03+00:00

    Made a slight change to your Select that should help,

                Select Case True
                    Case MyBytes.Take(OGG.Length).SequenceEqual(OGG)
                        DetectMagicNo = "OGG"
                    Case MyBytes.Take(PSD.Length).SequenceEqual(PSD)
                        DetectMagicNo = "PSD"
                    Case MyBytes.Take(MP3.Length).SequenceEqual(MP3)
                        'full case
                End Select
    
    
    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-06-07T12:50:38.3266667+00:00

    Hi

    Seems you are trying to get an impossible equality. You are comparing a 64 byte sequence with a 4 (or 6 whatever) sequence which can never be equal.

    I added a Function that pairs down the 64 bytes for a comparison. I have only tested with one of the file types and works fine so maybe it would work fine for all.

    ' Form1 with Lavel1
    Option Strict On
    Option Explicit On
    Imports System.IO
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Label1.Text = DetectMagicNo("C:\Users\lesha\Desktop\sample-ogg.ogg")
    	End Sub
    	Public Function DetectMagicNo(InputFile As String) As String
    		Using MyFileStream As New FileStream(InputFile, FileMode.Open, FileAccess.Read)
    			Dim MyBytes(63) As Byte
    			Dim OGG As Byte() = {&H4F, &H67, &H67, &H53}
    			Dim PSD As Byte() = {&H38, &H42, &H50, &H53}
    			Dim MP3 As Byte() = {&HFF, &HFB, &HFF, &HF3, &HFF, &HF2}
    
    			MyFileStream.Read(MyBytes, 0, 64)
    
    			Select Case True
    				Case Tk(4, MyBytes).SequenceEqual(OGG)
    					Return "OGG"
    				Case Tk(4, MyBytes).SequenceEqual(PSD)
    					Return "PSD"
    				Case Tk(6, MyBytes).SequenceEqual(MP3)
    					Return "MP3"
    			End Select
    		End Using
    		Return "None"
    	End Function
    	Function Tk(n As Integer, bytes() As Byte) As Byte()
    		Dim b(n - 1) As Byte
    		For i As Integer = 0 To n - 1
    			b(i) = bytes(i)
    		Next
    		Return b
    	End Function
    End Class
    
    
    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.