about summary refs log tree commit diff
path: root/src/librandom/random_string.c
blob: f8cabc3b627ca56cfad161d60269cfedb3c8b4ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* ISC license. */

#include <skalibs/sysdeps.h>

#ifdef SKALIBS_HASARC4RANDOM

#include <skalibs/nonposix.h>
#include <stdlib.h>
#include <skalibs/random.h>

void random_string (char *s, size_t n)
{
  arc4random_buf(s, n) ;
}

#else
#ifdef SKALIBS_HASGETRANDOM

#include <skalibs/nonposix.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <skalibs/random.h>

static int getrandom (void *buf, size_t buflen, unsigned int flags)
{
  return syscall(SYS_getrandom, buf, buflen, flags) ;
}

void random_string (char *s, size_t n)
{
  while (n)
  {
    int r = getrandom(s, n, 0) ;
    if (r >= 0)
    {
      s += r ;
      n -= r ;
    }
  }
}

#else
#ifdef SKALIBS_HASDEVURANDOM

#include <skalibs/allreadwrite.h>
#include <skalibs/random.h>
#include "random-internal.h"

void random_string (char *s, size_t n)
{
  size_t r = allread(random_fd, s, n) ;
  if (r < n) surf(&surf_here, s+r, n-r) ;
}

#else /* default */

#include <skalibs/random.h>
#include "random-internal.h"

void random_string (char *s, size_t n)
{
  surf(&surf_here, s, n) ;
}

#endif
#endif
#endif