mirror of
https://github.com/urbit/shrub.git
synced 2024-12-22 10:21:31 +03:00
Simpler implementation
This commit is contained in:
parent
a9f39225c5
commit
7a7e7b30f8
17
include/ent.h
Normal file
17
include/ent.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef _ENT_H
|
||||
#define _ENT_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/*
|
||||
* Fills buf with high-quality entropy.
|
||||
*
|
||||
* buflen is the number of bytes, no greater than 256.
|
||||
*
|
||||
* Returns 0 on success. On failure, returns -1 and sets errno to
|
||||
* indicate the error.
|
||||
*/
|
||||
int
|
||||
ent_getentropy(void* buf, size_t buflen);
|
||||
|
||||
#endif /* _ENT_H */
|
@ -1,26 +0,0 @@
|
||||
#ifndef _LIBENT_DETAIL_H
|
||||
#define _LIBENT_DETAIL_H
|
||||
|
||||
#if defined _WIN32 || defined __CYGWIN__
|
||||
# ifdef WIN_EXPORT
|
||||
# ifdef __GNUC__
|
||||
# define ENT_EXPORT __attribute__ ((dllexport))
|
||||
# else
|
||||
# define ENT_EXPORT __declspec(dllexport)
|
||||
# endif
|
||||
# else
|
||||
# ifdef __GNUC__
|
||||
# define ENT_EXPORT __attribute__ ((dllimport))
|
||||
# else
|
||||
# define ENT_EXPORT __declspec(dllimport)
|
||||
# endif
|
||||
# endif
|
||||
#else
|
||||
# if __GNUC__ >= 4
|
||||
# define ENT_EXPORT __attribute__ ((visibility ("default")))
|
||||
# else
|
||||
# define ENT_EXPORT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#endif /* _LIBENT_DETAIL_H */
|
@ -1,12 +0,0 @@
|
||||
#ifndef _LIBENT_ENT_H
|
||||
#define _LIBENT_ENT_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <ent/detail.h>
|
||||
|
||||
ENT_EXPORT
|
||||
int
|
||||
ent_getentropy(void *buf, size_t buflen);
|
||||
|
||||
#endif /* _LIBENT_ENT_H */
|
26
meson.build
26
meson.build
@ -3,11 +3,33 @@ project('libent', 'c', 'cpp',
|
||||
version : '0.0.1',
|
||||
license : 'MIT')
|
||||
|
||||
inc = include_directories('include')
|
||||
conf_data = configuration_data()
|
||||
compiler = meson.get_compiler('c')
|
||||
|
||||
install_headers('include/ent/ent.h', subdir : 'ent')
|
||||
code = '''#include <unistd.h>
|
||||
#include <sys/random.h>
|
||||
int main() { return getentropy((void*)0, 0); }
|
||||
'''
|
||||
have_getentropy = compiler.links(code,name : 'system getentropy')
|
||||
conf_data.set('USE_GETENTROPY', have_getentropy)
|
||||
|
||||
if not have_getentropy
|
||||
code = '''#include <fcntl.h>
|
||||
int main() { return -1 != open("/dev/urandom", O_RDONLY) ? 0 : 1; }
|
||||
'''
|
||||
use_urandom = compiler.run(code, name : 'open /dev/urandom')
|
||||
conf_data.set('USE_URANDOM', use_urandom.returncode() == 0)
|
||||
endif
|
||||
|
||||
configure_file(output : 'config.h',
|
||||
configuration : conf_data)
|
||||
|
||||
inc = include_directories('.', 'include')
|
||||
|
||||
install_headers('include/ent.h')
|
||||
|
||||
ent_sources = ['src/ent.c']
|
||||
|
||||
libent = shared_library('ent',
|
||||
ent_sources,
|
||||
include_directories : inc,
|
||||
|
41
src/ent.c
41
src/ent.c
@ -1,14 +1,43 @@
|
||||
#include <ent/ent.h>
|
||||
#include <ent.h>
|
||||
|
||||
#if defined __OPENBSD__ || (defined __linux__ && defined _DEFAULT_SOURCE)
|
||||
#include <assert.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#if defined(USE_GETENTROPY)
|
||||
# include <unistd.h>
|
||||
# if defined(__APPLE__) || defined(__linux__)
|
||||
# include <sys/random.h>
|
||||
# endif
|
||||
# define getentropy_impl getentropy
|
||||
#elif defined __APPLE__ && defined __MACH__
|
||||
#elif defined(USE_URANDOM)
|
||||
# include <fcntl.h>
|
||||
# include <sys/types.h>
|
||||
# include <unistd.h>
|
||||
# include <sys/random.h>
|
||||
# define getentropy_impl getentropy
|
||||
#else
|
||||
# error "Port: getentropy unimplemented"
|
||||
# error "port: getentropy"
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(USE_URANDOM)
|
||||
|
||||
static int
|
||||
getentropy_impl(void* buf, size_t buflen)
|
||||
{
|
||||
int fd;
|
||||
|
||||
assert(buflen <= 256);
|
||||
if (-1 == (fd = open("/dev/urandom", O_RDONLY))) {
|
||||
return -1;
|
||||
}
|
||||
if (buflen != read(fd, buf, buflen)) {
|
||||
(void) close(fd);
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
int
|
||||
|
Loading…
Reference in New Issue
Block a user