GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — php70 ( e15417...9941dc )
by Joni
03:18
created
lib/ASN1/Type/TimeType.php 1 patch
Indentation   +78 added lines, -78 removed lines patch added patch discarded remove patch
@@ -10,88 +10,88 @@
 block discarded – undo
10 10
  */
11 11
 abstract class TimeType extends Element
12 12
 {
13
-    /**
14
-     * UTC timezone.
15
-     *
16
-     * @var string
17
-     */
18
-    const TZ_UTC = "UTC";
13
+	/**
14
+	 * UTC timezone.
15
+	 *
16
+	 * @var string
17
+	 */
18
+	const TZ_UTC = "UTC";
19 19
     
20
-    /**
21
-     * Date and time.
22
-     *
23
-     * @var \DateTimeImmutable $_dateTime
24
-     */
25
-    protected $_dateTime;
20
+	/**
21
+	 * Date and time.
22
+	 *
23
+	 * @var \DateTimeImmutable $_dateTime
24
+	 */
25
+	protected $_dateTime;
26 26
     
27
-    /**
28
-     * Constructor.
29
-     *
30
-     * @param \DateTimeImmutable $dt
31
-     */
32
-    public function __construct(\DateTimeImmutable $dt)
33
-    {
34
-        $this->_dateTime = $dt;
35
-    }
27
+	/**
28
+	 * Constructor.
29
+	 *
30
+	 * @param \DateTimeImmutable $dt
31
+	 */
32
+	public function __construct(\DateTimeImmutable $dt)
33
+	{
34
+		$this->_dateTime = $dt;
35
+	}
36 36
     
37
-    /**
38
-     * Initialize from datetime string.
39
-     *
40
-     * @link http://php.net/manual/en/datetime.formats.php
41
-     * @param string $time Time string
42
-     * @param string|null $tz Timezone, if null use default.
43
-     * @throws \RuntimeException
44
-     * @return self
45
-     */
46
-    public static function fromString(string $time, string $tz = null): self
47
-    {
48
-        try {
49
-            if (!isset($tz)) {
50
-                $tz = date_default_timezone_get();
51
-            }
52
-            return new static(
53
-                new \DateTimeImmutable($time, self::_createTimeZone($tz)));
54
-        } catch (\Exception $e) {
55
-            throw new \RuntimeException(
56
-                "Failed to create DateTime: " .
57
-                self::_getLastDateTimeImmutableErrorsStr(), 0, $e);
58
-        }
59
-    }
37
+	/**
38
+	 * Initialize from datetime string.
39
+	 *
40
+	 * @link http://php.net/manual/en/datetime.formats.php
41
+	 * @param string $time Time string
42
+	 * @param string|null $tz Timezone, if null use default.
43
+	 * @throws \RuntimeException
44
+	 * @return self
45
+	 */
46
+	public static function fromString(string $time, string $tz = null): self
47
+	{
48
+		try {
49
+			if (!isset($tz)) {
50
+				$tz = date_default_timezone_get();
51
+			}
52
+			return new static(
53
+				new \DateTimeImmutable($time, self::_createTimeZone($tz)));
54
+		} catch (\Exception $e) {
55
+			throw new \RuntimeException(
56
+				"Failed to create DateTime: " .
57
+				self::_getLastDateTimeImmutableErrorsStr(), 0, $e);
58
+		}
59
+	}
60 60
     
61
-    /**
62
-     * Get the date and time.
63
-     *
64
-     * @return \DateTimeImmutable
65
-     */
66
-    public function dateTime(): \DateTimeImmutable
67
-    {
68
-        return $this->_dateTime;
69
-    }
61
+	/**
62
+	 * Get the date and time.
63
+	 *
64
+	 * @return \DateTimeImmutable
65
+	 */
66
+	public function dateTime(): \DateTimeImmutable
67
+	{
68
+		return $this->_dateTime;
69
+	}
70 70
     
71
-    /**
72
-     * Create DateTimeZone object from string.
73
-     *
74
-     * @param string $tz
75
-     * @throws \UnexpectedValueException If timezone is invalid
76
-     * @return \DateTimeZone
77
-     */
78
-    protected static function _createTimeZone(string $tz): \DateTimeZone
79
-    {
80
-        try {
81
-            return new \DateTimeZone($tz);
82
-        } catch (\Exception $e) {
83
-            throw new \UnexpectedValueException("Invalid timezone.", 0, $e);
84
-        }
85
-    }
71
+	/**
72
+	 * Create DateTimeZone object from string.
73
+	 *
74
+	 * @param string $tz
75
+	 * @throws \UnexpectedValueException If timezone is invalid
76
+	 * @return \DateTimeZone
77
+	 */
78
+	protected static function _createTimeZone(string $tz): \DateTimeZone
79
+	{
80
+		try {
81
+			return new \DateTimeZone($tz);
82
+		} catch (\Exception $e) {
83
+			throw new \UnexpectedValueException("Invalid timezone.", 0, $e);
84
+		}
85
+	}
86 86
     
87
-    /**
88
-     * Get last error caused by DateTimeImmutable.
89
-     *
90
-     * @return string
91
-     */
92
-    protected static function _getLastDateTimeImmutableErrorsStr(): string
93
-    {
94
-        $errors = \DateTimeImmutable::getLastErrors()["errors"];
95
-        return implode(", ", $errors);
96
-    }
87
+	/**
88
+	 * Get last error caused by DateTimeImmutable.
89
+	 *
90
+	 * @return string
91
+	 */
92
+	protected static function _getLastDateTimeImmutableErrorsStr(): string
93
+	{
94
+		$errors = \DateTimeImmutable::getLastErrors()["errors"];
95
+		return implode(", ", $errors);
96
+	}
97 97
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/PrimitiveString.php 1 patch
Indentation   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -13,41 +13,41 @@
 block discarded – undo
13 13
  */
14 14
 abstract class PrimitiveString extends StringType
15 15
 {
16
-    use PrimitiveType;
16
+	use PrimitiveType;
17 17
     
18
-    /**
19
-     *
20
-     * @see \ASN1\Element::_encodedContentDER()
21
-     * @return string
22
-     */
23
-    protected function _encodedContentDER(): string
24
-    {
25
-        return $this->_string;
26
-    }
18
+	/**
19
+	 *
20
+	 * @see \ASN1\Element::_encodedContentDER()
21
+	 * @return string
22
+	 */
23
+	protected function _encodedContentDER(): string
24
+	{
25
+		return $this->_string;
26
+	}
27 27
     
28
-    /**
29
-     *
30
-     * {@inheritdoc}
31
-     * @see \ASN1\Element::_decodeFromDER()
32
-     * @return self
33
-     */
34
-    protected static function _decodeFromDER(Identifier $identifier,
35
-        string $data, int &$offset): ElementBase
36
-    {
37
-        $idx = $offset;
38
-        if (!$identifier->isPrimitive()) {
39
-            throw new DecodeException("DER encoded string must be primitive.");
40
-        }
41
-        $length = Length::expectFromDER($data, $idx)->intLength();
42
-        $str = $length ? substr($data, $idx, $length) : "";
43
-        // substr should never return false, since length is
44
-        // checked by Length::expectFromDER.
45
-        assert(is_string($str), new DecodeException("substr"));
46
-        $offset = $idx + $length;
47
-        try {
48
-            return new static($str);
49
-        } catch (\InvalidArgumentException $e) {
50
-            throw new DecodeException($e->getMessage(), 0, $e);
51
-        }
52
-    }
28
+	/**
29
+	 *
30
+	 * {@inheritdoc}
31
+	 * @see \ASN1\Element::_decodeFromDER()
32
+	 * @return self
33
+	 */
34
+	protected static function _decodeFromDER(Identifier $identifier,
35
+		string $data, int &$offset): ElementBase
36
+	{
37
+		$idx = $offset;
38
+		if (!$identifier->isPrimitive()) {
39
+			throw new DecodeException("DER encoded string must be primitive.");
40
+		}
41
+		$length = Length::expectFromDER($data, $idx)->intLength();
42
+		$str = $length ? substr($data, $idx, $length) : "";
43
+		// substr should never return false, since length is
44
+		// checked by Length::expectFromDER.
45
+		assert(is_string($str), new DecodeException("substr"));
46
+		$offset = $idx + $length;
47
+		try {
48
+			return new static($str);
49
+		} catch (\InvalidArgumentException $e) {
50
+			throw new DecodeException($e->getMessage(), 0, $e);
51
+		}
52
+	}
53 53
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/Real.php 1 patch
Indentation   +252 added lines, -252 removed lines patch added patch discarded remove patch
@@ -16,273 +16,273 @@
 block discarded – undo
16 16
  */
17 17
 class Real extends Element
18 18
 {
19
-    use UniversalClass;
20
-    use PrimitiveType;
19
+	use UniversalClass;
20
+	use PrimitiveType;
21 21
     
22
-    /**
23
-     * Regex pattern to parse NR3 form number conforming to DER.
24
-     *
25
-     * @var string
26
-     */
27
-    const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/';
22
+	/**
23
+	 * Regex pattern to parse NR3 form number conforming to DER.
24
+	 *
25
+	 * @var string
26
+	 */
27
+	const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/';
28 28
     
29
-    /**
30
-     * Regex pattern to parse PHP exponent number format.
31
-     *
32
-     * @link http://php.net/manual/en/language.types.float.php
33
-     * @var string
34
-     */
35
-    const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */
36
-        '([+\-]?'. // sign
37
-        '(?:'.
38
-            '\d+'. // LNUM
39
-            '|'.
40
-            '(?:\d*\.\d+|\d+\.\d*)'. // DNUM
41
-        '))[eE]'.
42
-        '([+\-]?\d+)'. // exponent
43
-    '$/'; /* @formatter:on */
29
+	/**
30
+	 * Regex pattern to parse PHP exponent number format.
31
+	 *
32
+	 * @link http://php.net/manual/en/language.types.float.php
33
+	 * @var string
34
+	 */
35
+	const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */
36
+		'([+\-]?'. // sign
37
+		'(?:'.
38
+			'\d+'. // LNUM
39
+			'|'.
40
+			'(?:\d*\.\d+|\d+\.\d*)'. // DNUM
41
+		'))[eE]'.
42
+		'([+\-]?\d+)'. // exponent
43
+	'$/'; /* @formatter:on */
44 44
     
45
-    /**
46
-     * Number zero represented in NR3 form.
47
-     *
48
-     * @var string
49
-     */
50
-    const NR3_ZERO = ".E+0";
45
+	/**
46
+	 * Number zero represented in NR3 form.
47
+	 *
48
+	 * @var string
49
+	 */
50
+	const NR3_ZERO = ".E+0";
51 51
     
52
-    /**
53
-     * Number in NR3 form.
54
-     *
55
-     * @var string
56
-     */
57
-    private $_number;
52
+	/**
53
+	 * Number in NR3 form.
54
+	 *
55
+	 * @var string
56
+	 */
57
+	private $_number;
58 58
     
59
-    /**
60
-     * Constructor.
61
-     *
62
-     * @param string $number Number in NR3 form.
63
-     */
64
-    public function __construct(string $number)
65
-    {
66
-        $this->_typeTag = self::TYPE_REAL;
67
-        if (!self::_validateNumber($number)) {
68
-            throw new \InvalidArgumentException(
69
-                "'$number' is not a valid NR3 form real.");
70
-        }
71
-        $this->_number = $number;
72
-    }
59
+	/**
60
+	 * Constructor.
61
+	 *
62
+	 * @param string $number Number in NR3 form.
63
+	 */
64
+	public function __construct(string $number)
65
+	{
66
+		$this->_typeTag = self::TYPE_REAL;
67
+		if (!self::_validateNumber($number)) {
68
+			throw new \InvalidArgumentException(
69
+				"'$number' is not a valid NR3 form real.");
70
+		}
71
+		$this->_number = $number;
72
+	}
73 73
     
74
-    /**
75
-     * Initialize from float.
76
-     *
77
-     * @param float $number
78
-     * @return self
79
-     */
80
-    public static function fromFloat(float $number): self
81
-    {
82
-        return new self(self::_decimalToNR3(strval($number)));
83
-    }
74
+	/**
75
+	 * Initialize from float.
76
+	 *
77
+	 * @param float $number
78
+	 * @return self
79
+	 */
80
+	public static function fromFloat(float $number): self
81
+	{
82
+		return new self(self::_decimalToNR3(strval($number)));
83
+	}
84 84
     
85
-    /**
86
-     * Get number as a float.
87
-     *
88
-     * @return float
89
-     */
90
-    public function float(): float
91
-    {
92
-        return self::_nr3ToDecimal($this->_number);
93
-    }
85
+	/**
86
+	 * Get number as a float.
87
+	 *
88
+	 * @return float
89
+	 */
90
+	public function float(): float
91
+	{
92
+		return self::_nr3ToDecimal($this->_number);
93
+	}
94 94
     
95
-    /**
96
-     *
97
-     * {@inheritdoc}
98
-     */
99
-    protected function _encodedContentDER(): string
100
-    {
101
-        /* if the real value is the value zero, there shall be no contents
95
+	/**
96
+	 *
97
+	 * {@inheritdoc}
98
+	 */
99
+	protected function _encodedContentDER(): string
100
+	{
101
+		/* if the real value is the value zero, there shall be no contents
102 102
          octets in the encoding. (X.690 07-2002, section 8.5.2) */
103
-        if (self::NR3_ZERO == $this->_number) {
104
-            return "";
105
-        }
106
-        // encode in NR3 decimal encoding
107
-        $data = chr(0x03) . $this->_number;
108
-        return $data;
109
-    }
103
+		if (self::NR3_ZERO == $this->_number) {
104
+			return "";
105
+		}
106
+		// encode in NR3 decimal encoding
107
+		$data = chr(0x03) . $this->_number;
108
+		return $data;
109
+	}
110 110
     
111
-    /**
112
-     *
113
-     * {@inheritdoc}
114
-     * @return self
115
-     */
116
-    protected static function _decodeFromDER(Identifier $identifier,
117
-        string $data, int &$offset): ElementBase
118
-    {
119
-        $idx = $offset;
120
-        $length = Length::expectFromDER($data, $idx)->intLength();
121
-        // if length is zero, value is zero (spec 8.5.2)
122
-        if (!$length) {
123
-            $obj = new self(self::NR3_ZERO);
124
-        } else {
125
-            $bytes = substr($data, $idx, $length);
126
-            $byte = ord($bytes[0]);
127
-            if (0x80 & $byte) { // bit 8 = 1
128
-                $obj = self::_decodeBinaryEncoding($bytes);
129
-            } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0
130
-                $obj = self::_decodeDecimalEncoding($bytes);
131
-            } else { // bit 8 = 0, bit 7 = 1
132
-                $obj = self::_decodeSpecialRealValue($bytes);
133
-            }
134
-        }
135
-        $offset = $idx + $length;
136
-        return $obj;
137
-    }
111
+	/**
112
+	 *
113
+	 * {@inheritdoc}
114
+	 * @return self
115
+	 */
116
+	protected static function _decodeFromDER(Identifier $identifier,
117
+		string $data, int &$offset): ElementBase
118
+	{
119
+		$idx = $offset;
120
+		$length = Length::expectFromDER($data, $idx)->intLength();
121
+		// if length is zero, value is zero (spec 8.5.2)
122
+		if (!$length) {
123
+			$obj = new self(self::NR3_ZERO);
124
+		} else {
125
+			$bytes = substr($data, $idx, $length);
126
+			$byte = ord($bytes[0]);
127
+			if (0x80 & $byte) { // bit 8 = 1
128
+				$obj = self::_decodeBinaryEncoding($bytes);
129
+			} else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0
130
+				$obj = self::_decodeDecimalEncoding($bytes);
131
+			} else { // bit 8 = 0, bit 7 = 1
132
+				$obj = self::_decodeSpecialRealValue($bytes);
133
+			}
134
+		}
135
+		$offset = $idx + $length;
136
+		return $obj;
137
+	}
138 138
     
139
-    /**
140
-     *
141
-     * @todo Implement
142
-     * @param string $data
143
-     */
144
-    protected static function _decodeBinaryEncoding(string $data)
145
-    {
146
-        throw new \RuntimeException(
147
-            "Binary encoding of REAL is not implemented.");
148
-    }
139
+	/**
140
+	 *
141
+	 * @todo Implement
142
+	 * @param string $data
143
+	 */
144
+	protected static function _decodeBinaryEncoding(string $data)
145
+	{
146
+		throw new \RuntimeException(
147
+			"Binary encoding of REAL is not implemented.");
148
+	}
149 149
     
150
-    /**
151
-     *
152
-     * @param string $data
153
-     * @throws \RuntimeException
154
-     * @return \ASN1\Type\Primitive\Real
155
-     */
156
-    protected static function _decodeDecimalEncoding(string $data): Real
157
-    {
158
-        $nr = ord($data[0]) & 0x03;
159
-        if ($nr != 0x03) {
160
-            throw new \RuntimeException("Only NR3 form supported.");
161
-        }
162
-        $str = substr($data, 1);
163
-        return new self($str);
164
-    }
150
+	/**
151
+	 *
152
+	 * @param string $data
153
+	 * @throws \RuntimeException
154
+	 * @return \ASN1\Type\Primitive\Real
155
+	 */
156
+	protected static function _decodeDecimalEncoding(string $data): Real
157
+	{
158
+		$nr = ord($data[0]) & 0x03;
159
+		if ($nr != 0x03) {
160
+			throw new \RuntimeException("Only NR3 form supported.");
161
+		}
162
+		$str = substr($data, 1);
163
+		return new self($str);
164
+	}
165 165
     
166
-    /**
167
-     *
168
-     * @todo Implement
169
-     * @param string $data
170
-     */
171
-    protected static function _decodeSpecialRealValue(string $data)
172
-    {
173
-        if (strlen($data) != 1) {
174
-            throw new DecodeException(
175
-                "SpecialRealValue must have one content octet.");
176
-        }
177
-        $byte = ord($data[0]);
178
-        if ($byte == 0x40) { // positive infinity
179
-            throw new \RuntimeException("PLUS-INFINITY not supported.");
180
-        } else if ($byte == 0x41) { // negative infinity
181
-            throw new \RuntimeException("MINUS-INFINITY not supported.");
182
-        } else {
183
-            throw new DecodeException("Invalid SpecialRealValue encoding.");
184
-        }
185
-    }
166
+	/**
167
+	 *
168
+	 * @todo Implement
169
+	 * @param string $data
170
+	 */
171
+	protected static function _decodeSpecialRealValue(string $data)
172
+	{
173
+		if (strlen($data) != 1) {
174
+			throw new DecodeException(
175
+				"SpecialRealValue must have one content octet.");
176
+		}
177
+		$byte = ord($data[0]);
178
+		if ($byte == 0x40) { // positive infinity
179
+			throw new \RuntimeException("PLUS-INFINITY not supported.");
180
+		} else if ($byte == 0x41) { // negative infinity
181
+			throw new \RuntimeException("MINUS-INFINITY not supported.");
182
+		} else {
183
+			throw new DecodeException("Invalid SpecialRealValue encoding.");
184
+		}
185
+	}
186 186
     
187
-    /**
188
-     * Convert decimal number string to NR3 form.
189
-     *
190
-     * @param string $str
191
-     * @return string
192
-     */
193
-    private static function _decimalToNR3(string $str): string
194
-    {
195
-        // if number is in exponent form
196
-        /** @var $match string[] */
197
-        if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) {
198
-            $parts = explode(".", $match[1]);
199
-            $m = ltrim($parts[0], "0");
200
-            $e = intval($match[2]);
201
-            // if mantissa had decimals
202
-            if (count($parts) == 2) {
203
-                $d = rtrim($parts[1], "0");
204
-                $e -= strlen($d);
205
-                $m .= $d;
206
-            }
207
-        } else {
208
-            // explode from decimal
209
-            $parts = explode(".", $str);
210
-            $m = ltrim($parts[0], "0");
211
-            // if number had decimals
212
-            if (count($parts) == 2) {
213
-                // exponent is negative number of the decimals
214
-                $e = -strlen($parts[1]);
215
-                // append decimals to the mantissa
216
-                $m .= $parts[1];
217
-            } else {
218
-                $e = 0;
219
-            }
220
-            // shift trailing zeroes from the mantissa to the exponent
221
-            while (substr($m, -1) === "0") {
222
-                $e++;
223
-                $m = substr($m, 0, -1);
224
-            }
225
-        }
226
-        /* if exponent is zero, it must be prefixed with a "+" sign
187
+	/**
188
+	 * Convert decimal number string to NR3 form.
189
+	 *
190
+	 * @param string $str
191
+	 * @return string
192
+	 */
193
+	private static function _decimalToNR3(string $str): string
194
+	{
195
+		// if number is in exponent form
196
+		/** @var $match string[] */
197
+		if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) {
198
+			$parts = explode(".", $match[1]);
199
+			$m = ltrim($parts[0], "0");
200
+			$e = intval($match[2]);
201
+			// if mantissa had decimals
202
+			if (count($parts) == 2) {
203
+				$d = rtrim($parts[1], "0");
204
+				$e -= strlen($d);
205
+				$m .= $d;
206
+			}
207
+		} else {
208
+			// explode from decimal
209
+			$parts = explode(".", $str);
210
+			$m = ltrim($parts[0], "0");
211
+			// if number had decimals
212
+			if (count($parts) == 2) {
213
+				// exponent is negative number of the decimals
214
+				$e = -strlen($parts[1]);
215
+				// append decimals to the mantissa
216
+				$m .= $parts[1];
217
+			} else {
218
+				$e = 0;
219
+			}
220
+			// shift trailing zeroes from the mantissa to the exponent
221
+			while (substr($m, -1) === "0") {
222
+				$e++;
223
+				$m = substr($m, 0, -1);
224
+			}
225
+		}
226
+		/* if exponent is zero, it must be prefixed with a "+" sign
227 227
          (X.690 07-2002, section 11.3.2.6) */
228
-        if (0 == $e) {
229
-            $es = "+";
230
-        } else {
231
-            $es = $e < 0 ? "-" : "";
232
-        }
233
-        return sprintf("%s.E%s%d", $m, $es, abs($e));
234
-    }
228
+		if (0 == $e) {
229
+			$es = "+";
230
+		} else {
231
+			$es = $e < 0 ? "-" : "";
232
+		}
233
+		return sprintf("%s.E%s%d", $m, $es, abs($e));
234
+	}
235 235
     
