2016-11-03 18:16:01 -04:00
|
|
|
package ber
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-10-15 15:27:33 -04:00
|
|
|
"fmt"
|
2016-11-03 18:16:01 -04:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
func readHeader(reader io.Reader) (identifier Identifier, length int, read int, err error) {
|
2020-10-15 15:27:33 -04:00
|
|
|
var (
|
|
|
|
c, l int
|
|
|
|
i Identifier
|
|
|
|
)
|
|
|
|
|
|
|
|
if i, c, err = readIdentifier(reader); err != nil {
|
2016-11-03 18:16:01 -04:00
|
|
|
return Identifier{}, 0, read, err
|
|
|
|
}
|
2020-10-15 15:27:33 -04:00
|
|
|
identifier = i
|
|
|
|
read += c
|
2016-11-03 18:16:01 -04:00
|
|
|
|
2020-10-15 15:27:33 -04:00
|
|
|
if l, c, err = readLength(reader); err != nil {
|
2016-11-03 18:16:01 -04:00
|
|
|
return Identifier{}, 0, read, err
|
|
|
|
}
|
2020-10-15 15:27:33 -04:00
|
|
|
length = l
|
|
|
|
read += c
|
2016-11-03 18:16:01 -04:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2020-10-15 15:27:33 -04:00
|
|
|
if length < LengthIndefinite {
|
|
|
|
err = fmt.Errorf("length cannot be less than %d", LengthIndefinite)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:16:01 -04:00
|
|
|
return identifier, length, read, nil
|
|
|
|
}
|