defender-security
/
vendor
/
spomky-labs
/
cbor-php
/
src
/
OtherObject
/
DoublePrecisionFloatObject.php
DoublePrecisionFloatObject.php in Defender Security – Malware Scanner, Login Security & Firewall 5.6.1, at vendor/spomky-labs/cbor-php/src/OtherObject/DoublePrecisionFloatObject.php
| 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\OtherObject; |
| 15 | |
| 16 | use Brick\Math\BigInteger; |
| 17 | use CBOR\Normalizable; |
| 18 | use CBOR\OtherObject as Base; |
| 19 | use CBOR\Utils; |
| 20 | use const INF; |
| 21 | use InvalidArgumentException; |
| 22 | use const NAN; |
| 23 | |
| 24 | final class DoublePrecisionFloatObject extends Base implements Normalizable |
| 25 | { |
| 26 | public static function supportedAdditionalInformation(): array |
| 27 | { |
| 28 | return [self::OBJECT_DOUBLE_PRECISION_FLOAT]; |
| 29 | } |
| 30 | |
| 31 | public static function createFromLoadedData(int $additionalInformation, ?string $data): Base |
| 32 | { |
| 33 | return new self($additionalInformation, $data); |
| 34 | } |
| 35 | |
| 36 | public static function create(string $value): self |
| 37 | { |
| 38 | if (mb_strlen($value, '8bit') !== 8) { |
| 39 | throw new InvalidArgumentException('The value is not a valid double precision floating point'); |
| 40 | } |
| 41 | |
| 42 | return new self(self::OBJECT_DOUBLE_PRECISION_FLOAT, $value); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @deprecated The method will be removed on v3.0. Please rely on the CBOR\Normalizable interface |
| 47 | */ |
| 48 | public function getNormalizedData(bool $ignoreTags = false) |
| 49 | { |
| 50 | return $this->normalize(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return float|int |
| 55 | */ |
| 56 | public function normalize() |
| 57 | { |
| 58 | $exponent = $this->getExponent(); |
| 59 | $mantissa = $this->getMantissa(); |
| 60 | $sign = $this->getSign(); |
| 61 | |
| 62 | if ($exponent === 0) { |
| 63 | $val = $mantissa * 2 ** (-(1022 + 52)); |
| 64 | } elseif ($exponent !== 0b11111111111) { |
| 65 | $val = ($mantissa + (1 << 52)) * 2 ** ($exponent - (1023 + 52)); |
| 66 | } else { |
| 67 | $val = $mantissa === 0 ? INF : NAN; |
| 68 | } |
| 69 | |
| 70 | return $sign * $val; |
| 71 | } |
| 72 | |
| 73 | public function getExponent(): int |
| 74 | { |
| 75 | $data = $this->data; |
| 76 | Utils::assertString($data, 'Invalid data'); |
| 77 | |
| 78 | return Utils::binToBigInteger($data)->shiftedRight(52)->and(Utils::hexToBigInteger('7ff'))->toInt(); |
| 79 | } |
| 80 | |
| 81 | public function getMantissa(): int |
| 82 | { |
| 83 | $data = $this->data; |
| 84 | Utils::assertString($data, 'Invalid data'); |
| 85 | |
| 86 | return Utils::binToBigInteger($data)->and(Utils::hexToBigInteger('fffffffffffff'))->toInt(); |
| 87 | } |
| 88 | |
| 89 | public function getSign(): int |
| 90 | { |
| 91 | $data = $this->data; |
| 92 | Utils::assertString($data, 'Invalid data'); |
| 93 | $sign = Utils::binToBigInteger($data)->shiftedRight(63); |
| 94 | |
| 95 | return $sign->isEqualTo(BigInteger::one()) ? -1 : 1; |
| 96 | } |
| 97 | } |
| 98 |