#!/usr/bin/perl
$file="PBR007_H117.bin";
$num=0;
$vshift1=0x312CE;
$vshift2=0x450;
$fnum=256;

open(IN, $file);
binmode(IN);
seek(IN,$vshift1,0);
seek(IN,$vshift2,1);
sysread(IN,$buf,1);
$fnum=ord($buf);
print "file count : $fnum\n";
seek(IN,3,1);

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

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

close(IN);
