Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Syntactic Confectionery Delight
 
PerlMonks  

New Huffman Obfus

by demerphq (Chancellor)
on Sep 16, 2001 at 05:11 UTC ( #112673=note: print w/ replies, xml ) Need Help??


in reply to Huffman encoder

Inspired by BooK's quite excellent obfuscated Huffman Encoder (434 chars on a w32 box), and many hours of my time playing with the algorithm I decided I would make an attempt at my own, and try to shave a characters off of the solution. After quite a bit of effort (this obfus stuff is hard.. ++ to those of you who make it look so easy) getting bitten by binmode, discovering which global variables could be used in exotic ways I wish to present my solutions: (All of the solutions expect input and ouput over STDIN and STDOUT)

My first solution is the equivelent to BooK's original encoder, ie it only encodes the message, without a header containing the information necessary to decode the message. It is 316 characters long (on a w32 box).

map{binmode$_}\*STDIN,\*STDOUT;sub r{my($n,$s,$c)=@_;$c=$$n[0];return$ +1{$c}=$s if!ref($c);map r($$c[$_],$s.$_),0,1;}sub h{$/=\1;$0{$_}++,$,.=$_ while +<>;@_=map [$_,$0{$_}],keys%0;while(@_>1){@_=sort{$$b[1]<=>$$a[1]}@_;$l=pop;$r=po +p;push@_ ,[[$l,$r],$$l[1]+$$r[1]];}r pop;$,=~s/./$1{$&}/gs;pack"SB*",$.,$,;}pri +nt h;
My next solution is the same thing but this time with the header information. I call it obfu_enhuff.pl, it is 337 bytes long.
$"=$_;map{binmode$_}\*STDIN,\*STDOUT;sub r{my($n,$s,$t,$c)=@_;$c=$$n[0 +];return $,++,$_{$c}=$s,push@0,pack"aCB16",$c,$t,$s if!ref($c);map r($$c[$_],$s +.$_,$t+1 ),0,1}sub h{$/=\1;$0{$_}++,$;.=$_ while<>;@_=map[$_,$0{$_}],keys%0;whi +le(@_>1) {@_=sort{$$b[1]<=>$$a[1]}@_;$l=pop;$r=pop;push@_,[[$l,$r],$$l[1]+$$r[1 +]]}r pop ;$;=~s/./$_{$&}/gs;chr($,)."@0".pack("SB*",$.,$;)}print h
The next is the corresponding decoder, as might be guessed I call it obfu_dehuff.pl, and it is 259 bytes long. The output of the encoder can be piped into this script and the result will be the original file. such as by
perl obfu_enhuff.pl <obfu_enhuff.pl | perl obfu_dehuff.pl
(usage on *nix machines might be little more graceful..)
$/=$_;map{binmode$_}\*STDIN,\*STDOUT;while($"=<>){map{$/=substr$",0,4, +'';($,, $;)=unpack"aC",$/;$_{(unpack"aCB$;",$/)[2]}=$,}1..ord substr$",0,1,''; +($,,$") =unpack"SB*",$";map{$;=1;$;++ until exists$_{substr$",0,$;};$\.=$_{sub +str$",0 ,$;,""}}1..$,;print""}
And for my last trick, I humbly present the combination of the two. This version I call obfu_huff.pl and is 622 bytes long. Like the others it uses STDIN and STDOUT but also takes an argument to determine if it decodes or encodes (no arguments at all).
perl obfu_huff.pl <obfu_huff.pl | perl obfu_huff.pl -d
$"=$/=$_;map{binmode$_}\*STDIN,\*STDOUT;sub r{my($n,$s,$t,$c)=@_;$c=$$ +n[0]; return$,++,$_{$c}=$s,push@0,pack"aCB16",$c,$t,$s if!ref($c);map r($$c[ +$_], $s.$_,$t+1),0,1}sub h{$/=\1;$0{$_}++,$;.=$_ while<>;@_=map[$_,$0{$_}], +keys %0;while(@_>1){@_=sort{$$b[1]<=>$$a[1]}@_;$l=pop;$r=pop;push@_,[[$l,$r +], $$l[1]+$$r[1]]}r pop;$;=~s/./$_{$&}/gs;chr($,)."@0".pack("SB*",$.,$;)} +if( pop@ARGV){while($"=<>){map{$/=substr$",0,4,'';($,,$;)=unpack"aC",$/;$_ +{( unpack"aCB$;",$/)[2]}=$,}1..ord substr$",0,1,'';($,,$")=unpack"SB*",$" +;map{ $;=1;$;++ until exists$_{substr$",0,$;};$\.=$_{substr$",0,$;,""}}1..$, +;print ""}}else{print h}
Many thanks to the Obfu crew and especially BooK for the inspiration and the walk through of his implementation, especially in regard to the use of funky global variables.

BTW, the code is set up for 80 columns, which means if you dont want it split you need to see your wrap code length to be 81 (go figure) in the User Settings to see all of them properly.

Oh and apologies to any who think this is a waste of time as its already been done, for me it was a _great_ exercise (I think that there are imprtant lessons to be learned by doing an obfu), and I'm damn proud of it. (never even looked at BooK's Huffman Decoder!)

:-)

Yves
--
You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)
UPDATE:Minor text changes


Comment on New Huffman Obfus
Select or Download Code
Re: New Huffman Obfus
by Anonymous Monk on Mar 21, 2012 at 09:06 UTC
    I want to implement the perl version for huffman encoder from http://blog.ektel.com.np/2012/03/vlsi-architecture-and-design-for-mpeg-encoder-decoder, thanks a lot and i hope it works with problem

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://112673]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2014-04-23 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (553 votes), past polls