/soc/2013/ankitkv/gobjectification: 26e080fb06bb: GObjectified g...

Ankit Vani a at nevitus.org
Fri Jun 14 02:41:22 EDT 2013


Changeset: 26e080fb06bb72f0571be0d81e1a0b415d16f92c
Author:	 Ankit Vani <a at nevitus.org>
Date:	 2013-06-14 12:11 +0530
Branch:	 soc.2013.gobjectification
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/26e080fb06bb

Description:

GObjectified gchecksum.c as PurpleSHA1Cipher, PurpleSHA256Cipher and PurpleMD5Cipher

diffstat:

 libpurple/ciphers/Makefile.am |    6 +-
 libpurple/ciphers/gchecksum.c |  143 -------------------------------
 libpurple/ciphers/md5.c       |  188 ++++++++++++++++++++++++++++++++++++++++++
 libpurple/ciphers/md5.h       |   67 ++++++++++++++
 libpurple/ciphers/sha1.c      |  188 ++++++++++++++++++++++++++++++++++++++++++
 libpurple/ciphers/sha1.h      |   67 ++++++++++++++
 libpurple/ciphers/sha256.c    |  188 ++++++++++++++++++++++++++++++++++++++++++
 libpurple/ciphers/sha256.h    |   67 ++++++++++++++
 8 files changed, 769 insertions(+), 145 deletions(-)

diffs (truncated from 962 to 300 lines):

diff --git a/libpurple/ciphers/Makefile.am b/libpurple/ciphers/Makefile.am
--- a/libpurple/ciphers/Makefile.am
+++ b/libpurple/ciphers/Makefile.am
@@ -12,11 +12,13 @@ libpurple_ciphers_la_SOURCES=\
 	$(AES_SOURCE) \
 	des.c \
 	des3.c \
-	gchecksum.c \
 	hmac.c \
 	md4.c \
+	md5.c \
 	pbkdf2.c \
-	rc4.c
+	rc4.c \
+	sha1.c \
+	sha256.c
 
 INCLUDES = -I$(top_srcdir)/libpurple
 
