xref: /PHP-7.4/ext/standard/uniqid.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 #include "php.h"
20 
21 #include <stdlib.h>
22 #if HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 
26 #include <string.h>
27 #include <errno.h>
28 
29 #include <stdio.h>
30 #ifdef PHP_WIN32
31 #include "win32/time.h"
32 #else
33 #include <sys/time.h>
34 #endif
35 
36 #include "php_lcg.h"
37 #include "uniqid.h"
38 
39 #ifdef HAVE_GETTIMEOFDAY
40 ZEND_TLS struct timeval prev_tv = { 0, 0 };
41 
42 /* {{{ proto string uniqid([string prefix [, bool more_entropy]])
43    Generates a unique ID */
PHP_FUNCTION(uniqid)44 PHP_FUNCTION(uniqid)
45 {
46 	char *prefix = "";
47 	zend_bool more_entropy = 0;
48 	zend_string *uniqid;
49 	int sec, usec;
50 	size_t prefix_len = 0;
51 	struct timeval tv;
52 
53 	ZEND_PARSE_PARAMETERS_START(0, 2)
54 		Z_PARAM_OPTIONAL
55 		Z_PARAM_STRING(prefix, prefix_len)
56 		Z_PARAM_BOOL(more_entropy)
57 	ZEND_PARSE_PARAMETERS_END();
58 
59 	/* This implementation needs current microsecond to change,
60 	 * hence we poll time until it does. This is much faster than
61 	 * calling usleep(1) which may cause the kernel to schedule
62 	 * another process, causing a pause of around 10ms.
63 	 */
64 	do {
65 		(void)gettimeofday((struct timeval *) &tv, (struct timezone *) NULL);
66 	} while (tv.tv_sec == prev_tv.tv_sec && tv.tv_usec == prev_tv.tv_usec);
67 
68 	prev_tv.tv_sec = tv.tv_sec;
69 	prev_tv.tv_usec = tv.tv_usec;
70 
71 	sec = (int) tv.tv_sec;
72 	usec = (int) (tv.tv_usec % 0x100000);
73 
74 	/* The max value usec can have is 0xF423F, so we use only five hex
75 	 * digits for usecs.
76 	 */
77 	if (more_entropy) {
78 		uniqid = strpprintf(0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg() * 10);
79 	} else {
80 		uniqid = strpprintf(0, "%s%08x%05x", prefix, sec, usec);
81 	}
82 
83 	RETURN_STR(uniqid);
84 }
85 #endif
86 /* }}} */
87