changeset 31670:2b041e31b825

Removed the "new" api I added by moving it to ciphers/gchecksum.c. Moved the gchecksum implements into gchecksum.c as a preproc macro, removed them from their individual files
author Gary Kramlich <grim@reaperworld.com>
date Thu, 17 Feb 2011 00:42:53 +0000
parents 554ac1421c9c
children 1e9425e70a94
files libpurple/cipher.c libpurple/cipher.h libpurple/ciphers/Makefile.am libpurple/ciphers/gchecksum.c libpurple/ciphers/md5.c libpurple/ciphers/sha1.c libpurple/ciphers/sha256.c
diffstat 7 files changed, 165 insertions(+), 221 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/cipher.c	Mon Feb 14 07:38:41 2011 +0000
+++ b/libpurple/cipher.c	Thu Feb 17 00:42:53 2011 +0000
@@ -40,86 +40,6 @@
 #include "signals.h"
 #include "value.h"
 
-#if GLIB_CHECK_VERSION(2,16,0)
-void
-purple_g_checksum_init(PurpleCipherContext *context, GChecksumType type)
-{
-	GChecksum *checksum;
-
-	checksum = g_checksum_new(type);
-	purple_cipher_context_set_data(context, checksum);
-}
-
-void
-purple_g_checksum_reset(PurpleCipherContext *context, GChecksumType type)
-{
-	GChecksum *checksum;
-
-	checksum = purple_cipher_context_get_data(context);
-	g_return_if_fail(checksum != NULL);
-
-#if GLIB_CHECK_VERSION(2,18,0)
-	g_checksum_reset(checksum);
-#else
-	g_checksum_free(checksum);
-	checksum = g_checksum_new(type);
-	purple_cipher_context_set_data(context, checksum);
-#endif
-}
-
-void
-purple_g_checksum_uninit(PurpleCipherContext *context)
-{
-	GChecksum *checksum;
-
-	checksum = purple_cipher_context_get_data(context);
-	g_return_if_fail(checksum != NULL);
-
-	g_checksum_free(checksum);
-}
-
-void
-purple_g_checksum_append(PurpleCipherContext *context, const guchar *data,
-                         gsize len)
-{
-	GChecksum *checksum;
-
-	checksum = purple_cipher_context_get_data(context);
-	g_return_if_fail(checksum != NULL);
-
-	while (len >= G_MAXSSIZE) {
-		g_checksum_update(checksum, data, G_MAXSSIZE);
-		len -= G_MAXSSIZE;
-		data += G_MAXSSIZE;
-	}
-
-	if (len)
-		g_checksum_update(checksum, data, len);
-}
-
-gboolean
-purple_g_checksum_digest(PurpleCipherContext *context, GChecksumType type,
-                         gsize len, guchar *digest, gsize *out_len)
-{
-	GChecksum *checksum;
-	const gssize required_length = g_checksum_type_get_length(type);
-
-	checksum = purple_cipher_context_get_data(context);
-
-	g_return_val_if_fail(len >= required_length, FALSE);
-	g_return_val_if_fail(checksum != NULL, FALSE);
-
-	g_checksum_get_digest(checksum, digest, &len);
-
-	purple_cipher_context_reset(context, NULL);
-
-	if (out_len)
-		*out_len = len;
-
-	return TRUE;
-}
-#endif
-
 /*******************************************************************************
  * Structs
  ******************************************************************************/
--- a/libpurple/cipher.h	Mon Feb 14 07:38:41 2011 +0000
+++ b/libpurple/cipher.h	Thu Feb 17 00:42:53 2011 +0000
@@ -496,19 +496,6 @@
 		const gchar *session_key);
 
 /*@}*/
-/*****************************************************************************/
-/** @name Purple Cipher GChecksum compatibility                              */
-/*****************************************************************************/
-/*@{*/
-#if GLIB_CHECK_VERSION(2,16,0)
-void purple_g_checksum_init(PurpleCipherContext *context, GChecksumType type);
-void purple_g_checksum_reset(PurpleCipherContext *context, GChecksumType type);
-void purple_g_checksum_uninit(PurpleCipherContext *context);
-void purple_g_checksum_append(PurpleCipherContext *context, const guchar *data, gsize len);
-gboolean purple_g_checksum_digest(PurpleCipherContext *context, GChecksumType type, gsize len, guchar *digest, gsize *out_len);
-
-#endif /* GLIB_CHECK_VERSION(2,16,0) */
-/*@}*/
 
 G_END_DECLS
 
