Coverage Report

Created: 2025-06-13 06:43

/src/php-src/ext/standard/uniqid.c
Line
Count
Source (jump to first uncovered line)
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
34
#include "ext/random/php_random.h"
35
#include "ext/random/php_random_csprng.h"
36
37
#ifdef HAVE_GETTIMEOFDAY
38
ZEND_TLS struct timeval prev_tv = { 0, 0 };
39
40
/* {{{ Generates a unique ID */
41
PHP_FUNCTION(uniqid)
42
0
{
43
0
  char *prefix = "";
44
0
  bool more_entropy = 0;
45
0
  zend_string *uniqid;
46
0
  int sec, usec;
47
0
  size_t prefix_len = 0;
48
0
  struct timeval tv;
49
50
0
  ZEND_PARSE_PARAMETERS_START(0, 2)
51
0
    Z_PARAM_OPTIONAL
52
0
    Z_PARAM_STRING(prefix, prefix_len)
53
0
    Z_PARAM_BOOL(more_entropy)
54
0
  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
0
  do {
62
0
    (void)gettimeofday((struct timeval *) &tv, (struct timezone *) NULL);
63
0
  } while (tv.tv_sec == prev_tv.tv_sec && tv.tv_usec == prev_tv.tv_usec);
64
65
0
  prev_tv.tv_sec = tv.tv_sec;
66
0
  prev_tv.tv_usec = tv.tv_usec;
67
68
0
  sec = (int) tv.tv_sec;
69
0
  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
0
  if (more_entropy) {
75
0
    uint32_t bytes;
76
0
    double seed;
77
0
    if (php_random_bytes_silent(&bytes, sizeof(uint32_t)) == FAILURE) {
78
0
      bytes = php_random_generate_fallback_seed();
79
0
    }
80
0
    seed = ((double) bytes / UINT32_MAX) * 10.0;
81
0
    uniqid = strpprintf(0, "%s%08x%05x%.8F", prefix, sec, usec, seed);
82
0
  } else {
83
0
    uniqid = strpprintf(0, "%s%08x%05x", prefix, sec, usec);
84
0
  }
85
86
0
  RETURN_STR(uniqid);
87
0
}
88
#endif
89
/* }}} */