-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathhtml_escape.c
77 lines (63 loc) · 1.82 KB
/
html_escape.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
#include "parsing.h"
ssize_t html_escape(const char *sp, ssize_t nb, char *dp, ssize_t *dn) {
ssize_t nd = *dn;
const char * ds = dp;
const char * ss = sp;
const quoted_t * tab = _HtmlQuoteTab;
/* find the special characters, copy on the fly */
while (nb > 0) {
int nc = 0;
uint8_t ch = 0;
ssize_t rb = 0;
const char * cur = 0;
/* not enough buffer space */
if (nd <= 0) {
return -(sp - ss) - 1;
}
/* find and copy */
if ((rb = memcchr_html_quote(sp, nb, dp, nd)) < 0) {
*dn = dp - ds - rb - 1;
return -(sp - ss - rb - 1) - 1;
}
/* skip already copied bytes */
sp += rb;
dp += rb;
nb -= rb;
nd -= rb;
/* stop if already finished */
if (nb <= 0) {
break;
}
/* mark cur postion */
cur = sp;
/* check for \u2028 and \u2029, binary is \xe2\x80\xa8 and \xe2\x80\xa9 */
if (unlikely(*sp == '\xe2')) {
if (nb >= 3 && *(sp+1) == '\x80' && (*(sp+2) == '\xa8' || *(sp+2) == '\xa9')) {
sp += 2, nb -= 2;
} else if (nd > 0) {
*dp++ = *sp++;
nb--, nd--;
continue;
} else {
return -(sp - ss) - 1;
}
}
/* get the escape entry, handle consecutive quotes */
ch = * (uint8_t*) sp;
nc = tab[ch].n;
/* check for buffer space */
if (nd < nc) {
*dn = dp - ds;
return -(cur - ss) - 1;
}
/* copy the quoted value */
memcpy_p8(dp, tab[ch].s, nc);
sp++;
nb--;
dp += nc;
nd -= nc;
}
/* all done */
*dn = dp - ds;
return sp - ss;
}