]>
code.delx.au - pulseaudio/blob - src/pulsecore/random.c
3f5919173676a898128eb97af8b301cb60a92a17
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/log.h>
42 static int has_whined
= 0;
44 static const char *devices
[] = { "/dev/urandom", "/dev/random", NULL
};
46 static int random_proper(void *ret_data
, size_t length
) {
48 assert(ret_data
&& length
);
52 #else /* OS_IS_WIN32 */
58 assert(ret_data
&& length
);
65 if ((fd
= open(*device
, O_RDONLY
)) >= 0) {
67 if ((r
= pa_loop_read(fd
, ret_data
, length
, NULL
)) < 0 || (size_t) r
!= length
)
79 #endif /* OS_IS_WIN32 */
82 void pa_random_seed(void) {
85 if (random_proper(&seed
, sizeof(unsigned int)) < 0) {
87 pa_log_warn("failed to get proper entropy. Falling back to seeding with current time.");
90 seed
= (unsigned int) time(NULL
);
96 void pa_random(void *ret_data
, size_t length
) {
100 assert(ret_data
&& length
);
102 if (random_proper(ret_data
, length
) >= 0)
106 pa_log_warn("failed to get proper entropy. Falling back to unsecure pseudo RNG.");
109 for (p
= ret_data
, l
= length
; l
> 0; p
++, l
--)
110 *p
= (uint8_t) rand();