/soc/2013/ankitkv/gobjectification: fde23518e1e5: Moved PurpleHa...

Ankit Vani a at nevitus.org
Sun Oct 20 07:07:08 EDT 2013


Changeset: fde23518e1e53f6670f2cdd15a8d181bbcf2845a
Author:	 Ankit Vani <a at nevitus.org>
Date:	 2013-10-20 15:37 +0530
Branch:	 soc.2013.gobjectification
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/fde23518e1e5

Description:

Moved PurpleHash to cipher.[ch]

diffstat:

 libpurple/Makefile.am                        |    2 -
 libpurple/Makefile.mingw                     |    3 +-
 libpurple/cipher.c                           |  217 ++++++++++++++++++++++++
 libpurple/cipher.h                           |  139 ++++++++++++++-
 libpurple/ciphers/aescipher.c                |    4 +-
 libpurple/ciphers/des3cipher.c               |    1 +
 libpurple/ciphers/descipher.c                |    1 +
 libpurple/ciphers/hmaccipher.c               |    1 +
 libpurple/ciphers/hmaccipher.h               |    1 -
 libpurple/ciphers/md4hash.c                  |    1 +
 libpurple/ciphers/md4hash.h                  |    2 +-
 libpurple/ciphers/md5hash.c                  |    1 +
 libpurple/ciphers/md5hash.h                  |    2 +-
 libpurple/ciphers/pbkdf2cipher.c             |    2 +-
 libpurple/ciphers/pbkdf2cipher.h             |    1 -
 libpurple/ciphers/rc4cipher.c                |    1 +
 libpurple/ciphers/sha1hash.c                 |    1 +
 libpurple/ciphers/sha1hash.h                 |    2 +-
 libpurple/ciphers/sha256hash.c               |    1 +
 libpurple/ciphers/sha256hash.h               |    2 +-
 libpurple/hash.c                             |  241 ---------------------------
 libpurple/hash.h                             |  168 ------------------
 libpurple/plugins/debug_example.c            |    2 +
 libpurple/plugins/helloworld.c               |    2 +
 libpurple/plugins/perl/Makefile.am           |    1 -
 libpurple/plugins/perl/common/Cipher.xs      |  105 +++++++++++
 libpurple/plugins/perl/common/Hash.xs        |  106 -----------
 libpurple/plugins/perl/common/MANIFEST       |    1 -
 libpurple/plugins/perl/common/Makefile.mingw |    1 -
 libpurple/plugins/perl/common/module.h       |    5 +-
 libpurple/protocols/jabber/auth_scram.h      |    2 +-
 libpurple/protocols/jabber/caps.h            |    2 +-
 libpurple/purple.h.in                        |    5 +-
 33 files changed, 482 insertions(+), 544 deletions(-)

diffs (truncated from 1415 to 300 lines):

diff --git a/libpurple/Makefile.am b/libpurple/Makefile.am
--- a/libpurple/Makefile.am
+++ b/libpurple/Makefile.am
@@ -75,7 +75,6 @@ purple_coresources = \
 	debug.c \
 	desktopitem.c \
 	eventloop.c \
-	hash.c \
 	http.c \
 	idle.c \
 	imgstore.c \
@@ -151,7 +150,6 @@ purple_coreheaders = \
 	debug.h \
 	desktopitem.h \
 	eventloop.h \
-	hash.h \
 	http.h \
 	idle.h \
 	imgstore.h \
diff --git a/libpurple/Makefile.mingw b/libpurple/Makefile.mingw
--- a/libpurple/Makefile.mingw
+++ b/libpurple/Makefile.mingw
@@ -70,7 +70,6 @@ C_SRC =	\
 			buddylist.c \
 			buddyicon.c \
 			certificate.c \
-			cipher.c \
 			ciphers/aescipher.c \
 			ciphers/descipher.c \
 			ciphers/des3cipher.c \
@@ -81,6 +80,7 @@ C_SRC =	\
 			ciphers/rc4cipher.c \
 			ciphers/sha1hash.c \
 			ciphers/sha256hash.c \
+			cipher.c \
 			circularbuffer.c \
 			cmds.c \
 			connection.c \
