[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 declare(strict_types=1); 4 5 /* 6 * The MIT License (MIT) 7 * 8 * Copyright (c) 2018-2020 Spomky-Labs 9 * 10 * This software may be modified and distributed under the terms 11 * of the MIT license. See the LICENSE file for details. 12 */ 13 14 namespace CBOR; 15 16 use Brick\Math\BigInteger; 17 use function chr; 18 use function count; 19 use InvalidArgumentException; 20 21 final class LengthCalculator 22 { 23 public static function getLengthOfString(string $data): array 24 { 25 $length = mb_strlen($data, '8bit'); 26 27 return self::computeLength($length); 28 } 29 30 public static function getLengthOfArray(array $data): array 31 { 32 $length = count($data); 33 34 return self::computeLength($length); 35 } 36 37 private static function computeLength(int $length): array 38 { 39 switch (true) { 40 case $length < 24: 41 return [$length, null]; 42 case $length < 0xFF: 43 return [24, chr($length)]; 44 case $length < 0xFFFF: 45 return [25, self::hex2bin(static::fixHexLength(Utils::intToHex($length)))]; 46 case $length < 0xFFFFFFFF: 47 return [26, self::hex2bin(static::fixHexLength(Utils::intToHex($length)))]; 48 case BigInteger::of($length)->isLessThan(BigInteger::fromBase('FFFFFFFFFFFFFFFF', 16)): 49 return [27, self::hex2bin(static::fixHexLength(Utils::intToHex($length)))]; 50 default: 51 return [31, null]; 52 } 53 } 54 55 private static function hex2bin(string $data): string 56 { 57 $result = hex2bin($data); 58 if (false === $result) { 59 throw new InvalidArgumentException('Unable to convert the data'); 60 } 61 62 return $result; 63 } 64 65 private static function fixHexLength(string $data): string 66 { 67 return str_pad($data, (int) (2 ** ceil(log(mb_strlen($data, '8bit'), 2))), '0', STR_PAD_LEFT); 68 } 69 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |