Skip to content

Commit

Permalink
MT#55283 add (un)escape parser methods
Browse files Browse the repository at this point in the history
Change-Id: I7d9be892c4b5a3e28b77bc4a42e4c0cee722a132
  • Loading branch information
rfuchs committed Aug 29, 2024
1 parent 348e4e2 commit 98d6575
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
13 changes: 13 additions & 0 deletions daemon/control_ng.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,15 @@ static str json_collapse(ng_parser_ctx_t *ctx, JsonNode *a, void **to_free) {
static void json_ctx_init(ng_parser_ctx_t *ctx, bencode_buffer_t *buf) {
*ctx = (ng_parser_ctx_t) { .parser = &ng_parser_json };
}
static str dummy_encode_len(char *out, const char *in, size_t in_len) {
return STR_LEN(in, in_len);
}
static str *dummy_decode_len(const char *in, size_t len) {
str *r = str_alloc(len);
memcpy(r->s, in, len);
r->s[len] = '\0';
return r;
}

const ng_parser_t ng_parser_native = {
.init = __bencode_ctx_init,
Expand Down Expand Up @@ -474,6 +483,8 @@ const ng_parser_t ng_parser_native = {
.list_add_string = bencode_list_add_string,
.list_add_str_dup = bencode_list_add_str_dup,
.pretty_print = bencode_pretty_print,
.escape = dummy_encode_len,
.unescape = dummy_decode_len,
};
const ng_parser_t ng_parser_json = {
.init = json_ctx_init,
Expand Down Expand Up @@ -508,6 +519,8 @@ const ng_parser_t ng_parser_json = {
.list_add_string = json_list_add_string,
.list_add_str_dup = json_list_add_str,
.pretty_print = json_pretty_print,
.escape = str_uri_encode_len,
.unescape = str_uri_decode_len,
};


Expand Down
14 changes: 7 additions & 7 deletions daemon/redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ static void json_get_hash_iter(const ng_parser_t *parser, str *key, parser_arg v
char *tmp = __g_memdup(key->s, key->len + 1);
tmp[key->len] = '\0';
// XXX eliminate string dup? eliminate URI decode?
if (g_hash_table_insert(arg.ht, tmp, str_uri_decode_len(val.s, val.len)) != TRUE)
if (g_hash_table_insert(arg.ht, tmp, parser->unescape(val.s, val.len)) != TRUE)
rlog(LOG_WARNING,"Key %s already exists", tmp);
}

Expand Down Expand Up @@ -1191,7 +1191,7 @@ struct cb_iter_ptrs { // XXX remove this?

static void json_build_list_cb_iter(str *val, unsigned int i, helper_arg arg) {
struct cb_iter_ptrs *args = arg.generic;
str *s = str_uri_decode_len(val->s, val->len);
str *s = redis_parser->unescape(val->s, val->len);
args->cb(s, args->cb_arg, args->list, args->ptr);
g_free(s);
}
Expand Down Expand Up @@ -2310,32 +2310,32 @@ int redis_restore(struct redis *r, bool foreign, int db) {
#define JSON_ADD_LIST_STRING(f,...) do { \
int len = snprintf(tmp,sizeof(tmp), f, __VA_ARGS__); \
char enc[len * 3 + 1]; \
str encstr = str_uri_encode_len(enc, tmp, len); \
str encstr = parser->escape(enc, tmp, len); \
parser->list_add_str_dup(inner, &encstr); \
} while (0)
#define JSON_SET_NSTRING(a,b,c,...) do { \
int len = snprintf(tmp,sizeof(tmp), c, __VA_ARGS__); \
char enc[len * 3 + 1]; \
str encstr = str_uri_encode_len(enc, tmp, len); \
str encstr = parser->escape(enc, tmp, len); \
snprintf(tmp,sizeof(tmp), a,b); \
parser->dict_add_str_dup(inner, tmp, &encstr); \
} while (0)
#define JSON_SET_NSTRING_CSTR(a,b,d) JSON_SET_NSTRING_LEN(a, b, strlen(d), d)
#define JSON_SET_NSTRING_LEN(a,b,l,d) do { \
char enc[l * 3 + 1]; \
str encstr = str_uri_encode_len(enc, d, l); \
str encstr = parser->escape(enc, d, l); \
snprintf(tmp,sizeof(tmp), a,b); \
parser->dict_add_str_dup(inner, tmp, &encstr); \
} while (0)
#define JSON_SET_SIMPLE(a,c,...) do { \
int len = snprintf(tmp,sizeof(tmp), c, __VA_ARGS__); \
char enc[len * 3 + 1]; \
str encstr = str_uri_encode_len(enc, tmp, len); \
str encstr = parser->escape(enc, tmp, len); \
parser->dict_add_str_dup(inner, a, &encstr); \
} while (0)
#define JSON_SET_SIMPLE_LEN(a,l,d) do { \
char enc[l * 3 + 1]; \
str encstr = str_uri_encode_len(enc, d, l); \
str encstr = parser->escape(enc, d, l); \
parser->dict_add_str_dup(inner, a, &encstr); \
} while (0)
#define JSON_SET_SIMPLE_CSTR(a,d) parser->dict_add_str_dup(inner, a, &STR(d))
Expand Down
2 changes: 2 additions & 0 deletions include/control_ng.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ struct ng_parser {
void (*list_add_str_dup)(parser_arg, const str *);
void (*list_add_string)(parser_arg , const char *);
void (*pretty_print)(parser_arg, GString *);
str (*escape)(char *, const char *, size_t);
str *(*unescape)(const char *, size_t);
};
struct ng_parser_ctx {
const ng_parser_t *parser;
Expand Down

0 comments on commit 98d6575

Please sign in to comment.