1 | <?php |
||
13 | class Length implements Encodable |
||
14 | { |
||
15 | /** |
||
16 | * Length. |
||
17 | * |
||
18 | * @var BigInt |
||
19 | */ |
||
20 | private $_length; |
||
21 | |||
22 | /** |
||
23 | * Whether length is indefinite. |
||
24 | * |
||
25 | * @var bool |
||
26 | */ |
||
27 | private $_indefinite; |
||
28 | |||
29 | /** |
||
30 | * Constructor. |
||
31 | * |
||
32 | * @param int|string $length Length |
||
33 | * @param bool $indefinite Whether length is indefinite |
||
34 | */ |
||
35 | 352 | 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 | 245 | public static function fromDER(string $data, int &$offset = null): self |
|
81 | |||
82 | /** |
||
83 | * Decode long form length. |
||
84 | * |
||
85 | * @param int $length Number of octets |
||
86 | * @param string $data Data |
||
87 | * @param int $offset Reference to the variable containing offset to the |
||
88 | * data. |
||
89 | * @throws DecodeException If decoding fails |
||
90 | * @return string Integer as a string |
||
91 | */ |
||
92 | 7 | private static function _decodeLongFormLength(int $length, string $data, |
|
107 | |||
108 | /** |
||
109 | * Decode length from DER. |
||
110 | * |
||
111 | * Throws an exception if length doesn't match with expected or if data |
||
112 | * doesn't contain enough bytes. |
||
113 | * |
||
114 | * Requirement of definite length is relaxed contrary to the specification |
||
115 | * (sect. 10.1). |
||
116 | * |
||
117 | * @see self::fromDER |
||
118 | * @param string $data DER data |
||
119 | * @param int $offset Reference to the offset variable |
||
120 | * @param int|null $expected Expected length, null to bypass checking |
||
121 | * @throws DecodeException If decoding or expectation fails |
||
122 | * @return self |
||
123 | */ |
||
124 | 232 | public static function expectFromDER(string $data, int &$offset, |
|
151 | |||
152 | /** |
||
153 | * |
||
154 | * @see Encodable::toDER() |
||
155 | * @throws \DomainException If length is too large to encode |
||
156 | * @return string |
||
157 | */ |
||
158 | 138 | public function toDER(): string |
|
186 | |||
187 | /** |
||
188 | * Get the length. |
||
189 | * |
||
190 | * @throws \LogicException If length is indefinite |
||
191 | * @return string Length as an integer string |
||
192 | */ |
||
193 | 5 | public function length(): string |
|
200 | |||
201 | /** |
||
202 | * Get the length as an integer. |
||
203 | * |
||
204 | * @throws \LogicException If length is indefinite |
||
205 | * @throws \RuntimeException If length overflows integer size |
||
206 | * @return int |
||
207 | */ |
||
208 | 234 | public function intLength(): int |
|
215 | |||
216 | /** |
||
217 | * Whether length is indefinite. |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | 234 | public function isIndefinite(): bool |
|
225 | } |
||
226 |