Skip to content

Commit f926137

Browse files
committed
performance improvement for integer
1 parent 9818a33 commit f926137

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

src/ASN1/Universal/Integer.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,14 @@ public function __get(string $name)
116116
public function setValue(Content $content)
117117
{
118118
$binaryData = $content->getBinary();
119-
$offsetIndex = 0;
120119
$contentLength = $this->contentLength->getLength();
121-
$isNegative = (\ord($binaryData[$offsetIndex]) & 0x80) != 0x00;
122-
$number = gmp_init(\ord($binaryData[$offsetIndex++]) & 0x7F, 10);
120+
$isNegative = (\ord($binaryData[0]) & 0x80) != 0x00;
123121

124-
for ($i = 0; $i < $contentLength - 1; $i++) {
125-
$number = gmp_or(gmp_mul($number, 0x100), \ord($binaryData[$offsetIndex++]));
126-
}
122+
$number = gmp_import($binaryData, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
127123

128124
if ($isNegative) {
129-
$number = gmp_sub($number, gmp_pow(2, 8 * $contentLength - 1));
125+
// For negative numbers in two's complement, we need to subtract 2^(bits)
126+
$number = gmp_sub($number, gmp_pow(2, 8 * $contentLength));
130127
}
131128

132129
$this->gmpValue = $number;

0 commit comments

Comments
 (0)