public static function expectFromDER(string $data, int &$offset,
        int $expected = null): self
    {
        $idx = $offset;
        $length = self::fromDER($data, $idx);
        // if certain length was expected
        if (isset($expected)) {
            if ($length->isIndefinite()) {
                throw new DecodeException('Expected length %d, got indefinite.',
                    $expected);
            }
            if ($expected != $length->intLength()) {
                throw new DecodeException(
                    sprintf("Expected length %d, got %d.", $expected,
                        $length->intLength()));
            }
        }
        // check that enough data is available
        if (!$length->isIndefinite() &&
            strlen($data) < $idx + $length->intLength()) {
            throw new DecodeException(
                sprintf("Length %d overflows data, %d bytes left.",
                    $length->intLength(), strlen($data) - $idx));
        }
        $offset = $idx;
        return $length;
    }
public static function expectFromDER(string $data, int &$offset,
        int $expected = null): self
    {
        $idx = $offset;
        $length = self::fromDER($data, $idx);
        // if certain length was expected
        if (isset($expected)) {
            if ($length->isIndefinite()) {
                throw new DecodeException('Expected length %d, got indefinite.',
                    $expected);
            }
            if ($expected != $length->intLength()) {
                throw new DecodeException(
                    sprintf("Expected length %d, got %d.", $expected,
                        $length->intLength()));
            }
        }
        // check that enough data is available
        if (!$length->isIndefinite() &&
            strlen($data) < $idx + $length->intLength()) {
            throw new DecodeException(
                sprintf("Length %d overflows data, %d bytes left.",
                    $length->intLength(), strlen($data) - $idx));
        }
        $offset = $idx;
        return $length;
    }
public static function expectFromDER ( string $ data , int & $ offset , int $ expected = null ) : self { $ idx = $ offset ; $ length = self :: fromDER ( $ data , $ idx ) ; // if certain length was expected if ( isset ( $ expected ) ) { if ( $ length -> isIndefinite ( ) ) { throw new DecodeException ( 'Expected length %d, got indefinite.' , $ expected ) ; } if ( $ expected != $ length -> intLength ( ) ) { throw new DecodeException ( sprintf ( "Expected length %d, got %d." , $ expected , $ length -> intLength ( ) ) ) ; } } // check that enough data is available if ( ! $ length -> isIndefinite ( ) && strlen ( $ data ) < $ idx + $ length -> intLength ( ) ) { throw new DecodeException ( sprintf ( "Length %d overflows data, %d bytes left." , $ length -> intLength ( ) , strlen ( $ data ) - $ idx ) ) ; } $ offset = $ idx ; return $ length ; }
Decode length from DER.

Throws an exception if length doesn't match with expected or if data
doesn't contain enough bytes.

Requirement of definite length is relaxed contrary to the specification
(sect. 10.1).

@see self::fromDER
@param string $data DER data
@param int $offset Reference to the offset variable
@param int|null $expected Expected length, null to bypass checking
@throws DecodeException If decoding or expectation fails
@return self
Decode length from DER .