From: Luca Boccassi Date: Thu, 16 Oct 2025 15:21:57 +0000 (+0100) Subject: json: add json_dispatch_unhex_iovec helper X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=26bf1b9e85f7121258a24cf69302fe5727855f40;p=thirdparty%2Fsystemd.git json: add json_dispatch_unhex_iovec helper --- diff --git a/src/libsystemd/sd-json/json-util.c b/src/libsystemd/sd-json/json-util.c index b5d687c644c..556d4c786bc 100644 --- a/src/libsystemd/sd-json/json-util.c +++ b/src/libsystemd/sd-json/json-util.c @@ -25,6 +25,24 @@ #include "unit-name.h" #include "user-util.h" +int json_dispatch_unhex_iovec(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + _cleanup_free_ void *buffer = NULL; + struct iovec *iov = ASSERT_PTR(userdata); + size_t sz; + int r; + + if (!sd_json_variant_is_string(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); + + r = sd_json_variant_unhex(variant, &buffer, &sz); + if (r < 0) + return json_log(variant, flags, r, "JSON field '%s' is not valid hex data.", strna(name)); + + free_and_replace(iov->iov_base, buffer); + iov->iov_len = sz; + return 0; +} + int json_dispatch_unbase64_iovec(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { _cleanup_free_ void *buffer = NULL; struct iovec *iov = ASSERT_PTR(userdata); diff --git a/src/libsystemd/sd-json/json-util.h b/src/libsystemd/sd-json/json-util.h index abacb298c6d..28d19a2db24 100644 --- a/src/libsystemd/sd-json/json-util.h +++ b/src/libsystemd/sd-json/json-util.h @@ -108,6 +108,7 @@ int json_log_internal(sd_json_variant *variant, int level, int error, const char #define json_log_oom(variant, flags) \ json_log(variant, flags, SYNTHETIC_ERRNO(ENOMEM), "Out of memory.") +int json_dispatch_unhex_iovec(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); int json_dispatch_unbase64_iovec(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); int json_dispatch_byte_array_iovec(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); int json_dispatch_user_group_name(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata);