Skip to content

Commit 4031860

Browse files
whitslackrustyrussell
authored andcommitted
ccan: update to get json_escape_unescape_len()
See: rustyrussell/ccan#123
1 parent 733bdfa commit 4031860

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

ccan/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
CCAN imported from http://ccodearchive.net.
22

3-
CCAN version: init-2593-gca094039
3+
CCAN version: init-2595-ge43c61c4

ccan/ccan/json_escape/json_escape.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* MIT (BSD) license - see LICENSE file for details */
22
#include <ccan/json_escape/json_escape.h>
33
#include <stdio.h>
4+
#include <ccan/tal/str/str.h>
45

56
struct json_escape *json_escape_string_(const tal_t *ctx,
67
const void *bytes, size_t len)
@@ -137,19 +138,24 @@ struct json_escape *json_escape_len(const tal_t *ctx, const char *str TAKES,
137138
}
138139

139140
/* By policy, we don't handle \u. Use UTF-8. */
140-
const char *json_escape_unescape(const tal_t *ctx, const struct json_escape *esc)
141+
static const char *unescape(const tal_t *ctx, const char *esc TAKES, size_t len)
141142
{
142-
char *unesc = tal_arr(ctx, char, strlen(esc->s) + 1);
143+
/* Fast path: can steal, and nothing to unescape. */
144+
if (is_taken(esc) && !memchr(esc, '\\', len))
145+
return tal_strndup(ctx, esc, len);
146+
147+
char *unesc = tal_arr(ctx, char, len + 1);
143148
size_t i, n;
144149

145-
for (i = n = 0; esc->s[i]; i++, n++) {
146-
if (esc->s[i] != '\\') {
147-
unesc[n] = esc->s[i];
150+
for (i = n = 0; i < len; i++, n++) {
151+
if (esc[i] != '\\') {
152+
unesc[n] = esc[i];
148153
continue;
149154
}
150155

151-
i++;
152-
switch (esc->s[i]) {
156+
if (++i == len)
157+
goto error;
158+
switch (esc[i]) {
153159
case 'n':
154160
unesc[n] = '\n';
155161
break;
@@ -168,13 +174,31 @@ const char *json_escape_unescape(const tal_t *ctx, const struct json_escape *esc
168174
case '/':
169175
case '\\':
170176
case '"':
171-
unesc[n] = esc->s[i];
177+
unesc[n] = esc[i];
172178
break;
173179
default:
180+
error:
181+
if (taken(esc))
182+
tal_free(esc);
174183
return tal_free(unesc);
175184
}
176185
}
177186

178187
unesc[n] = '\0';
188+
if (!tal_resize(&unesc, n + 1))
189+
goto error;
190+
if (taken(esc))
191+
tal_free(esc);
179192
return unesc;
180193
}
194+
195+
const char *json_escape_unescape(const tal_t *ctx, const struct json_escape *esc)
196+
{
197+
return unescape(ctx, esc->s, strlen(esc->s));
198+
}
199+
200+
const char *json_escape_unescape_len(const tal_t *ctx,
201+
const char *esc TAKES, size_t len)
202+
{
203+
return unescape(ctx, esc, len);
204+
}

ccan/ccan/json_escape/json_escape.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ struct json_escape *json_escape_string_(const tal_t *ctx,
4141
/* Be very careful here! Can fail! Doesn't handle \u: use UTF-8 please. */
4242
const char *json_escape_unescape(const tal_t *ctx,
4343
const struct json_escape *esc);
44+
45+
/* Be very careful here! Can fail! Doesn't handle \u: use UTF-8 please. */
46+
const char *json_escape_unescape_len(const tal_t *ctx,
47+
const char *esc TAKES, size_t len);
4448
#endif /* CCAN_JSON_ESCAPE_H */

ccan/tools/configurator/configurator.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ It will exit with non\-zero status if it has a problem\&. \fB1\fR means bad comm
208208
Rusty Russell wrote \fBconfigurator\fR\&.
209209
.SH "RESOURCES"
210210
.sp
211-
Main web site: http://ccodearchive\&.net/
211+
Main web site: https://github\&.com/rustyrussell/ccan
212212
.sp
213213
Wiki: https://github\&.com/rustyrussell/ccan/wiki/
214214
.SH "COPYING"

0 commit comments

Comments
 (0)