Migrated random plugin to INIT/METHOD macros

This commit is contained in:
Andreas Steffen 2010-12-04 11:37:03 +01:00
parent 79bbe64e17
commit 1bb67ff852
2 changed files with 26 additions and 25 deletions

View File

@ -31,10 +31,8 @@ struct private_random_plugin_t {
random_plugin_t public; random_plugin_t public;
}; };
/** METHOD(plugin_t, destroy, void,
* Implementation of random_plugin_t.gmptroy private_random_plugin_t *this)
*/
static void destroy(private_random_plugin_t *this)
{ {
lib->crypto->remove_rng(lib->crypto, lib->crypto->remove_rng(lib->crypto,
(rng_constructor_t)random_rng_create); (rng_constructor_t)random_rng_create);
@ -46,9 +44,15 @@ static void destroy(private_random_plugin_t *this)
*/ */
plugin_t *random_plugin_create() plugin_t *random_plugin_create()
{ {
private_random_plugin_t *this = malloc_thing(private_random_plugin_t); private_random_plugin_t *this;
this->public.plugin.destroy = (void(*)(plugin_t*))destroy; INIT(this,
.public = {
.plugin = {
.destroy = _destroy,
},
},
);
lib->crypto->add_rng(lib->crypto, RNG_STRONG, lib->crypto->add_rng(lib->crypto, RNG_STRONG,
(rng_constructor_t)random_rng_create); (rng_constructor_t)random_rng_create);

View File

@ -55,11 +55,8 @@ struct private_random_rng_t {
char *file; char *file;
}; };
/** METHOD(rng_t, get_bytes, void,
* Implementation of random_rng_t.get_bytes. private_random_rng_t *this, size_t bytes, u_int8_t *buffer)
*/
static void get_bytes(private_random_rng_t *this, size_t bytes,
u_int8_t *buffer)
{ {
size_t done; size_t done;
ssize_t got; ssize_t got;
@ -81,20 +78,15 @@ static void get_bytes(private_random_rng_t *this, size_t bytes,
} }
} }
/** METHOD(rng_t, allocate_bytes, void,
* Implementation of random_rng_t.allocate_bytes. private_random_rng_t *this, size_t bytes, chunk_t *chunk)
*/
static void allocate_bytes(private_random_rng_t *this, size_t bytes,
chunk_t *chunk)
{ {
*chunk = chunk_alloc(bytes); *chunk = chunk_alloc(bytes);
get_bytes(this, chunk->len, chunk->ptr); get_bytes(this, chunk->len, chunk->ptr);
} }
/** METHOD(rng_t, destroy, void,
* Implementation of random_rng_t.destroy. private_random_rng_t *this)
*/
static void destroy(private_random_rng_t *this)
{ {
close(this->dev); close(this->dev);
free(this); free(this);
@ -105,12 +97,17 @@ static void destroy(private_random_rng_t *this)
*/ */
random_rng_t *random_rng_create(rng_quality_t quality) random_rng_t *random_rng_create(rng_quality_t quality)
{ {
private_random_rng_t *this = malloc_thing(private_random_rng_t); private_random_rng_t *this;
/* public functions */ INIT(this,
this->public.rng.get_bytes = (void (*) (rng_t *, size_t, u_int8_t*)) get_bytes; .public = {
this->public.rng.allocate_bytes = (void (*) (rng_t *, size_t, chunk_t*)) allocate_bytes; .rng = {
this->public.rng.destroy = (void (*) (rng_t *))destroy; .get_bytes = _get_bytes,
.allocate_bytes = _allocate_bytes,
.destroy = _destroy,
},
},
);
if (quality == RNG_TRUE) if (quality == RNG_TRUE)
{ {