forked from CoolerVoid/0d1n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_entities.c
65 lines (50 loc) · 1.16 KB
/
html_entities.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
#include "html_entities.h"
#include "mem_ops.h"
char *html_entities(const char *str)
{
char *result = NULL;
size_t n = 0, len = 0;
const char *p=str;
int i = 0;
while (*p != '\0')
{
const char *change;
char buf[32];
if (*p == '<')
change = "<";
else if (*p == '>')
change = ">";
else if (*p == '\\')
change = "\\\\";
else if (*p == '&')
change = "&";
else if (*p == '"')
change = """;
else if (*p == '\'')
change = "'";
else if (*p == '-' && p > str && *(p - 1) == '-') {
change = "-";
} else if (*p < 0x20 || (unsigned char) *p > 0x7F) {
snprintf(buf, sizeof(buf), "&#x%x;", (unsigned char) *p);
change = buf;
} else {
buf[0] = *p;
buf[1] = '\0';
change = buf;
}
len = strlen(change);
if ( !i || i + len > n)
{
n = (i + len) << 1;
result = xreallocarray(result, n + 1,sizeof(char));
}
memcpy(result + i, change, len);
i += len;
p++;
}
result = xreallocarray(result, i + 1,sizeof(char));
result[i] = '\0';
// if(result != NULL)
/// free(result);
return result;
}