diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/serialize.c | 23 | ||||
-rw-r--r-- | src/shared/serialize.h | 1 | ||||
-rw-r--r-- | src/test/test-serialize.c | 18 |
3 files changed, 42 insertions, 0 deletions
diff --git a/src/shared/serialize.c b/src/shared/serialize.c index 2ffc524f15..dc282e398e 100644 --- a/src/shared/serialize.c +++ b/src/shared/serialize.c @@ -7,6 +7,7 @@ #include "escape.h" #include "fd-util.h" #include "fileio.h" +#include "hexdecoct.h" #include "memfd-util.h" #include "missing_mman.h" #include "missing_syscall.h" @@ -173,6 +174,28 @@ int serialize_pidref(FILE *f, FDSet *fds, const char *key, PidRef *pidref) { return serialize_item_format(f, key, "@%i", copy); } +int serialize_item_hexmem(FILE *f, const char *key, const void *p, size_t l) { + _cleanup_free_ char *encoded = NULL; + int r; + + assert(f); + assert(key); + assert(p || l == 0); + + if (l == 0) + return 0; + + encoded = hexmem(p, l); + if (!encoded) + return log_oom_debug(); + + r = serialize_item(f, key, encoded); + if (r < 0) + return r; + + return 1; +} + int deserialize_read_line(FILE *f, char **ret) { _cleanup_free_ char *line = NULL; int r; diff --git a/src/shared/serialize.h b/src/shared/serialize.h index 9463d1127c..17bc968c04 100644 --- a/src/shared/serialize.h +++ b/src/shared/serialize.h @@ -12,6 +12,7 @@ int serialize_item(FILE *f, const char *key, const char *value); int serialize_item_escaped(FILE *f, const char *key, const char *value); int serialize_item_format(FILE *f, const char *key, const char *value, ...) _printf_(3,4); +int serialize_item_hexmem(FILE *f, const char *key, const void *p, size_t l); int serialize_fd(FILE *f, FDSet *fds, const char *key, int fd); int serialize_usec(FILE *f, const char *key, usec_t usec); int serialize_dual_timestamp(FILE *f, const char *key, const dual_timestamp *t); diff --git a/src/test/test-serialize.c b/src/test/test-serialize.c index 36052b6485..501f5c6f2e 100644 --- a/src/test/test-serialize.c +++ b/src/test/test-serialize.c @@ -190,6 +190,24 @@ TEST(serialize_environment) { assert_se(strv_equal(env, env2)); } +TEST(serialize_item_hexmem) { + _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-serialize.XXXXXX"; + _cleanup_fclose_ FILE *f = NULL; + + assert_se(fmkostemp_safe(fn, "r+", &f) == 0); + log_info("/* %s (%s) */", __func__, fn); + + assert_se(serialize_item_hexmem(f, "a", NULL, 0) == 0); + assert_se(serialize_item_hexmem(f, "a", (uint8_t []){0xff, 0xff, 0xff}, sizeof(uint8_t) * 3) == 1); + + rewind(f); + + _cleanup_free_ char *line = NULL; + assert_se(read_line(f, LONG_LINE_MAX, &line) > 0); + assert_se(streq(line, "a=ffffff")); + +} + static int intro(void) { memset(long_string, 'x', sizeof(long_string)-1); char_array_0(long_string); |