236
-    /**
237
-     * Convert NR3 form number to decimal.
238
-     *
239
-     * @param string $str
240
-     * @throws \UnexpectedValueException
241
-     * @return float
242
-     */
243
-    private static function _nr3ToDecimal(string $str): float
244
-    {
245
-        /** @var $match string[] */
246
-        if (!preg_match(self::NR3_REGEX, $str, $match)) {
247
-            throw new \UnexpectedValueException(
248
-                "'$str' is not a valid NR3 form real.");
249
-        }
250
-        $m = $match[2];
251
-        // if number started with minus sign
252
-        $inv = $match[1] == "-";
253
-        $e = intval($match[3]);
254
-        // positive exponent
255
-        if ($e > 0) {
256
-            // pad with trailing zeroes
257
-            $num = $m . str_repeat("0", $e);
258
-        } else if ($e < 0) {
259
-            // pad with leading zeroes
260
-            if (strlen($m) < abs($e)) {
261
-                $m = str_repeat("0", abs($e) - strlen($m)) . $m;
262
-            }
263
-            // insert decimal point
264
-            $num = substr($m, 0, $e) . "." . substr($m, $e);
265
-        } else {
266
-            $num = empty($m) ? "0" : $m;
267
-        }
268
-        // if number is negative
269
-        if ($inv) {
270
-            $num = "-$num";
271
-        }
272
-        return floatval($num);
273
-    }
236
+	/**
237
+	 * Convert NR3 form number to decimal.
238
+	 *
239
+	 * @param string $str
240
+	 * @throws \UnexpectedValueException
241
+	 * @return float
242
+	 */
243
+	private static function _nr3ToDecimal(string $str): float
244
+	{
245
+		/** @var $match string[] */
246
+		if (!preg_match(self::NR3_REGEX, $str, $match)) {
247
+			throw new \UnexpectedValueException(
248
+				"'$str' is not a valid NR3 form real.");
249
+		}
250
+		$m = $match[2];
251
+		// if number started with minus sign
252
+		$inv = $match[1] == "-";
253
+		$e = intval($match[3]);
254
+		// positive exponent
255
+		if ($e > 0) {
256
+			// pad with trailing zeroes
257
+			$num = $m . str_repeat("0", $e);
258
+		} else if ($e < 0) {
259
+			// pad with leading zeroes
260
+			if (strlen($m) < abs($e)) {
261
+				$m = str_repeat("0", abs($e) - strlen($m)) . $m;
262
+			}
263
+			// insert decimal point
264
+			$num = substr($m, 0, $e) . "." . substr($m, $e);
265
+		} else {
266
+			$num = empty($m) ? "0" : $m;
267
+		}
268
+		// if number is negative
269
+		if ($inv) {
270
+			$num = "-$num";
271
+		}
272
+		return floatval($num);
273
+	}
274 274
     
