changeset 23049:190bc4ecf6c3

patch-03-add-qq_hex_dump
author SHiNE CsyFeK <csyfek@gmail.com>
date Tue, 24 Jun 2008 12:09:16 +0000
parents 9a5d140400f1
children 51dbe83ebbd3
files libpurple/protocols/qq/ChangeLog libpurple/protocols/qq/char_conv.c libpurple/protocols/qq/file_trans.c libpurple/protocols/qq/group_im.c libpurple/protocols/qq/group_network.c libpurple/protocols/qq/im.c libpurple/protocols/qq/login_logout.c libpurple/protocols/qq/packet_parse.c libpurple/protocols/qq/qq_proxy.c libpurple/protocols/qq/recv_core.c libpurple/protocols/qq/utils.c libpurple/protocols/qq/utils.h
diffstat 12 files changed, 94 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libpurple/protocols/qq/ChangeLog	Tue Jun 24 12:09:16 2008 +0000
@@ -0,0 +1,11 @@
+2008.05.05 - ccpaging <ecc_hy(at)hotmail.com>
+	* Add qq_hex_dump function
+
+2008.04.25 - ccpaging <ecc_hy(at)hotmail.com>, csyfek <csyfek(at)gmail.com>
+	* Rewrite read_packet and create_packet functions, use qq_put and qq_get functions instead
+	* New logic in accord with protocol models to handle packets, some related functions rewritten
+
+2008.03.24 - ccpaging <ecc_hy(at)hotmail.com>
+	* Remove qq_crypt function in crypt.c, use qq_crypt and qq_decrypt directly
+
+** since pidgin-2.4.0 ***
--- a/libpurple/protocols/qq/char_conv.c	Tue Jun 24 11:58:57 2008 +0000
+++ b/libpurple/protocols/qq/char_conv.c	Tue Jun 24 12:09:16 2008 +0000
@@ -114,10 +114,12 @@
 	if (error == NULL)
 		return ret;	/* conversion is OK */
 	else {			/* conversion error */
-		gchar *failed = hex_dump_to_str((guint8 *) str, (len == -1) ? strlen(str) : len);
 		purple_debug(PURPLE_DEBUG_ERROR, "QQ", "%s\n", error->message);
-		purple_debug(PURPLE_DEBUG_WARNING, "QQ", "Dump failed text\n%s", failed);
-		g_free(failed);
+
+		qq_hex_dump(PURPLE_DEBUG_WARNING, "QQ",
+			(guint8 *) str, (len == -1) ? strlen(str) : len,
+			"Dump failed text");
+
 		g_error_free(error);
 		return g_strdup(QQ_NULL_MSG);
 	}
--- a/libpurple/protocols/qq/file_trans.c	Tue Jun 24 11:58:57 2008 +0000
+++ b/libpurple/protocols/qq/file_trans.c	Tue Jun 24 12:09:16 2008 +0000
@@ -293,7 +293,6 @@
 	guint8 *raw_data, *encrypted_data;
 	time_t now;
 	ft_info *info;
-	gchar *hex_dump;
 
 	qd = (qq_data *) gc->proto_data;
 	info = (ft_info *) qd->xfer->data;
@@ -355,9 +354,9 @@
 		return;
 	}
 
-	hex_dump = hex_dump_to_str(raw_data, bytes);
-	purple_debug(PURPLE_DEBUG_INFO, "QQ", "sending packet[%s]: \n%s", qq_get_file_cmd_desc(packet_type), hex_dump);
-	g_free(hex_dump);
+	qq_hex_dump(PURPLE_DEBUG_INFO, "QQ",
+		raw_data, bytes,
+		"sending packet[%s]:", qq_get_file_cmd_desc(packet_type));
 
 	encrypted_len = bytes + 16;
 	encrypted_data = g_newa(guint8, encrypted_len);
@@ -531,7 +530,6 @@
 	guint16 seq;
 	guint8 hellobyte;
 	ft_info *info = (ft_info *) qd->xfer->data;
-	gchar *hex_dump;
 
 	bytes = 0;
 	bytes += _qq_get_file_header(&fh, data + bytes);
