summaryrefslogtreecommitdiff
path: root/genrand.c
blob: 4b43a954f1465a216e0fc1cfa15922d009535420 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "config.h"

#include "genrand.h"
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#undef  MAX
#define MAX(a, b)  (((a) > (b)) ? (a) : (b))

#undef  MIN
#define MIN(a, b)  (((a) < (b)) ? (a) : (b))


/* AIX requires this to be the first thing in the file.  */
#if defined(__GNUC__)

# if defined(__STRICT_ANSI__)
#  define alloca __builtin_alloca
# endif

#else

# ifdef HAVE_ALLOCA_H
#  include <alloca.h>
# elif defined(_AIX)
 #pragma alloca
# elif !defined(alloca) /* predefined by HP cc +Olibcalls */
char *alloca ();
# endif

#endif


static int
genrand_dev(unsigned char *buffer, int buf_len)
{
  int fd;

  fd = open("/dev/random", O_RDONLY);
  if(fd < 0)
    return 0;

  if(read(fd, buffer, buf_len) < buf_len)
    {
      close(fd);
      return 0;
    }

  close(fd);

  return 1;
}

static volatile int received_alarm = 0;

static void
handle_alarm(int signum)
{
  received_alarm = 1;
}

static unsigned char
hashlong(long val)
{
  unsigned char retval = 0, *ptr;
  int i;

  for(ptr = (unsigned char *)&val, i = 0; i < sizeof(val); i++)
    retval ^= ptr[i];

  return retval;
}

static int
genrand_unix(unsigned char *buffer, int buf_len)
{
  struct sigaction sa, oldsa;
  struct itimerval it, oldit;
  int i;
  long min, max;
  long *counts;
  double diff;
  long *uninit;

  counts = alloca(buf_len * sizeof(long));

  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = handle_alarm;
  sigaction(SIGALRM, &sa, &oldsa);
  memset(&it, 0, sizeof(it));
  it.it_value.tv_usec = 1;
  getitimer(ITIMER_REAL, &oldit);

  for(i = 0, min = LONG_MAX, max = 0; i < buf_len; i++)
    {
      received_alarm = 0;
      setitimer(ITIMER_REAL, &it, NULL);
      for(counts[i] = 0; !received_alarm; counts[i]++);

      max = MAX(counts[i], max);
      min = MIN(counts[i], min);
    }

  if(!(max - min))
    return 0;

  diff = max - min;

  uninit = alloca(buf_len * sizeof(long)); /* Purposely not initialized */
  for(i = 0; i < buf_len; i++)
    {
      long diffval;
      diffval = counts[i] - min;

      buffer[i] ^= (unsigned char)( ((double) (diffval*256)  / diff )) ^ hashlong(uninit[i]);
    }

  setitimer(ITIMER_REAL, &oldit, NULL);
  sigaction(SIGALRM, &oldsa, NULL);

  return 1;
}

void
esound_genrand(unsigned char *buffer, int buf_len)
{
  if(genrand_dev(buffer, buf_len))
    return;
  else if(genrand_unix(buffer, buf_len))
    return;
  else
    abort();
}