cvs: /php3/functions uniqid.c

From: Date: Fri, 17 Sep 1999 17:39:19 +0000
Subject: cvs: /php3/functions uniqid.c
Groups: php.dev 
Request: Send a blank email to php-dev+get-11040@lists.php.net to get a copy of this message
sas Fri Sep 17 13:39:19 1999 EDT Modified files: /php3/functions uniqid.c Log: Backport changes from PHP4. This makes the "more entropy" feature optional and not mandatory, thus enabling backwards compatibility for programs which rely on uniqid()s format. NOTE: You should _not_ rely on it in your programs. uniqid() should be used as input for hash functions like md5(). Index: php3/functions/uniqid.c diff -u php3/functions/uniqid.c:1.36 php3/functions/uniqid.c:1.37 --- php3/functions/uniqid.c:1.36 Sat Aug 21 13:44:31 1999 +++ php3/functions/uniqid.c Fri Sep 17 13:39:18 1999 @@ -27,7 +27,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: uniqid.c,v 1.36 1999/08/21 17:44:31 sas Exp $ */ +/* $Id: uniqid.c,v 1.37 1999/09/17 17:39:18 sas Exp $ */ #ifdef THREAD_SAFE #include "tls.h" #endif @@ -52,28 +52,38 @@ #include "php_lcg.h" #include "uniqid.h" -/* {{{ proto string uniqid(string prefix) +#define MORE_ENTROPY (argc == 2 && flags->value.lval) + +/* {{{ proto string uniqid(string prefix, [bool more_entropy]) Generate a unique id */ -void php3_uniqid(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(uniqid) { #ifdef HAVE_GETTIMEOFDAY - pval *prefix; - + pval *prefix, *flags; char uniqid[138]; - int sec, usec; + int sec, usec, argc; struct timeval tv; TLS_VARS; - if (ARG_COUNT(ht) != 1 || getParameters(ht,1,&prefix)==FAILURE) { + argc = ARG_COUNT(ht); + if (argc < 1 || argc > 2 || getParameters(ht,ac,&prefix,&flags)==FAILURE) { WRONG_PARAM_COUNT; } convert_to_string(prefix); + if (argc == 2) { + convert_to_boolean(flags); + } /* Do some bounds checking since we are using a char array. */ if (strlen(prefix->value.str.val) > 114) { php3_error(E_WARNING, "The prefix to uniqid should not be more than 114 characters."); return; } +#if HAVE_USLEEP && !(WIN32|WINNT) + if (!MORE_ENTROPY) { + usleep(1); + } +#endif gettimeofday((struct timeval *) &tv, (struct timezone *) NULL); sec = (int) tv.tv_sec; usec = (int) (tv.tv_usec % 1000000); @@ -81,7 +91,11 @@ /* The max value usec can have is 0xF423F, so we use only five hex * digits for usecs: */ - sprintf(uniqid, "%s%08x%05x%.8f", prefix->value.str.val, sec, usec, php_combined_lcg() * 10); + if (MORE_ENTROPY) { + sprintf(uniqid, "%s%08x%05x%.8f", prefix->value.str.val, sec, usec, php_combined_lcg() * 10); + } else { + sprintf(uniqid, "%s%08x%05x", prefix->value.str.val, sec, usec); + } RETURN_STRING(uniqid,1); #endif

« previous php.dev (#11040) next »