#!/usr/bin/perl -Tw # Copyright (c) 2005 Holt Sorenson # This code is made available under the Artistic License # (http://www.opensource.org/licenses/artistic-license.php) # This script, in it's current state, isn't recommended # on win32 hosts because it doesn't use binmode(). # This script requires CGI.pm >= 2.47 use strict; use MIME::Base64; use CGI qw/:standard -private_tempfiles/; # get the current date and stuff it in a string sub dstr() { my ($d, $i, $e) = (); $i = 5; foreach $e ( (localtime(time()))[5,4,3,2,1,0] ) { if ($i == 5) { $e += 1900; } elsif ($i == 4) { $e++; } if ($e < 10) { $e = "0" . $e; } $d .= $e; $i--; } return($d); } # acquire some random bits and return them as hex sub rnd() { my ($r1, $r2, $i) = (); open(RND, "0;$i--) { $r1 .= getc(RND) ^ getc(RND); } if (defined($r1) && length($r1) == 4) { foreach $i (split(/ */, $r1)) { $r2 .= sprintf("%02x", ord($i)); } return($r2); } else { return(-1); } return(-1); } # get lines from an array or a filehandle sub getline() { my ($b, $n) = ($_[0], $_[1]); # it's an array, return line $n unless $n exceeds array size if (ref($b) eq 'ARRAY') { if ($n >= scalar @{$b}) { return (); } else { return(${$b}[$n]); } } # it's a file handle, use readline elsif (ref($b) eq 'Fh') { return(readline($b)); } else { return (); } } # output base64 decode error page sub decode_error() { my ($mode, $ft) = ($_[0], $_[1]); if ($mode eq "html") { print "\nlbbdecode cgi - Error" . "\n" . "
" . "" . "Error decoding base64 encoded data.
 
${ft}
\n\n"; } elsif ($mode eq "text") { print "\n===> lbbdecode cgi error ${ft} -- Error decoding base64 encoded data.\n"; } else { print STDERR "\n===> lbbdecode cgi error ${ft}: Error mode ${mode} not supported at line __LINE__.\n"; } } # begin main { my ($b64blob, $decoded, $dstr, $rnd, $e, $q, $blob, $i, $l) = (); my $version = "0.2"; my $html_ft = "" . "low budget base64 decoder cgi (v${version}) - Holt Sorenson" . " <hso at nosneros dot net>
Copyright (c) 2005. " . "see: " . "http://www.nosneros.net/hso/code/
" . "lbbdecode cgi licensed under the " . "Artistic License.
"; my $txt_ft = "v${version}"; $dstr = &dstr(); $rnd = &rnd(); $q = new CGI; # a form isn't being submitted, so create one if (!$q->param('b64blob') && !$q->param('b64file')) { print $q->header, $q->start_html('lbbdecode cgi'), "\n", # file upload form $q->start_multipart_form, "\n" . "\n\n
", "" . "Browse to base64 encoded file:\n", "
", $q->filefield(-name=> 'b64file', -size=>45, -maxlength=>255), " ", $q->submit(-name=>'Submit'), $q->reset(-name=>'Reset'), "
\n", $q->end_form, # paste form $q->start_form, "\n" . "\n" . "\n\n
" . "Or
", "" . "Enter (paste) base64 encoded blob:", "", $q->submit(name=>'Submit'), $q->reset(name=>'Reset'), "
", $q->textarea(-name=>'b64blob',-rows=>32,-columns=>77), "
${html_ft}\n
\n", $q->end_form; # file upload failed if ($q->upload('uploaded_file') && $q->cgi_error()) { print $q->header(-status=>cgi_error); } } # try to decode and output elsif (param('b64file') || $q->param('b64blob')) { if ($rnd ne "-1" && length($rnd) == 8) { print "Content-type: application/octet-stream name=\"lbbdecode_output_${dstr}_$rnd.raw\"\n"; print "Content-disposition: attachment; filename=\"lbbdecode_output_${dstr}_$rnd.raw\"\n\n"; } else { print "Content-type: application/octet-stream name=\"lbbdecode_output_${dstr}.raw\"\n"; print "Content-disposition: attachment; filename=\"lbbdecode_output_${dstr}.raw\"\n\n"; } if (param('b64file')) { # make $blob the filehandle to the uploaded file $blob = $q->upload('b64file'); } elsif (param('b64blob')) { # make $blob an anonymous array reference to the base64 # that's split on linefeed and/or carriage return $blob = [split(/[\r\n]+/, $q->param('b64blob'))]; } else { &decode_error("text", $txt_ft); } $i = 0; while (defined($l = &getline($blob, $i))) { $decoded = MIME::Base64::decode_base64($l); # decode failed, print error if (!defined($decoded) || length($decoded) == 0) { &decode_error("text", $txt_ft); last; } else { print "$decoded"; } $decoded = (); $i++; } } } # end main