commit 8b8512d425f039b9391eeebbc51ab9e1b63cc293
parent 1a7da42e0adac827e83507d10d4f1bfc72f5c6b8
Author: nolash <dev@holbrook.no>
Date: Sun, 9 Feb 2020 21:23:56 +0100
Add random data generator
Diffstat:
4 files changed, 49 insertions(+), 3 deletions(-)
diff --git a/Makefile.am b/Makefile.am
@@ -1,12 +1,16 @@
AM_CFLAGS = -I$(top_srcdir)/include
-noinst_LTLIBRARIES = liblash.la libbtree.la libdebug.la
-liblash_la_SOURCES = hex.c endian.c tree.c
+noinst_LTLIBRARIES = liblash.la libbtree.la libdebug.la liblashtest.la
+
+liblash_la_SOURCES = hex.c endian.c tree.c
liblash_la_CFLAGS = -I./include
libbtree_la_SOURCES = tree.c
libbtree_la_CFLAGS = -I./include
+liblashtest_la_SOURCES = test.c
+liblashtest_la_CFLAGS = -I./include
+
libdebug_la_SOURCES = debug_log.c debug_timer.c
libdebug_la_CFLAGS = -I./include
diff --git a/hex.c b/hex.c
@@ -56,7 +56,7 @@ size_t hex2bin(const char *hex, unsigned char *out) {
return 0;
len /= 2;
- //memset(out, 'A', len);
+ memset(out, 'A', len);
for (i=0; i<len; i++) {
if (!hexchr2bin(hex[i*2], &b1) || !hexchr2bin(hex[i*2+1], &b2)) {
return 0;
diff --git a/include/test.h b/include/test.h
@@ -0,0 +1,7 @@
+#ifndef LASH_TEST_H_
+#define LASH_TEST_H_
+
+int randomData(void *buf, size_t l);
+int randomDataHex(void *buf, ssize_t l);
+
+#endif
diff --git a/test.c b/test.c
@@ -0,0 +1,35 @@
+#include <stdlib.h>
+#include <sys/random.h>
+
+#include "hex.h"
+
+int randomData(void *buf, size_t l) {
+ ssize_t r;
+
+ r = getrandom(buf, l, 0);
+ if (r != l) {
+ return 1;
+ }
+ return 0;
+}
+
+int randomDataHex(void *buf, ssize_t l) {
+ ssize_t lr;
+ int r;
+ char *rbuf;
+
+ lr = l / 2;
+ rbuf = calloc(sizeof(char), lr);
+ r = randomData(rbuf, lr);
+ if (r) {
+ free(rbuf);
+ return r;
+ }
+ r = toHex(rbuf, lr, (unsigned char*)buf, &l);
+ if (r) {
+ free(rbuf);
+ return 3;
+ }
+ free(rbuf);
+ return 0;
+}