1 /*
2 * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "config.h"
22
23 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #if HAVE_IO_H
27 #include <io.h>
28 #endif
29 #if HAVE_BCRYPT
30 #include <windows.h>
31 #include <bcrypt.h>
32 #endif
33 #include <fcntl.h>
34 #include <math.h>
36 #include <string.h>
44
45 #ifndef TEST
47 #endif
48
50 {
51 #if HAVE_UNISTD_H
53 int err = -1;
54
55 if (fd == -1)
56 return -1;
57 err =
read(fd, dst,
sizeof(*dst));
58 close(fd);
59
60 return err;
61 #else
62 return -1;
63 #endif
64 }
65
67 {
70 clock_t last_t = 0;
71 clock_t last_td = 0;
72 clock_t init_t = 0;
73 static uint64_t
i = 0;
74 static uint32_t
buffer[512] = { 0 };
75 unsigned char digest[20];
77
79
83 }else{
84 #ifdef AV_READ_TIME
87 #endif
88 }
89
90 for (;;) {
91 clock_t t = clock();
92 if (last_t + 2*last_td + (CLOCKS_PER_SEC > 1000) >= t) {
93 last_td = t - last_t;
94 buffer[
i & 511] = 1664525*
buffer[
i & 511] + 1013904223 + (last_td % 3294638521
U);
95 } else {
96 last_td = t - last_t;
97 buffer[++
i & 511] += last_td % 3294638521
U;
98 if ((t - init_t) >= CLOCKS_PER_SEC>>5)
99 if (last_i &&
i - last_i > 4 ||
i - last_i > 64 ||
TEST &&
i - last_i > 8)
100 break;
101 }
102 last_t = t;
103 if (!init_t)
104 init_t = t;
105 }
106
109 } else {
110 #ifdef AV_READ_TIME
112 #endif
113 }
114
119 }
120
122 {
124
125 #if HAVE_BCRYPT
126 BCRYPT_ALG_HANDLE algo_handle;
127 NTSTATUS
ret = BCryptOpenAlgorithmProvider(&algo_handle, BCRYPT_RNG_ALGORITHM,
128 MS_PRIMITIVE_PROVIDER, 0);
129 if (BCRYPT_SUCCESS(
ret)) {
130 NTSTATUS
ret = BCryptGenRandom(algo_handle, (UCHAR*)&
seed,
sizeof(
seed), 0);
131 BCryptCloseAlgorithmProvider(algo_handle, 0);
132 if (BCRYPT_SUCCESS(
ret))
134 }
135 #endif
136
137 #if HAVE_ARC4RANDOM
138 return arc4random();
139 #endif
140
146 }