Advertisement
sebbu

RTFD extractor

Feb 25th, 2012
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.76 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5. use integer;
  6. use assertions;
  7.  
  8. my $count=0;
  9.  
  10. my $num_args = $#ARGV;
  11. ++$num_args;
  12. print 'args=',$num_args,"\n";
  13. if ($num_args != 1) {
  14.     print "\nUsage: ",$0," filename\n";
  15.     exit;
  16. }
  17. #print $ARGV[1];
  18. print "Thanks, you gave me $num_args command-line arguments:\n";
  19. my $argnum;
  20. foreach $argnum (0 .. $#ARGV) {
  21.     print "\t$ARGV[$argnum]\n";
  22. }
  23. #exit;
  24.  
  25. open(FILE,$ARGV[0]) || die "Sorry, I couldn't open $ARGV[0]\n";
  26. my $line;
  27. my $size;
  28. #while ()
  29. {
  30.     $size=read(FILE, $line, 4) or die "Sorry, I couldn't read $ARGV[0]\n";
  31.     die "bad header 1\n" if ($size!=4);
  32.     $size=read(FILE, $line, 4);
  33.     $line=unpack('V',$line);#little-endian
  34.     die "bad header 2\n" if ($line!=0);
  35.     $size=read(FILE, $line, 4);
  36.     my $blks=unpack('V',$line);#little-endian
  37.     print "number of 'blocks' : $blks\n";
  38.     $size=read(FILE, $line, 4);
  39.     my $elmts=unpack('V',$line);#little-endian
  40.     print "number of element in  1st 'block' : $elmts\n";
  41.     my $filename;
  42.     my @filenames;
  43.     print "\n";
  44.     #for(my $i = 0; $i < $elmts; $i++) {
  45.     foreach my $i (1 .. $elmts) {
  46.         $size=read(FILE, $line, 4);
  47.         $line=unpack('V',$line);#little-endian
  48.         #print "size=$line\n";
  49.         $size=read(FILE, $filename, $line);#size = previous read
  50.         #print "filename=$filename\n";
  51.         push(@filenames, $filename);
  52.     }
  53.     #print "\n";
  54.     for(my $i = 0; $i < $elmts; $i++) {
  55.         print $filenames[$i],"\n";
  56.     }
  57.     my $fsize;
  58.     my @fsizes;
  59.     for(my $i = 0; $i < $elmts; $i++) {
  60.         $size=read(FILE, $line, 4);
  61.         $line=unpack('V',$line);#little-endian
  62.         push(@fsizes, $line);
  63.     }
  64.     print "\n";
  65.     for(my $i = 0; $i < $elmts; $i++) {
  66.         print $fsizes[$i],"\n";
  67.     }
  68.     print "\n";
  69.     my $data;
  70.     my $curpos;
  71.     my $line2;#skip
  72.     foreach my $i (0 .. $elmts-1) {
  73.         #$curpos = tell(FILE);
  74.         #print "curpos = $curpos\n";
  75.         if( $filenames[$i] ne '.' && $filenames[$i] ne '..' ) {
  76.             open(FILE2, ">", $filenames[$i]) || die "Sorry, I couldn't open $filenames[$i] for writing\n";
  77.         }
  78.         else {
  79.             open(FILE2, ">", $filenames[$i].'txt') || die "Sorry, I couldn't open $filenames[$i].txt for writing\n";
  80.         }
  81.         $size=read(FILE, $line, 4);
  82.         $line=unpack('V',$line);#little-endian
  83.         die "bad data header 1\n" if ($line!=1);
  84.         $size=read(FILE, $line, 4);
  85.         $line=unpack('l',$line);#little-endian
  86.         if( $line < 0 || $line > 2147483648) {
  87.             #comportement spécial
  88.             $size=read(FILE, $line, 4);
  89.             $line=unpack('l',$line);#little-endian
  90.             #print "data size = ",$line,"\n";
  91.             $size=read(FILE, $line2, 4);
  92.             $line2=unpack('l',$line2);#little-endian
  93.             seek(FILE, $line2, 1);#SEEK_CUR
  94.         }
  95.         #else {
  96.             #print "data size = ",$line,"\n";
  97.             $size=read(FILE, $data, $line);#size = previous read
  98.             print FILE2 $data;
  99.         #}
  100.         close(FILE2);
  101.     }
  102.     $curpos = tell(FILE);
  103.     print "curpos 2 = $curpos\n";
  104.     exit;
  105. }
  106. close(FILE);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement