1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "cryptsetup-token-util.h"
4 #include "string-util.h"
5
crypt_dump_buffer_to_hex_string(const char * buf,size_t buf_size,char ** ret_dump_str)6 int crypt_dump_buffer_to_hex_string(
7 const char *buf,
8 size_t buf_size,
9 char **ret_dump_str) {
10
11 int r;
12 _cleanup_free_ char *dump_str = NULL;
13
14 assert(buf || !buf_size);
15 assert(ret_dump_str);
16
17 for (size_t i = 0; i < buf_size; i++) {
18 /* crypt_dump() breaks line after every
19 * 16th couple of chars in dumped hexstring */
20 r = strextendf_with_separator(
21 &dump_str,
22 (i && !(i % 16)) ? CRYPT_DUMP_LINE_SEP : " ",
23 "%02hhx", buf[i]);
24 if (r < 0)
25 return r;
26 }
27
28 *ret_dump_str = TAKE_PTR(dump_str);
29
30 return 0;
31 }
32
crypt_dump_hex_string(const char * hex_str,char ** ret_dump_str)33 int crypt_dump_hex_string(const char *hex_str, char **ret_dump_str) {
34
35 int r;
36 size_t len;
37 _cleanup_free_ char *dump_str = NULL;
38
39 assert(hex_str);
40 assert(ret_dump_str);
41
42 len = strlen(hex_str) >> 1;
43
44 for (size_t i = 0; i < len; i++) {
45 /* crypt_dump() breaks line after every
46 * 16th couple of chars in dumped hexstring */
47 r = strextendf_with_separator(
48 &dump_str,
49 (i && !(i % 16)) ? CRYPT_DUMP_LINE_SEP : " ",
50 "%.2s", hex_str + (i<<1));
51 if (r < 0)
52 return r;
53 }
54
55 *ret_dump_str = TAKE_PTR(dump_str);
56
57 return 0;
58 }
59