changeset 11127:719779387f96

[gaim-migrate @ 13183] Change the base16 and base64 functions to use better data types, and make appropriate changes to other parts of the Gaim code to get rid of a few warnings and hopefully make things more correct. In other news, why is CVS HEAD crashing for me on exit? committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Tue, 19 Jul 2005 05:15:45 +0000
parents 2a3568cbd8a6
children 83dab64c5606
files src/mime.c src/protocols/jabber/auth.c src/protocols/jabber/buddy.c src/protocols/jabber/presence.c src/protocols/msn/slp.c src/protocols/trepia/trepia.c src/protocols/yahoo/yahoo.c src/util.c src/util.h
diffstat 9 files changed, 126 insertions(+), 108 deletions(-) [+]
line wrap: on
line diff
--- a/src/mime.c	Tue Jul 19 03:51:54 2005 +0000
+++ b/src/mime.c	Tue Jul 19 05:15:45 2005 +0000
@@ -328,10 +328,10 @@
     *len = part->data->len;
 
   } else if(! g_ascii_strcasecmp(enc, "base16")) {
-    *len = gaim_base16_decode(part->data->str, (unsigned char **) data);
+    *data = gaim_base16_decode(part->data->str, len);
 
   } else if(! g_ascii_strcasecmp(enc, "base64")) {
-    gaim_base64_decode(part->data->str, data, len);
+	*data = gaim_base64_decode(part->data->str, len);
 
   } else if(! g_ascii_strcasecmp(enc, "quoted-printable")) {
     gaim_quotedp_decode(part->data->str, data, len);
--- a/src/protocols/jabber/auth.c	Tue Jul 19 03:51:54 2005 +0000
+++ b/src/protocols/jabber/auth.c	Tue Jul 19 05:15:45 2005 +0000
@@ -64,7 +64,7 @@
 	if(js->auth_type == JABBER_AUTH_PLAIN) {
 		xmlnode *auth;
 		GString *response;
-		unsigned char *enc_out;
+		gchar *enc_out;
 
 		auth = xmlnode_new("auth");
 		xmlnode_set_attrib(auth, "xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
@@ -76,7 +76,7 @@
 		response = g_string_append(response,
 				gaim_connection_get_password(js->gc));
 
-		enc_out = gaim_base64_encode(response->str, response->len);
+		enc_out = gaim_base64_encode((guint8 *)response->str, response->len);
 
 		xmlnode_set_attrib(auth, "mechanism", "PLAIN");
 		xmlnode_insert_data(auth, enc_out, -1);
@@ -307,7 +307,8 @@
 	guint8 result[16];
 	size_t a1len;
 
-	unsigned char *x, *a1, *ha1, *ha2, *kd, *z, *convnode, *convpasswd;
+	unsigned char *x, *a1, *kd, *convnode, *convpasswd;
+	gchar *ha1, *ha2, *z;
 
 	if((convnode = g_convert(jid->node, strlen(jid->node), "iso-8859-1", "utf-8",
 					NULL, NULL, NULL)) == NULL) {
@@ -376,7 +377,7 @@
 			return;
 		}
 
-		gaim_base64_decode(enc_in, &dec_in, NULL);
+		dec_in = (char *)gaim_base64_decode(enc_in, NULL);
 		gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded challenge (%d): %s\n",
 				strlen(dec_in), dec_in);
 
@@ -444,7 +445,7 @@
 			g_free(auth_resp);
 			g_free(cnonce);
 
-			enc_out = gaim_base64_encode(response->str, response->len);
+			enc_out = gaim_base64_encode((guint8 *)response->str, response->len);
 
 			gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded response (%d): %s\n", response->len, response->str);
 
--- a/src/protocols/jabber/buddy.c	Tue Jul 19 03:51:54 2005 +0000
+++ b/src/protocols/jabber/buddy.c	Tue Jul 19 05:15:45 2005 +0000
@@ -385,7 +385,7 @@
 
 			if(avatar_file && g_file_get_contents(avatar_file, (gchar **)&avatar_data, &avatar_len, &error)) {
 				xmlnode *photo, *binval;
-				unsigned char *enc;
+				gchar *enc;
 				int i;
 				unsigned char hashval[20];
 				char *p, hash[41];
@@ -809,12 +809,14 @@
 				xmlnode *binval;
 				if((binval = xmlnode_get_child(child, "BINVAL")) &&
 						(bintext = xmlnode_get_data(binval))) {
-					int size, i;
+					gsize size;
+					guint8 *data;
+					int i;
 					unsigned char hashval[20];
-					char *data, *p, hash[41];
+					char *p, hash[41];
 					gboolean photo = (strcmp(child->name, "PHOTO") == 0);
 
-					gaim_base64_decode(text, &data, &size);
+					data = gaim_base64_decode(text, &size);
 
 					imgids = g_slist_prepend(imgids, GINT_TO_POINTER(gaim_imgstore_add(data, size, "logo.png")));
 					g_string_append_printf(info_text,
--- a/src/protocols/jabber/presence.c	Tue Jul 19 03:51:54 2005 +0000
+++ b/src/protocols/jabber/presence.c	Tue Jul 19 05:15:45 2005 +0000
@@ -206,8 +206,9 @@
 	JabberBuddy *jb = NULL;
 	GaimBuddy *b = NULL;
 	xmlnode *vcard, *photo, *binval;
-	char *text, *data;
-	int size;
+	char *text;
+	guint8 *data;
+	gsize size;
 	const char *from = xmlnode_get_attrib(packet, "from");
 
 	if(!from)
@@ -222,7 +223,7 @@
 		if((photo = xmlnode_get_child(vcard, "PHOTO")) &&
 				(binval = xmlnode_get_child(photo, "BINVAL")) &&
 				(text = xmlnode_get_data(binval))) {
-			gaim_base64_decode(text, &data, &size);
+			data = gaim_base64_decode(text, &size);
 
 			gaim_buddy_icons_set_for_user(js->gc->account, from, data, size);
 			if((b = gaim_find_buddy(js->gc->account, from))) {
--- a/src/protocols/msn/slp.c	Tue Jul 19 03:51:54 2005 +0000
+++ b/src/protocols/msn/slp.c	Tue Jul 19 05:15:45 2005 +0000
@@ -253,7 +253,7 @@
 		const char *sha1c;
 		const char *file_name;
 		char *content;
-		int len;
+		gsize len;
 		int type;
 
 		/* Send Ok */
@@ -267,7 +267,7 @@
 
 		slplink = slpcall->slplink;
 
-		gaim_base64_decode(context, &msnobj_data, &len);
+		msnobj_data = (char *)gaim_base64_decode(context, &len);
 		obj = msn_object_new_from_string(msnobj_data);
 		type = msn_object_get_type(obj);
 		sha1c = msn_object_get_sha1c(obj);
@@ -322,7 +322,7 @@
 		GaimAccount *account;
 		GaimXfer *xfer;
 		char *bin;
-		int bin_len;
+		gsize bin_len;
 		guint32 file_size;
 		char *file_name;
 		gunichar2 *uni_name;
@@ -339,7 +339,7 @@
 		xfer = gaim_xfer_new(account, GAIM_XFER_RECEIVE,
 							 slpcall->slplink->remote_user);
 
-		gaim_base64_decode(context, &bin, &bin_len);
+		bin = (char *)gaim_base64_decode(context, &bin_len);
 		file_size = GUINT32_FROM_LE(*((gsize *)bin + 2));
 
 		uni_name = (gunichar2 *)(bin + 20);
--- a/src/protocols/trepia/trepia.c	Tue Jul 19 03:51:54 2005 +0000
+++ b/src/protocols/trepia/trepia.c	Tue Jul 19 05:15:45 2005 +0000
@@ -851,7 +851,7 @@
 					char *icon;
 					int icon_len;
 
-					gaim_base64_decode(value, &icon, &icon_len);
+					icon = gaim_base64_decode(value, &icon_len);
 
 					gaim_buddy_icons_set_for_user(account,
 							username, icon, icon_len);
--- a/src/protocols/yahoo/yahoo.c	Tue Jul 19 03:51:54 2005 +0000
+++ b/src/protocols/yahoo/yahoo.c	Tue Jul 19 05:15:45 2005 +0000
@@ -307,11 +307,12 @@
 			break;
 		case 197: /* Avatars */
 		{
-			char *decoded, *tmp;
-			guint len;
+			guint8 *decoded;
+			char *tmp;
+			gsize len;
 
 			if (pair->value) {
-				gaim_base64_decode(pair->value, &decoded, &len);
+				decoded = gaim_base64_decode(pair->value, &len);
 				if (len) {
 					tmp = gaim_str_binary_to_ascii(decoded, len);
 					gaim_debug_info("yahoo", "Got key 197, value = %s\n", tmp);
@@ -1860,8 +1861,8 @@
 	GSList *l = pkt->hash;
 	char *who = NULL;
 	char *base64 = NULL;
-	char *decoded;
-	int len;
+	guint8 *decoded;
+	gsize len;
 
 	while (l) {
 		struct yahoo_pair *pair = l->data;
@@ -1901,7 +1902,7 @@
 		char *tmp2;
 		YahooFriend *f;
 
-		gaim_base64_decode(base64, &decoded, &len);
+		decoded = gaim_base64_decode(base64, &len);
 		if (len) {
 			char *tmp = gaim_str_binary_to_ascii(decoded, len);
 			gaim_debug_info("yahoo", "Got P2P service packet (from server): who = %s, ip = %s\n", who, tmp);
--- a/src/util.c	Tue Jul 19 03:51:54 2005 +0000
+++ b/src/util.c	Tue Jul 19 05:15:45 2005 +0000
@@ -67,34 +67,34 @@
 /**************************************************************************
  * Base16 Functions
  **************************************************************************/
-unsigned char *
-gaim_base16_encode(const unsigned char *data, int length)
+gchar *
+gaim_base16_encode(const guint8 *data, gsize len)
 {
 	int i;
-	unsigned char *ascii = NULL;
+	gchar *ascii = NULL;
 
 	g_return_val_if_fail(data != NULL, NULL);
-	g_return_val_if_fail(length > 0,   NULL);
-
-	ascii = g_malloc(length * 2 + 1);
-
-	for (i = 0; i < length; i++)
+	g_return_val_if_fail(len > 0,   NULL);
+
+	ascii = g_malloc(len * 2 + 1);
+
+	for (i = 0; i < len; i++)
 		snprintf(&ascii[i * 2], 3, "%02hhx", data[i]);
 
 	return ascii;
 }
 
-int
-gaim_base16_decode(const char *ascii, unsigned char **raw)
+guint8 *
+gaim_base16_decode(const char *str, gsize *ret_len)
 {
 	int len, i, accumulator = 0;
-	unsigned char *data;
-
-	g_return_val_if_fail(ascii != NULL, 0);
-
-	len = strlen(ascii);
-
-	g_return_val_if_fail(strlen(ascii) > 0, 0);
+	guint8 *data;
+
+	g_return_val_if_fail(str != NULL, NULL);
+
+	len = strlen(str);
+
+	g_return_val_if_fail(strlen(str) > 0, 0);
 	g_return_val_if_fail(len % 2 > 0,       0);
 
 	data = g_malloc(len / 2);
@@ -106,11 +106,11 @@
 		else
 			accumulator <<= 4;
 
-		if (isdigit(ascii[i]))
-			accumulator |= ascii[i] - 48;
+		if (isdigit(str[i]))
+			accumulator |= str[i] - 48;
 		else
 		{
-			switch(ascii[i])
+			switch(str[i])
 			{
 				case 'a':  case 'A':  accumulator |= 10;  break;
 				case 'b':  case 'B':  accumulator |= 11;  break;
@@ -125,9 +125,10 @@
 			data[(i - 1) / 2] = accumulator;
 	}
 
-	*raw = data;
-
-	return (len / 2);
+	if (ret_len != NULL)
+		*ret_len = len / 2;
+
+	return data;
 }
 
 /**************************************************************************
@@ -140,37 +141,37 @@
 static const char xdigits[] =
 	"0123456789abcdef";
 
-unsigned char *
-gaim_base64_encode(const unsigned char *in, size_t inlen)
+gchar *
+gaim_base64_encode(const guint8 *data, gsize len)
 {
 	char *out, *rv;
 
-	g_return_val_if_fail(in != NULL, NULL);
-	g_return_val_if_fail(inlen > 0,  NULL);
-
-	rv = out = g_malloc(((inlen/3)+1)*4 + 1);
-
-	for (; inlen >= 3; inlen -= 3)
+	g_return_val_if_fail(data != NULL, NULL);
+	g_return_val_if_fail(len > 0,  NULL);
+
+	rv = out = g_malloc(((len/3)+1)*4 + 1);
+
+	for (; len >= 3; len -= 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;
+		*out++ = alphabet[data[0] >> 2];
+		*out++ = alphabet[((data[0] << 4) & 0x30) | (data[1] >> 4)];
+		*out++ = alphabet[((data[1] << 2) & 0x3c) | (data[2] >> 6)];
+		*out++ = alphabet[data[2] & 0x3f];
+		data += 3;
 	}
 
-	if (inlen > 0)
+	if (len > 0)
 	{
 		unsigned char fragment;
 
-		*out++ = alphabet[in[0] >> 2];
-		fragment = (in[0] << 4) & 0x30;
-
-		if (inlen > 1)
-			fragment |= in[1] >> 4;
+		*out++ = alphabet[data[0] >> 2];
+		fragment = (data[0] << 4) & 0x30;
+
+		if (len > 1)
+			fragment |= data[1] >> 4;
 
 		*out++ = alphabet[fragment];
-		*out++ = (inlen < 2) ? '=' : alphabet[(in[1] << 2) & 0x3c];
+		*out++ = (len < 2) ? '=' : alphabet[(data[1] << 2) & 0x3c];
 		*out++ = '=';
 	}
 
@@ -179,19 +180,18 @@
 	return rv;
 }
 
-void
-gaim_base64_decode(const char *text, char **data, int *size)
+guint8 *
+gaim_base64_decode(const char *str, gsize *ret_len)
 {
-	char *out = NULL;
+	guint8 *out = NULL;
 	char tmp = 0;
 	const char *c;
 	gint32 tmp2 = 0;
 	int len = 0, n = 0;
 
-	g_return_if_fail(text != NULL);
-	g_return_if_fail(data != NULL);
-
-	c = text;
+	g_return_val_if_fail(str != NULL, NULL);
+
+	c = str;
 
 	while (*c) {
 		if (*c >= 'A' && *c <= 'Z') {
@@ -210,13 +210,13 @@
 		} else if (*c == '=') {
 			if (n == 3) {
 				out = g_realloc(out, len + 2);
-				out[len] = (char)(tmp2 >> 10) & 0xff;
+				out[len] = (guint8)(tmp2 >> 10) & 0xff;
 				len++;
-				out[len] = (char)(tmp2 >> 2) & 0xff;
+				out[len] = (guint8)(tmp2 >> 2) & 0xff;
 				len++;
 			} else if (n == 2) {
 				out = g_realloc(out, len + 1);
-				out[len] = (char)(tmp2 >> 4) & 0xff;
+				out[len] = (guint8)(tmp2 >> 4) & 0xff;
 				len++;
 			}
 			break;
@@ -225,11 +225,11 @@
 		n++;
 		if (n == 4) {
 			out = g_realloc(out, len + 3);
-			out[len] = (char)((tmp2 >> 16) & 0xff);
+			out[len] = (guint8)((tmp2 >> 16) & 0xff);
 			len++;
-			out[len] = (char)((tmp2 >> 8) & 0xff);
+			out[len] = (guint8)((tmp2 >> 8) & 0xff);
 			len++;
-			out[len] = (char)(tmp2 & 0xff);
+			out[len] = (guint8)(tmp2 & 0xff);
 			len++;
 			tmp2 = 0;
 			n = 0;
@@ -240,10 +240,10 @@
 	out = g_realloc(out, len + 1);
 	out[len] = 0;
 
-	*data = out;
-
-	if (size)
-		*size = len;
+	if (ret_len != NULL)
+		*ret_len = len;
+
+	return out;
 }
 
 /**************************************************************************
@@ -410,12 +410,12 @@
 				char *charset = g_strndup(charset0, encoding0 - charset0 - 1);
 				char *encoding = g_strndup(encoding0, encoded_text0 - encoding0 - 1);
 				char *encoded_text = g_strndup(encoded_text0, cur - encoded_text0 - 1);
-				char *decoded = NULL;
-				int dec_len;
+				guint8 *decoded = NULL;
+				gsize dec_len;
 				if (g_ascii_strcasecmp(encoding, "Q") == 0)
 					gaim_quotedp_decode(encoded_text, &decoded, &dec_len);
 				else if (g_ascii_strcasecmp(encoding, "B") == 0)
-					gaim_base64_decode(encoded_text, &decoded, &dec_len);
+					decoded = gaim_base64_decode(encoded_text, &dec_len);
 				else
 					decoded = NULL;
 				if (decoded) {
--- a/src/util.h	Tue Jul 19 03:51:54 2005 +0000
+++ b/src/util.h	Tue Jul 19 05:15:45 2005 +0000
@@ -43,28 +43,34 @@
 /*@{*/
 
 /**
- * Converts a string to its base-16 equivalent.
+ * Converts a chunk of binary data to its base-16 equivalent.
  *
- * @param str The string to convert.
- * @param len The length of the string.
+ * @param data The data to convert.
+ * @param len  The length of the data.
  *
- * @return The base-16 string.
+ * @return The base-16 string in the ASCII encoding.  Must be
+ *         g_free'd when no longer needed.
  *
  * @see gaim_base16_decode()
  */
-unsigned char *gaim_base16_encode(const unsigned char *str, int len);
+gchar *gaim_base16_encode(const guint8 *data, gsize len);
 
 /**
- * Converts a string back from its base-16 equivalent.
+ * Converts an ASCII string of base-16 encoded data to
+ * the binary equivalent.
  *
- * @param str     The string to convert back.
- * @param ret_str The returned, non-base-16 string.
+ * @param str     The base-16 string to convert to raw data.
+ * @param ret_len The length of the returned data.  You can
+ *                pass in NULL if you're sure that you know
+ *                the length of the decoded data, or if you
+ *                know you'll be able to use strlen to
+ *                determine the length, etc.
  *
- * @return The length of the returned string.
+ * @return The raw data.  Must be g_free'd when no longer needed.
  *
  * @see gaim_base16_encode()
  */
-int gaim_base16_decode(const char *str, unsigned char **ret_str);
+guint8 *gaim_base16_decode(const char *str, gsize *ret_len);
 
 /*@}*/
 
@@ -75,27 +81,34 @@
 /*@{*/
 
 /**
- * Converts a string to its base-64 equivalent.
+ * Converts a chunk of binary data to its base-64 equivalent.
  *
- * @param buf The data to convert.
- * @param len The length of the data.
+ * @param data The data to convert.
+ * @param len  The length of the data.
  *
- * @return The base-64 version of @a str.
+ * @return The base-64 string in the ASCII encoding.  Must be
+ *         g_free'd when no longer needed.
  *
  * @see gaim_base64_decode()
  */
-unsigned char *gaim_base64_encode(const unsigned char *buf, size_t len);
+gchar *gaim_base64_encode(const guint8 *data, gsize len);
 
 /**
- * Converts a string back from its base-64 equivalent.
+ * Converts an ASCII string of base-64 encoded data to
+ * the binary equivalent.
  *
- * @param str     The string to convert back.
- * @param ret_str The returned, non-base-64 string.
- * @param ret_len The returned string length.
+ * @param str     The base-64 string to convert to raw data.
+ * @param ret_len The length of the returned data.  You can
+ *                pass in NULL if you're sure that you know
+ *                the length of the decoded data, or if you
+ *                know you'll be able to use strlen to
+ *                determine the length, etc.
+ *
+ * @return The raw data.  Must be g_free'd when no longer needed.
  *
  * @see gaim_base64_encode()
  */
-void gaim_base64_decode(const char *str, char **ret_str, int *ret_len);
+guint8 *gaim_base64_decode(const char *str, gsize *ret_len);
 
 /*@}*/