changeset 27299:3d92b968d49f

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
author Mark Doliner <mark@kingant.net>
date Tue, 30 Jun 2009 01:39:08 +0000
parents f1385d462205
children a337d7a5cd0c
files libpurple/util.c
diffstat 1 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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 */
 }
 
 /**************************************************************************