Hi, all:
Hello,
Below is a script to extract jpegs from an image of a corrupted or accidentally erased CF or other removable media from a digital camera. It's (heavily) modified from some C++ code I found that works pretty well. Basically, it looks for the jpeg magic number, an assumes that the start of the file. The original, though, lumped everything from the last magic number to the end of the image into one big file. I'm trying to add a check for embedded EOFs. I never seem to find any, though. I don't really work with file systems much, so I'm not sure quite where it's going wrong.
I can see two problems right away.
[code] #!/usr/bin/perl use warnings; use strict;
my $image = shift @ARGV; my $file = 0; my $dir = "flash_recovery"; my $name; my $sector; my $seclen = 512; my $fileeof = 0; my $eofsec = qr/(.*\xff\xf8\xff\xff)/; # and variations incl. xffff my $magic = qr/^\xff\xd8\xff\xe1/;
mkdir $dir or die "$!" unless -d $dir; open(IMG, "< :raw", $image) or die "Couldn't open $image for reading: $!\n"; seek(IMG, 0, 0);
while (!eof) {
You need to specify the filehandle you are reading from.
until ( eof IMG ) {
read(IMG, $sector, $seclen) or die "Couldn't read from $name: $!\n"; next if $file == 0 and $sector !~ $magic; if ($sector =~ $magic) { close(SAVE); print "Recoverd image to $name\n" if $file != 0; $name = sprintf("%s/flash_%3.3d.jpg", $dir, ++$file); open(SAVE, "> :raw", $name) or die "Couldn't open $name for writing: $!\n"; $fileeof = 0; } next if $fileeof == 1; if ( $sector =~ $eofsec ) { print SAVE $1 or die "Couldn't write to $name:$!\n"; $fileeof = 1; } else { print SAVE $sector or die "Couldn't write to $name:$!\n"; } } close(SAVE); print "Recoverd image to $name\n"; print "\nRecovered $file files to current directory\n\n"; [/code]
The second problem is that $eofsec contains multiple bytes and may (and probably does) overlap $sector boundaries. The easy solution would be to read the entire file into a variable.
John -- use Perl; program fulfillment
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>