mirror of
https://github.com/irssi/irssi.git
synced 2025-04-26 13:01:11 -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:
parent
6300dfec71
commit
da67d3e8e6
@ -930,3 +930,23 @@ char **strsplit_len(const char *str, int len, gboolean onspace)
|
|||||||
|
|
||||||
return ret;
|
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;
|
||||||
|
}
|
||||||
|
@ -109,4 +109,8 @@ int find_substr(const char *list, const char *item);
|
|||||||
/* split `str' into `len' sized substrings */
|
/* split `str' into `len' sized substrings */
|
||||||
char **strsplit_len(const char *str, int len, gboolean onspace);
|
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
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user