xref: /PHP-7.2/ext/standard/uniqid.c (revision 7a7ec01a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Stig Sæther Bakken <ssb@php.net>                             |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
20 
21 #include "php.h"
22 
23 #include <stdlib.h>
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 
28 #include <string.h>
29 #include <errno.h>
30 
31 #include <stdio.h>
32 #ifdef PHP_WIN32
33 #include "win32/time.h"
34 #else
35 #include <sys/time.h>
36 #endif
37 
38 #include "php_lcg.h"
39 #include "uniqid.h"
40 
41 #ifdef HAVE_GETTIMEOFDAY
42 ZEND_TLS struct timeval prev_tv = { 0, 0 };
43 
44 /* {{{ proto string uniqid([string prefix [, bool more_entropy]])
45    Generates a unique ID */
PHP_FUNCTION(uniqid)46 PHP_FUNCTION(uniqid)
47 {
48 	char *prefix = "";
49 	zend_bool more_entropy = 0;
50 	zend_string *uniqid;
51 	int sec, usec;
52 	size_t prefix_len = 0;
53 	struct timeval tv;
54 
55 	ZEND_PARSE_PARAMETERS_START(0, 2)
56 		Z_PARAM_OPTIONAL
57 		Z_PARAM_STRING(prefix, prefix_len)
58 		Z_PARAM_BOOL(more_entropy)
59 	ZEND_PARSE_PARAMETERS_END();
60 
61 	/* This implementation needs current microsecond to change,
62 	 * hence we poll time until it does. This is much faster than
63 	 * calling usleep(1) which may cause the kernel to schedule
64 	 * another process, causing a pause of around 10ms.
65 	 */
66 	do {
67 		(void)gettimeofday((struct timeval *) &tv, (struct timezone *) NULL);
68 	} while (tv.tv_sec == prev_tv.tv_sec && tv.tv_usec == prev_tv.tv_usec);
69 
70 	prev_tv.tv_sec = tv.tv_sec;
71 	prev_tv.tv_usec = tv.tv_usec;
72 
73 	sec = (int) tv.tv_sec;
74 	usec = (int) (tv.tv_usec % 0x100000);
75 
76 	/* The max value usec can have is 0xF423F, so we use only five hex
77 	 * digits for usecs.
78 	 */
79 	if (more_entropy) {
80 		uniqid = strpprintf(0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg() * 10);
81 	} else {
82 		uniqid = strpprintf(0, "%s%08x%05x", prefix, sec, usec);
83 	}
84 
85 	RETURN_STR(uniqid);
86 }
87 #endif
88 /* }}} */
89 
90 /*
91  * Local variables:
92  * tab-width: 4
93  * c-basic-offset: 4
94  * End:
95  * vim600: sw=4 ts=4 fdm=marker
96  * vim<600: sw=4 ts=4
97  */
98