#!/usr/bin/perl
$file="image2";
$fnum=0;

open(IN, $file);
binmode(IN);
sysread(IN,$buf,1);
$fnum = ord($buf);

#skip
sysread(IN,$buf,3);
for($i=0;$i<$fnum;$i++){
  sysread(IN,$buf,24);
  for($j=0;$j<15;$j++){
    if(ord(substr($buf,$j,1))==0){
      last;
    }
  }
  $fn=substr($buf,0,$j);
  printf "$fn";
  $nname[$i]=$fn;
  for($j=0;$j<15-length($fn);$j++){
    printf " ";
  }
  for($j=16;$j<24;$j++){
    printf (" %02x",ord(substr($buf,$j,1)));
  }
  $nsize[$i]=ord(substr($buf,22,1))*256*256+
             ord(substr($buf,21,1))*256+ord(substr($buf,20,1));
  $nstart[$i]=ord(substr($buf,18,1))*256*256+
              ord(substr($buf,17,1))*256+ord(substr($buf,16,1));
  printf ("  %6d %6d\n",$nstart[$i],$nsize[$i]);
}

for($i=0;$i<$fnum;$i++){
  if($i!=$fnum-1){
    sysread(IN,$buf,$nstart[$i+1]-$nstart[$i]);
  } else {
    sysread(IN,$buf,$nsize[$i]);
  }
  open(OUT, "> $nname[$i]");
  binmode(OUT);
  print OUT $buf;
  close(OUT);
}

close(IN);
