index : pacman | |
Archlinux32 fork of pacman | gitolite user |
summaryrefslogtreecommitdiff |
author | Emil Velikov <emil.l.velikov@gmail.com> | 2020-12-23 22:43:57 +0000 |
---|---|---|
committer | Allan McRae <allan@archlinux.org> | 2020-12-29 12:38:23 +1000 |
commit | ccdd1e3fd92591755e2b94bf63416c7b30cd217a (patch) | |
tree | 5dd2bab62c6ca7299777e6916ade3f56653e3db8 /src/common | |
parent | 831fc568fc87a75bb6e05575b93a7541b49e7aba (diff) |
-rw-r--r-- | src/common/util-common.c | 26 | ||||
-rw-r--r-- | src/common/util-common.h | 1 |
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 * diff --git a/src/common/util-common.h b/src/common/util-common.h index 483d5da4..d9b6b4b7 100644 --- a/src/common/util-common.h +++ b/src/common/util-common.h @@ -23,6 +23,7 @@ #include <stdio.h> #include <sys/stat.h> /* struct stat */ +char *hex_representation(const unsigned char *bytes, size_t size); const char *mbasename(const char *path); char *mdirname(const char *path); |