275
-    /**
276
-     * Test that number is valid for this context.
277
-     *
278
-     * @param mixed $num
279
-     * @return bool
280
-     */
281
-    private static function _validateNumber($num): bool
282
-    {
283
-        if (!preg_match(self::NR3_REGEX, $num)) {
284
-            return false;
285
-        }
286
-        return true;
287
-    }
275
+	/**
276
+	 * Test that number is valid for this context.
277
+	 *
278
+	 * @param mixed $num
279
+	 * @return bool
280
+	 */
281
+	private static function _validateNumber($num): bool
282
+	{
283
+		if (!preg_match(self::NR3_REGEX, $num)) {
284
+			return false;
285
+		}
286
+		return true;
287
+	}
288 288
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/ObjectIdentifier.php 1 patch
Indentation   +183 added lines, -183 removed lines patch added patch discarded remove patch
@@ -16,197 +16,197 @@
 block discarded – undo
16 16
  */
17 17
 class ObjectIdentifier extends Element
18 18
 {
19
-    use UniversalClass;
20
-    use PrimitiveType;
19
+	use UniversalClass;
20
+	use PrimitiveType;
21 21
     
22
-    /**
23
-     * Object identifier in dotted format.
24
-     *
25
-     * @var string
26
-     */
27
-    protected $_oid;
22
+	/**
23
+	 * Object identifier in dotted format.
24
+	 *
25
+	 * @var string
26
+	 */
27
+	protected $_oid;
28 28
     
29
-    /**
30
-     * Object identifier split to sub ID's.
31
-     *
32
-     * @var \GMP[]
33
-     */
34
-    protected $_subids;
29
+	/**
30
+	 * Object identifier split to sub ID's.
31
+	 *
32
+	 * @var \GMP[]
33
+	 */
34
+	protected $_subids;
35 35
     
36
-    /**
37
-     * Constructor.
38
-     *
39
-     * @param string $oid OID in dotted format
40
-     */
41
-    public function __construct(string $oid)
42
-    {
43
-        $this->_oid = $oid;
44
-        $this->_subids = self::_explodeDottedOID($oid);
45
-        // if OID is non-empty
46
-        if (count($this->_subids) > 0) {
47
-            // check that at least two nodes are set
48
-            if (count($this->_subids) < 2) {
49
-                throw new \UnexpectedValueException(
50
-                    "OID must have at least two nodes.");
51
-            }
52
-            // check that root arc is in 0..2 range
53
-            if ($this->_subids[0] > 2) {
54
-                throw new \UnexpectedValueException(
55
-                    "Root arc must be in range of 0..2.");
56
-            }
57
-            // if root arc is 0 or 1, second node must be in 0..39 range
58
-            if ($this->_subids[0] < 2 && $this->_subids[1] >= 40) {
59
-                throw new \UnexpectedValueException(
60
-                    "Second node must be in 0..39 range for root arcs 0 and 1.");
61
-            }
62
-        }
63
-        $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER;
64
-    }
36
+	/**
37
+	 * Constructor.
38
+	 *
39
+	 * @param string $oid OID in dotted format
40
+	 */
41
+	public function __construct(string $oid)
42
+	{
43
+		$this->_oid = $oid;
44
+		$this->_subids = self::_explodeDottedOID($oid);
45
+		// if OID is non-empty
46
+		if (count($this->_subids) > 0) {
47
+			// check that at least two nodes are set
48
+			if (count($this->_subids) < 2) {
49
+				throw new \UnexpectedValueException(
50
+					"OID must have at least two nodes.");
51
+			}
52
+			// check that root arc is in 0..2 range
53
+			if ($this->_subids[0] > 2) {
54
+				throw new \UnexpectedValueException(
55
+					"Root arc must be in range of 0..2.");
56
+			}
57
+			// if root arc is 0 or 1, second node must be in 0..39 range
58
+			if ($this->_subids[0] < 2 && $this->_subids[1] >= 40) {
59
+				throw new \UnexpectedValueException(
60
+					"Second node must be in 0..39 range for root arcs 0 and 1.");
61
+			}
62
+		}
63
+		$this->_typeTag = self::TYPE_OBJECT_IDENTIFIER;
64
+	}
65 65
     
66
-    /**
67
-     * Get OID in dotted format.
68
-     *
69
-     * @return string
70
-     */
71
-    public function oid(): string
72
-    {
73
-        return $this->_oid;
74
-    }
66
+	/**
67
+	 * Get OID in dotted format.
68
+	 *
69
+	 * @return string
70
+	 */
71
+	public function oid(): string
72
+	{
73
+		return $this->_oid;
74
+	}
75 75
     
76
-    /**
77
-     *
78
-     * {@inheritdoc}
79
-     */
80
-    protected function _encodedContentDER(): string
81
-    {
82
-        $subids = $this->_subids;
83
-        // encode first two subids to one according to spec section 8.19.4
84
-        if (count($subids) >= 2) {
85
-            $num = ($subids[0] * 40) + $subids[1];
86
-            array_splice($subids, 0, 2, array($num));
87
-        }
88
-        return self::_encodeSubIDs(...$subids);
89
-    }
76
+	/**
77
+	 *
78
+	 * {@inheritdoc}
79
+	 */
80
+	protected function _encodedContentDER(): string
81
+	{
82
+		$subids = $this->_subids;
83
+		// encode first two subids to one according to spec section 8.19.4
84
+		if (count($subids) >= 2) {
85
+			$num = ($subids[0] * 40) + $subids[1];
86
+			array_splice($subids, 0, 2, array($num));
87
+		}
88
+		return self::_encodeSubIDs(...$subids);
89
+	}
90 90
     
91
-    /**
92
-     *
93
-     * {@inheritdoc}
94
-     * @return self
95
-     */
96
-    protected static function _decodeFromDER(Identifier $identifier,
97
-        string $data, int &$offset): ElementBase
98
-    {
99
-        $idx = $offset;
100
-        $len = Length::expectFromDER($data, $idx)->intLength();
101
-        $subids = self::_decodeSubIDs(substr($data, $idx, $len));
102
-        $idx += $len;
103
-        // decode first subidentifier according to spec section 8.19.4
104
-        if (isset($subids[0])) {
105
-            if ($subids[0] < 80) {
106
-                list($x, $y) = gmp_div_qr($subids[0], "40");
107
-            } else {
108
-                $x = gmp_init(2, 10);
109
-                $y = $subids[0] - 80;
110
-            }
111
-            array_splice($subids, 0, 1, array($x, $y));
112
-        }
113
-        $offset = $idx;
114
-        return new self(self::_implodeSubIDs(...$subids));
115
-    }
91
+	/**
92
+	 *
93
+	 * {@inheritdoc}
94
+	 * @return self
95
+	 */
96
+	protected static function _decodeFromDER(Identifier $identifier,
97
+		string $data, int &$offset): ElementBase
98
+	{
99
+		$idx = $offset;
100
+		$len = Length::expectFromDER($data, $idx)->intLength();
101
+		$subids = self::_decodeSubIDs(substr($data, $idx, $len));
102
+		$idx += $len;
103
+		// decode first subidentifier according to spec section 8.19.4
104
+		if (isset($subids[0])) {
105
+			if ($subids[0] < 80) {
106
+				list($x, $y) = gmp_div_qr($subids[0], "40");
107
+			} else {
108
+				$x = gmp_init(2, 10);
109
+				$y = $subids[0] - 80;
110
+			}
111
+			array_splice($subids, 0, 1, array($x, $y));
112
+		}
113
+		$offset = $idx;
114
+		return new self(self::_implodeSubIDs(...$subids));
115
+	}
116 116
     
117
-    /**
118
-     * Explode dotted OID to an array of sub ID's.
119
-     *
120
-     * @param string $oid OID in dotted format
121
-     * @return \GMP[] Array of GMP numbers
122
-     */
123
-    protected static function _explodeDottedOID(string $oid): array
124
-    {
125
-        $subids = [];
126
-        if (strlen($oid)) {
127
-            foreach (explode(".", $oid) as $subid) {
128
-                $n = @gmp_init($subid, 10);
129
-                if (false === $n) {
130
-                    throw new \UnexpectedValueException(
131
-                        "'$subid' is not a number.");
132
-                }
133
-                $subids[] = $n;
134
-            }
135
-        }
136
-        return $subids;
137
-    }
117
+	/**
118
+	 * Explode dotted OID to an array of sub ID's.
119
+	 *
120
+	 * @param string $oid OID in dotted format
121
+	 * @return \GMP[] Array of GMP numbers
122
+	 */
123
+	protected static function _explodeDottedOID(string $oid): array
124
+	{
125
+		$subids = [];
126
+		if (strlen($oid)) {
127
+			foreach (explode(".", $oid) as $subid) {
128
+				$n = @gmp_init($subid, 10);
129
+				if (false === $n) {
130
+					throw new \UnexpectedValueException(
131
+						"'$subid' is not a number.");
132
+				}
133
+				$subids[] = $n;
134
+			}
135
+		}
136
+		return $subids;
137
+	}
138 138
     
139
-    /**
140
-     * Implode an array of sub IDs to dotted OID format.
141
-     *
142
-     * @param \GMP ...$subids
143
-     * @return string
144
-     */
145
-    protected static function _implodeSubIDs(\GMP ...$subids): string
146
-    {
147
-        return implode(".",
148
-            array_map(function ($num) {
149
-                return gmp_strval($num, 10);
150
-            }, $subids));
151
-    }
139
+	/**
140
+	 * Implode an array of sub IDs to dotted OID format.
141
+	 *
142
+	 * @param \GMP ...$subids
143
+	 * @return string
144
+	 */
145
+	protected static function _implodeSubIDs(\GMP ...$subids): string
146
+	{
147
+		return implode(".",
148
+			array_map(function ($num) {
149
+				return gmp_strval($num, 10);
150
+			}, $subids));
151
+	}
152 152
     
153
-    /**
154
-     * Encode sub ID's to DER.
155
-     *
156
-     * @param \GMP ...$subids
157
-     * @return string
158
-     */
159
-    protected static function _encodeSubIDs(\GMP ...$subids): string
160
-    {
161
-        $data = "";
162
-        foreach ($subids as $subid) {
163
-            // if number fits to one base 128 byte
164
-            if ($subid < 128) {
165
-                $data .= chr(intval($subid));
166
-            } else { // encode to multiple bytes
167
-                $bytes = [];
168
-                do {
169
-                    array_unshift($bytes, 0x7f & gmp_intval($subid));
170
-                    $subid >>= 7;
171
-                } while ($subid > 0);
172
-                // all bytes except last must have bit 8 set to one
173
-                foreach (array_splice($bytes, 0, -1) as $byte) {
174
-                    $data .= chr(0x80 | $byte);
175
-                }
176
-                $data .= chr(reset($bytes));
177
-            }
178
-        }
179
-        return $data;
180
-    }
153
+	/**
154
+	 * Encode sub ID's to DER.
155
+	 *
156
+	 * @param \GMP ...$subids
157
+	 * @return string
158
+	 */
159
+	protected static function _encodeSubIDs(\GMP ...$subids): string
160
+	{
161
+		$data = "";
162
+		foreach ($subids as $subid) {
163
+			// if number fits to one base 128 byte
164
+			if ($subid < 128) {
165
+				$data .= chr(intval($subid));
166
+			} else { // encode to multiple bytes
167
+				$bytes = [];
168
+				do {
169
+					array_unshift($bytes, 0x7f & gmp_intval($subid));
170
+					$subid >>= 7;
171
+				} while ($subid > 0);
172
+				// all bytes except last must have bit 8 set to one
173
+				foreach (array_splice($bytes, 0, -1) as $byte) {
174
+					$data .= chr(0x80 | $byte);
175
+				}
176
+				$data .= chr(reset($bytes));
177
+			}
178
+		}
179
+		return $data;
180
+	}
181 181
     
182
-    /**
183
-     * Decode sub ID's from DER data.
184
-     *
185
-     * @param string $data
186
-     * @throws DecodeException
187
-     * @return \GMP[] Array of GMP numbers
188
-     */
189
-    protected static function _decodeSubIDs(string $data): array
190
-    {
191
-        $subids = [];
192
-        $idx = 0;
193
-        $end = strlen($data);
194
-        while ($idx < $end) {
195
-            $num = gmp_init("0", 10);
196
-            while (true) {
197
-                if ($idx >= $end) {
198
-                    throw new DecodeException("Unexpected end of data.");
199
-                }
200
-                $byte = ord($data[$idx++]);
201
-                $num |= $byte & 0x7f;
202
-                // bit 8 of the last octet is zero
203
-                if (!($byte & 0x80)) {
204
-                    break;
205
-                }
206
-                $num <<= 7;
207
-            }
208
-            $subids[] = $num;
209
-        }
210
-        return $subids;
211
-    }
182
+	/**
183
+	 * Decode sub ID's from DER data.
184
+	 *
185
+	 * @param string $data
186
+	 * @throws DecodeException
187
+	 * @return \GMP[] Array of GMP numbers
188
+	 */
189
+	protected static function _decodeSubIDs(string $data): array
190
+	{
191
+		$subids = [];
192
+		$idx = 0;
193
+		$end = strlen($data);
194
+		while ($idx < $end) {
195
+			$num = gmp_init("0", 10);
196
+			while (true) {
197
+				if ($idx >= $end) {
198
+					throw new DecodeException("Unexpected end of data.");
199
+				}
200
+				$byte = ord($data[$idx++]);
201
+				$num |= $byte & 0x7f;
202
+				// bit 8 of the last octet is zero
203
+				if (!($byte & 0x80)) {
204
+					break;
205
+				}
206
+				$num <<= 7;
207
+			}
208
+			$subids[] = $num;
209
+		}
210
+		return $subids;
211
+	}
212 212
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/UTCTime.php 1 patch
Indentation   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -16,73 +16,73 @@
 block discarded – undo
16 16
  */
17 17
 class UTCTime extends TimeType
18 18
 {
19
-    use UniversalClass;
20
-    use PrimitiveType;
19
+	use UniversalClass;
20
+	use PrimitiveType;
21 21
     
22
-    /**
23
-     * Regular expression to parse date.
24
-     *
25
-     * DER restricts format to UTC timezone (Z suffix).
26
-     *
27
-     * @var string
28
-     */
29
-    const REGEX = /* @formatter:off */ '#^' .
30
-        '(\d\d)' . /* YY */
31
-        '(\d\d)' . /* MM */
32
-        '(\d\d)' . /* DD */
33
-        '(\d\d)' . /* hh */
34
-        '(\d\d)' . /* mm */
35
-        '(\d\d)' . /* ss */
36
-        'Z' . /* TZ */
37
-        '$#' /* @formatter:on */;
22
+	/**
23
+	 * Regular expression to parse date.
24
+	 *
25
+	 * DER restricts format to UTC timezone (Z suffix).
26
+	 *
27
+	 * @var string
28
+	 */
29
+	const REGEX = /* @formatter:off */ '#^' .
30
+		'(\d\d)' . /* YY */
31
+		'(\d\d)' . /* MM */
32
+		'(\d\d)' . /* DD */
33
+		'(\d\d)' . /* hh */
34
+		'(\d\d)' . /* mm */
35
+		'(\d\d)' . /* ss */
36
+		'Z' . /* TZ */
37
+		'$#' /* @formatter:on */;
38 38
     
39
-    /**
40
-     * Constructor.
41
-     *
42
-     * @param \DateTimeImmutable $dt
43
-     */
44
-    public function __construct(\DateTimeImmutable $dt)
45
-    {
46
-        $this->_typeTag = self::TYPE_UTC_TIME;
47
-        parent::__construct($dt);
48
-    }
39
+	/**
40
+	 * Constructor.
41
+	 *
42
+	 * @param \DateTimeImmutable $dt
43
+	 */
44
+	public function __construct(\DateTimeImmutable $dt)
45
+	{
46
+		$this->_typeTag = self::TYPE_UTC_TIME;
47
+		parent::__construct($dt);
48
+	}
49 49
     
50
-    /**
51
-     *
52
-     * {@inheritdoc}
53
-     */
54
-    protected function _encodedContentDER(): string
55
-    {
56
-        $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC));
57
-        return $dt->format("ymdHis\Z");
58
-    }
50
+	/**
51
+	 *
52
+	 * {@inheritdoc}
53
+	 */
54
+	protected function _encodedContentDER(): string
55
+	{
56
+		$dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC));
57
+		return $dt->format("ymdHis\Z");
58
+	}
59 59
     
