changeset 13977:96947ec79828

[gaim-migrate @ 16536] Eliminated some nested function declarations. committer: Tailor Script <tailor@pidgin.im>
author Mark Huetsch <markhuetsch>
date Sat, 22 Jul 2006 11:44:46 +0000
parents 6a5aaf46bd52
children ea00953490a8
files src/protocols/qq/crypt.c
diffstat 1 files changed, 120 insertions(+), 104 deletions(-) [+]
line wrap: on
line diff
--- a/src/protocols/qq/crypt.c	Thu Jul 20 21:43:15 2006 +0000
+++ b/src/protocols/qq/crypt.c	Sat Jul 22 11:44:46 2006 +0000
@@ -40,91 +40,81 @@
 0x61C88647 is what we can track on the ASM codes.!!
 */
 
-#ifndef _WIN32
 #include <arpa/inet.h>
-#else
-#include "win32dep.h"
-#endif
 
 #include <string.h>
 
 #include "crypt.h"
-#include "debug.h"              // gaim_debug, by gfhuang
+#include "debug.h"              // gaim_debug
 
-/*****************************************************************************/
+/********************************************************************
+ * encryption 
+ *******************************************************************/
+
 static void qq_encipher(unsigned long *const v, const unsigned long *const k, unsigned long *const w)
 {
-	register unsigned long y = ntohl(v[0]), z = ntohl(v[1]), a = ntohl(k[0]), b = ntohl(k[1]), c = ntohl(k[2]), d = ntohl(k[3]), n = 0x10, sum = 0, delta = 0x9E3779B9;	/*  0x9E3779B9 - 0x100000000 = -0x61C88647 */
+	register unsigned long y = ntohl(v[0]), 
+		 z = ntohl(v[1]), 
+		 a = ntohl(k[0]), 
+		 b = ntohl(k[1]), 
+		 c = ntohl(k[2]), 
+		 d = ntohl(k[3]), 
+		 n = 0x10, 
+		 sum = 0, 
+		 delta = 0x9E3779B9;	/*  0x9E3779B9 - 0x100000000 = -0x61C88647 */
 
 	while (n-- > 0) {
 		sum += delta;
 		y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
 		z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
-	}			// while
-
-	w[0] = htonl(y);
-	w[1] = htonl(z);
-}				// qq_enciper
-
-/*****************************************************************************/
-static void qq_decipher(unsigned long *const v, const unsigned long *const k, unsigned long *const w)
-{
-	register unsigned long y = ntohl(v[0]), z = ntohl(v[1]), a = ntohl(k[0]), b = ntohl(k[1]), c = ntohl(k[2]), d = ntohl(k[3]), n = 0x10, sum = 0xE3779B90,	// why this ? must be related with n value
-	    delta = 0x9E3779B9;
-
-	/* sum = delta<<5, in general sum = delta * n */
-	while (n-- > 0) {
-		z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
-		y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
-		sum -= delta;
 	}
 
 	w[0] = htonl(y);
 	w[1] = htonl(z);
-}				// qq_decipher
+}
+
+static int rand(void) {	// it can be the real random seed function
+	return 0xdead;
+}			// override with number, convenient for debug
 