@@ -551,9 +549,9 @@
 	decryped_bytes += 4+1+1+19+1;	/* skip something */
 
 	purple_debug(PURPLE_DEBUG_INFO, "QQ", "==> [%d] receive %s packet\n", seq, qq_get_file_cmd_desc(packet_type));
-	hex_dump = hex_dump_to_str(decrypted_data, decrypted_len);
-	purple_debug(PURPLE_DEBUG_INFO, "QQ", "decrypted control packet received: \n%s", hex_dump);
-	g_free(hex_dump);
+	qq_hex_dump(PURPLE_DEBUG_INFO, "QQ",
+		decrypted_data, decrypted_len,
+		"decrypted control packet received:");
 
 	switch (packet_type) {
 		case QQ_FILE_CMD_NOTIFY_IP_ACK:
--- a/libpurple/protocols/qq/group_im.c	Tue Jun 24 11:58:57 2008 +0000
+++ b/libpurple/protocols/qq/group_im.c	Tue Jun 24 12:09:16 2008 +0000
@@ -294,7 +294,7 @@
 /* recv an IM from a group chat */
 void qq_process_recv_group_im(guint8 *data, gint data_len, guint32 internal_group_id, PurpleConnection *gc, guint16 im_type)
 {
-	gchar *msg_with_purple_smiley, *msg_utf8_encoded, *im_src_name, *hex_dump;
+	gchar *msg_with_purple_smiley, *msg_utf8_encoded, *im_src_name;
 	guint16 unknown;
 	guint32 unknown4;
 	PurpleConversation *conv;
@@ -311,8 +311,9 @@
 
 	qd = (qq_data *) gc->proto_data;
 
-	hex_dump = hex_dump_to_str(data, data_len);
-	purple_debug(PURPLE_DEBUG_INFO, "QQ", "group im hex dump\n%s\n", hex_dump);
+	qq_hex_dump(PURPLE_DEBUG_INFO, "QQ",
+		data, data_len,
+		"group im hex dump");
 
 	im_group = g_newa(qq_recv_group_im, 1);
 
@@ -393,7 +394,6 @@
 					(conv)), im_src_name, 0, msg_utf8_encoded, im_group->send_time);
 		g_free(im_src_name);
 	}
-	g_free(hex_dump);
 	g_free(msg_with_purple_smiley);
 	g_free(msg_utf8_encoded);
 	g_free(im_group->msg);
--- a/libpurple/protocols/qq/group_network.c	Tue Jun 24 11:58:57 2008 +0000
+++ b/libpurple/protocols/qq/group_network.c	Tue Jun 24 12:09:16 2008 +0000
@@ -98,12 +98,11 @@
 /* default process, dump only */
 static void _qq_process_group_cmd_reply_default(guint8 *data, gint len, PurpleConnection *gc)
 {
-	gchar *hex_dump;
 	g_return_if_fail(data != NULL && len > 0);
 
-	hex_dump = hex_dump_to_str(data, len);
-	purple_debug(PURPLE_DEBUG_INFO, "QQ", "Dump unprocessed group cmd reply:\n%s", hex_dump);
-	g_free(hex_dump);
+	qq_hex_dump(PURPLE_DEBUG_INFO, "QQ",
+		data, len, 
+		"Dump unprocessed group cmd reply:");
 }
 
 /* The lower layer command of send group cmd */
--- a/libpurple/protocols/qq/im.c	Tue Jun 24 11:58:57 2008 +0000
+++ b/libpurple/protocols/qq/im.c	Tue Jun 24 12:09:16 2008 +0000
@@ -340,7 +340,6 @@
 	gint bytes = 0;
 	qq_recv_normal_im_common *common;
 	qq_recv_normal_im_unprocessed *im_unprocessed;
-	gchar *hex_dump;
 
 	g_return_if_fail (data != NULL && len != 0);
 
@@ -389,9 +388,9 @@
 			purple_debug (PURPLE_DEBUG_WARNING, "QQ",
 					"Normal IM, unprocessed type [0x%04x]\n",
 					common->normal_im_type);
-			hex_dump = hex_dump_to_str(im_unprocessed->unknown, im_unprocessed->length);
-			purple_debug (PURPLE_DEBUG_WARNING, "QQ", "Dump unknown part.\n%s", hex_dump);
-			g_free(hex_dump);
+			purple_debug (PURPLE_DEBUG_WARNING, "QQ",
+				im_unprocessed->unknown, im_unprocessed->length,
+				"Dump unknown part.");
 			g_free (common->session_md5);
 			return;
 	}
