Merge pull request #2544 from signalwire/SWITCH_RAND_MAX
[Core] Introduce SWITCH_RAND_MAX to switch_rand()
This commit is contained in:
commit
be3c0b3ef6
|
@ -599,6 +599,13 @@ SWITCH_DECLARE_DATA extern switch_filenames SWITCH_GLOBAL_filenames;
|
||||||
|
|
||||||
#define SWITCH_ACCEPTABLE_INTERVAL(_i) (_i && _i <= SWITCH_MAX_INTERVAL && (_i % 10) == 0)
|
#define SWITCH_ACCEPTABLE_INTERVAL(_i) (_i && _i <= SWITCH_MAX_INTERVAL && (_i % 10) == 0)
|
||||||
|
|
||||||
|
/* Check if RAND_MAX is a power of 2 minus 1 or in other words all bits set */
|
||||||
|
#if ((RAND_MAX) & ((RAND_MAX) + 1)) == 0 && (RAND_MAX) != 0
|
||||||
|
#define SWITCH_RAND_MAX RAND_MAX
|
||||||
|
#else
|
||||||
|
#define SWITCH_RAND_MAX 0x7fff
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SWITCH_RW_READ,
|
SWITCH_RW_READ,
|
||||||
SWITCH_RW_WRITE
|
SWITCH_RW_WRITE
|
||||||
|
|
|
@ -4835,8 +4835,8 @@ SWITCH_DECLARE(int) switch_rand(void)
|
||||||
|
|
||||||
BCryptCloseAlgorithmProvider(hAlgorithm, 0);
|
BCryptCloseAlgorithmProvider(hAlgorithm, 0);
|
||||||
|
|
||||||
/* Make sure we return from 0 to RAND_MAX */
|
/* Make sure we return from 0 to SWITCH_RAND_MAX */
|
||||||
return (random_number & 0x7FFF);
|
return (random_number & (SWITCH_RAND_MAX));
|
||||||
#elif defined(__unix__) || defined(__APPLE__)
|
#elif defined(__unix__) || defined(__APPLE__)
|
||||||
int random_fd = open("/dev/urandom", O_RDONLY);
|
int random_fd = open("/dev/urandom", O_RDONLY);
|
||||||
ssize_t result;
|
ssize_t result;
|
||||||
|
@ -4865,8 +4865,8 @@ SWITCH_DECLARE(int) switch_rand(void)
|
||||||
|
|
||||||
close(random_fd);
|
close(random_fd);
|
||||||
|
|
||||||
/* Make sure we return from 0 to RAND_MAX */
|
/* Make sure we return from 0 to SWITCH_RAND_MAX */
|
||||||
return (random_number & 0x7FFF);
|
return (random_number & (SWITCH_RAND_MAX));
|
||||||
#else
|
#else
|
||||||
return rand();
|
return rand();
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue