#!/usr/bin/perl -w
use strict;
use Digest::MD5;

die "usage: $0 <file> <size> [<start> [<end>]]\n" if(@ARGV<2 or 4<@ARGV or (1<@ARGV and not $ARGV[1] =~ /^[1-9]\d*$/s) or (2<@ARGV and not $ARGV[2] =~ /^\d+$/s) or (3<@ARGV and not $ARGV[3] =~ /^[1-9]\d*$/s));
my ($file,$size,$start,$end)=@ARGV;

open(FILE,"<",$file) or die "open($file): $!";
$start=0 unless($start);
$end=(stat(FILE))[7] unless($end);

seek(FILE,$start,0) or die "seek: $!" if($start);

while($start<$end) {
	my $ctx=Digest::MD5->new();
	my $numtogo=$size;
	$numtogo=$end-$start if($end-$start<$numtogo);
	my $buf;
	print $start,"-";
	while(0<$numtogo) {
		read(FILE,$buf,8192<$numtogo?8192:$numtogo) or die "read: $!";
		$start+=length($buf);
		$numtogo-=length($buf);
		$ctx->add($buf);
	}
	print $start,": ",$ctx->hexdigest(),"\n";
}

close(FILE) or die "close: $!";
0;
