serialize: add serialize_item_hexmem()
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user