#!/usr/bin/perl
#
# Author: PinkFreud / Mirkwood Networks <pf-bin *at* mirkwood.net>
#
# License: GPL
#
$version = 1.0.2;
#
# Usage:
# offset <file> <starting offset> [ending offset] > foo.bar
#   dumps everything starting from 'starting offset' (and ending at 'ending
#   offset', if supplied) to file foo.bar
#
# To use with the linux kernel, do the following:
# do hexdump <kernel image> | less.  Search for 8b ?1f, followed by xx08.
# This is the offset of the compressed kernel image.  Feed the image name
# and the offset to this script, and pipe to zcat.

($file, $soffset, $eoffset) = @ARGV;
exit 1 unless $soffset;
$soffset = eval $soffset;

open (FILE, $file) or die "Cannot read $file: $!\n";
seek (FILE, $soffset, 0) or die "seek failed: $!\n";

if ($eoffset) {
  $eoffset = eval $eoffset;
  $bs = tell(FILE) + 1024 > $eoffset ? $eoffset - tell(FILE) : 1024;
  while (read (FILE, $data, $bs)) {
    print $data;
    $bs = tell(FILE) + 1024 > $eoffset ? $eoffset - tell(FILE) : 1024;
    last if tell(FILE) >= $eoffset;
  }
} else {
  print $data while (read (FILE, $data, 1024));
}

close (FILE);
