Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / src / pulsecore / random.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as
11   published by the Free Software Foundation; either version 2.1 of the
12   License, or (at your option) any later version.
13
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public
20   License along with PulseAudio; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <assert.h>
35 #include <time.h>
36
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/log.h>
39
40 #include "random.h"
41
42 static int has_whined = 0;
43
44 static const char *devices[] = { "/dev/urandom", "/dev/random", NULL };
45
46 static int random_proper(void *ret_data, size_t length) {
47 #ifdef OS_IS_WIN32
48     assert(ret_data && length);
49
50     return -1;
51
52 #else /* OS_IS_WIN32 */
53
54     int fd, ret = -1;
55     ssize_t r = 0;
56     const char **device;
57
58     assert(ret_data && length);
59
60     device = devices;
61
62     while (*device) {
63         ret = 0;
64
65         if ((fd = open(*device, O_RDONLY)) >= 0) {
66
67             if ((r = pa_loop_read(fd, ret_data, length, NULL)) < 0 || (size_t) r != length)
68                 ret = -1;
69
70             close(fd);
71         } else
72             ret = -1;
73
74         if (ret == 0)
75             break;
76     }
77
78     return ret;
79 #endif /* OS_IS_WIN32 */
80 }
81
82 void pa_random_seed(void) {
83     unsigned int seed;
84
85     if (random_proper(&seed, sizeof(unsigned int)) < 0) {
86         if (!has_whined)
87             pa_log_warn("failed to get proper entropy. Falling back to seeding with current time.");
88         has_whined = 1;
89
90         seed = (unsigned int) time(NULL);
91     }
92
93     srand(seed);
94 }
95
96 void pa_random(void *ret_data, size_t length) {
97     uint8_t *p;
98     size_t l;
99
100     assert(ret_data && length);
101
102     if (random_proper(ret_data, length) >= 0)
103         return;
104
105     if (!has_whined)
106         pa_log_warn("failed to get proper entropy. Falling back to unsecure pseudo RNG.");
107     has_whined = 1;
108
109     for (p = ret_data, l = length; l > 0; p++, l--)
110         *p = (uint8_t) rand();
111 }