#!/usr/bin/perl -w my $rcsid = '$Id: b64,v 1.4 2009/08/06 21:24:15 vogelke Exp $'; =head1 NAME b64 - encode/decode base-64 file. =head1 SYNOPSIS b64 [-dhmv] file [file ...] =head1 OPTIONS =over 4 =item B<-d> Input file is base-64 encoded, write an unencoded file with a generated filename. =item B<-h> Print a brief help message and exit. =item B<-m> Print the manual page and exit. =item B<-v> Print the version and exit. =back =head1 DESCRIPTION B will read the given input file(s) and either write them as base-64 or decode them from base-64. Default is to encode. =head1 AUTHOR Karl Vogel Sumaria Systems, Inc. =cut use strict; use subs qw/version/; use MIME::Base64 qw/decode_base64 encode_base64/; use IO::File; use Getopt::Long; use Pod::Usage; use File::Basename; use Carp; # # Options. # my $myname = basename($0); $myname =~ s/\.\w*$//; # strip any extension my %options; my @getopt_args = ( 'd', # decode? 'h|?', # print usage 'm', # print manpage 'v', # print version ); Getopt::Long::config("noignorecase", "bundling"); usage() unless GetOptions(\%options, @getopt_args); manpage() if $options{'m'}; version() if $options{'v'}; usage() if $options{'h'} || !@ARGV; # # Either encode or decode each file on the command line. # my ($fname, $oname, $ifh, $ofh); my $k = 0; foreach $fname (@ARGV) { $ifh = new IO::File; if ($options{d}) { ## Decode $ofh = new IO::File; $k++; $oname = sprintf ("%4.4d.out", $k); if ($ifh->open("< $fname")) { $_ = <$ifh>; s/begin //; $_ = decode_base64($_); print STDERR "decoding $fname\n"; if ($ofh->open("> $oname")) { while (<$ifh>) { print $ofh decode_base64($_); } $ofh->close; } else { croak "can't write $_\n"; } $ifh->close; } else { carp "$fname: cannot read: $!\n"; } } else { ## Encode my $buf; if ($ifh->open("< $fname")) { print "begin ", encode_base64($fname); binmode($ifh); while (read($ifh, $buf, 60 * 57)) { print encode_base64($buf); } $ifh->close; } else { carp "$fname: cannot read: $!\n"; } } } exit(0); #--------------------------------------------------------------------- # Print a usage message from the comment header and exit. sub usage { my ($emsg) = @_; require Pod::Usage; import Pod::Usage qw(pod2usage); carp "$emsg\n" if defined $emsg; pod2usage(-verbose => 1); } sub manpage { require Pod::Usage; import Pod::Usage qw(pod2usage); pod2usage(-exitstatus => 0, -verbose => 2); } #--------------------------------------------------------------------- # Print the current version and exit. sub version { $_ = $rcsid; s/,v / /; @_ = split; print "$myname v$_[2] $_[3] $_[4]\n"; exit(0); }