# HG changeset patch # User William Ehlhardt # Date 1180126351 0 # Node ID ba768014f91f9495614b219ef2286f898792f0d9 # Parent fe8a1051aa0aceb3f076dc9292b73d3b73013fac - Add purple_base16_encode_chunked, which is handy for key fingerprints. diff -r fe8a1051aa0a -r ba768014f91f libpurple/util.c --- a/libpurple/util.c Fri May 25 19:05:47 2007 +0000 +++ b/libpurple/util.c Fri May 25 20:52:31 2007 +0000 @@ -155,6 +155,31 @@ return data; } +gchar * +purple_base16_encode_chunked(const guchar *data, gsize len) +{ + int i; + gchar *ascii = NULL; + + g_return_val_if_fail(data != NULL, NULL); + g_return_val_if_fail(len > 0, NULL); + + /* For each byte of input, we need 2 bytes for the hex representation + * and 1 for the colon. + * The final colon will be replaced by a terminating NULL + */ + ascii = g_malloc(len * 3 + 1); + + for (i = 0; i < len; i++) + g_snprintf(&ascii[i * 3], 4, "%02hhx:", data[i]); + + /* Replace the final colon with NULL */ + ascii[len * 3 - 1] = 0; + + return ascii; +} + + /************************************************************************** * Base64 Functions **************************************************************************/ diff -r fe8a1051aa0a -r ba768014f91f libpurple/util.h --- a/libpurple/util.h Fri May 25 19:05:47 2007 +0000 +++ b/libpurple/util.h Fri May 25 20:52:31 2007 +0000 @@ -118,6 +118,21 @@ */ guchar *purple_base16_decode(const char *str, gsize *ret_len); +/** + * Converts a chunk of binary data to a chunked base-16 representation + * (handy for key fingerprints) + * + * Example output: 01:23:45:67:89:AB:CD:EF + * + * @param data The data to convert. + * @param len The length of the data. + * + * @return The base-16 string in the ASCII chunked encoding. Must be + * g_free'd when no longer needed. + */ +gchar *purple_base16_encode_chunked(const guchar *data, gsize len); + + /*@}*/ /**************************************************************************/