[gmime] Added a registry for GMimeCryptoContext's



commit 158319f53ba7b3f515b091a1ea3bdf948aaef505
Author: Jeffrey Stedfast <jestedfa microsoft com>
Date:   Sat Feb 18 10:09:21 2017 -0500

    Added a registry for GMimeCryptoContext's

 gmime/gmime-crypto-context.c |   54 ++++++++++++++++++++++++++++++++++++++++++
 gmime/gmime-crypto-context.h |   13 ++++++++++
 gmime/gmime-gpg-context.c    |    9 +-----
 gmime/gmime-gpg-context.h    |    2 +-
 gmime/gmime-pkcs7-context.c  |    9 +-----
 gmime/gmime-pkcs7-context.h  |    2 +-
 gmime/gmime.c                |   14 +++++++++++
 tests/test-pgp.c             |    3 +-
 tests/test-pgpmime.c         |    4 ++-
 tests/test-pkcs7.c           |    3 +-
 tests/test-smime.c           |    3 +-
 11 files changed, 96 insertions(+), 20 deletions(-)
---
diff --git a/gmime/gmime-crypto-context.c b/gmime/gmime-crypto-context.c
index 0853a43..d0038b3 100644
--- a/gmime/gmime-crypto-context.c
+++ b/gmime/gmime-crypto-context.c
@@ -26,6 +26,7 @@
 #include <string.h>
 
 #include "gmime-crypto-context.h"
+#include "gmime-common.h"
 #include "gmime-error.h"
 
 
@@ -86,6 +87,8 @@ static int crypto_export_keys (GMimeCryptoContext *ctx, const char *keys[],
                               GMimeStream *ostream, GError **err);
 
 
+static GHashTable *type_hash = NULL;
+
 static GObjectClass *parent_class = NULL;
 
 
@@ -108,6 +111,8 @@ g_mime_crypto_context_get_type (void)
                };
                
                type = g_type_register_static (G_TYPE_OBJECT, "GMimeCryptoContext", &info, 0);
+               
+               type_hash = g_hash_table_new_full (g_mime_strcase_hash, g_mime_strcase_equal, g_free, NULL);
        }
        
        return type;
@@ -154,6 +159,55 @@ g_mime_crypto_context_finalize (GObject *object)
 }
 
 
+void
+g_mime_crypto_context_shutdown (void)
+{
+       g_hash_table_destroy (type_hash);
+       type_hash = NULL;
+}
+
+
+/**
+ * g_mime_crypto_context_register:
+ * @protocol: crypto protocol
+ * @callback: a #GMimeCryptoContextNewFunc
+ *
+ * Registers the callback for the specified @protocol.
+ **/
+void
+g_mime_crypto_context_register (const char *protocol, GMimeCryptoContextNewFunc callback)
+{
+       GMimeCryptoContextNewFunc func;
+       
+       g_return_if_fail (protocol != NULL);
+       g_return_if_fail (callback != NULL);
+       
+       g_hash_table_replace (type_hash, g_strdup (protocol), callback);
+}
+
+
+/**
+ * g_mime_crypto_context_new:
+ * @protocol: the crypto protocol
+ *
+ * Creates a new crypto context for the specified @protocol.
+ *
+ * Returns: a newly allocated #GMimeCryptoContext.
+ **/
+GMimeCryptoContext *
+g_mime_crypto_context_new (const char *protocol)
+{
+       GMimeCryptoContextNewFunc func;
+       
+       g_return_val_if_fail (protocol != NULL, NULL);
+       
+       if (!(func = g_hash_table_lookup (type_hash, protocol)))
+               return NULL;
+       
+       return func ();
+}
+
+
 /**
  * g_mime_crypto_context_set_request_password:
  * @ctx: a #GMimeCryptoContext
diff --git a/gmime/gmime-crypto-context.h b/gmime/gmime-crypto-context.h
index 1dbf660..65b92bd 100644
--- a/gmime/gmime-crypto-context.h
+++ b/gmime/gmime-crypto-context.h
@@ -67,6 +67,16 @@ typedef gboolean (* GMimePasswordRequestFunc) (GMimeCryptoContext *ctx, const ch
 
 
 /**
+ * GMimeCryptoContextNewFunc:
+ *
+ * A callback used to create a new instance of a #GMimeCryptoContext subclass.
+ *
+ * Returns: a new #GMimeCryptoContext instance.
+ **/
+typedef GMimeCryptoContext * (* GMimeCryptoContextNewFunc) (void);
+
+
+/**
  * GMimeCryptoContext:
  * @parent_object: parent #GObject
  * @request_passwd: a callback for requesting a password
@@ -128,6 +138,9 @@ struct _GMimeCryptoContextClass {
 
 GType g_mime_crypto_context_get_type (void);
 
+void g_mime_crypto_context_register (const char *protocol, GMimeCryptoContextNewFunc callback);
+GMimeCryptoContext *g_mime_crypto_context_new (const char *protocol);
+
 void g_mime_crypto_context_set_request_password (GMimeCryptoContext *ctx, GMimePasswordRequestFunc 
request_passwd);
 
 /* digest algo mapping */
diff --git a/gmime/gmime-gpg-context.c b/gmime/gmime-gpg-context.c
index 9db3857..ebf68a4 100644
--- a/gmime/gmime-gpg-context.c
+++ b/gmime/gmime-gpg-context.c
@@ -947,17 +947,15 @@ gpg_set_always_trust (GMimeCryptoContext *context, gboolean always_trust)
 
 /**
  * g_mime_gpg_context_new:
- * @request_passwd: a #GMimePasswordRequestFunc
  *
  * Creates a new gpg crypto context object.
  *
  * Returns: (transfer full): a new gpg crypto context object.
  **/
 GMimeCryptoContext *
-g_mime_gpg_context_new (GMimePasswordRequestFunc request_passwd)
+g_mime_gpg_context_new (void)
 {
 #ifdef ENABLE_CRYPTO
-       GMimeCryptoContext *crypto;
        GMimeGpgContext *gpg;
        gpgme_ctx_t ctx;
        
@@ -975,10 +973,7 @@ g_mime_gpg_context_new (GMimePasswordRequestFunc request_passwd)
        gpgme_set_armor (ctx, TRUE);
        gpg->ctx = ctx;
        
-       crypto = (GMimeCryptoContext *) gpg;
-       crypto->request_passwd = request_passwd;
-       
-       return crypto;
+       return (GMimeCryptoContext *) gpg;
 #else
        return NULL;
 #endif /* ENABLE_CRYPTO */
diff --git a/gmime/gmime-gpg-context.h b/gmime/gmime-gpg-context.h
index dae61ab..6b255d6 100644
--- a/gmime/gmime-gpg-context.h
+++ b/gmime/gmime-gpg-context.h
@@ -40,7 +40,7 @@ typedef struct _GMimeGpgContextClass GMimeGpgContextClass;
 GType g_mime_gpg_context_get_type (void);
 
 
-GMimeCryptoContext *g_mime_gpg_context_new (GMimePasswordRequestFunc request_passwd);
+GMimeCryptoContext *g_mime_gpg_context_new (void);
 
 gboolean g_mime_gpg_context_get_auto_key_retrieve (GMimeGpgContext *ctx);
 void g_mime_gpg_context_set_auto_key_retrieve (GMimeGpgContext *ctx, gboolean auto_key_retrieve);
diff --git a/gmime/gmime-pkcs7-context.c b/gmime/gmime-pkcs7-context.c
index 77785dd..c154409 100644
--- a/gmime/gmime-pkcs7-context.c
+++ b/gmime/gmime-pkcs7-context.c
@@ -887,17 +887,15 @@ pkcs7_set_always_trust (GMimeCryptoContext *context, gboolean always_trust)
 
 /**
  * g_mime_pkcs7_context_new:
- * @request_passwd: a #GMimePasswordRequestFunc
  *
  * Creates a new pkcs7 crypto context object.
  *
  * Returns: (transfer full): a new pkcs7 crypto context object.
  **/
 GMimeCryptoContext *
-g_mime_pkcs7_context_new (GMimePasswordRequestFunc request_passwd)
+g_mime_pkcs7_context_new (void)
 {
 #ifdef ENABLE_CRYPTO
-       GMimeCryptoContext *crypto;
        GMimePkcs7Context *pkcs7;
        gpgme_ctx_t ctx;
        
@@ -914,10 +912,7 @@ g_mime_pkcs7_context_new (GMimePasswordRequestFunc request_passwd)
        gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
        pkcs7->ctx = ctx;
        
-       crypto = (GMimeCryptoContext *) pkcs7;
-       crypto->request_passwd = request_passwd;
-       
-       return crypto;
+       return (GMimeCryptoContext *) pkcs7;
 #else
        return NULL;
 #endif /* ENABLE_CRYPTO */
diff --git a/gmime/gmime-pkcs7-context.h b/gmime/gmime-pkcs7-context.h
index 67918a5..34a2352 100644
--- a/gmime/gmime-pkcs7-context.h
+++ b/gmime/gmime-pkcs7-context.h
@@ -38,7 +38,7 @@ typedef struct _GMimePkcs7ContextClass GMimePkcs7ContextClass;
 
 GType g_mime_pkcs7_context_get_type (void);
 
-GMimeCryptoContext *g_mime_pkcs7_context_new (GMimePasswordRequestFunc request_passwd);
+GMimeCryptoContext *g_mime_pkcs7_context_new (void);
 
 gboolean g_mime_pkcs7_context_get_always_trust (GMimePkcs7Context *ctx);
 void g_mime_pkcs7_context_set_always_trust (GMimePkcs7Context *ctx, gboolean always_trust);
diff --git a/gmime/gmime.c b/gmime/gmime.c
index b174c05..81eb621 100644
--- a/gmime/gmime.c
+++ b/gmime/gmime.c
@@ -48,6 +48,7 @@
  * Initialization, shutdown, and version-check functions.
  **/
 
+extern void g_mime_crypto_context_shutdown (void);
 extern void g_mime_iconv_utils_shutdown (void);
 extern void g_mime_iconv_utils_init (void);
 
@@ -193,6 +194,18 @@ g_mime_init (void)
        g_mime_object_register_type ("message", "rfc2822", g_mime_message_part_get_type ());
        g_mime_object_register_type ("message", "news", g_mime_message_part_get_type ());
        g_mime_object_register_type ("message", "partial", g_mime_message_partial_get_type ());
+       
+       g_mime_crypto_context_register ("application/x-pgp-signature", g_mime_gpg_context_new);
+       g_mime_crypto_context_register ("application/pgp-signature", g_mime_gpg_context_new);
+       g_mime_crypto_context_register ("application/x-pgp-encrypted", g_mime_gpg_context_new);
+       g_mime_crypto_context_register ("application/pgp-encrypted", g_mime_gpg_context_new);
+       g_mime_crypto_context_register ("application/pgp-keys", g_mime_gpg_context_new);
+       
+       g_mime_crypto_context_register ("application/x-pkcs7-signature", g_mime_pkcs7_context_new);
+       g_mime_crypto_context_register ("application/pkcs7-signature", g_mime_pkcs7_context_new);
+       g_mime_crypto_context_register ("application/x-pkcs7-mime", g_mime_pkcs7_context_new);
+       g_mime_crypto_context_register ("application/pkcs7-mime", g_mime_pkcs7_context_new);
+       g_mime_crypto_context_register ("application/pkcs7-keys", g_mime_pkcs7_context_new);
 }
 
 
@@ -209,6 +222,7 @@ g_mime_shutdown (void)
                return;
        
        g_mime_object_type_registry_shutdown ();
+       g_mime_crypto_context_shutdown ();
        g_mime_parser_options_shutdown ();
        g_mime_charset_map_shutdown ();
        g_mime_iconv_utils_shutdown ();
diff --git a/tests/test-pgp.c b/tests/test-pgp.c
index 9294074..1167ab6 100644
--- a/tests/test-pgp.c
+++ b/tests/test-pgp.c
@@ -311,7 +311,8 @@ int main (int argc, char **argv)
        
        testsuite_start ("GnuPG crypto context");
        
-       ctx = g_mime_gpg_context_new (request_passwd);
+       ctx = g_mime_gpg_context_new ();
+       g_mime_crypto_context_set_request_password (ctx, request_passwd);
        g_mime_crypto_context_set_always_trust (ctx, TRUE);
        
        testsuite_check ("GMimeGpgContext::import");
diff --git a/tests/test-pgpmime.c b/tests/test-pgpmime.c
index 1002ff0..4403a17 100644
--- a/tests/test-pgpmime.c
+++ b/tests/test-pgpmime.c
@@ -482,8 +482,10 @@ int main (int argc, char *argv[])
        
        testsuite_start ("PGP/MIME implementation");
        
-       ctx = g_mime_gpg_context_new (request_passwd);
+       ctx = g_mime_gpg_context_new ();
+       g_mime_crypto_context_set_request_password (ctx, request_passwd);
        g_mime_crypto_context_set_always_trust (ctx, TRUE);
+       
        if (g_mime_crypto_context_set_retrieve_session_key (ctx, TRUE, &err) != 0) {
                fprintf (stderr, "Failed to set retrieve_session_key on GMimeGpgContext: %s\n",
                         err ? err->message : "no error info returned" );
diff --git a/tests/test-pkcs7.c b/tests/test-pkcs7.c
index 32a8075..5907100 100644
--- a/tests/test-pkcs7.c
+++ b/tests/test-pkcs7.c
@@ -312,7 +312,8 @@ int main (int argc, char **argv)
        
        testsuite_start ("Pkcs7 crypto context");
        
-       ctx = g_mime_pkcs7_context_new (request_passwd);
+       ctx = g_mime_pkcs7_context_new ();
+       g_mime_crypto_context_set_request_password (ctx, request_passwd);
        g_mime_crypto_context_set_always_trust (ctx, TRUE);
        
        testsuite_check ("GMimePkcs7Context::import");
diff --git a/tests/test-smime.c b/tests/test-smime.c
index 957e96a..d8eecb7 100644
--- a/tests/test-smime.c
+++ b/tests/test-smime.c
@@ -452,7 +452,8 @@ int main (int argc, char *argv[])
        
        testsuite_start ("S/MIME implementation");
        
-       ctx = g_mime_pkcs7_context_new (request_passwd);
+       ctx = g_mime_pkcs7_context_new ();
+       g_mime_crypto_context_set_request_password (ctx, request_passwd);
        g_mime_crypto_context_set_always_trust (ctx, TRUE);
        
        if (g_mime_crypto_context_set_retrieve_session_key (ctx, TRUE, NULL) == 0) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]