random_seed.c
Go to the documentation of this file.
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 "timer.h"
34 #include "random_seed.h"
35 
36 static int read_random(uint32_t *dst, const char *file)
37 {
38 #if HAVE_UNISTD_H
39  int fd = open(file, O_RDONLY);
40  int err = -1;
41 
42  if (fd == -1)
43  return -1;
44  err = read(fd, dst, sizeof(*dst));
45  close(fd);
46 
47  return err;
48 #else
49  return -1;
50 #endif
51 }
52 
53 static uint32_t get_generic_seed(void)
54 {
55  clock_t last_t = 0;
56  int bits = 0;
57  uint64_t random = 0;
58  unsigned i;
59  float s = 0.000000000001;
60 
61  for (i = 0; bits < 64; i++) {
62  clock_t t = clock();
63  if (last_t && fabs(t - last_t) > s || t == (clock_t) -1) {
64  if (i < 10000 && s < (1 << 24)) {
65  s += s;
66  i = t = 0;
67  } else {
68  random = 2 * random + (i & 1);
69  bits++;
70  }
71  }
72  last_t = t;
73  }
74 #ifdef AV_READ_TIME
75  random ^= AV_READ_TIME();
76 #else
77  random ^= clock();
78 #endif
79 
80  random += random >> 32;
81 
82  return random;
83 }
84 
85 uint32_t av_get_random_seed(void)
86 {
87  uint32_t seed;
88 
89 #if HAVE_CRYPTGENRANDOM
90  HCRYPTPROV provider;
91  if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
92  CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
93  BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed);
94  CryptReleaseContext(provider, 0);
95  if (ret)
96  return seed;
97  }
98 #endif
99 
100  if (read_random(&seed, "/dev/urandom") == sizeof(seed))
101  return seed;
102  if (read_random(&seed, "/dev/random") == sizeof(seed))
103  return seed;
104  return get_generic_seed();
105 }
static int read_random(uint32_t *dst, const char *file)
Definition: random_seed.c:36
uint8_t bits
Definition: crc.c:31
static uint32_t get_generic_seed(void)
Definition: random_seed.c:53
#define AV_READ_TIME
Definition: timer.h:26
high precision timer, useful to profile code
static float t
NULL
Definition: eval.c:52
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
static unsigned int seed
Definition: videogen.c:78
static BOOL(WINAPI *cond_wait)(pthread_cond_t *cond
uint32_t av_get_random_seed(void)
Get random data.
Definition: random_seed.c:85