Thanks to Lev Selector, I have extended my original ob2.pl
into an "Accidental Smirch".

Originally, I was just trying to make it easier to generate
an obfuscated Perl program to print a simple text message
to annoy my friends with. Lev asked could it be extended
to convert a Perl program into an obfuscated one.
I said I didn't have a clue; then I realised that simply
replacing the final "print" with "eval" should do the trick.

ob4.pl
------
my $q;my $i=0;my $e=0;
if($ARGV[0] eq '-e'){$e=1;shift}
my %C=map{chr(),"'".chr()."'"}0..255;
$C{"\n"} = q#'\\\\'.('`'|'.')#;
$C{"\t"} = q#'\\\\'.((']'^'-')|'$')#;
$C{'$'}  = q#'\\\\'.'$'#;
$C{'@'}  = q#'\\\\'.'@'#;
$C{'\\'} = q#'\\\\'.'\\\\'#;
$C{'"'}  = q#'\\\\'.'"'#;
$C{"'"}  = q#'\\''#;
$C{'p'}  = q#(']'^'-')#;
# 'a'..'o'
$C{chr($_+64)}=q#('`'|#.($q=$_==39?'"':"'").chr()."$q)" for 33..47;
# 'q'..'z'
$C{chr($_+80)}=q#((']'^'-')|#.($q=$_==39?'"':"'").chr()."$q)"
   for 33..42;
# 'A'..'Z'.
$C{+uc}=qq#($C{$_}&~\' ')# for 'a'..'z';
# '0'..'9'
$C{$i++}=q#('^'^('`'|#.($q=$_==39?'"':"'").chr()."$q))"
   for (46,47,44,45,42,43,40,41,38,39);
print q#''=~('(?{'.#;
print $e ? qq#$C{e}.$C{v}.$C{a}.$C{l}.'"'#
         : qq#$C{p}.$C{r}.$C{i}.$C{n}.$C{t}.'"'#;
print map{'.'.$C{$_}}map/./gs,<>;
print q#.'"'.'})')#;

Example usage:
  perl ob4.pl fred.txt >fred.pl
where fred.txt contains, say:
  Just another Perl hacker
will generate a Perl program, fred.pl, containing
no alphanumeric characters (as before) while:
  perl ob4.pl -e myprog.pl >new.pl
will generate new.pl, an obfuscated version of
myprog.pl, with no alphanumeric characters.

For cheap thrills, I ran ob4.pl on itself a few times:
  perl ob4.pl -e ob4.pl >r1.pl
  perl r1.pl -e r1.pl >r2.pl
  perl r2.pl -e r2.pl >r3.pl
  perl r3.pl -e r3.pl >r4.pl
r4.pl is about 1 MB in size (I won't attach it to this
mail:-) and takes about a minute to run, but still
seems to work.

Andrew.

Reply via email to