1
0
mirror of https://github.com/irssi/irssi.git synced 2025-01-03 14:56:47 -05:00

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.
This commit is contained in:
Alexander Færøy 2016-10-16 13:20:14 +02:00
parent 6300dfec71
commit da67d3e8e6
No known key found for this signature in database
GPG Key ID: E15081D5D3C3DB53
2 changed files with 24 additions and 0 deletions

View File

@ -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;
}

View File

@ -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