/soc/2013/ankitkv/gobjectification: 74b179b1e8ef: GObjectify the...

Ankit Vani a at nevitus.org
Thu Jun 13 05:19:47 EDT 2013


Changeset: 74b179b1e8ef49ad15bc6b68c31c8051445b2125
Author:	 Ankit Vani <a at nevitus.org>
Date:	 2013-06-13 14:49 +0530
Branch:	 soc.2013.gobjectification
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/74b179b1e8ef

Description:

GObjectify the PurpleCipher structure

diffstat:

 libpurple/cipher.c          |  1124 +++++++++++++++---------------------------
 libpurple/cipher.h          |   502 ++----------------
 libpurple/ciphers/ciphers.h |    66 --
 libpurple/util.c            |   166 ++++++
 libpurple/util.h            |    42 +
 5 files changed, 688 insertions(+), 1212 deletions(-)

diffs (truncated from 2089 to 300 lines):

diff --git a/libpurple/cipher.c b/libpurple/cipher.c
--- a/libpurple/cipher.c
+++ b/libpurple/cipher.c
@@ -35,816 +35,506 @@
  */
 #include "internal.h"
 #include "cipher.h"
-#include "ciphers/ciphers.h"
-#include "dbus-maybe.h"
-#include "debug.h"
-#include "signals.h"
-#include "value.h"
-
-/*******************************************************************************
- * Structs
- ******************************************************************************/
-struct _PurpleCipher {
-	gchar *name;          /**< Internal name - used for searching */
-	PurpleCipherOps *ops; /**< Operations supported by this cipher */
-	guint ref;            /**< Reference count */
-};
-
-struct _PurpleCipherContext {
-	PurpleCipher *cipher; /**< Cipher this context is under */
-	gpointer data;        /**< Internal cipher state data */
-};
 
 /******************************************************************************
  * Globals
  *****************************************************************************/
-static GList *ciphers = NULL;
+static GObjectClass *parent_class = NULL;
+
+/******************************************************************************
+ * Object Stuff
+ *****************************************************************************/
+static void
+purple_cipher_finalize(GObject *obj) {
+	purple_cipher_reset(PURPLE_CIPHER(obj));
+
+	G_OBJECT_CLASS(parent_class)->finalize(obj);
+}
+
+static void
+purple_cipher_class_init(PurpleCipherClass *klass) {
+	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
+
+	parent_class = g_type_class_peek_parent(klass);
+
+	obj_class->finalize = purple_cipher_finalize;
+}
 
 /******************************************************************************
  * PurpleCipher API
  *****************************************************************************/
 const gchar *
 purple_cipher_get_name(PurpleCipher *cipher) {
+	PurpleCipherClass *klass = NULL;
+
 	g_return_val_if_fail(cipher, NULL);
+	g_return_val_if_fail(PURPLE_IS_CIPHER(cipher), NULL);
 
-	return cipher->name;
+	klass = PURPLE_CIPHER_GET_CLASS(cipher);
+	g_return_val_if_fail(klass->get_name, NULL);
+
+	return klass->get_name(cipher);
 }
 
