-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathquote.c
67 lines (56 loc) · 1.54 KB
/
quote.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
#include "parsing.h"
ssize_t quote(const char *sp, ssize_t nb, char *dp, ssize_t *dn, uint64_t flags) {
ssize_t nd = *dn;
const char * ds = dp;
const char * ss = sp;
const quoted_t * tab;
/* select quoting table */
if (!(flags & F_DBLUNQ)) {
tab = _SingleQuoteTab;
} else {
tab = _DoubleQuoteTab;
}
if (*dn >= nb * MAX_ESCAPED_BYTES) {
*dn = memcchr_quote_unsafe(sp, nb, dp, tab);
return nb;
}
/* find the special characters, copy on the fly */
while (nb != 0) {
int nc;
uint8_t ch;
ssize_t rb = memcchr_quote(sp, nb, dp, nd);
/* not enough buffer space */
if (rb < 0) {
*dn = dp - ds - rb - 1;
return -(sp - ss - rb - 1) - 1;
}
/* skip already copied bytes */
sp += rb;
dp += rb;
nb -= rb;
nd -= rb;
/* get the escape entry, handle consecutive quotes */
while (nb != 0) {
ch = *(uint8_t *)sp;
nc = tab[ch].n;
/* check for escape character */
if (nc == 0) {
break;
}
/* check for buffer space */
if (nc > nd) {
*dn = dp - ds;
return -(sp - 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;
}