diff src/util.c @ 6872:dd0eecfbe413

[gaim-migrate @ 7418] ok, these are some tweaks i've made to core code working on the new jabber plugin. - add gaim_find_buddy_in_group() that searches a specific group instead of the entire list. kinda handy. - re-did the base64 encoding function. i think it may have been broken, i'm not sure, but this i know works. - fix the formatted notify dialog to be more to my liking, and to have a working Close button. committer: Tailor Script <tailor@pidgin.im>
author Nathan Walp <nwalp@pidgin.im>
date Wed, 17 Sep 2003 03:45:04 +0000
parents 4ae5d9c3d9ec
children 083d1e4a9c78
line wrap: on
line diff
--- a/src/util.c	Wed Sep 17 03:22:44 2003 +0000
+++ b/src/util.c	Wed Sep 17 03:45:04 2003 +0000
@@ -319,47 +319,33 @@
 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 	"0123456789+/";
 
-/* This was borrowed from the Kame source, and then tweaked to our needs */
-char *tobase64(const unsigned char *buf, size_t len)
+char *tobase64(const unsigned char *in, size_t inlen)
 {
-	char *s = NULL, *rv = NULL;
-	unsigned long tmp;
-
-	s = g_malloc((4 * (len + 1)) / 3 + 1);
+	char *out, *rv;
 
-	rv = s;
-	while (len >= 3) {
-		tmp = buf[0] << 16 | buf[1] << 8 | buf[2];
-		s[0] = alphabet[tmp >> 18];
-		s[1] = alphabet[(tmp >> 12) & 077];
-		s[2] = alphabet[(tmp >> 6) & 077];
-		s[3] = alphabet[tmp & 077];
-		len -= 3;
-		buf += 3;
-		s += 4;
-	}
+	rv = out = g_malloc((4 * (inlen + 1)) / 3 + 1);
 
-	/* RFC 1521 enumerates these three possibilities... */
-	switch(len) {
-		case 2:
-			tmp = buf[0] << 16 | buf[1] << 8;
-			s[0] = alphabet[(tmp >> 18) & 077];
-			s[1] = alphabet[(tmp >> 12) & 077];
-			s[2] = alphabet[(tmp >> 6) & 077];
-			s[3] = '=';
-			s[4] = '\0';
-			break;
-		case 1:
-			tmp = buf[0] << 16;
-			s[0] = alphabet[(tmp >> 18) & 077];
-			s[1] = alphabet[(tmp >> 12) & 077];
-			s[2] = s[3] = '=';
-			s[4] = '\0';
-			break;
-		case 0:
-			s[0] = '\0';
-			break;
-	}
+    for (; inlen >= 3; inlen -= 3)
+        {
+            *out++ = alphabet[in[0] >> 2];
+            *out++ = alphabet[((in[0] << 4) & 0x30) | (in[1] >> 4)];
+            *out++ = alphabet[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
+            *out++ = alphabet[in[2] & 0x3f];
+            in += 3;
+        }
+    if (inlen > 0)
+        {
+            unsigned char fragment;
+
+            *out++ = alphabet[in[0] >> 2];
+            fragment = (in[0] << 4) & 0x30;
+            if (inlen > 1)
+                fragment |= in[1] >> 4;
+            *out++ = alphabet[fragment];
+            *out++ = (inlen < 2) ? '=' : alphabet[(in[1] << 2) & 0x3c];
+            *out++ = '=';
+        }
+    *out = '\0';
 
 	return rv;
 }