Skip to content

Instantly share code, notes, and snippets.

@Eibwen
Last active December 11, 2015 04:19
Show Gist options
  • Save Eibwen/4544594 to your computer and use it in GitHub Desktop.
Save Eibwen/4544594 to your computer and use it in GitHub Desktop.
Finally got bored enough to get around to writing a quine...
void Main()
{
string pre = SOURCE.Substring(0, SOURCE.LastIndexOf(DELIMITER));
string post = SOURCE.Substring(SOURCE.LastIndexOf(DELIMITER) + 1);
Console.Write(pre + SOURCE.Replace("\"", "\"\"") + post);
}
// Define other methods and classes here
const char DELIMITER = '$';
const string SOURCE = @"void Main()
{
string pre = SOURCE.Substring(0, SOURCE.LastIndexOf(DELIMITER));
string post = SOURCE.Substring(SOURCE.LastIndexOf(DELIMITER) + 1);
Console.Write(pre + SOURCE.Replace(""\"""", ""\""\"""") + post);
}
// Define other methods and classes here
const char DELIMITER = '$';
const string SOURCE = @""$"";
";
void Main()
{
Console.Write(SOURCE.Insert(POSITION, SOURCE.Replace("\"", "\"\"")));
}
// Define other methods and classes here
const int POSITION = 186;
const string SOURCE = @"void Main()
{
Console.Write(SOURCE.Insert(POSITION, SOURCE.Replace(""\"""", ""\""\"""")));
}
// Define other methods and classes here
const int POSITION = 186;
const string SOURCE = @"""";
";
void Main()
{
Console.Write(SOURCE.Insert(154, SOURCE.Replace("\"", "\"\"")));
}
// Define other methods and classes here
const string SOURCE = @"void Main()
{
Console.Write(SOURCE.Insert(154, SOURCE.Replace(""\"""", ""\""\"""")));
}
// Define other methods and classes here
const string SOURCE = @"""";
";
//This concept could be used to allow a quine function to be added to any program...
// would only need to pad with a few characters, and a unique string to replace from
// 1. point at source file
// 2. find a place to insert the method (so a position inside the class)
// 3. pad that position as needed (start posistion a specific mod3 value, total length a range of mod9 values)
// 3. insert method with replacement characters
// 4. base64 the file and insert the encoded version in
void Main()
{
Console.WriteLine(Encoding.UTF8.GetString(
Convert.FromBase64String(SRC.Replace(
Convert.ToBase64String(Encoding.UTF8.GetBytes("$$$")),
Convert.ToBase64String(Encoding.UTF8.GetBytes(SRC))))));
}
const string SRC = "dm9pZCBNYWluKCkNCnsNCglDb25zb2xlLldyaXRlTGluZShFbmNvZGluZy5VVEY4LkdldFN0cmluZygNCgkJQ29udmVydC5Gcm9tQmFzZTY0U3RyaW5nKFNSQy5SZXBsYWNlKA0KCQkJQ29udmVydC5Ub0Jhc2U2NFN0cmluZyhFbmNvZGluZy5VVEY4LkdldEJ5dGVzKCIkJCQiKSksDQoJCQlDb252ZXJ0LlRvQmFzZTY0U3RyaW5nKEVuY29kaW5nLlVURjguR2V0Qnl0ZXMoU1JDKSkpKSkpOw0KfSANCmNvbnN0IHN0cmluZyBTUkMgPSAiJCQkIjsg";
//This takes some tweaking to generate because both the unencoded verson and encoded version needs the length to be a multiple of 3
// and the replacement of encoded $$$ needs to be at an index multiple of 3
//In this respect, on this version I have some extra space characters
void Main()
{
string src = "aaaaaa$$$bbbbbb";
string SRC = Test(src).Dump();
//(src.Length % 12).Dump();
string ENC = SRC.Replace(
Enc("$$$"),
Enc(SRC));
ENC.Dump();
Denc(ENC).Dump(); //check that results actually hold up
}
public static string Enc(string s) { return Convert.ToBase64String(Encoding.UTF8.GetBytes(s)); }
public static string Denc(string s) { return Encoding.UTF8.GetString(Convert.FromBase64String(s)); }
public static string Test(string source)
{
//(source.Length % 3 == 0).Dump(); //not needed, due to padding
//(source.Length % 4 == 0).Dump(); //not needed
((source.Length + 2) % 9 < 3).Dump();
(source.LastIndexOf("$$$") % 3 == 0).Dump();
string enc = Enc(source);
enc.Contains("JCQk").Dump(); //Double check
(enc.Length % 3 == 0).Dump();
//(enc.Length % 4 == 0).Dump(); //always true
//string rep = Convert.ToBase64String(Encoding.UTF8.GetBytes("$$$")).Dump();
//enc.IndexOf(rep).Dump();
//enc.LastIndexOf(rep).Dump();
return enc;
}
public static void VerifyValidLengths()
{
string b = "";
for (int i = 0; i <= 30; ++i)
{
Util.HorizontalRun(false, i, ": ", ((i+2) % 9) < 3, " - ", !Enc(Enc(b)).EndsWith("=")).Dump();
b += "x";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment