This started as a test script while I was learning about bit operations, and it slowly evolved into an obfuscated JAPH. I know it exceeds the traditional "4 line rule", but I thought that it would be fun to limit the digits used to only 0 and 1, given the theme. It was not golfed in any way.
Enjoy!
$_=[[[[{},[]],{0111,'01110111',1000,'11100101'},[[]]],{0001,'01111110' ,0110,'00011010',0100,'011001000011111001111010',0010,'00000100',0011, '01010101000100000001111110100010101000000000101000110000010001001010' .'10100',0101,'011100001001010100000001'},[[]]]];%_=%{${${$_}[0]}[1]}; $s=eval{$O=0,$C=0,$t='This is my 100th PM post';sub{$O++,$C=@_?$C:eval pack('b*',vec(pack('b*',$_{1<<(1<<0<<1)*((1<<0+1<<0)+1<<0)}),$O-1,1<<0 )?$_{1<<0}:$_{1<<(1<<1<<1)-1}).$C.pack('c',vec(pack('b*',$_{1<<((1<<0) +(1<<1*1<<1)+1<<0)}),($O-1)%((1<<0*1<<1)+1<<0),1<<(1<<0+1<<0)+1)).pack ('b*',vec(pack('b*',$_{1<<((1<<0+1<<0)+(1<<1<<1))}),$O-1,1)?$_{1<<((1+ 1<<0)+1)}:$_{1<<0}).vec($t,$O-1,1<<(1<<1<<1)-(1<<0))};};;$o=pack('b*', $_{((1<<1)**(1<<1)-1)**(1<<1)});for(0..unpack('%b*',$o)-(1<<1)){$c=$_- (($_-$_%(1<<1<<1))/(1<<1<<0*1<<1)+($_%(1<<1+1<<0)?1:0));;$i=(vec($o,$c ,(1<<1<<0+1<<0))+(vec($o,$c+1,(1<<1<<1))<<(1<<1<<0+1))>>((1<<1<<1<<1>> 1)-$_%(1<<1<<1<<1>>1)-(1<<1<<0+1)*($_%(1<<1<<1>>1<<1)?0:1)));$i+=$i%(1 <<1)?-1:1 if((($_+1)%((1<<1+1<<1)+(1<<1)+(1<<1)))&&!(($_+1)%((1<<1)**( 1<<1)-1))||$_+1==(1<<(1<<1+1<<1>>1))+(((1<<1)+1)<<1));$T=vec(pack('C', $i),0,(1<<1<<1>>1<<1))+vec(pack('b*',$_{(1+((1<<1)**(1<<1))**((1<<1)** (1<<1)-1))}),$_,1)*(1<<(1<<(1<<1)))+(1<<((1<<1**1<<1)+1));;print pack( 'c',($T+=$T==(1<<((1<<1+1<<1>>1)+1))?0:(1<<(((1<<1+1<<1)>>1)+(1<<0*1<< 1))))-=$_%(1+(((1<<1+1<<1)>>1)+(1<<0*1<<1)<<1))==0?(1<<(((1<<1*1<<1<<1 )+(1<<1<<1>>1))>>1)):0);&$s}print pack('c',$s->(1<<0)^unpack('c',(pack ('b*',$_{(1<<1*1<<1*1<<1)*((1<<1)**(1<<0<<1)-1)**(1<<1<<1>>1<<0)}))));
Hint: the bit shifts are fun, but they aren't the main point of the obfuscation.
Tested on
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Bits & pieces
by jdalbec (Deacon) on Jul 16, 2005 at 03:18 UTC | |
B::Deparse gets rid of the bit shifts, but it breaks the code by omitting the parentheses after not below. After reinstating the parentheses and running the code through Perl::Tidy it's still not very readable. I've inserted some comments inside and outside the code to keep track of what's going on. Comments generally refer to the statements preceding them.
Most of the structure of $_ is obfuscation. Let's start over with %_ since $_ is never used again except for the localized $_ in the for loop. Some of these (little-endian) bitstrings are really bytestrings and I've commented them as such. I've also added column numbers below some of the strings. This subroutine is essentially pure obfuscation. It does some fairly random calculations using the characters of $t and a fudge factor is added at the end to produce the desired output. Now we come to the heart of it. It's rather convenient that a 16-bit checksum of $o comes out so close to the length. Heavy comments here. These two statements extract the low nybbles of the JAPH phrase from $o. The trick is that the nybbles are offset by only 3 bits so the 8's bit of each character is the 1's bit of the next character. Naturally, not all the 1's bits are correct, but surprisingly many are. Also, some of the higher-order bits in $i are set, but they get stripped off later. Here we fix up the erroneous 1's bits. Now we extract the low nybbles from $i and the 16's bits from $_{65}, and set the 32's bits unconditionally. Now we set the 64's bit unless the character is a space and reset the 32's bit for the two capital letters and print the result. As I mentioned above the calls to &$s are essentially pure obfuscation. In the last statement below a fudge factor $_{72} is xored with the final result to get a comma.
| [reply] [d/l] [select] |
by bobf (Monsignor) on Jul 18, 2005 at 07:58 UTC | |
Good job! Included below is an explanation of this JAPH which is a bit (ba-dum-bum) more verbose. Your analysis hit on the big points, but I wanted to fill in some of the more subtle reasoning. You correctly identified the main point of this JAPH, which is the overlapping, 3-bit offsets for the low nybbles. While the subroutine "is essentially pure obfuscation" (as are most obfus!), the closure was designed deliberately and the calculations are far from random. The "fudge factor" at the end was necessary, but the value of it is meaningful, given the operation that uses it.
The point
The Bitshifts
The Data Structure
The keys to the data hash were meant to look like the low nybble of binary 1 (0001), 2 (0010), ... 6 (0110). I hoped that someone walking through this manually (without a debugger) would forget that the leading 0 would force the keys to be interpreted as octals rather than as binaries. Therefore, the actual key values (and the result of the bitshifts in the code) are 1, 8, 9, 64, 65, and 72, which might be confusing if you thought the hash contained keys of only (1..6). As described below, the bitstring in $_{9} is composed of the low 4 bits (0..3, the low nybble) of the bytes in 'just another perl hacker'. The bitstring in $_{65} is composed of all of the 4th bits in the string. Note bit 5 is always set for these characters, bit 6 is set for every non-space character, and bit 7 is not set for any character.
The Closure
The index ($O) simply tracks the iteration number, and $commabyte is processed during each iteration and will eventually become the comma at the end of the JAPH. The text string is just something fun I stuck in to commemorate my 100th post here at PM, but the characters within that string are used in bit operations with $commabyte each time the closure is called. The value of $_{64} ('011001000011111001111010') is a bytestring representing three bitwise operations ('&|^'). $_{1} and $_{8} represent '~' and '' (the empty string), respectively. Therefore, the 5 parts to the eval simplify to:
Lines 2 and 5 are the two bytes used in the operation: the current status of $commabyte (retained between calls by the closure) and one character (at position $index-1) from $textstr. Line 3 determines which of the 3 bit operators is used during the current iteration (using %3 to find the proper offset in the string).
In lines 1 and 4, vec( '&|^', $index-1, 1 ) grabs one bit (at position $index-1) from the bytestring represented by '&|^'. That bit is used to determine whether the bitwise negation operator ('~') or an empty character is used in the string to be eval'd. Since lines 1 and 4 have the characters reversed in trinary conditional operator, the effect is to negate the first term The resulting string looks something like this:
where one of the two characters are negated
The Compressed Low Nybble Bitstring
The resulting bitstring is 0101010100010, but notice that the overlapping bits for 'u' and 's' do not match. As it turns out this inversion occurs for every third pair with the exception of the 12th and the 24th, and also for the 22nd pair. There is an adjustment in the loop to handle this. The compressed low nybble bitstring is assigned to $o just before the main loop.
The Main Loop
The low nybble for each character is extracted from the bitstring in $_{9}, but since the offsets are multiples of 3 and vec can only use offsets that are powers of 2, the process is a bit more complex. Specifically, the extraction must accomodate nybbles that cross frame boundaries. This is not possible with a single vec function (unless the frame width (BITS in vec) is set to a value at least as large as the length of the whole bitstring), so two 4-bit extractions are used. The offset of the first 4-bit frame ($c) is calculated and then vec is used to extract 4 bits at that and the following offset. The second nybble is left-shifted 4 to make it a high nybble, and the two are added to get a byte. Finally, since the desired nybble is located within that byte at an offset of 0..3 bits, the byte is shifted by that many bits to eliminate the extra bits. In the code below, the loop iterator ($_) is designated by $offset and the compressed bitstring ($_{9}) is represented by $bitstr.
At this point $i is a byte with the proper bits in the low nybble, but the low bit of the low nybble might need to be flipped (see the explanation of the compressed low nybble bitstring, above). This is tested and accomplished with the line:
Bit 4 (in the high nybble) is set by extracting the appropriate bit from the high bit string in $_{65}, and bit 5 is turned on for all characters. These are added to the low nybble (from $i) to give $T.
Bit 6 is set based on the value of $T. If the character should be a space, $T == 32 (only bit 5 is set) and no change is made, otherwise bit 6 is set. Finally, the 'j' and 'p' characters (the 0th and 13th characters in the JAPH) are capitalized by turning off bit 5, and the value is printed.
The last step in the main loop is to call the closure (see above).
The comma
Update: corrected explanation of the bitwise negation in the closure (thanks jdalbec). My original version of this obfu used the value of $O (the index) %2 to determine which term gets negated, and I forgot that I changed it to rely on the bitstring instead. I guess that's what I get for writing a post at 3am. :) | [reply] [d/l] [select] |
by jdalbec (Deacon) on Jul 19, 2005 at 03:01 UTC | |
the two characters are negated in alternate calls to the closure.For that to be true, $_{64} would have to be '010101010101010101010101'. But I see that the output of the closure is more predictable than I had thought at first. The final result depends only on the last three (to some extent, six) characters of the string (in fact it's the same as the last character of the string). I see also that I was bitten by contexts again. The correct list of values for &$s in scalar context is
| [reply] [d/l] [select] |