--- a/libpurple/protocols/qq/login_logout.c	Tue Jun 24 11:58:57 2008 +0000
+++ b/libpurple/protocols/qq/login_logout.c	Tue Jun 24 12:09:16 2008 +0000
@@ -385,7 +385,6 @@
 void qq_process_request_login_token_reply(guint8 *buf, gint buf_len, PurpleConnection *gc)
 {
 	qq_data *qd;
-	gchar *hex_dump;
 
 	g_return_if_fail(buf != NULL && buf_len != 0);
 
@@ -398,20 +397,18 @@
 			purple_debug(PURPLE_DEBUG_INFO, "QQ",
 					"Attempting to proceed with the actual packet length.\n");
 		}
-		hex_dump = hex_dump_to_str(buf+2, buf_len-2);
-		purple_debug(PURPLE_DEBUG_INFO, "QQ",
-				"<<< got a token with %d bytes -> [default] decrypt and dump\n%s", buf_len-2, hex_dump);
+		qq_hex_dump(PURPLE_DEBUG_INFO, "QQ",
+				buf+2, buf_len-2,
+				"<<< got a token -> [default] decrypt and dump");
 		qq_send_packet_login(gc, buf_len-2, buf+2);
 	} else {
 		purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Unknown request login token reply code : %d\n", buf[0]);
-		hex_dump = hex_dump_to_str(buf, buf_len);
-		purple_debug(PURPLE_DEBUG_WARNING, "QQ",
-				">>> %d bytes -> [default] decrypt and dump\n%s",
-				buf_len, hex_dump);
+		qq_hex_dump(PURPLE_DEBUG_WARNING, "QQ",
+				buf, buf_len,
+				">>> [default] decrypt and dump");
 		try_dump_as_gbk(buf, buf_len);
 		purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, _("Error requesting login token"));
 	}
-	g_free(hex_dump);
 }
 
 /* send logout packets to QQ server */
@@ -433,7 +430,6 @@
 	gint len, ret, bytes;
 	guint8 *data;
 	qq_data *qd;
-	gchar *hex_dump;
 
 	g_return_if_fail(buf != NULL && buf_len != 0);
 
@@ -466,11 +462,9 @@
 				break;
 			default:
 				purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Unknown reply code: %d\n", data[0]);
-				hex_dump = hex_dump_to_str(data, len);
-				purple_debug(PURPLE_DEBUG_WARNING, "QQ",
-						">>> %d bytes -> [default] decrypt and dump\n%s",
-						buf_len, hex_dump);
-				g_free(hex_dump);
+				qq_hex_dump(PURPLE_DEBUG_WARNING, "QQ",
+						data, len,
+						">>> [default] decrypt and dump");
 				try_dump_as_gbk(data, len);
 
 				ret = QQ_LOGIN_REPLY_MISC_ERROR;
--- a/libpurple/protocols/qq/packet_parse.c	Tue Jun 24 11:58:57 2008 +0000
+++ b/libpurple/protocols/qq/packet_parse.c	Tue Jun 24 12:09:16 2008 +0000
@@ -81,7 +81,7 @@
 	guint8 b_dest;
 	memcpy(&b_dest, buf, sizeof(b_dest));
 	*b = b_dest;
-        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get8] buf %d\n", (void *)buf);
+        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get8] buf %lu\n", (void *)buf);
         purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get8] b_dest 0x%2x, *b 0x%02x\n", b_dest, *b);
 	return sizeof(b_dest);
 }
@@ -135,7 +135,7 @@
 	guint16 w_dest;
 	memcpy(&w_dest, buf, sizeof(w_dest));
 	*w = g_ntohs(w_dest);
-        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get16] buf %d\n", (void *)buf);
+        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get16] buf %lu\n", (void *)buf);
         purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get16] w_dest 0x%04x, *w 0x%04x\n", w_dest, *w);
 	return sizeof(w_dest);
 }
@@ -183,7 +183,7 @@
 	guint32 dw_dest;
 	memcpy(&dw_dest, buf, sizeof(dw_dest));
 	*dw = g_ntohl(dw_dest);
