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