Server IP : 104.21.14.48 / Your IP : 18.119.29.70 [ Web Server : Apache System : Linux b70eb322-3aee-0c53-7c82-0db91281f2c6.secureserver.net 6.1.90-1.el9.elrepo.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 2 12:09:22 EDT 2024 x86_64 User : root ( 0) PHP Version : 8.0.30.2 Disable Function : NONE Domains : 0 Domains MySQL : ON | cURL : ON | WGET : ON | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/wp-content/plugins/defender-security/vendor/spomky-labs/cbor-php/src/ |
Upload File : |
<?php declare(strict_types=1); /* * The MIT License (MIT) * * Copyright (c) 2018-2020 Spomky-Labs * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. */ namespace CBOR; use Brick\Math\BigInteger; use function chr; use function count; use InvalidArgumentException; use const STR_PAD_LEFT; final class LengthCalculator { /** * @return array{int, null|string} */ public static function getLengthOfString(string $data): array { $length = mb_strlen($data, '8bit'); return self::computeLength($length); } /** * @param array<int|string, mixed> $data * * @return array{int, null|string} */ public static function getLengthOfArray(array $data): array { $length = count($data); return self::computeLength($length); } /** * @return array{int, null|string} */ private static function computeLength(int $length): array { switch (true) { case $length <= 23: return [$length, null]; case $length <= 0xFF: return [CBORObject::LENGTH_1_BYTE, chr($length)]; case $length <= 0xFFFF: return [CBORObject::LENGTH_2_BYTES, self::hex2bin(dechex($length))]; case $length <= 0xFFFFFFFF: return [CBORObject::LENGTH_4_BYTES, self::hex2bin(dechex($length))]; case BigInteger::of($length)->isLessThanOrEqualTo(BigInteger::fromBase('FFFFFFFFFFFFFFFF', 16)): return [CBORObject::LENGTH_8_BYTES, self::hex2bin(dechex($length))]; default: return [CBORObject::LENGTH_INDEFINITE, null]; } } private static function hex2bin(string $data): string { $data = str_pad($data, (int) (2 ** ceil(log(mb_strlen($data, '8bit'), 2))), '0', STR_PAD_LEFT); $result = hex2bin($data); if ($result === false) { throw new InvalidArgumentException('Unable to convert the data'); } return $result; } }