diff src/protocols/qq/file_trans.c @ 14021:ef8490f9e823

[gaim-migrate @ 16618] Replaced all C++-style comments with C-style ones. Cleaned up some comments and implemented a more consistent formatting scheme. committer: Tailor Script <tailor@pidgin.im>
author Mark Huetsch <markhuetsch>
date Wed, 02 Aug 2006 15:35:36 +0000
parents 983fd420e86b
children 3cefea0bf4a2
line wrap: on
line diff
--- a/src/protocols/qq/file_trans.c	Wed Aug 02 13:37:13 2006 +0000
+++ b/src/protocols/qq/file_trans.c	Wed Aug 02 15:35:36 2006 +0000
@@ -26,22 +26,20 @@
 #define random rand
 #endif
 
-#include "debug.h"		// gaim_debug
-#include "ft.h"			// gaim_xfer
-//#include "md5.h"
-#include "cipher.h"         //by gfhuang
+#include "debug.h"
+#include "ft.h"
+#include "cipher.h"
 
+#include "crypt.h"
 #include "file_trans.h"
-#include "send_file.h"	// ft_info
-#include "packet_parse.h"	//read_packet
+#include "header_info.h"
+#include "im.h"
+#include "packet_parse.h"
+#include "proxy.h"
 #include "send_core.h"
-#include "header_info.h"
-#include "im.h"		//gen_session_md5
-#include "crypt.h"		//qq_crypt
-#include "proxy.h"		//qq_proxy_write
+#include "send_file.h"
 
-extern gchar*
-hex_dump_to_str (const guint8 * buffer, gint bytes);
+extern gchar *hex_dump_to_str (const guint8 *buffer, gint bytes);
 
 struct _qq_file_header {
 	guint8 tag;
@@ -80,7 +78,6 @@
 
 static void _fill_filename_md5(const gchar *filename, gchar *md5)
 {
-//	md5_state_t ctx;   //gfhuang
 	GaimCipher *cipher;
 	GaimCipherContext *context;
 
@@ -91,18 +88,12 @@
 	gaim_cipher_context_append(context, filename, strlen(filename));
 	gaim_cipher_context_digest(context, 16, md5, NULL);
 	gaim_cipher_context_destroy(context);
-/*    gfhuang
-	md5_init(&ctx);
-	md5_append(&ctx, filename, strlen(filename));
-	md5_finish(&ctx, md5);
-*/
 }
 
 static void _fill_file_md5(const gchar *filename, gint filelen, gchar *md5)
 {
 	FILE *fp;
 	gchar *buffer;
-//	md5_state_t ctx;    //gfhuang
 	GaimCipher *cipher;
 	GaimCipherContext *context;
 
@@ -124,11 +115,7 @@
 	gaim_cipher_context_append(context, buffer, filelen);
 	gaim_cipher_context_digest(context, 16, md5, NULL);
 	gaim_cipher_context_destroy(context);
-/* by  gfhuang
-	md5_init(&ctx);
-	md5_append(&ctx, buffer, filelen);
-	md5_finish(&ctx, md5);
-*/
+
 	fclose(fp);
 }
 
@@ -179,8 +166,7 @@
 }
 
 /* The memmap version has better performance for big files transfering
- * but it will spend plenty of memory, so do not use it in a low-memory host
- */
+ * but it will spend plenty of memory, so do not use it in a low-memory host */
 #ifdef USE_MMAP
 #include <sys/mman.h>
 
@@ -204,8 +190,7 @@
 	return 0;
 }
 
-static gint 
-_qq_xfer_read_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer)
+static gint _qq_xfer_read_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer)
 {
 	ft_info *info = xfer->data;
 	gint readbytes;
@@ -216,8 +201,7 @@
 	return readbytes;
 }
 
-static gint
-_qq_xfer_write_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer)
+static gint _qq_xfer_write_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer)
 {
 	ft_info *info = xfer->data;
 
@@ -232,8 +216,7 @@
 	if (info->buffer) munmap(info->buffer, gaim_xfer_get_size(xfer));
 }
 #else