60
-    /**
61
-     *
62
-     * {@inheritdoc}
63
-     * @return self
64
-     */
65
-    protected static function _decodeFromDER(Identifier $identifier,
66
-        string $data, int &$offset): ElementBase
67
-    {
68
-        $idx = $offset;
69
-        $length = Length::expectFromDER($data, $idx)->intLength();
70
-        $str = substr($data, $idx, $length);
71
-        $idx += $length;
72
-        /** @var $match string[] */
73
-        if (!preg_match(self::REGEX, $str, $match)) {
74
-            throw new DecodeException("Invalid UTCTime format.");
75
-        }
76
-        list(, $year, $month, $day, $hour, $minute, $second) = $match;
77
-        $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC;
78
-        $dt = \DateTimeImmutable::createFromFormat("!ymdHisT", $time,
79
-            self::_createTimeZone(self::TZ_UTC));
80
-        if (!$dt) {
81
-            throw new DecodeException(
82
-                "Failed to decode UTCTime: " .
83
-                self::_getLastDateTimeImmutableErrorsStr());
84
-        }
85
-        $offset = $idx;
86
-        return new self($dt);
87
-    }
60
+	/**
61
+	 *
62
+	 * {@inheritdoc}
63
+	 * @return self
64
+	 */
65
+	protected static function _decodeFromDER(Identifier $identifier,
66
+		string $data, int &$offset): ElementBase
67
+	{
68
+		$idx = $offset;
69
+		$length = Length::expectFromDER($data, $idx)->intLength();
70
+		$str = substr($data, $idx, $length);
71
+		$idx += $length;
72
+		/** @var $match string[] */
73
+		if (!preg_match(self::REGEX, $str, $match)) {
74
+			throw new DecodeException("Invalid UTCTime format.");
75
+		}
76
+		list(, $year, $month, $day, $hour, $minute, $second) = $match;
77
+		$time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC;
78
+		$dt = \DateTimeImmutable::createFromFormat("!ymdHisT", $time,
79
+			self::_createTimeZone(self::TZ_UTC));
80
+		if (!$dt) {
81
+			throw new DecodeException(
82
+				"Failed to decode UTCTime: " .
83
+				self::_getLastDateTimeImmutableErrorsStr());
84
+		}
85
+		$offset = $idx;
86
+		return new self($dt);
87
+	}
88 88
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/Boolean.php 1 patch
Indentation   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -16,64 +16,64 @@
 block discarded – undo
