-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbad_encoding.c
More file actions
310 lines (277 loc) · 7.59 KB
/
bad_encoding.c
File metadata and controls
310 lines (277 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SPECIAL_SIZE 16
#define MAX_LINE_SIZE 4096
#ifdef __SPE_ARRAY
#define MAX_SPECIALS 2048
char all_specials[MAX_SPECIALS][MAX_SPECIAL_SIZE];
#endif
struct spetree
{
struct spetree* next[256];
char value[MAX_SPECIAL_SIZE];
char target[MAX_SPECIAL_SIZE];
char *line;
};
struct spetree root_spetree;
char *current_line_in_spetree = NULL;
void dump_spe(const char* special)
{
for (const char* s = special ; *s ; s++ )
{
fprintf(stderr, " [%x]", *s);
}
fprintf(stderr, "\n %s\n", special);
}
struct spetree* find_spetree(const char* special, int* length)
{
struct spetree* spt = &root_spetree;
for ( const char* s = special ; *s ; s++ )
{
if ( spt->next[*s] == NULL )
{
struct spetree* next = malloc(sizeof(struct spetree));
memset(next, 0, sizeof(struct spetree));
spt->next[*s] = next;
}
spt = spt->next[*s];
(*length) ++;
}
return spt;
}
void register_special(const char* value, const char* target)
{
int length = 0;
struct spetree* spt = find_spetree(value, &length);
strncpy(spt->value, value, MAX_SPECIAL_SIZE);
strncpy(spt->target, target, MAX_SPECIAL_SIZE);
fprintf(stderr, "Register at spt=%p\n", spt);
dump_spe(spt->value);
dump_spe(spt->target);
}
char* add_special(char* special, char* line)
{
int u;
int length = 0;
struct spetree *spt = find_spetree(special, &length);
// fprintf(stderr, "Add %s, spt=%p, spt->value=%s, spt->target=%s, length=%d\n", special, spt, spt->value, spt->target, length);
if ( spt->value[0] )
{
if ( spt->target[0] )
{
// fprintf(stderr, "Target %s line %s\n", spt->target, line);
char* s = special;
for ( const char* t = spt->target ; *t ; t++ )
{
*s++ = *t;
}
*s = 0;
return s;
}
return special + length;
}
strncpy(spt->value, special, MAX_SPECIAL_SIZE);
if ( current_line_in_spetree == NULL )
{
current_line_in_spetree = (char*) malloc ( MAX_LINE_SIZE );
}
strncpy(current_line_in_spetree, line, MAX_LINE_SIZE);
spt->line = current_line_in_spetree;
// fprintf(stderr, "At line '%s' :\n", line);
// dump_spe(special);
#ifdef _SPE_ARRAY
for ( u = 0 ; u < MAX_SPECIALS ; u++ )
{
if ( all_specials[u][0] == 0 )
break;
if ( strncmp(special, all_specials[u], MAX_SPECIAL_SIZE) == 0 )
return;
}
strncpy(all_specials[u], special, MAX_SPECIAL_SIZE);
#endif
return special + length;
}
void dump_spetree(struct spetree* spt)
{
if ( !spt->target[0] && spt->value[0] )
{
fprintf(stderr, "\nSpecial :");
dump_spe(spt->value);
fprintf(stderr, "%s\n", spt->line);
}
for ( int i = 0 ; i < 256 ; i++ )
{
if ( spt->next[i] )
{
dump_spetree(spt->next[i]);
}
}
}
void dump_all_specials()
{
fprintf(stderr, "All specials !\n");
dump_spetree(&root_spetree);
}
#ifdef _SPE_ARRAY
void dump_all_specials_array()
{
for ( int u = 0 ; u < MAX_SPECIALS ; u++ )
{
if ( all_specials[u][0] == 0 )
break;
// fprintf(stderr, "** %s\n", all_specials[u]);
dump_spe(all_specials[u]);
}
}
#endif
int main(int argc, char** argv)
{
#ifdef _SPE_ARRAY
for ( int u = 0 ; u < MAX_SPECIALS ; u++ )
{
all_specials[u][0] = 0;
}
#endif
memset(&root_spetree, 0, sizeof(struct spetree));
register_special("\x80", "\xc3\x87"); // é
register_special("\x81", "\xc3\xbc"); // é
register_special("\x82", "\xc3\xa9"); // é
register_special("\x83", "\xc3\xa2");
register_special("\x84", "\xc3\xa4");
register_special("\x85", "\xc3\xa0");
register_special("\x86", "\xc3\xa5");
register_special("\x87", "\xc3\xa7");
register_special("\x88", "\xc3\xa8");
register_special("\x8A", "\xc3\x80"); // è
register_special("\x8C", "\xc3\xae"); // è
register_special("\x8B", "\xc3\xaf");
register_special("\x91", "\xc2\xab"); // ï
register_special("\x92", "\xc2\xbb"); // ï
register_special("\x96", "\xc3\xbb"); // ï
register_special("\xc2\x82", "\xc3\xa9"); // é
register_special("\xc2\x8a", "\xc3\x80"); // è
register_special("\xc2\x8b", "\xc3\xbb"); // ï
register_special("\xb0", "\xc2\xb0"); // °
register_special("\xb4", "'"); // °
register_special("\xa4", "\xc3\xb1");
register_special("\xa8\xb0", "\xc3\xb3");
register_special("\xe7\xf5", "\xc3\xa7");
// Direct utf8 encodings
// register_special("\xe0", "\xc3\xe0"); // à
// register_special("\xe2", "\xc3\xa2"); // â
register_special("\xa1", "\xc2\xa1");
register_special("\xb7", "\xc2\xb6");
register_special("\xc0", "\xc3\x80");
register_special("\xc7", "\xc3\x87");
register_special("\xc9", "\xc3\x89");
register_special("\xca", "\xc3\x8a");
register_special("\xd3", "\xc3\xa0");
register_special("\xd9", "\xc3\x99");
register_special("\xde", "\xc3\xa8"); // Spurious ?
register_special("\xdf", "\xc3\x9f");
register_special("\xda", "\xc3\x9a");
register_special("\xe0", "\xc3\xa0");
register_special("\xe1", "\xc3\xa1");
register_special("\xe2", "\xc3\xa2");
register_special("\xe3", "\xc3\xa3");
register_special("\xe4", "\xc3\xa4");
register_special("\xe4\xe4", "\xc3\xa4\xc3\xa4");
register_special("\xe5", "\xc3\xa5");
register_special("\xe6", "\xc3\xa6");
register_special("\xe7", "\xc3\xa7");
register_special("\xe8", "\xc3\xa8");
register_special("\xe9", "\xc3\xa9");
register_special("\xea", "\xc3\xaa");
register_special("\xeb", "\xc3\xab");
register_special("\xec", "\xc3\xac");
register_special("\xed", "\xc3\xad");
register_special("\xee", "\xc3\xae");
register_special("\xef", "\xc3\xaf");
register_special("\xf0", "\xc3\xb0");
register_special("\xf1", "\xc3\xb1");
register_special("\xf2", "\xc3\xb2");
register_special("\xf3", "\xc3\xb3");
register_special("\xf4", "\xc3\xb4");
register_special("\xf5", "\xc3\xb5");
register_special("\xf6", "\xc3\xb6");
register_special("\xf7", "\xc3\xb7");
register_special("\xf8", "\xc3\xb8");
register_special("\xf9", "\xc3\xb9");
register_special("\xfa", "\xc3\xba");
register_special("\xfb", "\xc3\xbb");
register_special("\xfc", "\xc3\xbc");
register_special("\xfd", "\xc3\xbd");
register_special("\xfe", "\xc3\xbe");
register_special("\xff", "\xc3\xbf");
register_special("\xc2\xa6\xc3\x87", "\xc3\xa8");
register_special("\xc2\xa6\xc3\xbc", "\xc3\x80");
int in_spe = 0;
char line[MAX_LINE_SIZE];
char oline[MAX_LINE_SIZE];
char* l = line, *ol = oline, *special;
while ( 1 )
{
char c = getc(stdin);
if ( current_line_in_spetree && ( c == 255 || c== 0xa ) )
{
*l = 0;
strncpy(current_line_in_spetree, line, MAX_LINE_SIZE);
current_line_in_spetree = NULL;
}
if ( c == 255 )
{
if ( in_spe == 2 )
{
*l = 0;
l = add_special(special, line);
}
break;
}
if ( c < 0x80 && in_spe == 2 )
{
in_spe = 1;
*l = 0;
l = add_special(special, line);
}
*l++ = c;
*ol++ = c;
if ( c == 0xa )
{
*l++ = 0;
*ol++ = 0;
if ( in_spe )
{
if ( strncmp(oline, line, MAX_LINE_SIZE) )
{
// fprintf(stdout, "< %s> %s", oline, line);
}
else
{
fprintf(stdout, "| %s", line);
}
}
in_spe = 0;
l = line;
ol = oline;
continue;
}
// fprintf(stdout, "Got %x %c\n", c, c);
if ( c >= 0x80 )
{
// fprintf(stdout, "[%x] %c", c, c);
switch ( in_spe )
{
case 0:
in_spe = 2;
special = l - 1;
break;
case 1:
special = l - 1;
in_spe = 2;
break;
}
}
}
dump_all_specials();
}