C++ 这个人是如何使用 Microsoft Paint 编写“Hello World”的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5588649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How did this person code "Hello World" with Microsoft Paint?
提问by Eamonn O'Brien
I have just seen this within the past few days and cannot figure out how it works. The video I talk about is here:
我刚刚在过去几天内看到了这一点,无法弄清楚它是如何工作的。我谈论的视频在这里:
It's the top rated answerfrom this Stack Overflow question: Why was this program rejected by three compilers?
这是Stack Overflow 问题中评分最高的答案:为什么这个程序被三个编译器拒绝?
How is this bitmap able to show a C++ program for "Hello World"?
这个位图如何能够显示“Hello World”的 C++ 程序?
回答by Matteo Italia
A BMP (DIB) image is composed by a header followed by uncompressed1color data (for 24 bpp images it's 3 bytes per pixel, stored in reverse row order and with 4 bytes row stride).
BMP (DIB) 图像由标题和未压缩的1色数据组成(对于 24 bpp 图像,它是每像素 3 个字节,以相反的行顺序存储,行间距为 4 个字节)。
The bytes for color data are used to represent colors (i.e. none of them are "mandated" by the file format2, they all come from the color of each pixel), and there's a perfect 1:1 correspondence between pixel colors and bytes written in the file; thus, using perfectly chosen colors you can actually write anything you want in the file (with the exception of the header).
对于颜色数据的字节被用于表示颜色(它们都不即是“授权的”由文件格式2,它们都来自每个像素的颜色),并有一个完美的1:像素颜色之间一一对应和字节写入在文件中;因此,使用完美选择的颜色,您实际上可以在文件中写入任何您想要的内容(标题除外)。
When you open the generated file in notepad, the color data will be shown as text; you can still clearly see from the header (the part from BM
to the start of the text), that is mandated by the file format.
当您在记事本中打开生成的文件时,颜色数据将显示为文本;您仍然可以从标题(BM
文本开头的部分)清楚地看到,这是文件格式所要求的。
In my opinion this video was done this way: first the author calculated the size needed for the bitmap, and created a DIB file of the correct size filled with a color that expands to a simple pattern (e.g. all bytes 65 => 'A'
); then replaced such pattern with the "payload" code, as shown in the video.
在我看来,这个视频是这样完成的:首先,作者计算了位图所需的大小,并创建了一个正确大小的 DIB 文件,其中填充了一种扩展为简单模式的颜色(例如所有字节 65 => 'A'
);然后用“有效载荷”代码替换这种模式,如视频所示。
Notice however that it's not impossible to hand-craft the whole thing with notepad - with the color chooser dialog, an ASCII table and a basic knowledge of the DIB format it can be done, but it would be much much slower and error-prone.
但是请注意,使用记事本手工制作整个事情并非不可能 - 使用颜色选择器对话框、ASCII 表和 DIB 格式的基本知识可以完成,但它会慢得多并且容易出错。
More info about the DIB format
- There are RLE compressed DIBs, but in this case uncompressed bitmaps are used (and they are used really rarely anyway).
- With the exception of the stride, that was avoided using rows multiple of 4 bytes.
- 有 RLE 压缩的 DIB,但在这种情况下使用未压缩的位图(无论如何它们很少使用)。
- 除了 stride 之外,使用 4 字节的行倍数可以避免这种情况。
回答by Andrew Grimm
I assume you're referring to the answer to one of the April Fools questions.
我假设您指的是愚人节问题之一的答案。
My guess is that each pixel has a binary representation for it. And that each character in source code has a binary representation for it.
我的猜测是每个像素都有一个二进制表示。并且源代码中的每个字符都有一个二进制表示。
The person who created the program must have worked out the color for each pixel that'd have a binary representation that'd correspond to each character.
创建程序的人必须为每个像素计算出颜色,这些像素具有对应于每个字符的二进制表示。
回答by osa
From a theoretical computer science point of view, it would be interesting to ask, if every program can be written in such a way so that, viewed as a bitmap, you actually saw the source code that does the same thing. If you are seriously interested in such results, read e.g. about the Kleene's fixed point theorem.
从理论计算机科学的角度来看,如果每个程序都可以以这样的方式编写,那么从位图来看,您实际上看到了做同样事情的源代码,这会很有趣。如果您对这样的结果非常感兴趣,请阅读例如关于Kleene 不动点定理的内容。
Program-as-an-image can also be viewed as a form of code obfuscation. Not that it were particularly practical...
程序即图像也可以被视为代码混淆的一种形式。并不是说它特别实用......