1927933f5SAndreas Gohr<?php 2927933f5SAndreas Gohrdeclare(strict_types=1); 3927933f5SAndreas Gohrnamespace ParagonIE\ConstantTime; 4927933f5SAndreas Gohr 5*8e88a29bSAndreas Gohruse SensitiveParameter; 6927933f5SAndreas Gohruse TypeError; 7*8e88a29bSAndreas Gohruse function strlen; 8*8e88a29bSAndreas Gohruse function substr; 9927933f5SAndreas Gohr 10927933f5SAndreas Gohr/** 11927933f5SAndreas Gohr * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. 12927933f5SAndreas Gohr * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) 13927933f5SAndreas Gohr * 14927933f5SAndreas Gohr * Permission is hereby granted, free of charge, to any person obtaining a copy 15927933f5SAndreas Gohr * of this software and associated documentation files (the "Software"), to deal 16927933f5SAndreas Gohr * in the Software without restriction, including without limitation the rights 17927933f5SAndreas Gohr * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18927933f5SAndreas Gohr * copies of the Software, and to permit persons to whom the Software is 19927933f5SAndreas Gohr * furnished to do so, subject to the following conditions: 20927933f5SAndreas Gohr * 21927933f5SAndreas Gohr * The above copyright notice and this permission notice shall be included in all 22927933f5SAndreas Gohr * copies or substantial portions of the Software. 23927933f5SAndreas Gohr * 24927933f5SAndreas Gohr * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25927933f5SAndreas Gohr * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26927933f5SAndreas Gohr * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27927933f5SAndreas Gohr * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28927933f5SAndreas Gohr * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29927933f5SAndreas Gohr * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30927933f5SAndreas Gohr * SOFTWARE. 31927933f5SAndreas Gohr */ 32927933f5SAndreas Gohr 33927933f5SAndreas Gohr/** 34927933f5SAndreas Gohr * Class Binary 35927933f5SAndreas Gohr * 36927933f5SAndreas Gohr * Binary string operators that don't choke on 37927933f5SAndreas Gohr * mbstring.func_overload 38927933f5SAndreas Gohr * 39927933f5SAndreas Gohr * @package ParagonIE\ConstantTime 40927933f5SAndreas Gohr */ 41927933f5SAndreas Gohrabstract class Binary 42927933f5SAndreas Gohr{ 43927933f5SAndreas Gohr /** 44927933f5SAndreas Gohr * Safe string length 45927933f5SAndreas Gohr * 46927933f5SAndreas Gohr * @ref mbstring.func_overload 47927933f5SAndreas Gohr * 48927933f5SAndreas Gohr * @param string $str 49927933f5SAndreas Gohr * @return int 50927933f5SAndreas Gohr */ 51850e6620SAndreas Gohr public static function safeStrlen( 52*8e88a29bSAndreas Gohr #[SensitiveParameter] 53850e6620SAndreas Gohr string $str 54850e6620SAndreas Gohr ): int { 55*8e88a29bSAndreas Gohr return strlen($str); 56927933f5SAndreas Gohr } 57927933f5SAndreas Gohr 58927933f5SAndreas Gohr /** 59927933f5SAndreas Gohr * Safe substring 60927933f5SAndreas Gohr * 61927933f5SAndreas Gohr * @ref mbstring.func_overload 62927933f5SAndreas Gohr * 63927933f5SAndreas Gohr * @staticvar boolean $exists 64927933f5SAndreas Gohr * @param string $str 65927933f5SAndreas Gohr * @param int $start 66927933f5SAndreas Gohr * @param ?int $length 67927933f5SAndreas Gohr * @return string 68927933f5SAndreas Gohr * 69927933f5SAndreas Gohr * @throws TypeError 70927933f5SAndreas Gohr */ 71927933f5SAndreas Gohr public static function safeSubstr( 72*8e88a29bSAndreas Gohr #[SensitiveParameter] 73927933f5SAndreas Gohr string $str, 74927933f5SAndreas Gohr int $start = 0, 75*8e88a29bSAndreas Gohr ?int $length = null 76927933f5SAndreas Gohr ): string { 77927933f5SAndreas Gohr if ($length === 0) { 78927933f5SAndreas Gohr return ''; 79927933f5SAndreas Gohr } 80927933f5SAndreas Gohr // Unlike mb_substr(), substr() doesn't accept NULL for length 81927933f5SAndreas Gohr if ($length !== null) { 82*8e88a29bSAndreas Gohr return substr($str, $start, $length); 83927933f5SAndreas Gohr } else { 84*8e88a29bSAndreas Gohr return substr($str, $start); 85927933f5SAndreas Gohr } 86927933f5SAndreas Gohr } 87927933f5SAndreas Gohr} 88