From 2c9895dee66af2560371322e4778afa64f4ec2d7 Mon Sep 17 00:00:00 2001 From: Konrad Sztyber Date: Sun, 12 Dec 2021 11:00:31 +0100 Subject: [PATCH] json: add spdk_json_write_bytearray This function serializes a buffer as a hex string. Signed-off-by: Konrad Sztyber Change-Id: I09ab93bc626f6f6543b7c1ef033bcf807050862a Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/10651 Tested-by: SPDK CI Jenkins Reviewed-by: Ben Walker Reviewed-by: Jim Harris Reviewed-by: Aleksey Marchuk Community-CI: Mellanox Build Bot --- include/spdk/json.h | 3 +++ lib/json/Makefile | 2 +- lib/json/json_write.c | 49 ++++++++++++++++++++++++++++++++++++------ lib/json/spdk_json.map | 2 ++ 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/include/spdk/json.h b/include/spdk/json.h index 23ddd09440..41b01e2d58 100644 --- a/include/spdk/json.h +++ b/include/spdk/json.h @@ -218,6 +218,7 @@ int spdk_json_write_uint64(struct spdk_json_write_ctx *w, uint64_t val); int spdk_json_write_uint128(struct spdk_json_write_ctx *w, uint64_t low_val, uint64_t high_val); int spdk_json_write_string(struct spdk_json_write_ctx *w, const char *val); int spdk_json_write_string_raw(struct spdk_json_write_ctx *w, const char *val, size_t len); +int spdk_json_write_bytearray(struct spdk_json_write_ctx *w, const void *val, size_t len); /** * Write null-terminated UTF-16LE string. @@ -275,6 +276,8 @@ int spdk_json_write_named_string_fmt(struct spdk_json_write_ctx *w, const char * const char *fmt, ...) __attribute__((__format__(__printf__, 3, 4))); int spdk_json_write_named_string_fmt_v(struct spdk_json_write_ctx *w, const char *name, const char *fmt, va_list args); +int spdk_json_write_named_bytearray(struct spdk_json_write_ctx *w, const char *name, + const void *val, size_t len); int spdk_json_write_named_array_begin(struct spdk_json_write_ctx *w, const char *name); int spdk_json_write_named_object_begin(struct spdk_json_write_ctx *w, const char *name); diff --git a/lib/json/Makefile b/lib/json/Makefile index 2eeabd4a60..086fe98741 100644 --- a/lib/json/Makefile +++ b/lib/json/Makefile @@ -35,7 +35,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk SO_VER := 3 -SO_MINOR := 2 +SO_MINOR := 3 C_SRCS = json_parse.c json_util.c json_write.c LIBNAME = json diff --git a/lib/json/json_write.c b/lib/json/json_write.c index eb0a604594..c24ed237da 100644 --- a/lib/json/json_write.c +++ b/lib/json/json_write.c @@ -338,15 +338,20 @@ spdk_json_write_named_uint128(struct spdk_json_write_ctx *w, const char *name, } static void -write_hex_4(void *dest, uint16_t val) +write_hex_2(void *dest, uint8_t val) { - uint8_t *p = dest; + char *p = dest; char hex[] = "0123456789ABCDEF"; - p[0] = hex[(val >> 12)]; - p[1] = hex[(val >> 8) & 0xF]; - p[2] = hex[(val >> 4) & 0xF]; - p[3] = hex[val & 0xF]; + p[0] = hex[val >> 4]; + p[1] = hex[val & 0xf]; +} + +static void +write_hex_4(void *dest, uint16_t val) +{ + write_hex_2(dest, (uint8_t)(val >> 8)); + write_hex_2((char *)dest + 2, (uint8_t)(val & 0xff)); } static inline int @@ -529,6 +534,29 @@ spdk_json_write_string_fmt_v(struct spdk_json_write_ctx *w, const char *fmt, va_ return rc; } +int +spdk_json_write_bytearray(struct spdk_json_write_ctx *w, const void *val, size_t len) +{ + const uint8_t *v = val; + size_t i; + char *s; + int rc; + + s = malloc(2 * len + 1); + if (s == NULL) { + return -1; + } + + for (i = 0; i < len; ++i) { + write_hex_2(&s[2 * i], *v++); + } + s[2 * len] = '\0'; + + rc = spdk_json_write_string(w, s); + free(s); + return rc; +} + int spdk_json_write_array_begin(struct spdk_json_write_ctx *w) { @@ -758,6 +786,15 @@ int spdk_json_write_named_string_fmt_v(struct spdk_json_write_ctx *w, const char return rc; } +int +spdk_json_write_named_bytearray(struct spdk_json_write_ctx *w, const char *name, const void *val, + size_t len) +{ + int rc = spdk_json_write_name(w, name); + + return rc ? rc : spdk_json_write_bytearray(w, val, len); +} + int spdk_json_write_named_array_begin(struct spdk_json_write_ctx *w, const char *name) { int rc = spdk_json_write_name(w, name); diff --git a/lib/json/spdk_json.map b/lib/json/spdk_json.map index 5be4d147ce..19510122fd 100644 --- a/lib/json/spdk_json.map +++ b/lib/json/spdk_json.map @@ -43,6 +43,7 @@ spdk_json_write_string_utf16le_raw; spdk_json_write_string_fmt; spdk_json_write_string_fmt_v; + spdk_json_write_bytearray; spdk_json_write_array_begin; spdk_json_write_array_end; spdk_json_write_object_begin; @@ -64,6 +65,7 @@ spdk_json_write_named_string; spdk_json_write_named_string_fmt; spdk_json_write_named_string_fmt_v; + spdk_json_write_named_bytearray; spdk_json_write_named_array_begin; spdk_json_write_named_object_begin;