diff --git a/libpurple/ciphers/gchecksum.c b/libpurple/ciphers/gchecksum.c
deleted file mode 100644
--- a/libpurple/ciphers/gchecksum.c
+++ /dev/null
@@ -1,143 +0,0 @@
-#include "internal.h"
-#include <cipher.h>
-#include "ciphers.h"
-
-static void
-purple_g_checksum_init(PurpleCipherContext *context, GChecksumType type)
-{
-    GChecksum *checksum;
-
-    checksum = g_checksum_new(type);
-    purple_cipher_context_set_data(context, checksum);
-}
-
-static void
-purple_g_checksum_reset(PurpleCipherContext *context, GChecksumType type)
-{
-    GChecksum *checksum;
-
-    checksum = purple_cipher_context_get_data(context);
-    g_return_if_fail(checksum != NULL);
-
-    g_checksum_reset(checksum);
-}
-
-static 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);
-}
-
-static 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);
-}
-
-static gboolean
-purple_g_checksum_digest(PurpleCipherContext *context, GChecksumType type,
-                         guchar *digest, size_t buff_len)
-{
-    GChecksum *checksum;
-    const gssize required_len = g_checksum_type_get_length(type);
-    gsize digest_len = buff_len;
-
-    checksum = purple_cipher_context_get_data(context);
-
-    g_return_val_if_fail(buff_len >= required_len, FALSE);
-    g_return_val_if_fail(checksum != NULL, FALSE);
-
-    g_checksum_get_digest(checksum, digest, &digest_len);
-
-    if (digest_len != required_len)
-        return FALSE;
-
-    purple_cipher_context_reset(context, NULL);
-
-    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, guchar digest[], \
-		size_t len) \
-	{ \
-		return purple_g_checksum_digest(context, (type), digest, len); \
-	} \
-	\
-	static size_t \
-	lower##_get_digest_size(PurpleCipherContext *context) \
-	{ \
-		return g_checksum_type_get_length((type)); \
-	} \
-	\
-	static PurpleCipherOps camel##Ops = { \
-		NULL,                     /* Set option */       \
-		NULL,                     /* Get option */       \
-		lower##_init,             /* init */             \
-		lower##_reset,            /* reset */            \
-		lower##_reset,            /* reset state */      \
-		purple_g_checksum_uninit, /* uninit */           \
-		NULL,                     /* set iv */           \
-		purple_g_checksum_append, /* append */           \
-		lower##_digest,           /* digest */           \
-		lower##_get_digest_size,  /* get digest size */  \
-		NULL,                     /* encrypt */          \
-		NULL,                     /* decrypt */          \
-		NULL,                     /* set salt */         \
-		NULL,                     /* get salt size */    \
-		NULL,                     /* set key */          \
-		NULL,                     /* get key size */     \
-		NULL,                     /* set batch mode */   \
-		NULL,                     /* get batch mode */   \
-		lower##_get_block_size,   /* get block size */   \
-		NULL, NULL, NULL, NULL    /* reserved */         \
-	}; \
-	\
-	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);
diff --git a/libpurple/ciphers/md5.c b/libpurple/ciphers/md5.c
new file mode 100644
--- /dev/null
+++ b/libpurple/ciphers/md5.c
@@ -0,0 +1,188 @@
+/*
+ * purple
+ *
+ * Purple is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
+ */
+#include "md5.h"
+
+/*******************************************************************************
+ * Structs
+ ******************************************************************************/
+#define PURPLE_MD5_CIPHER_GET_PRIVATE(obj) \
+	(G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_MD5_CIPHER, PurpleMD5CipherPrivate))
+
+typedef struct {
+  GChecksum *checksum;
+} PurpleMD5CipherPrivate;
+
+/******************************************************************************
+ * Globals
+ *****************************************************************************/
+static GObjectClass *parent_class = NULL;
+
+/******************************************************************************
+ * Cipher Stuff
+ *****************************************************************************/
+
+static void
+purple_md5_cipher_reset(PurpleCipher *cipher)
+{
+	PurpleMD5Cipher *md5_cipher = PURPLE_MD5_CIPHER(cipher);
+	PurpleMD5CipherPrivate *priv = PURPLE_MD5_CIPHER_GET_PRIVATE(md5_cipher);
+
+	g_return_if_fail(priv != NULL);
+	g_return_if_fail(priv->checksum != NULL);
+
+	g_checksum_reset(priv->checksum);
+}
+
+static void
+purple_md5_cipher_append(PurpleCipher *cipher, const guchar *data,
+								gsize len)
+{
+	PurpleMD5Cipher *md5_cipher = PURPLE_MD5_CIPHER(cipher);
+	PurpleMD5CipherPrivate *priv = PURPLE_MD5_CIPHER_GET_PRIVATE(md5_cipher);
+
+	g_return_if_fail(priv != NULL);
+	g_return_if_fail(priv->checksum != NULL);
+
+	while (len >= G_MAXSSIZE) {
+		g_checksum_update(priv->checksum, data, G_MAXSSIZE);
+		len -= G_MAXSSIZE;
+		data += G_MAXSSIZE;
+	}
+
+	if (len)
+		g_checksum_update(priv->checksum, data, len);
+}
+
+static gboolean
+purple_md5_cipher_digest(PurpleCipher *cipher, guchar *digest, size_t buff_len)
+{
+	PurpleMD5Cipher *md5_cipher = PURPLE_MD5_CIPHER(cipher);
+	PurpleMD5CipherPrivate *priv = PURPLE_MD5_CIPHER_GET_PRIVATE(md5_cipher);
+
+	const gssize required_len = g_checksum_type_get_length(G_CHECKSUM_MD5);
+	gsize digest_len = buff_len;
+
+	g_return_val_if_fail(priv != NULL, FALSE);
+	g_return_val_if_fail(priv->checksum != NULL, FALSE);
+	g_return_val_if_fail(buff_len >= required_len, FALSE);
+
+	g_checksum_get_digest(priv->checksum, digest, &digest_len);
+
+	if (digest_len != required_len)
+		return FALSE;
+
+	purple_md5_cipher_reset(cipher);
+
+	return TRUE;
+}
+
+static size_t
+purple_md5_cipher_get_block_size(PurpleCipher *cipher)
+{
+	return 64;
+}
+
+static size_t
+purple_md5_cipher_get_digest_size(PurpleCipher *cipher)
+{
+	return g_checksum_type_get_length(G_CHECKSUM_MD5);
+}
+
+/******************************************************************************
+ * Object Stuff
+ *****************************************************************************/
+
+static void
+purple_md5_cipher_finalize(GObject *obj)
+{
+	PurpleMD5Cipher *md5_cipher = PURPLE_MD5_CIPHER(obj);
+	PurpleMD5CipherPrivate *priv = PURPLE_MD5_CIPHER_GET_PRIVATE(md5_cipher);
+
+	if (priv->checksum)
+		g_checksum_free(priv->checksum);
+
+	parent_class->finalize(obj);
+}
+
+static void
+purple_md5_cipher_class_init(PurpleMD5CipherClass *klass) {
+	GObjectClass *obj_class = G_OBJECT_CLASS(klass);



More information about the Commits mailing list