-        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get32] buf %d\n", (void *)buf);
+        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get32] buf %lu\n", (void *)buf);
         purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get32] dw_dest 0x%08x, *dw 0x%08x\n", dw_dest, *dw);
 	return sizeof(dw_dest);
 }
@@ -214,7 +214,7 @@
 gint qq_getdata(guint8 *data, gint datalen, guint8 *buf)
 {
     memcpy(data, buf, datalen);
-        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getdata] buf %d\n", (void *)buf);
+        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getdata] buf %lu\n", (void *)buf);
     return datalen;
 }
 
@@ -237,7 +237,7 @@
 {
 	guint32 dw_dest;
 	memcpy(&dw_dest, buf, sizeof(dw_dest));
-        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getime] buf %d\n", (void *)buf);
+        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getime] buf %lu\n", (void *)buf);
         purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getime] dw_dest before 0x%08x\n", dw_dest);
 	dw_dest = g_ntohl(dw_dest);
         purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getime] dw_dest after 0x%08x\n", dw_dest);
@@ -282,7 +282,7 @@
 gint qq_put8(guint8 *buf, guint8 b)
 {
     memcpy(buf, &b, sizeof(b));
-        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put8] buf %d\n", (void *)buf);
+        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put8] buf %lu\n", (void *)buf);
         purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put8] b 0x%02x\n", b);
     return sizeof(b);
 }
@@ -327,7 +327,7 @@
 {
     guint16 w_porter;
     w_porter = g_htons(w);
-        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put16] buf %d\n", (void *)buf);
+        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put16] buf %lu\n", (void *)buf);
         purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put16] w 0x%04x, w_porter 0x%04x\n", w, w_porter);
     memcpy(buf, &w_porter, sizeof(w_porter));
     return sizeof(w_porter);
@@ -370,7 +370,7 @@
 {
     guint32 dw_porter;
     dw_porter = g_htonl(dw);
-        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put32] buf %d\n", (void *)buf);
+        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put32] buf %lu\n", (void *)buf);
         purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put32] dw 0x%08x, dw_porter 0x%08x\n", dw, dw_porter);
     memcpy(buf, &dw_porter, sizeof(dw_porter));
     return sizeof(dw_porter);
@@ -404,7 +404,7 @@
 gint qq_putdata(guint8 *buf, guint8 *data, const int datalen)
 {
     memcpy(buf, data, datalen);
-        purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][putdata] buf %d\n", (void *)buf);
+	purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][putdata] buf %lu\n", (void *)buf);
     return datalen;
 }
 
--- a/libpurple/protocols/qq/qq_proxy.c	Tue Jun 24 11:58:57 2008 +0000
+++ b/libpurple/protocols/qq/qq_proxy.c	Tue Jun 24 12:09:16 2008 +0000
@@ -70,9 +70,9 @@
 	   */
 
 	/* modified by s3e, 20080424 */
-	gchar *packet_dump = hex_dump_to_str(buf, len);
-	purple_debug(PURPLE_DEBUG_INFO, desc, "\n%s\n", packet_dump);
-	g_free(packet_dump);
+	qq_hex_dump(PURPLE_DEBUG_INFO, desc,
+		buf, len,
+		"");
 }
 
 /* QQ 2003iii uses double MD5 for the pwkey to get the session key */
