BlueGit

projects-timfalken-com microchess

Files

microchess / web / lib / push / vendor / symfony / polyfill-php82 / Php82.php

Php82.php

Php82.php 12.2 KiB Download Raw
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Polyfill\Php82;
13
14/**
15 * @author Alexander M. Turek <me@derrabus.de>
16 * @author Greg Roach <greg@subaqua.co.uk>
17 *
18 * @internal
19 */
20class Php82
21{
22 /**
23 * Determines if a string matches the ODBC quoting rules.
24 *
25 * A valid quoted string begins with a '{', ends with a '}', and has no '}'
26 * inside of the string that aren't repeated (as to be escaped).
27 *
28 * These rules are what .NET also follows.
29 *
30 * @see https://github.com/php/php-src/blob/838f6bffff6363a204a2597cbfbaad1d7ee3f2b6/main/php_odbc_utils.c#L31-L57
31 */
32 public static function odbc_connection_string_is_quoted(string $str): bool
33 {
34 if ('' === $str || '{' !== $str[0]) {
35 return false;
36 }
37
38 /* Check for } that aren't doubled up or at the end of the string */
39 $length = \strlen($str) - 1;
40 for ($i = 0; $i < $length; ++$i) {
41 if ('}' !== $str[$i]) {
42 continue;
43 }
44
45 if ('}' !== $str[++$i]) {
46 return $i === $length;
47 }
48 }
49
50 return true;
51 }
52
53 /**
54 * Determines if a value for a connection string should be quoted.
55 *
56 * The ODBC specification mentions:
57 * "Because of connection string and initialization file grammar, keywords and
58 * attribute values that contain the characters []{}(),;?*=!@ not enclosed
59 * with braces should be avoided."
60 *
61 * Note that it assumes that the string is *not* already quoted. You should
62 * check beforehand.
63 *
64 * @see https://github.com/php/php-src/blob/838f6bffff6363a204a2597cbfbaad1d7ee3f2b6/main/php_odbc_utils.c#L59-L73
65 */
66 public static function odbc_connection_string_should_quote(string $str): bool
67 {
68 return false !== strpbrk($str, '[]{}(),;?*=!@');
69 }
70
71 public static function odbc_connection_string_quote(string $str): string
72 {
73 return '{'.str_replace('}', '}}', $str).'}';
74 }
75
76 /**
77 * Implementation closely based on the original C code - including the GOTOs
78 * and pointer-style string access.
79 *
80 * @see https://github.com/php/php-src/blob/master/Zend/zend_ini.c
81 */
82 public static function ini_parse_quantity(string $value): int
83 {
84 // Avoid dependency on ctype_space()
85 $ctype_space = " \t\v\r\n\f";
86
87 $str = 0;
88 $str_end = \strlen($value);
89 $digits = $str;
90 $overflow = false;
91
92 /* Ignore leading whitespace, but keep it for error messages. */
93 while ($digits < $str_end && false !== strpos($ctype_space, $value[$digits])) {
94 ++$digits;
95 }
96
97 /* Ignore trailing whitespace, but keep it for error messages. */
98 while ($digits < $str_end && false !== strpos($ctype_space, $value[$str_end - 1])) {
99 --$str_end;
100 }
101
102 if ($digits === $str_end) {
103 return 0;
104 }
105
106 $is_negative = false;
107
108 if ('+' === $value[$digits]) {
109 ++$digits;
110 } elseif ('-' === $value[$digits]) {
111 $is_negative = true;
112 ++$digits;
113 }
114
115 if ($value[$digits] < '0' || $value[$digits] > 9) {
116 $message = \sprintf(
117 'Invalid quantity "%s": no valid leading digits, interpreting as "0" for backwards compatibility',
118 self::escapeString($value)
119 );
120
121 trigger_error($message, \E_USER_WARNING);
122
123 return 0;
124 }
125
126 $base = 10;
127 $allowed_digits = '0123456789';
128
129 if ('0' === $value[$digits] && ($digits + 1 === $str_end || false === strpos($allowed_digits, $value[$digits + 1]))) {
130 if ($digits + 1 === $str_end) {
131 return 0;
132 }
133
134 switch ($value[$digits + 1]) {
135 case 'g':
136 case 'G':
137 case 'm':
138 case 'M':
139 case 'k':
140 case 'K':
141 goto evaluation;
142 case 'x':
143 case 'X':
144 $base = 16;
145 $allowed_digits = '0123456789abcdefABCDEF';
146 break;
147 case 'o':
148 case 'O':
149 $base = 8;
150 $allowed_digits = '01234567';
151 break;
152 case 'b':
153 case 'B':
154 $base = 2;
155 $allowed_digits = '01';
156 break;
157 default:
158 $message = \sprintf(
159 'Invalid prefix "0%s", interpreting as "0" for backwards compatibility',
160 $value[$digits + 1]
161 );
162 trigger_error($message, \E_USER_WARNING);
163
164 return 0;
165 }
166
167 $digits += 2;
168 if ($digits === $str_end) {
169 $message = \sprintf(
170 'Invalid quantity "%s": no digits after base prefix, interpreting as "0" for backwards compatibility',
171 self::escapeString($value)
172 );
173 trigger_error($message, \E_USER_WARNING);
174
175 return 0;
176 }
177
178 $digits_consumed = $digits;
179 /* Ignore leading whitespace. */
180 while ($digits_consumed < $str_end && false !== strpos($ctype_space, $value[$digits_consumed])) {
181 ++$digits_consumed;
182 }
183 if ($digits_consumed !== $str_end && ('+' === $value[$digits_consumed] || '-' === $value[$digits_consumed])) {
184 ++$digits_consumed;
185 }
186
187 if ('0' === $value[$digits_consumed]) {
188 /* Value is just 0 */
189 if ($digits_consumed + 1 === $str_end) {
190 goto evaluation;
191 }
192 switch ($value[$digits_consumed + 1]) {
193 case 'x':
194 case 'X':
195 case 'o':
196 case 'O':
197 case 'b':
198 case 'B':
199 $digits_consumed += 2;
200 break;
201 }
202 }
203
204 if ($digits !== $digits_consumed) {
205 $message = \sprintf(
206 'Invalid quantity "%s": no digits after base prefix, interpreting as "0" for backwards compatibility',
207 self::escapeString($value)
208 );
209 trigger_error($message, \E_USER_WARNING);
210
211 return 0;
212 }
213 }
214
215 evaluation:
216
217 if (10 === $base && '0' === $value[$digits]) {
218 $base = 8;
219 $allowed_digits = '01234567';
220 }
221
222 while ($digits < $str_end && ' ' === $value[$digits]) {
223 ++$digits;
224 }
225
226 if ($digits < $str_end && '+' === $value[$digits]) {
227 ++$digits;
228 } elseif ($digits < $str_end && '-' === $value[$digits]) {
229 $is_negative = true;
230 $overflow = true;
231 ++$digits;
232 }
233
234 $digits_end = $digits;
235
236 while ($digits_end < $str_end && false !== strpos($allowed_digits, $value[$digits_end])) {
237 ++$digits_end;
238 }
239
240 $retval = base_convert(substr($value, $digits, $digits_end - $digits), $base, 10);
241
242 if ($is_negative && '0' === $retval) {
243 $is_negative = false;
244 $overflow = false;
245 }
246
247 // Check for overflow - remember that -PHP_INT_MIN = 1 + PHP_INT_MAX
248 if ($is_negative) {
249 $signed_max = strtr((string) \PHP_INT_MIN, ['-' => '']);
250 } else {
251 $signed_max = (string) \PHP_INT_MAX;
252 }
253
254 $max_length = max(\strlen($retval), \strlen($signed_max));
255
256 $tmp1 = str_pad($retval, $max_length, '0', \STR_PAD_LEFT);
257 $tmp2 = str_pad($signed_max, $max_length, '0', \STR_PAD_LEFT);
258
259 if ($tmp1 > $tmp2) {
260 $retval = -1;
261 $overflow = true;
262 } elseif ($is_negative) {
263 $retval = '-'.$retval;
264 }
265
266 $retval = (int) $retval;
267
268 if ($digits_end === $digits) {
269 $message = \sprintf(
270 'Invalid quantity "%s": no valid leading digits, interpreting as "0" for backwards compatibility',
271 self::escapeString($value)
272 );
273 trigger_error($message, \E_USER_WARNING);
274
275 return 0;
276 }
277
278 /* Allow for whitespace between integer portion and any suffix character */
279 while ($digits_end < $str_end && false !== strpos($ctype_space, $value[$digits_end])) {
280 ++$digits_end;
281 }
282
283 /* No exponent suffix. */
284 if ($digits_end === $str_end) {
285 goto end;
286 }
287
288 switch ($value[$str_end - 1]) {
289 case 'g':
290 case 'G':
291 $shift = 30;
292 break;
293 case 'm':
294 case 'M':
295 $shift = 20;
296 break;
297 case 'k':
298 case 'K':
299 $shift = 10;
300 break;
301 default:
302 /* Unknown suffix */
303 $invalid = self::escapeString($value);
304 $interpreted = self::escapeString(substr($value, $str, $digits_end - $str));
305 $chr = self::escapeString($value[$str_end - 1]);
306
307 $message = \sprintf(
308 'Invalid quantity "%s": unknown multiplier "%s", interpreting as "%s" for backwards compatibility',
309 $invalid,
310 $chr,
311 $interpreted
312 );
313
314 trigger_error($message, \E_USER_WARNING);
315
316 return $retval;
317 }
318
319 $factor = 1 << $shift;
320
321 if (!$overflow) {
322 if ($retval > 0) {
323 $overflow = $retval > \PHP_INT_MAX / $factor;
324 } else {
325 $overflow = $retval < \PHP_INT_MIN / $factor;
326 }
327 }
328
329 if (\is_float($retval * $factor)) {
330 $overflow = true;
331 $retval <<= $shift;
332 } else {
333 $retval *= $factor;
334 }
335
336 if ($digits_end !== $str_end - 1) {
337 /* More than one character in suffix */
338 $message = \sprintf(
339 'Invalid quantity "%s", interpreting as "%s%s" for backwards compatibility',
340 self::escapeString($value),
341 self::escapeString(substr($value, $str, $digits_end - $str)),
342 self::escapeString($value[$str_end - 1])
343 );
344 trigger_error($message, \E_USER_WARNING);
345
346 return $retval;
347 }
348
349 end:
350
351 if ($overflow) {
352 /* Not specifying the resulting value here because the caller may make
353 * additional conversions. Not specifying the allowed range
354 * because the caller may do narrower range checks. */
355 $message = \sprintf(
356 'Invalid quantity "%s": value is out of range, using overflow result for backwards compatibility',
357 self::escapeString($value)
358 );
359 trigger_error($message, \E_USER_WARNING);
360 }
361
362 return $retval;
363 }
364
365 /**
366 * Escape the string to avoid null bytes and to make non-printable chars visible.
367 */
368 private static function escapeString(string $string): string
369 {
370 $escaped = '';
371
372 for ($n = 0, $len = \strlen($string); $n < $len; ++$n) {
373 $c = \ord($string[$n]);
374
375 if ($c < 32 || '\\' === $string[$n] || $c > 126) {
376 switch ($string[$n]) {
377 case "\n": $escaped .= '\\n'; break;
378 case "\r": $escaped .= '\\r'; break;
379 case "\t": $escaped .= '\\t'; break;
380 case "\f": $escaped .= '\\f'; break;
381 case "\v": $escaped .= '\\v'; break;
382 case '\\': $escaped .= '\\\\'; break;
383 case "\x1B": $escaped .= '\\e'; break;
384 default:
385 $escaped .= '\\x'.strtoupper(\sprintf('%02x', $c));
386 }
387 } else {
388 $escaped .= $string[$n];
389 }
390 }
391
392 return $escaped;
393 }
394}