--- a/libpurple/ciphers/Makefile.am	Mon Feb 14 07:38:41 2011 +0000
+++ b/libpurple/ciphers/Makefile.am	Thu Feb 17 00:42:53 2011 +0000
@@ -2,6 +2,7 @@
 
 libpurple_ciphers_la_SOURCES=\
 	des.c \
+	gchecksum.c \
 	hmac.c \
 	md4.c \
 	md5.c \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libpurple/ciphers/gchecksum.c	Thu Feb 17 00:42:53 2011 +0000
@@ -0,0 +1,132 @@
+#include <cipher.h>
+
+#if GLIB_CHECK_VERSION(2,16,0)
+
+void
+purple_g_checksum_init(PurpleCipherContext *context, GChecksumType type)
+{
+    GChecksum *checksum;
+
+    checksum = g_checksum_new(type);
+    purple_cipher_context_set_data(context, checksum);
+}
+
+void
+purple_g_checksum_reset(PurpleCipherContext *context, GChecksumType type)
+{
+    GChecksum *checksum;
+
+    checksum = purple_cipher_context_get_data(context);
+    g_return_if_fail(checksum != NULL);
+
+#if GLIB_CHECK_VERSION(2,18,0)
+    g_checksum_reset(checksum);
+#else
+    g_checksum_free(checksum);
+    checksum = g_checksum_new(type);
+    purple_cipher_context_set_data(context, checksum);
+#endif
+}
+
+void
+purple_g_checksum_uninit(PurpleCipherContext *context)
+{
+    GChecksum *checksum;
+
+    checksum = purple_cipher_context_get_data(context);
+    g_return_if_fail(checksum != NULL);
+
+    g_checksum_free(checksum);
+}
+
+void
+purple_g_checksum_append(PurpleCipherContext *context, const guchar *data,
+                         gsize len)
+{
+    GChecksum *checksum;
+
+    checksum = purple_cipher_context_get_data(context);
+    g_return_if_fail(checksum != NULL);
+
+    while (len >= G_MAXSSIZE) {
+        g_checksum_update(checksum, data, G_MAXSSIZE);
+        len -= G_MAXSSIZE;
+        data += G_MAXSSIZE;
+    }
+
+    if (len)
+        g_checksum_update(checksum, data, len);
+}
+
+gboolean
+purple_g_checksum_digest(PurpleCipherContext *context, GChecksumType type,
+                         gsize len, guchar *digest, gsize *out_len)
+{
+    GChecksum *checksum;
+    const gssize required_length = g_checksum_type_get_length(type);
+
+    checksum = purple_cipher_context_get_data(context);
+
+    g_return_val_if_fail(len >= required_length, FALSE);
+    g_return_val_if_fail(checksum != NULL, FALSE);
+
+    g_checksum_get_digest(checksum, digest, &len);
+
+    purple_cipher_context_reset(context, NULL);
+
+    if (out_len)
+        *out_len = len;
+
+    return TRUE;
+}
+
+/******************************************************************************
+ * Macros
+ *****************************************************************************/
+#define PURPLE_G_CHECKSUM_IMPLEMENTATION(lower, camel, type, block_size) \
+	static size_t \
+	lower##_get_block_size(PurpleCipherContext *context) { \
+		return (block_size); \
+	} \
+	\
+	static void \
+	lower##_init(PurpleCipherContext *context, gpointer extra) { \
+		purple_g_checksum_init(context, (type)); \
+	} \
+	\
+	static void \
+	lower##_reset(PurpleCipherContext *context, gpointer extra) { \
+		purple_g_checksum_reset(context, (type)); \
+	} \
+	\
+	static gboolean \
+	lower##_digest(PurpleCipherContext *context, gsize in_len, \
+	                 guchar digest[], size_t *out_len) \
+	{ \
+		return purple_g_checksum_digest(context, (type), in_len, digest, \
+		                                out_len); \
+	} \
+	\
+	static PurpleCipherOps camel##Ops = { \
+		.init = lower##_init, \
+		.reset = lower##_reset, \
+		.uninit = purple_g_checksum_uninit, \
+		.append = purple_g_checksum_append, \
+		.digest = lower##_digest, \
+		.get_block_size = lower##_get_block_size, \
+	}; \
+	\
+	PurpleCipherOps * \
+	purple_##lower##_cipher_get_ops(void) { \
+		return &camel##Ops; \
+	}
+
+/******************************************************************************
+ * Macro Expansion
+ *****************************************************************************/
+PURPLE_G_CHECKSUM_IMPLEMENTATION(md5, MD5, G_CHECKSUM_MD5, 64);
+PURPLE_G_CHECKSUM_IMPLEMENTATION(sha1, SHA1, G_CHECKSUM_SHA1, 64);
+PURPLE_G_CHECKSUM_IMPLEMENTATION(sha256, SHA256, G_CHECKSUM_SHA256, 64);
+
+#endif /* GLIB_CHECK_VERSION(2,16,0) */
+
--- a/libpurple/ciphers/md5.c	Mon Feb 14 07:38:41 2011 +0000
+++ b/libpurple/ciphers/md5.c	Thu Feb 17 00:42:53 2011 +0000
@@ -24,47 +24,9 @@
  */
 #include <cipher.h>
 