-/********************************************************************
- * encrypt part
- *******************************************************************/
+// we encrypt every eight byte chunk
+static void encrypt_every_8_byte(unsigned char *plain, unsigned char *plain_pre_8, unsigned char **crypted, 
+		unsigned char **crypted_pre_8, unsigned char *key, int *count, int *pos_in_byte, int *is_header) 
+{
+	// prepare plain text
+	for (*pos_in_byte = 0; *pos_in_byte < 8; (*pos_in_byte)++) {
+		if (*is_header) {
+			plain[*pos_in_byte] ^= plain_pre_8[*pos_in_byte];
+		} else {
+			plain[*pos_in_byte] ^= (*crypted_pre_8)[*pos_in_byte];
+		}
+	}
+	qq_encipher((unsigned long *) plain, (unsigned long *) key, (unsigned long *) *crypted);	// encrypt it
+
+	for (*pos_in_byte = 0; *pos_in_byte < 8; (*pos_in_byte)++) {
+		(*crypted)[*pos_in_byte] ^= plain_pre_8[*pos_in_byte];
+	}
+	memcpy(plain_pre_8, plain, 8);	// prepare next
+
+	*crypted_pre_8 = *crypted;	// store position of previous 8 byte
+	*crypted += 8;			// prepare next output
+	*count += 8;			// outstrlen increase by 8
+	*pos_in_byte = 0;		// back to start
+	*is_header = 0;			// and exit header
+}					// encrypt_every_8_byte
+
 
 static void qq_encrypt(unsigned char *instr, int instrlen, unsigned char *key, unsigned char *outstr, int *outstrlen_prt)
 {
-	unsigned char plain[8],	// plain text buffer
-	 plain_pre_8[8],	// plain text buffer, previous 8 bytes
-	*crypted,		// crypted text
-	*crypted_pre_8,		// crypted test, previous 8 bytes
-	*inp;			// current position in instr
-	int pos_in_byte = 1,	// loop in the byte 
-	    is_header = 1,	// header is one byte
-	    count = 0,		// number of bytes being crypted
-	    padding = 0;	// number of padding stuff
-
-	int rand(void) {	// it can be the real random seed function
-		return 0xdead;
-	}			// override with number, convenient for debug
-
-  /*** we encrypt every eight byte ***/
-	void encrypt_every_8_byte(void) {
-		for (pos_in_byte = 0; pos_in_byte < 8; pos_in_byte++) {
-			if (is_header) {
-				plain[pos_in_byte] ^= plain_pre_8[pos_in_byte];
-			} else {
-				plain[pos_in_byte] ^= crypted_pre_8[pos_in_byte];
-			}
-		}		// prepare plain text
-		qq_encipher((unsigned long *) plain, (unsigned long *) key, (unsigned long *) crypted);	// encrypt it
-
-		for (pos_in_byte = 0; pos_in_byte < 8; pos_in_byte++) {
-			crypted[pos_in_byte] ^= plain_pre_8[pos_in_byte];
-		}
-		memcpy(plain_pre_8, plain, 8);	// prepare next
-
-		crypted_pre_8 = crypted;	// store position of previous 8 byte
-		crypted += 8;	// prepare next output
-		count += 8;	// outstrlen increase by 8
-		pos_in_byte = 0;	// back to start
-		is_header = 0;	// and exit header
-	}			// encrypt_every_8_byte
+	unsigned char plain[8],		// plain text buffer
+		plain_pre_8[8],		// plain text buffer, previous 8 bytes
+		*crypted,		// crypted text
+		*crypted_pre_8,		// crypted test, previous 8 bytes
+		*inp;			// current position in instr
+	int pos_in_byte = 1,		// loop in the byte 
+		is_header = 1,		// header is one byte
+		count = 0,		// number of bytes being crypted
+		padding = 0;		// number of padding stuff
 
 	pos_in_byte = (instrlen + 0x0a) % 8;	// header padding decided by instrlen
 	if (pos_in_byte) {
@@ -138,13 +128,13 @@
 	crypted = crypted_pre_8 = outstr;
 
 	padding = 1;		// pad some stuff in header
-	while (padding <= 2) {	// at most two byte 
+	while (padding <= 2) {	// at most two bytes
 		if (pos_in_byte < 8) {
 			plain[pos_in_byte++] = rand() & 0xff;
 			padding++;
 		}
 		if (pos_in_byte == 8) {
-			encrypt_every_8_byte();
+			encrypt_every_8_byte(plain, plain_pre_8, &crypted, &crypted_pre_8, key, &count, &pos_in_byte, &is_header);
 		}
 	}
 
@@ -155,62 +145,88 @@
 			instrlen--;
 		}
 		if (pos_in_byte == 8) {
-			encrypt_every_8_byte();
+			encrypt_every_8_byte(plain, plain_pre_8, &crypted, &crypted_pre_8, key, &count, &pos_in_byte, &is_header);
 		}
 	}
 
-	padding = 1;		// pad some stuff in tailer
-	while (padding <= 7) {	// at most sever byte
+	padding = 1;		// pad some stuff in tail
+	while (padding <= 7) {	// at most seven bytes
 		if (pos_in_byte < 8) {
 			plain[pos_in_byte++] = 0x00;
 			padding++;
 		}
 		if (pos_in_byte == 8) {
-			encrypt_every_8_byte();
+			encrypt_every_8_byte(plain, plain_pre_8, &crypted, &crypted_pre_8, key, &count, &pos_in_byte, &is_header);
 		}
 	}
 
 	*outstrlen_prt = count;
-}				// qq_encrypt
+}
 
 
 /******************************************************************** 
- * [decrypt part]
- * return 0 if failed, otherwise return 1
+ * decryption 
  ********************************************************************/
 
