混淆的科学计算器。请解释它是如何工作的

时间:2013-08-02 12:48:50

标签: c obfuscation

我试图了解以下程序的工作原理。它是一个命令行科学计算器。来源取自here。对于IOCCC条目来说,这看起来很可读,但显然不是。

#include <stdio.h>
#include <math.h>
#define clear 1;if(c>=11){c=0;sscanf(_,"%lf%c",&r,&c);while(*++_-c);}\
  else if(argc>=4&&!main(4-(*_++=='('),argv))_++;g:c+=
#define puts(d,e) return 0;}{double a;int b;char c=(argc<4?d)&15;\
  b=(*_%__LINE__+7)%9*(3*e>>c&1);c+=
#define I(d) (r);if(argc<4&&*#d==*_){a=r;r=usage?r*a:r+a;goto g;}c=c
#define return if(argc==2)printf("%f\n",r);return argc>=4+
#define usage main(4-__LINE__/26,argv)
#define calculator *_*(int)
#define l (r);r=--b?r:
#define _ argv[1]
#define x

double r;
int main(int argc,char** argv){
  if(argc<2){
    puts(
      usage: calculator 11/26+222/31
      +~~~~~~~~~~~~~~~~~~~~~~~~calculator-\
      !                          7.584,367 )
      +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
      ! clear ! 0 ||l   -x  l   tan  I (/) |
      +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
      ! 1 | 2 | 3 ||l  1/x  l   cos  I (*) |
      +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
      ! 4 | 5 | 6 ||l  exp  l  sqrt  I (+) |
      +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
      ! 7 | 8 | 9 ||l  sin  l   log  I (-) |
      +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(0
    );
  }
  return 0;
}

它使用-Wall在没有警告的情况下编译gcc 4.7.2(Linux)。我试图简化它,但对下面给出的略微修改的形式的任何进一步更改都会产生意外结果(输出错误)。

#include <stdio.h>
#include <math.h>
#define clear 1;if(c>=11){c=0;sscanf(_,"%lf%c",&r,&c);while(*++_-c);}\
  else if(argc>=4&&!main(4-(*_++=='('),argv))_++;g:c+=
#define puts(d,e) return 0;}{double a;int b;char c=(argc<4?d)&15;\
  b=(*_%__LINE__+7)%9*(3*e>>c&1);c+=
#define I(d) (r);if(argc<4&&*#d==*_){a=r;r=usage?r*a:r+a;goto g;}c=c
#define return if(argc==2)printf("%f\n",r);return argc>=4+
#define usage main(4-__LINE__/26,argv)
#define calculator *_*(int)
#define l (r);r=--b?r:
#define _ argv[1]
#define x

double r;
int main(int argc,char** argv){
  if(argc<2){
    puts(
      usage: calculator 11/26+222/31
      +calculator-\
      !                          7.584,367 )
      +
      ! clear ! 0 ||l   -x  l   tan  I (/) |
      +
      ! 1 | 2 | 3 ||l  1/x  l   cos  I (*) |
      +
      ! 4 | 5 | 6 ||l  exp  l  sqrt  I (+) |
      +
      ! 7 | 8 | 9 ||l  sin  l   log  I (-) |
      +(0);
  }
  return 0;
}

有人可以解释它是如何运作的吗?

1 个答案:

答案 0 :(得分:4)

它扩展为如下所示:

double r;
int
main (int argc, char **argv)
{
  if (argc < 2)
    {
      if (argc == 2)
    printf ("%f\n", r);
      return argc >= 4 + 0;
    }
  {
    double a;
    int b;
    char c = (argc < 4 ? main (4 - 21 / 26,
                   argv) : *argv[1] * (int) 11 / 26 + 222 / 31 +
          ~~~~~~~~~~~~~~~~~~~~~~~~*argv[1] * (int) -!7.584) & 15;
    b = (*argv[1] % 21 + 7) % 9 * (3 * 367 >> c & 1);
    c += +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+!1;
    if (c >= 11)
      {
    c = 0;
    sscanf (argv[1], "%lf%c", &r, &c);
    while (*++argv[1] - c);
      }
    else if (argc >= 4 && !main (4 - (*argv[1]++ == '('), argv))
      argv[1]++;
  g:c += !0 || (r);
    r = --b ? r : -(r);
    r = --b ? r : tan (r);
    if (argc < 4 && *"/" == *argv[1])
      {
    a = r;
    r = main (4 - 23 / 26, argv) ? r * a : r + a;
    goto g;
      }
    c = c | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+!1 | 2 | 3 || (r);
    r = --b ? r : 1 / (r);
    r = --b ? r : cos (r);
    if (argc < 4 && *"*" == *argv[1])
      {
    a = r;
    r = main (4 - 25 / 26, argv) ? r * a : r + a;
    goto g;
      }
    c = c | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+!4 | 5 | 6 || (r);
    r = --b ? r : exp (r);
    r = --b ? r : sqrt (r);
    if (argc < 4 && *"+" == *argv[1])
      {
    a = r;
    r = main (4 - 27 / 26, argv) ? r * a : r + a;
    goto g;
      }
    c = c | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+!7 | 8 | 9 || (r);
    r = --b ? r : sin (r);
    r = --b ? r : log (r);
    if (argc < 4 && *"-" == *argv[1])
      {
    a = r;
    r = main (4 - 29 / 26, argv) ? r * a : r + a;
    goto g;
      }
    c = c | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(0);
  }
  if (argc == 2)
    printf ("%f\n", r);
  return argc >= 4 + 0;
}

它仍然不完全可读,但至少现在没有任何东西对你隐藏。你应该能够做到简化而不会被咬得太厉害。

由于同样的原因@cmaster说,我不打算进行完整的解释。