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