Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

JA(C|P)H

by ktross (Deacon)
on Mar 22, 2005 at 15:50 UTC ( [id://441514]=obfuscated: print w/replies, xml ) Need Help??

Here is my first attempt at a multiple-programming-language obfu. It runs in perl and C++. Not very pretty though.... Can anyone suggest methods for merging two codesets?
//* print '';@_=<DATA>;$_[5]=~s /int //g;$_[5]=~ s/J/\$J/g; //* print '';$_[5]=~ s/A/\$A/g;$_[5]=~ s/P/\$P/g;$_[5]=~ s/H/\$H/g; //* print '';$_[6]=~ s/char /\@/;$_[6]=~ s/\[\]//;$_[7]=~ s/ i/\$i/g; //* print '';$_[6]=~ s/\{/\(/;$_[6]=~ s/\}/\)/;$_[7]=~ s/int//; //* print '';$_[7]=~ s/switch\(\$i\)\{/CASE\:/;for($i=8;$i<13;$i++){ //* print '';$_[$i]=~ s/cout<</print \$/;$_[$i]=~ s/<<j/\,\$j/g; //* print '';$_[$i]=~ s/ / \$i eq /;$_[$i]=~ s/case/\(/; //* print '';$_[$i]=~ s/\:/\)\&\&do\{/;$_[$i]=~ s/\;/\}\;/; //* print '';$_[$i]=~ s/J/\$J/g;$_[$i]=~ s/P/\$P/g; //* print '';$_[$i]=~ s/A/\$A/g;$_[$i]=~ s/H/\$H/g;} //* print '';$_[12]=~ s/\}//;$_ [4]= '#'.$_ [4];my $japh; //* print '';$_[10]=~ s/\$japh\[\$H\+\$H\+\$H\+\$J\]/ \$japh\[5\]\,\$j +aph\[12\]\,\$japh\[8\],\$japh\[13\] /; //* print '';foreach(@_){$japh = $japh . $_}eval $japh; // __END__ #include <stdio.h> #include <iostream.h> void main() {int J=0;int A=0;int P=0;int H=0;J++;A+=2;P-=1;H+=5; char japh[] = {'J','s','a','u','t','P','h','c','r',' ','n','o','e','l' +,'k',',','C'}; for(int i=0; i<5; i++){switch( i){ case 0:cout<<japh[J+P]<<japh[A+J]<<japh[J]<<japh[A+A]<<japh[9];break; case 1:cout<<japh[A]<<japh[H+H]<<japh[11]<<japh[H+P]<<japh[H+J];break; case 2:cout<<japh[H+H+A]<<japh[8]<<japh[H+A+A]<<japh[H+H+H+J];break; case 3:cout<<japh[H+A+A]<<japh[H+J]<<japh[J+A+P]<<japh[H+A];break; case 4:cout<<japh[H+H+H+P]<<japh[H+H+A]<<japh[H+H+P+P]<<japh[H+H+H];}} }

Replies are listed 'Best First'.
Re: JA(C|P)H
by jdalbec (Deacon) on Mar 23, 2005 at 03:00 UTC
    This uses a few tricks to convert C into valid Perl. You can also do some tricks in the opposite direction using #define. Unfortunately you're limited by the fact that $ and (I think) @ are not valid characters in C code. However, gcc allows them so if you don't mind restricting the C code to gcc compilation you have more freedom.
    #include <stdio.h> main() #ifdef __PERL__ ;sub main {} my $i; sub i:lvalue {$i} #endif { int i; for(i = 0; (i) < 10; (i)++){ printf("%d\n",i); } }

      (++)

      Thanks, this is exactly the sort of thing I was looking for. I really like the fact that you defined main as a sub. That hadn't occured to me. Using #define had crossed my mind, but I decided not to use it this time.

      What really intreges me about this example is the line: 'sub i:lvalue {$i}'. To be honest, this is the first time I've come across an lvalue sub. I know what it is doing, but not exactly how it is doing it. Documentation here I come!

      Thanks again, you've raised some interesting possibilities.

Re: JA(C|P)H
by eyepopslikeamosquito (Archbishop) on Mar 26, 2005 at 12:35 UTC

    When researching The Lighter Side of Perl Culture (Part III): Obfu, I found some golden oldies: P(ython|erl) (suck|rule)s!, C is Perl, Perl is C. Oh, and I made a feeble attempt in TPR(0,0):

    #include <stdio.h> #include <stdlib.h> #define strtol(a,b) strtol(a,0,b) #define $ /* use POSIX;$argv[1]=pop;"*/ main(int argc,char *argv[]) //"; { printf("%ld\n",strtol($ argv[1],36)); }

    Lyon's Jerome Quelin showcased an octo-lingual (perl, c, c++, befunge, brainf**k, python, ook, html/javascript) program that computed the Fibonacci series in his YAPC::Europe 2003 talk. Sorry, I don't have a link (and I tried to find one), so if anyone knows of a link to Jerome's amazing feat, please let me know.

      I haven't been able to locate Jerome Quelin's octo-lingual code but I did find a quatro-lingual (perl, c, brain@#!*, Befunge) tutoral by him (offsite)

      I also found another node by AlexL that prints hello world in four languages (perl, C, Tcl, and REBOL) and an octo-lingual code here(offsite) by Kevin Bungard, Peter Lisle, and Chris Tham.

Re: JA(C|P)H
by BioGeek (Hermit) on Apr 29, 2005 at 15:15 UTC
    Hmm, I seem to be runnin into the following error message:
    JACPH_441514.c:17:22: iostream.h: No such file or directory JACPH_441514.c: In function `main': JACPH_441514.c:22: `for' loop initial declaration used outside C99 mod +e JACPH_441514.c:23: `cout' undeclared (first use in this function) JACPH_441514.c:23: (Each undeclared identifier is reported only once JACPH_441514.c:23: for each function it appears in.) JACPH_441514.c:20: warning: return type of `main' is not `int'


    (I don't know C, so be gentle of if this is a trivial error message)

      You need to have iostream.h in your c library files for this one. That should clear up all of the errors except possibly

      JACPH_441514.c:22: `for' loop initial declaration used outside C99 mode

      Don't know what that ones about. Not a c expert myself. Runs under Visual c++ 6.0

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://441514]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found