-static int 
-_qq_xfer_open_file(const gchar *filename, const gchar *method, GaimXfer *xfer)
+static int _qq_xfer_open_file(const gchar *filename, const gchar *method, GaimXfer *xfer)
 {
 	ft_info *info = xfer->data;
 	info->dest_fp = fopen(gaim_xfer_get_local_filename(xfer), method);
@@ -243,8 +226,7 @@
 	return 0;
 }
 
-static gint
-_qq_xfer_read_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer)
+static gint _qq_xfer_read_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer)
 {
 	ft_info *info = xfer->data;
 
@@ -252,8 +234,7 @@
 	return fread(buffer, 1, len, info->dest_fp);
 }
 
-static gint
-_qq_xfer_write_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer)
+static gint _qq_xfer_write_file(guint8 *buffer, guint index, guint len, GaimXfer *xfer)
 {
 	ft_info *info = xfer->data;
 	fseek(info->dest_fp, index * len, SEEK_SET);
@@ -268,8 +249,7 @@
 }
 #endif
 
-static gint 
-_qq_send_file(GaimConnection *gc, guint8 *data, gint len, guint16 packet_type, guint32 to_uid)
+static gint _qq_send_file(GaimConnection *gc, guint8 *data, gint len, guint16 packet_type, guint32 to_uid)
 {
 	gint bytes;
 	guint8 *cursor, *buf;
@@ -296,18 +276,15 @@
 
 	ssize_t _qq_xfer_write(const char *buf, size_t len, GaimXfer *xfer);
 	if (bytes == len + 12) {
-		//gaim_xfer_write(qd->xfer, buf, bytes);
 		_qq_xfer_write(buf, bytes, qd->xfer);
 	} else
 		gaim_debug(GAIM_DEBUG_INFO, "QQ", "send_file: want %d but got %d\n", len + 12, bytes);
 	return bytes;
 }
 
-
 extern gchar *_gen_session_md5(gint uid, gchar *session_key);
 
