|
|
|
@ -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
|
|
|
|
|