in reply to Comparing the bytes of a file
Someone just gave a good answer to a similar question on the perl.beginners list today. It was how to detect jpg files, but the method should work for you
#!/usr/bin/perl #all .gif, .jpg and .png file have a special header id that identify t +heir #types. for example, starting from the 7th byte of a JPEG file, you sh +ould #see the special id as: ox4A 0x46 0x49 0x46 and 0x00, the following ch +ecks #for that: my $t = undef; my($one,$two,$three,$four,$five); open(IMG,"$ARGV[0]") || die $!; sysread(IMG,$t,6); #-- discard the first 6 bytes #-- read the next five bytes sysread(IMG,$one,1); sysread(IMG,$two,1); sysread(IMG,$three,1); sysread(IMG,$four,1); sysread(IMG,$five,1); #-- unpack them and make sure they are the right magic ID #-- you can get the id from the JPEG specification if(unpack("C",$one) == 74 && unpack("C",$two) == 70 && unpack("C",$three) == 73 && unpack("C",$four) == 70 && unpack("C",$five) == 0){ print "jpeg\n"; }else{ print "not jpeg\n" } close(IMG); #you can do pretty much the same thing to png file. just go to google, + search #for png specification, find out what the maigc id should look like an +d just #code it according to the specfication.
|
|---|