16 16
  */
17 17
 class Boolean extends Element
18 18
 {
19
-    use UniversalClass;
20
-    use PrimitiveType;
19
+	use UniversalClass;
20
+	use PrimitiveType;
21 21
     
22
-    /**
23
-     * Value.
24
-     *
25
-     * @var bool
26
-     */
27
-    private $_bool;
22
+	/**
23
+	 * Value.
24
+	 *
25
+	 * @var bool
26
+	 */
27
+	private $_bool;
28 28
     
29
-    /**
30
-     * Constructor.
31
-     *
32
-     * @param bool $bool
33
-     */
34
-    public function __construct(bool $bool)
35
-    {
36
-        $this->_typeTag = self::TYPE_BOOLEAN;
37
-        $this->_bool = $bool;
38
-    }
29
+	/**
30
+	 * Constructor.
31
+	 *
32
+	 * @param bool $bool
33
+	 */
34
+	public function __construct(bool $bool)
35
+	{
36
+		$this->_typeTag = self::TYPE_BOOLEAN;
37
+		$this->_bool = $bool;
38
+	}
39 39
     
40
-    /**
41
-     * Get the value.
42
-     *
43
-     * @return bool
44
-     */
45
-    public function value(): bool
46
-    {
47
-        return $this->_bool;
48
-    }
40
+	/**
41
+	 * Get the value.
42
+	 *
43
+	 * @return bool
44
+	 */
45
+	public function value(): bool
46
+	{
47
+		return $this->_bool;
48
+	}
49 49
     
50
-    /**
51
-     *
52
-     * {@inheritdoc}
53
-     */
54
-    protected function _encodedContentDER(): string
55
-    {
56
-        return $this->_bool ? chr(0xff) : chr(0);
57
-    }
50
+	/**
51
+	 *
52
+	 * {@inheritdoc}
53
+	 */
54
+	protected function _encodedContentDER(): string
55
+	{
56
+		return $this->_bool ? chr(0xff) : chr(0);
57
+	}
58 58
     
59
-    /**
60
-     *
61
-     * {@inheritdoc}
62
-     * @return self
63
-     */
64
-    protected static function _decodeFromDER(Identifier $identifier,
65
-        string $data, int &$offset): ElementBase
66
-    {
67
-        $idx = $offset;
68
-        Length::expectFromDER($data, $idx, 1);
69
-        $byte = ord($data[$idx++]);
70
-        if ($byte !== 0) {
71
-            if ($byte != 0xff) {
72
-                throw new DecodeException(
73
-                    "DER encoded boolean true must have all bits set to 1.");
74
-            }
75
-        }
76
-        $offset = $idx;
77
-        return new self($byte !== 0);
78
-    }
59
+	/**
60
+	 *
61
+	 * {@inheritdoc}
62
+	 * @return self
63
+	 */
64
+	protected static function _decodeFromDER(Identifier $identifier,
65
+		string $data, int &$offset): ElementBase
66
+	{
67
+		$idx = $offset;
68
+		Length::expectFromDER($data, $idx, 1);
69
+		$byte = ord($data[$idx++]);
70
+		if ($byte !== 0) {
71
+			if ($byte != 0xff) {
72
+				throw new DecodeException(
73
+					"DER encoded boolean true must have all bits set to 1.");
74
+			}
75
+		}
76
+		$offset = $idx;
77
+		return new self($byte !== 0);
78
+	}
79 79
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/BitString.php 1 patch
Indentation   +182 added lines, -182 removed lines patch added patch discarded remove patch
@@ -16,195 +16,195 @@
 block discarded – undo
16 16
  */
17 17
 class BitString extends StringType
18 18
 {
19
-    use UniversalClass;
20
-    use PrimitiveType;
19
+	use UniversalClass;
20
+	use PrimitiveType;
21 21
     
22
-    /**
23
-     * Number of unused bits in the last octet.
24
-     *
25
-     * @var int $_unusedBits
26
-     */
27
-    protected $_unusedBits;
22
+	/**
23
+	 * Number of unused bits in the last octet.
24
+	 *
25
+	 * @var int $_unusedBits
26
+	 */
27
+	protected $_unusedBits;
28 28
     
29
-    /**
30
-     * Constructor.
31
-     *
32
-     * @param string $string Content octets
33
-     * @param int $unused_bits Number of unused bits in the last octet
34
-     */
35
-    public function __construct(string $string, int $unused_bits = 0)
36
-    {
37
-        $this->_typeTag = self::TYPE_BIT_STRING;
38
-        parent::__construct($string);
39
-        $this->_unusedBits = $unused_bits;
40
-    }
29
+	/**
30
+	 * Constructor.
31
+	 *
32
+	 * @param string $string Content octets
33
+	 * @param int $unused_bits Number of unused bits in the last octet
34
+	 */
35
+	public function __construct(string $string, int $unused_bits = 0)
36
+	{
37
+		$this->_typeTag = self::TYPE_BIT_STRING;
38
+		parent::__construct($string);
39
+		$this->_unusedBits = $unused_bits;
40
+	}
41 41
     
42
-    /**
43
-     * Get the number of bits in the string.
44
-     *
45
-     * @return int
46
-     */
47
-    public function numBits(): int
48
-    {
49
-        return strlen($this->_string) * 8 - $this->_unusedBits;
50
-    }
42
+	/**
43
+	 * Get the number of bits in the string.
44
+	 *
45
+	 * @return int
46
+	 */
47
+	public function numBits(): int
48
+	{
49
+		return strlen($this->_string) * 8 - $this->_unusedBits;
50
+	}
51 51
     
52
-    /**
53
-     * Get the number of unused bits in the last octet of the string.
54
-     *
55
-     * @return int
56
-     */
57
-    public function unusedBits(): int
58
-    {
59
-        return $this->_unusedBits;
60
-    }
52
+	/**
53
+	 * Get the number of unused bits in the last octet of the string.
54
+	 *
55
+	 * @return int
56
+	 */
57
+	public function unusedBits(): int
58
+	{
59
+		return $this->_unusedBits;
60
+	}
61 61
     
62
-    /**
63
-     * Test whether bit is set.
64
-     *
65
-     * @param int $idx Bit index.
66
-     *        Most significant bit of the first octet is index 0.
67
-     * @return bool
68
-     */
69
-    public function testBit(int $idx): bool
70
-    {
71
-        // octet index
72
-        $oi = (int) floor($idx / 8);
73
-        // if octet is outside range
74
-        if ($oi < 0 || $oi >= strlen($this->_string)) {
75
-            throw new \OutOfBoundsException("Index is out of bounds.");
76
-        }
77
-        // bit index
78
-        $bi = $idx % 8;
79
-        // if tested bit is last octet's unused bit
80
-        if ($oi == strlen($this->_string) - 1) {
81
-            if ($bi >= 8 - $this->_unusedBits) {
82
-                throw new \OutOfBoundsException(
83
-                    "Index refers to an unused bit.");
84
-            }
85
-        }
86
-        $byte = $this->_string[$oi];
87
-        // index 0 is the most significant bit in byte
88
-        $mask = 0x01 << (7 - $bi);
89
-        return (ord($byte) & $mask) > 0;
90
-    }
62
+	/**
63
+	 * Test whether bit is set.
64
+	 *
65
+	 * @param int $idx Bit index.
66
+	 *        Most significant bit of the first octet is index 0.
67
+	 * @return bool
68
+	 */
69
+	public function testBit(int $idx): bool
70
+	{
71
+		// octet index
72
+		$oi = (int) floor($idx / 8);
73
+		// if octet is outside range
74
+		if ($oi < 0 || $oi >= strlen($this->_string)) {
75
+			throw new \OutOfBoundsException("Index is out of bounds.");
76
+		}
77
+		// bit index
78
+		$bi = $idx % 8;
79
+		// if tested bit is last octet's unused bit
80
+		if ($oi == strlen($this->_string) - 1) {
81
+			if ($bi >= 8 - $this->_unusedBits) {
82
+				throw new \OutOfBoundsException(
83
+					"Index refers to an unused bit.");
84
+			}
85
+		}
86
+		$byte = $this->_string[$oi];
87
+		// index 0 is the most significant bit in byte
88
+		$mask = 0x01 << (7 - $bi);
89
+		return (ord($byte) & $mask) > 0;
90
+	}
91 91
     
92
-    /**
93
-     * Get range of bits.
94
-     *
95
-     * @param int $start Index of first bit
96
-     * @param int $length Number of bits in range
97
-     * @throws \OutOfBoundsException
98
-     * @return string Integer of $length bits
99
-     */
100
-    public function range(int $start, int $length): string
101
-    {
102
-        if (!$length) {
103
-            return "0";
104
-        }
105
-        if ($start + $length > $this->numBits()) {
106
-            throw new \OutOfBoundsException("Not enough bits.");
107
-        }
108
-        $bits = gmp_init(0);
109
-        $idx = $start;
110
-        $end = $start + $length;
111
-        while (true) {
112
-            $bit = $this->testBit($idx) ? 1 : 0;
113
-            $bits |= $bit;
114
-            if (++$idx >= $end) {
115
-                break;
116
-            }
117
-            $bits <<= 1;
118
-        }
119
-        return gmp_strval($bits, 10);
120
-    }
92
+	/**
93
+	 * Get range of bits.
94
+	 *
95
+	 * @param int $start Index of first bit
96
+	 * @param int $length Number of bits in range
97
+	 * @throws \OutOfBoundsException
98
+	 * @return string Integer of $length bits
99
+	 */
100
+	public function range(int $start, int $length): string
101
+	{
102
+		if (!$length) {
103
+			return "0";
104
+		}
105
+		if ($start + $length > $this->numBits()) {
106
+			throw new \OutOfBoundsException("Not enough bits.");
107
+		}
108
+		$bits = gmp_init(0);
109
+		$idx = $start;
110
+		$end = $start + $length;
111
+		while (true) {
112
+			$bit = $this->testBit($idx) ? 1 : 0;
113
+			$bits |= $bit;
114
+			if (++$idx >= $end) {
115
+				break;
116
+			}
117
+			$bits <<= 1;
118
+		}
119
+		return gmp_strval($bits, 10);
120
+	}
121 121
     
122
-    /**
123
-     * Get a copy of the bit string with trailing zeroes removed.
124
-     *
125
-     * @return self
126
-     */
127
-    public function withoutTrailingZeroes(): self
128
-    {
129
-        // if bit string was empty
130
-        if (!strlen($this->_string)) {
131
-            return new self("");
132
-        }
133
-        $bits = $this->_string;
134
-        // count number of empty trailing octets
135
-        $unused_octets = 0;
136
-        for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) {
137
-            if ($bits[$idx] != "\x0") {
138
-                break;
139
-            }
140
-        }
141
-        // strip trailing octets
142
-        if ($unused_octets) {
143
-            $bits = substr($bits, 0, -$unused_octets);
144
-        }
145
-        // if bit string was full of zeroes
146
-        if (!strlen($bits)) {
147
-            return new self("");
148
-        }
149
-        // count number of trailing zeroes in the last octet
150
-        $unused_bits = 0;
151
-        $byte = ord($bits[strlen($bits) - 1]);
152
-        while (!($byte & 0x01)) {
153
-            $unused_bits++;
154
-            $byte >>= 1;
155
-        }
156
-        return new self($bits, $unused_bits);
157
-    }
122
+	/**
123
+	 * Get a copy of the bit string with trailing zeroes removed.
124
+	 *
125
+	 * @return self
126
+	 */
127
+	public function withoutTrailingZeroes(): self
128
+	{
129
+		// if bit string was empty
130
+		if (!strlen($this->_string)) {
131
+			return new self("");
132
+		}
133
+		$bits = $this->_string;
134
+		// count number of empty trailing octets
135
+		$unused_octets = 0;
136
+		for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) {
137
+			if ($bits[$idx] != "\x0") {
138
+				break;
139
+			}
140
+		}
141
+		// strip trailing octets
142
+		if ($unused_octets) {
143
+			$bits = substr($bits, 0, -$unused_octets);
144
+		}
145
+		// if bit string was full of zeroes
146
+		if (!strlen($bits)) {
147
+			return new self("");
148
+		}
149
+		// count number of trailing zeroes in the last octet
150
+		$unused_bits = 0;
151
+		$byte = ord($bits[strlen($bits) - 1]);
152
+		while (!($byte & 0x01)) {
153
+			$unused_bits++;
154
+			$byte >>= 1;
155
+		}
156
+		return new self($bits, $unused_bits);
157
+	}
158 158
     