-/********************************************************************************/
-// send a file to udp channel with QQ_FILE_CONTROL_PACKET_TAG
+/* send a file to udp channel with QQ_FILE_CONTROL_PACKET_TAG */
 void qq_send_file_ctl_packet(GaimConnection *gc, guint16 packet_type, guint32 to_uid, guint8 hellobyte)
 {
 	qq_data *qd;
@@ -350,8 +327,8 @@
 	bytes += create_packet_dw(raw_data, &cursor, 0x00000000);
 	bytes += create_packet_w(raw_data, &cursor, 0x0000);
 	bytes += create_packet_b(raw_data, &cursor, 0x00);
-	// 0x65: send a file, 0x6b: send a custom face, by gfhuang
-	bytes += create_packet_b(raw_data, &cursor, QQ_FILE_TRANSFER_FILE); // FIXME temp by gfhuang
+	/* 0x65: send a file, 0x6b: send a custom face */
+	bytes += create_packet_b(raw_data, &cursor, QQ_FILE_TRANSFER_FILE); /* FIXME temp by gfhuang */
 	switch (packet_type)
 	{
 		case QQ_FILE_CMD_SENDER_SAY_HELLO:
@@ -380,7 +357,7 @@
 		encrypted_len = bytes + 16;
 		encrypted_data = g_newa(guint8, encrypted_len);
 		qq_crypt(ENCRYPT, raw_data, bytes, info->file_session_key, encrypted_data, &encrypted_len);
-		//debug: try to decrypt it
+		/*debug: try to decrypt it */
 		/*
 		if (QQ_DEBUG) {
 			gaim_debug(GAIM_DEBUG_INFO, "QQ", "encrypted packet: \n%s",
@@ -411,11 +388,9 @@
 	g_free(md5);
 }
 
-/********************************************************************************/
-// send a file to udp channel with QQ_FILE_DATA_PACKET_TAG
-static void 
-_qq_send_file_data_packet(GaimConnection *gc, guint16 packet_type, guint8 sub_type, guint32 fragment_index, 
-		guint16 seq, guint8 *data, gint len)
+/* send a file to udp channel with QQ_FILE_DATA_PACKET_TAG */
+static void _qq_send_file_data_packet(GaimConnection *gc, guint16 packet_type, guint8 sub_type, 
+		guint32 fragment_index, guint16 seq, guint8 *data, gint len)
 {
 	gint bytes;
 	guint8 *raw_data, *cursor;
@@ -457,7 +432,8 @@
 					info->fragment_num = (filesize - 1) / QQ_FILE_FRAGMENT_MAXLEN + 1;
 					info->fragment_len = QQ_FILE_FRAGMENT_MAXLEN;
 
-					gaim_debug(GAIM_DEBUG_INFO, "QQ", "start transfering data, %d fragments with %d length each\n",
+					gaim_debug(GAIM_DEBUG_INFO, "QQ", 
+							"start transfering data, %d fragments with %d length each\n",
 							info->fragment_num, info->fragment_len);
 					/* Unknown */
 					bytes += create_packet_w(raw_data, &cursor, 0x0000);
@@ -481,12 +457,13 @@
 							filename_len);
 					break;
 				case QQ_FILE_DATA_INFO:
-					gaim_debug(GAIM_DEBUG_INFO, "QQ", "sending %dth fragment with length %d, offset %d\n",
+					gaim_debug(GAIM_DEBUG_INFO, "QQ", 
+							"sending %dth fragment with length %d, offset %d\n",
 							fragment_index, len, (fragment_index-1)*fragment_size);
-					//bytes += create_packet_w(raw_data, &cursor, ++(qd->send_seq));
+					/* bytes += create_packet_w(raw_data, &cursor, ++(qd->send_seq)); */
 					bytes += create_packet_w(raw_data, &cursor, info->send_seq);
 					bytes += create_packet_b(raw_data, &cursor, sub_type);
-					//bytes += create_packet_dw(raw_data, &cursor, fragment_index);
+					/* bytes += create_packet_dw(raw_data, &cursor, fragment_index); */
 					bytes += create_packet_dw(raw_data, &cursor, fragment_index - 1);
 					bytes += create_packet_dw(raw_data, &cursor, (fragment_index - 1) * fragment_size);
 					bytes += create_packet_w(raw_data, &cursor, len);
@@ -494,10 +471,10 @@
 					break;
 				case QQ_FILE_EOF:
 					gaim_debug(GAIM_DEBUG_INFO, "QQ", "end of sending data\n");
-					//bytes += create_packet_w(raw_data, &cursor, info->fragment_num + 1);
+					/* bytes += create_packet_w(raw_data, &cursor, info->fragment_num + 1); */
 					bytes += create_packet_w(raw_data, &cursor, info->fragment_num);
 					bytes += create_packet_b(raw_data, &cursor, sub_type);
-					//gaim_xfer_set_completed(qd->xfer, TRUE);
+					/* gaim_xfer_set_completed(qd->xfer, TRUE); */
 			}
 			break;
 		case QQ_FILE_CMD_FILE_OP_ACK:
@@ -523,7 +500,7 @@
 	_qq_send_file(gc, raw_data, bytes, QQ_FILE_DATA_PACKET_TAG, info->to_uid);
 }
 
-/* An conversation starts like this
+/* A conversation starts like this:
  * Sender ==> Receiver [QQ_FILE_CMD_PING]
  * Sender <== Receiver [QQ_FILE_CMD_PONG]
  * Sender ==> Receiver [QQ_FILE_CMD_SENDER_SAY_HELLO]
@@ -542,8 +519,7 @@
  */
 
 
-static void
-_qq_process_recv_file_ctl_packet(GaimConnection *gc, guint8 *data, guint8 *cursor,
+static void _qq_process_recv_file_ctl_packet(GaimConnection *gc, guint8 *data, guint8 *cursor,
 		gint len, qq_file_header *fh)
 {
 	guint8 *decrypted_data;
@@ -560,7 +536,7 @@
 
 	md5 = _gen_session_md5(qd->uid, qd->session_key);
 	if (qq_crypt(DECRYPT, cursor, len - (cursor - data), md5, decrypted_data, &decrypted_len)) {
-		cursor = decrypted_data + 16;	//skip md5 section
+		cursor = decrypted_data + 16;	/* skip md5 section */
 		read_packet_w(decrypted_data, &cursor, decrypted_len, &packet_type);
 		read_packet_w(decrypted_data, &cursor, decrypted_len, &seq);
 		cursor += 4+1+1+19+1;
@@ -571,7 +547,7 @@
 			case QQ_FILE_CMD_NOTIFY_IP_ACK:
 				cursor = decrypted_data;
 				qq_get_conn_info(decrypted_data, &cursor, decrypted_len, info);
-//				qq_send_file_ctl_packet(gc, QQ_FILE_CMD_PING, fh->sender_uid, 0);
+/*				qq_send_file_ctl_packet(gc, QQ_FILE_CMD_PING, fh->sender_uid, 0); */
 				qq_send_file_ctl_packet(gc, QQ_FILE_CMD_SENDER_SAY_HELLO, fh->sender_uid, 0);	
 				break;
 			case QQ_FILE_CMD_SENDER_SAY_HELLO:
@@ -611,18 +587,17 @@
 	g_free(md5);
 }
 
-static void
-_qq_recv_file_progess(GaimConnection *gc, guint8 *buffer, guint16 len, guint32 index, guint32 offset)
+static void _qq_recv_file_progess(GaimConnection *gc, guint8 *buffer, guint16 len, guint32 index, guint32 offset)
 {
 	qq_data *qd = (qq_data *) gc->proto_data;
 	GaimXfer *xfer = qd->xfer;
 	ft_info *info = (ft_info *) xfer->data;
 	guint32 mask;
 
-	gaim_debug(GAIM_DEBUG_INFO, "QQ", "receiving %dth fragment with length %d, slide window status %o, max_fragment_index %d\n", 
+	gaim_debug(GAIM_DEBUG_INFO, "QQ", 
+			"receiving %dth fragment with length %d, slide window status %o, max_fragment_index %d\n", 
 			index, len, info->window, info->max_fragment_index);
-	if (info->window == 0 && info->max_fragment_index == 0)
-	{
+	if (info->window == 0 && info->max_fragment_index == 0) {
 		if (_qq_xfer_open_file(gaim_xfer_get_local_filename(xfer), "wb", xfer) == -1) {
 			gaim_xfer_cancel_local(xfer);
 			return;
@@ -655,8 +630,7 @@
 			index, info->window, info->max_fragment_index);
 }
 
-static void
-_qq_send_file_progess(GaimConnection *gc)
+static void _qq_send_file_progess(GaimConnection *gc)
 {
 	qq_data *qd = (qq_data *) gc->proto_data;
 	GaimXfer *xfer = qd->xfer;
@@ -688,15 +662,15 @@
 	}
 }
 
-static void
-_qq_update_send_progess(GaimConnection *gc, guint32 fragment_index)
+static void _qq_update_send_progess(GaimConnection *gc, guint32 fragment_index)
 {
 	qq_data *qd = (qq_data *) gc->proto_data;
 	GaimXfer *xfer = qd->xfer;
 	ft_info *info = (ft_info *) xfer->data;
 	guint32 mask;
 
-	gaim_debug(GAIM_DEBUG_INFO, "QQ", "receiving %dth fragment ack, slide window status %o, max_fragment_index %d\n", 
+	gaim_debug(GAIM_DEBUG_INFO, "QQ", 
+			"receiving %dth fragment ack, slide window status %o, max_fragment_index %d\n", 
 			fragment_index, info->window, info->max_fragment_index);
 	if (fragment_index < info->max_fragment_index || 
 			fragment_index >= info->max_fragment_index + sizeof(info->window)) {
@@ -722,7 +696,7 @@
 		mask = 0x1 << (info->max_fragment_index % sizeof(info->window));
 		while (info->window & mask)
 		{
-			//move the slide window
+			/* move the slide window */
 			info->window &= ~mask;
 			guint8 *buffer;
 			gint readbytes;
@@ -739,12 +713,12 @@
 			else mask = mask << 1;
 		}
 	}
-	gaim_debug(GAIM_DEBUG_INFO, "QQ", "procceed %dth fragment ack, slide window status %o, max_fragment_index %d\n", 
+	gaim_debug(GAIM_DEBUG_INFO, "QQ", 
+			"procceed %dth fragment ack, slide window status %o, max_fragment_index %d\n", 
 			fragment_index, info->window, info->max_fragment_index);
 }
 
-static void
-_qq_process_recv_file_data(GaimConnection *gc, guint8 *data, guint8 *cursor,
+static void _qq_process_recv_file_data(GaimConnection *gc, guint8 *data, guint8 *cursor,
 		gint len, guint32 to_uid)
 {
 	guint16 packet_type;
@@ -756,7 +730,7 @@
 	qq_data *qd = (qq_data *) gc->proto_data;
 	ft_info *info = (ft_info *) qd->xfer->data;
 	
-	cursor += 1;//skip an unknown byte
+	cursor += 1; /* skip an unknown byte */
 	read_packet_w(data, &cursor, len, &packet_type);
 	switch(packet_type)
 	{
@@ -766,7 +740,7 @@
 			switch (sub_type)
 			{
 				case QQ_FILE_BASIC_INFO:
-					cursor += 4;	//file length, we have already known it from xfer
+					cursor += 4;	/* file length, we have already known it from xfer */
 					read_packet_dw(data, &cursor, len, &info->fragment_num);
 					read_packet_dw(data, &cursor, len, &info->fragment_len);
 
@@ -776,7 +750,8 @@
 
 					info->max_fragment_index = 0;
 					info->window = 0;
-					gaim_debug(GAIM_DEBUG_INFO, "QQ", "start receiving data, %d fragments with %d length each\n",
+					gaim_debug(GAIM_DEBUG_INFO, "QQ", 
+							"start receiving data, %d fragments with %d length each\n",
 							info->fragment_num, info->fragment_len);
 					_qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP_ACK, sub_type,
 							0, 0, NULL, 0);
@@ -785,7 +760,8 @@
 					read_packet_dw(data, &cursor, len, &fragment_index);
 					read_packet_dw(data, &cursor, len, &fragment_offset);
 					read_packet_w(data, &cursor, len, &fragment_len);
-					gaim_debug(GAIM_DEBUG_INFO, "QQ", "received %dth fragment with length %d, offset %d\n",
+					gaim_debug(GAIM_DEBUG_INFO, "QQ", 
+							"received %dth fragment with length %d, offset %d\n",
 							fragment_index, fragment_len, fragment_offset);
 					
 					_qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP_ACK, sub_type,
@@ -815,8 +791,8 @@
 					_qq_update_send_progess(gc, fragment_index);
 					if (gaim_xfer_is_completed(qd->xfer))
 						_qq_send_file_data_packet(gc, QQ_FILE_CMD_FILE_OP, QQ_FILE_EOF, 0, 0, NULL, 0);
-				//	else
-				//		_qq_send_file_progess(gc);
+				/*	else
+						_qq_send_file_progess(gc); */
 					break;
 				case QQ_FILE_EOF:
 					/* FIXME: OK, we can end the connection successfully */
@@ -865,4 +841,3 @@
 			gaim_debug(GAIM_DEBUG_INFO, "QQ", "unknown packet tag");
 	}
 }
-