# Quines

In computing, a quine is a computer program which produces a copy of its own source code as its only output. A quine is a fixed point of an execution environment, when the execution environment is viewed as a function. Quines are possible in any programming language that has the ability to output any computable string, as a direct consequence of Kleene's recursion theorem.
[Quoted from Wikipedia]

The following line is an example for an quine in c:
char*x="char*x=%c%s%c;main() {printf(x,34,x,34,10);}%c";main() {printf(x,34,x,34,10);}

## Quines as fixed points

Fixed points x of functions f() are arguments whoses according solution f(x) ist the same (f(x) = x).
Quines are such fixed points, where:
x is the sourcecode, and
f() is the interpreter / compiler with its underlaying system.

Some Fixed points are trivial to find, e.g. in many languages the empty string or in HQ9+ the program „Q“ ;). Having one quine found, its often easy to compute a whole family of similar quines by modifying the source systematically. That way a payload (called „intron“) can be inserted.

In the case of an interpreted language its possible to redirect the output of an quine to a file which will be the same as the executed program. In the case of an compiled language, executing the binary of the quine will output the source code. For receiving the executed program, its necessary to compile it again. Seeing the code as argument x for the compilerfunction f1(), the binary is y = f1(x). The execution environment as function f2() applied to the machine code y will result to f2(y) = f2(f1(x)) = x. For above mentioned compiled languages, its hard to find quines x with f1(x) = x (fixed point over compiler without execution environment) or f2(y) = y (fixed point over execution environment without compiler). In particular if it should be a quine in the above mentioned way (f2(y) = f2(f1(x)) = x) its sophisticated, because it demands x = y. That kind of programs are „Polyglot Quines“ which will be studied further down.

## Quines using more than one languages

### Shapeshifting Quines

In the following section, I want to explain an approach for quines which uses more than one language:
Provided you have different quines with introns in languages which are Turing-complete and allows simply string processing, its relatively easy to write „Shapeshifting Quines“. Those are a set of different quines xi in the according runtime environments fi(), which satisfy the condition ∀m,n: fm(xm,n) = xn. This means, every Quine xm can print all other quines xn (the choise of the ouput-language n is decided by an delivered argument).

#### My Implementation

Each of the programs must contain a representation of all other codes. I used two arrays of escaped ASCII-strings. The first one for the actual codes, the second one for keeping the information how fields of arrays are seperated in the different languages. During execution, the code is printed, escape sequences are interpreted and at the location of the both arrays (represented by %s), the arrays are newly assembled and includet.
This design allows easy addition of further languages (the effort for a new language is independent of the number of already implemented ones).
The output-language can be choosen via command-line argument (0: Python, 1: ShellScript, 2: C, 3: Assembler). If the argument is omitted, the code in the current language is returned.

View the Source (and Output) in Python
View the Source (and Output) as ShellScript
View the Source (and Output) in C
View the Source (and Output) in Assembler

Anyhow, it would be silly to release just an binary ;)

### Polyglot Quines

Polyglots are programs which are valid in more than one language (one file being evaluated by different interpreters or compilers). Polyglots are based on creative use of the involved languages, not all combinations of languages are possible (if the valid programs of the languages are disjoint).
Polyglot Quines (programs which can be interpreted by different runtime systems and printing allways the own source) are consequently hard to write.

#### Example

I wrote the following Polyglot Quine in C, ShellScript and Python. After some includes and declarations, the in hexadecimal ASCII encoded source is assigned to the variable „quine“ in all languages. Thereafter the output is generated for each language. The difficulty is to mask the code of the different programming languages in front of each other.

1 #include <stdio.h> // \
2 nx='''
3 /*
4 ''' # */ char* \
5 quine="23696e636c756465203c737464696f2e683e202f2f205c20200a6e783d2727270a2f2a0a2727272023202a2f20636861722a205c0a7175696e653d2240223b0a2320646566696e65206e78202f2f205c0a6e783d2727270a696e74206d61696e28766f6964297b2063686172206865785b355d2c202a73746f703b206865785b305d3d2730273b206865785b315d3d2778273b206865785b345d3d303b20696e7420693b20666f722028693d303b207175696e655b695d3b20692b3d3229207b206865785b325d3d7175696e655b695d3b206865785b335d3d7175696e655b692b315d3b206966287175696e655b695d3d3d273427202626207175696e655b692b315d3d3d27302729207b207072696e746628222573222c207175696e65293b207d20656c7365207b207072696e746628222563222c20737472746f6c286865782c202673746f702c20313629293b207d207d207d202f2a0a2727270a60222222226563686f206578656320747275653b2028206c656e3d24286563686f20247175696e65207c207763202d63293b20666f7220282820693d303b693c2428286c656e2d3129293b692b3d322029293b20646f20633d247b7175696e653a24693a327d3b206966205b2022246322203d2022343022205d203b207468656e206563686f202d6e20247175696e653b20656c7365207072696e746620225c5c782463223b2066693b20646f6e652029203e2632603b202320222222600a696d706f7274207379730a693d300a7768696c652069203c206c656e287175696e65293a200a202020206966207175696e655b693a692b325d3d3d223430223a0a20202020202020207379732e7374646f75742e777269746528207175696e6520290a20202020656c73653a0a20202020202020207379732e7374646f75742e7772697465282063687228696e74287175696e655b693a692b325d2c3136292920290a2020202069202b3d20322023202a2f0a";
6 # define nx // \
7 nx='''
8 int main(void){ char hex[5], *stop; hex[0]='0'; hex[1]='x'; hex[4]=0; int i; for (i=0; quine[i]; i+=2) { hex[2]=quine[i]; hex[3]=quine[i+1]; if(quine[i]=='4' && quine[i+1]=='0') { printf("%s", quine); } else { printf("%c", strtol(hex, &stop, 16)); } } } /*
9 '''
10 `""""echo exec true; ( len=\$(echo \$quine | wc -c); for (( i=0;i<\$((len-1));i+=2 )); do c=\${quine:\$i:2}; if [ "\$c" = "40" ] ; then echo -n \$quine; else printf "\\x\$c"; fi; done ) >&2`; # """`
11 import sys
12 i=0
13 while i < len(quine):
14     if quine[i:i+2]=="40":
15         sys.stdout.write( quine )
16     else:
17         sys.stdout.write( chr(int(quine[i:i+2],16)) )
18     i += 2 # */