ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Bigint.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types=1);
3 
4 namespace ZipStream;
5 
7 
8 class Bigint
9 {
13  private $bytes = [0, 0, 0, 0, 0, 0, 0, 0];
14 
20  public function __construct(int $value = 0)
21  {
22  $this->fillBytes($value, 0, 8);
23  }
24 
33  protected function fillBytes(int $value, int $start, int $count): void
34  {
35  for ($i = 0; $i < $count; $i++) {
36  $this->bytes[$start + $i] = $i >= PHP_INT_SIZE ? 0 : $value & 0xFF;
37  $value >>= 8;
38  }
39  }
40 
47  public static function init(int $value = 0): self
48  {
49  return new self($value);
50  }
51 
59  public static function fromLowHigh(int $low, int $high): self
60  {
61  $bigint = new Bigint();
62  $bigint->fillBytes($low, 0, 4);
63  $bigint->fillBytes($high, 4, 4);
64  return $bigint;
65  }
66 
72  public function getHigh32(): int
73  {
74  return $this->getValue(4, 4);
75  }
76 
84  public function getValue(int $end = 0, int $length = 8): int
85  {
86  $result = 0;
87  for ($i = $end + $length - 1; $i >= $end; $i--) {
88  $result <<= 8;
89  $result |= $this->bytes[$i];
90  }
91  return $result;
92  }
93 
100  public function getLowFF(bool $force = false): float
101  {
102  if ($force || $this->isOver32()) {
103  return (float)0xFFFFFFFF;
104  }
105  return (float)$this->getLow32();
106  }
107 
114  public function isOver32(bool $force = false): bool
115  {
116  // value 0xFFFFFFFF already needs a Zip64 header
117  return $force ||
118  max(array_slice($this->bytes, 4, 4)) > 0 ||
119  min(array_slice($this->bytes, 0, 4)) === 0xFF;
120  }
121 
127  public function getLow32(): int
128  {
129  return $this->getValue(0, 4);
130  }
131 
137  public function getHex64(): string
138  {
139  $result = '0x';
140  for ($i = 7; $i >= 0; $i--) {
141  $result .= sprintf('%02X', $this->bytes[$i]);
142  }
143  return $result;
144  }
145 
152  public function add(Bigint $other): Bigint
153  {
154  $result = clone $this;
155  $overflow = false;
156  for ($i = 0; $i < 8; $i++) {
157  $result->bytes[$i] += $other->bytes[$i];
158  if ($overflow) {
159  $result->bytes[$i]++;
160  $overflow = false;
161  }
162  if ($result->bytes[$i] & 0x100) {
163  $overflow = true;
164  $result->bytes[$i] &= 0xFF;
165  }
166  }
167  if ($overflow) {
168  throw new OverflowException;
169  }
170  return $result;
171  }
172 }
Class Version .
Definition: Bigint.php:4
$result
getValue(int $end=0, int $length=8)
Get value from bytes array.
Definition: Bigint.php:84
static init(int $value=0)
Get an instance.
Definition: Bigint.php:47
isOver32(bool $force=false)
Check if is over 32.
Definition: Bigint.php:114
static fromLowHigh(int $low, int $high)
Fill bytes from low to high.
Definition: Bigint.php:59
fillBytes(int $value, int $start, int $count)
Fill the bytes field with int.
Definition: Bigint.php:33
add(Bigint $other)
Add.
Definition: Bigint.php:152
$start
Definition: bench.php:8
getLowFF(bool $force=false)
Get low FF.
Definition: Bigint.php:100
getLow32()
Get low 32.
Definition: Bigint.php:127
__construct(int $value=0)
Initialize the bytes array.
Definition: Bigint.php:20
getHex64()
Get hexadecimal.
Definition: Bigint.php:137
getHigh32()
Get high 32.
Definition: Bigint.php:72
$i
Definition: disco.tpl.php:19