Monday, February 07, 2005 10:32 AM
Michael S. Kaplan
Normalization as obfuscation in C#
Take a look at the following code, let me know what you think of it (compiled with Whidbey Beta 2, note the preview of the exciting new StringInfo methods for dealing with text elements!):
namespace àáâãäå {
using System;
using System.Text;
using System.Globalization;
class àáâãäå
{
[STAThread]
static void Main(string[] args) {
àáâãäå(); àáâãäå(); àáâãäå();
àáâãäå(); àáâãäå(); àáâãäå(); àáâãäå();
}
static void àáâãäå(string àáâãäå) {
StringBuilder àáâãäå = new StringBuilder();
StringInfo àáâãäå = new StringInfo(àáâãäå);
àáâãäå.Append(àáâãäå.Normalize(NormalizationForm.FormC));
àáâãäå.Append(": ");
for(int àáâãäå=0; àáâãäå < àáâãäå.LengthInTextElements; àáâãäå++) {
string àáâãäå = àáâãäå.SubstringByTextElements(àáâãäå, 1);
if(àáâãäå.IsNormalized(NormalizationForm.FormC)) {
àáâãäå.Append("C");
} else if(àáâãäå.IsNormalized(NormalizationForm.FormD)) {
àáâãäå.Append("D");
} else {
àáâãäå.Append("_");
}
}
Console.WriteLine(àáâãäå.ToString());
return;
}
static void àáâãäå() {
àáâãäå.àáâãäå("àáâãäå");
}
static void àáâãäå() {
àáâãäå.àáâãäå("àáâãäå");
}
static void àáâãäå() {
àáâãäå.àáâãäå("àáâãäå");
}
static void àáâãäå() {
àáâãäå.àáâãäå("àáâãäå");
}
static void àáâãäå() {
àáâãäå.àáâãäå("àáâãäå");
}
static void àáâãäå() {
àáâãäå.àáâãäå("àáâãäå");
}
static void àáâãäå() {
àáâãäå.àáâãäå("àáâãäå");
}
}
}
It compiles, even though it looks like the namespace, the class name, every procedure (other than main) and every variable looks like the same string.
The wonders of various combinations of the string "àáâãäå".
(Interestingly, due to all the exciting work of someone from the VS team, the cursor moves over the text elements as letters and thus it was an interesting challenge getting this written!)
Do you think it makes the code less readable? :-)
Compile it on the command line:
c:\temp>csc àáâãäå.cs
and then run it to see the output:
c:\temp>àáâãäå
àáâaäå: CCCCCC
àáâaäå: DDDDDD
àáâaäå: DCCCCC
àáâaäå: DDCCCC
àáâaäå: DDDCCC
àáâaäå: DDDDCC
àáâaäå: DDDDDC
Interesting, no? :-)
This post brought to you by "à", "á", "â", "ã", "ä", and "å" (U+00e0, U+00e1, U+00e2, U+00e3, U+00e4, and U+00e5, a.k.a. LATIN SMALL LETTER A WITH GRAVE, LATIN SMALL LETTER A WITH ACUTE, LATIN SMALL LETTER A WITH CIRCUMFLEX, LATIN SMALL LETTER A WITH TILDE, LATIN SMALL LETTER A WITH DIAERESIS, and LATIN SMALL LETTER A WITH RING ABOVE)
Well who did you think would be willing to sponsor this rubbish? :-)