Show me the money
分类: C/C++
2011-09-08 15:27:31
1. 原始代码
main(t,_,a ) char * a; { return!
0 t<3?
main(-79,-13,a+ main(-87,1-_, main(-86, 0, a+1 )
+a)):
1, t<_? main( t+1, _, a ) :3,
main ( -94, -27+t, a ) &&t == 2 ?_ <13 ?
main ( 2, _+1, "%s %d %d\n" )
:9:16: t<0? t<-72? main( _, t, "@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/") : t<-50? _==*a ? putchar(31[a]):
main(-65,_,a+1) : main((*a == '/') + t, _, a + 1 ) :
0
main ( 2, 2 , "%s") :*a=='/'||
main(0,
main(-61,*a, "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry")
,a+1);} |
这段及其混乱的代码的运行结果居然是一篇文章。它怎么做到的呢?让我们开始一步步进行分析吧。
2. 做一些名字替换,_换成argp,a换成str;然后分析 ? : 的层次结构。请注意,? : 运算符是从右向左结合的,所以分析的时候要先找到最右边的一个 ? :,加上();然后再找到右边倒数第二个? :,再加上括号…依次类推
不过本人眼神不太好,分析不了几层就头晕眼花,所以特意写了个脚本来进行分析。
{ cat <<'__EOF__' !0 < t ? t < 3 ? main(-79,-13, str + main(-87, 1- argp, main(-86, 0, str +1) + str )) : 1, t < argp ? main( t+1, argp , str) : 3, main ( -94, -27+t, str) && t == 2 ? argp <13 ? main ( 2, argp +1, "%s %d %d\n" ) : 9 : 16 : t < 0 ? t < -72 ? main(argp , t, coding1) : t < -50 ? argp ==* str ? putchar(31[ str ]) : main(-65, argp , str +1) : main((* str == '/') + t, argp , str + 1 ) : 0 < t ? main (2, 2 , "%s") : *str == '/' || main(0, main(-61, *str , coding2), str+1) __EOF__ } | awk -- ' $1!=""{ for(i=1; i<=NF; i++) { if($i == "?" || $i == ":") { stack[++stack_top] = token stack[++stack_top] = $i token = "" } else token = token " " $i } } END { if(token) stack[++stack_top] = token level = 0 while(stack_top >= 5 && level < 16) { level ++; for(i=stack_top; i>=1; i--) { if(stack[i] == "?") { if(1) token = sprintf("( %s ?/*L%d*/ %s :/*L%d*/ %s )\n", stack[i-1], level, stack[i+1], level, stack[i+3]) seed = i break } } stack[seed - 1] = token for(i = seed ; i <= stack_top - 4; i++) { stack[i] = stack[i + 4] } stack_top -= 4 } for(i=1; i<=stack_top; i++) print stack[i] }' |
得到
( !0 < t ?/*L10*/ ( t < 3 ?/*L9*/ main(-79,-13, str + main(-87, 1- argp, main(-86, 0, str +1) + str )) :/*L9*/ ( 1, t < argp ?/*L8*/ main( t+1, argp , str) :/*L8*/ ( 3, main ( -94, -27+t, str) && t == 2 ?/*L7*/ ( argp <13 ?/*L6*/ main ( 2, argp +1, "%s %d %d\n" ) :/*L6*/ 9 ) :/*L7*/ 16 ) ) ) :/*L10*/ ( t < 0 ?/*L5*/ ( t < -72 ?/*L4*/ main(argp , t, coding1) :/*L4*/ ( t < -50 ?/*L3*/ ( argp ==* str ?/*L2*/ putchar(31[ str ]) :/*L2*/ main(-65, argp , str +1) ) :/*L3*/ main((* str == '/') + t, argp , str + 1 ) ) ) :/*L5*/ ( 0 < t ?/*L1*/ main (2, 2 , "%s") :/*L1*/ *str == '/' || main(0, main(-61, *str , coding2), str+1) ) ) ) |
3. 很遗憾,这个分析结果是错误的,因为存在逗号表达式,而逗号的优先级是最低的,所以我们要对分析结果做一下修正
( !0 < t ?/*L10*/ ( t < 3 ?/*L9*/ main(-79,-13, str + main(-87, 1- argp, main(-86, 0, str +1) + str )) :/*L9*/ 1, ( t < argp ?/*L8*/ main( t+1, argp , str) :/*L8*/ 3, ( main ( -94, -27+t, str) && t == 2 ?/*L7*/ ( argp <13 ?/*L6*/ main ( 2, argp +1, "%s %d %d\n" ) :/*L6*/ 9 ) :/*L7*/ 16 ) ) ) :/*L10*/ ( t < 0 ?/*L5*/ ( t < -72 ?/*L4*/ main(argp , t, coding1) :/*L4*/ ( t < -50 ?/*L3*/ ( argp ==* str ?/*L2*/ putchar(31[ str ]) :/*L2*/ main(-65, argp , str +1) ) :/*L3*/ main((* str == '/') + t, argp , str + 1 ) ) ) :/*L5*/ ( 0 < t ?/*L1*/ main (2, 2 , "%s") :/*L1*/ *str == '/' || main(0, main(-61, *str , coding2), str+1) ) ) ) |
4. 将? : 转成 if else,方便阅读代码
const char *coding1 = "@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/"; const char *coding2 = "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"; main(t, argp , str ) char *str ; { int result; if( !0 < t ) { if(t < 3) result = main(-79,-13, str + main(-87, 1- argp, main(-86, 0, str +1) + str )) ; else result = 1; if(t < argp ) main( t+1, argp , str) ; else 3; if( main ( -94, -27+t, str) && t == 2 ) if (argp <13 ) main ( 2, argp +1, "%s %d %d\n" ) ; else 9 ; else 16 ; return result; } else { if(t < 0) { if(t < -72) result = main(argp , t, coding1) ; else { if(t < -50) { if(argp ==* str ) result = putchar(31[ str ]); else result = main(-65, argp , str +1); } else result = main((* str == '/') + t, argp , str + 1 ) ; } } else result = ( 0 < t ?/*L1*/ main (2, 2 , "%s") :/*L1*/ *str == '/' || main(0, main(-61, *str , coding2), str+1) ) ; } return result; } |
5. 简化代码
const char *coding1 = "@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/"; const char *coding2 = "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"; int main(int t, int p, char *str) { int result, n; if( t > 1 ) { if(t < 3 ) { n = main(-86, 0, str +1); n = main(-87, 1-p, n + str ); result = main(-79, -13, str + n) ; } else result = 1; if(t < p ) main( t+1, p , str) ; if( main(-94, -27+t, str) && t == 2 ) if (p < 13) main (2, p +1, "%s %d %d\n"); return result; } else if(t < 0) { if(t < -72) result = main(p , t, coding1) ; else { if(t < -50) { if(p == *str ) result = putchar(31[ str ]); else result = main(-65, p , str +1); } else result = main((* str == '/') + t, p , str + 1 ) ; } } else if( t == 1 ) result = main (2, 2 , "%s") ; else { /* t == 0 */ if( *str == '/' ) { result = 1; } else { n = main(-61, *str , coding2); result = !! main(0, n, str+1); } } return result; } |