diff --git a/Makefile b/Makefile index be2cdee1..feb65757 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/src/httplib_get_random.c b/src/httplib_get_random.c new file mode 100644 index 00000000..acec6ffd --- /dev/null +++ b/src/httplib_get_random.c @@ -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 */ diff --git a/src/libhttp.c b/src/libhttp.c index 80330641..96c51006 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -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 */