]> git.sesse.net Git - ffmpeg/blob - libavutil/random_seed.c
Merge commit '0edae4e6286096023cdd6adea74722fa06029867'
[ffmpeg] / libavutil / random_seed.c
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 #include <fcntl.h>
27 #include <math.h>
28 #include <time.h>
29 #include <string.h>
30 #include "avassert.h"
31 #include "timer.h"
32 #include "random_seed.h"
33 #include "sha.h"
34 #include "intreadwrite.h"
35
36 #ifndef TEST
37 #define TEST 0
38 #endif
39
40 static int read_random(uint32_t *dst, const char *file)
41 {
42 #if HAVE_UNISTD_H
43     int fd = open(file, O_RDONLY);
44     int err = -1;
45
46     if (fd == -1)
47         return -1;
48     err = read(fd, dst, sizeof(*dst));
49     close(fd);
50
51     return err;
52 #else
53     return -1;
54 #endif
55 }
56
57 static uint32_t get_generic_seed(void)
58 {
59     uint8_t tmp[120];
60     struct AVSHA *sha = (void*)tmp;
61     clock_t last_t  = 0;
62     static uint64_t i = 0;
63     static uint32_t buffer[512] = {0};
64     unsigned char digest[32];
65     uint64_t last_i = i;
66
67     av_assert0(sizeof(tmp) >= av_sha_size);
68
69     if(TEST){
70         memset(buffer, 0, sizeof(buffer));
71         last_i = i = 0;
72     }else{
73 #ifdef AV_READ_TIME
74         buffer[13] ^= AV_READ_TIME();
75         buffer[41] ^= AV_READ_TIME()>>32;
76 #endif
77     }
78
79     for (;;) {
80         clock_t t = clock();
81
82         if(last_t == t){
83             buffer[i&511]++;
84         }else{
85             buffer[++i&511]+= (t-last_t) % 3294638521U;
86             if(last_i && i-last_i > 4 || i-last_i > 64 || TEST && i-last_i > 8)
87                 break;
88         }
89         last_t = t;
90     }
91
92     if(TEST)
93         buffer[0] = buffer[1] = 0;
94
95     av_sha_init(sha, 160);
96     av_sha_update(sha, (uint8_t*)buffer, sizeof(buffer));
97     av_sha_final(sha, digest);
98     return AV_RB32(digest) + AV_RB32(digest+32);
99 }
100
101 uint32_t av_get_random_seed(void)
102 {
103     uint32_t seed;
104
105     if (read_random(&seed, "/dev/urandom") == sizeof(seed))
106         return seed;
107     if (read_random(&seed, "/dev/random")  == sizeof(seed))
108         return seed;
109     return get_generic_seed();
110 }
111
112 #if TEST
113 #undef printf
114 #define N 256
115 #include <stdio.h>
116
117 int main(void)
118 {
119     int i, j, retry;
120     uint32_t seeds[N];
121
122     for (retry=0; retry<3; retry++){
123         for (i=0; i<N; i++){
124             seeds[i] = av_get_random_seed();
125             for (j=0; j<i; j++)
126                 if (seeds[j] == seeds[i])
127                     goto retry;
128         }
129         printf("seeds OK\n");
130         return 0;
131         retry:;
132     }
133     printf("FAIL at %d with %X\n", j, seeds[j]);
134     return 1;
135 }
136 #endif