@@ -92,7 +92,6 @@ C_SRC =	\
 			dnsquery.c \
 			dnssrv.c \
 			eventloop.c \
-			hash.c \
 			http.c \
 			idle.c \
 			imgstore.c \
diff --git a/libpurple/cipher.c b/libpurple/cipher.c
--- a/libpurple/cipher.c
+++ b/libpurple/cipher.c
@@ -335,3 +335,220 @@ purple_cipher_get_block_size(PurpleCiphe
 
 	return -1;
 }
+
+/******************************************************************************
+ * PurpleHash API
+ *****************************************************************************/
+GType
+purple_hash_get_type(void) {
+	static GType type = 0;
+
+	if(type == 0) {
+		static const GTypeInfo info = {
+			sizeof(PurpleHashClass),
+			NULL,
+			NULL,
+			NULL,
+			NULL,
+			NULL,
+			sizeof(PurpleHash),
+			0,
+			NULL,
+			NULL
+		};
+
+		type = g_type_register_static(G_TYPE_OBJECT,
+									  "PurpleHash",
+									  &info, G_TYPE_FLAG_ABSTRACT);
+	}
+
+	return type;
+}
+
+/**
+ * purple_hash_reset:
+ * @hash: The hash to reset
+ *
+ * Resets a hash to it's default value
+ *
+ * @note If you have set an IV you will have to set it after resetting
+ */
+void
+purple_hash_reset(PurpleHash *hash) {
+	PurpleHashClass *klass = NULL;
+
+	g_return_if_fail(PURPLE_IS_HASH(hash));
+
+	klass = PURPLE_HASH_GET_CLASS(hash);
+
+	if(klass && klass->reset)
+		klass->reset(hash);
+	else
+		purple_debug_warning("hash", "the %s hash does not implement the "
+						"reset method\n",
+						g_type_name(G_TYPE_FROM_INSTANCE(hash)));
+}
+
+/**
+ * Resets a hash state to it's default value, but doesn't touch stateless
+ * configuration.
+ *
+ * That means, IV and digest context will be wiped out, but keys, ops or salt
+ * will remain untouched.
+ */
+void
+purple_hash_reset_state(PurpleHash *hash) {
+	PurpleHashClass *klass = NULL;
+
+	g_return_if_fail(PURPLE_IS_HASH(hash));
+
+	klass = PURPLE_HASH_GET_CLASS(hash);
+
+	if(klass && klass->reset_state)
+		klass->reset_state(hash);
+	else
+		purple_debug_warning("hash", "the %s hash does not implement the "
+						"reset_state method\n",
+						g_type_name(G_TYPE_FROM_INSTANCE(hash)));
+}
+
+/**
+ * purple_hash_append:
+ * @hash: The hash to append data to
+ * @data: The data to append
+ * @len: The length of the data
+ *
+ * Appends data to the hash
+ */
+void
+purple_hash_append(PurpleHash *hash, const guchar *data,
+								size_t len)
+{
+	PurpleHashClass *klass = NULL;
+
+	g_return_if_fail(PURPLE_IS_HASH(hash));
+
+	klass = PURPLE_HASH_GET_CLASS(hash);
+
+	if(klass && klass->append)
+		klass->append(hash, data, len);
+	else
+		purple_debug_warning("hash", "the %s hash does not implement the "
+						"append method\n",
+						g_type_name(G_TYPE_FROM_INSTANCE(hash)));
+}
+
+/**
+ * purple_hash_digest:
+ * @hash: The hash to digest
+ * @in_len: The length of the buffer
+ * @digest: The return buffer for the digest
+ * @out_len: The length of the returned value
+ *
+ * Digests a hash
+ *
+ * Return Value: TRUE if the digest was successful, FALSE otherwise.
+ */
+gboolean
+purple_hash_digest(PurpleHash *hash, guchar digest[], size_t len)
+{
+	PurpleHashClass *klass = NULL;
+
+	g_return_val_if_fail(PURPLE_IS_HASH(hash), FALSE);
+
+	klass = PURPLE_HASH_GET_CLASS(hash);
+
+	if(klass && klass->digest)
+		return klass->digest(hash, digest, len);
+	else
+		purple_debug_warning("hash", "the %s hash does not implement the "
+						"digest method\n",
+						g_type_name(G_TYPE_FROM_INSTANCE(hash)));
+
+	return FALSE;
+}
+
+/**
+ * purple_hash_digest_to_str:
+ * @hash: The hash to get a digest from
+ * @in_len: The length of the buffer
+ * @digest_s: The return buffer for the string digest
+ * @out_len: The length of the returned value
+ *
+ * Converts a guchar digest into a hex string
+ *
+ * Return Value: TRUE if the digest was successful, FALSE otherwise.
+ */
+gboolean
+purple_hash_digest_to_str(PurpleHash *hash, gchar digest_s[], size_t len)
+{
+	/* 8k is a bit excessive, will tweak later. */
+	guchar digest[BUF_LEN * 4];
+	size_t digest_size, n;
+
+	g_return_val_if_fail(PURPLE_IS_HASH(hash), FALSE);
+	g_return_val_if_fail(digest_s, FALSE);
+
+	digest_size = purple_hash_get_digest_size(hash);
+
+	g_return_val_if_fail(digest_size <= BUF_LEN * 4, FALSE);
+
+	if(!purple_hash_digest(hash, digest, sizeof(digest)))
+		return FALSE;
+
+	/* Every digest byte occupies 2 chars + the NUL at the end. */
+	g_return_val_if_fail(digest_size * 2 + 1 <= len, FALSE);
+
+	for(n = 0; n < digest_size; n++)
+		sprintf(digest_s + (n * 2), "%02x", digest[n]);
+
+	digest_s[n * 2] = '\0';
+
+	return TRUE;
+}
+
+size_t
+purple_hash_get_digest_size(PurpleHash *hash)
+{
+	PurpleHashClass *klass = NULL;
+
+	g_return_val_if_fail(PURPLE_IS_HASH(hash), FALSE);
+
+	klass = PURPLE_HASH_GET_CLASS(hash);
+
+	if(klass && klass->get_digest_size)
+		return klass->get_digest_size(hash);
+	else
+		purple_debug_warning("hash", "the %s hash does not implement the "
+						"get_digest_size method\n",
+						g_type_name(G_TYPE_FROM_INSTANCE(hash)));
+
+	return FALSE;
+}
+
+/**
+ * purple_hash_get_block_size:
+ * @hash: The hash whose block size to get
+ *
+ * Gets the block size of a hash
+ *
+ * Return Value: The block size of the hash
+ */
+size_t
+purple_hash_get_block_size(PurpleHash *hash)
+{
+	PurpleHashClass *klass = NULL;
+
+	g_return_val_if_fail(PURPLE_IS_HASH(hash), -1);
+
+	klass = PURPLE_HASH_GET_CLASS(hash);
+
+	if(klass && klass->get_block_size)
+		return klass->get_block_size(hash);
+	else
+		purple_debug_warning("hash", "the %s hash does not implement the "
+						"get_block_size method\n",
+						g_type_name(G_TYPE_FROM_INSTANCE(hash)));
+
+	return -1;
+}
diff --git a/libpurple/cipher.h b/libpurple/cipher.h
--- a/libpurple/cipher.h
+++ b/libpurple/cipher.h
@@ -1,7 +1,6 @@
 /**
- * @file cipher.h Purple Cipher API
+ * @file ciphers.h Purple Cipher and Hash API
  * @ingroup core
- * @see @ref cipher-signals
  */
 
 /* purple
@@ -24,15 +23,13 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
  */
-#ifndef PURPLE_CIPHER_H
-#define PURPLE_CIPHER_H
+#ifndef PURPLE_CIPHERS_H
+#define PURPLE_CIPHERS_H
 
 #include <glib.h>
 #include <glib-object.h>
 #include <string.h>
 
-#include "internal.h"
-
 #define PURPLE_TYPE_CIPHER				(purple_cipher_get_type())
 #define PURPLE_CIPHER(obj)				(G_TYPE_CHECK_INSTANCE_CAST((obj), PURPLE_TYPE_CIPHER, PurpleCipher))
 #define PURPLE_CIPHER_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST((klass), PURPLE_TYPE_CIPHER, PurpleCipherClass))



More information about the Commits mailing list