--- a/libpurple/protocols/qq/recv_core.c	Tue Jun 24 11:58:57 2008 +0000
+++ b/libpurple/protocols/qq/recv_core.c	Tue Jun 24 12:09:16 2008 +0000
@@ -92,11 +92,10 @@
 
 	_qq_show_packet("Processing unknown packet", buf, len);
 	if (qq_decrypt(buf, buf_len, qd->session_key, data, &len)) {
-		gchar *hex_dump = hex_dump_to_str(data, len);
-		purple_debug(PURPLE_DEBUG_WARNING, "QQ",
-				">>> [%d] %s, %d bytes -> [default] decrypt and dump\n%s",
-				seq, qq_get_cmd_desc(cmd), buf_len, hex_dump);
-		g_free(hex_dump);
+		qq_hex_dump(PURPLE_DEBUG_WARNING, "QQ",
+				data, len,
+				">>> [%d] %s -> [default] decrypt and dump",
+				seq, qq_get_cmd_desc(cmd));
 		try_dump_as_gbk(data, len);
 	} else {
 		purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Fail decrypt packet with default process\n");
@@ -118,10 +117,9 @@
 	bytes_expected = qd->use_tcp ? QQ_TCP_HEADER_LENGTH : QQ_UDP_HEADER_LENGTH;
 
 	if (buf_len < bytes_expected) {
-		gchar *hex_dump = hex_dump_to_str(buf, buf_len);
-		purple_debug(PURPLE_DEBUG_ERROR,
-				"QQ", "Received packet is too short, dump and drop\n%s", hex_dump);
-		g_free(hex_dump);
+		qq_hex_dump(PURPLE_DEBUG_ERROR, "QQ",
+				buf, buf_len,
+				"Received packet is too short, dump and drop");
 		return;
 	}
 
@@ -153,10 +151,9 @@
 	}
 
 	if ((buf[buf_len - 1] != QQ_PACKET_TAIL) || (header.header_tag != QQ_PACKET_TAG)) {
-		gchar *hex_dump = hex_dump_to_str(buf, buf_len);
-		purple_debug(PURPLE_DEBUG_ERROR,
-				"QQ", "Unknown QQ proctocol, dump and drop\n%s", hex_dump);
-		g_free(hex_dump);
+		qq_hex_dump(PURPLE_DEBUG_ERROR, "QQ",
+			buf, buf_len,
+			"Unknown QQ proctocol, dump and drop");
 		return;
 	}
 
--- a/libpurple/protocols/qq/utils.c	Tue Jun 24 11:58:57 2008 +0000
+++ b/libpurple/protocols/qq/utils.c	Tue Jun 24 12:09:16 2008 +0000
@@ -294,7 +294,7 @@
 
 /* Dumps a chunk of raw data into an ASCII hex string.
  * The return should be freed later. */
-gchar *hex_dump_to_str(const guint8 *const buffer, gint bytes)
+static gchar *hex_dump_to_str(const guint8 *const buffer, gint bytes)
 {
 	GString *str;
 	gchar *ret;
@@ -331,6 +331,31 @@
 	return ret;
 }
 
+void qq_hex_dump(PurpleDebugLevel level, const char *category,
+		const guint8 *pdata, gint bytes,	
+		const char *format, ...)
+{
+	va_list args;
+	char *arg_s = NULL;
+	gchar *phex = NULL;
+
+	g_return_if_fail(level != PURPLE_DEBUG_ALL);
+	g_return_if_fail(format != NULL);
+
+	va_start(args, format);
+	arg_s = g_strdup_vprintf(format, args);
+	va_end(args);
+
+	if (bytes <= 0) {
+		purple_debug(level, category, arg_s);
+		return;
+	}
+
+	phex = hex_dump_to_str(pdata, bytes);
+	purple_debug(level, category, "%s - (len %d)\n%s", arg_s, bytes, phex);
+	g_free(phex);
+}
+
 /* convert face num from packet (0-299) to local face (1-100) */
 gchar *face_to_icon_str(gint face)
 {
--- a/libpurple/protocols/qq/utils.h	Tue Jun 24 11:58:57 2008 +0000
+++ b/libpurple/protocols/qq/utils.h	Tue Jun 24 12:09:16 2008 +0000
@@ -28,6 +28,8 @@
 #include <stdio.h>
 #include <glib.h>
 
+#include "debug.h"
+
 gchar *get_name_by_index_str(gchar **array, const gchar *index_str, gint amount);
 gchar *get_index_str_by_name(gchar **array, const gchar *name, gint amount);
 gint qq_string_to_dec_value(const gchar *str);
@@ -46,8 +48,10 @@
 
 void try_dump_as_gbk(const guint8 *const data, gint len);
 
+void qq_hex_dump(PurpleDebugLevel level, const char *category,
+		const guint8 *pdata, gint bytes,	
+		const char *format, ...);
 guint8 *hex_str_to_bytes(const gchar *buf, gint *out_len);
-gchar *hex_dump_to_str(const guint8 *buf, gint buf_len);
 
 const gchar *qq_buddy_icon_dir(void);
 const gchar *qq_win32_buddy_icon_dir(void);