Php82.php 12 KB

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