diff options
author | Alexander Færøy <ahf@0x90.dk> | 2016-10-16 13:20:14 +0200 |
---|---|---|
committer | Alexander Færøy <ahf@0x90.dk> | 2016-10-22 20:36:50 +0200 |
commit | da67d3e8e69eb5fb702a3dd39356d38a1ee9d8cd (patch) | |
tree | b3faa43fd92fd001e09ce5b5007330e7bf669dae /src | |
parent | 6300dfec71d376c96351708f76a6c4ee4a187eb5 (diff) | |
download | irssi-da67d3e8e69eb5fb702a3dd39356d38a1ee9d8cd.zip |
Add function to convert a buffer to a colon-delimited hex string.
This patch adds binary_to_hex(), which can take an input buffer and
convert it to colon-delimited hex strings suitable for printing for
fingerprints.
Diffstat (limited to 'src')
-rw-r--r-- | src/core/misc.c | 20 | ||||
-rw-r--r-- | src/core/misc.h | 4 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/core/misc.c b/src/core/misc.c index bc9f504e..0bb1f7e6 100644 --- a/src/core/misc.c +++ b/src/core/misc.c @@ -930,3 +930,23 @@ char **strsplit_len(const char *str, int len, gboolean onspace) return ret; } + +char *binary_to_hex(unsigned char *buffer, size_t size) +{ + static const char hex[] = "0123456789ABCDEF"; + char *result = NULL; + int i; + + if (buffer == NULL || size == 0) + return NULL; + + result = g_malloc(3 * size); + + for (i = 0; i < size; i++) { + result[i * 3 + 0] = hex[(buffer[i] >> 4) & 0xf]; + result[i * 3 + 1] = hex[(buffer[i] >> 0) & 0xf]; + result[i * 3 + 2] = i == size - 1 ? '\0' : ':'; + } + + return result; +} diff --git a/src/core/misc.h b/src/core/misc.h index c095e131..00637da0 100644 --- a/src/core/misc.h +++ b/src/core/misc.h @@ -109,4 +109,8 @@ int find_substr(const char *list, const char *item); /* split `str' into `len' sized substrings */ char **strsplit_len(const char *str, int len, gboolean onspace); +/* Convert a given buffer to a printable, colon-delimited, hex string and + * return a pointer to the newly allocated buffer */ +char *binary_to_hex(unsigned char *buffer, size_t size); + #endif |