-#define MD5_HMAC_BLOCK_SIZE 64
-
-static size_t
-md5_get_block_size(PurpleCipherContext *context)
-{
-	/* This does not change (in this case) */
-	return MD5_HMAC_BLOCK_SIZE;
-}
-
-#if GLIB_CHECK_VERSION(2,16,0)
-
-static void
-md5_init(PurpleCipherContext *context, void *extra)
-{
-	purple_g_checksum_init(context, G_CHECKSUM_MD5);
-}
+#if !GLIB_CHECK_VERSION(2,16,0)
 
-static void
-md5_reset(PurpleCipherContext *context, void *extra)
-{
-	purple_g_checksum_reset(context, G_CHECKSUM_MD5);
-}
-
-static gboolean
-md5_digest(PurpleCipherContext *context, gsize in_len, guchar digest[16],
-		size_t *out_len)
-{
-	return purple_g_checksum_digest(context, G_CHECKSUM_MD5, in_len,
-			digest, out_len);
-}
-
-static PurpleCipherOps MD5Ops = {
-	.init = md5_init,
-	.reset = md5_reset,
-	.uninit = purple_g_checksum_uninit,
-	.append = purple_g_checksum_append,
-	.digest = md5_digest,
-	.get_block_size = md5_get_block_size,
-};
-
-#else /* GLIB_CHECK_VERSION(2,16,0) */
+#define MD5_HMAC_BLOCK_SIZE 64
 
 struct MD5Context {
 	guint32 total[2];
@@ -85,6 +47,13 @@
 	(b)[(i) + 3] = (guchar)((n) >> 24);     \
 }
 
+static size_t
+md5_get_block_size(PurpleCipherContext *context)
+{
+	/* This does not change (in this case) */
+	return MD5_HMAC_BLOCK_SIZE;
+}
+
 static void
 md5_init(PurpleCipherContext *context, gpointer extra) {
 	struct MD5Context *md5_context;
@@ -336,10 +305,10 @@
 	.get_block_size = md5_get_block_size,
 };
 
-#endif /* GLIB_CHECK_VERSION(2,16,0) */
-
 PurpleCipherOps *
 purple_md5_cipher_get_ops(void) {
 	return &MD5Ops;
 }
 
+#endif /* !GLIB_CHECK_VERSION(2,16,0) */
+
--- a/libpurple/ciphers/sha1.c	Mon Feb 14 07:38:41 2011 +0000
+++ b/libpurple/ciphers/sha1.c	Thu Feb 17 00:42:53 2011 +0000
@@ -21,47 +21,7 @@
  */
 #include <cipher.h>
 