-guint
-purple_cipher_get_capabilities(PurpleCipher *cipher) {
-	PurpleCipherOps *ops = NULL;
-	guint caps = 0;
+GType
+purple_cipher_get_type(void) {
+	static GType type = 0;
 
-	g_return_val_if_fail(cipher, 0);
+	if(type == 0) {
+		static const GTypeInfo info = {
+			sizeof(PurpleCipherClass),
+			NULL,
+			NULL,
+			(GClassInitFunc)purple_cipher_class_init,
+			NULL,
+			NULL,
+			sizeof(PurpleCipher),
+			0,
+			NULL,
+			NULL
+		};
 
-	ops = cipher->ops;
-	g_return_val_if_fail(ops, 0);
+		type = g_type_register_static(G_TYPE_OBJECT,
+									  "PurpleCipher",
+									  &info, G_TYPE_FLAG_ABSTRACT);
+	}
 
-	if(ops->set_option)
-		caps |= PURPLE_CIPHER_CAPS_SET_OPT;
-	if(ops->get_option)
-		caps |= PURPLE_CIPHER_CAPS_GET_OPT;
-	if(ops->init)
-		caps |= PURPLE_CIPHER_CAPS_INIT;
-	if(ops->reset)
-		caps |= PURPLE_CIPHER_CAPS_RESET;
-	if(ops->reset_state)
-		caps |= PURPLE_CIPHER_CAPS_RESET_STATE;
-	if(ops->uninit)
-		caps |= PURPLE_CIPHER_CAPS_UNINIT;
-	if(ops->set_iv)
-		caps |= PURPLE_CIPHER_CAPS_SET_IV;
-	if(ops->append)
-		caps |= PURPLE_CIPHER_CAPS_APPEND;
-	if(ops->digest)
-		caps |= PURPLE_CIPHER_CAPS_DIGEST;
-	if(ops->get_digest_size)
-		caps |= PURPLE_CIPHER_CAPS_GET_DIGEST_SIZE;
-	if(ops->encrypt)
-		caps |= PURPLE_CIPHER_CAPS_ENCRYPT;
-	if(ops->decrypt)
-		caps |= PURPLE_CIPHER_CAPS_DECRYPT;
-	if(ops->set_salt)
-		caps |= PURPLE_CIPHER_CAPS_SET_SALT;
-	if(ops->get_salt_size)
-		caps |= PURPLE_CIPHER_CAPS_GET_SALT_SIZE;
-	if(ops->set_key)
-		caps |= PURPLE_CIPHER_CAPS_SET_KEY;
-	if (ops->get_key_size)
-		caps |= PURPLE_CIPHER_CAPS_GET_KEY_SIZE;
-	if(ops->set_batch_mode)
-		caps |= PURPLE_CIPHER_CAPS_SET_BATCH_MODE;
-	if(ops->get_batch_mode)
-		caps |= PURPLE_CIPHER_CAPS_GET_BATCH_MODE;
-	if(ops->get_block_size)
-		caps |= PURPLE_CIPHER_CAPS_GET_BLOCK_SIZE;
-
-	return caps;
+	return type;
 }
 
-ssize_t
-purple_cipher_digest_region(const gchar *name, const guchar *data,
-	size_t data_len, guchar digest[], size_t out_size)
-{
-	PurpleCipher *cipher;
-	PurpleCipherContext *context;
-	ssize_t digest_size;
-	gboolean succ;
+GType
+purple_cipher_batch_mode_get_type(void) {
+	static GType type = 0;
 
-	g_return_val_if_fail(name, -1);
-	g_return_val_if_fail(data, -1);
+	if(type == 0) {
+		static const GEnumValue values[] = {
+			{ PURPLE_CIPHER_BATCH_MODE_ECB, "ECB", "ECB" },
+			{ PURPLE_CIPHER_BATCH_MODE_CBC, "CBC", "CBC" },
+			{ 0, NULL, NULL },
+		};
 
-	cipher = purple_ciphers_find_cipher(name);
-
-	g_return_val_if_fail(cipher, -1);
-
-	if(!cipher->ops->append || !cipher->ops->digest || !cipher->ops->get_digest_size) {
-		purple_debug_warning("cipher", "purple_cipher_region failed: "
-						"the %s cipher does not support appending and or "
-						"digesting.", cipher->name);
-		return -1;
+		type = g_enum_register_static("PurpleCipherBatchMode", values);
 	}
 
-	context = purple_cipher_context_new(cipher, NULL);
-	digest_size = purple_cipher_context_get_digest_size(context);
-	if (out_size < digest_size) {
-		purple_debug_error("cipher", "purple_cipher_region failed: "
-			"provided output buffer too small\n");
-		purple_cipher_context_destroy(context);
-		return -1;
-	}
-	purple_cipher_context_append(context, data, data_len);
-	succ = purple_cipher_context_digest(context, digest, out_size);
-	purple_cipher_context_destroy(context);
-
-	return succ ? digest_size : -1;
+	return type;
 }
 
-/******************************************************************************
- * PurpleCiphers API
- *****************************************************************************/
-PurpleCipher *
-purple_ciphers_find_cipher(const gchar *name) {
-	PurpleCipher *cipher;
-	GList *l;
+/**
+ * purple_cipher_reset:
+ * @cipher: The cipher to reset
+ *
+ * Resets a cipher to it's default value
+ *
+ * @note If you have set an IV you will have to set it after resetting
+ */
+void
+purple_cipher_reset(PurpleCipher *cipher) {
+	PurpleCipherClass *klass = NULL;
 
-	g_return_val_if_fail(name, NULL);
+	g_return_if_fail(PURPLE_IS_CIPHER(cipher));
 
-	for(l = ciphers; l; l = l->next) {
-		cipher = PURPLE_CIPHER(l->data);
+	klass = PURPLE_CIPHER_GET_CLASS(cipher);
 
-		if(!g_ascii_strcasecmp(cipher->name, name))
-			return cipher;
-	}
-
-	return NULL;
+	if(klass && klass->reset)
+		klass->reset(cipher);
 }
 
-PurpleCipher *
-purple_ciphers_register_cipher(const gchar *name, PurpleCipherOps *ops) {
-	PurpleCipher *cipher = NULL;
+/**
+ * purple_cipher_set_iv:
+ * @cipher: The cipher to set the IV to
+ * @iv: The initialization vector to set
+ * @len: The len of the IV
+ *
+ * @note This should only be called right after a cipher is created or reset
+ *
+ * Sets the initialization vector for a cipher
+ */
+void
+purple_cipher_set_iv(PurpleCipher *cipher, guchar *iv, size_t len)
+{
+	PurpleCipherClass *klass = NULL;
 
-	g_return_val_if_fail(name, NULL);
-	g_return_val_if_fail(ops, NULL);
-	g_return_val_if_fail(!purple_ciphers_find_cipher(name), NULL);
+	g_return_if_fail(PURPLE_IS_CIPHER(cipher));
+	g_return_if_fail(iv);
 
-	cipher = g_new0(PurpleCipher, 1);
-	PURPLE_DBUS_REGISTER_POINTER(cipher, PurpleCipher);
+	klass = PURPLE_CIPHER_GET_CLASS(cipher);
 
-	cipher->name = g_strdup(name);
-	cipher->ops = ops;
-
-	ciphers = g_list_append(ciphers, cipher);
-
-	purple_signal_emit(purple_ciphers_get_handle(), "cipher-added", cipher);
-
-	return cipher;
+	if(klass && klass->set_iv)
+		klass->set_iv(cipher, iv, len);
+	else
+		purple_debug_warning("cipher", "the %s cipher does not implement the "
+						"set_iv method\n",
+						klass->get_name ? klass->get_name(cipher) : "");
 }
 
+/**
+ * purple_cipher_append:
+ * @cipher: The cipher to append data to
+ * @data: The data to append
+ * @len: The length of the data
+ *
+ * Appends data to the cipher
+ */
+void
+purple_cipher_append(PurpleCipher *cipher, const guchar *data,
+								size_t len)
+{
+	PurpleCipherClass *klass = NULL;
+
+	g_return_if_fail(PURPLE_IS_CIPHER(cipher));
+
+	klass = PURPLE_CIPHER_GET_CLASS(cipher);
+
+	if(klass && klass->append)
+		klass->append(cipher, data, len);
+	else
+		purple_debug_warning("cipher", "the %s cipher does not implement the "
+						"append method\n",
+						klass->get_name ? klass->get_name(cipher) : "");
+}
+
+/**
+ * purple_cipher_digest:
+ * @cipher: The cipher to digest
+ * @in_len: The length of the buffer



More information about the Commits mailing list