Replaced builder_t objects by simple builder_function_t functions

This commit is contained in:
Martin Willi
2009-09-10 16:20:17 +02:00
parent 3ce9438b60
commit f678f5c77e
4 changed files with 35 additions and 189 deletions
-8
View File
@@ -54,11 +54,3 @@ ENUM(builder_part_names, BUILD_FROM_FILE, BUILD_END,
"BUILD_END", "BUILD_END",
); );
/**
* See header.
*/
void* builder_free(builder_t *this)
{
free(this);
return NULL;
}
+10 -45
View File
@@ -21,16 +21,21 @@
#ifndef BUILDER_H_ #ifndef BUILDER_H_
#define BUILDER_H_ #define BUILDER_H_
typedef struct builder_t builder_t; #include <stdarg.h>
typedef enum builder_part_t builder_part_t; typedef enum builder_part_t builder_part_t;
/** /**
* Constructor function which creates a new builder instance. * Constructor function to build credentials.
* *
* @param subtype constructor specific subtype, e.g. certificate_type_t * Any added parts are cloned/refcounted by the builder implementation, a
* @return builder to construct a instance of type * caller may need to free the passed ressources themself.
*
* @param subtype constructor specific subtype, e.g. a certificate_type_t
* @param args list of builder part types, followed by parts, BUILD_END
* @return builder specific credential, NULL on error
*/ */
typedef builder_t* (*builder_constructor_t)(int subtype); typedef void* (*builder_function_t)(int subtype, va_list args);
#include <library.h> #include <library.h>
@@ -119,44 +124,4 @@ enum builder_part_t {
*/ */
extern enum_name_t *builder_part_names; extern enum_name_t *builder_part_names;
/**
* Credential construction API.
*
* The builder allows the construction of credentials in a generic and
* flexible way.
*/
struct builder_t {
/**
* Add a part to the construct.
*
* Any added parts are cloned/refcounted by the builder implementation, a
* caller may need to free the passed ressources themself.
*
* @param part kind of part
* @param ... part specific variable argument
*/
void (*add)(builder_t *this, builder_part_t part, ...);
/**
* Build the construct with all supplied parts.
*
* Once build() is called, the builder gets destroyed.
*
* @return specific interface, as requested with constructor.
*/
void* (*build)(builder_t *this);
};
/**
* Helper macro to cancel a build in a builder
*/
#define builder_cancel(builder) { (builder)->add = (void*)nop; \
(builder)->build = (void*)builder_free; }
/**
* Helper function for a cancelled build.
*/
void* builder_free(builder_t *this);
#endif /** BUILDER_H_ @}*/ #endif /** BUILDER_H_ @}*/
@@ -64,55 +64,16 @@ struct entry_t {
credential_type_t type; credential_type_t type;
/** subtype of credential, e.g. certificate_type_t */ /** subtype of credential, e.g. certificate_type_t */
int subtype; int subtype;
/** builder construction function */ /** builder function */
builder_constructor_t constructor; builder_function_t constructor;
}; };
/**
* type/subtype filter function for builder_enumerator
*/
static bool builder_filter(entry_t *data, entry_t **in, builder_t **out)
{
builder_t *builder;
if (data->type == (*in)->type &&
data->subtype == (*in)->subtype)
{
builder = (*in)->constructor(data->subtype);
if (builder)
{
*out = builder;
return TRUE;
}
}
return FALSE;
}
/**
* Implementation of credential_factory_t.create_builder_enumerator.
*/
static enumerator_t* create_builder_enumerator(
private_credential_factory_t *this, credential_type_t type, int subtype)
{
entry_t *data = malloc_thing(entry_t);
data->type = type;
data->subtype = subtype;
this->lock->read_lock(this->lock);
return enumerator_create_cleaner(
enumerator_create_filter(
this->constructors->create_enumerator(this->constructors),
(void*)builder_filter, data, free),
(void*)this->lock->unlock, this->lock);
}
/** /**
* Implementation of credential_factory_t.add_builder_constructor. * Implementation of credential_factory_t.add_builder_constructor.
*/ */
static void add_builder(private_credential_factory_t *this, static void add_builder(private_credential_factory_t *this,
credential_type_t type, int subtype, credential_type_t type, int subtype,
builder_constructor_t constructor) builder_function_t constructor)
{ {
entry_t *entry = malloc_thing(entry_t); entry_t *entry = malloc_thing(entry_t);
@@ -128,7 +89,7 @@ static void add_builder(private_credential_factory_t *this,
* Implementation of credential_factory_t.remove_builder. * Implementation of credential_factory_t.remove_builder.
*/ */
static void remove_builder(private_credential_factory_t *this, static void remove_builder(private_credential_factory_t *this,
builder_constructor_t constructor) builder_function_t constructor)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
entry_t *entry; entry_t *entry;
@@ -154,92 +115,34 @@ static void* create(private_credential_factory_t *this, credential_type_t type,
int subtype, ...) int subtype, ...)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
builder_t *builder; entry_t *entry;
builder_part_t part;
va_list args; va_list args;
void* construct = NULL, *fn, *data; void *construct = NULL;
int failures = 0; int failures = 0;
uintptr_t level; uintptr_t level;
level = (uintptr_t)pthread_getspecific(this->recursive); level = (uintptr_t)pthread_getspecific(this->recursive);
pthread_setspecific(this->recursive, (void*)level + 1); pthread_setspecific(this->recursive, (void*)level + 1);
enumerator = create_builder_enumerator(this, type, subtype); this->lock->read_lock(this->lock);
while (enumerator->enumerate(enumerator, &builder)) enumerator = this->constructors->create_enumerator(this->constructors);
while (enumerator->enumerate(enumerator, &entry))
{ {
va_start(args, subtype); if (entry->type == type && entry->subtype == subtype)
while (TRUE)
{ {
part = va_arg(args, builder_part_t); va_start(args, subtype);
switch (part) construct = entry->constructor(subtype, args);
va_end(args);
if (construct)
{ {
case BUILD_END: break;
break;
case BUILD_BLOB_PEM:
case BUILD_BLOB_ASN1_DER:
case BUILD_BLOB_PGP:
case BUILD_BLOB_DNSKEY:
case BUILD_PASSPHRASE:
case BUILD_SERIAL:
case BUILD_RSA_MODULUS:
case BUILD_RSA_PUB_EXP:
case BUILD_RSA_PRIV_EXP:
case BUILD_RSA_PRIME1:
case BUILD_RSA_PRIME2:
case BUILD_RSA_EXP1:
case BUILD_RSA_EXP2:
case BUILD_RSA_COEFF:
builder->add(builder, part, va_arg(args, chunk_t));
continue;
case BUILD_X509_FLAG:
builder->add(builder, part, va_arg(args, x509_flag_t));
continue;
case BUILD_KEY_SIZE:
case BUILD_FROM_FD:
builder->add(builder, part, va_arg(args, u_int));
continue;
case BUILD_DIGEST_ALG:
builder->add(builder, part, va_arg(args, int));
continue;
case BUILD_NOT_BEFORE_TIME:
case BUILD_NOT_AFTER_TIME:
builder->add(builder, part, va_arg(args, time_t));
continue;
case BUILD_FROM_FILE:
case BUILD_AGENT_SOCKET:
case BUILD_SIGNING_KEY:
case BUILD_PUBLIC_KEY:
case BUILD_SUBJECT:
case BUILD_SUBJECT_ALTNAMES:
case BUILD_ISSUER:
case BUILD_ISSUER_ALTNAMES:
case BUILD_SIGNING_CERT:
case BUILD_CA_CERT:
case BUILD_CERT:
case BUILD_IETF_GROUP_ATTR:
case BUILD_SMARTCARD_KEYID:
case BUILD_SMARTCARD_PIN:
builder->add(builder, part, va_arg(args, void*));
continue;
case BUILD_PASSPHRASE_CALLBACK:
fn = va_arg(args, void*);
data = va_arg(args, void*);
builder->add(builder, part, fn, data);
continue;
/* no default to get a compiler warning */
} }
break; failures++;
} }
va_end(args);
construct = builder->build(builder);
if (construct)
{
break;
}
failures++;
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
if (!construct && !level) if (!construct && !level)
{ {
enum_name_t *names = key_type_names; enum_name_t *names = key_type_names;
@@ -274,9 +177,8 @@ credential_factory_t *credential_factory_create()
private_credential_factory_t *this = malloc_thing(private_credential_factory_t); private_credential_factory_t *this = malloc_thing(private_credential_factory_t);
this->public.create = (void*(*)(credential_factory_t*, credential_type_t type, int subtype, ...))create; this->public.create = (void*(*)(credential_factory_t*, credential_type_t type, int subtype, ...))create;
this->public.create_builder_enumerator = (enumerator_t*(*)(credential_factory_t*, credential_type_t type, int subtype))create_builder_enumerator; this->public.add_builder = (void(*)(credential_factory_t*,credential_type_t type, int subtype, builder_function_t constructor))add_builder;
this->public.add_builder = (void(*)(credential_factory_t*,credential_type_t type, int subtype, builder_constructor_t constructor))add_builder; this->public.remove_builder = (void(*)(credential_factory_t*,builder_function_t constructor))remove_builder;
this->public.remove_builder = (void(*)(credential_factory_t*,builder_constructor_t constructor))remove_builder;
this->public.destroy = (void(*)(credential_factory_t*))destroy; this->public.destroy = (void(*)(credential_factory_t*))destroy;
this->constructors = linked_list_create(); this->constructors = linked_list_create();
@@ -53,7 +53,7 @@ struct credential_factory_t {
* *
* The variable argument list takes builder_part_t types followed * The variable argument list takes builder_part_t types followed
* by the type specific value. The list must be terminated using BUILD_END. * by the type specific value. The list must be terminated using BUILD_END.
* All passed parts get cloned/refcounted by the builder implementations, * All passed parts get cloned/refcounted by the builder functions,
* so free up allocated ressources after successful and unsuccessful * so free up allocated ressources after successful and unsuccessful
* invocations. * invocations.
* *
@@ -66,34 +66,21 @@ struct credential_factory_t {
int subtype, ...); int subtype, ...);
/** /**
* Create an enumerator for a builder type. * Register a credential builder function.
*
* The build() method has to be called on each enumerated builder to
* cleanup associated ressources.
*
* @param type type of credentials the builder creates
* @param subtype type specific subtype, such as certificate_type_t
* @return enumerator over builder_t
*/
enumerator_t* (*create_builder_enumerator)(credential_factory_t *this,
credential_type_t type, int subtype);
/**
* Register a builder_t constructor function.
* *
* @param type type of credential the builder creates * @param type type of credential the builder creates
* @param constructor builder constructor function to register * @param constructor builder constructor function to register
*/ */
void (*add_builder)(credential_factory_t *this, void (*add_builder)(credential_factory_t *this,
credential_type_t type, int subtype, credential_type_t type, int subtype,
builder_constructor_t constructor); builder_function_t constructor);
/** /**
* Unregister a builder_t constructor function. * Unregister a credential builder function.
* *
* @param constructor constructor function to unregister. * @param constructor constructor function to unregister.
*/ */
void (*remove_builder)(credential_factory_t *this, void (*remove_builder)(credential_factory_t *this,
builder_constructor_t constructor); builder_function_t constructor);
/** /**
* Destroy a credential_factory instance. * Destroy a credential_factory instance.