1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

json-util: Add JSON_BUILD_PAIR_BASE64_NON_EMPTY() and friends

This commit is contained in:
Daan De Meyer 2024-08-13 13:04:06 +02:00
parent a85e9670f6
commit 6647bbeab1
2 changed files with 44 additions and 0 deletions

View File

@ -160,6 +160,10 @@ enum {
_JSON_BUILD_PAIR_DUAL_TIMESTAMP_NON_NULL,
_JSON_BUILD_PAIR_RATELIMIT_ENABLED,
_JSON_BUILD_PAIR_CALLBACK_NON_NULL,
_JSON_BUILD_PAIR_BASE64_NON_EMPTY,
_JSON_BUILD_PAIR_BASE32HEX_NON_EMPTY,
_JSON_BUILD_PAIR_HEX_NON_EMPTY,
_JSON_BUILD_PAIR_OCTESCAPE_NON_EMPTY,
_SD_JSON_BUILD_REALLYMAX,
};
@ -195,6 +199,10 @@ enum {
#define JSON_BUILD_PAIR_DUAL_TIMESTAMP_NON_NULL(name, t) _JSON_BUILD_PAIR_DUAL_TIMESTAMP_NON_NULL, (const char*) { name }, (dual_timestamp*) { t }
#define JSON_BUILD_PAIR_RATELIMIT_ENABLED(name, rl) _JSON_BUILD_PAIR_RATELIMIT_ENABLED, (const char*) { name }, (const RateLimit*) { rl }
#define JSON_BUILD_PAIR_CALLBACK_NON_NULL(name, c, u) _JSON_BUILD_PAIR_CALLBACK_NON_NULL, (const char*) { name }, (sd_json_build_callback_t) { c }, (void*) { u }
#define JSON_BUILD_PAIR_BASE64_NON_EMPTY(name, v, n) _JSON_BUILD_PAIR_BASE64_NON_EMPTY, (const char*) { name }, (const void*) { v }, (size_t) { n }
#define JSON_BUILD_PAIR_BASE32HEX_NON_EMPTY(name, v, n) _JSON_BUILD_PAIR_BASE32HEX_NON_EMPTY, (const char*) { name }, (const void*) { v }, (size_t) { n }
#define JSON_BUILD_PAIR_HEX_NON_EMPTY(name, v, n) _JSON_BUILD_PAIR_HEX_NON_EMPTY, (const char*) { name }, (const void*) { v }, (size_t) { n }
#define JSON_BUILD_PAIR_OCTESCAPE_NON_EMPTY(name, v, n) _JSON_BUILD_PAIR_HEX_NON_EMPTY, (const char*) { name }, (const void*) { v }, (size_t) { n }
#define JSON_BUILD_PAIR_IOVEC_BASE64(name, iov) SD_JSON_BUILD_PAIR(name, JSON_BUILD_IOVEC_BASE64(iov))
#define JSON_BUILD_PAIR_IOVEC_HEX(name, iov) SD_JSON_BUILD_PAIR(name, JSON_BUILD_IOVEC_HEX(iov))

View File

@ -4769,6 +4769,42 @@ _public_ int sd_json_buildv(sd_json_variant **ret, va_list ap) {
current->expect = EXPECT_OBJECT_KEY;
break;
}
case _JSON_BUILD_PAIR_BASE64_NON_EMPTY:
case _JSON_BUILD_PAIR_BASE32HEX_NON_EMPTY:
case _JSON_BUILD_PAIR_HEX_NON_EMPTY:
case _JSON_BUILD_PAIR_OCTESCAPE_NON_EMPTY: {
const void *p;
size_t sz;
const char *n;
if (current->expect != EXPECT_OBJECT_KEY) {
r = -EINVAL;
goto finish;
}
n = va_arg(ap, const char*);
p = va_arg(ap, const void *);
sz = va_arg(ap, size_t);
if (sz > 0 && current->n_suppress == 0) {
r = sd_json_variant_new_string(&add, n);
if (r < 0)
goto finish;
r = command == _SD_JSON_BUILD_BASE64 ? sd_json_variant_new_base64(&add_more, p, sz) :
command == _SD_JSON_BUILD_BASE32HEX ? sd_json_variant_new_base32hex(&add_more, p, sz) :
command == _SD_JSON_BUILD_HEX ? sd_json_variant_new_hex(&add_more, p, sz) :
sd_json_variant_new_octescape(&add_more, p, sz);
if (r < 0)
goto finish;
}
n_subtract = 2; /* we generated two item */
current->expect = EXPECT_OBJECT_KEY;
break;
}
}
/* If variants were generated, add them to our current variant, but only if we are not supposed to suppress additions */