header.go 779 B

1234567891011121314151617181920212223242526272829303132333435
  1. package ber
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. )
  7. func readHeader(reader io.Reader) (identifier Identifier, length int, read int, err error) {
  8. if i, c, err := readIdentifier(reader); err != nil {
  9. return Identifier{}, 0, read, err
  10. } else {
  11. identifier = i
  12. read += c
  13. }
  14. if l, c, err := readLength(reader); err != nil {
  15. return Identifier{}, 0, read, err
  16. } else {
  17. length = l
  18. read += c
  19. }
  20. // Validate length type with identifier (x.600, 8.1.3.2.a)
  21. if length == LengthIndefinite && identifier.TagType == TypePrimitive {
  22. return Identifier{}, 0, read, errors.New("indefinite length used with primitive type")
  23. }
  24. if length < LengthIndefinite {
  25. err = fmt.Errorf("length cannot be less than %d", LengthIndefinite)
  26. return
  27. }
  28. return identifier, length, read, nil
  29. }