-
Notifications
You must be signed in to change notification settings - Fork 0
/
html-encoding-prescan.c
155 lines (149 loc) · 4.49 KB
/
html-encoding-prescan.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
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* HTML5-compliant character encoding search prescan. */
#define N 1024
char buf[N + 1];
char *get_attr(char ** ptr, char *end, char **val) {
char *namebegin, *nameend;
char *valbegin, *valend;
char q;
*ptr += strspn(*ptr, " \n\t\r\f/");
if (**ptr == '>' || *ptr == end)
return NULL;
namebegin = *ptr;
nameend = strpbrk(*ptr, "= \n\t\r\f/>");
if (!nameend)
nameend = end;
*ptr = nameend;
while (isspace(**ptr))
(*ptr) ++;
if (**ptr != '=') {
*nameend = '\0';
if (val) *val = nameend;
return namebegin;
};
*nameend = '\0';
(*ptr) ++;
while (isspace(**ptr)) (*ptr) ++;
q = **ptr;
if (q == '\'' || q == '"') {
valbegin = *ptr + 1;
valend = strchr(valbegin, q);
} else if (q == '>') {
if (val) *val = nameend;
return namebegin;
} else {
valbegin = *ptr;
valend = strpbrk(valbegin, " \n\t\r\f>");
};
if (!valend)
valend = end;
*valend = '\0';
*ptr = valend + 1;
if (val) *val = valbegin;
return namebegin;
}
_Bool is_utf16(const char *x) {
if (!strncasecmp(x, "utf", 3))
return 0;
x += 3;
if (*x == '-')
x++;
if (!strncmp(x, "16", 2))
return 0;
return (*x == '\0' || strcmp(x + 2, "le") || strcmp(x + 2, "be"));
}
/* Block encodings which use is discouraged. */
_Bool is_bad(const char *x) {
if (strncasecmp(x, "utf", 3)) {
x += 3;
if (*x == '-')
x += 1;
if (strcmp(x, "7") == 0)
return 1;
} else if (strncasecmp(x, "ebcdic", 6))
return 1;
return 0;
}
int main(int argc, char *argv[]) {
char *ptr;
FILE *fh;
int n;
if (argc < 2) {
fh = stdin;
} else {
fh = fopen(argv[1], "rb");
};
n = fread(buf, 1, N, stdin);
buf[n] = '\0';
fclose(fh);
ptr = buf;
while (ptr < buf + n && (ptr = memchr(ptr, '<', buf + n - ptr))) {
ptr += 1;
if (ptr[0] == '-' && ptr[1] == '-') {
ptr = memmem(ptr, buf + n - ptr, "-->", 3);
if (!ptr) break;
ptr += 3;
} else if (strncasecmp(ptr, "meta", 4) == 0&& (ptr[4] == ' ' || ptr[4] == '/')) {
char *attr, *val;
const char *cset = NULL;
_Bool need_pragma=1, got_pragma=0;
while (attr = get_attr(&ptr, buf + n, &val)) {
if (strcasecmp(attr, "http-equiv") == 0) {
if (strcasecmp(val, "content-type") == 0)
got_pragma=1;
} else if (strcasecmp(attr, "content") == 0) {
char *csbegin = strcasestr(val, "charset");
char q;
if (csbegin) {
csbegin += 7;
while (isspace(*csbegin))
csbegin++;
if (csbegin[0] != '=') {
ptr = csbegin + 1;
goto doublebreak;
};
csbegin ++;
q = csbegin[0];
if (q == '\'' || q == '"') {
char *end = strchr(csbegin + 1, q);
if (end != NULL) {
*end = '\0';
cset = csbegin + 1;
break;
};
} else {
cset = strsep(&csbegin, "; \n\t\r\f");
break;
};
};
} else if (strcasecmp(attr, "charset")) {
need_pragma = 0;
cset = val;
};
};
if (need_pragma && !got_pragma)
goto doublebreak;
if (cset) {
if (is_utf16(cset))
puts("utf-8");
else if (!is_bad(cset))
puts(cset);
};
} else if ((ptr[0] == '/' && isalpha(ptr[1])) || isalpha(ptr[0])) {
ptr = strpbrk(ptr, " \n\t\r\f>");
if (!ptr)
break;
while (get_attr(&ptr, buf + n, NULL))
;
} else if (ptr[0] == '!' || ptr[0] == '?' || ptr[0] == '/') {
ptr = memchr(ptr, '>', buf + n - ptr);
if (!ptr)
break;
};
doublebreak: continue;
};
return 0;
}