Use glib's base64 encode and decode functions if they're available.

Tue, 30 Jun 2009 01:39:08 +0000

author
Mark Doliner <markdoliner@pidgin.im>
date
Tue, 30 Jun 2009 01:39:08 +0000
changeset 27459
c4832605b830
parent 27458
d48df8e69f08
child 27465
b3a1227fb54b

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

libpurple/util.c file | annotate | diff | comparison | revisions
--- a/libpurple/util.c	Mon Jun 29 13:40:18 2009 +0000
+++ b/libpurple/util.c	Tue Jun 30 01:39:08 2009 +0000
@@ -219,6 +219,9 @@
 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 @@
 	*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 @@
 		*ret_len = len;
 
 	return out;
+#endif /* GLIB < 2.12.0 */
 }
 
 /**************************************************************************

mercurial