Binary.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace ParagonIE\ConstantTime;
  3. /**
  4. * Copyright (c) 2016 - 2017 Paragon Initiative Enterprises.
  5. * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. /**
  26. * Class Binary
  27. *
  28. * Binary string operators that don't choke on
  29. * mbstring.func_overload
  30. *
  31. * @package ParagonIE\ConstantTime
  32. */
  33. abstract class Binary
  34. {
  35. /**
  36. * Safe string length
  37. *
  38. * @ref mbstring.func_overload
  39. *
  40. * @param string $str
  41. * @return int
  42. */
  43. public static function safeStrlen($str)
  44. {
  45. if (\function_exists('mb_strlen')) {
  46. return (int) \mb_strlen($str, '8bit');
  47. } else {
  48. return (int) \strlen($str);
  49. }
  50. }
  51. /**
  52. * Safe substring
  53. *
  54. * @ref mbstring.func_overload
  55. *
  56. * @staticvar boolean $exists
  57. * @param string $str
  58. * @param int $start
  59. * @param int $length
  60. * @return string
  61. * @throws \TypeError
  62. */
  63. public static function safeSubstr(
  64. $str,
  65. $start = 0,
  66. $length = \null
  67. ) {
  68. if (\function_exists('mb_substr')) {
  69. // mb_substr($str, 0, null, '8bit') returns an empty string on PHP
  70. // 5.3, so we have to find the length ourselves.
  71. if (\is_null($length)) {
  72. if ($start >= 0) {
  73. $length = self::safeStrlen($str) - $start;
  74. } else {
  75. $length = -$start;
  76. }
  77. }
  78. // $length calculation above might result in a 0-length string
  79. if ($length === 0) {
  80. return '';
  81. }
  82. return \mb_substr($str, $start, $length, '8bit');
  83. }
  84. if ($length === 0) {
  85. return '';
  86. }
  87. // Unlike mb_substr(), substr() doesn't accept null for length
  88. if (!is_null($length)) {
  89. return \substr($str, $start, $length);
  90. } else {
  91. return \substr($str, $start);
  92. }
  93. }
  94. }