pidgin: 22e14265: Use glib's base64 encode and decode func...

markdoliner at pidgin.im markdoliner at pidgin.im
Mon Jun 29 21:40:28 EDT 2009


-----------------------------------------------------------------
Revision: 22e14265a47cdddb4c9eb1ee0d2ce2989f9fce61
Ancestor: 419f272346afcd280fb35c4e9f789521c1391087
Author: markdoliner at pidgin.im
Date: 2009-06-30T01:39:08
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/22e14265a47cdddb4c9eb1ee0d2ce2989f9fce61

Modified files:
        libpurple/util.c

ChangeLog: 

Use glib's base64 encode and decode functions if they're available.
Our purple_base64_decode() implementation is horrendously
ineffecient, by the way.  It allocates memory inside a while loop while
decoding.  It should allocate memory ahead of time (the glib version
does this).

The glib version is here if anyone wants to steal it:
http://git.gnome.org./cgit/glib/tree/glib/gbase64.c

-------------- next part --------------
============================================================
--- libpurple/util.c	e1d2253a3c331f4132f4ea55c9466b6882c650a3
+++ libpurple/util.c	ac73360688fae8239a297e449c02255f994750c6
@@ -219,6 +219,9 @@ purple_base64_encode(const guchar *data,
 gchar *
 purple_base64_encode(const guchar *data, gsize len)
 {
+#if GLIB_CHECK_VERSION(2,12,0)
+	return g_base64_encode(data, len);
+#else
 	char *out, *rv;
 
 	g_return_val_if_fail(data != NULL, NULL);
@@ -253,11 +256,21 @@ purple_base64_encode(const guchar *data,
 	*out = '\0';
 
 	return rv;
+#endif /* GLIB < 2.12.0 */
 }
 
 guchar *
 purple_base64_decode(const char *str, gsize *ret_len)
 {
+#if GLIB_CHECK_VERSION(2,12,0)
+	/*
+	 * We want to allow ret_len to be NULL for backward compatibility,
+	 * but g_base64_decode() requires a valid length variable.  So if
+	 * ret_len is NULL then pass in a dummy variable.
+	 */
+	gsize unused;
+	return g_base64_decode(str, ret_len != NULL ? ret_len : &unused);
+#else
 	guchar *out = NULL;
 	char tmp = 0;
 	const char *c;
@@ -319,6 +332,7 @@ purple_base64_decode(const char *str, gs
 		*ret_len = len;
 
 	return out;
+#endif /* GLIB < 2.12.0 */
 }
 
 /**************************************************************************


More information about the Commits mailing list