]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
add "xencoding", an encoding scheme of numbers into invisible parts of messages
authorRudolf Polzer <divVerent@xonotic.org>
Sun, 12 Jun 2011 12:02:33 +0000 (14:02 +0200)
committerRudolf Polzer <divVerent@xonotic.org>
Sun, 12 Jun 2011 12:02:33 +0000 (14:02 +0200)
qcsrc/common/util.qc
qcsrc/common/util.qh

index 89cb92baf3fce6b4f08d881a208db2f106f67c63..23d4f3431941958bbf188467aa90ab8ede62061e 100644 (file)
@@ -2017,3 +2017,37 @@ string CTX(string s)
                return s;
        return substring(s, p+1, -1);
 }
+
+// x-encoding (encoding as zero length invisible string)
+const string XENCODE_2  = "xX";
+const string XENCODE_22 = "0123456789abcdefABCDEF";
+string xencode(float f)
+{
+       float a, b, c, d;
+       d = mod(f, 22); f = floor(f / 22);
+       c = mod(f, 22); f = floor(f / 22);
+       b = mod(f, 22); f = floor(f / 22);
+       a = mod(f,  2); // f = floor(f /  2);
+       return strcat(
+               "^",
+               substring(XENCODE_2,  a, 1),
+               substring(XENCODE_22, b, 1),
+               substring(XENCODE_22, c, 1),
+               substring(XENCODE_22, d, 1)
+       );
+}
+float xdecode(string s)
+{
+       float a, b, c, d;
+       if(substring(s, 0, 1) != "^")
+               return -1;
+       if(strlen(s) < 5)
+               return -1;
+       a = strstrofs(XENCODE_2,  substring(s, 1, 1), 0);
+       b = strstrofs(XENCODE_22, substring(s, 2, 1), 0);
+       c = strstrofs(XENCODE_22, substring(s, 3, 1), 0);
+       d = strstrofs(XENCODE_22, substring(s, 4, 1), 0);
+       if(a < 0 || b < 0 || c < 0 || d < 0)
+               return -1;
+       return ((a * 22 + b) * 22 + c) * 22 + d;
+}
index 49afad22d7f19bc62f552b8dcdbf3884f7e8654d..5a5792cb73e054e8ef54a60f9ef4bdab9725a775 100644 (file)
@@ -258,3 +258,10 @@ string prvm_language;
 string language_filename(string s);
 string CTX(string s);
 #define ZCTX(s) strzone(CTX(s))
+
+// x-encoding (encoding as zero length invisible string)
+// encodes approx. 14 bits into 5 bytes of color code string
+const float XENCODE_MAX = 21295; // 2*22*22*22-1
+const float XENCODE_LEN = 5;
+string xencode(float f);
+float xdecode(string s);