Debian Codegraph @

src/cryptsetup/cryptsetup-tokens/cryptsetup-token-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "cryptsetup-token-util.h"
5 #include "string-util.h"
6
7 int crypt_dump_buffer_to_hex_string(
8 const char *buf,
9 size_t buf_size,
10 char **ret_dump_str) {
11
12 int r;
13 _cleanup_free_ char *dump_str = NULL;
14
15 assert(buf || !buf_size);
16 assert(ret_dump_str);
17
18 for (size_t i = 0; i < buf_size; i++) {
19 /* crypt_dump() breaks line after every
20 * 16th couple of chars in dumped hexstring */
21 r = strextendf_with_separator(
22 &dump_str,
23 (i && !(i % 16)) ? CRYPT_DUMP_LINE_SEP : " ",
24 "%02hhx", buf[i]);
25 if (r < 0)
26 return r;
27 }
28
29 *ret_dump_str = TAKE_PTR(dump_str);
30
31 return 0;
32 }
33
34 int crypt_normalize_pin(const void *pin, size_t pin_size, char **ret_pin_string) {
35 assert(pin || pin_size == 0);
36 assert(ret_pin_string);
37
38 if (pin_size == 0) {
39 *ret_pin_string = NULL;
40 return 0;
41 }
42
43 return make_cstring(pin, pin_size, MAKE_CSTRING_ALLOW_TRAILING_NUL, ret_pin_string);
44 }