getbookmark.pl
#!/usr/bin/perl
use strict;
# Syntax: perl getbookmark.pl onetimepad_file
# This displays the value of the book marked from the given one-time pad.
# Execution starts here
&main;
sub main()
{
my $intNumArgs = $#ARGV + 1;
if ($intNumArgs != 1)
{
print "Syntax: perl getbookmark.pl onetimepad_file\n";
exit 1;
}
# Read the arguments on the command line
my $strPadFilename = $ARGV[0];
my $lngBookmark = ReadBookmark($strPadFilename);
if ($lngBookmark == 0)
{
exit 1;
}
print "Bookmark is ".$lngBookmark."\n";
exit 0;
}
sub ReadBookmark($)
{
my $strPadFilename = $_[0];
if (!open PAD_FILE, "<$strPadFilename")
{
print "ERROR:Can't open file $strPadFilename\n";
return 0;
}
# Handle the file as bytes instead of text
binmode PAD_FILE;
my $lngBookmark = unpack('L', <PAD_FILE>);
close PAD_FILE;
return $lngBookmark;
}
