Binary.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace ParagonIE\ConstantTime;
  3. /**
  4. * Copyright (c) 2016 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 \mb_strlen($str, '8bit');
  47. } else {
  48. return \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 ($length === null) {
  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 ($length !== null) {
  89. return \substr($str, $start, $length);
  90. } else {
  91. return \substr($str, $start);
  92. }
  93. }
  94. }