#!/usr/bin/perl -w
#
# getfile - get length data from inputfile at offset into outputfile.
#
# usage: getfile infile offset length outfile
#
# where offset and/or length may be decimal or hexadecimal
# if hexadecimal, must start with '0x'
#
# N. DeBaggis
#

use strict;

my $buf;

if(@ARGV != 4){ die "$0 inputfile offset length outputfile\n" };
if(! -f $ARGV[0]){ die "$ARGV[0] not a file $!" };

my $offset = $ARGV[1];
my $length = $ARGV[2];

if($offset =~ m/^0x/){
   $offset = hex($offset);
}
if($length =~ m/^0x/){
   $length = hex($length);
}

print "reading data from $ARGV[0] offset $offset length $length into $ARGV[3]\n";

open(FH, "$ARGV[0]") or die "can't open $ARGV[0] : $!";
binmode(FH);
open(OUT, ">$ARGV[3]") or die "can't open $ARGV[3] : $!";
binmode(OUT);

seek(FH, $offset, 0) or die "can't seek to offset $offset in $ARGV[0] : $!";
read(FH, $buf, $length) or die "can't read $ARGV[0] : $!";
close(FH);
print OUT $buf;
close(OUT);




