1 | <?php |
||
13 | class Length implements Encodable |
||
14 | { |
||
15 | /** |
||
16 | * Length. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | private $_length; |
||
21 | |||
22 | /** |
||
23 | * Whether length is indefinite. |
||
24 | * |
||
25 | * @var boolean |
||
26 | */ |
||
27 | private $_indefinite; |
||
28 | |||
29 | /** |
||
30 | * Length as an integer type. |
||
31 | * |
||
32 | * @internal Lazily initialized |
||
33 | * @var int |
||
34 | */ |
||
35 | private $_intLength; |
||
36 | |||
37 | /** |
||
38 | * Constructor. |
||
39 | * |
||
40 | * @param int|string $length Length |
||
41 | * @param boolean $indefinite Whether length is indefinite |
||
42 | */ |
||
43 | 309 | public function __construct($length, bool $indefinite = false) |
|
48 | |||
49 | /** |
||
50 | * Decode length component from DER data. |
||
51 | * |
||
52 | * @param string $data DER encoded data |
||
53 | * @param int|null $offset Reference to the variable that contains offset |
||
54 | * into the data where to start parsing. Variable is updated to |
||
55 | * the offset next to the parsed length component. If null, start |
||
56 | * from offset 0. |
||
57 | * @throws DecodeException If decoding fails |
||
58 | * @return self |
||
59 | */ |
||
60 | 212 | public static function fromDER(string $data, int &$offset = null): self |
|
87 | |||
88 | /** |
||
89 | * Decode long form length. |
||
90 | * |
||
91 | * @param int $length Number of octets |
||
92 | * @param string $data Data |
||
93 | * @param int $offset Reference to the variable containing offset to the |
||
94 | * data. |
||
95 | * @throws DecodeException If decoding fails |
||
96 | * @return string Integer as a string |
||
97 | */ |
||
98 | 7 | private static function _decodeLongFormLength(int $length, string $data, |
|
114 | |||
115 | /** |
||
116 | * Decode length from DER. |
||
117 | * |
||
118 | * Throws an exception if length doesn't match with expected or if data |
||
119 | * doesn't contain enough bytes. |
||
120 | * |
||
121 | * @see self::fromDER |
||
122 | * @param string $data DER data |
||
123 | * @param int $offset Reference to the offset variable |
||
124 | * @param int|null $expected Expected length, null to bypass checking |
||
125 | * @throws DecodeException If decoding or expectation fails |
||
126 | * @return self |
||
127 | */ |
||
128 | 199 | public static function expectFromDER(string $data, int &$offset, |
|
152 | |||
153 | /** |
||
154 | * |
||
155 | * @see Encodable::toDER() |
||
156 | * @throws \DomainException If length is too large to encode |
||
157 | * @return string |
||
158 | */ |
||
159 | 118 | public function toDER(): string |
|
187 | |||
188 | /** |
||
189 | * Get the length. |
||
190 | * |
||
191 | * @throws \LogicException If length is indefinite |
||
192 | * @return string Length as an integer string |
||
193 | */ |
||
194 | 205 | public function length(): string |
|
201 | |||
202 | /** |
||
203 | * Get the length as an integer. |
||
204 | * |
||
205 | * @throws \LogicException If length is indefinite |
||
206 | * @throws \RuntimeException If length overflows integer size |
||
207 | * @return int |
||
208 | */ |
||
209 | 202 | public function intLength(): int |
|
220 | |||
221 | /** |
||
222 | * Whether length is indefinite. |
||
223 | * |
||
224 | * @return boolean |
||
225 | */ |
||
226 | 201 | public function isIndefinite(): bool |
|
230 | } |
||
231 |