forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_escaped.c
162 lines (148 loc) · 3.14 KB
/
json_escaped.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
157
158
159
160
161
162
#include <common/json_escaped.h>
#include <stdio.h>
struct json_escaped *json_escaped_string_(const tal_t *ctx,
const void *bytes, size_t len)
{
struct json_escaped *esc;
esc = (void *)tal_arr_label(ctx, char, len + 1,
TAL_LABEL(struct json_escaped, ""));
memcpy(esc->s, bytes, len);
esc->s[len] = '\0';
return esc;
}
struct json_escaped *json_to_escaped_string(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok)
{
if (tok->type != JSMN_STRING)
return NULL;
/* jsmn always gives us ~ well-formed strings. */
return json_escaped_string_(ctx, buffer + tok->start,
tok->end - tok->start);
}
bool json_escaped_eq(const struct json_escaped *a,
const struct json_escaped *b)
{
return streq(a->s, b->s);
}
static struct json_escaped *escape(const tal_t *ctx,
const char *str TAKES,
bool partial)
{
struct json_escaped *esc;
size_t i, n;
/* Worst case: all \uXXXX */
esc = (struct json_escaped *)tal_arr(ctx, char, strlen(str) * 6 + 1);
for (i = n = 0; str[i]; i++, n++) {
char escape = 0;
switch (str[i]) {
case '\n':
escape = 'n';
break;
case '\b':
escape = 'b';
break;
case '\f':
escape = 'f';
break;
case '\t':
escape = 't';
break;
case '\r':
escape = 'r';
break;
case '\\':
if (partial) {
/* Don't double-escape standard escapes. */
if (str[i+1] == 'n'
|| str[i+1] == 'b'
|| str[i+1] == 'f'
|| str[i+1] == 't'
|| str[i+1] == 'r'
|| str[i+1] == '/'
|| str[i+1] == '\\'
|| str[i+1] == '"') {
escape = str[i+1];
i++;
break;
}
if (str[i+1] == 'u'
&& cisxdigit(str[i+2])
&& cisxdigit(str[i+3])
&& cisxdigit(str[i+4])
&& cisxdigit(str[i+5])) {
memcpy(esc->s + n, str + i, 6);
n += 5;
i += 5;
continue;
}
} /* fall thru */
case '"':
escape = str[i];
break;
default:
if ((unsigned)str[i] < ' ' || str[i] == 127) {
snprintf(esc->s + n, 7, "\\u%04X", str[i]);
n += 5;
continue;
}
}
if (escape) {
esc->s[n++] = '\\';
esc->s[n] = escape;
} else
esc->s[n] = str[i];
}
esc->s[n] = '\0';
if (taken(str))
tal_free(str);
return esc;
}
struct json_escaped *json_partial_escape(const tal_t *ctx, const char *str TAKES)
{
return escape(ctx, str, true);
}
struct json_escaped *json_escape(const tal_t *ctx, const char *str TAKES)
{
return escape(ctx, str, false);
}
/* By policy, we don't handle \u. Use UTF-8. */
const char *json_escaped_unescape(const tal_t *ctx,
const struct json_escaped *esc)
{
char *unesc = tal_arr(ctx, char, strlen(esc->s) + 1);
size_t i, n;
for (i = n = 0; esc->s[i]; i++, n++) {
if (esc->s[i] != '\\') {
unesc[n] = esc->s[i];
continue;
}
i++;
switch (esc->s[i]) {
case 'n':
unesc[n] = '\n';
break;
case 'b':
unesc[n] = '\b';
break;
case 'f':
unesc[n] = '\f';
break;
case 't':
unesc[n] = '\t';
break;
case 'r':
unesc[n] = '\r';
break;
case '/':
case '\\':
case '"':
unesc[n] = esc->s[i];
break;
default:
return tal_free(unesc);
}
}
unesc[n] = '\0';
return unesc;
}