159
-    /**
160
-     *
161
-     * {@inheritdoc}
162
-     */
163
-    protected function _encodedContentDER(): string
164
-    {
165
-        $der = chr($this->_unusedBits);
166
-        $der .= $this->_string;
167
-        if ($this->_unusedBits) {
168
-            $octet = $der[strlen($der) - 1];
169
-            // set unused bits to zero
170
-            $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1));
171
-            $der[strlen($der) - 1] = $octet;
172
-        }
173
-        return $der;
174
-    }
159
+	/**
160
+	 *
161
+	 * {@inheritdoc}
162
+	 */
163
+	protected function _encodedContentDER(): string
164
+	{
165
+		$der = chr($this->_unusedBits);
166
+		$der .= $this->_string;
167
+		if ($this->_unusedBits) {
168
+			$octet = $der[strlen($der) - 1];
169
+			// set unused bits to zero
170
+			$octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1));
171
+			$der[strlen($der) - 1] = $octet;
172
+		}
173
+		return $der;
174
+	}
175 175
     
176
-    /**
177
-     *
178
-     * {@inheritdoc}
179
-     * @return self
180
-     */
181
-    protected static function _decodeFromDER(Identifier $identifier,
182
-        string $data, int &$offset): ElementBase
183
-    {
184
-        $idx = $offset;
185
-        $length = Length::expectFromDER($data, $idx);
186
-        if ($length->intLength() < 1) {
187
-            throw new DecodeException("Bit string length must be at least 1.");
188
-        }
189
-        $unused_bits = ord($data[$idx++]);
190
-        if ($unused_bits > 7) {
191
-            throw new DecodeException(
192
-                "Unused bits in a bit string must be less than 8.");
193
-        }
194
-        $str_len = $length->intLength() - 1;
195
-        if ($str_len) {
196
-            $str = substr($data, $idx, $str_len);
197
-            if ($unused_bits) {
198
-                $mask = (1 << $unused_bits) - 1;
199
-                if (ord($str[strlen($str) - 1]) & $mask) {
200
-                    throw new DecodeException(
201
-                        "DER encoded bit string must have zero padding.");
202
-                }
203
-            }
204
-        } else {
205
-            $str = "";
206
-        }
207
-        $offset = $idx + $str_len;
208
-        return new self($str, $unused_bits);
209
-    }
176
+	/**
177
+	 *
178
+	 * {@inheritdoc}
179
+	 * @return self
180
+	 */
181
+	protected static function _decodeFromDER(Identifier $identifier,
182
+		string $data, int &$offset): ElementBase
183
+	{
184
+		$idx = $offset;
185
+		$length = Length::expectFromDER($data, $idx);
186
+		if ($length->intLength() < 1) {
187
+			throw new DecodeException("Bit string length must be at least 1.");
188
+		}
189
+		$unused_bits = ord($data[$idx++]);
190
+		if ($unused_bits > 7) {
191
+			throw new DecodeException(
192
+				"Unused bits in a bit string must be less than 8.");
193
+		}
194
+		$str_len = $length->intLength() - 1;
195
+		if ($str_len) {
196
+			$str = substr($data, $idx, $str_len);
197
+			if ($unused_bits) {
198
+				$mask = (1 << $unused_bits) - 1;
199
+				if (ord($str[strlen($str) - 1]) & $mask) {
200
+					throw new DecodeException(
201
+						"DER encoded bit string must have zero padding.");
202
+				}
203
+			}
204
+		} else {
205
+			$str = "";
206
+		}
207
+		$offset = $idx + $str_len;
208
+		return new self($str, $unused_bits);
209
+	}
210 210
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/NullType.php 1 patch
Indentation   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -16,41 +16,41 @@
 block discarded – undo
