PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.4.0
Defender Security – Malware Scanner, Login Security & Firewall v5.4.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / spomky-labs / cbor-php / src / NegativeIntegerObject.php

NegativeIntegerObject.php in Defender Security – Malware Scanner, Login Security & Firewall 5.4.0, at vendor/spomky-labs/cbor-php/src/NegativeIntegerObject.php

137 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 InvalidArgumentException;
18 use const STR_PAD_LEFT;
19
20 /**
21 * @final
22 */
23 class NegativeIntegerObject extends AbstractCBORObject implements Normalizable
24 {
25 private const MAJOR_TYPE = self::MAJOR_TYPE_NEGATIVE_INTEGER;
26
27 /**
28 * @var string|null
29 */
30 private $data;
31
32 public function __construct(int $additionalInformation, ?string $data)
33 {
34 parent::__construct(self::MAJOR_TYPE, $additionalInformation);
35 $this->data = $data;
36 }
37
38 public function __toString(): string
39 {
40 $result = parent::__toString();
41 if ($this->data !== null) {
42 $result .= $this->data;
43 }
44
45 return $result;
46 }
47
48 public static function createObjectForValue(int $additionalInformation, ?string $data): self
49 {
50 return new self($additionalInformation, $data);
51 }
52
53 public static function create(int $value): self
54 {
55 return self::createFromString((string) $value);
56 }
57
58 public static function createFromString(string $value): self
59 {
60 $integer = BigInteger::of($value);
61
62 return self::createBigInteger($integer);
63 }
64
65 public function getValue(): string
66 {
67 if ($this->data === null) {
68 return (string) (-1 - $this->additionalInformation);
69 }
70
71 $result = Utils::binToBigInteger($this->data);
72 $minusOne = BigInteger::of(-1);
73
74 return $minusOne->minus($result)
75 ->toBase(10)
76 ;
77 }
78
79 public function normalize(): string
80 {
81 return $this->getValue();
82 }
83
84 /**
85 * @deprecated The method will be removed on v3.0. Please rely on the CBOR\Normalizable interface
86 */
87 public function getNormalizedData(bool $ignoreTags = false): string
88 {
89 return $this->getValue();
90 }
91
92 private static function createBigInteger(BigInteger $integer): self
93 {
94 if ($integer->isGreaterThanOrEqualTo(BigInteger::zero())) {
95 throw new InvalidArgumentException('The value must be a negative integer.');
96 }
97
98 $minusOne = BigInteger::of(-1);
99 $computed_value = $minusOne->minus($integer);
100
101 switch (true) {
102 case $computed_value->isLessThan(BigInteger::of(24)):
103 $ai = $computed_value->toInt();
104 $data = null;
105 break;
106 case $computed_value->isLessThan(BigInteger::fromBase('FF', 16)):
107 $ai = 24;
108 $data = self::hex2bin(str_pad($computed_value->toBase(16), 2, '0', STR_PAD_LEFT));
109 break;
110 case $computed_value->isLessThan(BigInteger::fromBase('FFFF', 16)):
111 $ai = 25;
112 $data = self::hex2bin(str_pad($computed_value->toBase(16), 4, '0', STR_PAD_LEFT));
113 break;
114 case $computed_value->isLessThan(BigInteger::fromBase('FFFFFFFF', 16)):
115 $ai = 26;
116 $data = self::hex2bin(str_pad($computed_value->toBase(16), 8, '0', STR_PAD_LEFT));
117 break;
118 default:
119 throw new InvalidArgumentException(
120 'Out of range. Please use NegativeBigIntegerTag tag with ByteStringObject object instead.'
121 );
122 }
123
124 return new self($ai, $data);
125 }
126
127 private static function hex2bin(string $data): string
128 {
129 $result = hex2bin($data);
130 if ($result === false) {
131 throw new InvalidArgumentException('Unable to convert the data');
132 }
133
134 return $result;
135 }
136 }
137