From: Quentin Young Date: Tue, 10 Aug 2021 15:42:19 +0000 (-0400) Subject: lib: add frrstr_hex to hexdump buffers X-Git-Tag: base_8.1~205^2~1 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=0c0830c599d61bcbb45a6a7ef6b6dea5b77f957b;p=matthieu%2Ffrr.git lib: add frrstr_hex to hexdump buffers Signed-off-by: Quentin Young --- diff --git a/lib/frrstr.c b/lib/frrstr.c index 7ef5fffd12..1b98b224cc 100644 --- a/lib/frrstr.c +++ b/lib/frrstr.c @@ -18,9 +18,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +#include "zebra.h" #include #include @@ -217,3 +215,21 @@ int all_digit(const char *str) return 0; return 1; } + + +char *frrstr_hex(char *buff, size_t bufsiz, const uint8_t *str, size_t num) +{ + if (bufsiz == 0) + return buff; + + char tmp[3]; + + buff[0] = '\0'; + + for (size_t i = 0; i < num; i++) { + snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]); + strlcat(buff, tmp, bufsiz); + } + + return buff; +} diff --git a/lib/frrstr.h b/lib/frrstr.h index 441d7b8670..d52d6a4482 100644 --- a/lib/frrstr.h +++ b/lib/frrstr.h @@ -154,6 +154,26 @@ bool frrstr_endswith(const char *str, const char *suffix); */ int all_digit(const char *str); +/* + * Copy the hexadecimal representation of the string to a buffer. + * + * buff + * Buffer to copy result into with size of at least (2 * num) + 1. + * + * bufsiz + * Size of destination buffer. + * + * str + * String to represent as hexadecimal. + * + * num + * Number of characters to copy. + * + * Returns: + * Pointer to buffer containing resulting hexadecimal representation. + */ +char *frrstr_hex(char *buff, size_t bufsiz, const uint8_t *str, size_t num); + #ifdef __cplusplus } #endif