Created
October 27, 2010 02:42
-
-
Save skreuzer/648319 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/web/usr/perl/bin/perl | |
use Getopt::Long; | |
GetOptions("i|image=s" => \$image, "q|quiet" => \$quiet); | |
if(!$image) { | |
print STDERR "Usage: $0 -i /path/to/image.jpg"; | |
exit 2; | |
} | |
open(JPEG, "$image") or die "Unable to open $image: $!\n"; | |
binmode(JPEG); | |
read(JPEG, $s, 2); | |
if($s eq "\xFF\xD8") { | |
while(1) { | |
my($ff, $mark) = unpack("CC", jpeg_groper(2)); | |
last if($ff != 0xFF); | |
next if(($mark == 0xDA) || ($mark == 0xD9)); | |
my $len = unpack("n", jpeg_groper(2)); | |
last if($len < 0); | |
$len -= 2; | |
if($len < 0) { | |
qprint("CORRUPT JPEG: Bad Field Length"); | |
exit 1; | |
} | |
jpeg_groper($len) if($len); | |
} | |
} else { | |
qprint("CORRUPT JPEG: Invalid Magic Number"); | |
exit 1; | |
} | |
close(JPEG); | |
qprint("JPEG OK"); | |
exit 0; | |
sub jpeg_groper { | |
my $b; | |
my $n = read(JPEG, $b, @_[0]); | |
if(!defined $n) { | |
qprint("CORRUPT JPEG: Read Failed"); | |
return -1; | |
} elsif ($n != @_[0]) { | |
qprint("CORRUPT JPEG: Short Read (@_[0] of $n)"); | |
return -1; | |
} | |
return $b; | |
} | |
sub qprint { | |
print @_[0] . "\n" if(!$quiet); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment