gpt4 book ai didi

c - 这个 1984 年国际混淆 C 代码竞赛获奖作品是如何运作的?

转载 作者:太空狗 更新时间:2023-10-29 15:45:49 25 4
gpt4 key购买 nike

我是 C 的新手,我急切地想知道这段代码的工作原理是什么?

int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}

来源可以在这里找到:http://www.ioccc.org/1984/anonymous.c

下面是代码附带的提示:

Dishonorable mention: Anonymous

The author was too embarrassed that he/she could write such trash, so I promised to protect their identity. I will say that the author of this program has a well known connection with the C programming language. This program is a unique variation on the age old "Hello, world" program. What reads like a read may be written like a write! Copyright (c) 1984, Landon Curt Noll. All Rights Reserved. Permission for personal, educational or non-profit use is granted provided this this copyright and notice are included in its entirety and remains unaltered. All other uses must receive prior permission in writing from both Landon Curt Noll and Larry Bassel.

最佳答案

当您有混淆代码时,您需要清理物理布局、添加一些空白、添加必要的缩进,然后编译代码。编译器的警告会告诉您很多有关程序隐藏的一些内容。

先行简化 - 添加空格

int i;
main()
{
for( ; i["]<i;++i){--i;}"];
read('-'-'-', i+++"hell\o, world!\n", '/'/'/'));
}

read(j,i,p)
{
write(j/p+p,i---j,i/i);
}

当使用 gcc -Wall 编译程序时,我收到以下警告:

soc.c:2:1: warning: return type defaults to ‘int’ [enabled by default]
main()
^
soc.c: In function ‘main’:
soc.c:4:4: warning: implicit declaration of function ‘read’ [-Wimplicit-function-declaration]
for( ; i["]<i;++i){--i;}"]; read('-'-'-', i+++"hell\o, world!\n", '/'/'/'));
^
soc.c:4:50: warning: unknown escape sequence: '\o' [enabled by default]
for( ; i["]<i;++i){--i;}"]; read('-'-'-', i+++"hell\o, world!\n", '/'/'/'));
^
soc.c: At top level:
soc.c:7:1: warning: return type defaults to ‘int’ [enabled by default]
read(j,i,p)
^
soc.c: In function ‘read’:
soc.c:7:1: warning: type of ‘j’ defaults to ‘int’ [enabled by default]
soc.c:7:1: warning: type of ‘i’ defaults to ‘int’ [enabled by default]
soc.c:7:1: warning: type of ‘p’ defaults to ‘int’ [enabled by default]
soc.c:9:4: warning: implicit declaration of function ‘write’ [-Wimplicit-function-declaration]
write(j/p+p,i---j,i/i);
^
soc.c:9:17: warning: operation on ‘i’ may be undefined [-Wsequence-point]
write(j/p+p,i---j,i/i);
^
soc.c:9:17: warning: operation on ‘i’ may be undefined [-Wsequence-point]
soc.c:10:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^

二次切割简化 - 去混淆

根据以上警告,程序可以反混淆:

int i;

void read(int j, char* i, int p);
void write(int j, char* i, int p);

int main()
{
for( ; i["]<i;++i){--i;}"];
read('-'-'-', , '/'/'/'));
return 0;
}

void read(int j, char* i, int p)
{
write(j/p+p, (i--) - j, 1);
}

以上版本没有编译器警告并产生相同的输出。

第三次简化 - 取消混淆更多

表达式i["]<i;++i){--i;}"]用于运行循环 14 次。就那么简单。可以简化为i < 14 .

'-'-'-'0 .

'/'/'/'1 .

i++ + "hello, world!\n"s + i++ 相同其中 s可以是char const* s = "hello, world!\n";

for循环可以简化为:

   char const* s = "hello, world!\n";
for( ; i < 14; read(0, s+i++, 1));

由于j的值(value)在 read始终为零,执行 read可以简化为:

void read(int j, char* i, int p)
{
write(0, (i--), 1);
}

表达式(i--)可以简化为 i因为递减作为副作用不会改变函数的工作原理。也就是说,上面的函数是:

void read(int j, char* i, int p)
{
write(0, i, 1);
}

当我们意识到参数 j 的值时总是 0和参数值 p总是 1 , 我们可以改变 for在主函数中循环:

   for( ; i < 14; i++)
{
write(0, s+i, 1);
}

因此,整个程序可以简化为:

void write(int j, char const* i, int p);

int main()
{
int i = 0;
char const* s = "hello, world!\n";
for( ; i < 14; i++ )
{
write(0, s+i, 1);
}
return 0;
}

第四次简化 - 让它变得微不足道

以上版本有一个硬编码编号14 .即s中的字符数.因此,通过将程序更改为:

void write(int j, char const* i, int p);

int main()
{
write(0, "hello, world!\n", 14);
return 0;
}

关于c - 这个 1984 年国际混淆 C 代码竞赛获奖作品是如何运作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33537294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com