Skip to content

Commit

Permalink
tlv: DecodingBuffer.IterElements
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Nov 27, 2024
1 parent a2fa005 commit f8bc12f
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions ndn/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (data Data) Field() tlv.Field {
func (data *Data) UnmarshalBinary(value []byte) (e error) {
*data = Data{}
d := tlv.DecodingBuffer(value)
for _, de := range d.Elements() {
for de := range d.IterElements() {
switch de.Type {
case an.TtName:
if e = de.UnmarshalValue(&data.Name); e != nil {
Expand Down Expand Up @@ -263,7 +263,7 @@ func (data *Data) UnmarshalBinary(value []byte) (e error) {

func (data *Data) decodeMetaInfo(value []byte) (e error) {
d := tlv.DecodingBuffer(value)
for _, de := range d.Elements() {
for de := range d.IterElements() {
switch de.Type {
case an.TtContentType:
if data.ContentType = ContentType(de.UnmarshalNNI(math.MaxUint64, &e, tlv.ErrRange)); e != nil {
Expand Down
4 changes: 2 additions & 2 deletions ndn/interest.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (interest *Interest) UnmarshalBinary(wire []byte) (e error) {
*interest = Interest{}
d := tlv.DecodingBuffer(wire)
var paramsPortion []byte
for _, de := range d.Elements() {
for de := range d.IterElements() {
switch de.Type {
case an.TtName:
if e = de.UnmarshalValue(&interest.Name); e != nil {
Expand Down Expand Up @@ -332,7 +332,7 @@ func (fh ForwardingHint) Field() tlv.Field {
// UnmarshalBinary decodes from TLV-VALUE.
func (fh *ForwardingHint) UnmarshalBinary(wire []byte) error {
d := tlv.DecodingBuffer(wire)
for _, de := range d.Elements() {
for de := range d.IterElements() {
switch de.Type {
case an.TtName:
var del Name
Expand Down
2 changes: 1 addition & 1 deletion ndn/keychain/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (vp ValidityPeriod) Field() tlv.Field {
func (vp *ValidityPeriod) UnmarshalBinary(wire []byte) (e error) {
*vp = ValidityPeriod{}
d := tlv.DecodingBuffer(wire)
for _, de := range d.Elements() {
for de := range d.IterElements() {
switch de.Type {
case an.TtNotBefore:
vp.NotBefore = parseValidityPeriodTime(de.Value)
Expand Down
2 changes: 1 addition & 1 deletion ndn/keychain/safebag.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func ImportSafeBag(wire, passphrase []byte) (pvt PrivateKey, cert *Certificate,

var pvtKey any
d := tlv.DecodingBuffer(safeBagTLV.Value)
for _, de := range d.Elements() {
for de := range d.IterElements() {
switch de.Type {
case an.TtData:
var data ndn.Data
Expand Down
2 changes: 1 addition & 1 deletion ndn/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (pkt *Packet) UnmarshalTLV(typ uint32, value []byte) (e error) {
func (pkt *Packet) decodeValue(value []byte) (e error) {
fragment := LpFragment{FragCount: 1}
d := tlv.DecodingBuffer(value)
for _, de := range d.Elements() {
for de := range d.IterElements() {
switch de.Type {
case an.TtLpSeqNum:
if de.Length() != 8 {
Expand Down
2 changes: 1 addition & 1 deletion ndn/rdr/rdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (m *Metadata) Decode(value []byte, extensions MetadataDecoderMap) error {
*m = Metadata{}
d := tlv.DecodingBuffer(value)
hasName := false
for _, de := range d.Elements() {
for de := range d.IterElements() {
var f MetadataFieldDecoder
if de.Type == an.TtName && !hasName {
f = m.decodeName
Expand Down
4 changes: 2 additions & 2 deletions ndn/sig-info.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (kl KeyLocator) Field() tlv.Field {
func (kl *KeyLocator) UnmarshalBinary(wire []byte) error {
*kl = KeyLocator{}
d := tlv.DecodingBuffer(wire)
for _, de := range d.Elements() {
for de := range d.IterElements() {
switch de.Type {
case an.TtName:
if e := de.UnmarshalValue(&kl.Name); e != nil {
Expand Down Expand Up @@ -102,7 +102,7 @@ func (si *SigInfo) FindExtension(typ uint32) *tlv.Element {
func (si *SigInfo) UnmarshalBinary(wire []byte) (e error) {
*si = SigInfo{}
d := tlv.DecodingBuffer(wire)
for _, de := range d.Elements() {
for de := range d.IterElements() {
switch de.Type {
case an.TtSigType:
if si.Type = uint32(de.UnmarshalNNI(math.MaxUint32, &e, ErrSigType)); e != nil {
Expand Down
29 changes: 20 additions & 9 deletions ndn/tlv/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package tlv

import (
"encoding"
"iter"
"slices"
)

// Unmarshaler is the interface implemented by an object that can decode an TLV element representation of itself.
Expand Down Expand Up @@ -58,17 +60,26 @@ func (d *DecodingBuffer) Element() (de DecodingElement, e error) {
return de, nil
}

// Elements recognizes TLV elements from the buffer.
// Bytes that cannot be recognized as TLV elements are left in the decoder.
func (d *DecodingBuffer) Elements() (list []DecodingElement) {
for {
de, e := d.Element()
if e != nil {
break
// IterElements returns an iterator that recognizes TLV elements from the buffer.
//
// Iteration stops when no more TLV elements can be recognized.
// Remaining bytes are left in the decoder.
func (d *DecodingBuffer) IterElements() iter.Seq[DecodingElement] {
return func(yield func(DecodingElement) bool) {
for {
de, e := d.Element()
if e != nil || !yield(de) {
return
}
}
list = append(list, de)
}
return list
}

// Elements recognizes TLV elements from the buffer.
//
// Remaining bytes that cannot be recognized as TLV elements are left in the decoder.
func (d *DecodingBuffer) Elements() []DecodingElement {
return slices.Collect(d.IterElements())
}

// DecodingElement represents an TLV element during decoding.
Expand Down
2 changes: 1 addition & 1 deletion ndni/datagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (gen *DataGen) Init(m *pktmbuf.Packet, args ...any) {
}

d := tlv.DecodingBuffer(wire)
for _, de := range d.Elements() {
for de := range d.IterElements() {
switch de.Type {
case an.TtName:
gen.suffix = C.LName{
Expand Down
2 changes: 1 addition & 1 deletion ndni/interest-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (tpl *InterestTemplate) Init(args ...any) {
*tpl = InterestTemplate{}

d := tlv.DecodingBuffer(wire)
for _, de := range d.Elements() {
for de := range d.IterElements() {
switch de.Type {
case an.TtName:
tpl.prefixL = C.uint16_t(copy(cptr.AsByteSlice(tpl.prefixV[:]), de.Value))
Expand Down

0 comments on commit f8bc12f

Please sign in to comment.