tlv: expose unknown values

This commit is contained in:
Joost Jager committed 2019-12-10 06:54:20 +01:00
1 parent 1ab3107df4
commit cbe213fd0c
4 files changed
+39 -24

No files matched your search

+4 -3
View File
@@ -12,9 +12,10 @@ import (
// Type is an 64-bit identifier for a TLV Record.
type Type uint64
// TypeSet is an unordered set of Types. The map item boolean values indicate
// whether the type that we parsed was known.
type TypeSet map[Type]bool
// TypeMap is a map of parsed Types. The map values are byte slices. If the byte
// slice is nil, the type was successfully parsed. Otherwise the value is byte
// slice containing the encoded data.
type TypeMap map[Type][]byte
// Encoder is a signature for methods that can encode TLV values. An error
// should be returned if the Encoder cannot support the underlying type of val.
+25 -11
View File
@@ -1,6 +1,7 @@
package tlv
import (
"bytes"
"errors"
"io"
"io/ioutil"
@@ -139,16 +140,16 @@ func (s *Stream) Decode(r io.Reader) error {
}
// DecodeWithParsedTypes is identical to Decode, but if successful, returns a
// TypeSet containing the types of all records that were decoded or ignored from
// TypeMap containing the types of all records that were decoded or ignored from
// the stream.
func (s *Stream) DecodeWithParsedTypes(r io.Reader) (TypeSet, error) {
return s.decode(r, make(TypeSet))
func (s *Stream) DecodeWithParsedTypes(r io.Reader) (TypeMap, error) {
return s.decode(r, make(TypeMap))
}
// decode is a helper function that performs the basis of stream decoding. If
// the caller needs the set of parsed types, it must provide an initialized
// parsedTypes, otherwise the returned TypeSet will be nil.
func (s *Stream) decode(r io.Reader, parsedTypes TypeSet) (TypeSet, error) {
// parsedTypes, otherwise the returned TypeMap will be nil.
func (s *Stream) decode(r io.Reader, parsedTypes TypeMap) (TypeMap, error) {
var (
typ Type
min Type
@@ -230,10 +231,25 @@ func (s *Stream) decode(r io.Reader, parsedTypes TypeSet) (TypeSet, error) {
return nil, err
}
// Record the successfully decoded type if the caller
// provided an initialized TypeMap.
if parsedTypes != nil {
parsedTypes[typ] = nil
}
// Otherwise, the record type is unknown and is odd, discard the
// number of bytes specified by length.
default:
_, err := io.CopyN(ioutil.Discard, r, int64(length))
// If the caller provided an initialized TypeMap, record
// the encoded bytes.
var b *bytes.Buffer
writer := ioutil.Discard
if parsedTypes != nil {
b = bytes.NewBuffer(make([]byte, 0, length))
writer = b
}
_, err := io.CopyN(writer, r, int64(length))
switch {
// We'll convert any EOFs to ErrUnexpectedEOF, since this
@@ -245,12 +261,10 @@ func (s *Stream) decode(r io.Reader, parsedTypes TypeSet) (TypeSet, error) {
case err != nil:
return nil, err
}
}
// Record the successfully decoded or ignored type if the
// caller provided an initialized TypeSet.
if parsedTypes != nil {
parsedTypes[typ] = ok
if parsedTypes != nil {
parsedTypes[typ] = b.Bytes()
}
}
// Update our record index so that we can begin our next search
+6 -6
View File
@@ -12,7 +12,7 @@ type parsedTypeTest struct {
name string
encode []tlv.Type
decode []tlv.Type
expParsedTypes tlv.TypeSet
expParsedTypes tlv.TypeMap
}
// TestParsedTypes asserts that a Stream will properly return the set of types
@@ -29,17 +29,17 @@ func TestParsedTypes(t *testing.T) {
name: "known and unknown",
encode: []tlv.Type{knownType, unknownType},
decode: []tlv.Type{knownType},
expParsedTypes: tlv.TypeSet{
unknownType: false,
knownType: true,
expParsedTypes: tlv.TypeMap{
unknownType: []byte{0, 0, 0, 0, 0, 0, 0, 0},
knownType: nil,
},
},
{
name: "known and missing known",
encode: []tlv.Type{knownType},
decode: []tlv.Type{knownType, secondKnownType},
expParsedTypes: tlv.TypeSet{
knownType: true,
expParsedTypes: tlv.TypeMap{
knownType: nil,
},
},
}