#!perl -w
use strict;
use IO::File;
if (@ARGV!=2) {
    die "Usage: perl xx1.pl osimage512.dat  cabdir\n"
}
my $ifn=shift;
my $ifh=IO::File->new($ifn, "r") or die "$ifn:$!\n";
binmode $ifh;

my $ofd=shift;
my $ofs=0;
$ifh->seek(0x8000, SEEK_SET);
my $n= readdword($ifh);

sub readdword {
    my $dw;
    $_[0]->read($dw,4*($_[1]||1));
    return unpack 'V*', $dw;
}

for (my $i=0 ; $i<$n ; $i++)
{
    my $ofs= $ifh->tell();
    my $name;
    $ifh->read($name, 256);
    $name =~ s/\x00+$//;
    my ($x, $l)= readdword($ifh, 2);
    printf("%08lx: %08lx %08lx %s\n", $ofs, $x, $l, $name);
    my $data;
    $ifh->read($data, $l);


    my $ofh= IO::File->new("$ofd/$name", "w") or die "$ofd/$name: $!\n";
    binmode $ofh;
    $ofh->print($data);
    $ofh->close();
}

$ifh->close;

