cvs: php4(RAND_REDESIGN) /ext/standard basic_functions.c basic_functions.h crypt.c lcg.c php_crypt.h php_rand.h php_standard.h rand.c

From: Date: Fri, 24 Aug 2001 23:47:27 +0000
Subject: cvs: php4(RAND_REDESIGN) /ext/standard basic_functions.c basic_functions.h crypt.c lcg.c php_crypt.h php_rand.h php_standard.h rand.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-7052@lists.php.net to get a copy of this message
jeroen Fri Aug 24 19:47:27 2001 EDT Modified files: (Branch: RAND_REDESIGN) /php4/ext/standard basic_functions.c basic_functions.h crypt.c lcg.c php_crypt.h php_rand.h php_standard.h rand.c Log: Implement (not yet totally complete) INI-logic for rand. Seed random generators on script execution start (was already done in some configurations by crypt, removed that)

Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.386 php4/ext/standard/basic_functions.c:1.386.2.1 --- php4/ext/standard/basic_functions.c:1.386 Wed Aug 15 18:49:15 2001 +++ php4/ext/standard/basic_functions.c Fri Aug 24 19:47:27 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.386 2001/08/15 22:49:15 zeev Exp $ */ +/* $Id: basic_functions.c,v 1.386.2.1 2001/08/24 23:47:27 jeroen Exp $ */ #include "php.h" #include "php_main.h" @@ -25,7 +25,6 @@ #include "php_ini.h" #include "internal_functions_registry.h" #include "php_standard.h" -#include "php_math.h" #include "php_incomplete_class.h" #include "ext/standard/info.h" #include "zend_operators.h" @@ -868,10 +867,6 @@ #endif BG(user_shutdown_function_names)=NULL; -#if HAVE_CRYPT - PHP_RINIT(crypt)(INIT_FUNC_ARGS_PASSTHRU); -#endif - #ifndef ZTS PHP_RINIT(lcg)(INIT_FUNC_ARGS_PASSTHRU); #endif @@ -879,6 +874,7 @@ PHP_RINIT(filestat)(INIT_FUNC_ARGS_PASSTHRU); PHP_RINIT(syslog)(INIT_FUNC_ARGS_PASSTHRU); PHP_RINIT(dir)(INIT_FUNC_ARGS_PASSTHRU); + PHP_RINIT(rand)(INIT_FUNC_ARGS_PASSTHRU); #ifdef TRANS_SID if (BG(use_trans_sid)) { Index: php4/ext/standard/basic_functions.h diff -u php4/ext/standard/basic_functions.h:1.88 php4/ext/standard/basic_functions.h:1.88.4.1 --- php4/ext/standard/basic_functions.h:1.88 Wed Aug 8 16:05:36 2001 +++ php4/ext/standard/basic_functions.h Fri Aug 24 19:47:27 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.h,v 1.88 2001/08/08 20:05:36 zeev Exp $ */ +/* $Id: basic_functions.h,v 1.88.4.1 2001/08/24 23:47:27 jeroen Exp $ */ #ifndef BASIC_FUNCTIONS_H #define BASIC_FUNCTIONS_H @@ -171,6 +171,10 @@ struct stat lsb; /* rand.c */ + int rand_generator; /* current ini-setting */ + int rand_generator_current; /* current (by overriding by [mt_]srand) */ + + /* rand_mt.c */ php_uint32 state[MT_N+1]; /* state vector + 1 extra to not violate ANSI C */ php_uint32 *next; /* next random value is computed from here */ int left; /* can *next++ this many times before reloading */ Index: php4/ext/standard/crypt.c diff -u php4/ext/standard/crypt.c:1.45.4.1 php4/ext/standard/crypt.c:1.45.4.2 --- php4/ext/standard/crypt.c:1.45.4.1 Wed Aug 22 18:15:33 2001 +++ php4/ext/standard/crypt.c Fri Aug 24 19:47:27 2001 @@ -17,7 +17,7 @@ | Rasmus Lerdorf <rasmus@lerdorf.on.ca> | +----------------------------------------------------------------------+ */ -/* $Id: crypt.c,v 1.45.4.1 2001/08/22 22:15:33 jeroen Exp $ */ +/* $Id: crypt.c,v 1.45.4.2 2001/08/24 23:47:27 jeroen Exp $ */ #include <stdlib.h> #include "php.h" @@ -89,8 +89,6 @@ #define PHP_CRYPT_RAND php_rand() -static int php_crypt_rand_seeded=0; - PHP_MINIT_FUNCTION(crypt) { REGISTER_LONG_CONSTANT("CRYPT_SALT_LENGTH", PHP_MAX_SALT_LEN, CONST_CS | CONST_PERSISTENT); @@ -99,17 +97,6 @@ REGISTER_LONG_CONSTANT("CRYPT_MD5", PHP_MD5_CRYPT, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CRYPT_BLOWFISH", PHP_BLOWFISH_CRYPT, CONST_CS | CONST_PERSISTENT); - return SUCCESS; -} - - -PHP_RINIT_FUNCTION(crypt) -{ - if(!php_crypt_rand_seeded) { - /* FIXME (jeroen): temporary fix for RAND_REDESIGN */ - php_srand_sys(time(0) * getpid() * (php_combined_lcg(TSRMLS_C) * 10000.0)); - php_crypt_rand_seeded=1; - } return SUCCESS; } Index: php4/ext/standard/lcg.c diff -u php4/ext/standard/lcg.c:1.24 php4/ext/standard/lcg.c:1.24.4.1 --- php4/ext/standard/lcg.c:1.24 Sat Aug 11 13:03:37 2001 +++ php4/ext/standard/lcg.c Fri Aug 24 19:47:27 2001 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: lcg.c,v 1.24 2001/08/11 17:03:37 zeev Exp $ */ +/* $Id: lcg.c,v 1.24.4.1 2001/08/24 23:47:27 jeroen Exp $ */ #include "php.h" #include "php_lcg.h" @@ -42,6 +42,11 @@ * The function combines two CGs with periods of * 2^31 - 85 and 2^31 - 249. The period of this function * is equal to the product of both primes. + * + * There are only about 65k distinct starting values, that's + * not much... If PHP is running as CGI, randomness is quite bad. + * If it is run as a module, it's long-livin', so no problem in that + * case. */ #define MODMULT(a, b, c, m, s) q = s/a;s=b*(s-a*q)-c*q;if(s<0)s+=m @@ -103,5 +108,5 @@ * c-basic-offset: 4 * End: * vim600: sw=4 ts=4 tw=78 fdm=marker - * vim<600: sw=4 ts=4 tw=78 + * vim<600: sw=8 ts=8 tw=78 */ Index: php4/ext/standard/php_crypt.h diff -u php4/ext/standard/php_crypt.h:1.9 php4/ext/standard/php_crypt.h:1.9.4.1 --- php4/ext/standard/php_crypt.h:1.9 Sun Jul 29 21:56:37 2001 +++ php4/ext/standard/php_crypt.h Fri Aug 24 19:47:27 2001 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_crypt.h,v 1.9 2001/07/30 01:56:37 zeev Exp $ */ +/* $Id: php_crypt.h,v 1.9.4.1 2001/08/24 23:47:27 jeroen Exp $ */ #ifndef PHP_CRYPT_H #define PHP_CRYPT_H @@ -26,7 +26,6 @@ PHP_FUNCTION(crypt); #if HAVE_CRYPT PHP_MINIT_FUNCTION(crypt); -PHP_RINIT_FUNCTION(crypt); #endif #endif Index: php4/ext/standard/php_rand.h diff -u php4/ext/standard/php_rand.h:1.8.8.1 php4/ext/standard/php_rand.h:1.8.8.2 --- php4/ext/standard/php_rand.h:1.8.8.1 Wed Aug 22 17:53:21 2001 +++ php4/ext/standard/php_rand.h Fri Aug 24 19:47:27 2001 @@ -19,7 +19,7 @@ | Based on code from: Shawn Cokus <Cokus@math.washington.edu> | +----------------------------------------------------------------------+ */ -/* $Id: php_rand.h,v 1.8.8.1 2001/08/22 21:53:21 jeroen Exp $ */ +/* $Id: php_rand.h,v 1.8.8.2 2001/08/24 23:47:27 jeroen Exp $ */ /* Layout implementation random functions * @@ -115,6 +115,14 @@ /* Define random generator constants */ #define RAND_SYS 1 #define RAND_MT 2 +#define RAND_LCG 3 +#define RAND_SYS_STR "system" +#define RAND_MT_STR "mt" +#define RAND_LCG_STR "lcg" + +#define RAND_DEFAULT RAND_MT +#define RAND_DEFAULT_STR RAND_MT_STR + /* BC */ #define PHP_RAND_MAX php_randmax() Index: php4/ext/standard/php_standard.h diff -u php4/ext/standard/php_standard.h:1.7 php4/ext/standard/php_standard.h:1.7.8.1 --- php4/ext/standard/php_standard.h:1.7 Mon Feb 26 01:07:23 2001 +++ php4/ext/standard/php_standard.h Fri Aug 24 19:47:27 2001 @@ -16,10 +16,11 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_standard.h,v 1.7 2001/02/26 06:07:23 andi Exp $ */ +/* $Id: php_standard.h,v 1.7.8.1 2001/08/24 23:47:27 jeroen Exp $ */ #include "basic_functions.h" #include "php_math.h" +#include "php_rand.h" #include "php_string.h" #include "base64.h" #include "php_dir.h" Index: php4/ext/standard/rand.c diff -u php4/ext/standard/rand.c:1.31.4.1 php4/ext/standard/rand.c:1.31.4.2 --- php4/ext/standard/rand.c:1.31.4.1 Wed Aug 22 17:53:21 2001 +++ php4/ext/standard/rand.c Fri Aug 24 19:47:27 2001 @@ -17,19 +17,80 @@ | Pedro Melo <melo@ip.pt> | +----------------------------------------------------------------------+ */ -/* $Id: rand.c,v 1.31.4.1 2001/08/22 21:53:21 jeroen Exp $ */ +/* $Id: rand.c,v 1.31.4.2 2001/08/24 23:47:27 jeroen Exp $ */ #include <stdlib.h> #include "php.h" #include "php_math.h" #include "php_rand.h" +#include "php_ini.h" #include "zend_execute.h" #include "basic_functions.h" /* See php_rand.h for information about layout */ + +#define SRAND_A_RANDOM_SEED (time(0) * getpid() * (php_combined_lcg(TSRMLS_C) * 10000.0)) /* something with microtime? */ + +/* TODO: check that this function is called on the start of each script + * execution: not more often, not less often. + * + * Note that system rand is inherently thread-unsafe: A different thread can + * always eat up some rand()'s, and thus nuking your expected sequence. + * Another reason to use MT... + */ +PHP_RINIT_FUNCTION(rand) +{ + /* seed all number-generators */ + /* FIXME: or seed relevant numgen on init/update ini-entry? */ + php_srand_sys(SRAND_A_RANDOM_SEED); + php_srand_mt(SRAND_A_RANDOM_SEED); +} + +/* INI */ +static int randgen_str_to_int(char *str, int strlen) +{ + /* manually check all cases, or some loop to automate this + * kind of stuff, so that a new random number generator + * can be added more easily? + * + * --jeroen + */ + if (!strcasecmp(str,RAND_SYS_STR)) { + return RAND_SYS; + } else if (!strcasecmp(str,RAND_MT_STR)) { + return RAND_MT; + } else if (!strcasecmp(str,RAND_LCG_STR)) { + return RAND_LCG; + } + return 0; /* FIXME: include that f*** .h that has FALSE */ +} + +/* FIXME: check that this is called on initial ini-parsing too */ +/* FIXME: what if no ini-entry was present? */ +static PHP_INI_MH(OnUpdateRandGen) +{ + /* Set BG(rand_generator) to the correct integer value indicating + * ini-setting */ + BG(rand_generator) = randgen_str_to_int(new_value, new_value_length); + if (!BG(rand_generator)) { + /* FIXME: is this possible? What happens if this occurs during + * ini-parsing at startup? */ + php_error(E_WARNING,"Invalid value for random_number_generator: \"%s\"", new_value); + /* Fallback: */ + BG(rand_generator) = RAND_DEFAULT; + } +#ifdef DEBUG_RAND + printf("\nRAND-INI updated: %d\n",BG(rand_generator)); +#endif + return SUCCESS; +} + +PHP_INI_BEGIN() + PHP_INI_ENTRY("random_number_generator", RAND_DEFAULT_STR, PHP_INI_ALL, OnUpdateRandGen) +PHP_INI_END() /* srand */
« previous php.cvs (#7052) next »