]> git.sesse.net Git - ffmpeg/blob - libavutil/random_seed.c
AVOptions: do not range check flag options.
[ffmpeg] / libavutil / random_seed.c
1 /*
2  * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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_CRYPTGENRANDOM
27 #include <windows.h>
28 #include <wincrypt.h>
29 #endif
30 #include <fcntl.h>
31 #include <math.h>
32 #include <time.h>
33 #include "internal.h"
34 #include "timer.h"
35 #include "random_seed.h"
36
37 static int read_random(uint32_t *dst, const char *file)
38 {
39 #if HAVE_UNISTD_H
40     int fd = avpriv_open(file, O_RDONLY);
41     int err = -1;
42
43     if (fd == -1)
44         return -1;
45     err = read(fd, dst, sizeof(*dst));
46     close(fd);
47
48     return err;
49 #else
50     return -1;
51 #endif
52 }
53
54 static uint32_t get_generic_seed(void)
55 {
56     clock_t last_t  = 0;
57     int bits        = 0;
58     uint64_t random = 0;
59     unsigned i;
60     float s = 0.000000000001;
61
62     for (i = 0; bits < 64; i++) {
63         clock_t t = clock();
64         if (last_t && fabs(t - last_t) > s || t == (clock_t) -1) {
65             if (i < 10000 && s < (1 << 24)) {
66                 s += s;
67                 i = t = 0;
68             } else {
69                 random = 2 * random + (i & 1);
70                 bits++;
71             }
72         }
73         last_t = t;
74     }
75 #ifdef AV_READ_TIME
76     random ^= AV_READ_TIME();
77 #else
78     random ^= clock();
79 #endif
80
81     random += random >> 32;
82
83     return random;
84 }
85
86 uint32_t av_get_random_seed(void)
87 {
88     uint32_t seed;
89
90 #if HAVE_CRYPTGENRANDOM
91     HCRYPTPROV provider;
92     if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
93                             CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
94         BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed);
95         CryptReleaseContext(provider, 0);
96         if (ret)
97             return seed;
98     }
99 #endif
100
101     if (read_random(&seed, "/dev/urandom") == sizeof(seed))
102         return seed;
103     if (read_random(&seed, "/dev/random")  == sizeof(seed))
104         return seed;
105     return get_generic_seed();
106 }