16 16
  */
17 17
 class NullType extends Element
18 18
 {
19
-    use UniversalClass;
20
-    use PrimitiveType;
19
+	use UniversalClass;
20
+	use PrimitiveType;
21 21
     
22
-    /**
23
-     * Constructor.
24
-     */
25
-    public function __construct()
26
-    {
27
-        $this->_typeTag = self::TYPE_NULL;
28
-    }
22
+	/**
23
+	 * Constructor.
24
+	 */
25
+	public function __construct()
26
+	{
27
+		$this->_typeTag = self::TYPE_NULL;
28
+	}
29 29
     
30
-    /**
31
-     *
32
-     * {@inheritdoc}
33
-     */
34
-    protected function _encodedContentDER(): string
35
-    {
36
-        return "";
37
-    }
30
+	/**
31
+	 *
32
+	 * {@inheritdoc}
33
+	 */
34
+	protected function _encodedContentDER(): string
35
+	{
36
+		return "";
37
+	}
38 38
     
39
-    /**
40
-     *
41
-     * {@inheritdoc}
42
-     * @return self
43
-     */
44
-    protected static function _decodeFromDER(Identifier $identifier,
45
-        string $data, int &$offset): ElementBase
46
-    {
47
-        $idx = $offset;
48
-        if (!$identifier->isPrimitive()) {
49
-            throw new DecodeException("Null value must be primitive.");
50
-        }
51
-        // null type has always zero length
52
-        Length::expectFromDER($data, $idx, 0);
53
-        $offset = $idx;
54
-        return new self();
55
-    }
39
+	/**
40
+	 *
41
+	 * {@inheritdoc}
42
+	 * @return self
43
+	 */
44
+	protected static function _decodeFromDER(Identifier $identifier,
45
+		string $data, int &$offset): ElementBase
46
+	{
47
+		$idx = $offset;
48
+		if (!$identifier->isPrimitive()) {
49
+			throw new DecodeException("Null value must be primitive.");
50
+		}
51
+		// null type has always zero length
52
+		Length::expectFromDER($data, $idx, 0);
53
+		$offset = $idx;
54
+		return new self();
55
+	}
56 56
 }
