# Usage: STRING = ReadFile(FILENAME, START, LENGTH) # Reads an entire file or part of a file in binary mode. # Returns the file contents as a string. # An optional second argument will move the file pointer before reading, # and an optional third argument limits the number of bytes to read. sub ReadFile { my $F = defined $_[0] ? $_[0] : ''; $F =~ tr#><*%$?\r\n\"\0|##d; -e $F || return ''; -f $F || return ''; my $S = -s $F; $S || return ''; my $L = defined $_2 ? $_2 : $S; $L > 0 || return ''; local *H; sysopen(H, $F, 0) || return ''; binmode H; my $P = defined $_1 ? $_1 : 0; $P >= 0 or $P = 0; $P < $S || return ''; $P < 1 || sysseek(H, $P, 0); my $D = ''; sysread(H, $D, $L); close H; return $D; }