diff options
Diffstat (limited to 'random')
-rw-r--r-- | random/Makefile.am | 17 | ||||
-rw-r--r-- | random/random-glib.c | 43 | ||||
-rw-r--r-- | random/random-glib.h | 17 | ||||
-rw-r--r-- | random/random.c | 39 | ||||
-rw-r--r-- | random/random.h | 40 | ||||
-rw-r--r-- | random/test.c | 19 |
6 files changed, 175 insertions, 0 deletions
diff --git a/random/Makefile.am b/random/Makefile.am new file mode 100644 index 0000000..38e2841 --- /dev/null +++ b/random/Makefile.am @@ -0,0 +1,17 @@ + +AM_CFLAGS = -Wall -Werror $(GLIB_CFLAGS) + +noinst_LTLIBRARIES = librandom.la + +librandom_la_SOURCES = \ + random.h \ + random.c \ + random-glib.h \ + random-glib.c + +check_PROGRAMS = test + +test_LDADD = $(GLIB_LIBS) librandom.la + +TESTS = $(check_PROGRAMS) + diff --git a/random/random-glib.c b/random/random-glib.c new file mode 100644 index 0000000..420f5d3 --- /dev/null +++ b/random/random-glib.c @@ -0,0 +1,43 @@ + +#include "random-glib.h" + +static void +rng_seed (NiceRNG *rng, guint32 seed) +{ + g_random_set_seed (seed); +} + +static void +rng_generate_bytes (NiceRNG *rng, guint len, gchar *buf) +{ + guint i; + + for (i = 0; i < len; i++) + buf[i] = g_random_int_range (0, 256); +} + +static guint +rng_generate_int (NiceRNG *rng, guint low, guint high) +{ + return g_random_int_range (low, high); +} + +static void +rng_free (NiceRNG *rng) +{ + g_slice_free (NiceRNG, rng); +} + +NiceRNG * +nice_glib_rng_new (void) +{ + NiceRNG *ret; + + ret = g_slice_new0 (NiceRNG); + ret->seed = rng_seed; + ret->generate_bytes = rng_generate_bytes; + ret->generate_int = rng_generate_int; + ret->free = rng_free; + return ret; +} + diff --git a/random/random-glib.h b/random/random-glib.h new file mode 100644 index 0000000..22e53ed --- /dev/null +++ b/random/random-glib.h @@ -0,0 +1,17 @@ + +#ifndef _RANDOM_GLIB_H +#define _RANDOM_GLIB_H + +#include <glib.h> + +#include "random.h" + +G_BEGIN_DECLS + +NiceRNG * +nice_glib_rng_new (void); + +G_END_DECLS + +#endif /* _RANDOM_GLIB_H */ + diff --git a/random/random.c b/random/random.c new file mode 100644 index 0000000..321f863 --- /dev/null +++ b/random/random.c @@ -0,0 +1,39 @@ + +#include "random.h" +#include "random-glib.h" + +NiceRNG * +nice_rng_new () +{ + NiceRNG *rng; + + rng = nice_glib_rng_new (); + rng->seed (rng, 0); + return rng; +} + +void +nice_rng_generate_bytes (NiceRNG *rng, guint len, gchar *buf) +{ + rng->generate_bytes (rng, len, buf); +} + +guint +nice_rng_generate_int (NiceRNG *rng, guint low, guint high) +{ + return rng->generate_int (rng, low, high); +} + +void +nice_rng_generate_bytes_print (NiceRNG *rng, guint len, gchar *buf) +{ + guint i; + gchar *chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "01234567890"; + + for (i = 0; i < len; i++) + buf[i] = chars[nice_rng_generate_int (rng, 0, 62)]; +} + diff --git a/random/random.h b/random/random.h new file mode 100644 index 0000000..2e31e63 --- /dev/null +++ b/random/random.h @@ -0,0 +1,40 @@ + +#ifndef _RANDOM_H +#define _RANDOM_H + +#include <glib.h> + +G_BEGIN_DECLS + +typedef struct _NiceRNG NiceRNG; + +struct _NiceRNG { + void (*seed) (NiceRNG *src, guint32 seed); + void (*generate_bytes) (NiceRNG *src, guint len, gchar *buf); + guint (*generate_int) (NiceRNG *src, guint low, guint high); + void (*free) (NiceRNG *src); + gpointer priv; +}; + +NiceRNG * +nice_rng_new (void); + +void +nice_rng_seed (NiceRNG *rng, guint32 seed); + +void +nice_rng_generate_bytes (NiceRNG *rng, guint len, gchar *buf); + +void +nice_rng_generate_bytes_print (NiceRNG *rng, guint len, gchar *buf); + +guint +nice_rng_generate_int (NiceRNG *rng, guint low, guint high); + +void +nice_rng_free (NiceRNG *rng); + +G_END_DECLS + +#endif // _RANDOM_H + diff --git a/random/test.c b/random/test.c new file mode 100644 index 0000000..8c59938 --- /dev/null +++ b/random/test.c @@ -0,0 +1,19 @@ + +#include <string.h> + +#include "random.h" + +int +main (void) +{ + NiceRNG *rng; + gchar buf[9]; + + rng = nice_rng_new (); + nice_rng_generate_bytes_print (rng, 8, buf); + buf[8] = '\0'; + //g_debug ("%s", buf); + g_assert (0 == strcmp (buf, "S9PObXR5")); + return 0; +} + |