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