-
Notifications
You must be signed in to change notification settings - Fork 2
/
confuse.c
156 lines (138 loc) · 4.61 KB
/
confuse.c
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
/*
* libcharset client utility which, given two Unicode code points,
* will search for character sets which encode the two code points the
* same way. The idea is that if you see some piece of misencoded text
* which uses (say) an oe ligature where you expected (as it might be)
* a pound sign, you can use this utility to suggest which two
* character sets might have been confused with each other to cause
* that effect.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include "charset.h"
#define MAXENCLEN 20
int main(int argc, char **argv)
{
wchar_t *chars;
struct enc { char string[MAXENCLEN]; int len; } *encodings;
int nchars;
int i, j, k, cs;
const char *sep;
setlocale(LC_ALL, "");
chars = malloc(argc * sizeof(wchar_t));
if (!chars) {
fprintf(stderr, "out of memory\n");
return 1;
}
nchars = 0;
while (--argc) {
char *p = *++argv;
char *orig = p;
char *end;
int base = 16, semi_ok = 0;
wchar_t ch;
if ((p[0] == 'U' || p[0] == 'u') &&
(p[1] == '-' || p[1] == '+')) {
p += 2;
} else if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
p += 2;
} else if (p[0] == '&' && p[1] == '#') {
p += 2;
if (p[0] == 'x' || p[0] == 'X')
p++;
else
base = 10;
semi_ok = 1;
} else if (mbtowc(&ch, p, strlen(p)) == strlen(p)) {
chars[nchars++] = ch;
continue;
}
chars[nchars++] = strtoul(p, &end, base);
if (!*end || (semi_ok && !strcmp(end, ";")))
continue;
else {
fprintf(stderr, "unable to parse '%s' as a Unicode code point\n",
orig);
return 1;
}
}
encodings = malloc(nchars * CS_LIMIT * sizeof(struct enc));
for (cs = 0; cs < CS_LIMIT; cs++) {
for (i = 0; i < nchars; i++) {
wchar_t inbuf[1];
const wchar_t *inptr;
int inlen, error, ret;
if (!charset_exists(cs)) {
encodings[i*CS_LIMIT+cs].len = 0;
continue;
}
inbuf[0] = chars[i];
inptr = inbuf;
inlen = 1;
error = 0;
ret = charset_from_unicode(&inptr, &inlen,
encodings[i*CS_LIMIT+cs].string,
MAXENCLEN, cs, NULL, &error);
if (error || inlen > 0)
encodings[i*CS_LIMIT+cs].len = 0;
else
encodings[i*CS_LIMIT+cs].len = ret;
}
}
/*
* Really simple and slow approach to finding each distinct string
* and outputting it.
*/
for (i = 0; i < nchars*CS_LIMIT; i++) {
const char *thisstr = encodings[i].string;
int thislen = encodings[i].len;
if (thislen == 0)
continue;
for (j = 0; j < i; j++)
if (encodings[j].len == thislen &&
!memcmp(encodings[j].string, thisstr, thislen))
break;
if (j < i)
continue; /* not the first instance of this encoding */
/*
* See if every character is encoded like this somewhere.
*/
for (j = 0; j < nchars; j++) {
for (cs = 0; cs < CS_LIMIT; cs++) {
if (encodings[j*CS_LIMIT+cs].len == thislen &&
!memcmp(encodings[j*CS_LIMIT+cs].string, thisstr, thislen))
break;
}
if (cs == CS_LIMIT)
break; /* this char not in any cs */
}
if (j < nchars)
continue; /* some char not in any cs */
/*
* Match! Print the encoding, then all charsets.
*/
for (j = 0; j < nchars; j++) {
for (k = 0; k < thislen; k++)
printf("%s%02X", k>0?" ":"", (unsigned)(thisstr[k] & 0xFF));
printf(" = ");
if (chars[j] >= 0x10000)
printf("U-%08X", (unsigned)chars[j]);
else
printf("U+%04X", (unsigned)chars[j]);
printf(" in:");
sep = " ";
for (cs = 0; cs < CS_LIMIT; cs++)
if (encodings[j*CS_LIMIT+cs].len == thislen &&
!memcmp(encodings[j*CS_LIMIT+cs].string, thisstr, thislen))
{
printf("%s%s", sep, charset_to_localenc(cs));
sep = ", ";
}
printf("\n");
}
printf("\n");
}
return 0;
}