Uuid.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Uuid;
  11. /**
  12. * @internal
  13. *
  14. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  15. */
  16. final class Uuid
  17. {
  18. public const UUID_VARIANT_NCS = 0;
  19. public const UUID_VARIANT_DCE = 1;
  20. public const UUID_VARIANT_MICROSOFT = 2;
  21. public const UUID_VARIANT_OTHER = 3;
  22. public const UUID_TYPE_DEFAULT = 0;
  23. public const UUID_TYPE_TIME = 1;
  24. public const UUID_TYPE_MD5 = 3;
  25. public const UUID_TYPE_DCE = 4; // Deprecated alias
  26. public const UUID_TYPE_NAME = 1; // Deprecated alias
  27. public const UUID_TYPE_RANDOM = 4;
  28. public const UUID_TYPE_SHA1 = 5;
  29. public const UUID_TYPE_NULL = -1;
  30. public const UUID_TYPE_INVALID = -42;
  31. // https://tools.ietf.org/html/rfc4122#section-4.1.4
  32. // 0x01b21dd213814000 is the number of 100-ns intervals between the
  33. // UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
  34. public const TIME_OFFSET_INT = 0x01B21DD213814000;
  35. public const TIME_OFFSET_BIN = "\x01\xb2\x1d\xd2\x13\x81\x40\x00";
  36. public const TIME_OFFSET_COM = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
  37. public static function uuid_create($uuid_type = \UUID_TYPE_DEFAULT)
  38. {
  39. if (!is_numeric($uuid_type) && null !== $uuid_type) {
  40. trigger_error(sprintf('uuid_create() expects parameter 1 to be int, %s given', \gettype($uuid_type)), \E_USER_WARNING);
  41. return null;
  42. }
  43. switch ((int) $uuid_type) {
  44. case self::UUID_TYPE_NAME:
  45. case self::UUID_TYPE_TIME:
  46. return self::uuid_generate_time();
  47. case self::UUID_TYPE_DCE:
  48. case self::UUID_TYPE_RANDOM:
  49. case self::UUID_TYPE_DEFAULT:
  50. return self::uuid_generate_random();
  51. default:
  52. trigger_error(sprintf("Unknown/invalid UUID type '%d' requested, using default type instead", $uuid_type), \E_USER_WARNING);
  53. return self::uuid_generate_random();
  54. }
  55. }
  56. public static function uuid_generate_md5($uuid_ns, $name)
  57. {
  58. if (!\is_string($uuid_ns = self::toString($uuid_ns))) {
  59. trigger_error(sprintf('uuid_generate_md5() expects parameter 1 to be string, %s given', \gettype($uuid_ns)), \E_USER_WARNING);
  60. return null;
  61. }
  62. if (!\is_string($name = self::toString($name))) {
  63. trigger_error(sprintf('uuid_generate_md5() expects parameter 2 to be string, %s given', \gettype($name)), \E_USER_WARNING);
  64. return null;
  65. }
  66. if (!self::isValid($uuid_ns)) {
  67. if (80000 > \PHP_VERSION_ID) {
  68. return false;
  69. }
  70. throw new \ValueError('uuid_generate_md5(): Argument #1 ($uuid_ns) UUID expected');
  71. }
  72. $hash = md5(hex2bin(str_replace('-', '', $uuid_ns)).$name);
  73. return sprintf('%08s-%04s-3%03s-%04x-%012s',
  74. // 32 bits for "time_low"
  75. substr($hash, 0, 8),
  76. // 16 bits for "time_mid"
  77. substr($hash, 8, 4),
  78. // 16 bits for "time_hi_and_version",
  79. // four most significant bits holds version number 3
  80. substr($hash, 13, 3),
  81. // 16 bits:
  82. // * 8 bits for "clk_seq_hi_res",
  83. // * 8 bits for "clk_seq_low",
  84. hexdec(substr($hash, 16, 4)) & 0x3FFF | 0x8000,
  85. // 48 bits for "node"
  86. substr($hash, 20, 12)
  87. );
  88. }
  89. public static function uuid_generate_sha1($uuid_ns, $name)
  90. {
  91. if (!\is_string($uuid_ns = self::toString($uuid_ns))) {
  92. trigger_error(sprintf('uuid_generate_sha1() expects parameter 1 to be string, %s given', \gettype($uuid_ns)), \E_USER_WARNING);
  93. return null;
  94. }
  95. if (!\is_string($name = self::toString($name))) {
  96. trigger_error(sprintf('uuid_generate_sha1() expects parameter 2 to be string, %s given', \gettype($name)), \E_USER_WARNING);
  97. return null;
  98. }
  99. if (!self::isValid($uuid_ns)) {
  100. if (80000 > \PHP_VERSION_ID) {
  101. return false;
  102. }
  103. throw new \ValueError('uuid_generate_sha1(): Argument #1 ($uuid_ns) UUID expected');
  104. }
  105. $hash = sha1(hex2bin(str_replace('-', '', $uuid_ns)).$name);
  106. return sprintf('%08s-%04s-5%03s-%04x-%012s',
  107. // 32 bits for "time_low"
  108. substr($hash, 0, 8),
  109. // 16 bits for "time_mid"
  110. substr($hash, 8, 4),
  111. // 16 bits for "time_hi_and_version",
  112. // four most significant bits holds version number 5
  113. substr($hash, 13, 3),
  114. // 16 bits:
  115. // * 8 bits for "clk_seq_hi_res",
  116. // * 8 bits for "clk_seq_low",
  117. // WARNING: On old libuuid version, there is a bug. 0x0fff is used instead of 0x3fff
  118. // See https://github.com/karelzak/util-linux/commit/d6ddf07d31dfdc894eb8e7e6842aa856342c526e
  119. hexdec(substr($hash, 16, 4)) & 0x3FFF | 0x8000,
  120. // 48 bits for "node"
  121. substr($hash, 20, 12)
  122. );
  123. }
  124. public static function uuid_is_valid($uuid)
  125. {
  126. if (!\is_string($uuid = self::toString($uuid))) {
  127. trigger_error(sprintf('uuid_is_valid() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
  128. return null;
  129. }
  130. return self::isValid($uuid);
  131. }
  132. public static function uuid_compare($uuid1, $uuid2)
  133. {
  134. if (!\is_string($uuid1 = self::toString($uuid1))) {
  135. trigger_error(sprintf('uuid_compare() expects parameter 1 to be string, %s given', \gettype($uuid1)), \E_USER_WARNING);
  136. return null;
  137. }
  138. if (!\is_string($uuid2 = self::toString($uuid2))) {
  139. trigger_error(sprintf('uuid_compare() expects parameter 2 to be string, %s given', \gettype($uuid2)), \E_USER_WARNING);
  140. return null;
  141. }
  142. if (!self::isValid($uuid1)) {
  143. if (80000 > \PHP_VERSION_ID) {
  144. return false;
  145. }
  146. throw new \ValueError('uuid_compare(): Argument #1 ($uuid1) UUID expected');
  147. }
  148. if (!self::isValid($uuid2)) {
  149. if (80000 > \PHP_VERSION_ID) {
  150. return false;
  151. }
  152. throw new \ValueError('uuid_compare(): Argument #2 ($uuid2) UUID expected');
  153. }
  154. return strcasecmp($uuid1, $uuid2);
  155. }
  156. public static function uuid_is_null($uuid)
  157. {
  158. if (!\is_string($uuid = self::toString($uuid))) {
  159. trigger_error(sprintf('uuid_is_null() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
  160. return null;
  161. }
  162. if (80000 <= \PHP_VERSION_ID && !self::isValid($uuid)) {
  163. throw new \ValueError('uuid_is_null(): Argument #1 ($uuid) UUID expected');
  164. }
  165. return '00000000-0000-0000-0000-000000000000' === $uuid;
  166. }
  167. public static function uuid_type($uuid)
  168. {
  169. if (!\is_string($uuid = self::toString($uuid))) {
  170. trigger_error(sprintf('uuid_type() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
  171. return null;
  172. }
  173. if ('00000000-0000-0000-0000-000000000000' === $uuid) {
  174. return self::UUID_TYPE_NULL;
  175. }
  176. if (null === $parsed = self::parse($uuid)) {
  177. if (80000 > \PHP_VERSION_ID) {
  178. return false;
  179. }
  180. throw new \ValueError('uuid_type(): Argument #1 ($uuid) UUID expected');
  181. }
  182. return $parsed['version'];
  183. }
  184. public static function uuid_variant($uuid)
  185. {
  186. if (!\is_string($uuid = self::toString($uuid))) {
  187. trigger_error(sprintf('uuid_variant() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
  188. return null;
  189. }
  190. if ('00000000-0000-0000-0000-000000000000' === $uuid) {
  191. return self::UUID_TYPE_NULL;
  192. }
  193. if (null === $parsed = self::parse($uuid)) {
  194. if (80000 > \PHP_VERSION_ID) {
  195. return false;
  196. }
  197. throw new \ValueError('uuid_variant(): Argument #1 ($uuid) UUID expected');
  198. }
  199. if (($parsed['clock_seq'] & 0x8000) === 0) {
  200. return self::UUID_VARIANT_NCS;
  201. }
  202. if (($parsed['clock_seq'] & 0x4000) === 0) {
  203. return self::UUID_VARIANT_DCE;
  204. }
  205. if (($parsed['clock_seq'] & 0x2000) === 0) {
  206. return self::UUID_VARIANT_MICROSOFT;
  207. }
  208. return self::UUID_VARIANT_OTHER;
  209. }
  210. public static function uuid_time($uuid)
  211. {
  212. if (!\is_string($uuid = self::toString($uuid))) {
  213. trigger_error(sprintf('uuid_time() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
  214. return null;
  215. }
  216. $parsed = self::parse($uuid);
  217. if (self::UUID_TYPE_TIME !== ($parsed['version'] ?? null)) {
  218. if (80000 > \PHP_VERSION_ID) {
  219. return false;
  220. }
  221. throw new \ValueError('uuid_time(): Argument #1 ($uuid) UUID DCE TIME expected');
  222. }
  223. if (\PHP_INT_SIZE >= 8) {
  224. return intdiv(hexdec($parsed['time']) - self::TIME_OFFSET_INT, 10000000);
  225. }
  226. $time = str_pad(hex2bin($parsed['time']), 8, "\0", \STR_PAD_LEFT);
  227. $time = self::binaryAdd($time, self::TIME_OFFSET_COM);
  228. $time[0] = $time[0] & "\x7F";
  229. return (int) substr(self::toDecimal($time), 0, -7);
  230. }
  231. public static function uuid_mac($uuid)
  232. {
  233. if (!\is_string($uuid = self::toString($uuid))) {
  234. trigger_error(sprintf('uuid_mac() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
  235. return null;
  236. }
  237. $parsed = self::parse($uuid);
  238. if (self::UUID_TYPE_TIME !== ($parsed['version'] ?? null)) {
  239. if (80000 > \PHP_VERSION_ID) {
  240. return false;
  241. }
  242. throw new \ValueError('uuid_mac(): Argument #1 ($uuid) UUID DCE TIME expected');
  243. }
  244. return strtr($parsed['node'], 'ABCDEF', 'abcdef');
  245. }
  246. public static function uuid_parse($uuid)
  247. {
  248. if (!\is_string($uuid = self::toString($uuid))) {
  249. trigger_error(sprintf('uuid_parse() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
  250. return null;
  251. }
  252. if (!self::isValid($uuid)) {
  253. if (80000 > \PHP_VERSION_ID) {
  254. return false;
  255. }
  256. throw new \ValueError('uuid_parse(): Argument #1 ($uuid) UUID expected');
  257. }
  258. return hex2bin(str_replace('-', '', $uuid));
  259. }
  260. public static function uuid_unparse($bytes)
  261. {
  262. if (!\is_string($bytes = self::toString($bytes))) {
  263. trigger_error(sprintf('uuid_unparse() expects parameter 1 to be string, %s given', \gettype($bytes)), \E_USER_WARNING);
  264. return null;
  265. }
  266. if (16 !== \strlen($bytes)) {
  267. if (80000 > \PHP_VERSION_ID) {
  268. return false;
  269. }
  270. throw new \ValueError('uuid_unparse(): Argument #1 ($uuid) UUID expected');
  271. }
  272. $uuid = bin2hex($bytes);
  273. $uuid = substr_replace($uuid, '-', 8, 0);
  274. $uuid = substr_replace($uuid, '-', 13, 0);
  275. $uuid = substr_replace($uuid, '-', 18, 0);
  276. return substr_replace($uuid, '-', 23, 0);
  277. }
  278. private static function uuid_generate_random()
  279. {
  280. $uuid = bin2hex(random_bytes(16));
  281. return sprintf('%08s-%04s-4%03s-%04x-%012s',
  282. // 32 bits for "time_low"
  283. substr($uuid, 0, 8),
  284. // 16 bits for "time_mid"
  285. substr($uuid, 8, 4),
  286. // 16 bits for "time_hi_and_version",
  287. // four most significant bits holds version number 4
  288. substr($uuid, 13, 3),
  289. // 16 bits:
  290. // * 8 bits for "clk_seq_hi_res",
  291. // * 8 bits for "clk_seq_low",
  292. // two most significant bits holds zero and one for variant DCE1.1
  293. hexdec(substr($uuid, 16, 4)) & 0x3FFF | 0x8000,
  294. // 48 bits for "node"
  295. substr($uuid, 20, 12)
  296. );
  297. }
  298. /**
  299. * @see http://tools.ietf.org/html/rfc4122#section-4.2.2
  300. */
  301. private static function uuid_generate_time()
  302. {
  303. $time = microtime(false);
  304. $time = substr($time, 11).substr($time, 2, 7);
  305. if (\PHP_INT_SIZE >= 8) {
  306. $time = str_pad(dechex($time + self::TIME_OFFSET_INT), 16, '0', \STR_PAD_LEFT);
  307. } else {
  308. $time = str_pad(self::toBinary($time), 8, "\0", \STR_PAD_LEFT);
  309. $time = self::binaryAdd($time, self::TIME_OFFSET_BIN);
  310. $time = bin2hex($time);
  311. }
  312. // https://tools.ietf.org/html/rfc4122#section-4.1.5
  313. // We are using a random data for the sake of simplicity: since we are
  314. // not able to get a super precise timeOfDay as a unique sequence
  315. $clockSeq = random_int(0, 0x3FFF);
  316. static $node;
  317. if (null === $node) {
  318. if (\function_exists('apcu_fetch')) {
  319. $node = apcu_fetch('__symfony_uuid_node');
  320. if (false === $node) {
  321. $node = sprintf('%06x%06x',
  322. random_int(0, 0xFFFFFF) | 0x010000,
  323. random_int(0, 0xFFFFFF)
  324. );
  325. apcu_store('__symfony_uuid_node', $node);
  326. }
  327. } else {
  328. $node = sprintf('%06x%06x',
  329. random_int(0, 0xFFFFFF) | 0x010000,
  330. random_int(0, 0xFFFFFF)
  331. );
  332. }
  333. }
  334. return sprintf('%08s-%04s-1%03s-%04x-%012s',
  335. // 32 bits for "time_low"
  336. substr($time, -8),
  337. // 16 bits for "time_mid"
  338. substr($time, -12, 4),
  339. // 16 bits for "time_hi_and_version",
  340. // four most significant bits holds version number 1
  341. substr($time, -15, 3),
  342. // 16 bits:
  343. // * 8 bits for "clk_seq_hi_res",
  344. // * 8 bits for "clk_seq_low",
  345. // two most significant bits holds zero and one for variant DCE1.1
  346. $clockSeq | 0x8000,
  347. // 48 bits for "node"
  348. $node
  349. );
  350. }
  351. private static function isValid($uuid)
  352. {
  353. return (bool) preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid);
  354. }
  355. private static function parse($uuid)
  356. {
  357. if (!preg_match('{^(?<time_low>[0-9a-f]{8})-(?<time_mid>[0-9a-f]{4})-(?<version>[0-9a-f])(?<time_hi>[0-9a-f]{3})-(?<clock_seq>[0-9a-f]{4})-(?<node>[0-9a-f]{12})$}Di', $uuid, $matches)) {
  358. return null;
  359. }
  360. return [
  361. 'time' => '0'.$matches['time_hi'].$matches['time_mid'].$matches['time_low'],
  362. 'version' => hexdec($matches['version']),
  363. 'clock_seq' => hexdec($matches['clock_seq']),
  364. 'node' => $matches['node'],
  365. ];
  366. }
  367. private static function toString($v)
  368. {
  369. if (\is_string($v) || null === $v || (\is_object($v) ? method_exists($v, '__toString') : \is_scalar($v))) {
  370. return (string) $v;
  371. }
  372. return $v;
  373. }
  374. private static function toBinary($digits)
  375. {
  376. $bytes = '';
  377. $count = \strlen($digits);
  378. while ($count) {
  379. $quotient = [];
  380. $remainder = 0;
  381. for ($i = 0; $i !== $count; ++$i) {
  382. $carry = $digits[$i] + $remainder * 10;
  383. $digit = $carry >> 8;
  384. $remainder = $carry & 0xFF;
  385. if ($digit || $quotient) {
  386. $quotient[] = $digit;
  387. }
  388. }
  389. $bytes = \chr($remainder).$bytes;
  390. $count = \count($digits = $quotient);
  391. }
  392. return $bytes;
  393. }
  394. private static function toDecimal($bytes)
  395. {
  396. $digits = '';
  397. $bytes = array_values(unpack('C*', $bytes));
  398. while ($count = \count($bytes)) {
  399. $quotient = [];
  400. $remainder = 0;
  401. for ($i = 0; $i !== $count; ++$i) {
  402. $carry = $bytes[$i] + ($remainder << 8);
  403. $digit = (int) ($carry / 10);
  404. $remainder = $carry % 10;
  405. if ($digit || $quotient) {
  406. $quotient[] = $digit;
  407. }
  408. }
  409. $digits = $remainder.$digits;
  410. $bytes = $quotient;
  411. }
  412. return $digits;
  413. }
  414. private static function binaryAdd($a, $b)
  415. {
  416. $sum = 0;
  417. for ($i = 7; 0 <= $i; --$i) {
  418. $sum += \ord($a[$i]) + \ord($b[$i]);
  419. $a[$i] = \chr($sum & 0xFF);
  420. $sum >>= 8;
  421. }
  422. return $a;
  423. }
  424. }