changeset 30041:3affd3508612

Remove some duplicate code.
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Sat, 24 Apr 2010 20:37:49 +0000
parents 0cc4f8651462
children 4dc2e7aadffe
files libpurple/protocols/msn/directconn.c
diffstat 1 files changed, 20 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/msn/directconn.c	Sat Apr 24 06:52:06 2010 +0000
+++ b/libpurple/protocols/msn/directconn.c	Sat Apr 24 20:37:49 2010 +0000
@@ -47,27 +47,22 @@
 #define DC_MAX_PACKET_SIZE    (DC_PACKET_HEADER_SIZE + DC_MAX_BODY_SIZE)
 
 static void
-msn_dc_generate_nonce(MsnDirectConn *dc)
+msn_dc_calculate_nonce_hash(MsnDirectConnNonceType type,
+                            const guchar nonce[16], gchar nonce_hash[37])
 {
-	guint32 *nonce;
-	int i;
 	guchar digest[20];
 
-	nonce = (guint32 *)&dc->nonce;
-	for (i = 0; i < 4; i++)
-		nonce[i] = rand();
-
-	if (dc->nonce_type == DC_NONCE_SHA1) {
+	if (type == DC_NONCE_SHA1) {
 		PurpleCipher *cipher = purple_ciphers_find_cipher("sha1");
 		PurpleCipherContext *context = purple_cipher_context_new(cipher, NULL);
-		purple_cipher_context_append(context, dc->nonce, sizeof(dc->nonce));
+		purple_cipher_context_append(context, nonce, sizeof(nonce));
 		purple_cipher_context_digest(context, sizeof(digest), digest, NULL);
 		purple_cipher_context_destroy(context);
-	} else if (dc->nonce_type == DC_NONCE_PLAIN) {
+	} else if (type == DC_NONCE_PLAIN) {
 		memcpy(digest, nonce, 16);
 	}
 
-	g_sprintf(dc->nonce_hash,
+	g_sprintf(nonce_hash,
 	          "%08X-%04X-%04X-%04X-%08X%04X",
 	          GUINT32_FROM_LE(*((guint32 *)(digest + 0))),
 	          GUINT16_FROM_LE(*((guint16 *)(digest + 4))),
@@ -76,6 +71,19 @@
 	          GUINT32_FROM_BE(*((guint32 *)(digest + 10))),
 	          GUINT16_FROM_BE(*((guint16 *)(digest + 14)))
 	);
+}
+
+static void
+msn_dc_generate_nonce(MsnDirectConn *dc)
+{
+	guint32 *nonce;
+	int i;
+
+	nonce = (guint32 *)&dc->nonce;
+	for (i = 0; i < 4; i++)
+		nonce[i] = rand();
+
+	msn_dc_calculate_nonce_hash(dc->nonce_type, dc->nonce, dc->nonce_hash);
 
 	if (purple_debug_is_verbose())
 		purple_debug_info("msn", "DC %p generated nonce %s\n", dc, dc->nonce_hash);
@@ -690,10 +698,7 @@
 static gboolean
 msn_dc_verify_handshake(MsnDirectConn *dc, guint32 packet_length)
 {
-	PurpleCipherContext *context;
-	PurpleCipher *cipher;
 	guchar nonce[16];
-	guchar digest[20];
 	gchar  nonce_hash[37];
 
 	if (packet_length != DC_PACKET_HEADER_SIZE)
@@ -701,25 +706,7 @@
 
 	memcpy(nonce, dc->in_buffer + 4 + offsetof(MsnDcContext, ack_id), 16);
 
-	if (dc->nonce_type == DC_NONCE_SHA1) {
-		cipher = purple_ciphers_find_cipher("sha1");
-		context = purple_cipher_context_new(cipher, NULL);
-		purple_cipher_context_append(context, nonce, sizeof(nonce));
-		purple_cipher_context_digest(context, sizeof(digest), digest, NULL);
-		purple_cipher_context_destroy(context);
-	} else if (dc->nonce_type == DC_NONCE_PLAIN) {
-		memcpy(digest, nonce, 16);
-	}
-
-	g_sprintf(nonce_hash,
-	          "%08X-%04X-%04X-%04X-%08X%04X",
-	          GUINT32_FROM_LE(*((guint32 *)(digest + 0))),
-	          GUINT16_FROM_LE(*((guint16 *)(digest + 4))),
-	          GUINT16_FROM_LE(*((guint16 *)(digest + 6))),
-	          GUINT16_FROM_BE(*((guint16 *)(digest + 8))),
-	          GUINT32_FROM_BE(*((guint32 *)(digest + 10))),
-	          GUINT16_FROM_BE(*((guint16 *)(digest + 14)))
-	);
+	msn_dc_calculate_nonce_hash(dc->nonce_type, nonce, nonce_hash);
 
 	if (g_str_equal(dc->remote_nonce, nonce_hash)) {
 		purple_debug_info("msn",