+static void qq_decipher(unsigned long *const v, const unsigned long *const k, unsigned long *const w)
+{
+	register unsigned long y = ntohl(v[0]), 
+		z = ntohl(v[1]), 
+		a = ntohl(k[0]), 
+		b = ntohl(k[1]), 
+		c = ntohl(k[2]), 
+		d = ntohl(k[3]), 
+		n = 0x10, 
+		sum = 0xE3779B90,	// why this ? must be related with n value
+		delta = 0x9E3779B9;
+
+	/* sum = delta<<5, in general sum = delta * n */
+	while (n-- > 0) {
+		z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
+		y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
+		sum -= delta;
+	}
+
+	w[0] = htonl(y);
+	w[1] = htonl(z);
+}
+
+static int decrypt_every_8_byte(unsigned char **crypt_buff, const int instrlen, const unsigned char * const key, 
+	int *context_start, unsigned char *decrypted, int *pos_in_byte)
+{
+	for (*pos_in_byte = 0; *pos_in_byte < 8; (*pos_in_byte)++) {
+		if (*context_start + *pos_in_byte >= instrlen)
+			return 1;
+		decrypted[*pos_in_byte] ^= (*crypt_buff)[*pos_in_byte];
+	}
+	qq_decipher((unsigned long *) decrypted, (unsigned long *) key, (unsigned long *) decrypted);
+
+	*context_start += 8;
+	*crypt_buff += 8;
+	*pos_in_byte = 0;
+
+	return 1;
+}
+
+// return 0 if failed, 1 otherwise
 static int qq_decrypt(unsigned char *instr, int instrlen, unsigned char *key, unsigned char *outstr, int *outstrlen_ptr)
 {
 	unsigned char decrypted[8], m[8], *crypt_buff, *crypt_buff_pre_8, *outp;
 	int count, context_start, pos_in_byte, padding;
 
-	int decrypt_every_8_byte(void) {
-		for (pos_in_byte = 0; pos_in_byte < 8; pos_in_byte++) {
-			if (context_start + pos_in_byte >= instrlen)
-				return 1;
-			decrypted[pos_in_byte] ^= crypt_buff[pos_in_byte];
-		}
-		qq_decipher((unsigned long *) decrypted, (unsigned long *) key, (unsigned long *) decrypted);
-
-		context_start += 8;
-		crypt_buff += 8;
-		pos_in_byte = 0;
-		return 1;
-	}			// decrypt_every_8_byte
-
 	// at least 16 bytes and %8 == 0
 	if ((instrlen % 8) || (instrlen < 16)) { 
-		//debug info by gfhuang
-		 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Packet len is not times of 8 bytes, read %d bytes\n", instrlen);
+		gaim_debug(GAIM_DEBUG_ERROR, "QQ", 
+			"Packet len is either too short or not a multiple of 8 bytes, read %d bytes\n", instrlen);
 		return 0;
 	}
 	// get information from header
 	qq_decipher((unsigned long *) instr, (unsigned long *) key, (unsigned long *) decrypted);
 	pos_in_byte = decrypted[0] & 0x7;
 	count = instrlen - pos_in_byte - 10;	// this is the plaintext length
-	// return if outstr buffer is not large enought or error plaintext length
+	// return if outstr buffer is not large enough or error plaintext length
 	if (*outstrlen_ptr < count || count < 0) {
-		 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Buffer len %d is less than real len %d", *outstrlen_ptr, count);
+		gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Buffer len %d is less than real len %d", *outstrlen_ptr, count);
 		return 0;
 	}
 
@@ -219,23 +235,23 @@
 	*outstrlen_ptr = count;	// everything is ok! set return string length
 
 	crypt_buff = instr + 8;	// address of real data start 
-	context_start = 8;	// context is at the second 8 byte
+	context_start = 8;	// context is at the second chunk of 8 bytes
 	pos_in_byte++;		// start of paddng stuff
 
 	padding = 1;		// at least one in header
 	while (padding <= 2) {	// there are 2 byte padding stuff in header
-		if (pos_in_byte < 8) {	// bypass the padding stuff, none sense data
+		if (pos_in_byte < 8) {	// bypass the padding stuff, it's nonsense data
 			pos_in_byte++;
 			padding++;
 		}
 		if (pos_in_byte == 8) {
 			crypt_buff_pre_8 = instr;
-			if (!decrypt_every_8_byte()) {
-				 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error A");
+			if (!decrypt_every_8_byte(&crypt_buff, instrlen, key, &context_start, decrypted, &pos_in_byte)) {
+				gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error A");
 				return 0;
 			}
 		}
-	}			// while
+	}
 
 	outp = outstr;
 	while (count != 0) {
@@ -247,12 +263,12 @@
 		}
 		if (pos_in_byte == 8) {
 			crypt_buff_pre_8 = crypt_buff - 8;
-			if (!decrypt_every_8_byte()) {
-				 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error B");
+			if (!decrypt_every_8_byte(&crypt_buff, instrlen, key, &context_start, decrypted, &pos_in_byte)) {
+				gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error B");
 				return 0;
 			}
 		}
-	}			// while
+	}
 
 	for (padding = 1; padding < 8; padding++) {
 		if (pos_in_byte < 8) {
@@ -262,14 +278,14 @@
 		}
 		if (pos_in_byte == 8) {
 			crypt_buff_pre_8 = crypt_buff;
-			if (!decrypt_every_8_byte()) {
-				 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error C");
+			if (!decrypt_every_8_byte(&crypt_buff, instrlen, key, &context_start, decrypted, &pos_in_byte)) {
+				gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error C");
 				return 0;
 			}
 		}
-	}			// for
+	}
 	return 1;
-}				// qq_decrypt
+}
 
 /*****************************************************************************/
 /* This is the Public Function */
@@ -283,7 +299,7 @@
 		qq_encrypt(instr, instrlen, key, outstr, outstrlen_ptr);
 
 	return 1;		// flag must be DECRYPT or ENCRYPT
-}				// qq_crypt
+}
 
 /*****************************************************************************/
 // END OF FILE