summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/go-asn1-ber/asn1-ber/header.go
blob: 7dfa6b9a7ddb6c3ca94ec4dd564779829033757c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package ber

import (
	"errors"
	"fmt"
	"io"
)

func readHeader(reader io.Reader) (identifier Identifier, length int, read int, err error) {
	var (
		c, l int
		i    Identifier
	)

	if i, c, err = readIdentifier(reader); err != nil {
		return Identifier{}, 0, read, err
	}
	identifier = i
	read += c

	if l, c, err = readLength(reader); err != nil {
		return Identifier{}, 0, read, err
	}
	length = l
	read += c

	// Validate length type with identifier (x.600, 8.1.3.2.a)
	if length == LengthIndefinite && identifier.TagType == TypePrimitive {
		return Identifier{}, 0, read, errors.New("indefinite length used with primitive type")
	}

	if length < LengthIndefinite {
		err = fmt.Errorf("length cannot be less than %d", LengthIndefinite)
		return
	}

	return identifier, length, read, nil
}