github.com/metacubex/quic-go@v0.44.1-0.20240520163451-20b689a59136/internal/wire/new_token_frame.go (about)

     1  package wire
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  
     7  	"github.com/metacubex/quic-go/internal/protocol"
     8  	"github.com/metacubex/quic-go/quicvarint"
     9  )
    10  
    11  // A NewTokenFrame is a NEW_TOKEN frame
    12  type NewTokenFrame struct {
    13  	Token []byte
    14  }
    15  
    16  func parseNewTokenFrame(b []byte, _ protocol.Version) (*NewTokenFrame, int, error) {
    17  	tokenLen, l, err := quicvarint.Parse(b)
    18  	if err != nil {
    19  		return nil, 0, replaceUnexpectedEOF(err)
    20  	}
    21  	b = b[l:]
    22  	if tokenLen == 0 {
    23  		return nil, 0, errors.New("token must not be empty")
    24  	}
    25  	if uint64(len(b)) < tokenLen {
    26  		return nil, 0, io.EOF
    27  	}
    28  	token := make([]byte, int(tokenLen))
    29  	copy(token, b)
    30  	return &NewTokenFrame{Token: token}, l + int(tokenLen), nil
    31  }
    32  
    33  func (f *NewTokenFrame) Append(b []byte, _ protocol.Version) ([]byte, error) {
    34  	b = append(b, newTokenFrameType)
    35  	b = quicvarint.Append(b, uint64(len(f.Token)))
    36  	b = append(b, f.Token...)
    37  	return b, nil
    38  }
    39  
    40  // Length of a written frame
    41  func (f *NewTokenFrame) Length(protocol.Version) protocol.ByteCount {
    42  	return 1 + protocol.ByteCount(quicvarint.Len(uint64(len(f.Token)))+len(f.Token))
    43  }