Note: the source must be viewed with tabstop 8. To appreciate the beauty have your eyes half closed. Why I think this program is obfuscated: C is a free format language. It is therefore a question of style how variables, operators, declarations, functions and syntactic elements like braces and parentheses are grouped together. As a rule of thumb, I put spaces around operators, after opening braces and before closing braces. I have generalized this rule by putting whitespace after *any* non-white space character (with the exception of preprocessor directives of course, and the e and H macros due to the size limit of 3217). There are no recognizable keywords anymore. There are no recognizable string literals any more. The main function is invisible. All operators longer than one character have gone. They have been replaced by macro calls with quite a number of arguments. The keys are the token pasting and stringizing macros e and O. The "arcane rules of token pasting" [K&R2] are used up to three levels, H(O,r)R(O,r)! which is needed to make one 8-character token from 8 characters ('unsigned'). BTW, C has no 9 or more character reserved words, so a fourth level is only required if you intend to generate object names from 9 or more components. The final round even swaps the tokens, so all keywords must be spelled backwards (sort of, can you spot printf in the P macro?). This strongly calls for the worst abuse of the preprocessor award... Try to indent the source. The unbalanced parentheses confuse Solaris' indent to the max (although the result does compile): % indent prog.c Warning@26: Extra ) Warning@28: Extra ) Error@43: Unbalanced parens Warning@44: Extra ) Error@51: Unbalanced parens Warning@51: Extra ) Error@58: Unbalanced parens Warning@59: Extra ) Error@61: Unbalanced parens Warning@62: Extra ) Error@76: Unbalanced parens Warning@78: Extra ) Error@89: Unbalanced parens Warning@91: Extra ) Warning@93: Extra ) Warning@95: Extra ) Warning@96: Extra ) Looking at the indented source is even *less* fun, because the nice diamond pattern has been replaced by something that looks like, uhm, modem noise. The #inclusion of *after* the #defines is a small torture test for the system includes files. We hope there are no parameter names in prototypes. Lint the source. It's clean. How obfuscated... What's the exit status? The program exits with J - 1 where J is the return value from several non-void functions (needed to shut up lint). The last assignment is J = printf ("%c", 10); so the exit status is 0 unless the last printf failed, which is even a *useful* status. A further obfuscation is the use of 10 instead of '\n'. '\n' can not be constructed by token pasting, AFAIK. That's why prog is likely to produce occasional garbage on non-ASCII execution character set systems. What this program does: ----------------------- The program is an expression evaluator for a limited class of expressions. Known operators are + - * /, operands are real numbers (integers or fractions). The operands are restricted in that they must differ by a constant value, for example 1 + 2 * 3 / 4 - 5. For any combination of operators the expression is evaluated and compared to a goal value. If they compare equal the expression is printed. If the goal value is specified as 0/0 all expressions and results are printed. For any fraction, the numerator and denominator are divided by their greatest common divisor. The program accepts up to 4 parameters: 1) The number of operators in the expression [default: 9] 2) The value of the first operand [1] 3) The (possibly negative) increment to the next operand [1] 4) The goal value or 0/0 [42] On ANSI/ISO C conformant systems (where long is at least 32 bits) overflow will occur in the default case (first operand 1, increment 1) only for more than 11 operators. Because a bit mask is used that needs twice the number of operands in bits (plus a sentinel value) on those systems the first parameter must not exceed 15. If long is at least 64 bits overflow will occur for more than 19 operators but you will probably wait quite some time for every of the 4^19 = 274,877,906,944 combinations to be evaluated... While I'm at it, on a 50 MHz sparc, 4^11 expressions are evaluated in about 3 cpu minutes (gcc -O3). Sample arguments: ----------------- % prog 6 % prog -7 7 % prog 7 7 0 % prog 8 9 -1 -67/21 % prog 8 2 2/5 -98/25 % prog 2 1/0 1 1/0 % prog 15 1 0 0/0 Miscellaneous ------------- The diamond pattern can be continued without a break. For a nice printout try this: % sh -c 'while :; do grep -v \# prog.c; done' Here is the original program, with only slight obfuscations. I have appended a perl script that transforms this C source to the format my entry has. The main part is substituting long names with one character identifiers, replacing keywords by macro calls and then putting tabs in the right places. It's relatively easy to adapt it to transform other sources as well. #include /* $Id: hunni.c,v 1.5 1996/11/17 20:58:04 schweikh Exp schweikh $ */ /* usage: hunni [n_op [start [increment [goal]]]] */ /* Up to 20! = 2432902008176640000 */ /* < 9223372036854775807 = 2^63-1 */ /* n! fits in signed long long ints, */ /* with 19 operators there are 4^19 = 274,877,906,944 combinations, */ /* up to 12! = 479001600 */ /* < 2147483647 = 2^31-1 */ /* n! fits in signed long ints, */ /* with 11 operators there are 4^11 = 4,194,304 combinations. */ /*@-loopswitchbreak@*/ /*@-protoparamprefix p_@*/ /*@-pointerarith@*/ /*@+ignorequals@*/ #define FMT "%ld" /* printf and scanf format for this type */ #define LONGEST long /* longest type of this implementation */ typedef struct { LONGEST num, den; } fraction; typedef unsigned LONGEST bitvec; static fraction init[20], work[20], param[4] = { { 9, 1 } , { 1, 1 } , { 1, 1 } , { 6 * 7, 1 } }; static int J; static void out (fraction f) { if (f.den - 1) { J = printf (FMT "/" FMT, f.num, f.den); } else { J = printf (FMT, f.num); } } static void normalize (fraction *p_f) { LONGEST a = (*p_f).num, b = (*p_f).den; if (b) { while (a) { LONGEST i = b % a; b = a; a = i; } b = b < 0 ? - b : b ; (*p_f).num /= b; (*p_f).den /= b; } } int main (int argc, char *argv[]) { bitvec m, mask; int i, cursor; for (i = 1; i < 5; i = i + 1) { if (argc > i) { J = sscanf (argv[i], FMT "/" FMT, ¶m[i-1].num, ¶m[i-1].den); } normalize (param + i - 1); } init[0] = param[1]; for (i = 0; i < param[0].num; i = i + 1) { init[i+1].num = init[i].num * param[2].den + param[2].num * init[i].den; init[i+1].den = init[i].den * param[2].den; normalize (init + i + 1); } for (mask = ~(~0 << param[0].num*2); ~(bitvec)0 - mask; mask = mask - 1) { /* Pass one computes mult and div. */ /* For add and sub the right operand is copied. */ work[cursor = 0] = init[0]; for (m = mask, i = 1; ! (i > param[0].num); i = i + 1, m = m / 4) { if ((m & 3) < 2) { if (m & 1) { work[cursor].num *= init[i].den; if ((work[cursor].den *= init[i].num) < 0) { work[cursor].num *= -1; work[cursor].den *= -1; } } else { work[cursor].num *= init[i].num; work[cursor].den *= init[i].den; } } else { work[cursor = cursor + 1] = init[i]; } } /* Pass two computes the remaining adds and subs. */ for (m = mask, i = cursor = 0; i < param[0].num; i = i + 1, m = m / 4) { if ((m & 3) > 1) { work[0].num = work[0].num * work[cursor+1].den + ((m & 1) ? -1 : +1) * work[cursor+1].num * work[0].den; work[0].den = work[0].den * work[cursor = cursor + 1].den; } } normalize (work); /* output result */ if (!param[3].den || (!(param[3].num - work[0].num) && !(param[3].den - work[0].den))) { for (m = mask, i = 0; i < param[0].num; i = i + 1, m = m / 4) { out (init[i]); J = printf (" %c ", "*/+-"[m & 3]); } out (init[i]); J = printf (" = "); out (work[0]); J = printf ("%c", 10); /* newline */ } } return J - 1; } #!/appclient/bin/perl print "#define C \" \"\n"; print "#define O( _ ) # _\n"; print "#define R( n , d ) e ( n , d )\n"; print "#define e(p,o)o##p\n"; print "#define D O ( % ) O ( l ) O ( d )\n"; print "#define U R ( e ( g , n ) , e ( o , l ) )\n"; print "#define M H ( R ( e ( c , i ) , t ) , R ( e ( a , t ) , s ) )\n"; print "#define P H ( R ( e ( f , t ) , n ) , R ( e ( i , r ) , p ) ) (\n"; print "#define H(O,r)R(O,r)\n"; print "#include\n"; while (<>) { chop; s,/\*.*\*/,,g; # rm one line comments s:([^a-z])int:$1R(e(t,n),i):g; s:for:R(e(r,o),f):g; s:main:R(e(n,i),e(a,m)):g; s:([^a-z])char:$1R(e(r,a),e(h,c)):g; s:printf \(:P :g; s:void:R(e(d,i),e(o,v)):g; s:else:R(e(e,s),e(l,e)):g; s:while:H(R(e(e,l),i),e(h,w)):g; s:struct:H(R(e(t,c),u),R(e(r,t),s)):g; s:return:H(R(e(n,r),u),R(e(t,e),r)):g; s:sscanf:H(R(e(f,n),a),R(e(c,s),s)):g; s:typedef:H(R(e(f,e),e(d,e)),R(e(p,y),t)):g; s:unsigned:H(R(e(d,e),e(n,g)),R(e(i,s),e(n,u))):g; s,"/", O(/),g; s,20,4*5,g; s," = ",C O(=) C,g; s," %c ",C O(%)O(c)C,g; s,"%c",O(%)O(c),g; s,"%ld",O(%)O(l)O(d),g; s,"\*/\+-",O(*)O(/)O(+)O(-),g; s,init,I,g; s,param,p,g; s,static,M,g; s,work,w,g; s,cursor,c,g; s,normalize,n,g; s,fraction,F,g; s,num,n,g; s,den,t,g; s,LONGEST,U,g; s,argc,a,g; s,argv,v,g; s,bitvec,B,g; s,mask,W,g; s,FMT,D,g; s,p_f,f,g; s,calc,k,g; s,out,o,g; s:10:e(0,1):g; s:<<:e(<,<):g; s:!=:e(=,!):g; s:if:e(f,i):g; s:\*=:e(=,*):g; s:/=:e(=,/):g; s:\|\|:e(|,|):g; s:&&:e(&,&):g; s:42:6*7:g; s:(\S): $1 :g unless /^#/; s:[ \t]+: :g; s:^ ::; s: $::; unless (/^$/) { $code .= "$_ " unless (/^#/); } } chop $code; $code =~ s/(.{31}) /$1\n/g; @line = split (/\n/, $code); for ($i = 0; $i <= $#line; ++$i) { $_ = $line[$i]; $rem = $i % 8; if ($rem == 0) { s/(.{8})(.{8})(.{8})(.{7})/\t\1\t\2\t\3\t\4/; } elsif ($rem == 1 || $rem == 7) { substr ($_,1,1) = "\t"; substr ($_,7,1) = "\t"; substr ($_,9,1) = "\t"; substr ($_,15,1) = "\t"; substr ($_,17,1) = "\t"; substr ($_,23,1) = "\t"; substr ($_,25,1) = "\t"; } elsif ($rem == 2 || $rem == 6) { substr ($_,3,1) = "\t"; substr ($_,7,1) = "\t"; substr ($_,11,1) = "\t"; substr ($_,15,1) = "\t"; substr ($_,19,1) = "\t"; substr ($_,23,1) = "\t"; substr ($_,27,1) = "\t"; } elsif ($rem == 3 || $rem == 5) { substr ($_,5,1) = "\t"; substr ($_,7,1) = "\t"; substr ($_,13,1) = "\t"; substr ($_,15,1) = "\t"; substr ($_,21,1) = "\t"; substr ($_,23,1) = "\t"; substr ($_,29,1) = "\t"; } else { s/(.{8})(.{8})(.{8})(.{7})/\1\t\2\t\3\t\4/; } print "$_\n"; }