Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/src/common/util-common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/util-common.c')
-rw-r--r--src/common/util-common.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/common/util-common.c b/src/common/util-common.c
index 7d43ac0d..fc892c11 100644
--- a/src/common/util-common.c
+++ b/src/common/util-common.c
@@ -25,6 +25,32 @@
#include "util-common.h"
+/** Create a string representing bytes in hexadecimal.
+ * @param bytes the bytes to represent in hexadecimal
+ * @param size number of bytes to consider
+ * @return a NULL terminated string with the hexadecimal representation of
+ * bytes or NULL on error. This string must be freed.
+ */
+char *hex_representation(const unsigned char *bytes, size_t size)
+{
+ static const char *hex_digits = "0123456789abcdef";
+ char *str = malloc(2 * size + 1);
+ size_t i;
+
+ if(!str) {
+ return NULL;
+ }
+
+ for(i = 0; i < size; i++) {
+ str[2 * i] = hex_digits[bytes[i] >> 4];
+ str[2 * i + 1] = hex_digits[bytes[i] & 0x0f];
+ }
+
+ str[2 * size] = '\0';
+
+ return str;
+}
+
/** Parse the basename of a program from a path.
* @param path path to parse basename from
*