-#define SHA1_HMAC_BLOCK_SIZE    64
-
-static size_t
-sha1_get_block_size(PurpleCipherContext *context)
-{
-	/* This does not change (in this case) */
-	return SHA1_HMAC_BLOCK_SIZE;
-}
-
-#if GLIB_CHECK_VERSION(2,16,0)
-
-static void
-sha1_init(PurpleCipherContext *context, void *extra)
-{
-	purple_g_checksum_init(context, G_CHECKSUM_SHA1);
-}
-
-static void
-sha1_reset(PurpleCipherContext *context, void *extra)
-{
-	purple_g_checksum_reset(context, G_CHECKSUM_SHA1);
-}
-
-static gboolean
-sha1_digest(PurpleCipherContext *context, gsize in_len, guchar digest[20],
-            gsize *out_len)
-{
-	return purple_g_checksum_digest(context, G_CHECKSUM_SHA1, in_len,
-	                                digest, out_len);
-}
-
-static PurpleCipherOps SHA1Ops = {
-	.init = sha1_init,
-	.reset = sha1_reset,
-	.uninit = purple_g_checksum_uninit,
-	.append = purple_g_checksum_append,
-	.digest = sha1_digest,
-	.get_block_size = sha1_get_block_size,
-};
-
-#else /* GLIB_CHECK_VERSION(2,16,0) */
+#if !GLIB_CHECK_VERSION(2,16,0)
 
 #define SHA1_HMAC_BLOCK_SIZE    64
 #define SHA1_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xFFFFFFFF)
@@ -76,6 +36,13 @@
 	guint32 sizeLo;
 };
 
+static size_t
+sha1_get_block_size(PurpleCipherContext *context)
+{
+	/* This does not change (in this case) */
+	return SHA1_HMAC_BLOCK_SIZE;
+}
+
 static void
 sha1_hash_block(struct SHA1Context *sha1_ctx) {
 	gint i;
@@ -294,10 +261,10 @@
 	.get_block_size = sha1_get_block_size,
 };
 
-#endif /* GLIB_CHECK_VERSION(2,16,0) */
-
 PurpleCipherOps *
 purple_sha1_cipher_get_ops(void) {
 	return &SHA1Ops;
 }
 
+#endif /* !GLIB_CHECK_VERSION(2,16,0) */
+
--- a/libpurple/ciphers/sha256.c	Mon Feb 14 07:38:41 2011 +0000
+++ b/libpurple/ciphers/sha256.c	Thu Feb 17 00:42:53 2011 +0000
@@ -21,48 +21,9 @@
  */
 #include <cipher.h>
 
-#define SHA256_HMAC_BLOCK_SIZE  64
-
-static size_t
-sha256_get_block_size(PurpleCipherContext *context)
-{
-	/* This does not change (in this case) */
-	return SHA256_HMAC_BLOCK_SIZE;
-}
-
-#if GLIB_CHECK_VERSION(2,16,0)
-
-static void
-sha256_init(PurpleCipherContext *context, void *extra)
-{
-	purple_g_checksum_init(context, G_CHECKSUM_SHA256);
-}
+#if !GLIB_CHECK_VERSION(2,16,0)
 
-static void
-sha256_reset(PurpleCipherContext *context, void *extra)
-{
-	purple_g_checksum_reset(context, G_CHECKSUM_SHA256);
-}
-
-static gboolean
-sha256_digest(PurpleCipherContext *context, gsize in_len, guchar digest[20],
-              gsize *out_len)
-{
-	return purple_g_checksum_digest(context, G_CHECKSUM_SHA256, in_len,
-	                                digest, out_len);
-}
-
-static PurpleCipherOps SHA256Ops = {
-	.init = sha256_init,
-	.reset = sha256_reset,
-	.uninit = purple_g_checksum_uninit,
-	.append = purple_g_checksum_append,
-	.digest = sha256_digest,
-	.get_block_size = sha256_get_block_size,
-};
-
-#else /* GLIB_CHECK_VERSION(2,16,0) */
-
+#define SHA256_HMAC_BLOCK_SIZE  64
 #define SHA256_ROTR(X,n) ((((X) >> (n)) | ((X) << (32-(n)))) & 0xFFFFFFFF)
 
 static const guint32 sha256_K[64] =
@@ -87,6 +48,13 @@
 	guint32 sizeLo;
 };
 
+static size_t
+sha256_get_block_size(PurpleCipherContext *context)
+{
+	/* This does not change (in this case) */
+	return SHA256_HMAC_BLOCK_SIZE;
+}
+
 static void
 sha256_hash_block(struct SHA256Context *sha256_ctx) {
 	gint i;
@@ -296,10 +264,10 @@
 	.get_block_size = sha256_get_block_size,
 };
 
-#endif /* GLIB_CHECK_VERSION(2,16,0) */
-
 PurpleCipherOps *
 purple_sha256_cipher_get_ops(void) {
 	return &SHA256Ops;
 }
 
+#endif /* !GLIB_CHECK_VERSION(2,16,0) */
+