Commit 00905c00 authored by Dafydd Harries's avatar Dafydd Harries

add random number generation code

darcs-hash:20070131115247-c9803-7ab718af8e311c0c37df2aa92343a6bdc1b9d30c.gz
parent 8c989834
SUBDIRS = local stun tests udp agent nice
SUBDIRS = local stun tests udp agent random nice
EXTRA_DIST = scripts
......@@ -12,6 +12,7 @@ AC_CONFIG_FILES([
local/Makefile
udp/Makefile
nice/Makefile
random/Makefile
])
# Checks for programs.
......
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)
#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;
}
#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 */
#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)];
}
#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
#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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment