php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
uniqid.c
Go to the documentation of this file.
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#ifdef 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
36
37#ifdef HAVE_GETTIMEOFDAY
38ZEND_TLS struct timeval prev_tv = { 0, 0 };
39
40/* {{{ Generates a unique ID */
42{
43 char *prefix = "";
44 bool more_entropy = 0;
46 int sec, usec;
47 size_t prefix_len = 0;
48 struct timeval tv;
49
52 Z_PARAM_STRING(prefix, prefix_len)
53 Z_PARAM_BOOL(more_entropy)
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) {
79 }
80 seed = ((double) bytes / UINT32_MAX) * 10.0;
81 uniqid = strpprintf(0, "%s%08x%05x%.8F", prefix, sec, usec, seed);
82 } else {
83 uniqid = strpprintf(0, "%s%08x%05x", prefix, sec, usec);
84 }
85
87}
88#endif
89/* }}} */
uniqid(string $prefix="", bool $more_entropy=false)
gettimeofday(bool $as_float=false)
#define NULL
Definition gdcache.h:45
#define prefix
#define PHP_FUNCTION
Definition php.h:364
#define UINT32_MAX
PHPAPI uint64_t php_random_generate_fallback_seed(void)
Definition random.c:716
struct timeval tv
Definition session.c:1280
#define strpprintf
Definition spprintf.h:30
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define Z_PARAM_OPTIONAL
Definition zend_API.h:1667
#define Z_PARAM_STRING(dest, dest_len)
Definition zend_API.h:2071
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define RETURN_STR(s)
Definition zend_API.h:1039
#define Z_PARAM_BOOL(dest)
Definition zend_API.h:1726
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)
struct _zend_string zend_string
@ FAILURE
Definition zend_types.h:61
#define ZEND_TLS
Definition zend_types.h:84