diff --git a/hex2chars.c b/hex2chars.c new file mode 100644 --- /dev/null +++ b/hex2chars.c @@ -0,0 +1,139 @@ +/** + * $Id: hex2chars.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/hex2chars.c,v $ + * + */ + +#include +#include +#include +#include + +#ifndef RUNS +#define RUNS 16 +#endif + +/* + * =708== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1) + * ==708== malloc/free: in use at exit: 352 bytes in 32 blocks. + * ==708== malloc/free: 48 allocs, 16 frees, 3,680 bytes allocated. + * ==708== For counts of detected errors, rerun with: -v + * ==708== searching for pointers to 32 not-freed blocks. + * ==708== checked 68,684 bytes. + */ + +const char *characters = "xuzicjklmneopbdaqfrstvgwhy"; + +char *remap_works (char *str, int slen) +{ + int i; + int pos; + static char *s; + + if ((s = malloc (slen + 1)) == NULL) + return NULL; + + for (i = 0; i < slen; i++) { + if (str[i] >= 'a') { + pos = str[i] - 'a'; + s[i] = characters[pos]; + } else { + /* + * Let numbers asis + */ + s[i] = str[i]; + } + } + + s[i] = 0; + return s; +} + +char *remap_works_not (char *str, int slen) +{ + int i, j; + double value; + double *table; + static char *s; + + int clen = strlen (characters); + + if ((s = malloc (slen + 1)) == NULL) + return NULL; + + if ((table = calloc (clen, sizeof (double))) == NULL) + return NULL; + + for (i = 0; i < clen; i++) + table[i] = cos ('a' + i); + + for (i = 0; i < slen; i++) { + if (str[i] >= 'a') { + value = cos (str[i]); + for (j = 0; j < clen; j++) { + /* + * flip and expand characters + */ + s[i] = '*'; + if (value == table[j]) { + s[i] = characters[j]; + break; + } + + if ((fabs (value - table[j])) < 0.001) { + s[i] = characters[j]; + break; + } + } + } else { + /* + * Let numbers asis + */ + s[i] = str[i]; + } + } + + s[i] = 0; + + if (table != NULL) + free (table); + + return s; +} + +int main (int argc, char **argv) +{ + int i; + int len; + char *start; + char *end; + + if (argc != 2) { + printf ("Usage: %s \n", argv[0]); + exit (0); + } + + start = argv[1]; + len = strlen (start); + + printf ("\n --- REMAP_WORKS FINE ---\n\n"); + for (i = 0; i < RUNS; i++) { + if ((end = remap_works (start, len)) != NULL) + printf ("%s\t<==>\t%s\n", start, end); + start = end; + } + + /* + * reset state + */ + start = argv[1]; + + printf ("\n --- REMAP_WORKS_NOT because floating-point comparison is evil at all ---\n\n"); + for (i = 0; i < RUNS; i++) { + if ((end = remap_works_not (start, len)) != NULL) + printf ("%s\t<==>\t%s\n", start, end); + start = end; + } + + return 0; +}