BinaryUtil.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\Component\Uid;
  11. /**
  12. * @internal
  13. *
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class BinaryUtil
  17. {
  18. public const BASE10 = [
  19. '' => '0123456789',
  20. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  21. ];
  22. public const BASE58 = [
  23. '' => '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
  24. 1 => 0, 1, 2, 3, 4, 5, 6, 7, 8, 'A' => 9,
  25. 'B' => 10, 'C' => 11, 'D' => 12, 'E' => 13, 'F' => 14, 'G' => 15,
  26. 'H' => 16, 'J' => 17, 'K' => 18, 'L' => 19, 'M' => 20, 'N' => 21,
  27. 'P' => 22, 'Q' => 23, 'R' => 24, 'S' => 25, 'T' => 26, 'U' => 27,
  28. 'V' => 28, 'W' => 29, 'X' => 30, 'Y' => 31, 'Z' => 32, 'a' => 33,
  29. 'b' => 34, 'c' => 35, 'd' => 36, 'e' => 37, 'f' => 38, 'g' => 39,
  30. 'h' => 40, 'i' => 41, 'j' => 42, 'k' => 43, 'm' => 44, 'n' => 45,
  31. 'o' => 46, 'p' => 47, 'q' => 48, 'r' => 49, 's' => 50, 't' => 51,
  32. 'u' => 52, 'v' => 53, 'w' => 54, 'x' => 55, 'y' => 56, 'z' => 57,
  33. ];
  34. // https://datatracker.ietf.org/doc/html/rfc9562#section-5.1
  35. // 0x01b21dd213814000 is the number of 100-ns intervals between the
  36. // UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
  37. private const TIME_OFFSET_INT = 0x01B21DD213814000;
  38. private const TIME_OFFSET_BIN = "\x01\xb2\x1d\xd2\x13\x81\x40\x00";
  39. private const TIME_OFFSET_COM1 = "\xfe\x4d\xe2\x2d\xec\x7e\xbf\xff";
  40. private const TIME_OFFSET_COM2 = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
  41. public static function toBase(string $bytes, array $map): string
  42. {
  43. $base = \strlen($alphabet = $map['']);
  44. $bytes = array_values(unpack(\PHP_INT_SIZE >= 8 ? 'n*' : 'C*', $bytes));
  45. $digits = '';
  46. while ($count = \count($bytes)) {
  47. $quotient = [];
  48. $remainder = 0;
  49. for ($i = 0; $i !== $count; ++$i) {
  50. $carry = $bytes[$i] + ($remainder << (\PHP_INT_SIZE >= 8 ? 16 : 8));
  51. $digit = intdiv($carry, $base);
  52. $remainder = $carry % $base;
  53. if ($digit || $quotient) {
  54. $quotient[] = $digit;
  55. }
  56. }
  57. $digits = $alphabet[$remainder].$digits;
  58. $bytes = $quotient;
  59. }
  60. return $digits;
  61. }
  62. public static function fromBase(string $digits, array $map): string
  63. {
  64. $base = \strlen($map['']);
  65. $count = \strlen($digits);
  66. $bytes = [];
  67. while ($count) {
  68. $quotient = [];
  69. $remainder = 0;
  70. for ($i = 0; $i !== $count; ++$i) {
  71. $carry = ($bytes ? $digits[$i] : $map[$digits[$i]]) + $remainder * $base;
  72. if (\PHP_INT_SIZE >= 8) {
  73. $digit = $carry >> 16;
  74. $remainder = $carry & 0xFFFF;
  75. } else {
  76. $digit = $carry >> 8;
  77. $remainder = $carry & 0xFF;
  78. }
  79. if ($digit || $quotient) {
  80. $quotient[] = $digit;
  81. }
  82. }
  83. $bytes[] = $remainder;
  84. $count = \count($digits = $quotient);
  85. }
  86. return pack(\PHP_INT_SIZE >= 8 ? 'n*' : 'C*', ...array_reverse($bytes));
  87. }
  88. public static function add(string $a, string $b): string
  89. {
  90. $carry = 0;
  91. for ($i = 7; 0 <= $i; --$i) {
  92. $carry += \ord($a[$i]) + \ord($b[$i]);
  93. $a[$i] = \chr($carry & 0xFF);
  94. $carry >>= 8;
  95. }
  96. return $a;
  97. }
  98. /**
  99. * @param string $time Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
  100. */
  101. public static function hexToDateTime(string $time): \DateTimeImmutable
  102. {
  103. if (\PHP_INT_SIZE >= 8) {
  104. $time = (string) (hexdec($time) - self::TIME_OFFSET_INT);
  105. } else {
  106. $time = str_pad(hex2bin($time), 8, "\0", \STR_PAD_LEFT);
  107. if (self::TIME_OFFSET_BIN <= $time) {
  108. $time = self::add($time, self::TIME_OFFSET_COM2);
  109. $time[0] = $time[0] & "\x7F";
  110. $time = self::toBase($time, self::BASE10);
  111. } else {
  112. $time = self::add($time, self::TIME_OFFSET_COM1);
  113. $time = '-'.self::toBase($time ^ "\xff\xff\xff\xff\xff\xff\xff\xff", self::BASE10);
  114. }
  115. }
  116. if (9 > \strlen($time)) {
  117. $time = '-' === $time[0] ? '-'.str_pad(substr($time, 1), 8, '0', \STR_PAD_LEFT) : str_pad($time, 8, '0', \STR_PAD_LEFT);
  118. }
  119. return \DateTimeImmutable::createFromFormat('U.u?', substr_replace($time, '.', -7, 0));
  120. }
  121. /**
  122. * @return string Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
  123. */
  124. public static function dateTimeToHex(\DateTimeInterface $time): string
  125. {
  126. if (\PHP_INT_SIZE >= 8) {
  127. if (-self::TIME_OFFSET_INT > $time = (int) $time->format('Uu0')) {
  128. throw new \InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
  129. }
  130. return str_pad(dechex(self::TIME_OFFSET_INT + $time), 16, '0', \STR_PAD_LEFT);
  131. }
  132. $time = $time->format('Uu0');
  133. $negative = '-' === $time[0];
  134. if ($negative && self::TIME_OFFSET_INT < $time = substr($time, 1)) {
  135. throw new \InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
  136. }
  137. $time = self::fromBase($time, self::BASE10);
  138. $time = str_pad($time, 8, "\0", \STR_PAD_LEFT);
  139. if ($negative) {
  140. $time = self::add($time, self::TIME_OFFSET_COM1) ^ "\xff\xff\xff\xff\xff\xff\xff\xff";
  141. } else {
  142. $time = self::add($time, self::TIME_OFFSET_BIN);
  143. }
  144. return bin2hex($time);
  145. }
  146. }