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