File Header using V...
 
Notifications
Clear all

File Header using VB.NET

5 Posts
4 Users
0 Reactions
1,349 Views
(@thexplanet)
New Member
Joined: 14 years ago
Posts: 1
Topic starter  

Hello All,

I am trying to code a tool which will detect the files on the basis of its Header value(Magic number), and not File extension or MIME type.

however, I am unable to read the first 4-bytes using VB.NET

I tried StreamReader/BinaryReader for the same however it starts at offset 0, and it fetches entire file eof (-1).
and i just need the first 4-bytes only.

if anyone experienced in coding please help me with a small piece of code.

Thanks.


   
Quote
(@ash368)
Active Member
Joined: 20 years ago
Posts: 17
 

I don't code in VB, but in Pascal you need to declare what you intend to read in. Since you want the first four bytes this would equate to a 32bit integer and this would be the variable you want to read in.

In this instance

var
HeaderInteger;

Load your file into a memory or file stream. The standard header for a jpeg would be FFD8FFE0, but what you actually want to read in is the integer value which is 3774863615.

Go to the first byte in the file which would be file.position = file.size-file.size
Then read in the first four bytes

file.read(header,Sizeof(header));

if header = 3774863615 then

show message('This is a JPG header);

Hope this helps.


   
ReplyQuote
MDCR
 MDCR
(@mdcr)
Reputable Member
Joined: 15 years ago
Posts: 376
 

See Fileopen and read the part about random access and the recordlength parameter (set it to 4)
http//msdn.microsoft.com/en-us/library/afh37kh8%28v=VS.90%29.aspx

Then see the example on this page
http//msdn.microsoft.com/en-us/library/y1swwyc4%28v=VS.90%29.aspx


   
ReplyQuote
 Ddan
(@ddan)
Eminent Member
Joined: 14 years ago
Posts: 42
 

Hi,

Assuming you've got the filestream/binary reader part working, then what you need is

'if image is the filestream and br is the binary reader of image

image.Position=0 'set to beginning of file
magic=br.ReadUint32 'read 4 bytes, and move position by 4

'think this gives little-endian format if this is important

Ddan


   
ReplyQuote
 Ddan
(@ddan)
Eminent Member
Joined: 14 years ago
Posts: 42
 

Hi,

Assuming you've got the filestream/binary reader part working, then what you need is

'if image is the filestream and br is the binary reader of image

image.Position=0 'set to beginning of file
magic=br.ReadUint32 'read 4 bytes, and move position by 4

'think this gives little-endian format if this is important

Ddan


   
ReplyQuote
Share: