1
0
mirror of https://github.com/lammertb/libhttp.git synced 2026-01-27 08:02:47 +03:00

Moved get_random to own file

This commit is contained in:
Lammert Bies
2016-12-11 12:52:34 +01:00
parent 2f24c26117
commit 4e4484aaba
3 changed files with 62 additions and 34 deletions

View File

@@ -71,6 +71,7 @@ LIB_SOURCES = src/libhttp.c \
src/httplib_get_cookie.c \
src/httplib_get_first_ssl_listener_index.c \
src/httplib_get_mime_type.c \
src/httplib_get_random.c \
src/httplib_get_rel_url_at_current_server.c \
src/httplib_get_remote_ip.c \
src/httplib_get_request_handler.c \

61
src/httplib_get_random.c Normal file
View File

@@ -0,0 +1,61 @@
/*
* Copyright (c) 2016 Lammert Bies
* Copyright (c) 2013-2016 the Civetweb developers
* Copyright (c) 2004-2013 Sergey Lyubka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "libhttp-private.h"
/* Get a random number (independent of C rand function) */
uint64_t XX_httplib_get_random( void ) {
static uint64_t lfsr = 0; /* Linear feedback shift register */
static uint64_t lcg = 0; /* Linear congruential generator */
struct timespec now;
memset(&now, 0, sizeof(now));
clock_gettime(CLOCK_MONOTONIC, &now);
if (lfsr == 0) {
/* lfsr will be only 0 if has not been initialized,
* so this code is called only once. */
lfsr = (((uint64_t)now.tv_sec) << 21) ^ ((uint64_t)now.tv_nsec)
^ ((uint64_t)(ptrdiff_t)&now) ^ (((uint64_t)time(NULL)) << 33);
lcg = (((uint64_t)now.tv_sec) << 25) + (uint64_t)now.tv_nsec
+ (uint64_t)(ptrdiff_t)&now;
} else {
/* Get the next step of both random number generators. */
lfsr = (lfsr >> 1)
| ((((lfsr >> 0) ^ (lfsr >> 1) ^ (lfsr >> 3) ^ (lfsr >> 4)) & 1)
<< 63);
lcg = lcg * 6364136223846793005 + 1442695040888963407;
}
/* Combining two pseudo-random number generators and a high resolution part
* of the current server time will make it hard (impossible?) to guess the
* next number. */
return (lfsr ^ lcg ^ (uint64_t)now.tv_nsec);
} /* XX_httplib_get_random */

View File

@@ -2551,37 +2551,3 @@ int XX_httplib_set_non_blocking_mode( SOCKET sock ) {
} /* XX_httplib_set_non_blocking_mode */
#endif /* _WIN32 */
/* End of initial operating system specific define block. */
/* Get a random number (independent of C rand function) */
uint64_t XX_httplib_get_random( void ) {
static uint64_t lfsr = 0; /* Linear feedback shift register */
static uint64_t lcg = 0; /* Linear congruential generator */
struct timespec now;
memset(&now, 0, sizeof(now));
clock_gettime(CLOCK_MONOTONIC, &now);
if (lfsr == 0) {
/* lfsr will be only 0 if has not been initialized,
* so this code is called only once. */
lfsr = (((uint64_t)now.tv_sec) << 21) ^ ((uint64_t)now.tv_nsec)
^ ((uint64_t)(ptrdiff_t)&now) ^ (((uint64_t)time(NULL)) << 33);
lcg = (((uint64_t)now.tv_sec) << 25) + (uint64_t)now.tv_nsec
+ (uint64_t)(ptrdiff_t)&now;
} else {
/* Get the next step of both random number generators. */
lfsr = (lfsr >> 1)
| ((((lfsr >> 0) ^ (lfsr >> 1) ^ (lfsr >> 3) ^ (lfsr >> 4)) & 1)
<< 63);
lcg = lcg * 6364136223846793005 + 1442695040888963407;
}
/* Combining two pseudo-random number generators and a high resolution part
* of the current server time will make it hard (impossible?) to guess the
* next number. */
return (lfsr ^ lcg ^ (uint64_t)now.tv_nsec);
} /* XX_httplib_get_random */