Skip to content

Instantly share code, notes, and snippets.

@19h
Created February 24, 2012 22:10
Show Gist options
  • Save 19h/1904115 to your computer and use it in GitHub Desktop.
Save 19h/1904115 to your computer and use it in GitHub Desktop.
Recover .tar(.gz) Headers
#!/usr/bin/perl -w
use strict;
my $tarfile;
my $c;
my $hit;
my $header;
my@src = (ord('u'), ord('s'), ord('t'), ord('a'), ord('r'), ord(" "), ord(" "), 0);#my@src = (ord('u'), ord('s'), ord('t'), ord('a'), ord('r'), 0, ord('0'), ord('0'));
die "No tar given"
if $#ARGV != 0;
$tarfile = $ARGV[0];
open(IN, $tarfile) or die "Unable to open `$tarfile': $!";
$hit = 0;
$| = 1;
seek(IN, 257, 0) or die "Failure: 257 characters seek `$tarfile': $!";
while (read(IN, $c, 1) == 1) {
($hit = 0, next) unless(ord($c) == $src[$hit]);
$hit = $hit + 1;
(print "hit: $hit", next) unless $hit > $#src;#we have a probable header at(pos - 265)!my $pos = tell(IN) - 265;
seek(IN, $pos, 0)
or(warn "Seek?? $pos in `$tarfile': $!", next);
(read(IN, $header, 512) == 512)
or(warn "Could not read 512 byte header at position $pos in `$tarfile': $!", seek(IN, $pos + 265, 0), next);
my($name, $mode, $uid, $gid, $size, $mtime, $chksum, $typeflag, $linkname, $magic, $version, $uname, $gname, $devmajor, $devminor, $prefix) = unpack("Z100a8a8a8Z12a12a8a1a100a6a2a32a32a8a8Z155", $header);
$size = int $size;
printf("%s:%s:%s:%s\n", $tarfile, ($pos + 1), $name, $size);
$hit = 0;
}
close(IN) or warn "Error closing `$tarfile': $!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment