STAILQify struct CryptModules 800/head
authorPietro Cerutti <gahr@gahr.ch>
Fri, 29 Sep 2017 10:22:38 +0000 (10:22 +0000)
committerRichard Russon <rich@flatcap.org>
Fri, 29 Sep 2017 12:59:38 +0000 (13:59 +0100)
ncrypt/crypt_mod.c
ncrypt/crypt_mod.h

index b62cdec7be224404a5d1a7301a2d9f9376403bed..73a1cc68fd5d39033a541070e7f746cdddf8f040 100644 (file)
 #include "config.h"
 #include "lib/lib.h"
 #include "crypt_mod.h"
+#include "queue.h"
 
 /**
  * struct CryptModule - A crypto plugin module
  *
  * A type of a variable to keep track of registered crypto modules.
  */
+
+STAILQ_HEAD(CryptModules, CryptModule)
+modules = STAILQ_HEAD_INITIALIZER(modules);
 struct CryptModule
 {
-  crypt_module_specs_t specs;
-  struct CryptModule *next, **prevp;
+  struct CryptModuleSpecs *specs;
+  STAILQ_ENTRY(CryptModule) entries;
 };
 
-static struct CryptModule *modules;
-
 /**
  * crypto_module_register - Register a new crypto module
  */
-void crypto_module_register(crypt_module_specs_t specs)
+void crypto_module_register(struct CryptModuleSpecs *specs)
 {
-  struct CryptModule *module_new = safe_malloc(sizeof(*module_new));
-
-  module_new->specs = specs;
-  module_new->next = modules;
-  if (modules)
-    modules->prevp = &module_new->next;
-  modules = module_new;
+  struct CryptModule *module = safe_calloc(1, sizeof(struct CryptModule));
+  module->specs = specs;
+  STAILQ_INSERT_HEAD(&modules, module, entries);
 }
 
 /**
@@ -57,12 +55,15 @@ void crypto_module_register(crypt_module_specs_t specs)
  * Return the crypto module specs for IDENTIFIER.
  * This function is usually used via the CRYPT_MOD_CALL[_CHECK] macros.
  */
-crypt_module_specs_t crypto_module_lookup(int identifier)
+struct CryptModuleSpecs *crypto_module_lookup(int identifier)
 {
-  struct CryptModule *module = modules;
-
-  while (module && (module->specs->identifier != identifier))
-    module = module->next;
-
-  return module ? module->specs : NULL;
+  struct CryptModule *module = NULL;
+  STAILQ_FOREACH(module, &modules, entries)
+  {
+    if (module->specs->identifier == identifier)
+    {
+      return module->specs;
+    }
+  }
+  return NULL;
 }
index 26bd2cc170fc903a9977f3d594237213428bcf0e..1f4dfc84798623fb00aeca36be8a2773343a0059 100644 (file)
@@ -74,7 +74,7 @@ typedef void (*crypt_func_set_sender_t)(const char *sender);
  *
  * A structure to keep all crypto module functions together.
  */
-typedef struct CryptModuleFunctions
+struct CryptModuleFunctions
 {
   /* Common/General functions.  */
   crypt_func_init_t                init;
@@ -103,25 +103,25 @@ typedef struct CryptModuleFunctions
   crypt_func_smime_verify_sender_t      smime_verify_sender;
   crypt_func_smime_build_smime_entity_t smime_build_smime_entity;
   crypt_func_smime_invoke_import_t      smime_invoke_import;
-} crypt_module_functions_t;
+};
 
 /**
  * struct CryptModuleSpecs - Crypto API
  *
  * A structure to describe a crypto module.
  */
-typedef struct CryptModuleSpecs
+struct CryptModuleSpecs
 {
   int identifier; /**< Identifying bit */
-  crypt_module_functions_t functions;
-} * crypt_module_specs_t;
+  struct CryptModuleFunctions functions;
+};
 
 /*
    High Level crypto module interface.
  */
 
-void crypto_module_register(crypt_module_specs_t specs);
-crypt_module_specs_t crypto_module_lookup(int identifier);
+void crypto_module_register(struct CryptModuleSpecs *specs);
+struct CryptModuleSpecs *crypto_module_lookup(int identifier);
 
 /** If the crypto module identifier by IDENTIFIER has been registered,
    call its function FUNC.  Do nothing else.  This may be used as an