From 16c421574fd6c2568da51fc36c47cf811ba23442 Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Mon, 11 Jul 2011 23:21:10 +1000 Subject: [PATCH] Add the scramble() and unscramble() functions These functions provide simple encryption and decryption to thwart casual cheating! --- src/utils.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/utils.h | 5 ++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/utils.c b/src/utils.c index cad6019..0d18c13 100644 --- a/src/utils.c +++ b/src/utils.c @@ -46,7 +46,6 @@ static char *data_directory_str = NULL; // Writable data dir pathname * Utility function definitions * ************************************************************************/ - /*----------------------------------------------------------------------- Function: init_program_name - Make the program name "canonical" Arguments: argv - Same as passed to main() @@ -305,3 +304,77 @@ extern int randi (int limit) { return randf() * (double) limit; } + + +/*----------------------------------------------------------------------- + Function: scramble - Scramble (encrypt) the buffer + Arguments: key - Encryption/decryption key + buf - Pointer to buffer to encrypt + bufsize - Size of buffer + Returns: char * - Pointer to buffer + + This function scrambles (encrypts) the buffer pointed to by buf using a + trivial in-place encryption algorithm. If key is zero, no encryption + takes place. The buffer buf must contain a string terminated by '\0'. + The characters '\r', '\n' and '\0' are guaranteed to remain the same + after encryption. At most bufsize characters are encrypted; buf is + returned as the result. +*/ + +char *scramble (int key, char *buf, int bufsize) +{ + int i; + char c, e; + + + if (key != 0) { + key = (~ key) & 0xFF; + for (i = 0; (i < bufsize) && (buf[i] != '\0'); i++) { + c = buf[i]; + e = c ^ key; // Simple encryption: XOR! + if ((c != '\r') && (c != '\n') + && (e != '\r') && (e != '\n') && (e != '\0')) { + buf[i] = e; + } + } + } + + return buf; +} + + +/*----------------------------------------------------------------------- + Function: unscramble - Unscramble (decrypt) the buffer + Arguments: key - Encryption/decryption key + buf - Pointer to buffer to decrypt + bufsize - Size of buffer + Returns: char * - Pointer to buffer + + This function unscrambles (decrypts) the buffer pointed to by buf using + a trivial in-place decryption algorithm. If key is zero, no decryption + takes place. The buffer buf must contain a string terminated by '\0'. + The characters '\r', '\n' and '\0' are guaranteed to remain the same + after decryption. At most bufsize characters are decrypted; buf is + returned as the result. +*/ + +char *unscramble (int key, char *buf, int bufsize) +{ + int i; + char c, d; + + + if (key != 0) { + key = (~ key) & 0xFF; + for (i = 0; (i < bufsize) && (buf[i] != '\0'); i++) { + c = buf[i]; + d = c ^ key; // Simple decryption: XOR! + if ((c != '\r') && (c != '\n') + && (d != '\r') && (d != '\n') && (d != '\0')) { + buf[i] = d; + } + } + } + + return buf; +} diff --git a/src/utils.h b/src/utils.h index b7fee37..a87dc80 100644 --- a/src/utils.h +++ b/src/utils.h @@ -77,5 +77,10 @@ extern void init_rand (void); extern double randf (void); extern int randi (int limit); +// Encryption and decryption functions to stop casual cheating! + +extern char *scramble (int key, char *buf, int bufsize); +extern char *unscramble (int key, char *buf, int bufsize); + #endif /* included_UTILS_H */