]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: add frrstr_hex to hexdump buffers
authorQuentin Young <qlyoung@nvidia.com>
Tue, 10 Aug 2021 15:42:19 +0000 (11:42 -0400)
committerQuentin Young <qlyoung@nvidia.com>
Thu, 12 Aug 2021 20:25:57 +0000 (16:25 -0400)
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
lib/frrstr.c
lib/frrstr.h

index 7ef5fffd12011398771ca505989b3c9172e32407..1b98b224cc9e329d0fad8adbec15d2662feac4dc 100644 (file)
@@ -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 <string.h>
 #include <ctype.h>
@@ -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;
+}
index 441d7b86703c0839b27d1301830988c8c1e83454..d52d6a4482293440a758ea12f3abb5deb29342e8 100644 (file)
@@ -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