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