MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_rnd.cc
1 /*
2  Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
17 #include <my_rnd.h>
18 
19 #if defined(HAVE_YASSL)
20 
21 #if defined(YASSL_PREFIX)
22 #define RAND_bytes yaRAND_bytes
23 #endif /* YASSL_PREFIX */
24 
25 #include <openssl/ssl.h>
26 
27 #elif defined(HAVE_OPENSSL)
28 #include <openssl/rand.h>
29 #include <openssl/err.h>
30 #endif /* HAVE_YASSL */
31 
32 
33 /*
34  A wrapper to use OpenSSL/yaSSL PRNGs.
35 */
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
49 double my_rnd(struct rand_struct *rand_st)
50 {
51  rand_st->seed1= (rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
52  rand_st->seed2= (rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
53  return (((double) rand_st->seed1) / rand_st->max_value_dbl);
54 }
55 
67 double my_rnd_ssl(struct rand_struct *rand_st)
68 {
69 #if defined(HAVE_YASSL) || defined(HAVE_OPENSSL)
70  int rc;
71  unsigned int res;
72 
73 #if defined(HAVE_YASSL) /* YaSSL */
74  rc= yaSSL::RAND_bytes((unsigned char *) &res, sizeof (unsigned int));
75 
76  if (!rc)
77  return my_rnd(rand_st);
78 #else /* OpenSSL */
79  rc= RAND_bytes((unsigned char *) &res, sizeof (unsigned int));
80 
81  if (!rc)
82  {
83  ERR_clear_error();
84  return my_rnd(rand_st);
85  }
86 #endif /* HAVE_YASSL */
87 
88  return (double)res / (double)UINT_MAX;
89 
90 #else /* !defined(HAVE_YASSL) && !defined(HAVE_OPENSSL) */
91 
92  return my_rnd(rand_st);
93 
94 #endif /* defined(HAVE_YASSL) || defined(HAVE_OPENSSL) */
95 }
96 
97 #ifdef __cplusplus
98 }
99 #endif