1 | <?php |
||
13 | class Length implements Encodable |
||
14 | { |
||
15 | /** |
||
16 | * Length. |
||
17 | * |
||
18 | * @var int|string |
||
19 | */ |
||
20 | private $_length; |
||
21 | |||
22 | /** |
||
23 | * Whether length is indefinite. |
||
24 | * |
||
25 | * @var boolean |
||
26 | */ |
||
27 | private $_indefinite; |
||
28 | |||
29 | /** |
||
30 | * Constructor. |
||
31 | * |
||
32 | * @param int|string $length Length |
||
33 | * @param boolean $indefinite Whether length is indefinite |
||
34 | */ |
||
35 | 309 | public function __construct($length, bool $indefinite = false) |
|
40 | |||
41 | /** |
||
42 | * Decode length component from DER data. |
||
43 | * |
||
44 | * @param string $data DER encoded data |
||
45 | * @param int|null $offset Reference to the variable that contains offset |
||
46 | * into the data where to start parsing. Variable is updated to |
||
47 | * the offset next to the parsed length component. If null, start |
||
48 | * from offset 0. |
||
49 | * @throws DecodeException If decoding fails |
||
50 | * @return self |
||
51 | */ |
||
52 | 212 | public static function fromDER(string $data, int &$offset = null): self |
|
79 | |||
80 | /** |
||
81 | * Decode long form length. |
||
82 | * |
||
83 | * @param int $length Number of octets |
||
84 | * @param string $data Data |
||
85 | * @param int $offset Reference to the variable containing offset to the |
||
86 | * data. |
||
87 | * @throws DecodeException If decoding fails |
||
88 | * @return string Integer as a string |
||
89 | */ |
||
90 | 7 | private static function _decodeLongFormLength(int $length, string $data, |
|
106 | |||
107 | /** |
||
108 | * Decode length from DER. |
||
109 | * |
||
110 | * Throws an exception if length doesn't match with expected or if data |
||
111 | * doesn't contain enough bytes. |
||
112 | * |
||
113 | * @see self::fromDER |
||
114 | * @param string $data DER data |
||
115 | * @param int $offset Reference to the offset variable |
||
116 | * @param int|null $expected Expected length, null to bypass checking |
||
117 | * @throws DecodeException If decoding or expectation fails |
||
118 | * @return self |
||
119 | */ |
||
120 | 199 | public static function expectFromDER(string $data, int &$offset, |
|
144 | |||
145 | /** |
||
146 | * |
||
147 | * @see Encodable::toDER() |
||
148 | * @throws \DomainException If length is too large to encode |
||
149 | * @return string |
||
150 | */ |
||
151 | 118 | public function toDER(): string |
|
179 | |||
180 | /** |
||
181 | * Get the length. |
||
182 | * |
||
183 | * @throws \LogicException If length is indefinite |
||
184 | * @return int|string |
||
185 | */ |
||
186 | 5 | public function length() |
|
193 | |||
194 | /** |
||
195 | * @return int |
||
196 | */ |
||
197 | 178 | public function intVal(): int |
|
209 | |||
210 | /** |
||
211 | * Whether length is indefinite. |
||
212 | * |
||
213 | * @return boolean |
||
214 | */ |
||
215 | 201 | public function isIndefinite(): bool |
|
219 | } |
||
220 |