2 * Make a hexquoted copy of a string
4 * Copyright (c) 2016-2018 Dmitry V. Levin <ldv@altlinux.org>
7 * SPDX-License-Identifier: GPL-2.0-or-later
17 hexquote_strndup(const char *src, const size_t src_len)
19 const size_t dst_size = 4 * src_len + 1;
20 assert(dst_size > src_len);
22 char *dst = malloc(dst_size);
24 perror_msg_and_fail("malloc(%zu)", dst_size);
28 for (i = 0; i < src_len; ++i) {
29 unsigned int c = ((const unsigned char *) src)[i];
32 *(p++) = "0123456789abcdef"[c >> 4];
33 *(p++) = "0123456789abcdef"[c & 0xf];