Debian Codegraph @

lib/3rdParty/composer/symfony/uid/BinaryUtil.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Uid;
13
14 use Symfony\Component\Uid\Exception\InvalidArgumentException;
15
16 /**
17 * @internal
18 *
19 * @author Nicolas Grekas <[email protected]>
20 */
21 class BinaryUtil
22 {
23 public const BASE10 = [
24 '' => '0123456789',
25 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
26 ];
27
28 public const BASE58 = [
29 '' => '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
30 1 => 0, 1, 2, 3, 4, 5, 6, 7, 8, 'A' => 9,
31 'B' => 10, 'C' => 11, 'D' => 12, 'E' => 13, 'F' => 14, 'G' => 15,
32 'H' => 16, 'J' => 17, 'K' => 18, 'L' => 19, 'M' => 20, 'N' => 21,
33 'P' => 22, 'Q' => 23, 'R' => 24, 'S' => 25, 'T' => 26, 'U' => 27,
34 'V' => 28, 'W' => 29, 'X' => 30, 'Y' => 31, 'Z' => 32, 'a' => 33,
35 'b' => 34, 'c' => 35, 'd' => 36, 'e' => 37, 'f' => 38, 'g' => 39,
36 'h' => 40, 'i' => 41, 'j' => 42, 'k' => 43, 'm' => 44, 'n' => 45,
37 'o' => 46, 'p' => 47, 'q' => 48, 'r' => 49, 's' => 50, 't' => 51,
38 'u' => 52, 'v' => 53, 'w' => 54, 'x' => 55, 'y' => 56, 'z' => 57,
39 ];
40
41 // https://datatracker.ietf.org/doc/html/rfc9562#section-5.1
42 // 0x01b21dd213814000 is the number of 100-ns intervals between the
43 // UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
44 private const TIME_OFFSET_INT = 0x01B21DD213814000;
45 private const TIME_OFFSET_BIN = "\x01\xb2\x1d\xd2\x13\x81\x40\x00";
46 private const TIME_OFFSET_COM1 = "\xfe\x4d\xe2\x2d\xec\x7e\xbf\xff";
47 private const TIME_OFFSET_COM2 = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
48
49 public static function toBase(string $bytes, array $map): string
50 {
51 $base = \strlen($alphabet = $map['']);
52 $bytes = array_values(unpack(\PHP_INT_SIZE >= 8 ? 'n*' : 'C*', $bytes));
53 $digits = '';
54
55 while ($count = \count($bytes)) {
56 $quotient = [];
57 $remainder = 0;
58
59 for ($i = 0; $i !== $count; ++$i) {
60 $carry = $bytes[$i] + ($remainder << (\PHP_INT_SIZE >= 8 ? 16 : 8));
61 $digit = intdiv($carry, $base);
62 $remainder = $carry % $base;
63
64 if ($digit || $quotient) {
65 $quotient[] = $digit;
66 }
67 }
68
69 $digits = $alphabet[$remainder].$digits;
70 $bytes = $quotient;
71 }
72
73 return $digits;
74 }
75
76 public static function fromBase(string $digits, array $map): string
77 {
78 $base = \strlen($map['']);
79 $count = \strlen($digits);
80 $bytes = [];
81
82 while ($count) {
83 $quotient = [];
84 $remainder = 0;
85
86 for ($i = 0; $i !== $count; ++$i) {
87 $carry = ($bytes ? $digits[$i] : $map[$digits[$i]]) + $remainder * $base;
88
89 if (\PHP_INT_SIZE >= 8) {
90 $digit = $carry >> 16;
91 $remainder = $carry & 0xFFFF;
92 } else {
93 $digit = $carry >> 8;
94 $remainder = $carry & 0xFF;
95 }
96
97 if ($digit || $quotient) {
98 $quotient[] = $digit;
99 }
100 }
101
102 $bytes[] = $remainder;
103 $count = \count($digits = $quotient);
104 }
105
106 return pack(\PHP_INT_SIZE >= 8 ? 'n*' : 'C*', ...array_reverse($bytes));
107 }
108
109 public static function add(string $a, string $b): string
110 {
111 $carry = 0;
112 for ($i = 7; 0 <= $i; --$i) {
113 $carry += \ord($a[$i]) + \ord($b[$i]);
114 $a[$i] = \chr($carry & 0xFF);
115 $carry >>= 8;
116 }
117
118 return $a;
119 }
120
121 /**
122 * @param string $time Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
123 *
124 * @return string Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 as a numeric string
125 */
126 public static function hexToNumericString(string $time): string
127 {
128 if (\PHP_INT_SIZE >= 8) {
129 $time = (string) (hexdec($time) - self::TIME_OFFSET_INT);
130 } else {
131 $time = str_pad(hex2bin($time), 8, "\0", \STR_PAD_LEFT);
132
133 if (self::TIME_OFFSET_BIN <= $time) {
134 $time = self::add($time, self::TIME_OFFSET_COM2);
135 $time[0] = $time[0] & "\x7F";
136 $time = self::toBase($time, self::BASE10);
137 } else {
138 $time = self::add($time, self::TIME_OFFSET_COM1);
139 $time = '-'.self::toBase($time ^ "\xff\xff\xff\xff\xff\xff\xff\xff", self::BASE10);
140 }
141 }
142
143 if (9 > \strlen($time)) {
144 $time = '-' === $time[0] ? '-'.str_pad(substr($time, 1), 8, '0', \STR_PAD_LEFT) : str_pad($time, 8, '0', \STR_PAD_LEFT);
145 }
146
147 return $time;
148 }
149
150 /**
151 * Sub-microseconds are lost since they are not handled by \DateTimeImmutable.
152 *
153 * @param string $time Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
154 */
155 public static function hexToDateTime(string $time): \DateTimeImmutable
156 {
157 return \DateTimeImmutable::createFromFormat('U.u?', substr_replace(self::hexToNumericString($time), '.', -7, 0));
158 }
159
160 /**
161 * @return string Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
162 */
163 public static function dateTimeToHex(\DateTimeInterface $time): string
164 {
165 if (\PHP_INT_SIZE >= 8) {
166 if (-self::TIME_OFFSET_INT > $time = (int) $time->format('Uu0')) {
167 throw new InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
168 }
169
170 return str_pad(dechex(self::TIME_OFFSET_INT + $time), 16, '0', \STR_PAD_LEFT);
171 }
172
173 $time = $time->format('Uu0');
174 $negative = '-' === $time[0];
175 if ($negative && self::TIME_OFFSET_INT < $time = substr($time, 1)) {
176 throw new InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
177 }
178 $time = self::fromBase($time, self::BASE10);
179 $time = str_pad($time, 8, "\0", \STR_PAD_LEFT);
180
181 if ($negative) {
182 $time = self::add($time, self::TIME_OFFSET_COM1) ^ "\xff\xff\xff\xff\xff\xff\xff\xff";
183 } else {
184 $time = self::add($time, self::TIME_OFFSET_BIN);
185 }
186
187 return bin2hex($time);
188 }
189 }
190
191 // @php-cs-fixer-ignore long_to_shorthand_operator To prevent false positive causing "Cannot use assign-op operators with string offsets" error