comparison libpurple/util.c @ 27217: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 01f1929d0936
children a337d7a5cd0c
comparison
equal deleted inserted replaced
27216:f1385d462205 27217:3d92b968d49f
217 "0123456789abcdef"; 217 "0123456789abcdef";
218 218
219 gchar * 219 gchar *
220 purple_base64_encode(const guchar *data, gsize len) 220 purple_base64_encode(const guchar *data, gsize len)
221 { 221 {
222 #if GLIB_CHECK_VERSION(2,12,0)
223 return g_base64_encode(data, len);
224 #else
222 char *out, *rv; 225 char *out, *rv;
223 226
224 g_return_val_if_fail(data != NULL, NULL); 227 g_return_val_if_fail(data != NULL, NULL);
225 g_return_val_if_fail(len > 0, NULL); 228 g_return_val_if_fail(len > 0, NULL);
226 229
251 } 254 }
252 255
253 *out = '\0'; 256 *out = '\0';
254 257
255 return rv; 258 return rv;
259 #endif /* GLIB < 2.12.0 */
256 } 260 }
257 261
258 guchar * 262 guchar *
259 purple_base64_decode(const char *str, gsize *ret_len) 263 purple_base64_decode(const char *str, gsize *ret_len)
260 { 264 {
265 #if GLIB_CHECK_VERSION(2,12,0)
266 /*
267 * We want to allow ret_len to be NULL for backward compatibility,
268 * but g_base64_decode() requires a valid length variable. So if
269 * ret_len is NULL then pass in a dummy variable.
270 */
271 gsize unused;
272 return g_base64_decode(str, ret_len != NULL ? ret_len : &unused);
273 #else
261 guchar *out = NULL; 274 guchar *out = NULL;
262 char tmp = 0; 275 char tmp = 0;
263 const char *c; 276 const char *c;
264 gint32 tmp2 = 0; 277 gint32 tmp2 = 0;
265 int len = 0, n = 0; 278 int len = 0, n = 0;
317 330
318 if (ret_len != NULL) 331 if (ret_len != NULL)
319 *ret_len = len; 332 *ret_len = len;
320 333
321 return out; 334 return out;
335 #endif /* GLIB < 2.12.0 */
322 } 336 }
323 337
324 /************************************************************************** 338 /**************************************************************************
325 * Quoted Printable Functions (see RFC 2045). 339 * Quoted Printable Functions (see RFC 2045).
326 **************************************************************************/ 340 **************************************************************************/