aaaaaa123456789

C quine

Sep 27th, 2013 (edited)
142
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.20 KB
  1. const char * sourceCode =
  2.   "I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUg"
  3.   "PHN0cmluZy5oPgoKLyoKICAgVGhpcyBwcm9ncmFtIGlzIGEgcXVpbmUuIFRoYXQg"
  4.   "bWVhbnMsIGl0IG91dHB1dHMgaXRzIG93biBzb3VyY2Ugd2hlbiBydW4uCiAgIFRo"
  5.   "ZSBnbG9iYWwgdmFyaWFibGUgc291cmNlQ29kZSBzaW1wbHkgY29udGFpbnMgYSBi"
  6.   "YXNlNjQgZW5jb2Rpbmcgb2YKICAgZXZlcnl0aGluZyB0aGF0IGZvbGxvd3MgLS0g"
  7.   "dGhlIGNvZGUgb25seSBoYXMgdG8gcHJpbnQgaXQgYXMgZGF0YSwgYW5kIHRoZW4K"
  8.   "ICAgYXMgY29kZS4gSWRlYSB0YWtlbiBmcm9tIGh0dHA6Ly93d3cubWFkb3JlLm9y"
  9.   "Zy9+ZGF2aWQvY29tcHV0ZXJzL3F1aW5lLmh0bWwKICAgKG5vdCBzdG9sZW4sIGJ1"
  10.   "dCB0aGUgc2FtZSBwcmluY2lwbGUgaXMgYXBwbGllZCkuCiAgIFRoaXMgc2hvdWxk"
  11.   "IGNvbXBpbGUgdW5kZXIgYW55IEM5OS1jb21wbGlhbnQgY29tcGlsZXIsIGFuZCBp"
  12.   "dCdzIG5vdCBsaW1pdGVkCiAgIHRvIGFueSBzcGVjaWZpYyBhcmNoaXRlY3R1cmUu"
  13.   "CiAgIFRoZSBiYXNlNjQgZW5jb2RpbmcgdXNlcyArIGFuZCAvIGFzIDYyIGFuZCA2"
  14.   "MywgYW5kIG9taXRzIHRyYWlsaW5nIGVxdWFscy4KICAgCiAgIElmIHlvdSByZWFk"
  15.   "IHRoaXMgeW91J3JlIGEgbm9vYi4KKi8KCnZvaWQgb3V0cHV0RGF0YShjb25zdCBj"
  16.   "aGFyICopOwpjaGFyICogYmFzZTY0X2RlY29kZShjb25zdCBjaGFyICopOwp1bnNp"
  17.   "Z25lZCBjaGFyIGJhc2U2NF9pbmRleChjaGFyKTsKCmlubGluZSB2b2lkIG91dHB1"
  18.   "dCAoY29uc3QgY2hhciAqIHN0cmluZykgewogIC8vIGxldCdzIHNwYXJlIG1lIHNv"
  19.   "bWUgdHlwaW5nCiAgcHJpbnRmKCIlcyIsIHN0cmluZyk7Cn0KCmludCBtYWluICh2"
  20.   "b2lkKSB7CiAgb3V0cHV0KCJjb25zdCBjaGFyICogc291cmNlQ29kZSA9XG4iKTsg"
  21.   "Ly8gZmlyc3Qgb2YgYWxsLCB0aGUgZGVjbGFyYXRpb24uLi4KICBvdXRwdXREYXRh"
  22.   "KHNvdXJjZUNvZGUpOyAvLyAuLi5hbmQgdGhlbiB0aGUgZGF0YSBpdCBjb250YWlu"
  23.   "cwogIGNoYXIgKiBwYXJzZWRDb2RlID0gYmFzZTY0X2RlY29kZShzb3VyY2VDb2Rl"
  24.   "KTsgLy8gZGVjb2RlIHRoZSBkYXRhCiAgb3V0cHV0KHBhcnNlZENvZGUpOyAgLy8g"
  25.   "YW5kIG91dHB1dCB0aGUgY29kZSAoaW4gdGhlIGRlY29kZWQgZGF0YSkKICBmcmVl"
  26.   "KHBhcnNlZENvZGUpOyAvLyByZWxlYXNlIHRoZSBidWZmZXIgdGhhdCBiYXNlNjRf"
  27.   "ZGVjb2RlKCkgYWNxdWlyZWQKICByZXR1cm4gMDsKfQoKdm9pZCBvdXRwdXREYXRh"
  28.   "IChjb25zdCBjaGFyICogc3RyaW5nKSB7CiAgLy8gNjQgY2hhcmFjdGVycyBwZXIg"
  29.   "bGluZSwgb3RoZXIgdGhhbiB0aGUgbGFzdCBsaW5lCiAgLy8gbGFzdCBsaW5lIGhh"
  30.   "cyB0aGUgc2VtaWNvbG9uCiAgY2hhciBidWZmZXJbNjVdOwogIG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vbiwgYW5kIHR3byBuZXdsaW5l"
  46.   "cwp9CgpjaGFyICogYmFzZTY0X2RlY29kZSAoY29uc3QgY2hhciAqIGRhdGEpIHsK"
  47.   "ICAvLyB0aGlzIGp1c3QgZGVjb2RlcyB0aGUgYmFzZTY0IGVuY29kZWQgZGF0YSBh"
  48.   "bmQgcmV0dXJucyB0aGUgcmVzdWx0CiAgY2hhciAqIHJlc3VsdCA9IG1hbGxvYyhz"
  49.   "dHJsZW4oZGF0YSkgKyAxKTsgLy8gdGhpcyB3aWxsIGNlcnRhaW5seSBzdWZmaWNl"
  50.   "CiAgdW5zaWduZWQgcmVtYWluaW5nID0gc3RybGVuKGRhdGEpOwogIGNvbnN0IGNo"
  51.   "YXIgKiBjdXJyZW50OyAvLyBjdXJyZW50IHBvaW50ZXIgdG8gdGhlIHNvdXJjZSBi"
  52.   "dWZmZXIKICBjaGFyICogaW5zZXJ0aW9uOyAvLyBpbnNlcnRpb24gcG9pbnRlciB0"
  53.   "byB0aGUgZGVzdGluYXRpb24gYnVmZmVyCiAgdW5zaWduZWQgY2hhciBkZWNvZGlu"
  54.   "Z3NbNF07IC8vIHRlbXBvcmFyeSBidWZmZXIKICB1bnNpZ25lZCBjaGFyIGk7CiAg"
  55.   "Zm9yIChjdXJyZW50ID0gZGF0YSwgaW5zZXJ0aW9uID0gcmVzdWx0OyAvLyBpbml0"
  56.   "aWFsIHZhbHVlcwogICAgICAgcmVtYWluaW5nID49IDQ7IC8vIGl0ZXJhdGUgd2hp"
  57.   "bGUgdGhlcmUncyBhdCBsZWFzdCA0IGJ5dGVzIHRvIGRlY29kZQogICAgICAgY3Vy"
  58.   "cmVudCArPSA0LCBpbnNlcnRpb24gKz0gMywgcmVtYWluaW5nIC09IDQpIHsgLy8g"
  59.   "aW5jcmVtZW50IGFuZCBzdWNoCiAgICBmb3IgKGkgPSAwOyBpIDwgNDsgaSArKykg"
  60.   "ZGVjb2RpbmdzW2ldID0gYmFzZTY0X2luZGV4KGN1cnJlbnRbaV0pOwogICAgLy8g"
  61.   "YW5kIG5vdyBkZWNvZGUgdGhlIGJhc2U2NCBkYXRhLiBUaGlzIGNvZGUgaXMgYXdm"
  62.   "dWwsIEkga25vdwogICAgaW5zZXJ0aW9uWzBdID0gKGRlY29kaW5nc1swXSA8PCAy"
  63.   "KSB8IChkZWNvZGluZ3NbMV0gPj4gNCk7CiAgICBpbnNlcnRpb25bMV0gPSAoKGRl"
  64.   "Y29kaW5nc1sxXSAmIDE1KSA8PCA0KSB8IChkZWNvZGluZ3NbMl0gPj4gMik7CiAg"
  65.   "ICBpbnNlcnRpb25bMl0gPSAoKGRlY29kaW5nc1syXSAmIDMpIDw8IDYpIHwgZGVj"
  66.   "b2RpbmdzWzNdOwogIH0KICBpZiAocmVtYWluaW5nKSAvLyBpZiBzb21ldGhpbmcg"
  67.   "cmVtYWlucywgZGVjb2RlIGl0IHRvbwogICAgZm9yIChpID0gMDsgaSA8IHJlbWFp"
  68.   "bmluZzsgaSArKykgZGVjb2RpbmdzW2ldID0gYmFzZTY0X2luZGV4KGN1cnJlbnRb"
  69.   "aV0pOwogIHN3aXRjaCAocmVtYWluaW5nKSB7CiAgICBjYXNlIDM6CiAgICAgIGlu"
  70.   "c2VydGlvblsxXSA9ICgoZGVjb2RpbmdzWzFdICYgMTUpIDw8IDQpIHwgKGRlY29k"
  71.   "aW5nc1syXSA+PiAyKTsKICAgICAgLy8gbm8gYnJlYWsgb24gcHVycG9zZSEKICAg"
  72.   "IGNhc2UgMjoKICAgICAgaW5zZXJ0aW9uWzBdID0gKGRlY29kaW5nc1swXSA8PCAy"
  73.   "KSB8IChkZWNvZGluZ3NbMV0gPj4gNCk7CiAgfQogIGluc2VydGlvbiArPSByZW1h"
  74.   "aW5pbmcgPyByZW1haW5pbmcgOiAxOyAvLyB1Z2x5LCBidXQgd29ya3MKICBpbnNl"
  75.   "cnRpb25bLTFdID0gMDsgLy8gYmVmb3JlIHRoZSB3dGYsIHRoaXMganVzdCBhZGRz"
  76.   "IGEgbnVsbCB0ZXJtaW5hdG9yCiAgcmV0dXJuIHJlYWxsb2MocmVzdWx0LCBpbnNl"
  77.   "cnRpb24gLSByZXN1bHQpOyAvLyBva2F5LCB5b3UgY2FuIHd0ZiBub3cKfQoKdW5z"
  78.   "aWduZWQgY2hhciBiYXNlNjRfaW5kZXggKGNoYXIgY2hhcmFjdGVyKSB7CiAgLy8g"
  79.   "dGhpcyBmdW5jdGlvbiByZXR1cm5zIGEgbnVtYmVyIGZyb20gMCB0byA2MyBpbmRp"
  80.   "Y2F0aW5nIHdoYXQgdGhlIHZhbHVlCiAgLy8gb2YgYSBjZXJ0YWluIGJhc2U2NCBl"
  81.   "bmNvZGluZyBjaGFyYWN0ZXIgaXMKICAvLyBBLVo6IDAtMjUsIGEtejogMjYtNTEs"
  82.   "IDAtOTogNTItNjEsICs6IDYyLCAvOiA2MwogIGlmIChjaGFyYWN0ZXIgPT0gJysn"
  83.   "KSByZXR1cm4gNjI7CiAgaWYgKGNoYXJhY3RlciA9PSAnLycpIHJldHVybiA2MzsK"
  84.   "ICBpZiAoKGNoYXJhY3RlciA+PSAnMCcpICYmIChjaGFyYWN0ZXIgPD0gJzknKSkg"
  85.   "cmV0dXJuIGNoYXJhY3RlciArIDUyIC0gJzAnOwogIGlmICgoY2hhcmFjdGVyID49"
  86.   "ICdBJykgJiYgKGNoYXJhY3RlciA8PSAnWicpKSByZXR1cm4gY2hhcmFjdGVyIC0g"
  87.   "J0EnOwogIGlmICgoY2hhcmFjdGVyID49ICdhJykgJiYgKGNoYXJhY3RlciA8PSAn"
  88.   "eicpKSByZXR1cm4gY2hhcmFjdGVyICsgMjYgLSAnYSc7CiAgcmV0dXJuIDI1NTsg"
  89.   "Ly8gaW52YWxpZCB2YWx1ZQp9Cg";
  90.  
  91. #include <stdio.h>
  92. #include <stdlib.h>
  93. #include <string.h>
  94.  
  95. /*
  96.    This program is a quine. That means, it outputs its own source when run.
  97.    The global variable sourceCode simply contains a base64 encoding of
  98.    everything that follows -- the code only has to print it as data, and then
  99.    as code. Idea taken from http://www.madore.org/~david/computers/quine.html
  100.    (not stolen, but the same principle is applied).
  101.    This should compile under any C99-compliant compiler, and it's not limited
  102.    to any specific architecture.
  103.    The base64 encoding uses + and / as 62 and 63, and omits trailing equals.
  104.    
  105.    If you read this you're a noob.
  106. */
  107.  
  108. void outputData(const char *);
  109. char * base64_decode(const char *);
  110. unsigned char base64_index(char);
  111.  
  112. inline void output (const char * string) {
  113.   // let's spare me some typing
  114.   printf("%s", string);
  115. }
  116.  
  117. int main (void) {
  118.   output("const char * sourceCode =\n"); // first of all, the declaration...
  119.   outputData(sourceCode); // ...and then the data it contains
  120.   char * parsedCode = base64_decode(sourceCode); // decode the data
  121.   output(parsedCode);  // and output the code (in the decoded data)
  122.   free(parsedCode); // release the buffer that base64_decode() acquired
  123.   return 0;
  124. }
  125.  
  126. void outputData (const char * string) {
  127.   // 64 characters per line, other than the last line
  128.   // last line has the semicolon
  129.   char buffer[65];
  130.   buffer[64] = 0; // make sure the null terminator is already there
  131.   const char * current;
  132.   unsigned remainder = strlen(string);
  133.   for (current = string; remainder >= 64; current += 64, remainder -= 64) {
  134.     // iterate for every 64 characters of the string, until not enough remain
  135.     output("  \""); // every line starts like this
  136.     memcpy(buffer, current, 64); // the current 64 characters
  137.     output(buffer);
  138.     output("\"\n"); // and every line ends like this
  139.   }
  140.   output("  \""); // the final line of the data declaration
  141.   memcpy(buffer, current, remainder);
  142.   buffer[remainder] = 0; // make sure the terminator is in the right place!
  143.   output(buffer);
  144.   output("\";\n\n"); // last line has a semicolon, and two newlines
  145. }
  146.  
  147. char * base64_decode (const char * data) {
  148.   // this just decodes the base64 encoded data and returns the result
  149.   char * result = malloc(strlen(data) + 1); // this will certainly suffice
  150.   unsigned remaining = strlen(data);
  151.   const char * current; // current pointer to the source buffer
  152.   char * insertion; // insertion pointer to the destination buffer
  153.   unsigned char decodings[4]; // temporary buffer
  154.   unsigned char i;
  155.   for (current = data, insertion = result; // initial values
  156.        remaining >= 4; // iterate while there's at least 4 bytes to decode
  157.        current += 4, insertion += 3, remaining -= 4) { // increment and such
  158.     for (i = 0; i < 4; i ++) decodings[i] = base64_index(current[i]);
  159.     // and now decode the base64 data. This code is awful, I know
  160.     insertion[0] = (decodings[0] << 2) | (decodings[1] >> 4);
  161.     insertion[1] = ((decodings[1] & 15) << 4) | (decodings[2] >> 2);
  162.     insertion[2] = ((decodings[2] & 3) << 6) | decodings[3];
  163.   }
  164.   if (remaining) // if something remains, decode it too
  165.     for (i = 0; i < remaining; i ++) decodings[i] = base64_index(current[i]);
  166.   switch (remaining) {
  167.     case 3:
  168.       insertion[1] = ((decodings[1] & 15) << 4) | (decodings[2] >> 2);
  169.       // no break on purpose!
  170.     case 2:
  171.       insertion[0] = (decodings[0] << 2) | (decodings[1] >> 4);
  172.   }
  173.   insertion += remaining ? remaining : 1; // ugly, but works
  174.   insertion[-1] = 0; // before the wtf, this just adds a null terminator
  175.   return realloc(result, insertion - result); // okay, you can wtf now
  176. }
  177.  
  178. unsigned char base64_index (char character) {
  179.   // this function returns a number from 0 to 63 indicating what the value
  180.   // of a certain base64 encoding character is
  181.   // A-Z: 0-25, a-z: 26-51, 0-9: 52-61, +: 62, /: 63
  182.   if (character == '+') return 62;
  183.   if (character == '/') return 63;
  184.   if ((character >= '0') && (character <= '9')) return character + 52 - '0';
  185.   if ((character >= 'A') && (character <= 'Z')) return character - 'A';
  186.   if ((character >= 'a') && (character <= 'z')) return character + 26 - 'a';
  187.   return 255; // invalid value
  188. }
RAW Paste Data Copied