Please login to merge, or discard this patch.
lib/ASN1/Type/Primitive/GeneralizedTime.php 1 patch
Indentation   +103 added lines, -103 removed lines patch added patch discarded remove patch
@@ -16,113 +16,113 @@
 block discarded – undo
16 16
  */
17 17
 class GeneralizedTime extends TimeType
18 18
 {
19
-    use UniversalClass;
20
-    use PrimitiveType;
19
+	use UniversalClass;
20
+	use PrimitiveType;
21 21
     
22
-    /**
23
-     * Regular expression to parse date.
24
-     *
25
-     * DER restricts format to UTC timezone (Z suffix).
26
-     *
27
-     * @var string
28
-     */
29
-    const REGEX = /* @formatter:off */ '#^' .
30
-        '(\d\d\d\d)' . /* YYYY */
31
-        '(\d\d)' . /* MM */
32
-        '(\d\d)' . /* DD */
33
-        '(\d\d)' . /* hh */
34
-        '(\d\d)' . /* mm */
35
-        '(\d\d)' . /* ss */
36
-        '(?:\.(\d+))?' . /* frac */
37
-        'Z' . /* TZ */
38
-        '$#' /* @formatter:on */;
22
+	/**
23
+	 * Regular expression to parse date.
24
+	 *
25
+	 * DER restricts format to UTC timezone (Z suffix).
26
+	 *
27
+	 * @var string
28
+	 */
29
+	const REGEX = /* @formatter:off */ '#^' .
30
+		'(\d\d\d\d)' . /* YYYY */
31
+		'(\d\d)' . /* MM */
32
+		'(\d\d)' . /* DD */
33
+		'(\d\d)' . /* hh */
34
+		'(\d\d)' . /* mm */
35
+		'(\d\d)' . /* ss */
36
+		'(?:\.(\d+))?' . /* frac */
37
+		'Z' . /* TZ */
38
+		'$#' /* @formatter:on */;
39 39
     
40
-    /**
41
-     * Cached formatted date.
42
-     *
43
-     * @var string|null
44
-     */
45
-    private $_formatted;
40
+	/**
41
+	 * Cached formatted date.
42
+	 *
43
+	 * @var string|null
44
+	 */
45
+	private $_formatted;
46 46
     
47
-    /**
48
-     * Constructor.
49
-     *
50
-     * @param \DateTimeImmutable $dt
51
-     */
52
-    public function __construct(\DateTimeImmutable $dt)
53
-    {
54
-        $this->_typeTag = self::TYPE_GENERALIZED_TIME;
55
-        parent::__construct($dt);
56
-    }
47
+	/**
48
+	 * Constructor.
49
+	 *
50
+	 * @param \DateTimeImmutable $dt
51
+	 */
52
+	public function __construct(\DateTimeImmutable $dt)
53
+	{
54
+		$this->_typeTag = self::TYPE_GENERALIZED_TIME;
55
+		parent::__construct($dt);
56
+	}
57 57
     
58
-    /**
59
-     * Clear cached variables on clone.
60
-     */
61
-    public function __clone()
62
-    {
63
-        $this->_formatted = null;
64
-    }
58
+	/**
59
+	 * Clear cached variables on clone.
60
+	 */
61
+	public function __clone()
62
+	{
63
+		$this->_formatted = null;
64
+	}
65 65
     
66
-    /**
67
-     *
68
-     * {@inheritdoc}
69
-     */
70
-    protected function _encodedContentDER(): string
71
-    {
72
-        if (!isset($this->_formatted)) {
73
-            $dt = $this->_dateTime->setTimezone(
74
-                self::_createTimeZone(self::TZ_UTC));
75
-            $this->_formatted = $dt->format("YmdHis");
76
-            // if fractions were used
77
-            $frac = $dt->format("u");
78
-            if ($frac != 0) {
79
-                $frac = rtrim($frac, "0");
80
-                $this->_formatted .= ".$frac";
81
-            }
82
-            // timezone
83
-            $this->_formatted .= "Z";
84
-        }
85
-        return $this->_formatted;
86
-    }
66
+	/**
67
+	 *
68
+	 * {@inheritdoc}
69
+	 */
70
+	protected function _encodedContentDER(): string
71
+	{
72
+		if (!isset($this->_formatted)) {
73
+			$dt = $this->_dateTime->setTimezone(
74
+				self::_createTimeZone(self::TZ_UTC));
75
+			$this->_formatted = $dt->format("YmdHis");
76
+			// if fractions were used
77
+			$frac = $dt->format("u");
78
+			if ($frac != 0) {
79
+				$frac = rtrim($frac, "0");
80
+				$this->_formatted .= ".$frac";
81
+			}
82
+			// timezone
83
+			$this->_formatted .= "Z";
84
+		}
85
+		return $this->_formatted;
86
+	}
87 87
     
88
-    /**
89
-     *
90
-     * {@inheritdoc}
91
-     * @return self
92
-     */
93
-    protected static function _decodeFromDER(Identifier $identifier,
94
-        string $data, int &$offset): ElementBase
95
-    {
96
-        $idx = $offset;
97
-        $length = Length::expectFromDER($data, $idx)->intLength();
98
-        $str = substr($data, $idx, $length);
99
-        $idx += $length;
100
-        /** @var $match string[] */
101
-        if (!preg_match(self::REGEX, $str, $match)) {
102
-            throw new DecodeException("Invalid GeneralizedTime format.");
103
-        }
104
-        list(, $year, $month, $day, $hour, $minute, $second) = $match;
105
-        if (isset($match[7])) {
106
-            $frac = $match[7];
107
-            // DER restricts trailing zeroes in fractional seconds component
108
-            if ('0' === $frac[strlen($frac) - 1]) {
109
-                throw new DecodeException(
110
-                    "Fractional seconds must omit trailing zeroes.");
111
-            }
112
-            $frac = (int) $frac;
113
-        } else {
114
-            $frac = 0;
115
-        }
116
-        $time = $year . $month . $day . $hour . $minute . $second . "." . $frac .
117
-            self::TZ_UTC;
118
-        $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time,
119
-            self::_createTimeZone(self::TZ_UTC));
120
-        if (!$dt) {
121
-            throw new DecodeException(
122
-                "Failed to decode GeneralizedTime: " .
123
-                self::_getLastDateTimeImmutableErrorsStr());
124
-        }
125
-        $offset = $idx;
126
-        return new self($dt);
127
-    }
88
+	/**
89
+	 *
90
+	 * {@inheritdoc}
91
+	 * @return self
92
+	 */
93
+	protected static function _decodeFromDER(Identifier $identifier,
94
+		string $data, int &$offset): ElementBase
95
+	{
96
+		$idx = $offset;
97
+		$length = Length::expectFromDER($data, $idx)->intLength();
98
+		$str = substr($data, $idx, $length);
99
+		$idx += $length;
100
+		/** @var $match string[] */
101
+		if (!preg_match(self::REGEX, $str, $match)) {
102
+			throw new DecodeException("Invalid GeneralizedTime format.");
103
+		}
104
+		list(, $year, $month, $day, $hour, $minute, $second) = $match;
105
+		if (isset($match[7])) {
106
+			$frac = $match[7];
107
+			// DER restricts trailing zeroes in fractional seconds component
108
+			if ('0' === $frac[strlen($frac) - 1]) {
109
+				throw new DecodeException(
110
+					"Fractional seconds must omit trailing zeroes.");
111
+			}
112
+			$frac = (int) $frac;
113
+		} else {
114
+			$frac = 0;
115
+		}
116
+		$time = $year . $month . $day . $hour . $minute . $second . "." . $frac .
117
+			self::TZ_UTC;
118
+		$dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time,
119
+			self::_createTimeZone(self::TZ_UTC));
120
+		if (!$dt) {
121
+			throw new DecodeException(
122
+				"Failed to decode GeneralizedTime: " .
123
+				self::_getLastDateTimeImmutableErrorsStr());
124
+		}
125
+		$offset = $idx;
126
+		return new self($dt);
127
+	}
128 128
 }
Please login to merge, or discard this patch.