author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sun, 19 Jul 2009 19:44:48 +0200 | |
changeset 101 | 176448af80fa |
parent 77 | 49e0babccb23 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
77 | 2 |
* hex2chars.c |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
0 | 4 |
*/ |
5 |
||
6 |
#include <stdio.h> |
|
7 |
#include <stdlib.h> |
|
8 |
#include <string.h> |
|
9 |
#include <math.h> |
|
10 |
||
11 |
#ifndef RUNS |
|
12 |
#define RUNS 16 |
|
13 |
#endif |
|
14 |
||
15 |
/* |
|
16 |
* =708== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1) |
|
17 |
* ==708== malloc/free: in use at exit: 352 bytes in 32 blocks. |
|
18 |
* ==708== malloc/free: 48 allocs, 16 frees, 3,680 bytes allocated. |
|
19 |
* ==708== For counts of detected errors, rerun with: -v |
|
20 |
* ==708== searching for pointers to 32 not-freed blocks. |
|
21 |
* ==708== checked 68,684 bytes. |
|
22 |
*/ |
|
23 |
||
24 |
const char *characters = "xuzicjklmneopbdaqfrstvgwhy"; |
|
25 |
||
26 |
char *remap_works (char *str, int slen) |
|
27 |
{ |
|
28 |
int i; |
|
29 |
int pos; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
30 |
|
0 | 31 |
static char *s; |
32 |
||
33 |
if ((s = malloc (slen + 1)) == NULL) |
|
34 |
return NULL; |
|
35 |
||
36 |
for (i = 0; i < slen; i++) { |
|
37 |
if (str[i] >= 'a') { |
|
38 |
pos = str[i] - 'a'; |
|
39 |
s[i] = characters[pos]; |
|
40 |
} else { |
|
41 |
/* |
|
42 |
* Let numbers asis |
|
43 |
*/ |
|
44 |
s[i] = str[i]; |
|
45 |
} |
|
46 |
} |
|
47 |
||
48 |
s[i] = 0; |
|
49 |
return s; |
|
50 |
} |
|
51 |
||
52 |
char *remap_works_not (char *str, int slen) |
|
53 |
{ |
|
54 |
int i, j; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
55 |
|
0 | 56 |
double value; |
57 |
double *table; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
58 |
|
0 | 59 |
static char *s; |
60 |
||
61 |
int clen = strlen (characters); |
|
62 |
||
63 |
if ((s = malloc (slen + 1)) == NULL) |
|
64 |
return NULL; |
|
65 |
||
66 |
if ((table = calloc (clen, sizeof (double))) == NULL) |
|
67 |
return NULL; |
|
68 |
||
69 |
for (i = 0; i < clen; i++) |
|
70 |
table[i] = cos ('a' + i); |
|
71 |
||
72 |
for (i = 0; i < slen; i++) { |
|
73 |
if (str[i] >= 'a') { |
|
74 |
value = cos (str[i]); |
|
75 |
for (j = 0; j < clen; j++) { |
|
76 |
/* |
|
77 |
* flip and expand characters |
|
78 |
*/ |
|
79 |
s[i] = '*'; |
|
80 |
if (value == table[j]) { |
|
81 |
s[i] = characters[j]; |
|
82 |
break; |
|
83 |
} |
|
84 |
||
85 |
if ((fabs (value - table[j])) < 0.001) { |
|
86 |
s[i] = characters[j]; |
|
87 |
break; |
|
88 |
} |
|
89 |
} |
|
90 |
} else { |
|
91 |
/* |
|
92 |
* Let numbers asis |
|
93 |
*/ |
|
94 |
s[i] = str[i]; |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
s[i] = 0; |
|
99 |
||
100 |
if (table != NULL) |
|
101 |
free (table); |
|
102 |
||
103 |
return s; |
|
104 |
} |
|
105 |
||
106 |
int main (int argc, char **argv) |
|
107 |
{ |
|
108 |
int i; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
109 |
|
0 | 110 |
int len; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
111 |
|
0 | 112 |
char *start; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
113 |
|
0 | 114 |
char *end; |
115 |
||
116 |
if (argc != 2) { |
|
117 |
printf ("Usage: %s <string>\n", argv[0]); |
|
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
118 |
return EXIT_FAILURE; |
0 | 119 |
} |
120 |
||
121 |
start = argv[1]; |
|
122 |
len = strlen (start); |
|
123 |
||
124 |
printf ("\n --- REMAP_WORKS FINE ---\n\n"); |
|
125 |
for (i = 0; i < RUNS; i++) { |
|
126 |
if ((end = remap_works (start, len)) != NULL) |
|
127 |
printf ("%s\t<==>\t%s\n", start, end); |
|
128 |
start = end; |
|
129 |
} |
|
130 |
||
131 |
/* |
|
132 |
* reset state |
|
133 |
*/ |
|
134 |
start = argv[1]; |
|
135 |
||
136 |
printf ("\n --- REMAP_WORKS_NOT because floating-point comparison is evil at all ---\n\n"); |
|
137 |
for (i = 0; i < RUNS; i++) { |
|
138 |
if ((end = remap_works_not (start, len)) != NULL) |
|
139 |
printf ("%s\t<==>\t%s\n", start, end); |
|
140 |
start = end; |
|
141 |
} |
|
142 |
||
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
143 |
return EXIT_SUCCESS; |
0 | 144 |
} |