uniqid.c 2.8 KB

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