Commit eaff9f89 authored by Philipp Schafft's avatar Philipp Schafft 🦁 Committed by Philipp Schafft
Browse files

Feature: Added igloo_prng_write_file(), and igloo_prng_read_file()

parent e2b8f5d5
......@@ -80,7 +80,9 @@ AS_IF([test "$ice_found_std_headers" != "yes"], [
AC_MSG_ERROR([Unable to find the standard headers])
])
AC_CHECK_HEADERS([sys/select.h errno.h])
AC_CHECK_HEADERS([sys/select.h errno.h fcntl.h])
AC_CHECK_FUNCS([open close read write fopen fclose fread fwrite], [], [AC_MSG_ERROR([Required I/O functions missing])])
AC_CHECK_FUNCS_ONCE([clock_gettime gettimeofday ftime clock_nanosleep nanosleep usleep select])
......
......@@ -29,10 +29,19 @@
#include <sys/utsname.h>
#endif
#if defined(HAVE_GETUID) || defined(HAVE_GETPID) || defined(HAVE_GETPPID)
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <igloo/prng.h>
#include <igloo/error.h>
......@@ -43,6 +52,7 @@
#include "private.h"
#define HASHFUNC "SHA3-512" // must be a 512 bit function!
#define BLOCK_LENGTH (512/8)
#define MAX_BITS_PER_HASH 192
#define MAX_BITS_PER_STATE (MAX_BITS_PER_HASH*3)
......@@ -440,7 +450,84 @@ ssize_t igloo_prng_read(igloo_ro_t instance, void *buffer, size_t len, igloo_prn
}
/* write len pseudo random bytes to a file. If len is -1 a default value is used. */
igloo_error_t igloo_prng_write_file(igloo_ro_t instance, const char *filename, ssize_t len, igloo_prng_flags_t flags);
igloo_error_t igloo_prng_write_file(igloo_ro_t instance, const char *filename, ssize_t len, igloo_prng_flags_t flags)
{
char buffer[BLOCK_LENGTH*16];
size_t done = 0;
FILE *file;
if (len < 0)
len = 1024;
file = fopen(filename, "wb");
if (!file)
return igloo_ERROR_GENERIC;
while (done < (size_t)len) {
size_t todo = (size_t)len < sizeof(buffer) ? (size_t)len : sizeof(buffer);
ssize_t res = igloo_prng_read(instance, buffer, todo, flags);
if (res < 1) {
fclose(file);
return igloo_ERROR_GENERIC;
}
if (fwrite(buffer, 1, res, file) != (size_t)res) {
fclose(file);
return igloo_ERROR_GENERIC;
}
done += res;
}
fclose(file);
return igloo_ERROR_NONE;
}
/* read at max len bytes from the file and see the PRNG with it. if len is -1 all of the file is read. */
igloo_error_t igloo_prng_read_file(igloo_ro_t instance, const char *filename, ssize_t len, ssize_t bits, igloo_prng_flags_t flags);
igloo_error_t igloo_prng_read_file(igloo_ro_t instance, const char *filename, ssize_t len, ssize_t bits, igloo_prng_flags_t flags)
{
char buffer[BLOCK_LENGTH*16];
size_t done = 0;
int fh;
igloo_error_t error = igloo_ERROR_NONE;
if (len < 0 || len > 1048576)
len = 1048576;
#ifdef O_CLOEXEC
fh = open(filename, O_RDONLY|O_CLOEXEC, 0);
#else
fh = open(filename, O_RDONLY, 0);
#endif
if (fh < 0)
return igloo_ERROR_GENERIC;
while (done < (size_t)len) {
size_t todo = (size_t)len < sizeof(buffer) ? (size_t)len : sizeof(buffer);
size_t res = read(fh, buffer, todo);
ssize_t bufferbits;
if (res < 1) {
close(fh);
return igloo_ERROR_NONE;
}
if (bits < 0 || len < 1) {
bufferbits = -1;
} else {
bufferbits = (bits * res) / len;
}
error = igloo_prng_write(instance, buffer, res, bufferbits, flags);
if (error != igloo_ERROR_NONE)
break;
done += res;
}
close(fh);
return error;
}
Supports Markdown
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