json: add spdk_json_write_bytearray
This function serializes a buffer as a hex string. Signed-off-by: Konrad Sztyber <konrad.sztyber@intel.com> Change-Id: I09ab93bc626f6f6543b7c1ef033bcf807050862a Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/10651 Tested-by: SPDK CI Jenkins <sys_sgci@intel.com> Reviewed-by: Ben Walker <benjamin.walker@intel.com> Reviewed-by: Jim Harris <james.r.harris@intel.com> Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com> Community-CI: Mellanox Build Bot
This commit is contained in:
parent
2b65309b6c
commit
2c9895dee6
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user